Internet of [Moving] Things: TMC5130 + Raspberry Pi 3B+

Let’s say we’re working together in a shared office space where you can rent some desk for a certain time. For your convenience, the tables can be adjusted in height. What if the system already learned your preferred settings for standing and sitting and automatically adjusts as you wish as soon as you walk up to the table? Slight correction? Just grab your phone and make some finer adjustments.

This tutorial shall give a simple starter project for web-accessible motion applications. We’re working on a Raspberry Pi 3B+ with Raspian installed in combination with a TMC5130-BOB stepper driver breakout board. The basic principle will be the same for different drivers. The driver strength of this particular chip is probably too small for the shown application but it’s perfect for a first test on the desk.

The TMC5130 is a stepper motor driver with integrated motion controller, also called a cDriver™ . Therefore, it’s absolutely sufficient to command motions. The movement itself will not require any processing power from the Raspberry Pi, while the motor moves smoothly with outstanding performance.

Below you can find the 10 steps to take. It might be that some of them are already done in your case or you know a better way of doing it. Please feel free to share your wisdom 🙂

1. Set up Network & Interfacing

First we connect keyboard, mouse & display to Raspberry Pi and connect it to a network via wifi or cable. In the following steps we assume that the Raspi has access to the internet.

Now we should set static IP addresses for networks in order to access the Raspberry via networks & sub networks. In this tutorial, we use the following:

WIFI (wifi) http://192.168.178.184/

LAN (eth0) http://192.168.178.185/

Now we can make the Raspi accessible through your favorite machine. Therefore we open the configuration on the console:

With the Interfacing options we can enable SSH, SPI and GPIOs:

The SPI and the GPIOs will be required in order to access the driver chip.

Enabling SSH allows us to address the Raspi from our preferred device via network by its static IP in an SSH console like Putty. Even more convenient might be the access via remote desktop. The XRDP tool enables graphical access via tools like Windows Remote Desktop. Just install and connect to the IP:

For both you can log in with your standard Raspi credits.

2. Update

For the best user experience, we should make sure that all software is up to date. Run update/upgrade/dist-upgrade one after the other and reboot if packages were installed when running one of these commands. Start over until there are no updates left to install:

3. Install SPI Driver

The SPI driver can be found in the bcm2835 library. For this guide version “1.58” was used. In this case X.XX is replaced by “1.58”.
You can check the latest version by scrolling to the bottom of the following page: http://www.airspayce.com/mikem/bcm2835/.
Run one line after the other.

4. Download and install Apache Server, PHP and common Libraries

There are countless tutorials of that on the web. Here are the required lines for the most common setup for PHP7. Run one line after the other:

5. Download Project Files

We put the project files directly to the web server directory. The following lines will navigate there, unpack the files, remove the archive and replace the default ‘hello world’ landing page:

Of course you can download the package directly in order to have a look upfront:

http://blog.trinamic.com/wp-content/uploads/2019/03/TMC5130_Raspi_Webserver.zip

6. Wire Everything Up

Here’s the pinout.

Now we can also connect the motor. Make sure that the motor is never disconnected when the driver is active. Therefore I highly recommend to always connect the motor first and then power the system. In this case you can choose something like 5…46V. At 24V, your power supply should supply 1A at least. For lower voltages a bit more.

7. Test TMC5130

Now we can compile the C source code that parametrizes the driver:

You can find everything in the src sub-directory. I suggest to access it with your favorite text editor that has built-in SSH (SFTP) support.

The code includes the access to the chip registers, simple functions for movements and a standard initialization. It utilizes the SPI interface in order to communicate with the chip. All in all it’s just around 120 lines plus defines in the header.

In the TMC5130_init function you can find the basic initialization. Please check whether the programmed current of around 0.7A RMS is suitable for your motor. There is also a second example for 0.25 A RMS.

With this line you can run the compiled program:

Make sure the system is powered for the test. Otherwise, nothing will happen. If everything is OK, the motor starts moving with constant velocity.

Our program is not clever enough to stop the motor yet – that’s the next step. If you want to stop it, just unpower it. This will also reset the driver chip.

8. Demo Program

Before starting, we have to comment the main function in the TMC5130.c, because now we want to run the main of the project.c file.

Now that is done we can compile the whole project:

Everything worked well? Now we can test the program with different parameters. Please try one line after the other:

In the main loop of the project.c file, we are looking at the different ASCII commands (i,r,0,t,b,g,s,d) and the corresponding parameters and use the TMC5130 code to perform different movements.

By the way, the number of 51200 steps is exactly one revolution for the most common hybrid stepper motors. Typically these motors have 200 full steps. The driver runs with 256 microsteps per default. 200*256=51200.

The given velocity of 100000 is here around 77500pps. You can calculate it with the following formula:

v[pps]=v*fclk/2^24

As we are using the internal clock of the chip of around 13MHz, this gives:

v[pps] = 100000 * 13000000 / 2^24

As we know that one reveolution has 51200 micro steps, we are moving 77500/51200 or around 1.5 revolutions per second (rps). This is around 1.5*60 = 90 revolutions per minute (rpm).

Further information regarding real world units can be found in the datasheet.

9. Make Programs Accessible

Our two motor control programs work well now. But as they are using important hardware blocks of the Raspi you need certain rights. Therefore we need to allow the www-data user to access our programs as sudo. All we need to do is add following to the sudoers file. As the slightest mistake in this modification might mean that we lock ourselves out of the Raspi, we’re doing well in using the visudo helper, which performs a syntax check:

Here we can add the following lines to the bottom:

Now we can save the file and exit.

10. Check Out the Web Project

You can find all web relevant files in the web subfolder:

We linked the index.html before. This means we can now open our browser and access the web project on the Raspi via its IP address (whichever you use http://192.168.178.184 or http://192.168.178.185) directly from our browser. (If you call it from the Raspi directly it’s of course http://localhost/)

All the forms are defined in the index.html file. Submitting a form by clicking a button will call the run.php with all given parameters. The run.php is called in an iframe, which is shown in the feedback field. Therefore all responses will be shown there.

The run.php translates the commands from the web interface into calls of our little project program, which will move the motor. Most commands are triggering or stopping a motion and inform that the end of the program is reached.

There is one additional command d (|<>|), which will start the demo, where the motor is moved back and forth. In mode the program stays active. In a state machine the actual status of the motor is monitored. As soon as the motor arrives at a target position the motor goes back where it came from. Even though the program is active, everything still needs only around 1% of processing power.

Summary

Of course you can build way way fancier programs, utilize java script and sweet graphics. But I hope this project gives you a brief idea of how to get started.

This project is meant as a starter for different projects around automation. You can shut your window blinds, open your cat flap or rock your baby to sleep. Very similar implementations allow stronger drivers like the TMC5160 or TMC5161, but also smaller two-axis systems that are e.g. based on the TMC5041.

As the actual driving of the motors doesn’t consume processing power of the Raspberry Pi You can combine different motors for several tasks and still have easily enough processing capabilities for powerful application layers.

For the driver itself, we’re working with standard C code. Therefore you can also use it on any microcontroller or platform in order to make it work in different environments.

Please feel free to add a comment if this post was helpful, about what you made out of it or if there is anything I can improve.

Thomas

2 Comments

  • Tamas

    Dear Trinamic!

    I am trying to use a TMC5130 but no success. I use a custom board with STM32L4 MCU. The SPI communication with the TMC5130 does not work.
    As in the datasheet, I send 5 bytes (First byte is the address with the R/W flag)
    For any of the registers I always receive zero values. But the first byte (received immediately after the address byte) apparently contains a valid status. The state of the REFL and REFR pins are reflected properly in the status byte. Also, the IC is live , and the connections should be good!

    But what is the problem? I receive always zeroes for all of the registers.
    When trying to write any registers, it reads back always zero.

    If I remove the module from the socket, I receive full 0xff, because there is a weak pull-up resistor on the MISO line.

  • Thomas Ernst

    Dear Tamas,

    Here’s my best guess: Might it be that the chip select goes high between the bytes you send?
    This might explain that the status byte is transferred properly, but all the others not.

    Please make sure to keep the chip select line low for all five bytes. I hope this solves it.

    Best regards,

    Thomas

Leave a Reply

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