Skip to content

StallGuard

Christian edited this page Nov 11, 2023 · 1 revision

StallGuard

The TMC2209 driver has the capability to measure the motor load. This can be used to detect stalls during movement or to perform a sensorless homing.

grafik

How to use StallGuard

The driver calculates the StallGuard result (SG_RESULT; 0 - 510) internally and compares this with the double of the StallGuard threshold (SGTHRS; 0 - 255). You can get the StallGuard result with this function:

get_stallguard_result()

and you can set the StallGuard threshold with this function:

set_stallguard_threshold(threshold)

When the driver detects a stall (SG_RESULT < 2* SGTHRS), the DIAG pin is pulsed. Inside this library there is a function which can be used to attach a userdefined callback function to StallGuard via interrupt on a Raspberry Pi pin which should be connected to the DIAG pin of the TMC2209:

set_stallguard_callback(pin_stallguard, threshold, callback)

StallGuard only works in StealthChop mode and above the speed of TCOOLTHRS (TCOOLTHRS ≥ TSTEP > TPWMTHRS), this can be set with the function:

set_coolstep_threshold(threshold)

because TCOOLTHRS is compared to TSTEP which is the time for 1/256 microstep (1 - 2^20-1) the set_stallguard_callback function has a parameter min_speed which internally calculates the TCOOLTHRS for a given velocity in steps/seconds. Just like set_max_speed(speed). The min_speed for StallGuard should be set the value slightly lower than your max_speed (like 800/1000) to prevent StallGuard from triggering while accelerating.

StallGuard tuning

The datasheet has a chapter (11.2 Tuning StallGuard4; page 59) which describes the procedure to tune StallGuard for your setup: Monitoring the SG_Result value can be done with this function:

test_stallguard_threshold(self, steps)

It prints the current movement phase (accel, max_speed or decel) and the current SG_Result. Note that this is not synchronised with each step.

Clone this wiki locally