christiansandberg / canopen

CANopen for Python
http://canopen.readthedocs.io/
MIT License
423 stars 192 forks source link

Mapping multiple 16 bit values to TPDO causes error #397

Open mikbeng opened 9 months ago

mikbeng commented 9 months ago

Hi,

I have a problem when trying to map more than two 16bit values to a single TPDO.

Specifically, when I've configured my mapping and call node.tpdo.save(), get the error: KeyError: 'Subindex 3 was not found'

I've tried to map two 32 bit variables to the same TPDO and that works. A combination of one 32bit variable and one 16bit variable also works.

Here is my code that reproduces the error:

import canopen
import time
import logging

logging.basicConfig(level=logging.DEBUG)  # This will show all messages from DEBUG and above

network = canopen.Network()
network.connect(bustype='kvaser', channel=0, bitrate=500000)
node = network.add_node(99, 'Baumuller_CANopen/eds/03_10_158.eds')
print("Manufacturer Device Name = {}".format(node.sdo[0x1008].phys))

# Read current PDO configuration
node.tpdo.read()

# Do some changes to TPDO2
node.tpdo[2].clear()
node.tpdo[2].add_variable(0x6044,0)     #vl_control_effort - 16bit value
node.tpdo[2].add_variable(0x6072,0)     #Max Torque - 16bit value
node.tpdo[2].add_variable(0x414C,0)     #Iq setpoint - 16bit value
node.tpdo[2].trans_type = 254
node.tpdo[2].event_timer = 1000
node.tpdo[2].enabled = True

# Save new configuration (node must be in pre-operational)
node.nmt.state = 'PRE-OPERATIONAL'
node.tpdo.save()
node.nmt.state = 'OPERATIONAL'

# Using a callback to asynchronously receive values
# Do not do any blocking operations here!
def print_func(message):
    print('%s received' % message.name)
    for var in message:
        print('%s = %d' % (var.name, var.raw))

node.tpdo[2].add_callback(print_func)
time.sleep(3)

And here is the terminal output:

G:\dev> python .\baumuller_canopen\test\test_canopen_pdo2.py
DEBUG:can:can config: {'bitrate': 500000, 'interface': 'kvaser', 'channel': 0}
INFO:can.kvaser:loaded kvaser's CAN library
DEBUG:can.kvaser:Initializing Kvaser CAN library
DEBUG:can.kvaser:CAN library initialized
INFO:can.kvaser:CAN Filters: None
INFO:can.kvaser:Got configuration of: {'bitrate': 500000}
DEBUG:can.kvaser:Initialising bus instance
INFO:can.kvaser:Found 3 available channels
INFO:can.kvaser:0: Kvaser Leaf Light v2, S/N 20414 (#1)
INFO:can.kvaser:1: Kvaser Virtual CAN Driver, S/N 0 (#1)
INFO:can.kvaser:2: Kvaser Virtual CAN Driver, S/N 0 (#2)
DEBUG:can.kvaser:Creating read handle to bus channel: 0
DEBUG:can.kvaser:Creating separate handle for TX on channel: 0
DEBUG:can.kvaser:Going bus on TX handle
INFO:can.kvaser:Hardware filtering has been disabled
INFO:canopen.network:Connected to 'Kvaser Leaf Light v2, S/N 20414 (#1)'
DEBUG:canopen.pdo:TPDO Map as 4
DEBUG:canopen.pdo:RPDO Map as 4
DEBUG:canopen.sdo.client:Reading 0x1008:0 from node 99
DEBUG:canopen.sdo.client:Using segmented transfer of 11 bytes
DEBUG:canopen.variable:Value of manufacturer device name (0x1008:0) is 'bmaXX 4400'
Manufacturer Device Name = bmaXX 4400
DEBUG:canopen.sdo.client:Reading 0x1800:1 from node 99
DEBUG:canopen.variable:Value of transmit PDO communication parameter.COB-ID (0x1800:1) is 483
INFO:canopen.pdo.base:COB-ID is 0x1E3
INFO:canopen.pdo.base:PDO is enabled
INFO:canopen.pdo.base:RTR is allowed
DEBUG:canopen.sdo.client:Reading 0x1800:2 from node 99
DEBUG:canopen.variable:Value of transmit PDO communication parameter.transmission type (0x1800:2) is 255
INFO:canopen.pdo.base:Transmission type is 255
DEBUG:canopen.sdo.client:Reading 0x1800:3 from node 99
DEBUG:canopen.variable:Value of transmit PDO communication parameter.inhibit time (0x1800:3) is 0
INFO:canopen.pdo.base:Inhibit time is set to 0 ms
DEBUG:canopen.sdo.client:Reading 0x1800:5 from node 99
DEBUG:canopen.variable:Value of transmit PDO communication parameter.event timer (0x1800:5) is 0
INFO:canopen.pdo.base:Event timer is set to 0 ms
INFO:canopen.pdo.base:Could not read SYNC start value ('Subindex 6 was not found')
DEBUG:canopen.sdo.client:Reading 0x1A00:0 from node 99
DEBUG:canopen.variable:Value of transmit PDO mapping parameter.number of entries (0x1A00:0) is 1
DEBUG:canopen.sdo.client:Reading 0x1A00:1 from node 99
DEBUG:canopen.variable:Value of transmit PDO mapping parameter.PDO mapping entry (0x1A00:1) is 1614872592
INFO:canopen.pdo.base:Adding statusword (0x6041:0) at bits 0 - 15 to PDO map
INFO:canopen.pdo.base:Subscribing to enabled PDO 0x1E3 on the network
DEBUG:canopen.sdo.client:Reading 0x1801:1 from node 99
DEBUG:canopen.variable:Value of transmit PDO communication parameter.COB-ID (0x1801:1) is 2147484387
INFO:canopen.pdo.base:COB-ID is 0x2E3
INFO:canopen.pdo.base:PDO is disabled
INFO:canopen.pdo.base:RTR is allowed
DEBUG:canopen.sdo.client:Reading 0x1801:2 from node 99
DEBUG:canopen.variable:Value of transmit PDO communication parameter.transmission type (0x1801:2) is 254
INFO:canopen.pdo.base:Transmission type is 254
DEBUG:canopen.sdo.client:Reading 0x1801:3 from node 99
DEBUG:canopen.variable:Value of transmit PDO communication parameter.inhibit time (0x1801:3) is 0
INFO:canopen.pdo.base:Inhibit time is set to 0 ms
DEBUG:canopen.sdo.client:Reading 0x1801:5 from node 99
DEBUG:canopen.variable:Value of transmit PDO communication parameter.event timer (0x1801:5) is 1000
INFO:canopen.pdo.base:Event timer is set to 1000 ms
INFO:canopen.pdo.base:Could not read SYNC start value ('Subindex 6 was not found')
DEBUG:canopen.sdo.client:Reading 0x1A01:0 from node 99
DEBUG:canopen.variable:Value of transmit PDO mapping parameter.number of entries (0x1A01:0) is 0
DEBUG:canopen.sdo.client:Reading 0x1802:1 from node 99
DEBUG:canopen.variable:Value of transmit PDO communication parameter.COB-ID (0x1802:1) is 2147484643
INFO:canopen.pdo.base:COB-ID is 0x3E3
INFO:canopen.pdo.base:PDO is disabled
INFO:canopen.pdo.base:RTR is allowed
DEBUG:canopen.sdo.client:Reading 0x1802:2 from node 99
DEBUG:canopen.variable:Value of transmit PDO communication parameter.transmission type (0x1802:2) is 254
INFO:canopen.pdo.base:Transmission type is 254
DEBUG:canopen.sdo.client:Reading 0x1802:3 from node 99
DEBUG:canopen.variable:Value of transmit PDO communication parameter.inhibit time (0x1802:3) is 0
INFO:canopen.pdo.base:Inhibit time is set to 0 ms
DEBUG:canopen.sdo.client:Reading 0x1802:5 from node 99
DEBUG:canopen.variable:Value of transmit PDO communication parameter.event timer (0x1802:5) is 1000
INFO:canopen.pdo.base:Event timer is set to 1000 ms
INFO:canopen.pdo.base:Could not read SYNC start value ('Subindex 6 was not found')
DEBUG:canopen.sdo.client:Reading 0x1A02:0 from node 99
DEBUG:canopen.variable:Value of transmit PDO mapping parameter.number of entries (0x1A02:0) is 0
DEBUG:canopen.sdo.client:Reading 0x1803:1 from node 99
DEBUG:canopen.variable:Value of transmit PDO communication parameter.COB-ID (0x1803:1) is 1251
INFO:canopen.pdo.base:COB-ID is 0x4E3
INFO:canopen.pdo.base:PDO is enabled
INFO:canopen.pdo.base:RTR is allowed
DEBUG:canopen.sdo.client:Reading 0x1803:2 from node 99
DEBUG:canopen.variable:Value of transmit PDO communication parameter.transmission type (0x1803:2) is 254
INFO:canopen.pdo.base:Transmission type is 254
DEBUG:canopen.sdo.client:Reading 0x1803:3 from node 99
DEBUG:canopen.variable:Value of transmit PDO communication parameter.inhibit time (0x1803:3) is 0
INFO:canopen.pdo.base:Inhibit time is set to 0 ms
DEBUG:canopen.sdo.client:Reading 0x1803:5 from node 99
DEBUG:canopen.variable:Value of transmit PDO communication parameter.event timer (0x1803:5) is 0
INFO:canopen.pdo.base:Event timer is set to 0 ms
INFO:canopen.pdo.base:Could not read SYNC start value ('Subindex 6 was not found')
DEBUG:canopen.sdo.client:Reading 0x1A03:0 from node 99
DEBUG:canopen.variable:Value of transmit PDO mapping parameter.number of entries (0x1A03:0) is 2
DEBUG:canopen.sdo.client:Reading 0x1A03:1 from node 99
DEBUG:canopen.variable:Value of transmit PDO mapping parameter.PDO mapping entry (0x1A03:1) is 1614872592
INFO:canopen.pdo.base:Adding statusword (0x6041:0) at bits 0 - 15 to PDO map
DEBUG:canopen.sdo.client:Reading 0x1A03:2 from node 99
DEBUG:canopen.variable:Value of transmit PDO mapping parameter.PDO mapping entry (0x1A03:2) is 1617690656
INFO:canopen.pdo.base:Adding velocity_actual_value (0x606C:0) at bits 16 - 47 to PDO map
INFO:canopen.pdo.base:Subscribing to enabled PDO 0x4E3 on the network
INFO:canopen.pdo.base:Adding vl_control_effort (0x6044:0) at bits 0 - 15 to PDO map
INFO:canopen.pdo.base:Adding max_torque (0x6072:0) at bits 16 - 31 to PDO map
INFO:canopen.pdo.base:Adding IqSetLimited (0x414C:0) at bits 32 - 47 to PDO map
INFO:canopen.nmt:Changing NMT state on node 99 from INITIALISING to PRE-OPERATIONAL
INFO:canopen.nmt:Sending NMT command 0x80 to node 99
INFO:canopen.pdo.base:Setting COB-ID 0x1E3 and temporarily disabling PDO
DEBUG:canopen.variable:Writing transmit PDO communication parameter.COB-ID (0x1800:1) = 2147484131
INFO:canopen.pdo.base:Setting transmission type to 255
DEBUG:canopen.variable:Writing transmit PDO communication parameter.transmission type (0x1800:2) = 255
INFO:canopen.pdo.base:Setting inhibit time to 0 us
DEBUG:canopen.variable:Writing transmit PDO communication parameter.inhibit time (0x1800:3) = 0
INFO:canopen.pdo.base:Setting event timer to 0 ms
DEBUG:canopen.variable:Writing transmit PDO communication parameter.event timer (0x1800:5) = 0
DEBUG:canopen.variable:Writing transmit PDO mapping parameter.number of entries (0x1A00:0) = 0
INFO:canopen.pdo.base:Writing statusword (0x6041:0, 16 bits) to PDO map
DEBUG:canopen.variable:Writing transmit PDO mapping parameter.PDO mapping entry (0x1A00:1) = 1614872592
DEBUG:canopen.variable:Writing transmit PDO mapping parameter.number of entries (0x1A00:0) = 1
DEBUG:canopen.variable:Writing transmit PDO communication parameter.COB-ID (0x1800:1) = 483
INFO:canopen.pdo.base:Subscribing to enabled PDO 0x1E3 on the network
INFO:canopen.pdo.base:Setting COB-ID 0x2E3 and temporarily disabling PDO
DEBUG:canopen.variable:Writing transmit PDO communication parameter.COB-ID (0x1801:1) = 2147484387
INFO:canopen.pdo.base:Setting transmission type to 254
DEBUG:canopen.variable:Writing transmit PDO communication parameter.transmission type (0x1801:2) = 254
INFO:canopen.pdo.base:Setting inhibit time to 0 us
DEBUG:canopen.variable:Writing transmit PDO communication parameter.inhibit time (0x1801:3) = 0
INFO:canopen.pdo.base:Setting event timer to 1000 ms
DEBUG:canopen.variable:Writing transmit PDO communication parameter.event timer (0x1801:5) = 1000
DEBUG:canopen.variable:Writing transmit PDO mapping parameter.number of entries (0x1A01:0) = 0
INFO:canopen.pdo.base:Writing vl_control_effort (0x6044:0, 16 bits) to PDO map
DEBUG:canopen.variable:Writing transmit PDO mapping parameter.PDO mapping entry (0x1A01:1) = 1615069200
INFO:canopen.pdo.base:Writing max_torque (0x6072:0, 16 bits) to PDO map
DEBUG:canopen.variable:Writing transmit PDO mapping parameter.PDO mapping entry (0x1A01:2) = 1618083856
INFO:canopen.pdo.base:Writing IqSetLimited (0x414C:0, 16 bits) to PDO map
Traceback (most recent call last):
  File "G:\Min enhet\Chalmers\Exjobb 2.0\dev\baumuller_canopen\test\test_canopen_pdo2.py", line 26, in <module>
    node.tpdo.save()
  File "C:\Users\mbengt\AppData\Local\Programs\Python\Python311\Lib\site-packages\canopen\pdo\base.py", line 61, in save
    pdo_map.save()
  File "C:\Users\mbengt\AppData\Local\Programs\Python\Python311\Lib\site-packages\canopen\pdo\base.py", line 401, in save
    self.map_array[subindex].raw = (var.index << 16 |
    ~~~~~~~~~~~~~~^^^^^^^^^^
  File "C:\Users\mbengt\AppData\Local\Programs\Python\Python311\Lib\site-packages\canopen\sdo\base.py", line 89, in __getitem__
    return Variable(self.sdo_node, self.od[subindex])
                                   ~~~~~~~^^^^^^^^^^
  File "C:\Users\mbengt\AppData\Local\Programs\Python\Python311\Lib\site-packages\canopen\objectdictionary\__init__.py", line 180, in __getitem__
    raise KeyError("Subindex %s was not found" % subindex)
KeyError: 'Subindex 3 was not found'
friederschueler commented 1 month ago

Have you tried to save only your configured TPDO?

node.tpdo[2].save()

Because from your log, it seems that some later PDOs are producing this error, maybe they are missconfigured?