PID tuning in LinuxCNC
linuxcnc, pid ·There are many articles about PID tuning in general and about LinuxCNC in particular. For example, here is a good article about PID tuning and man page with more nitty-gritty details or even source code with the most intimate details. That is why below will be only my personal experience together with a couple practical tips which might be useful… or not.
Metrics to collect
There are tons of metrics, however, below are most valuable for PID tuning:
joint.X.f-error
- error between planned and actual position,joint.X.vel-cmd
- velocity command,joint.X.pos-fb
- actual position,<board_name>.encoder.00.velocity
- actual velocity from encoder.
Documentation and AI
Use documentation and any available AI. Ask any AI is a good start point. Then, do the test run, collect resulted charts
as halscope.csv
(there is an option to export the charts) and feed it into AI again to obtain PID values and tips for
the next iteration. Not always, but AI might provide useful hints.
PID tune
Pick max expected feed rate and pretty distant target position. For example, 1000mm/s and 20mm distance. Place
<board_name>.encoder.00.velocity
over joint.X.vel-cmd
to clearly see how actual velocity follows the command. And
select a proper scale for joint.X.f-error
to see small oscillations.
Then, do the following steps:
- If you have configured
BACKLASH
- remove it. It will make PID tuning harder. You can return it back after the PID tweak. - Set
DEADBAND
inini
file to scale of the encoder. 0.005 in my case. - Set
FF1
to 1 and pick a really highP
. For example, 1000. Do a movement and checkjoint.X.f-error
. There should be huge oscillations, especially in the end of the path. - Reduce
P
until oscillations will be acceptable. Not so big as in point 1, but still noticeable. In my case it was ~50. - Add a bit
I
. Try something between 0.1 and 1.0. Aim: oscillations should disappear, but overall velocity should be on expectable level. IfI
is too high, velocity will be low. Kinda too smooth movement. - If unable to remove oscillations, and keep a proper velocity - increase
D
. Usually less than 1. - If you have a clear pattern in
f-error
after all tweaks - ask Google and/or AI. There are many threads on LinuxCNC forum with fixes for different border cases.
Resulted configuration
I found two working PIDs for my machine:
- very smooth movement if velocity is under 180mm/s:
P=40
,I=0.25
,D=0.9
,FF0=0
,FF1=0.05
,FF2=0
.f-error
is under 0.03mm. - Wide velocity range up to 1500mm/s, but spikes up to 0.1mm at fast and long movement:
P=50
,I=0.2
,D=0
,FF0=0
,FF1=1.0
,FF2=0
.
Other notes
- Ziegler-Nichols method didn’t work for me. But there are many success stories on the Internet.
- There is no a one single PID for the machine. Different approaches might provide a pretty the same result.
- Seems like for different target velocity might be needed different PIDs. For example, to be under 0.01mm with velocity < 100mm/s and to run on 1000mm/s+ might be needed different PIDs for the same machine.
- Obvious, but worth to mention: even if axis has the same BSD, servo etc. need to tweak PID independently for them.