HF-TMC51x0 Driver (TMC5130 & TMC5160) 0.1.0-dev
Hardware Agnostic C++ Driver for the TMC51x0 (TMC5130 & TMC5160)
Loading...
Searching...
No Matches
tmc51x0_config_builder.hpp
Go to the documentation of this file.
1
71#pragma once
72#include <cstdint>
73#include "../tmc51x0_types.hpp"
74
75namespace tmc51x0 {
76
88private:
90
91public:
98 ConfigBuilder() = default;
99
105 explicit ConfigBuilder(const DriverConfig& cfg) : config_(cfg) {}
106
107 // ========== MOTOR SPECIFICATION ==========
108
122 ConfigBuilder& WithMotorMa(uint16_t steps_per_rev, uint16_t rated_current_ma) {
123 config_.motor_spec.steps_per_rev = steps_per_rev;
124 config_.motor_spec.rated_current_ma = rated_current_ma;
125 return *this;
126 }
127
134 ConfigBuilder& WithRunCurrentMa(uint16_t run_current_ma) {
135 config_.motor_spec.run_current_ma = run_current_ma;
136 return *this;
137 }
138
145 ConfigBuilder& WithHoldCurrentMa(uint16_t hold_current_ma) {
146 config_.motor_spec.hold_current_ma = hold_current_ma;
147 return *this;
148 }
149
160 ConfigBuilder& WithSupplyVoltageMv(uint32_t voltage_mv) {
162 return *this;
163 }
164
177 ConfigBuilder& WithSenseResistorMohm(uint32_t resistance_mohm) {
178 config_.motor_spec.sense_resistor_mohm = resistance_mohm;
179 return *this;
180 }
181
190 ConfigBuilder& WithWindingResistanceMohm(uint32_t resistance_mohm) {
192 return *this;
193 }
194
205 return *this;
206 }
207
220 return *this;
221 }
222
231 return *this;
232 }
233
234 // ========== BOARD CONFIGURATION (Power Stage) ==========
235
246 return *this;
247 }
248
255 ConfigBuilder& WithBbmTimeNs(uint32_t time_ns) {
257 return *this;
258 }
259
267 config_.power_stage.sense_filter = filter_time;
268 return *this;
269 }
270
279 return *this;
280 }
281
288 ConfigBuilder& WithS2vsVoltageMv(uint16_t voltage_mv) {
290 return *this;
291 }
292
299 ConfigBuilder& WithS2gVoltageMv(uint16_t voltage_mv) {
301 return *this;
302 }
303
312 return *this;
313 }
314
323 return *this;
324 }
325
326 // ========== CHOPPER CONFIGURATION ==========
327
335 config_.chopper.mode = mode;
336 return *this;
337 }
338
346 config_.chopper.toff = toff;
347 return *this;
348 }
349
357 config_.chopper.tbl = static_cast<uint8_t>(blank_time);
358 return *this;
359 }
360
368 config_.chopper.hstrt = hstrt;
369 return *this;
370 }
371
379 config_.chopper.hend = hend;
380 return *this;
381 }
382
390 config_.chopper.tfd = tfd;
391 return *this;
392 }
393
401 config_.chopper.tpfd = tpfd;
402 return *this;
403 }
404
412 config_.chopper.mres = mres;
413 return *this;
414 }
415
423 config_.chopper.intpol = enable;
424 return *this;
425 }
426
427 // ========== STEALTHCHOP CONFIGURATION ==========
428
437 return *this;
438 }
439
453
462 return *this;
463 }
464
473 return *this;
474 }
475
484 return *this;
485 }
486
495 return *this;
496 }
497
498 // ========== STALLGUARD CONFIGURATION ==========
499
517 config_.stallguard.threshold = threshold;
518 return *this;
519 }
520
532 return *this;
533 }
534
552 return *this;
553 }
554
567 return *this;
568 }
569
588 config_.stallguard = sg_config;
589 return *this;
590 }
591
592 // ========== GLOBAL CONFIGURATION ==========
593
602 return *this;
603 }
604
613 return *this;
614 }
615
624 return *this;
625 }
626
627 // ========== MOTION CONFIGURATION (Ramp) ==========
628
641 config_.ramp_config.vmax = velocity;
642 return *this;
643 }
644
657 config_.ramp_config.amax = acceleration;
658 return *this;
659 }
660
668 config_.ramp_config.dmax = deceleration;
669 return *this;
670 }
671
679 config_.ramp_config.vstart = velocity;
680 return *this;
681 }
682
690 config_.ramp_config.vstop = velocity;
691 return *this;
692 }
693
701 config_.ramp_config.v1 = velocity;
702 return *this;
703 }
704
712 config_.ramp_config.a1 = acceleration;
713 return *this;
714 }
715
723 config_.ramp_config.d1 = deceleration;
724 return *this;
725 }
726
738 return *this;
739 }
740
749 return *this;
750 }
751
752 // ========== CLOCK CONFIGURATION ==========
753
763
770 ConfigBuilder& WithExternalClockHz(uint32_t frequency_hz) {
772 return *this;
773 }
774
775 // ========== MECHANICAL SYSTEM ==========
776
790
805
820
828 ConfigBuilder& WithBeltDrive(uint16_t pulley_teeth, float belt_pitch_mm) {
830 config_.mechanical.belt_pulley_teeth = pulley_teeth;
831 config_.mechanical.belt_pitch_mm = belt_pitch_mm;
834 return *this;
835 }
836
837 // ========== DIRECTION ==========
838
846 config_.direction = direction;
847 return *this;
848 }
849
850 // ========== BUILD ==========
851
864 return config_;
865 }
866
876 return config_;
877 }
878
884 const DriverConfig& Config() const {
885 return config_;
886 }
887};
888
889} // namespace tmc51x0
Fluent configuration builder for DriverConfig.
Definition tmc51x0_config_builder.hpp:87
ConfigBuilder & WithFirstDeceleration(const AccelerationValue &deceleration)
Set first deceleration phase.
Definition tmc51x0_config_builder.hpp:722
ConfigBuilder & WithRampPowerDownDelayMs(float delay_ms)
Set ramp power down delay.
Definition tmc51x0_config_builder.hpp:736
ConfigBuilder & WithStallGuardThreshold(int8_t threshold)
Set StallGuard2 threshold (SGT)
Definition tmc51x0_config_builder.hpp:516
ConfigBuilder & WithLeadScrew(float pitch_mm)
Configure lead screw system.
Definition tmc51x0_config_builder.hpp:812
ConfigBuilder & WithShortFilter(uint8_t filter)
Set short detection filter bandwidth.
Definition tmc51x0_config_builder.hpp:310
ConfigBuilder & WithHysteresisEnd(uint8_t hend)
Set hysteresis end value (SpreadCycle mode)
Definition tmc51x0_config_builder.hpp:378
ConfigBuilder & WithMaxSpeed(const VelocityValue &velocity)
Set maximum velocity.
Definition tmc51x0_config_builder.hpp:640
ConfigBuilder & WithShortDetectionDelayUsX10(uint8_t delay_us_x10)
Set short detection delay.
Definition tmc51x0_config_builder.hpp:321
ConfigBuilder & WithStealthChopPwmOfs(uint8_t ofs)
Set StealthChop PWM offset.
Definition tmc51x0_config_builder.hpp:471
ConfigBuilder & WithS2gVoltageMv(uint16_t voltage_mv)
Set short to GND detector voltage threshold.
Definition tmc51x0_config_builder.hpp:299
ConfigBuilder & WithWindingInductanceMh(float inductance_mh)
Set winding inductance value.
Definition tmc51x0_config_builder.hpp:203
ConfigBuilder & WithHoldCurrentMa(uint16_t hold_current_ma)
Set hold current value.
Definition tmc51x0_config_builder.hpp:145
ConfigBuilder & WithVelocityThreshold(const VelocityValue &velocity)
Set velocity threshold for acceleration/deceleration phase transition.
Definition tmc51x0_config_builder.hpp:700
ConfigBuilder & WithStealthChop(bool enable)
Enable/disable StealthChop.
Definition tmc51x0_config_builder.hpp:435
DriverConfig Build() const
Build the final configuration.
Definition tmc51x0_config_builder.hpp:863
DriverConfig & Config()
Get mutable reference to config for advanced customization.
Definition tmc51x0_config_builder.hpp:875
ConfigBuilder & WithMicrostepResolution(MicrostepResolution mres)
Set microstep resolution.
Definition tmc51x0_config_builder.hpp:411
ConfigBuilder & WithMotorMa(uint16_t steps_per_rev, uint16_t rated_current_ma)
Configure motor specifications.
Definition tmc51x0_config_builder.hpp:122
ConfigBuilder & WithS2vsVoltageMv(uint16_t voltage_mv)
Set short to VS detector voltage threshold.
Definition tmc51x0_config_builder.hpp:288
ConfigBuilder & WithMotorPowerDownDelayMs(float delay_ms)
Set motor power down delay time.
Definition tmc51x0_config_builder.hpp:218
ConfigBuilder(const DriverConfig &cfg)
Construct from existing config.
Definition tmc51x0_config_builder.hpp:105
ConfigBuilder & WithStallGuardMaxVelocity(float velocity, Unit unit=Unit::RPM)
Set StallGuard2 maximum velocity threshold.
Definition tmc51x0_config_builder.hpp:564
ConfigBuilder & WithStallGuard(const StallGuardConfig &sg_config)
Configure StallGuard2 with a complete config struct.
Definition tmc51x0_config_builder.hpp:587
ConfigBuilder & WithFastDecayTime(uint8_t tfd)
Set fast decay time (Classic mode only)
Definition tmc51x0_config_builder.hpp:389
ConfigBuilder & WithInternalClock()
Use internal clock (12MHz)
Definition tmc51x0_config_builder.hpp:759
ConfigBuilder & WithStealthChopAutoscale(bool enable)
Enable/disable StealthChop PWM autoscale.
Definition tmc51x0_config_builder.hpp:482
ConfigBuilder & WithExternalClockHz(uint32_t frequency_hz)
Use external clock.
Definition tmc51x0_config_builder.hpp:770
ConfigBuilder & WithDirectDrive()
Configure direct drive system.
Definition tmc51x0_config_builder.hpp:782
ConfigBuilder & WithRecalibration(bool enable)
Enable/disable recalibration.
Definition tmc51x0_config_builder.hpp:600
ConfigBuilder & WithStallGuardMinVelocity(float velocity, Unit unit=Unit::RPM)
Set StallGuard2 minimum velocity threshold.
Definition tmc51x0_config_builder.hpp:549
ConfigBuilder & WithChopperToff(uint8_t toff)
Set chopper off-time (TOFF)
Definition tmc51x0_config_builder.hpp:345
ConfigBuilder & WithGearbox(float gear_ratio)
Configure gearbox system.
Definition tmc51x0_config_builder.hpp:797
ConfigBuilder & WithInterpolation(bool enable)
Enable/disable interpolation.
Definition tmc51x0_config_builder.hpp:422
DriverConfig config_
Definition tmc51x0_config_builder.hpp:89
ConfigBuilder & WithOverTempProtection(OverTempProtection protection)
Set over-temperature protection level.
Definition tmc51x0_config_builder.hpp:277
ConfigBuilder & WithMosfetMillerChargeNc(float charge_nc)
Set MOSFET Miller charge.
Definition tmc51x0_config_builder.hpp:244
ConfigBuilder & WithStealthChopStepFilter(bool enable)
Enable/disable StealthChop step filter.
Definition tmc51x0_config_builder.hpp:622
ConfigBuilder & WithHysteresisStart(uint8_t hstrt)
Set hysteresis start value (SpreadCycle mode)
Definition tmc51x0_config_builder.hpp:367
ConfigBuilder & WithAcceleration(const AccelerationValue &acceleration)
Set maximum acceleration.
Definition tmc51x0_config_builder.hpp:656
ConfigBuilder & WithDirection(MotorDirection direction)
Set motor direction.
Definition tmc51x0_config_builder.hpp:845
ConfigBuilder()=default
Default constructor.
ConfigBuilder & WithStealthChopAutograd(bool enable)
Enable/disable StealthChop PWM autograd.
Definition tmc51x0_config_builder.hpp:493
ConfigBuilder & WithStopSpeed(const VelocityValue &velocity)
Set stop velocity.
Definition tmc51x0_config_builder.hpp:689
ConfigBuilder & WithRunCurrentMa(uint16_t run_current_ma)
Set run current value.
Definition tmc51x0_config_builder.hpp:134
ConfigBuilder & WithSenseResistorMohm(uint32_t resistance_mohm)
Set sense resistor value.
Definition tmc51x0_config_builder.hpp:177
ConfigBuilder & WithShortStandstillTimeout(bool enable)
Enable/disable short standstill timeout.
Definition tmc51x0_config_builder.hpp:611
ConfigBuilder & WithWindingResistanceMohm(uint32_t resistance_mohm)
Set winding resistance value.
Definition tmc51x0_config_builder.hpp:190
ConfigBuilder & WithPassiveFastDecayTime(uint8_t tpfd)
Set passive fast decay time.
Definition tmc51x0_config_builder.hpp:400
ConfigBuilder & WithBeltDrive(uint16_t pulley_teeth, float belt_pitch_mm)
Configure belt drive system.
Definition tmc51x0_config_builder.hpp:828
ConfigBuilder & WithStallGuardFilter(bool enable)
Enable/disable StallGuard2 filter.
Definition tmc51x0_config_builder.hpp:530
ConfigBuilder & WithStealthChopPwmFreq(uint8_t freq)
Set StealthChop PWM frequency.
Definition tmc51x0_config_builder.hpp:460
const DriverConfig & Config() const
Get const reference to config.
Definition tmc51x0_config_builder.hpp:884
ConfigBuilder & WithFirstAcceleration(const AccelerationValue &acceleration)
Set first acceleration phase.
Definition tmc51x0_config_builder.hpp:711
ConfigBuilder & WithChopperMode(ChopperMode mode)
Set chopper mode.
Definition tmc51x0_config_builder.hpp:334
ConfigBuilder & WithStealthChopThreshold(const VelocityValue &velocity)
Set StealthChop threshold velocity.
Definition tmc51x0_config_builder.hpp:448
ConfigBuilder & WithStepsPerRevolution(uint16_t steps)
Set steps per revolution.
Definition tmc51x0_config_builder.hpp:229
ConfigBuilder & WithStartSpeed(const VelocityValue &velocity)
Set start velocity.
Definition tmc51x0_config_builder.hpp:678
ConfigBuilder & WithSupplyVoltageMv(uint32_t voltage_mv)
Set supply voltage value.
Definition tmc51x0_config_builder.hpp:160
ConfigBuilder & WithZeroWaitTimeMs(float wait_ms)
Set zero velocity wait time.
Definition tmc51x0_config_builder.hpp:747
ConfigBuilder & WithChopperBlankTime(ChopperBlankTime blank_time)
Set chopper blank time.
Definition tmc51x0_config_builder.hpp:356
ConfigBuilder & WithBbmTimeNs(uint32_t time_ns)
Set Break Before Make time.
Definition tmc51x0_config_builder.hpp:255
ConfigBuilder & WithSenseFilter(SenseFilterTime filter_time)
Set sense amplifier filter time.
Definition tmc51x0_config_builder.hpp:266
ConfigBuilder & WithDeceleration(const AccelerationValue &deceleration)
Set maximum deceleration.
Definition tmc51x0_config_builder.hpp:667
Definition tmc51x0_register_defs.cpp:10
MicrostepResolution
Microstep resolution enumeration.
Definition tmc51x0_types.hpp:707
OverTempProtection
Over-temperature protection level enumeration.
Definition tmc51x0_types.hpp:486
ChopperBlankTime
Comparator blank time enumeration.
Definition tmc51x0_types.hpp:692
@ Gearbox
Gearbox reduction.
@ LeadScrew
Lead screw drive.
@ BeltDrive
Belt drive with pulleys.
@ DirectDrive
Direct drive (motor shaft directly connected)
Unit
Unit enumeration.
Definition tmc51x0_types.hpp:176
@ RPM
Revolutions per Minute (Velocity only, typically)
MotorDirection
Motor direction enumeration.
Definition tmc51x0_types.hpp:95
SenseFilterTime
Sense amplifier filter time constant enumeration.
Definition tmc51x0_types.hpp:471
ChopperMode
Chopper mode enumeration.
Definition tmc51x0_types.hpp:671
Self-describing acceleration value with explicit unit.
Definition tmc51x0_types.hpp:1812
ChopperMode mode
Chopper mode selection.
Definition tmc51x0_types.hpp:867
uint8_t hend
Hysteresis end value (SpreadCycle mode) or Sine wave offset (Classic mode)
Definition tmc51x0_types.hpp:947
bool intpol
Enable interpolation to 256 microsteps.
Definition tmc51x0_types.hpp:1019
uint8_t toff
Off time (slow decay time) setting.
Definition tmc51x0_types.hpp:891
uint8_t hstrt
Hysteresis start value (SpreadCycle mode only)
Definition tmc51x0_types.hpp:929
uint8_t tfd
Fast decay time (Classic mode only)
Definition tmc51x0_types.hpp:964
uint8_t tpfd
Passive fast decay time.
Definition tmc51x0_types.hpp:991
MicrostepResolution mres
Microstep resolution.
Definition tmc51x0_types.hpp:1006
uint8_t tbl
Comparator blank time.
Definition tmc51x0_types.hpp:908
Driver initialization configuration structure.
Definition tmc51x0_types.hpp:2870
RampConfig ramp_config
Ramp generator configuration (with unit support)
Definition tmc51x0_types.hpp:2879
StallGuardConfig stallguard
StallGuard2 configuration (defaults: threshold=0, disabled)
Definition tmc51x0_types.hpp:2886
ExternalClockConfig external_clk_config
External clock configuration (0 = use internal 12 MHz, >0 = external clock frequency)
Definition tmc51x0_types.hpp:2883
GlobalConfig global_config
Global configuration (GCONF register)
Definition tmc51x0_types.hpp:2878
MotorDirection direction
Motor direction (normal or inverse)
Definition tmc51x0_types.hpp:2875
MotorSpec motor_spec
Motor specifications (physical parameters for automatic current calculation)
Definition tmc51x0_types.hpp:2871
PowerStageParameters power_stage
Power stage configuration (includes short protection)
Definition tmc51x0_types.hpp:2872
MechanicalSystem mechanical
Mechanical system configuration (gearing, leadscrew, etc.) for unit conversions.
Definition tmc51x0_types.hpp:2873
ChopperConfig chopper
Chopper configuration (SpreadCycle or Classic mode)
Definition tmc51x0_types.hpp:2876
StealthChopConfig stealthchop
StealthChop configuration.
Definition tmc51x0_types.hpp:2877
uint32_t frequency_hz
External clock frequency in Hz.
Definition tmc51x0_types.hpp:2669
bool en_stealthchop_step_filter
Definition tmc51x0_types.hpp:2759
bool en_stealthchop_mode
Definition tmc51x0_types.hpp:2757
bool en_short_standstill_timeout
Definition tmc51x0_types.hpp:2752
bool recalibrate
Bit 0: Zero crossing recalibration during driver disable.
Definition tmc51x0_types.hpp:2751
float belt_pitch_mm
Belt pitch in mm (for BeltDrive)
Definition tmc51x0_types.hpp:294
uint16_t belt_pulley_teeth
Number of teeth on motor pulley (for BeltDrive)
Definition tmc51x0_types.hpp:293
MechanicalSystemType system_type
Type of mechanical system.
Definition tmc51x0_types.hpp:291
float gear_ratio
Gear ratio (output/input, for Gearbox)
Definition tmc51x0_types.hpp:295
float lead_screw_pitch_mm
Lead screw pitch in mm (for LeadScrew)
Definition tmc51x0_types.hpp:292
uint16_t steps_per_rev
Definition tmc51x0_types.hpp:329
uint32_t supply_voltage_mv
Motor supply voltage in millivolts (e.g., 24000 for 24V, 0 = not specified)
Definition tmc51x0_types.hpp:341
uint32_t sense_resistor_mohm
Sense resistor value in milliohms (e.g., 50 for 0.05Ω, 0 = not specified)
Definition tmc51x0_types.hpp:340
uint16_t hold_current_ma
Desired hold current in milliamps (0 = auto-calculate as 30% of run)
Definition tmc51x0_types.hpp:337
uint16_t run_current_ma
Desired run current in milliamps (0 = use rated_current_ma)
Definition tmc51x0_types.hpp:336
uint32_t winding_resistance_mohm
Winding resistance in milliohms (required for StealthChop lower limit calc)
Definition tmc51x0_types.hpp:332
float iholddelay_ms
Total motor power down delay time (IHOLDDELAY)
Definition tmc51x0_types.hpp:444
float winding_inductance_mh
Winding inductance in millihenries (optional, 0 = not specified) (for StealthChop)
Definition tmc51x0_types.hpp:333
uint16_t rated_current_ma
Rated motor current in milliamps (RMS)
Definition tmc51x0_types.hpp:331
float mosfet_miller_charge_nc
MOSFET Miller charge in nanocoulombs (used to calculate DRVSTRENGTH)
Definition tmc51x0_types.hpp:519
OverTempProtection over_temp_protection
Over-temperature protection level for bridge disable.
Definition tmc51x0_types.hpp:569
uint16_t s2vs_voltage_mv
Short to VS detector voltage threshold in millivolts.
Definition tmc51x0_types.hpp:588
uint32_t bbm_time_ns
Break Before Make time in nanoseconds.
Definition tmc51x0_types.hpp:549
SenseFilterTime sense_filter
Sense amplifier filter time constant.
Definition tmc51x0_types.hpp:559
uint16_t s2g_voltage_mv
Short to GND detector voltage threshold in millivolts.
Definition tmc51x0_types.hpp:608
uint8_t shortfilter
Spike filtering bandwidth for short detection (0-3)
Definition tmc51x0_types.hpp:619
uint8_t short_detection_delay_us_x10
Short detection delay in microseconds (0.1µs resolution)
Definition tmc51x0_types.hpp:638
VelocityValue vstart
Start velocity (0 = can be set to zero if not used)
Definition tmc51x0_types.hpp:1857
AccelerationValue dmax
Maximum deceleration (used above V1, 0 = uses AMAX value)
Definition tmc51x0_types.hpp:1865
float tzerowait_ms
Velocity-zero wait time (TZEROWAIT)
Definition tmc51x0_types.hpp:1918
AccelerationValue a1
First acceleration (used between VSTART and V1, 0 = use AMAX)
Definition tmc51x0_types.hpp:1864
float tpowerdown_ms
Definition tmc51x0_types.hpp:1869
AccelerationValue amax
Maximum acceleration (used above V1, 0 = must be set via SetAcceleration() before motion)
Definition tmc51x0_types.hpp:1863
VelocityValue v1
Transition velocity (switches between A1/AMAX and D1/DMAX, 0 = disabled)
Definition tmc51x0_types.hpp:1860
VelocityValue vstop
Stop velocity (must be >= VSTART, minimum 1 recommended)
Definition tmc51x0_types.hpp:1858
VelocityValue vmax
Maximum velocity (0 = must be set via SetMaxSpeed() before motion)
Definition tmc51x0_types.hpp:1859
AccelerationValue d1
First deceleration (used between VSTOP and V1, must not be 0 in positioning mode)
Definition tmc51x0_types.hpp:1866
StallGuard2 configuration structure.
Definition tmc51x0_types.hpp:1519
bool enable_filter
Enable StallGuard2 filter.
Definition tmc51x0_types.hpp:1548
float min_velocity
Lower velocity threshold for StallGuard2 operation.
Definition tmc51x0_types.hpp:1560
Unit velocity_unit
Unit for velocity thresholds.
Definition tmc51x0_types.hpp:1578
float max_velocity
Upper velocity threshold for StallGuard2 operation.
Definition tmc51x0_types.hpp:1571
int8_t threshold
StallGuard2 threshold value.
Definition tmc51x0_types.hpp:1534
uint8_t pwm_ofs
PWM amplitude offset.
Definition tmc51x0_types.hpp:1256
Unit velocity_threshold_unit
Unit for velocity threshold.
Definition tmc51x0_types.hpp:1417
float velocity_threshold
StealthChop velocity threshold (TPWMTHRS)
Definition tmc51x0_types.hpp:1410
bool pwm_autoscale
Enable automatic current scaling.
Definition tmc51x0_types.hpp:1314
uint8_t pwm_freq
PWM frequency selection.
Definition tmc51x0_types.hpp:1293
bool pwm_autograd
Enable automatic gradient adaptation.
Definition tmc51x0_types.hpp:1332
Self-describing velocity value with explicit unit.
Definition tmc51x0_types.hpp:1778
float value
Velocity magnitude.
Definition tmc51x0_types.hpp:1779
Unit unit
Velocity unit.
Definition tmc51x0_types.hpp:1780
Type definitions and enumerations for TMC51x0 stepper motor driver (TMC5130 & TMC5160)