Stepper Class

The Stepper class is a driver is designed to control a set of stepper motor drivers via SPI. Specifically, it is designed to interface with the TMC4210, which communicates with most any other driver via step and direction signals.

class stepper.Stepper(ENN: pyb.Pin, CS: pyb.Pin, SCK: pyb.Timer, CLK: pyb.Timer.channel, spi: pyb.SPI, usrs: int = 8, k: int = 48, PULSE_div: int = 4, RAMP_div: int = 4, name: str = 'Motor')

Bases: object

Variables
  • ENN (pyb.Pin) – The pin associated with the enable pin on the driver.

  • CS (pyb.pin) – The pin associated with the chip select for the driver.

  • SCK (pyb.Timer) – The timer object used for synchronizing the stepper motor. Its frequency is used in calculated velocity and acceleration values.

  • CLK – Clock channel of the timer object.

  • spi (pyb.SPI) – SPI object used in communicating with the driver. The pins and timers involved are set up by default by the SPI module.

  • usrs (integer) – Microstep resolution (\(\mu\) Step ReSolution). This is a setting of the Step/Direction, often selected by bridging jumpers. It determines how many microsteps each motor step is subdivided into.

  • k (integer) – Number of steps for one revolution of the motor. This is a function of the physical motor used.

  • PULSE_div (integer) – A parameter that subdivides the clock frequency, defining the maximum step pulse rate. PULSE_div divides the clock frequency by \(2^{PULSE_{div}}\)

  • RAMP_div (integer) – Similar to PULSE_div, RAMP_div scales the accleration parameters instead of the velocity.

  • name (string) – A name for each instance of the class, used for debugging.

_P_calc(A_max)

Calculates appropriate values for \(P_{mul}\) and \(P_{div}\), which are timing parameters for controlling motor stepping. First, the following ratio is calculated.

\[p = \frac{0.99 A_{max}}{128 \cdot 2^{RAMP_{div} - PULSE_{div}}}\]

Next, starting at 0, integers are substituted for \(P_{div}\) in the equation below until \(P_{mul}\) is acceptably between 128 and 255.

\[P_{mul} = p \cdot 2^{3 + P_{div}}\]

At this point, the values of \(P_{mul}\) and \(P_{div}\) returned.

Parameters

A_max (binary) – Maximum allowable acceleration (as a binary value)

Returns

\(P_{mul}\), \(P_{div}\) in that order.

Return type

tuple

_byteToPos(byte)

Converts a binary value back into an angle (in radians). The following equation is used.

\[pos = \frac{byte \cdot 2\pi}{k \cdot usrs}\]
Parameters

byte (binary or integer) – Position (as a binary value)

Returns

Position (in radians)

Return type

float

_byteToVel(byte)

Converts a binary value back into an angular velocity (in radians/sec). The following equation is used.

\[pos = \frac{byte \cdot f \cdot 2\pi}{k \cdot usrs \cdot 2^{PULSE_{div}} \cdot 2048 \cdot 32}\]
Parameters

byte (binary or integer) – Velocity (as a binary value)

Returns

Velocity (in radians)

Return type

float

_config(R_M: bytes = 0, REF_CONF: bytes = 12, Vmin: int = 15, Vmax: int = 15, Amax: int = 500, save: bool = True)

Configures settings on the driver by writing to various registers via SPI.

Note

The specific addresses are hard coded for the TMC4210 stepper driver. If a different driver is used, pleas modify the addresses accordingly!

The R_M bits are used to select the mode as follows.

Mode Selection

R_M

Mode

00

Ramp Mode: Driver controls velocity and acceleration to reach a target position.

01

Soft Mode: Similar to ramp mode, but approaches the target more gently.

10

Velocity Mode: Driver controls acceleration to reach a target velocity.

11

Hold Mode: The driver controls only step and direction, leaving the microcontroller in full control of the velocity and acceleration.

For configuring the reference switches using REF_CONF, please consult the driver datasheet.

Parameters
  • R_M (bytes) – Mode selection.

  • REF_CONF (bytes) – Reference switch configuration

  • Vmin (float) – Minimum velocity before stopping (radians/sec)

  • Vmax (float) – Maximum velocity allowed (radians/sec)

  • Amax (float) – Maximum acceleration allowed (radians/sec^2)

  • save (boolean) – Determines whether the current configuration settings are saved as attributes. On by default, switched off for temporary setting changes.

_posToByte(pos)

Converts an angle in radians to a binary value. The following equation is used.

\[byte=\frac{pos \cdot k \cdot usrs}{2\pi}\]
Parameters

pos (float) – Position (in radians)

Returns

Position (as a binary value)

Return type

integer

_velToByte(vel)

Converts an angular velocity in radians/sec to a binary value. The following equation is used.

\[byte=\frac{vel \cdot k \cdot usrs \cdot 2^{PULSE_{div}} \cdot 2048 \cdot 32}{f \cdot 2\pi}\]
Parameters

pos (float) – Velocity (in radians/sec)

Returns

Velocity (as a binary value)

Return type

integer

disable()

Disables the motor by setting the enable pin to high.

enable()

Enables the motor by setting the enable pin to low.

getLp()

Returns the value of the lp bit, which indicates whether the reference switch is activated. Not tested, not implimented.

Returns

Value of the lp bit; 0 for not active, 1 for active.

Return type

bytes

homeInit()

Arms the homing procedure. Once this method is called, the motor can be driven into the reference switch. Not tested, not implimented.

readX()

Reads the current angle of the motor.

Note

The motor does not contain any kind of sensor. Position is determined by dead-reckoning, so the value here only describes where the driver thinks it’s put the motor. If the motor is turned externally, or if the motor drops steps due to excessive resistance or acceleration, this value will not be accurate!

Returns

The current angle (in radians)

Return type

float

setActX(Xact: int)

Redefines the current position of the motor for calibration. To prevent the motor from moving during this process, the driver is first reconfigured to velocity mode, then the new value is written to both the X_ACTUAL and the X_TARGET register. Finally, the driver is reconfigured back to whatever settings were previously used.

Parameters

Xact (integer) – Motor angle to be written.

setTargV(V: int)

During hold mode or velocity mode, this method sets the desired velocity.

Parameters

V (integer) – Desired velocity (in radians/sec)

setTargX(X: int)

Defines an angle for the motor to move toward.

Parameters

X (integer) – Desired angle (in radians)