Hi all,
I have been working for some time on automatic PA calibration using load cells in the hotends, and while some improvements are still needed, I think my code reached a stage where it can be tested by and potentially used on a regular basis by others. The code is published in this branch. There is also some basic configuration reference, description of gcode commands, and status reference (all subject to change; suggestions on improvements are also welcome).
Some suggestions for installation and configuration:
- You must install numpy, e.g. via
~/klippy-env/bin/pip install -v "numpy<1.26"(this is from official Klipper docs, perhaps you can actually drop that <1.26 version restriction part. - I recommend if at all possible to install scipy too via
~/klippy-env/bin/pip install scipyand `sudo apt install libatlas-base-dev libopenblas-dev`and setuse_scipy = truein the[pa_tester]configuration section. - For the load_cell, I think that
sample_rateabove 100 is required, and actually should be above 300, ideally 500+ (I personally tested at 1000 with ADS131M02). This means that, in practice, it should be possible to test with HX717 (supports 320 samples/sec), ADS1220 (supports high sampling rates, 660 is the default), and ADS131M0x (supports very high sampling rates, though I think it makes sense to stick with 500-1000) - all these ADCs are currently supported by mainline Klipper (though perhaps if you can patch a different sensor support over my branch, that should work too).
Some notable [pa_tester] parameters:
purge_lengthis the length of initial purge, this is needed to purge old potentially degraded filament from the hotend and build-up initial pressure in the hotend; I’d recommend to set it to the length of the hot side of the hotend (on Volcano, I set it to 25 mm)high_flow(in mm^3/sec) determines the fastest extrusion rate during the test, I’d recommend to set it up to 80% of a maximum typical flow rate for your hotend or below - e.g. to the maximum extrusion rate you’d be printing with the tested filament (for example, I set it to 16 mm^3/sec on Volcano hotend).slow_flowis the flow rate for slow extrusion during the test, default is 2 and can probably be left at that value (which corresponds to 25 mm/sec linear extrusion speed when printing at 0.2 mm layer height and 0.4 mm extrusion width - this would be a reasonable linear speed for features like bridges or very strong overhangs).segment_time- this is a duration of a single element of the test, default value of 0.5 seconds is good for direct drive extruders and reasonably hard filaments. If you want to test PA calibration on bowden extruders or with flexible filaments, you’ll need to increase the value of this parameter to 1-3 seconds.filter_window- the duration of signal smoothing filter, default value is 0.01 sec, though it can be increased to 0.015-0.02 sec if you suspect strong noise in the sensor output. However, if output signal has strong pulsations from power lines (at 50-60 Hz) and/or Klipper hotend/fans PWM (which is 10 Hz by default) - which may happen especially if load cell wires go near hotend or fan wires - signal filter may not help really and you should better focus on resolving these cross-talks.extrude_force_sign- this tells whether the force readings increase (1) or decrease (-1) as extrusion goes faster; the algorithms can detect this automatically, but it can also be overridden or just hard-coded for a specific loadcell for (slightly?) improved reliability.method- determines test method to be used for PA calibration.
Note that all of the parameters above can be overridden at runtime via GCode command parameters (so you can easily adjust them). As for the methods, two methods have been implemented:
step_response- this method simply oscillates between high and slow extrusion rates, measures force response, and fits it to exponential decay modelA + B * exp(-C * t) + D * t, which naturally follows from linear PA model (actual PA value is1/C, andDis introduce to account for potential sensor drift) using weighted non-linear minimal squares method.search_overshoot- this method uses binary-search-like method to test different PA values (by extruding with the tested PA value, measuring force response, and searching for signs of PA overshoot - force overshooting and then dropping) to find an optimal PA value just before an overshoot occurs.
step_response is faster and requires much less filament, but I guess it depends more on the filament flow following the theoretical model, while search_overshoot is slower and requires more plastic, but it measures effects of Pressure Advance ‘more directly’ if you will. For testing, it would be great to try both out, and then print more regular PA tests with the same filament to compare results, and then, most importantly, try to print real prints with the PA values from both methods (if they are different) to see how the automatically calibrated PA values work for real-life prints (so, I’d say, not just compare against more classical PA print tests, but also try to print with the calibrated PA value(s) and see how they work). Also, during automatic PA tests please scrape debug data using motan script:
python3 ~/klipper/scripts/motan/data_logger.py ~/printer_data/comms/klippy.sock /tmp/<some-test-name> -s '*'
especially if you run into some issues, and attach both generated files per test to you report alongside with klippy.log - it would help a ton to collect more real-life data.
On a separate note, I am aware of other recent (and not so recent) projects in this area, e.g. PrusaPATuner (by CNC Kitchen), bd_pressure (semi-opensource project), autopa (partially a port of PATuner to Klipper) to name a few. However, my implementation is completely independent and not based on any of them, though basic principles behind the approaches are somewhat similar. That said, given that this area got a lot of momentum and interest both from users and developers recently, I really hope we can merge some working version of automatic PA calibration into the mainline Klipper some time soon.