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 >::Thresholds Struct Reference

Velocity thresholds / mode thresholds (TPWMTHRS, TCOOLTHRS, THIGH) More...

#include <tmc51x0.hpp>

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

Public Member Functions

 Thresholds (TMC51x0 &driver) noexcept
 
Result< void > SetStealthChopVelocityThreshold (float value, Unit unit) noexcept
 Set StealthChop velocity threshold (TPWMTHRS)
 
Result< float > GetStealthChopVelocityThreshold (Unit unit) const noexcept
 Get StealthChop velocity threshold (TPWMTHRS) from local tracking.
 
Result< void > SetTcoolthrs (float threshold, Unit unit) noexcept
 Set StallGuard/CoolStep threshold velocity (TCOOLTHRS)
 
Result< float > GetTcoolthrs (Unit unit) const noexcept
 Get cached StallGuard/CoolStep threshold velocity (TCOOLTHRS)
 
Result< void > SetHighSpeedThreshold (float value, Unit unit) noexcept
 Set High-Speed velocity threshold (THIGH)
 
Result< void > SetDcStepVelocityThreshold (float value, Unit unit) noexcept
 Set DcStep velocity threshold (VDCMIN)
 
Result< float > GetDcStepVelocityThreshold (Unit unit) const noexcept
 Get cached DcStep velocity threshold (VDCMIN)
 
uint32_t GetVdcminRegisterValue () const noexcept
 Get locally tracked VDCMIN register value (raw)
 
Result< void > SetModeChangeSpeeds (float pwm_thrs, float cool_thrs, float high_thrs, Unit unit) noexcept
 Convenience: set TPWMTHRS, TCOOLTHRS, and THIGH in one call.
 
uint32_t GetTpwmthrsRegisterValue () const noexcept
 Get locally tracked TPWMTHRS register value (raw)
 
uint32_t GetTcoolthrsRegisterValue () const noexcept
 Get locally tracked TCOOLTHRS register value (raw)
 
uint32_t GetThighRegisterValue () const noexcept
 Get locally tracked THIGH register value (raw)
 
Result< void > SetModeHysteresis (float lower_speed, float upper_speed, Unit unit) noexcept
 Configure StealthChop to SpreadCycle transition with hysteresis.
 
Result< void > UpdateModeHysteresis () noexcept
 Update TPWMTHRS based on current chopper mode for hysteresis.
 
Result< void > DisableModeHysteresis () noexcept
 Disable mode hysteresis (use single TPWMTHRS threshold)
 
bool IsModeHysteresisEnabled () const noexcept
 Check if mode hysteresis is currently configured.
 

Private Attributes

TMC51x0driver_
 
uint32_t hysteresis_to_spreadcycle_tstep_ {0}
 
uint32_t hysteresis_to_stealthchop_tstep_ {0}
 
bool hysteresis_enabled_ {false}
 

Detailed Description

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

Velocity thresholds / mode thresholds (TPWMTHRS, TCOOLTHRS, THIGH)

Consolidates all "threshold" knobs so users don't have to guess whether a given threshold belongs to motor control, StallGuard, or diagnostics.

Constructor & Destructor Documentation

◆ Thresholds()

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

Member Function Documentation

◆ DisableModeHysteresis()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::Thresholds::DisableModeHysteresis ( )
noexcept

Disable mode hysteresis (use single TPWMTHRS threshold)

Returns
Result<void> indicating success or error

Clears the hysteresis configuration and returns to standard single-threshold mode switching behavior.

◆ GetDcStepVelocityThreshold()

template<typename CommType >
Result< float > tmc51x0::TMC51x0< CommType >::Thresholds::GetDcStepVelocityThreshold ( Unit unit) const
noexcept

Get cached DcStep velocity threshold (VDCMIN)

Parameters
unitUnit to return the cached value in
Returns
Result<float> containing the cached value or error

This returns the locally tracked value (VDCMIN is treated as write-only by this driver).

◆ GetStealthChopVelocityThreshold()

template<typename CommType >
Result< float > tmc51x0::TMC51x0< CommType >::Thresholds::GetStealthChopVelocityThreshold ( Unit unit) const
noexcept

Get StealthChop velocity threshold (TPWMTHRS) from local tracking.

Parameters
unitUnit of the returned threshold
Returns
Result<float> containing the threshold in requested units

◆ GetTcoolthrs()

template<typename CommType >
Result< float > tmc51x0::TMC51x0< CommType >::Thresholds::GetTcoolthrs ( Unit unit) const
noexcept

Get cached StallGuard/CoolStep threshold velocity (TCOOLTHRS)

Parameters
unitUnit to return the cached value in
Returns
Result<float> containing the cached value or error

◆ GetTcoolthrsRegisterValue()

template<typename CommType >
uint32_t tmc51x0::TMC51x0< CommType >::Thresholds::GetTcoolthrsRegisterValue ( ) const
noexcept

Get locally tracked TCOOLTHRS register value (raw)

Returns
Raw TCOOLTHRS register value (20-bit meaningful range)

◆ GetThighRegisterValue()

template<typename CommType >
uint32_t tmc51x0::TMC51x0< CommType >::Thresholds::GetThighRegisterValue ( ) const
noexcept

Get locally tracked THIGH register value (raw)

Returns
Raw THIGH register value (20-bit meaningful range)
Note
THIGH defines the upper velocity limit for StallGuard2/CoolStep. Above this velocity, SG and CoolStep are disabled.

◆ GetTpwmthrsRegisterValue()

template<typename CommType >
uint32_t tmc51x0::TMC51x0< CommType >::Thresholds::GetTpwmthrsRegisterValue ( ) const
noexcept

Get locally tracked TPWMTHRS register value (raw)

Returns
Raw TPWMTHRS register value (20-bit meaningful range)

◆ GetVdcminRegisterValue()

template<typename CommType >
uint32_t tmc51x0::TMC51x0< CommType >::Thresholds::GetVdcminRegisterValue ( ) const
noexcept

Get locally tracked VDCMIN register value (raw)

Returns
Raw VDCMIN register value

◆ IsModeHysteresisEnabled()

template<typename CommType >
bool tmc51x0::TMC51x0< CommType >::Thresholds::IsModeHysteresisEnabled ( ) const
noexcept

Check if mode hysteresis is currently configured.

Returns
true if hysteresis is enabled with valid upper/lower thresholds

◆ SetDcStepVelocityThreshold()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::Thresholds::SetDcStepVelocityThreshold ( float value,
Unit unit )
noexcept

Set DcStep velocity threshold (VDCMIN)

Parameters
valueVelocity threshold value (0 disables)
unitUnit of the value
Returns
Result<void> indicating success or error

VDCMIN is part of the ramp-generator driver feature control register set. Per datasheet, VDCMIN-based DcStep enable is only valid when using the internal ramp generator (SD_MODE=LOW). In external STEP/DIR mode, DcStep is enabled via the external DCEN pin instead.

Return values
INVALID_STATEif called while in external STEP/DIR mode

◆ SetHighSpeedThreshold()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::Thresholds::SetHighSpeedThreshold ( float value,
Unit unit )
noexcept

Set High-Speed velocity threshold (THIGH)

Parameters
valueVelocity threshold value
unitUnit of the value
Returns
Result<void> indicating success or error

◆ SetModeChangeSpeeds()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::Thresholds::SetModeChangeSpeeds ( float pwm_thrs,
float cool_thrs,
float high_thrs,
Unit unit )
noexcept

Convenience: set TPWMTHRS, TCOOLTHRS, and THIGH in one call.

Parameters
pwm_thrsSpeed threshold for StealthChop (TPWMTHRS)
cool_thrsSpeed threshold for StallGuard/CoolStep (TCOOLTHRS)
high_thrsSpeed threshold for high-speed mode (THIGH)
unitUnit of the speed values
Returns
Result<void> indicating success or error

◆ SetModeHysteresis()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::Thresholds::SetModeHysteresis ( float lower_speed,
float upper_speed,
Unit unit )
noexcept

Configure StealthChop to SpreadCycle transition with hysteresis.

Parameters
lower_speedSpeed below which StealthChop activates (lower threshold)
upper_speedSpeed above which SpreadCycle activates (upper threshold)
unitSpeed unit for both thresholds
Returns
Result<void> indicating success or error

Creates a velocity hysteresis band to prevent mode oscillation near the threshold. The motor will:

  • Switch to SpreadCycle when accelerating above upper_speed
  • Switch to StealthChop when decelerating below lower_speed

Implementation note: The TMC5160 has only one TPWMTHRS register. This method dynamically updates TPWMTHRS based on current chopper mode to create the hysteresis effect. Call UpdateModeHysteresis() periodically or after velocity changes for best results.

Example: SetModeHysteresis(50, 70, Unit::RPM) creates a 20 RPM dead band:

  • StealthChop → SpreadCycle at 70 RPM (accelerating)
  • SpreadCycle → StealthChop at 50 RPM (decelerating)
Note
Requires GCONF.en_pwm_mode = 1 (StealthChop enabled globally)
See also
Datasheet section 9.2: Switching between SpreadCycle and StealthChop

◆ SetStealthChopVelocityThreshold()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::Thresholds::SetStealthChopVelocityThreshold ( float value,
Unit unit )
noexcept

Set StealthChop velocity threshold (TPWMTHRS)

Parameters
valueVelocity threshold value
unitUnit of the value
Returns
Result<void> indicating success or error

◆ SetTcoolthrs()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::Thresholds::SetTcoolthrs ( float threshold,
Unit unit )
noexcept

Set StallGuard/CoolStep threshold velocity (TCOOLTHRS)

Parameters
thresholdThreshold velocity
unitUnit of threshold
Returns
Result<void> indicating success or error

◆ UpdateModeHysteresis()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::Thresholds::UpdateModeHysteresis ( )
noexcept

Update TPWMTHRS based on current chopper mode for hysteresis.

Returns
Result<void> indicating success or error

Call this method periodically to maintain hysteresis behavior. It reads the current chopper mode (DRV_STATUS.stealth) and updates TPWMTHRS to the appropriate threshold:

  • If in StealthChop: set TPWMTHRS to upper threshold (switch at higher speed)
  • If in SpreadCycle: set TPWMTHRS to lower threshold (switch at lower speed)
Note
Only effective after calling SetModeHysteresis() to configure the upper and lower thresholds.

Member Data Documentation

◆ driver_

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

◆ hysteresis_enabled_

template<typename CommType >
bool tmc51x0::TMC51x0< CommType >::Thresholds::hysteresis_enabled_ {false}
private

◆ hysteresis_to_spreadcycle_tstep_

template<typename CommType >
uint32_t tmc51x0::TMC51x0< CommType >::Thresholds::hysteresis_to_spreadcycle_tstep_ {0}
private

◆ hysteresis_to_stealthchop_tstep_

template<typename CommType >
uint32_t tmc51x0::TMC51x0< CommType >::Thresholds::hysteresis_to_stealthchop_tstep_ {0}
private

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