Skip to content

Commit 1909846

Browse files
committed
Replace mapping in class PDO with proper PdoMaps object.
The mechanisms to lookup objects, implemented in class PdoMaps, did not apply to the PDO class itself, because it simply used a dictionary instead of a PdoMaps object. That also violates the static typing rules. Make the PdoBase.maps attribute mandatory and accept only type PdoMaps. To allow a basically "empty" PdoMaps object, adjust its constructor to skip adding entries when neither offset parameter is given as non-zero. Instead, access the PdoMaps.maps attribute directly to inject the offset-based TX and RX PdoMap objects in the PDO class constructor. Add some explanation why relative indices cannot be used here.
1 parent d4d7fe2 commit 1909846

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

canopen/pdo/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,19 @@ class PDO(PdoBase):
2424
:param tpdo: TPDO object holding the Transmit PDO mappings
2525
"""
2626

27-
def __init__(self, node, rpdo, tpdo):
27+
def __init__(self, node, rpdo: PdoBase, tpdo: PdoBase):
2828
super(PDO, self).__init__(node)
2929
self.rx = rpdo.map
3030
self.tx = tpdo.map
3131

32-
self.map = {}
33-
# the object 0x1A00 equals to key '1' so we remove 1 from the key
32+
self.map = PdoMaps(0, 0, self)
33+
# Combine RX and TX entries, but only via mapping parameter index. Relative index
34+
# numbers would be ambiguous.
35+
# The object 0x1A00 equals to key '1' so we remove 1 from the key
3436
for key, value in self.rx.items():
35-
self.map[0x1600 + (key - 1)] = value
37+
self.map.maps[self.rx.map_offset + (key - 1)] = value
3638
for key, value in self.tx.items():
37-
self.map[0x1A00 + (key - 1)] = value
39+
self.map.maps[self.tx.map_offset + (key - 1)] = value
3840

3941

4042
class RPDO(PdoBase):

canopen/pdo/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class PdoBase(Mapping):
3434

3535
def __init__(self, node: Union[LocalNode, RemoteNode]):
3636
self.network: canopen.network.Network = canopen.network._UNINITIALIZED_NETWORK
37-
self.map: Optional[PdoMaps] = None
37+
self.map: PdoMaps # must initialize in derived classes
3838
self.node: Union[LocalNode, RemoteNode] = node
3939

4040
def __iter__(self):
@@ -158,6 +158,9 @@ def __init__(self, com_offset: int, map_offset: int, pdo_node: PdoBase, cob_base
158158
self.maps: dict[int, PdoMap] = {}
159159
self.com_offset = com_offset
160160
self.map_offset = map_offset
161+
if not com_offset and not map_offset:
162+
# Skip generating entries without parameter index offsets
163+
return
161164
for map_no in range(512):
162165
if com_offset + map_no in pdo_node.node.object_dictionary:
163166
new_map = PdoMap(

0 commit comments

Comments
 (0)