Moving motors from a Python script

Since last time I've been pretty busy with some work related projects, so the CANopen safari has suffered a bit. Luckily I've got a little bit more time lately so I have picked up the CANopen stuff.

It's been a pretty heavy learning curve. I've spent a lot of time looking at the console and trying to understand what is going on. I received a lot of help from various people (thanks for showing me correct messages Uwe!) getting to understand the DS402 state engine. As well as (thanks Michael!) getting new programming-related knowledge.

In hindsight the combination of picking the tools (libraries) and understanding these conbines with getting to grips with the CANopen internals was pretty "heavy". For example; If you send an SDO message with the wrong amount of bytes, you get an unexpected response. So you not only have to understand using the library, but you have to understand the library correctly.

So, past days, pieces of the puzzle fall into place, and I have a nice video to show for it.

Libcanopen has python bindings (extended by Michael, thanks for making the MNT commands available in the python bindings). So with the python script:

  • the various states of the DS402 state engine have to be processed.
  • a profile position mode is chosen
  • a target position is set

Then after the first scrip "initializing" the device,  the script toggles the bit to start the previous set-up travel order.

Now that this is in place I'll work towards gluing Machinekit and CANopen together with Python. First creating some userland components and get the motor running from a Machinekit GUI button.

Better to first understand what we need to do before writing RT components. Long live the Python glue!

Some additional links:

  • Working towards Machinekit Issue #589
  • USB to socketcan USBTin device.
  • node 2 and 3 Maxon Epos 24/1 CANopen devices.
  • node 5 Nanotec CL3-E CANopen device.
  • My fork of libcanopen and the installation instructions:
    bas@jessie:~$ sudo apt-get install python-dev cython pkg-config
    bas@jessie:~$ git clone
    bas@jessie:~$ cd libcanopen
    bas@jessie:~/libcanopen$ git fetch origin
    bas@jessie:~/libcanopen$ git checkout machinekit-prep
    bas@jessie:~/libcanopen$ sh
    bas@jessie:~/libcanopen$ sudo make install
    bas@jessie:~/libcanopen$ cd cycanopen
    bas@jessie:~/libcanopen/cycanopen$ make
    bas@jessie:~/libcanopen/cycanopen$ cd ..
    bas@jessie:~/libcanopen$ sudo ldconfig

8 thoughts on “Moving motors from a Python script

  1. Hi!

    Thanks for the work you already did. This is super nice because I have some Nanotec Motors here and I want to run them via CANOpen and Python as well. Unfortunately my knowledge is quiet small, so I have a few questions:

    a) Do you know a good OpenCAN tutorial? CANBUS seems not the easiest thing on earth. (I was happy to work with the UART last time)

    b) Will this script also run on openWRT Systems like Onion Omega? (
    That would be nice because then a UDPtoMotor Bridge could easily be installed. My goal is to control these Motors via MAX/MSP.

    Thank you,


  2. Hi!
    This Board is a Linux Computer with USB connection and so I would buy a USB->CAN Converter as you mentioned it here as well. But is it possible to get this thing running on a Raspi?



    • Getting the USBTin to work depends what distro you run on the Raspi, and what is supported in the distro (socketcan).
      If you run Debian Jessie big chance it works. But that's something for you to find out :)

      Plus it all depends on what you want to do. If you really want to send time determined CANOpen messages (SYNC etc) then big chance the USBtin over USB does not work as you expect.
      If you use it for trying out, setting up the motor, sending/receiving SDO's etc. Then I'd go for the USBtin.

  3. Hi!
    I just want to get the motors run. No complex task just configure ramping, velocity and amount of steps. I know how it works with Nanotec controllers via RS485 which is super easy, so basic functionality should be the same.

  4. Hello there, maybe the topic is old but I am quite new to this :) I have some nanotec canOpen steppers and I want to program them for Clocl/Direction mode so I can run them with Rasp Pi GPIO pins. What is the most suitable interface to use Nanotec's software (Plug & Drive Studio) would USBtin work? I couldn't find anything on Nanotecs site.

  5. No, USBtin won't work with plug and drive studio, you need an IXXAT adapter for that. You do not need to run plug and drive studio if you're prepared to dig into CANOpen.

    You need to set the Objects in the OD so that you get into the Clock/Direction mode.

    If you have an USB version, for example this: you use NanoJ "Language" to set registers, and put a file containing the program on the device via USB.

    If you communicate thru CANOpen, work from linux and have a look at Because USBtin works with socketcan, you can send SDO's to the device when you start. Just a small python script at startup will take care of writing to the dictionary via SDO messages

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.