HF-TMC51x0 Driver (TMC5130 & TMC5160) 0.1.0-dev
Hardware Agnostic C++ Driver for the TMC51x0 (TMC5130 & TMC5160)
Loading...
Searching...
No Matches
tmc51x0::TMC51x0< CommType >::MotorControl Struct Reference

Motor control subsystem. More...

#include <tmc51x0.hpp>

Collaboration diagram for tmc51x0::TMC51x0< CommType >::MotorControl:
[legend]

Public Member Functions

 MotorControl (TMC51x0 &driver) noexcept
 Construct motor control subsystem.
 
Result< void > Enable () noexcept
 Enable the motor driver.
 
Result< void > Disable () noexcept
 Disable the motor driver.
 
Result< void > SetCurrent (uint8_t irun, uint8_t ihold) noexcept
 Set motor run and hold current.
 
Result< void > ConfigureChopper (const ChopperConfig &config) noexcept
 Configure chopper settings.
 
Result< void > SetMicrostepResolution (MicrostepResolution mres, const MicrostepChangeOptions &opts=MicrostepChangeOptions{}) noexcept
 Change microstep resolution (CHOPCONF.MRES)
 
Result< void > ConfigureStealthChop (const StealthChopConfig &config) noexcept
 Configure stealthChop settings.
 
Result< void > ConfigureMotorCurrent (const MotorSpec &motor_spec) noexcept
 Configure motor current from motor specifications.
 
Result< void > SetGlobalScaler (uint16_t scaler) noexcept
 Set global current scaler.
 
Result< GlobalConfigGetGlobalConfig () noexcept
 Get global configuration.
 
Result< void > ConfigureCoolStep (const CoolStepConfig &config) noexcept
 Configure CoolStep current reduction.
 
Result< void > ConfigureDcStep (const DcStepConfig &config) noexcept
 Configure dcStep automatic commutation.
 
Result< void > SetMicrostepLookupTable (uint8_t index, uint32_t value) noexcept
 Set microstep lookup table entry.
 
Result< void > SetMicrostepLookupTableSegmentation (uint8_t width_sel_0, uint8_t width_sel_1, uint8_t width_sel_2, uint8_t width_sel_3, uint8_t lut_seg_start1, uint8_t lut_seg_start2, uint8_t lut_seg_start3) noexcept
 Set microstep lookup table segmentation.
 
Result< void > SetMicrostepLookupTableStart (uint16_t start_current) noexcept
 Set microstep lookup table start current.
 
Result< void > ConfigureMicrostepLutPreset (MicrostepLutPreset preset) noexcept
 Configure microstep lookup table with a preset waveform.
 
Result< void > ConfigureMicrostepLutCustom (const uint32_t lut[8], uint8_t w0, uint8_t w1, uint8_t w2, uint8_t w3, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t start_sin, uint8_t start_sin90) noexcept
 Configure microstep lookup table from raw data.
 
Result< void > SetupMotorFromSpec (const MotorSpec &motor_spec, const MechanicalSystem *mechanical_system=nullptr) noexcept
 Setup motor from high-level specifications.
 
Result< void > ConfigureGlobalConfig (const GlobalConfig &config) noexcept
 Configure global configuration (GCONF register)
 
Result< void > SetStealthChopEnabled (bool enabled) noexcept
 Enable/Disable StealthChop (PWM mode)
 
Result< ChopperConfigGetChopperConfig () noexcept
 Get chopper configuration.
 
Result< bool > IsEnabled () noexcept
 Check if motor driver is enabled.
 
Result< bool > IsStealthChopEnabled () noexcept
 Check if StealthChop is enabled.
 
Result< bool > IsStealthChopCalibrated () noexcept
 Check if StealthChop is calibrated and working.
 
Result< Diag0ConfigGetDiag0Config () noexcept
 Get DIAG0 pin configuration.
 
Result< void > SetDiag0Config (const Diag0Config &config) noexcept
 Set DIAG0 pin configuration.
 
Result< Diag1ConfigGetDiag1Config () noexcept
 Get DIAG1 pin configuration.
 
Result< void > SetDiag1Config (const Diag1Config &config) noexcept
 Set DIAG1 pin configuration.
 
Result< void > SetCoilCurrents (int16_t coil_a, int16_t coil_b) noexcept
 Set coil currents for direct mode operation.
 
Result< void > SetIholdDelayMs (float total_delay_ms) noexcept
 Set motor power down delay (IHOLDDELAY) in milliseconds.
 

Private Attributes

TMC51x0driver_
 Reference to parent driver instance.
 

Detailed Description

template<typename CommType>
struct tmc51x0::TMC51x0< CommType >::MotorControl

Motor control subsystem.

Provides methods for controlling motor current, chopper configuration, and stealthChop operation.

Constructor & Destructor Documentation

◆ MotorControl()

template<typename CommType >
tmc51x0::TMC51x0< CommType >::MotorControl::MotorControl ( TMC51x0 & driver)
inlineexplicitnoexcept

Construct motor control subsystem.

Parameters
driverReference to parent TMC51x0 driver instance

Member Function Documentation

◆ ConfigureChopper()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::ConfigureChopper ( const ChopperConfig & config)
noexcept

Configure chopper settings.

Parameters
configChopper configuration structure
Returns
Result<void> indicating success or error

◆ ConfigureCoolStep()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::ConfigureCoolStep ( const CoolStepConfig & config)
noexcept

Configure CoolStep current reduction.

Parameters
configCoolStep configuration structure
Returns
Result<void> indicating success or error

◆ ConfigureDcStep()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::ConfigureDcStep ( const DcStepConfig & config)
noexcept

Configure dcStep automatic commutation.

Parameters
configdcStep configuration structure
Returns
Result<void> indicating success or error

◆ ConfigureGlobalConfig()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::ConfigureGlobalConfig ( const GlobalConfig & config)
noexcept

Configure global configuration (GCONF register)

Parameters
configGlobal configuration structure
Returns
Result<void> indicating success or error

◆ ConfigureMicrostepLutCustom()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::ConfigureMicrostepLutCustom ( const uint32_t lut[8],
uint8_t w0,
uint8_t w1,
uint8_t w2,
uint8_t w3,
uint8_t x1,
uint8_t x2,
uint8_t x3,
uint8_t start_sin,
uint8_t start_sin90 )
noexcept

Configure microstep lookup table from raw data.

Parameters
lutArray of 8 uint32_t values for MSLUT0-7
w0Width select for segment 0 (0-3)
w1Width select for segment 1 (0-3)
w2Width select for segment 2 (0-3)
w3Width select for segment 3 (0-3)
x1Segment 1 start (0-255)
x2Segment 2 start (0-255)
x3Segment 3 start (0-255)
start_sinStart sine value at entry 0 (0-255)
start_sin90Sine value at entry 256 / 90° (0-255)
Returns
Result<void> indicating success or error

Programs all MSLUT registers with custom values for advanced waveform shaping. Use this for custom motor characteristics or torque ripple compensation.

See also
Datasheet Section 18: Microstep Lookup Table

◆ ConfigureMicrostepLutPreset()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::ConfigureMicrostepLutPreset ( MicrostepLutPreset preset)
noexcept

Configure microstep lookup table with a preset waveform.

Parameters
presetWaveform preset (DEFAULT_SINE, PURE_SINE, etc.)
Returns
Result<void> indicating success or error

Programs all MSLUT registers (MSLUT0-7, MSLUTSEL, MSLUTSTART) with optimized values for the selected waveform type.

Available presets:

  • DEFAULT_SINE: TMC5160 power-on default (slightly modified sine)
  • PURE_SINE: Mathematical pure sine wave (smooth, standard torque)
Note
The lookup table defines the first quarter of the sine wave; the driver mirrors it for the remaining three quarters.
See also
Datasheet Section 18: Microstep Lookup Table

◆ ConfigureMotorCurrent()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::ConfigureMotorCurrent ( const MotorSpec & motor_spec)
noexcept

Configure motor current from motor specifications.

Parameters
motor_specMotor specifications including current, sense resistor, supply voltage
Returns
Result<void> indicating success or error

Automatically calculates and sets IRUN, IHOLD, and GLOBAL_SCALER from motor specifications. Also sets IHOLDDELAY if configured in motor_spec.

◆ ConfigureStealthChop()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::ConfigureStealthChop ( const StealthChopConfig & config)
noexcept

Configure stealthChop settings.

Parameters
configStealthChop configuration structure
Returns
Result<void> indicating success or error

◆ Disable()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::Disable ( )
noexcept

Disable the motor driver.

Returns
Result<void> indicating success or error

◆ Enable()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::Enable ( )
noexcept

Enable the motor driver.

Returns
Result<void> indicating success or error

◆ GetChopperConfig()

template<typename CommType >
Result< ChopperConfig > tmc51x0::TMC51x0< CommType >::MotorControl::GetChopperConfig ( )
noexcept

Get chopper configuration.

Parameters
configReference to store current ChopperConfig
Returns
Result<ChopperConfig> containing the value or error

◆ GetDiag0Config()

template<typename CommType >
Result< Diag0Config > tmc51x0::TMC51x0< CommType >::MotorControl::GetDiag0Config ( )
noexcept

Get DIAG0 pin configuration.

Parameters
configReference to store current Diag0Config
Returns
Result<Diag0Config> containing the value or error

Reads DIAG0 configuration from GCONF register using read-modify-write pattern.

◆ GetDiag1Config()

template<typename CommType >
Result< Diag1Config > tmc51x0::TMC51x0< CommType >::MotorControl::GetDiag1Config ( )
noexcept

Get DIAG1 pin configuration.

Parameters
configReference to store current Diag1Config
Returns
Result<Diag1Config> containing the value or error

Reads DIAG1 configuration from GCONF register using read-modify-write pattern.

◆ GetGlobalConfig()

template<typename CommType >
Result< GlobalConfig > tmc51x0::TMC51x0< CommType >::MotorControl::GetGlobalConfig ( )
noexcept

Get global configuration.

Parameters
configReference to store current GlobalConfig
Returns
Result<GlobalConfig> containing the value or error

◆ IsEnabled()

template<typename CommType >
Result< bool > tmc51x0::TMC51x0< CommType >::MotorControl::IsEnabled ( )
noexcept

Check if motor driver is enabled.

Returns
Result<bool> containing true if motor is enabled (toff > 0), false otherwise

◆ IsStealthChopCalibrated()

template<typename CommType >
Result< bool > tmc51x0::TMC51x0< CommType >::MotorControl::IsStealthChopCalibrated ( )
noexcept

Check if StealthChop is calibrated and working.

Returns
Result<bool> containing true if StealthChop is calibrated (pwm_scale_auto != 0), false otherwise

StealthChop requires calibration to work properly. If not calibrated, the motor may not move or may have poor performance.

◆ IsStealthChopEnabled()

template<typename CommType >
Result< bool > tmc51x0::TMC51x0< CommType >::MotorControl::IsStealthChopEnabled ( )
noexcept

Check if StealthChop is enabled.

Returns
Result<bool> containing true if StealthChop is enabled, false otherwise

◆ SetCoilCurrents()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::SetCoilCurrents ( int16_t coil_a,
int16_t coil_b )
noexcept

Set coil currents for direct mode operation.

Parameters
coil_aCoil A current target (signed, range ±248 recommended for safe operation)
coil_bCoil B current target (signed, range ±248 recommended for safe operation)
Returns
Result<void> indicating success or error

Sets coil current targets directly via XTARGET register when direct_mode is enabled. In direct mode, XTARGET controls coil currents instead of position:

  • Bits 8..0: Coil A current (signed 9-bit)
  • Bits 24..16: Coil B current (signed 9-bit)

Current is scaled by IHOLD setting. The STEP/DIR inputs and motion controller are not used in direct mode.

Note
Requires direct_mode to be enabled via ConfigureGlobalConfig().
For SpreadCycle mode, limit amplitude to ±248 for IHOLD up to 31 and hysteresis settings not exceeding 15 to keep sufficient regulation margin.
Values are automatically constrained to valid 9-bit signed range (-256 to +255).
Warning
This method overwrites XTARGET register. Do not use with normal position control.

◆ SetCurrent()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::SetCurrent ( uint8_t irun,
uint8_t ihold )
noexcept

Set motor run and hold current.

Parameters
irunRun current (0-31, where 31 = 100% of global scaler)
iholdHold current (0-31, where 31 = 100% of global scaler)
Returns
Result<void> indicating success or error

◆ SetDiag0Config()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::SetDiag0Config ( const Diag0Config & config)
noexcept

Set DIAG0 pin configuration.

Parameters
configDiag0Config structure with DIAG0 settings
Returns
Result<void> indicating success or error

Writes DIAG0 configuration to GCONF register using read-modify-write pattern. Preserves all other GCONF bits.

◆ SetDiag1Config()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::SetDiag1Config ( const Diag1Config & config)
noexcept

Set DIAG1 pin configuration.

Parameters
configDiag1Config structure with DIAG1 settings
Returns
Result<void> indicating success or error

Writes DIAG1 configuration to GCONF register using read-modify-write pattern. Preserves all other GCONF bits.

Note
steps_skipped should not be enabled with other DIAG1 options (mutually exclusive).

◆ SetGlobalScaler()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::SetGlobalScaler ( uint16_t scaler)
noexcept

Set global current scaler.

Parameters
scalerGlobal scaler value (32-256)
Returns
Result<void> indicating success or error

◆ SetIholdDelayMs()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::SetIholdDelayMs ( float total_delay_ms)
noexcept

Set motor power down delay (IHOLDDELAY) in milliseconds.

Parameters
total_delay_msTotal motor power down delay time in milliseconds (0.0 = instant, automatically calculated to register value 0-15)
Returns
Result<void> indicating success or error

Sets the total delay time for motor power down after motion as soon as standstill is detected (stst=1) and TPOWERDOWN has expired. The smooth transition avoids a motor jerk upon power down.

The delay is automatically calculated from the desired total delay time, clock frequency, and the number of current reduction steps (IRUN - IHOLD).

Calculation: IHOLDDELAY = round((total_delay_ms / (IRUN - IHOLD)) × f_clk / (1000 × 2^18)) Total delay = (IRUN - IHOLD) × IHOLDDELAY × (2^18 / f_clk)

Note
If IRUN == IHOLD, the delay is always 0 regardless of this setting.
At 12 MHz with 15 current steps: typical range is 200-700 ms total delay.
The actual delay may differ slightly from the desired value due to register quantization.
This reads the current IRUN and IHOLD values from the driver to calculate the delay.

◆ SetMicrostepLookupTable()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::SetMicrostepLookupTable ( uint8_t index,
uint32_t value )
noexcept

Set microstep lookup table entry.

Parameters
indexLookup table index (0-7)
valueLookup table value (32-bit)
Returns
Result<void> indicating success or error

◆ SetMicrostepLookupTableSegmentation()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::SetMicrostepLookupTableSegmentation ( uint8_t width_sel_0,
uint8_t width_sel_1,
uint8_t width_sel_2,
uint8_t width_sel_3,
uint8_t lut_seg_start1,
uint8_t lut_seg_start2,
uint8_t lut_seg_start3 )
noexcept

Set microstep lookup table segmentation.

Parameters
width_sel_0Width selection for segment 0 (0-3)
width_sel_1Width selection for segment 1 (0-3)
width_sel_2Width selection for segment 2 (0-3)
width_sel_3Width selection for segment 3 (0-3)
lut_seg_start1Start position for segment 1 (0-255)
lut_seg_start2Start position for segment 2 (0-255)
lut_seg_start3Start position for segment 3 (0-255)
Returns
Result<void> indicating success or error

◆ SetMicrostepLookupTableStart()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::SetMicrostepLookupTableStart ( uint16_t start_current)
noexcept

Set microstep lookup table start current.

Parameters
start_currentStart current value (0-255)
Returns
Result<void> indicating success or error

◆ SetMicrostepResolution()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::SetMicrostepResolution ( MicrostepResolution mres,
const MicrostepChangeOptions & opts = MicrostepChangeOptions{} )
noexcept

Change microstep resolution (CHOPCONF.MRES)

Parameters
mresNew microstep resolution
optsOptions controlling rescaling behavior
Returns
Result<void> indicating success or error

By default, this preserves physical meaning across the MRES change:

  • Position registers (XACTUAL/XTARGET/X_COMPARE) are rescaled in microstep counts
  • Motion profile registers are rewritten so speed/accel in user units remain unchanged
  • Encoder scaling is recalculated if encoder is configured

If the motor is not in standstill and opts.require_standstill is true (default), the operation fails with INVALID_STATE.

◆ SetStealthChopEnabled()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::SetStealthChopEnabled ( bool enabled)
noexcept

Enable/Disable StealthChop (PWM mode)

Parameters
enabledtrue to enable StealthChop, false for SpreadCycle
Returns
Result<void> indicating success or error

◆ SetupMotorFromSpec()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::MotorControl::SetupMotorFromSpec ( const MotorSpec & motor_spec,
const MechanicalSystem * mechanical_system = nullptr )
noexcept

Setup motor from high-level specifications.

Parameters
motor_specMotor specification structure
mechanical_systemOptional mechanical system configuration
Returns
Result<void> indicating success or error

Automatically calculates and sets motor current, chopper configuration, and other parameters based on motor specifications.

Member Data Documentation

◆ driver_

template<typename CommType >
TMC51x0& tmc51x0::TMC51x0< CommType >::MotorControl::driver_
private

Reference to parent driver instance.


The documentation for this struct was generated from the following file: