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 cycanopen-402-prepare.py 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 cycanopen-positionmove.py 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 https://github.com/luminize/libcanopen
    bas@jessie:~$ cd libcanopen
    bas@jessie:~/libcanopen$ git fetch origin
    bas@jessie:~/libcanopen$ git checkout machinekit-prep
    bas@jessie:~/libcanopen$ sh build.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
Share/Bookmark

6 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? (https://onion.io)
    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,

    Ralf

  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?

    Thanks,

    Ralf

    • 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.

Leave a Reply to Bas de Bruijn Cancel reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>