Multiple instances of odrivetool/python

Hey, im using socketcan on linux to talk with odrive. My program controls one odrive over canbus, specified by its serialnumber. I would like to run multiple instances of this program to control multiple odrives, but when i do i get: [CanAdapter] cannot open bulk stream. Same problem if i run
odrivetool --can can0 -s serialnumber in two terminals. Does odrivetool take ownership over all connected odrives even when i specify the serialnumber?

Hi!

Interesting.

With -s it only connects to that one ODrive, so running several instances on the same SocketCAN interface should be fine.

Generally the main issue is that the ODrivetool (specifically the backend libodrive) instances don’t communicate with each other. So in the discovery → autobaud → serial-number assignment scheme, the instances can fight: by default the ODrive is unaddressed (node_id 63), and odrivetool/libodrive assigns a node ID when it connects. Separate instances don’t coordinate on that, so they’ll both pick 0 and knock each other’s ODrive off its node ID. So first, each ODrive should be given a unique node ID (e.g. over a single odrivetool instance), and ideally have the baudrate explicitly assigned (to be the same as the USB-CAN adapter’s):

odrv0.axis0.config.can.node_id = 1  # 2, 3, ... for the others
odrv0.can.config.baud_rate = 250000 # Example, change to what yours is (e.g. 500k, 1Mbaud, etc)
odrv0.save_configuration()

Also, if you do have multiple instances, you should have only one instance per ODrive – a second connection takes over from the first, and the first drops out a few seconds later. So always pass the serial number.

You may continue to get the [CanAdapter] cannot open bulk stream message, but assuming it’s one instance per ODrive + each ODrive has a unique and explicitly-set baudrate and node ID, this should be harmless (it’s just the odrivetool / libodrive instances noticing each other on the CAN bus). But let me know if it specifically still doesn’t work after that.

Thank you, forgot to handle node_id when i use multiple instances, that may well be the cause. Will try to assign node id and try again.

Still with node_ids set, the same message keeps coming. What do you mean by unique baudrate? They should have the same baudrate as thet exist on the same can network?

It works, and i can control the motors seemingly without any problems. However, its a bit annoying when testing since it fills up the terminal…

Glad it’s working!

Still with node_ids set, the same message keeps coming. What do you mean by unique baudrate? They should have the same baudrate as thet exist on the same can network?

Sorry, that was badly worded on my part – the node IDs need to be unique, but the baudrate should be the same on all of them (matching your USB-CAN adapter).

For the message spam, you can turn the CAN adapter’s logging down to errors-only with an environment variable:

LIBODRIVE_DEBUG_CANADAPTER=1 odrivetool --can can0 -s <serial>

or in your Python program, before importing odrive:

import os
os.environ["LIBODRIVE_DEBUG_CANADAPTER"] = "1"

But yes – this should probably also be a log level change on libodrive, as multiple instances with a single USB-CAN adapter should be supported.