Skip to content

Commit adc15da

Browse files
Merge pull request #253 from acolomb/subscribe-pdo-without-read-or-save
2 parents 179022c + e3c0432 commit adc15da

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

canopen/pdo/__init__.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99

1010
class PDO(PdoBase):
11-
"""PDO Class for backwards compatibility
11+
"""PDO Class for backwards compatibility.
12+
1213
:param rpdo: RPDO object holding the Receive PDO mappings
1314
:param tpdo: TPDO object holding the Transmit PDO mappings
1415
"""
@@ -27,9 +28,11 @@ def __init__(self, node, rpdo, tpdo):
2728

2829

2930
class RPDO(PdoBase):
30-
"""PDO specialization for the Receive PDO enabling the transfer of data from the master to the node.
31+
"""Receive PDO to transfer data from somewhere to the represented node.
32+
3133
Properties 0x1400 to 0x1403 | Mapping 0x1600 to 0x1603.
32-
:param object node: Parent node for this object."""
34+
:param object node: Parent node for this object.
35+
"""
3336

3437
def __init__(self, node):
3538
super(RPDO, self).__init__(node)
@@ -38,8 +41,10 @@ def __init__(self, node):
3841

3942
def stop(self):
4043
"""Stop transmission of all RPDOs.
44+
4145
:raise TypeError: Exception is thrown if the node associated with the PDO does not
42-
support this function"""
46+
support this function.
47+
"""
4348
if isinstance(self.node, canopen.RemoteNode):
4449
for pdo in self.map.values():
4550
pdo.stop()
@@ -48,8 +53,11 @@ def stop(self):
4853

4954

5055
class TPDO(PdoBase):
51-
"""PDO specialization for the Transmit PDO enabling the transfer of data from the node to the master.
52-
Properties 0x1800 to 0x1803 | Mapping 0x1A00 to 0x1A03."""
56+
"""Transmit PDO to broadcast data from the represented node to the network.
57+
58+
Properties 0x1800 to 0x1803 | Mapping 0x1A00 to 0x1A03.
59+
:param object node: Parent node for this object.
60+
"""
5361

5462
def __init__(self, node):
5563
super(TPDO, self).__init__(node)
@@ -58,8 +66,10 @@ def __init__(self, node):
5866

5967
def stop(self):
6068
"""Stop transmission of all TPDOs.
69+
6170
:raise TypeError: Exception is thrown if the node associated with the PDO does not
62-
support this function"""
71+
support this function.
72+
"""
6373
if isinstance(canopen.LocalNode, self.node):
6474
for pdo in self.map.values():
6575
pdo.stop()

canopen/pdo/base.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ def save(self):
5959
for pdo_map in self.map.values():
6060
pdo_map.save()
6161

62+
def subscribe(self):
63+
"""Register the node's PDOs for reception on the network.
64+
65+
This normally happens when the PDO configuration is read from
66+
or saved to the node. Use this method to avoid the SDO flood
67+
associated with read() or save(), if the local PDO setup is
68+
known to match what's stored on the node.
69+
"""
70+
for pdo_map in self.map.values():
71+
pdo_map.subscribe()
72+
6273
def export(self, filename):
6374
"""Export current configuration to a database file.
6475
@@ -331,8 +342,7 @@ def read(self):
331342
if index and size:
332343
self.add_variable(index, subindex, size)
333344

334-
if self.enabled:
335-
self.pdo_node.network.subscribe(self.cob_id, self.on_message)
345+
self.subscribe()
336346

337347
def save(self):
338348
"""Save PDO configuration for this map using SDO."""
@@ -387,9 +397,19 @@ def save(self):
387397
self._update_data_size()
388398

389399
if self.enabled:
390-
logger.info("Enabling PDO")
391400
self.com_record[1].raw = self.cob_id | (RTR_NOT_ALLOWED if not self.rtr_allowed else 0x0)
401+
self.subscribe()
402+
403+
def subscribe(self):
404+
"""Register the PDO for reception on the network.
392405
406+
This normally happens when the PDO configuration is read from
407+
or saved to the node. Use this method to avoid the SDO flood
408+
associated with read() or save(), if the local PDO setup is
409+
known to match what's stored on the node.
410+
"""
411+
if self.enabled:
412+
logger.info("Subscribing to enabled PDO 0x%X on the network", self.cob_id)
393413
self.pdo_node.network.subscribe(self.cob_id, self.on_message)
394414

395415
def clear(self):

0 commit comments

Comments
 (0)