TMC2130Stepper

by teemuatlut

teemuatlut / TMC2130Stepper

Arduino library for Trinamic TMC2130 Stepper driver

132 Stars 39 Forks Last release: Not found GNU Affero General Public License v3.0 187 Commits 15 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

This library has been superseded by the newer TMCStepper library which supports many more of the Trinamic stepper drivers and more MCU architectures.

TMC2130Stepper

Arduino library for Trinamic TMC2130 Stepper driver

Installation:

Use the Arduino IDE library manager (Sketch -> Include library -> Manage libraries...)

Search for TMC2130Stepper and then install.

Or download the zip file from Github and extract it to
your-scetchbook-location/libraries
and restart the IDE.

or goto to you arduino libraries folder and in command line git clone https://github.com/teemuatlut/TMC2130Stepper.git

What works:

Nearly all the features in the registeries are configurable through get/set functions. See below for a list of functions. Datasheet (link) from Trinamic also provides further detail into the settings available.

Simple example

/*
Initializes the library and turns the motor in alternating directions.
*/
#define EN_PIN    38  // Nano v3:   16 Mega:    38  //enable (CFG6)
#define DIR_PIN   55  //            19          55  //direction
#define STEP_PIN  54  //            18          54  //step
#define CS_PIN    40  //            17          64  //chip select

bool dir = true;

#include <tmc2130stepper.h> TMC2130Stepper TMC2130 = TMC2130Stepper(EN_PIN, DIR_PIN, STEP_PIN, CS_PIN);

void setup() { Serial.begin(9600); TMC2130.begin(); // Initiate pins and registeries TMC2130.SilentStepStick2130(600); // Set stepper current to 600mA TMC2130.stealthChop(1); // Enable extremely quiet stepping

digitalWrite(EN_PIN, LOW);

}

void loop() { digitalWrite(STEP_PIN, HIGH); delayMicroseconds(10); digitalWrite(STEP_PIN, LOW); delayMicroseconds(10); uint32_t ms = millis(); static uint32_t last_time = 0; if ((ms - last_time) > 2000) { if (dir) { Serial.println("Dir -> 0"); TMC2130.shaft_dir(0); } else { Serial.println("Dir -> 1"); TMC2130.shaft_dir(1); } dir = !dir; Serial.println(TMC2130.GCONF(), BIN); last_time = ms; } } </tmc2130stepper.h>

Current calculations

A simple way to set the current is to use the setCurrent() method.

I_rms = (CS+1)/32 * V_fs/(R_sense+0.02mOhm) * 1/sqrt(2)
I_motor = I_sine/248 * (CS+1/32) * V_fs/(R_sense+0.02mOhm)

Where:
Irms is the rms current
I
motor is the motor current
CS is the Current Scale value
Vfs is the voltage determined by vsense
Rsense is the chosen sense resistor
I
sine is the current position in the sine table. Imotor reaches peak value at Isine = 248

If externalref is enabled, Vfs is scaled by V_ain/2.5V

Functions

Function

Argument range Returns Description
begin - - Initialized pins Enable, Direction, Step and Chip Select.
Initialized the SPI pins MOSI, MISO and SCK.
Calls spi.begin()
Sets offtime = 2 and blanktime = 24
setCurrent 0..2000
0.1 .. 1
0..1
- Helper function to set the motor RMS current.
Arguments:
uint16t Desired current in milliamps
float Sense resistor value
float Multiplier for holding current
Example for SilentStepStick2130: setCurrent(1200, 0.11, 0.5)

Makes use of the runcurrent() and hold_current() funtions.

SilentStepStick2130 0..2000 - Calls the begin() functions and according to the argument sets the current with sense resistor being 0.11 and multiplier being 0.5

Register functions:

Note: You can read the saved value by calling a function without providing an argument.

GCONF register

Function

Argument range Returns Description
GCONF - uint32t Read actual bits from the register
externalref 0/1 bool Use external voltage reference for coil currents
internalsenseR 0/1 bool Use internal sense resistors
stealthChop 0/1 bool Enable stealthChop (dependant on velocity thresholds)
commutation 0/1 bool Enable commutation by full step encoder
shaftdir 0/1 bool Inverse motor direction
diag0errors 0/1 bool Enable DIAG0 active on driver errors: Over temperature (ot), short to GND (s2g), undervoltage chargepump (uvcp)
diag0tempprewarn 0/1 bool Enable DIAG0 active on driver over temperature prewarning
diag0stall 0/1 bool Enable DIAG0 active on motor stall (set TCOOLTHRS before using this feature)
diag1stall 0/1 bool Enable DIAG1 active on motor stall (set TCOOLTHRS before using this feature)
diag1index 0/1 bool Enable DIAG1 active on index position (microstep look up table position 0)
diag1chopperon 0/1 bool Enable DIAG1 active when chopper is on
diag1stepsskipped 0/1 bool Enable output toggle when steps are skipped in dcStep mode (increment of LOSTSTEPS). Do not enable in conjunction with other DIAG1 options.
diag0activehigh 0/1 bool Set DIAG0 to active high
diag1activehigh 0/1 bool Set DIAG1 to active high
smallhysteresis 0/1 bool 0: Hysteresis for step frequency comparison is 1/16
1: Hysteresis for step frequency comparison is 1/32
stopenable 0/1 bool Emergency stop: DCIN stops the sequencer when tied high (no steps become executed by the sequencer, motor goes to standstill state).
directmode 0/1 bool Motor coil currents and polarity are directly controlled by the SPI interface.

IHOLD_IRUN register

Function

Argument Returns Description
holdcurrent 0..31 uint8t Standstill current (0=1/32…31=32/32)
runcurrent 0..31 uint8t Motor run current (0=1/32…31=32/32)
holddelay 0..15 uint8t Controls the number of clock cycles for motor power down after a motion as soon as standstill is detected (stst=1) and TPOWERDOWN has expired.

REG_TPOWERDOWN register

Function

Argument Returns Description
powerdowndelay 0..255 uint8t powerdown_delay sets the delay time after stand still (stst) of the motor to motor current power down. Time range is about 0 to 4 seconds.
0…((2^8)-1) * 2^18 tCLK

REG_TSTEP register

Function

Argument Returns Description
microsteptime - uint32t Read the actual measured time between two 1/256 microsteps derived from the step input frequency in units of 1/fCLK.

REG_TPWMTHRS register

Function

Argument Returns Description
stealthmaxspeed 0..1,048,575 uint32_t This is the upper velocity for stealthChop voltage PWM mode. TSTEP ≥ TPWMTHRS - stealthChop PWM mode is enabled, if configured - dcStep is disabled

REG_TCOOLTHRS register

Function

Argument Returns Description
coolstepminspeed 0..1,048,575 uint32_t This is the lower threshold velocity for switching on smart energy coolStep and stallGuard feature.

REG_THIGH register

Function

Argument Returns Description
modeswspeed 0..1,048,575 uint32_t This velocity setting allows velocity dependent switching into a different chopper mode and fullstepping to maximize torque.

REG_XDRIRECT register

Function

Argument Returns Description
coilAcurrent -255..+255 int16t Specifies Motor coil currents and polarity directly programmed via the serial interface. In this mode, the current is scaled by IHOLD setting.
coilBcurrent -255..+255 int16t As above.

REG_VDCMIN register

Function

Argument Returns Description
DCstepminspeed 0..8,388,607 uint32_t The automatic commutation dcStep becomes enabled by the external signal DCEN. VDCMIN is used as the minimum step velocity when the motor is heavily loaded.
Hint: Also set DCCTRL parameters in order to operate dcStep.

REG_CHOPCONF register

Function

Argument Returns Description
CHOPCONF - uint32t Read actual bits from the register
offtime 0..15 uint8t Off time setting controls duration of slow decay phase
NCLK= 12 + 32*TOFF
Initialized to value 2 (NCLK = 76) by begin()
hysteresisstart 1..8 uint8t Add 1, 2, …, 8 to hysteresis low value HEND (1/512 of this setting adds to current setting) Attention: Effective HEND+HSTRT ≤ 16. Hint: Hysteresis decrement is done each 16 clocks
fastdecaytime 0..15 uint8t Fast decay time setting TFD with NCLK= 32*HSTRT
hysteresisend -3..12 int8t This is the hysteresis value which becomes used for the hysteresis chopper.
sineoffset -3..12 int8t This is the sine wave offset and 1/512 of the value becomes added to the absolute value of each sine wave entry.
disableIcomparator 0/1 bool 1: Disables current comparator usage for termination of the fast decay cycle.
choppermode needs to be 1.
randomofftime 0/1 bool 0: Chopper off time is fixed as set by TOFF
1: Random mode, TOFF is random modulated by dNCLK= -12 … +3 clocks.
choppermode 0/1 uint8t 0: Standard mode (spreadCycle)
1: Constant off time with fast decay time.
Fast decay time is also terminated when the negative nominal current is reached. Fast decay is after on time.
blanktime 16, 24,
36, 54
uint8t Set comparator blank time to 16, 24, 36 or 54 clocks.
Hint: 24 or 36 is recommended for most applications
Initialized to 36 (register value = 3) by begin()
highsenseR 0/1 bool 0: Low sensitivity, high sense resistor voltage
1: High sensitivity, low sense resistor voltage
fullstepthreshold 0/1 bool This bit enables switching to fullstep, when VHIGH is exceeded. Switching takes place only at 45° position. The fullstep target current uses the current value from the microstep table at the 45° position.
highspeedmode 0/1 bool This bit enables switching to chm=1 and fd=0, when VHIGH is exceeded. This way, a higher velocity can be achieved. Can be combined with vhighfs=1. If set, the TOFF setting automatically becomes doubled during high velocity operation in order to avoid doubling of the chopper frequency.
syncphases 0..15 bool Synchronization of the chopper for both phases of a two phase motor in order to avoid the occurrence of a beat, especially at low motor velocities. It is automatically switched off above VHIGH.
microsteps 255, 128, 64, 32, 16,
8, 4, 2, 0 (FULLSTEP)
uint8t Reduced microstep resolution for Step/Dir operation. The resolution gives the number of microstep entries per sine quarter wave.
interpolate 0/1 bool The actual microstep resolution becomes extrapolated to 256 microsteps for smoothest motor operation.
doubleedgestep 0/1 bool Enable step impulse at each step edge to reduce step frequency requirement.
disableshortprotection 0/1 bool 0: Short to GND protection is on
1: Short to GND protection is disabled

REG_COOLCONF register

Function

Argument Returns Description
COOLCONF - uint32t Read actual bits from the register
sgmin 0..15 uint8t If the stallGuard2 result falls below sgmin32, the motor current becomes increased to reduce motor load angle.
sgmax 0..15 uint8t If the stallGuard2 result is equal to or above (sgmin+sgmax+1)32, the motor current becomes decreased to save energy.
sgstepwidth 1, 2, 4, 8 uint8t Current increment steps per measured stallGuard2 value
sgcurrentdecrease 1, 2, 8, 32 uint8t For each (value) stallGuard2 values decrease by one
smartmincurrent uint8t uint8t 0: 1/2 of current setting (IRUN)
1: 1/4 of current setting (IRUN)
sgstallvalue int8t int8t This signed value controls stallGuard2 level for stall output and sets the optimum measurement range for readout. A lower value gives a higher sensitivity. Zero is the starting value working with most motors. -64 to +63: A higher value makes stallGuard2 less sensitive and requires more torque to indicate a stall.
sgfilter uint8t uint8_t 0: Standard mode, high time resolution for stallGuard2
1: Filtered mode, stallGuard2 signal updated for each four fullsteps (resp. six fullsteps for 3 phase motor) only to compensate for motor pole tolerances

REG_PWMCONF register

Function

Argument Returns Description
PWMCONF - uint32t Read actual bits from the register
stealthamplitude 0..255 uint8t pwm autoscale=0
User defined PWM amplitude offset (0-255) The resulting amplitude (limited to 0…255) is: PWMAMPL + PWMGRAD * 256 / TSTEP

pwm_ autoscale=1
User defined maximum PWM amplitude when switching back from current chopper mode to voltage PWM mode (switch over velocity defined by TPWMTHRS). Do not set too low values, as the regulation cannot measure the current when the actual PWM value goes below a setting specific value. Settings above 0x40 recommended.

stealthgradient 0..255 uint8t pwm_ autoscale=0
Velocity dependent gradient for PWM amplitude: PWMGRAD * 256 / TSTEP is added to PWMAMPL

pwm_ autoscale=1
User defined maximum PWM amplitude change per half wave (1 to 15)

stealthfreq 0..3 uint8t 0: fPWM=2/1024 fCLK
1: fPWM=2/683 fCLK
2: fPWM=2/512 fCLK
3: fPWM=2/410 fCLK
stealthautoscale 0/1 bool 0: User defined PWM amplitude. The current settings have no influence.
1: Enable automatic current control Attention: When using a user defined sine wave table, the amplitude of this sine wave table should not be less than 244. Best results are obtained with 247 to 252 as peak values.
stealthsymmetric 0/1 bool 0: The PWM value may change within each PWM cycle (standard mode)
1: A symmetric PWM cycle is enforced
standstillmode 0..3 uint8t Stand still option when motor current setting is zero (I_HOLD=0).
0: Normal operation
1: Freewheeling
2: Coil shorted using LS drivers
3: Coil shorted using HS drivers

REG_DRVSTATUS register

Function

Argument Returns Description
DRVSTATUS - uint32_t Read actual bits from the register

REGPWMSCALE register

Function

Argument Returns Description
PWMSCALE - uint32t Read actual bits from the register

REGENCMCTRL register

Function

Argument Returns Description
invert_encoder 0/1 bool Invert encoder inputs
maxspeed 0/1 bool Ignore Step input. If set, the hold current IHOLD determines the motor current, unless a step source is activated

REGLOSTSTEPS register

Function

Argument Returns Description
LOSTSTEPS - uint32t Read actual bits from the register

Read raw registry:

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.