-
Notifications
You must be signed in to change notification settings - Fork 197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LocalNode improvements for TPDOs #524
base: master
Are you sure you want to change the base?
Changes from all commits
e184bf5
853f7eb
290a358
b5e546f
822009c
43e32a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,8 @@ def __init__( | |
self.add_write_callback(self.nmt.on_write) | ||
self.emcy = EmcyProducer(0x80 + self.id) | ||
|
||
self.nmt.add_state_change_callback(self._nmt_state_changed) | ||
|
||
def associate_network(self, network): | ||
self.network = network | ||
self.sdo.network = network | ||
|
@@ -115,6 +117,15 @@ def set_data( | |
self.data_store.setdefault(index, {}) | ||
self.data_store[index][subindex] = bytes(data) | ||
|
||
if 0x1800 <= index <= 0x19FF: | ||
# TPDO Communication parameter changed | ||
tpdoNum = (index - 0x1800) + 1 | ||
self._tpdo_configuration_write(tpdoNum) | ||
elif 0x1A00 <= index <= 0x1BFF: | ||
# TPDO Mapping parameter changed | ||
tpdoNum = (index - 0x1A00) + 1 | ||
self._tpdo_configuration_write(tpdoNum) | ||
Comment on lines
+120
to
+127
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be shortened a bit because the ranges are adjacent. You only need one range check and one modulo to find the TPDO number. But feel free to ignore this if you think it's not an improvement. |
||
|
||
def _find_object(self, index, subindex): | ||
if index not in self.object_dictionary: | ||
# Index does not exist | ||
|
@@ -127,3 +138,32 @@ def _find_object(self, index, subindex): | |
raise SdoAbortedError(0x06090011) | ||
obj = obj[subindex] | ||
return obj | ||
|
||
def _nmt_state_changed(self, old_state, new_state): | ||
if new_state == "OPERATIONAL": | ||
for pdo in self.tpdo.map.values(): | ||
if pdo.enabled: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a matter of taste, but I would prefer an inverted check and a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this will generate warnings for any TPDO not configured as periodic, right? Those should probably rather be skipped silently. |
||
try: | ||
pdo.start() | ||
logger.info("Successfully started %s", pdo.name) | ||
except ValueError: | ||
logger.warning("Failed to start %s due to missing period", pdo.name) | ||
except Exception: | ||
logger.exception("Unknown error starting %s", pdo.name) | ||
else: | ||
logger.info("%s not enabled", pdo.name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now this logging is probably too much. Worst case, it will be emitted for all 512 PDOs. That's too much even on DEBUG level. I'd just drop this log call. |
||
else: | ||
self.tpdo.stop() | ||
|
||
def _tpdo_configuration_write(self, tpdoNum): | ||
pdo = self.tpdo.map[tpdoNum] | ||
|
||
# Only allowed to edit pdo configuration in pre-op or operational | ||
if self.nmt.state not in ("PRE-OPERATIONAL", "OPERATIONAL"): | ||
logger.warning("Tried to configure %s when not in pre-op or operational", pdo.name) | ||
return | ||
|
||
try: | ||
pdo.read(from_od=True) | ||
except: | ||
pass | ||
Comment on lines
+166
to
+169
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would we want to silently swallow any and all exceptions here? |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,6 +200,37 @@ def test_save(self): | |
self.remote_node.pdo.save() | ||
self.local_node.pdo.save() | ||
|
||
def test_send_pdo_on_operational(self): | ||
self.local_node.tpdo[1].period = 0.5 | ||
|
||
self.local_node.nmt.state = 'INITIALISING' | ||
self.local_node.nmt.state = 'PRE-OPERATIONAL' | ||
self.local_node.nmt.state = 'OPERATIONAL' | ||
|
||
self.assertNotEqual(self.local_node.tpdo[1]._task, None) | ||
|
||
def test_config_pdo(self): | ||
# Disable tpdo 1 | ||
self.local_node.tpdo[1].enabled = False | ||
self.local_node.tpdo[1].cob_id = 0 | ||
self.local_node.tpdo[1].period = 0.5 # manually assign a period | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand that comment. Just above, you do the same without a comment. Is this intended to test mix & match between direct assignment and SDO based changes? Isn't it using the value from the inhibit time object anyway? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't use inhibit time currently. I have a different branch for this, I wasn't sure if I should try to pull it in with this stuff. Yes the intention here was to test to make sure the SDOs are correctly being propagated to the PDO configuration stack. grantweiss-RaymondCorp@0a9f50d There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The approach of falling back to |
||
|
||
self.local_node.nmt.state = 'INITIALISING' | ||
self.local_node.nmt.state = 'PRE-OPERATIONAL' | ||
|
||
# Attempt to re-enable tpdo 1 via sdo writing | ||
PDO_NOT_VALID = 1 << 31 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possible to re-use |
||
odDefaultVal = self.local_node.object_dictionary["Transmit PDO 0 communication parameters.COB-ID use by TPDO 1"].default | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd use the numeric index here, as it's less fragile and shorter. There's already a typo in that description, and fixing it would break the test 😉 |
||
enabledCobId = odDefaultVal & ~PDO_NOT_VALID # Ensure invalid bit is not set | ||
|
||
self.remote_node.sdo["Transmit PDO 0 communication parameters.COB-ID use by TPDO 1"].raw = enabledCobId | ||
|
||
# Transition to operational | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two comments don't really help. The test code should be simple enough to not need an explanation. |
||
self.local_node.nmt.state = 'OPERATIONAL' | ||
|
||
# Ensure tpdo automatically started with transition | ||
self.assertNotEqual(self.local_node.tpdo[1]._task, None) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering whether this should be made opt-in. There is certainly existing code using
LocalNode
which already manages the TPDO transmissions externally, because the library didn't use to do it. With this change, these users could end up with duplicated transmissions, or at least things stepping on each others' toes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would be the preferred way to achieve this? A keyword argument in the
LocalNode
constructor?Would the
_tpdo_configuration_write
call insideset_data
also be opt-in?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think an additional argument
auto_tpdo: bool = False
would be fine, with appropriate documentation (docstring) of course.And while we're at it, I think the
_nmt_state_changed
name does not convey what the function does. It should rather be named_tpdo_auto_start_stop
, then this line will clearly express what the connection is (NMT state --> TPDO start).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I think if someone writes to the local node's objects, it is quite expected to affect the PDO settings. Actually even for RPDOs. Of course our processing of these parameters is quite rudimentary now and not really standard compliant, but that can be improved step by step.