FWIW,
It could be tricky if done blindly and without at least knowing the motor characteristics.
Also, Klipper literally enables and disables diag pins on homing, which adds confusion on top of that.
And because it generally works, it is really hard to redo this thing, I suspect there will be no functional changes until homing is reimplemented in Klipper.
There is a PR with the Stallguard monitoring tool which could shine light on the actually SG values while motor is spinning.
master
← nefelim4ag:coolstep
opened 02:35PM - 07 May 24 UTC
Hi, as https://github.com/Klipper3d/klipper/pull/6139 is merged, I can now show … the stall guard measurement stuff.
I'm not sure that it is the perfect solution to poll without batch processing, and not sure about timings/frequency.
I try to mimic ADXL345 code, to avoid adding any new complexity.
My [discourse thread](https://klipper.discourse.group/t/tmc-coolstep-and-how-to-make-it-works) with more graphs, and some comments.
So, there is the problem that I'm trying to solve:
Coolstep is simple and hard to configure because there is no near real-time visibility from the user side.
The original TMC solution is to directly reproduce and measure your load.
We can't see Stallguard pin output outside of homing actions, to track stalls.
But we can poll it.
*And there are many different printers and half of them now are CoreXY, which makes things tricky here.
High-level solution:
- Monitor driver for status
- Store History with stallguard/speed/current data.
- Make it human-readable.
For now, I added the command: `MEASURE_STALLGUARD STEPPER=<stepper>`
It will start to poll/filter/store data, and on the second call - store them in csv.
```
cat /tmp/stepper_y-20240515_024227.csv
#time,velocity,sg_result,csactual
43668.927338,191.3,67,30
43668.952901,203.8,67,28
43668.977954,203.8,67,28
43669.003832,203.8,67,28
43669.363363,203.8,74,27
```
There is a dump script for data processing:
```
scripts/calibrate_stallguard.py /tmp/stepper_y-20240512_010858.csv -o ~/klipper/stepper_y-20240512_010858.png
```
Example output for one hour print with CoolStep enabled, unfiltered, cumulative graph.
```
~/klipper/scripts/calibrate_stallguard.py stepper_y-20240515_004650.csv -o stepper_y-20240515_004650_cumulative_unfiltered.png -e 100000
```

As you can see there is crazy noise on low velocities, also there are crazy speeds calculated from step timing - possibly because of step compression.
Let's filter that, and analyze it together. (min 1 mm/s, max 200 mm/s)
```
~/klipper/scripts/calibrate_stallguard.py stepper_y-20240515_004650.csv -o stepper_y-20240515_004650_cumulative_low.png -s 1 -e 200
```

There is StallGuard noise below ~35 mm/s - not enough back EMF.
The suggested SG range for StallGuard2 is in range 0..100, assuming there is more or less correct SGT (-10..0..10) value (mine is 1 or 2, depending on the motor).
We can track min & mean SG value, till they stabilize.
Min becomes zero on speeds above ~125 mm/s (> 3 RPS)
Mean becomes acceptable slightly early at ~80 mm/s. (2 RPS)
*CS is 31 on TMC5160 with reasonable sense resistor and equal CS max here.
The minimum threshold for Coolstep is 32, because SEMIN must be > 0, SEMIN = 1, (SG = SEMIN * 32)
So, I picked up the value 150 mm/s as a threshold with a safe margin, and I know my printer is limited to 1000 mm/s.
(let's just ignore high-speed diagonal moves here)
So, the last one with adequate range.
```
~/klipper/scripts/calibrate_stallguard.py stepper_y-20240515_004650.csv -o stepper_y-20240515_004650_cumulative.png -s 130 -e 1000
```

Mean SG is around ~60, if SG is above 64 current can be reduced SEMIN=1, SEMAX=0 ~ (SEMIN + SEMAX+1)*32
If below 32, the current should be bumped up.
There we are, with enabled CoolStep there is a mean CS of around 25 and min down to 16, because of SEIMIN=0 (50%).
25/31 * 100 = 80 %, 20% current reduction on medium velocity moves.
There is missing part, I didn't test the "high" velocities where backEMF is too big, because for me they are [too large](https://www.reprapfirmware.org/emf.html).

So, I can suggest assuming half of the supply voltage as back EMF is a safe margin, according to the graph.
Because up to 1000 mm/s there is still adequate response from CoolStep in my test.
---
My 2 cents:
I have only TMC5160 48V & TMC2209, so only this combination was tested.
The motors are LDO 2504AC.
I initially tuned SGT values by sensorless homing.
I so far have no issues with Coolstep, with almost default values.
CoolStep threshold will control sensorless homing, and IIRC, sensorless homing speed must be above that threshold.
On my printer, sensorless homing works fine with 50 mm/s, but with CoolStep at such a low threshold and with allowed reduction SEIMIN=1 (25% of CS) leads to skipping steps.
With an extruder, CoolStep can work, but its usefulness really depends on setup and extrusion flow.
On orbiter 2, flow shall be above 25 mm3^s. to get 2 RPS.
Will be happy to hear any feedback, even if these things are useless.
Thanks!
Raw data:
[stepper_y-20240515_004650.csv](https://github.com/Klipper3d/klipper/files/15315327/stepper_y-20240515_004650.csv)
From a configuration tuning perspective, it is more or less straightforward.
You first need to get the diag pin working, it could be hard, but it should start to at least trigger on high sensitivity, and it does not trigger on low.
Then, the only problem that you would have is literally the sensitivity configuration.
You generally want to have backEMF not zero, around 1V is already nice.
You generally want to understand how your SG value behaves.
Because, as stated in the PR and in the initial topic, it is tricky and speed-dependent, obviously.
Hi, I spent some time under the hood, klipper has a nice codebase.
So, I made TMC CoolStep work and also made some tools to measure live driver values (similar to ADXL345).
[sg_result_by_velocity_steppery_x]
[sg_result_by_velocity_stepper_y]
There are some graphs of XY.
A bit of explaining:
They are cumulative - no time here only value distribution
CS is the actual current scaler max value 31 (or IRUN)
SG is stall guard live measurements.
I had TMC5160 with ~48V power for reference, LDO…
Also, you have to be aware that Stallgurad4 used in TMC2209 is different to Stallguard2 used in TMC5160/2130, so they works and tunes slightly differently.
Also, because SG4 uses the StealthChop, there could be a minimum current limitation.
Like, with high probability, your low current settings have zero effect.
So, for example, with current defaults, where PWM_FREQ==0 (2/1024) and TBL == 2 (32).
Vm / VoHm * PWM_FREQ * TBL = Imin
24 / 2 * 2/1024 * 32 = 0.75A
(I don’t know your motor parameters or supply voltages, so you could verify it for your installation).
Also, there is a trick, like with high motor inductance and high acceleration, the motor could trigger early. It could be workarounded by decreasing current, by setting the COOLSTEP threshold >20mm/s as is done on some Prusas.
Hope that helps a little.
BTW, it obviously should be generally easier to do on the Cartesian machines where the 1 motor drives one axis.