Best practices for using this library #471
-
Hello! I'm looking for best practices for using this library (and implicitly also CAN/CANopen best practices, as I'm a completely newbie with this protocol). So far I've written down the following:
If there are more obvious stuff, do's and dont's, or doh's, please shout out; I'm eager to learn :) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 13 replies
-
Are you familiar with the CANopen protocol concepts in general? I'm wildly guessing what you know about CANopen, so pardon me if this is way too basic: I usually refer this to our new employees: https://www.csselectronics.com/pages/canopen-tutorial-simple-intro The NMT service (Network Management) state is a "runmode" state that all CANopen devices shall have. When a device boots it shall start in PRE-OPERATIONAL and each device must be set into OPERATIONAL mode by a special agent on the network named NMT master. If you only do PDO can only be generated in OPERATIONAL. SDOs are possible in both PRE-OPERATIONAL and OPERATIONAL mode. (I'm sorry I don't have any readily off-hand examples, as we only use the asyncio port of canopen. I'd onlyt be misleading for the syntax.) |
Beta Was this translation helpful? Give feedback.
-
Note that one of the most beautiful details in CANopen protocol design is the possibility to remap PDOs. Not only their content, but also their applied COB-ID. This way, data that is already transmitted on the bus can be consumed by several nodes, without a master needing to receive from one and relay to another device. Just setting an RPDO's COB-ID to the value of another node's TPDO COB-ID will make the former node receive data directly from the latter, without additional delay of one cycle, and reducing the overall bus load. So you see the protocol is very flexible and API usage mostly depends on your desired communication structure. If all you need is scripted access to some parameters (not a cyclic control loop), then SDO is usually the right tool. One important detail about SDO: Each "channel" (e.g. the default (0x580 / 0x600) + $NODEID pair for SDO1) is stateful, both on client and server side. That means if several clients want to exchange data with the same server, you either need to use several SDO channels (which many devices don't have). Or you need to coordinate which SDO client is allowed to use the channel by other means. For one application, we have this convention for example: The "master" device (central brain of the machine) configures all slaves (e.g. drive controllers, sensors) using SDO, after resetting them and waiting for their PRE-OPERATIONAL boot-up message. It then puts all devices in OPERATIONAL state, then ceases SDO communication and uses only SYNC+PDO (and the enabled heartbeat). Another node is used for diagnosis, but will only do SDO requests when the device is found to be in OPERATIONAL already, avoiding clashes with the master's SDO usage. Hope some of this makes sense and gives you a better picture. We might be able to give more targeted advice if you describe the application where you want to employ the canopen API. Who is doing what, controlling whom, and which roles are filled by a node implemented in Python? |
Beta Was this translation helpful? Give feedback.
I've never used (and didn't know about)
network.check()
. Must be optional obviously :-) Usually better to put logging inside any callbacks you install and try to make them not raise exceptions.The heartbeat protocol is used for a number of different schemes, like Node Guarding, Life Guarding, and the Boot-up Protocol.
wait_for_heartbeat()
is only the correct approach if you know that one will be sent out by a device. That happens only once during power-on, or if you send the NMT state change commandRESET
orRESET COMMUNICATION
. It's possible to send it as a broadcast by usingnetwork.nmt.state = 'RESET'
, if that is appropriate for your application lifecycle. I've had projects where …