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

Encoder subsystem. More...

#include <tmc51x0.hpp>

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

Public Member Functions

 Encoder (TMC51x0 &driver) noexcept
 Construct encoder subsystem.
 
Result< void > Configure (const EncoderConfig &config) noexcept
 Configure encoder settings.
 
Result< EncoderConfigGetEncoderConfig () noexcept
 Get current encoder configuration.
 
Result< void > SetNChannelActiveLevel (ReferenceSwitchActiveLevel active_level) noexcept
 Set N channel active level (determines polarity)
 
Result< void > SetNChannelSensitivity (EncoderNSensitivity sensitivity) noexcept
 Set N channel sensitivity (edge/level detection)
 
Result< void > SetClearMode (EncoderClearMode clear_mode) noexcept
 Set encoder clear mode.
 
Result< void > SetPrescalerMode (EncoderPrescalerMode prescaler_mode) noexcept
 Set encoder prescaler mode.
 
Result< void > SetLatchXactualEnabled (bool enable) noexcept
 Enable/disable latching XACTUAL with encoder position.
 
Result< bool > IsLatchXactualEnabled () noexcept
 Check if XACTUAL latching is enabled.
 
Result< int32_t > GetPosition () noexcept
 Get encoder position.
 
Result< void > SetResolution (int32_t motor_steps, int32_t enc_resolution, bool inverted=false) noexcept
 Set encoder resolution.
 
Result< void > SetAllowedDeviation (int32_t steps) noexcept
 Set encoder allowed deviation.
 
Result< bool > IsNEventDetected () noexcept
 Check if an encoder N-event was detected (ENC_STATUS.n_event)
 
Result< void > ClearNEventFlag () noexcept
 Clear the encoder N-event flag (ENC_STATUS.n_event, W1C)
 
Result< int32_t > GetLatchedPosition () noexcept
 Get encoder latched position.
 
Result< bool > IsDeviationWarning () noexcept
 Check if encoder deviation warning is active.
 
Result< void > ClearDeviationWarning () noexcept
 Clear encoder deviation warning flag.
 

Private Attributes

TMC51x0driver_
 Reference to parent driver instance.
 

Detailed Description

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

Encoder subsystem.

Provides methods for encoder configuration and reading encoder position.

Constructor & Destructor Documentation

◆ Encoder()

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

Construct encoder subsystem.

Parameters
driverReference to parent TMC51x0 driver instance

Member Function Documentation

◆ ClearDeviationWarning()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::Encoder::ClearDeviationWarning ( )
noexcept

Clear encoder deviation warning flag.

Returns
Result<void> indicating success or error

Attempts to clear the deviation warning flag. The flag will only clear if the deviation is no longer present (X_ENC and XACTUAL are within ENC_DEVIATION steps).

Note
This writes '1' to the deviation_warn bit in ENC_STATUS (W1C).

◆ ClearNEventFlag()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::Encoder::ClearNEventFlag ( )
noexcept

Clear the encoder N-event flag (ENC_STATUS.n_event, W1C)

Returns
Result<void> indicating success or error
Return values
INVALID_STATEif called while in external STEP/DIR mode

◆ Configure()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::Encoder::Configure ( const EncoderConfig & config)
noexcept

Configure encoder settings.

Parameters
configEncoder configuration structure
Returns
Result<void> indicating success or error
Note
Encoder registers are only functional when the chip is using the internal ramp generator (SD_MODE=LOW). In external STEP/DIR mode the encoder pins are repurposed (DCEN/DCIN/DCO) and encoder functionality is not available.
Return values
INVALID_STATEif called while in external STEP/DIR mode

◆ GetEncoderConfig()

template<typename CommType >
Result< EncoderConfig > tmc51x0::TMC51x0< CommType >::Encoder::GetEncoderConfig ( )
noexcept

Get current encoder configuration.

Parameters
configReference to store current configuration
Returns
Result<EncoderConfig> containing the value or error
Return values
INVALID_STATEif called while in external STEP/DIR mode

◆ GetLatchedPosition()

template<typename CommType >
Result< int32_t > tmc51x0::TMC51x0< CommType >::Encoder::GetLatchedPosition ( )
noexcept

Get encoder latched position.

Parameters
positionReference to store encoder position latched on N event
Returns
Result<int32_t> containing the value or error

Reads the encoder position that was latched on the last N channel event.

Return values
INVALID_STATEif called while in external STEP/DIR mode

◆ GetPosition()

template<typename CommType >
Result< int32_t > tmc51x0::TMC51x0< CommType >::Encoder::GetPosition ( )
noexcept

Get encoder position.

Parameters
positionReference to store encoder position in steps
Returns
Result<int32_t> containing the value or error
Return values
INVALID_STATEif called while in external STEP/DIR mode

◆ IsDeviationWarning()

template<typename CommType >
Result< bool > tmc51x0::TMC51x0< CommType >::Encoder::IsDeviationWarning ( )
noexcept

Check if encoder deviation warning is active.

Returns
Result<bool> true if deviation between X_ENC and XACTUAL exceeds the threshold set by SetAllowedDeviation()

Deviation detection compares the encoder position (X_ENC) with the internal motor position (XACTUAL). If they differ by more than ENC_DEVIATION steps, a warning is triggered.

This is useful for:

  • Detecting lost steps (motor missed steps due to overload)
  • Detecting mechanical slippage
  • Closed-loop position verification
Note
Set ENC_DEVIATION to 0 to disable deviation detection.
The warning cannot be cleared while deviation persists.
See also
SetAllowedDeviation()

◆ IsLatchXactualEnabled()

template<typename CommType >
Result< bool > tmc51x0::TMC51x0< CommType >::Encoder::IsLatchXactualEnabled ( )
noexcept

Check if XACTUAL latching is enabled.

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

◆ IsNEventDetected()

template<typename CommType >
Result< bool > tmc51x0::TMC51x0< CommType >::Encoder::IsNEventDetected ( )
noexcept

Check if an encoder N-event was detected (ENC_STATUS.n_event)

Returns
Result<bool> true if an N-event is latched, false otherwise

ENC_STATUS.n_event is a write-1-to-clear flag set by the hardware on an N-channel event (as configured by ENCMODE).

Return values
INVALID_STATEif called while in external STEP/DIR mode

◆ SetAllowedDeviation()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::Encoder::SetAllowedDeviation ( int32_t steps)
noexcept

Set encoder allowed deviation.

Parameters
stepsMaximum number of steps deviation before warning
Returns
Result<void> indicating success or error

◆ SetClearMode()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::Encoder::SetClearMode ( EncoderClearMode clear_mode)
noexcept

Set encoder clear mode.

Parameters
clear_modeClear mode (DISABLED, ONCE, CONTINUOUS)
Returns
Result<void> indicating success or error

Updates only clear mode, preserving other settings.

◆ SetLatchXactualEnabled()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::Encoder::SetLatchXactualEnabled ( bool enable)
noexcept

Enable/disable latching XACTUAL with encoder position.

Parameters
enabletrue to latch XACTUAL together with X_ENC on N-event
Returns
Result<void> indicating success or error

When enabled, the motor position (XACTUAL) is latched into X_LATCH simultaneously with the encoder position on N-channel events. This is useful for precise position verification and homing.

Updates only latch_x_act, preserving other ENCMODE settings.

See also
Datasheet Section 20: Encoder Interface, ENCMODE.latch_x_act

◆ SetNChannelActiveLevel()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::Encoder::SetNChannelActiveLevel ( ReferenceSwitchActiveLevel active_level)
noexcept

Set N channel active level (determines polarity)

Parameters
active_levelActive level (ACTIVE_LOW or ACTIVE_HIGH)
Returns
Result<void> indicating success or error

Updates only the active level, preserving other settings. Shares ReferenceSwitchActiveLevel enum with reference switches.

◆ SetNChannelSensitivity()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::Encoder::SetNChannelSensitivity ( EncoderNSensitivity sensitivity)
noexcept

Set N channel sensitivity (edge/level detection)

Parameters
sensitivitySensitivity mode (ACTIVE_LEVEL, RISING_EDGE, FALLING_EDGE, BOTH_EDGES)
Returns
Result<void> indicating success or error

Updates only sensitivity, preserving other settings.

◆ SetPrescalerMode()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::Encoder::SetPrescalerMode ( EncoderPrescalerMode prescaler_mode)
noexcept

Set encoder prescaler mode.

Parameters
prescaler_modePrescaler mode (BINARY or DECIMAL)
Returns
Result<void> indicating success or error

Updates only prescaler mode, preserving other settings.

◆ SetResolution()

template<typename CommType >
Result< void > tmc51x0::TMC51x0< CommType >::Encoder::SetResolution ( int32_t motor_steps,
int32_t enc_resolution,
bool inverted = false )
noexcept

Set encoder resolution.

Parameters
motor_stepsNumber of steps per turn for the motor
enc_resolutionActual encoder resolution (pulses per turn)
invertedWhether encoder and motor rotations are inverted
Returns
Result<void> indicating success or error

Member Data Documentation

◆ driver_

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

Reference to parent driver instance.


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