Skip to content

Commit 7c5e4ce

Browse files
acolombaf-silva
andauthored
DS402: Simplify setup procedure. (#259)
* p402: Do not switch any states during setup_402_state_machine(). Reading the PDO configuration is possible in OPERATIONAL or PRE-OPERATIONAL states, so switching that is unnecessary. The application should be responsible to handle such transitions, and the library function should be usable without disturbing the application logic. Changing the DS402 state machine to SWITCH ON DISABLED is also not necessary. The drive may be in whatever state from a previous usage, and then the change to SWITCH ON DISABLED may even trigger an exception because there is no way to reach it directly. So this transition should also be the application's responsibility. * p402: Check NMT state before reading PDO configuration. SDOs are allowed in all but the STOPPED state. That would lead to a timeout and an SdoCommunicationError exception. Checking the NMT state here raises an exception without a timeout involved. * p402: Make reading the PDO configuration optional during setup. If the application already configured the PDOs and called .save() on the pdo.Maps object, there is no sense in reading everything back again in the setup_pdos() method. Provide an optional argument to disable that behavior. A call to subscribe to the PDOs from the network is added because that side-effect of pdo.read() is necessary for the TPDO callback to work. * p402: Allow skipping PDO upload from setup_402_state_machine(). Add an optional argument which is simply passed down to setup_pdos() to choose whether reading the PDO configuration is necessary. * Fix DS402 documentation to match the implementation. Besides the changes regarding setup_402_state_machine(), there were numerous errors where the documentation talks about nonexistent or differently named attributes. Also fix the description regaring what the method actually does. It won't configure the TPDO1 to contain the Statusword, but only check the PDO configuration for Statusword and Controlword presence. Co-authored-by: André Filipe Silva <[email protected]>
1 parent e5355f4 commit 7c5e4ce

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

canopen/profiles/p402.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,22 +212,31 @@ def __init__(self, node_id, object_dictionary):
212212
self.tpdo_pointers = {} # { index: pdo.Map instance }
213213
self.rpdo_pointers = {} # { index: pdo.Map instance }
214214

215-
def setup_402_state_machine(self):
215+
def setup_402_state_machine(self, read_pdos=True):
216216
"""Configure the state machine by searching for a TPDO that has the StatusWord mapped.
217217
218+
:param bool read_pdos: Upload current PDO configuration from node.
218219
:raises ValueError:
219220
If the the node can't find a Statusword configured in any of the TPDOs.
220221
"""
221-
self.nmt.state = 'PRE-OPERATIONAL' # Why is this necessary?
222-
self.setup_pdos()
222+
self.setup_pdos(read_pdos)
223223
self._check_controlword_configured()
224224
self._check_statusword_configured()
225-
self._check_op_mode_configured()
226-
self.nmt.state = 'OPERATIONAL'
227-
self.state = 'SWITCH ON DISABLED' # Why change state?
228225

229-
def setup_pdos(self):
230-
self.pdo.read() # TPDO and RPDO configurations
226+
def setup_pdos(self, upload=True):
227+
"""Find the relevant PDO configuration to handle the state machine.
228+
229+
:param bool upload:
230+
Retrieve up-to-date configuration via SDO. If False, the node's mappings must
231+
already be configured in the object, matching the drive's settings.
232+
:raises AssertionError:
233+
When the node's NMT state disallows SDOs for reading the PDO configuration.
234+
"""
235+
if upload:
236+
assert self.nmt.state in 'PRE-OPERATIONAL', 'OPERATIONAL'
237+
self.pdo.read() # TPDO and RPDO configurations
238+
else:
239+
self.pdo.subscribe() # Get notified on reception, usually a side-effect of read()
231240
self._init_tpdo_values()
232241
self._init_rpdo_pointers()
233242

doc/profiles.rst

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ The current status can be read from the device by reading the register
3434
0x6041, which is called the "Statusword".
3535
Changes in state can only be done in the 'OPERATIONAL' state of the NmtMaster
3636

37-
TPDO1 needs to be set up correctly. For this, run the the
38-
`BaseNode402.setup_402_state_machine()` method. Note that this setup
39-
routine will change only TPDO1 and automatically go to the 'OPERATIONAL' state
40-
of the NmtMaster::
37+
PDOs with the Controlword and Statusword mapped need to be set up correctly,
38+
which is the default configuration of most DS402-compatible drives. To make
39+
them accessible to the state machine implementation, run the the
40+
`BaseNode402.setup_402_state_machine()` method. Note that this setup routine
41+
will read the current PDO configuration by default, causing some SDO traffic.
42+
That works only in the 'OPERATIONAL' or 'PRE-OPERATIONAL' states of the
43+
:class:`NmtMaster`::
4144

4245
# run the setup routine for TPDO1 and it's callback
4346
some_node.setup_402_state_machine()
@@ -50,21 +53,20 @@ Write Controlword and read Statusword::
5053
# Read the state of the Statusword
5154
some_node.sdo[0x6041].raw
5255

53-
During operation the state can change to states which cannot be commanded
54-
by the Controlword, for example a 'FAULT' state.
55-
Therefore the :class:`PowerStateMachine` class (in similarity to the :class:`NmtMaster`
56-
class) automatically monitors state changes of the Statusword which is sent
57-
by TPDO1. The available callback on thet TPDO1 will then extract the
58-
information and mirror the state change in the :attr:`BaseNode402.powerstate_402`
59-
attribute.
56+
During operation the state can change to states which cannot be commanded by the
57+
Controlword, for example a 'FAULT' state. Therefore the :class:`BaseNode402`
58+
class (in similarity to :class:`NmtMaster`) automatically monitors state changes
59+
of the Statusword which is sent by TPDO. The available callback on that TPDO
60+
will then extract the information and mirror the state change in the
61+
:attr:`BaseNode402.state` attribute.
6062

6163
Similar to the :class:`NmtMaster` class, the states of the :class:`BaseNode402`
62-
class :attr:`._state` attribute can be read and set (command) by a string::
64+
class :attr:`.state` attribute can be read and set (command) by a string::
6365

6466
# command a state (an SDO message will be called)
65-
some_node.powerstate_402.state = 'SWITCHED ON'
67+
some_node.state = 'SWITCHED ON'
6668
# read the current state
67-
some_node.powerstate_402.state = 'SWITCHED ON'
69+
some_node.state = 'SWITCHED ON'
6870

6971
Available states:
7072

0 commit comments

Comments
 (0)