Friday, 3 August 2018

log book notes: trinamic 5130-bob on Raspberry pi

These are notes on using a trinamic 5130-bob on Raspberry Pi.
Here is a short video of it running....

I intend to have a Python interface to the device eventually.

I'm running this on a pi 3b+ with raspbian lite installed.

I'm basically following this page, but switching 5160 to 5130.

The broadcom driver version I have is 1.56. (check here)
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.56.tar.gz
tar zxvf bcm2835-XXX.tar
cd bcm2835-XXX
./configure
make
sudo make check
sudo make install

https://www.trinamic.com/fileadmin/assets/Support/TTAP/TMC-API_Release_v3.03.zip

The trinamic software also needs wiringPi installed....

And you need to follow their (trinamic) directory instructions to make to work.

Finally small differences in the detailed spec between 5130 and 5160 mean that the GCONF setting should be 04, not 0C - see main.c line 39 on the trinamic website page linked above.


I could now drive a motor successfully. I have added a few more tweaks to the code which are detailed below. (The code is here as well)



The example code can only drive a single motor per Raspberry Pi and it looks like it will be messy to make it cope with multiple motors so I am now working on a pure Python driver for the chip.

The motor I'm testing on is a Mitsumi M42SP-6K I liberated from an old dead HP printer, and I'm using a 24v power supply. With the default settings the motor gets quite toasty as the current limit is much to high, I found it helped to disable the output stage whenever a test run finished - the original code leaves the output stage on when it exits. I also tweaked the code to allow slightly more testing to be done.

Here is the C program - note global replace 5160 with 5130 has been applied, but was pointless really - it just means you have to make the same change to the SPI_TMC c & h files as well.

#include <stdio.h>
#include <wiringPi.h>
#include <bcm2835.h>
#include "SPI_TMC.h"

// Include the IC(s) you want to use
#include "TMC-API/tmc/ic/TMC5130/TMC5130.h"
#define MOTOR0            0 // Motor 0
void resetMotorDrivers(uint8 motor);

int main(int argc, char **argv) {
    if (!bcm2835_init())
      return 1;
    wiringPiSetupGpio();
     
    // Initiate SPI 
    bcm2835_spi_begin();
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // MSB first
      bcm2835_spi_setDataMode(BCM2835_SPI_MODE3); // SPI Mode 3
      bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_256); // 1 MHz clock
      bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // define CS pin
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // set CS polarity to low

    /***** TMC5130-BOB Example *****/
    pinMode(2, OUTPUT);
    digitalWrite(2, HIGH); // Apply VCC_IO voltage to Motor 0
    pinMode(4, OUTPUT);
    digitalWrite(4, LOW); // Use internal clock
    pinMode(3, OUTPUT);
    digitalWrite(3, LOW); // Enable driver stage
    resetMotorDrivers(MOTOR0);

    // EN_PWM_MODE=1 enables stealthChop™
    tmc5130_writeInt(MOTOR0, TMC5130_GCONF, 0x00000004);
     // TOFF=3, HSTRT=4, HEND=1, TBL=2, CHM=0 (spreadCycle™)
    tmc5130_writeInt(MOTOR0, TMC5130_CHOPCONF, 0x000100C3);
     // IHOLD=10, IRUN=15 (max. current), IHOLDDELAY=6
    tmc5130_writeInt(MOTOR0, TMC5130_IHOLD_IRUN, 0x00080F0A);
     // TPOWERDOWN=10: Delay before power down in stand still
    tmc5130_writeInt(MOTOR0, TMC5130_TPOWERDOWN, 0x0000000A);
     // TPWMTHRS=500
    tmc5130_writeInt(MOTOR0, TMC5130_TPWMTHRS, 0x000001F4);
     // Values for speed and acceleration
    tmc5130_writeInt(MOTOR0, TMC5130_VSTART, 1);
    tmc5130_writeInt(MOTOR0, TMC5130_A1, 500);
    tmc5130_writeInt(MOTOR0, TMC5130_V1, 26843);
    tmc5130_writeInt(MOTOR0, TMC5130_AMAX, 1000);  
    tmc5130_writeInt(MOTOR0, TMC5130_VMAX, 300000);
    tmc5130_writeInt(MOTOR0, TMC5130_DMAX, 1100);
    tmc5130_writeInt(MOTOR0, TMC5130_D1, 600);
    tmc5130_writeInt(MOTOR0, TMC5130_VSTOP, 10);
    tmc5130_writeInt(MOTOR0, TMC5130_RAMPMODE, TMC5130_MODE_POSITION);
    int targetlist[]={ 48*.25*256
                       , 0
                       , 48*5*256
                       , 0
                       , 48*25*256
                       , 0
                       , 48*125*256
                       , 0
                       , 12345};
    int lpos=0;
    while (targetlist[lpos] != 12345) {
        int target=targetlist[lpos++];
        tmc5130_writeInt(MOTOR0, TMC5130_XTARGET, target);
        printf("Target position now %12d, status 0x%04x\n", target, tmc5130_readInt(MOTOR0, TMC5130_GSTAT));
        for (int l=0; l<20; l++) {
            int rstat=tmc5130_readInt(MOTOR0, TMC5130_RAMPSTAT);
            char* rmsg="ramping\0";
            if (rstat & 256) {
                rmsg="vmax reached\0";
            }
            printf("Position: %12d,  step 0x%08x status 0x%04x, %s\n"
                   , tmc5130_readInt(MOTOR0, TMC5130_XACTUAL)
                   , tmc5130_readInt(MOTOR0, TMC5130_TSTEP)
                   , tmc5130_readInt(MOTOR0, TMC5130_GSTAT)
                   , rmsg);
            if (rstat & 512) {
                l=20;
            }
            delay(1000);
        }
    }
    digitalWrite(3, HIGH); // Disable driver stage
    // End SPI communication
      bcm2835_spi_end();
       bcm2835_close();

    return 0;
}

void resetMotorDrivers(uint8 motor) {
    if(!tmc5130_readInt(MOTOR0, TMC5130_VACTUAL)) {
        digitalWrite(2, LOW); // Apply VCC_IO voltage to Motor 0
        delay(10);
        digitalWrite(2, HIGH); // Apply VCC_IO voltage to Motor 0
        delay(10);
    }
}

But now it's Python time!

9 comments:

  1. I‘d be very interested in the Python/pigpio version. Would you happen to have some code that you‘d be willing to share already?
    TIA! beat

    ReplyDelete
  2. I've put a simple, pure python version on github.
    https://github.com/pootle/tripipy

    ReplyDelete
  3. Hello pootle,

    I've been trying to get the TMC5130 BOB running per your github instructions and code. I'm pretty sure I have things connected to the RPi 3B+ corrrectly, but keep getting this type of error message.

    >>> mot=chipdrive.tmc5130(stepsPerRev=48)
    23:02:42 INFO (3371) MainThread trinamicDriver.resetChip: chip reset attempted
    23:02:42 DEBUG (3371) MainThread trinamicDriver.enableOutput: disabled
    23:02:42 INFO (3371) MainThread trinamicDriver.__init__: controller initialised using spi master on channel 1, clock frequency 15,000,000.
    23:02:42 ERROR (3371) MainThread trinamicDriver._checkRegName: The register VMAX is not writable
    Traceback (most recent call last):
    File "", line 1, in
    mot=chipdrive.tmc5130(stepsPerRev=48)
    File "/home/pi/Focus_Rail/Trinamic/TMC_EXAMPLE-2/tripipy-master/chipdrive.py", line 60, in __init__
    currently=self.md.readWriteMultiple(regsettings,regactions)
    File "/home/pi/Focus_Rail/Trinamic/TMC_EXAMPLE-2/tripipy-master/trinamicDriver.py", line 255, in readWriteMultiple
    regint, prevdef = self._checkRegName(prevname, 'R')
    File "/home/pi/Focus_Rail/Trinamic/TMC_EXAMPLE-2/tripipy-master/trinamicDriver.py", line 363, in _checkRegName
    raise RuntimeError("The register %s is not writable" % str(regName))
    RuntimeError: The register VMAX is not writable
    >>>

    I've also tried the Trinamic site with their TMC5160 program and your suggestions and no luck (even tried the TMC5160 BOB with same result). I'm and EE, but not a programmer (design RFIC chips), so very limited coding skills, although I have done some work with the Pololu Tic-500 stepper controller (USB) and wrote the code in Python. This is working very well but I'm interested in these Trinimac devices to reduce the motor noise and vibration of the DIY optical imaging setup I'm working on.

    Any suggestions or help is greatly appreciated.

    Cheers,

    Mike

    ReplyDelete
  4. Hi pootle,
    This is great stuff, thanks for translating the code to Python 3. I've been toying with the TMC5160 BOB, and it looks like all I need to get it working is change line 63 in the chipdrive.py file to read:

    ('GCONF',0x0000000C),

    And this will change to the correct registry pin to set the GCONF?

    Thanks!!
    Kobin

    ReplyDelete
    Replies
    1. That looks reasonable - I had to mess with GCONF going the other way!

      Delete
    2. One more question... is the wiring setup for the TMC5130 using your Python code the same as here:
      http://blog.trinamic.com/2018/02/19/stepper-motor-with-tmc5160/

      Thanks again!!
      Kobin

      Delete
    3. Yes - they should be the same pins for the first chip - couple of others used as well if you have a second chip

      Delete
    4. OK thanks much, my TMC5130 chip must be fried then because I am not able to get the motor to move and I triple checked all the wiring. If a new BOB doesn't work, is there a way to contact you via PM or email? Happy to pay for your time for helping.
      Best
      Kobin
      kobin@element-lab.com

      Delete
    5. Ah! sorry, the notification of your post went down a rabbit hole somewhere. Are you still working on this?

      Delete