]> pere.pagekite.me Git - homepage.git/blob - blog/data/2022-07-16-linuxcnc-pid-autotuning.txt
New post, on megactl.
[homepage.git] / blog / data / 2022-07-16-linuxcnc-pid-autotuning.txt
1 Title: Automatic LinuxCNC servo PID tuning?
2 Tags: english, debian, linuxcnc, 3d-printer, robot
3 Date: 2022-07-16 22:30
4
5 <p>While working on a CNC with servo motors controlled by the
6 <a href="https://en.wikipedia.org/wiki/LinuxCNC">LinuxCNC</a>
7 <a href="https://en.wikipedia.org/wiki/PID_controller">PID
8 controller</a>, I recently had to learn how to tune the collection of values
9 that control such mathematical machinery that a PID controller is. It
10 proved to be a lot harder than I hoped, and I still have not succeeded
11 in getting the Z PID controller to successfully defy gravity, nor X
12 and Y to move accurately and reliably. But while climbing up this
13 rather steep learning curve, I discovered that some motor control
14 systems are able to tune their PID controllers. I got the impression
15 from the documentation that LinuxCNC were not. This proved to be not
16 true.</p>
17
18 <p>The LinuxCNC
19 <a href="http://linuxcnc.org/docs/html/man/man9/pid.9.html">pid
20 component</a> is the recommended PID controller to use. It uses eight
21 constants <tt>Pgain</tt>, <tt>Igain</tt>, <tt>Dgain</tt>,
22 <tt>bias</tt>, <tt>FF0</tt>, <tt>FF1</tt>, <tt>FF2</tt> and
23 <tt>FF3</tt> to calculate the output value based on current and wanted
24 state, and all of these need to have a sensible value for the
25 controller to behave properly. Note, there are even more values
26 involved, theser are just the most important ones. In my case I need
27 the X, Y and Z axes to follow the requested path with little error.
28 This has proved quite a challenge for someone who have never tuned a
29 PID controller before, but there is at least some help to be found.
30
31 <p>I discovered that included in LinuxCNC was this old PID component
32 at_pid claiming to have auto tuning capabilities. Sadly it had been
33 neglected since 2011, and could not be used as a plug in replacement
34 for the default pid component. One would have to rewriting the
35 LinuxCNC HAL setup to test at_pid. This was rather sad, when I wanted
36 to quickly test auto tuning to see if it did a better job than me at
37 figuring out good P, I and D values to use.</p>
38
39 <p>I decided to have a look if the situation could be improved. This
40 involved trying to understand the code and history of the pid and
41 at_pid components. Apparently they had a common ancestor, as code
42 structure, comments and variable names were quite close to each other.
43 Sadly this was not reflected in the git history, making it hard to
44 figure out what really happened. My guess is that the author of
45 <a href="https://github.com/LinuxCNC/linuxcnc/blob/master/src/hal/components/at_pid.c">at_pid.c</a>
46 took a version of
47 <a href="https://github.com/LinuxCNC/linuxcnc/blob/master/src/hal/components/pid.c">pid.c</a>,
48 rewrote it to follow the structure he wished pid.c to have, then added
49 support for auto tuning and finally got it included into the LinuxCNC
50 repository. The restructuring and lack of early history made it
51 harder to figure out which part of the code were relevant to the auto
52 tuning, and which part of the code needed to be updated to work the
53 same way as the current pid.c implementation. I started by trying to
54 isolate relevant changes in pid.c, and applying them to at_pid.c. My
55 aim was to make sure the at_pid component could replace the pid
56 component with a simple change in the HAL setup loadrt line, without
57 having to "rewire" the rest of the HAL configuration. After a few
58 hours following this approach, I had learned quite a lot about the
59 code structure of both components, while concluding I was heading down
60 the wrong rabbit hole, and should get back to the surface and find a
61 different path.</p>
62
63 <p>For the second attempt, I decided to throw away all the PID control
64 related part of the original at_pid.c, and instead isolate and lift
65 the auto tuning part of the code and inject it into a copy of pid.c.
66 This ensured compatibility with the current pid component, while
67 adding auto tuning as a run time option. To make it easier to identify
68 the relevant parts in the future, I wrapped all the auto tuning code
69 with '#ifdef AUTO_TUNER'. The end result behave just like the current
70 pid component by default, as that part of the code is identical. The
71 <a href="https://github.com/LinuxCNC/linuxcnc/pull/1820">end result
72 entered the LinuxCNC master branch</a> a few days ago.</p>
73
74 <p>To enable auto tuning, one need to set a few HAL pins in the PID
75 component. The most important ones are <tt>tune-effort</tt>,
76 <tt>tune-mode</tt> and <tt>tune-start</tt>. But lets take a step
77 back, and see what the auto tuning code will do. I do not know the
78 mathematical foundation of the at_pid algorithm, but from observation
79 I can tell that the algorithm will, when enabled, produce a square
80 wave pattern centered around the <tt>bias</tt> value on the output pin
81 of the PID controller. This can be seen using the HAL Scope provided
82 by LinuxCNC. In my case, this is translated into voltage (+-10V) sent
83 to the motor controller, which in turn is translated into motor speed.
84 So at_pid will ask the motor to move the axis back and forth. The
85 number of cycles in the pattern is controlled by the
86 <tt>tune-cycles</tt> pin, and the extremes of the wave pattern is
87 controlled by the <tt>tune-effort</tt> pin. Of course, trying to
88 change the direction of a physical object instantly (as in going
89 directly from a positive voltage to the equivalent negative voltage)
90 do not change velocity instantly, and it take some time for the object
91 to slow down and move in the opposite direction. This result in a
92 more smooth movement wave form, as the axis in question were vibrating
93 back and forth. When the axis reached the target speed in the
94 opposing direction, the auto tuner change direction again. After
95 several of these changes, the average time delay between the 'peaks'
96 and 'valleys' of this movement graph is then used to calculate
97 proposed values for Pgain, Igain and Dgain, and insert them into the
98 HAL model to use by the pid controller. The auto tuned settings are
99 not great, but htye work a lot better than the values I had been able
100 to cook up on my own, at least for the horizontal X and Y axis. But I
101 had to use very small <tt>tune-effort<tt> values, as my motor
102 controllers error out if the voltage change too quickly. I've been
103 less lucky with the Z axis, which is moving a heavy object up and
104 down, and seem to confuse the algorithm. The Z axis movement became a
105 lot better when I introduced a <tt>bias</tt> value to counter the
106 gravitational drag, but I will have to work a lot more on the Z axis
107 PID values.</p>
108
109 <p>Armed with this knowledge, it is time to look at how to do the
110 tuning. Lets say the HAL configuration in question load the PID
111 component for X, Y and Z like this:</p>
112
113 <blockquote><pre>
114 loadrt pid names=pid.x,pid.y,pid.z
115 </pre></blockquote>
116
117 <p>Armed with the new and improved at_pid component, the new line will
118 look like this:</p>
119
120 <blockquote><pre>
121 loadrt at_pid names=pid.x,pid.y,pid.z
122 </pre></blockquote>
123
124 <p>The rest of the HAL setup can stay the same. This work because the
125 components are referenced by name. If the component had used count=3
126 instead, all use of pid.# had to be changed to at_pid.#.</p>
127
128 <p>To start tuning the X axis, move the axis to the middle of its
129 range, to make sure it do not hit anything when it start moving back
130 and forth. Next, set the <tt>tune-effort</tt> to a low number in the
131 output range. I used 0.1 as my initial value. Next, assign 1 to the
132 <tt>tune-mode</tt> value. Note, this will disable the pid controlling
133 part and feed 0 to the output pin, which in my case initially caused a
134 lot of drift. In my case it proved to be a good idea with X and Y to
135 tune the motor driver to make sure 0 voltage stopped the motor
136 rotation. On the other hand, for the Z axis this proved to be a bad
137 idea, so it will depend on your setup. It might help to set the
138 <tt>bias</tt> value to a output value that reduce or eliminate the
139 axis drift. Finally, after setting <tt>tune-mode</tt>, set
140 <tt>tune-start</tt> to 1 to activate the auto tuning. If all go well,
141 your axis will vibrate for a few seconds and when it is done, new
142 values for Pgain, Igain and Dgain will be active. To test them,
143 change <tt>tune-mode</tt> back to 0. Note that this might cause the
144 machine to suddenly jerk as it bring the axis back to its commanded
145 position, which it might have drifted away from during tuning. To
146 summarize with some halcmd lines:</p>
147
148 <blockquote><pre>
149 setp pid.x.tune-effort 0.1
150 setp pid.x.tune-mode 1
151 setp pid.x.tune-start 1
152 # wait for the tuning to complete
153 setp pid.x.tune-mode 0
154 </pre></blockquote>
155
156 <p>After doing this task quite a few times while trying to figure out
157 how to properly tune the PID controllers on the machine in, I decided
158 to figure out if this process could be automated, and wrote a script
159 to do the entire tuning process from power on. The end result will
160 ensure the machine is powered on and ready to run, home all axis if it
161 is not already done, check that the extra tuning pins are available,
162 move the axis to its mid point, run the auto tuning and re-enable the
163 pid controller when it is done. It can be run several times. Check
164 out the
165 <a href="https://github.com/SebKuzminsky/MazakVQC1540/blob/bon-dev/scripts/run-auto-pid-tuner">run-auto-pid-tuner</a>
166 script on github if you want to learn how it is done.</p>
167
168 <p>My hope is that this little adventure can inspire someone who know
169 more about motor PID controller tuning can implement even better
170 algorithms for automatic PID tuning in LinuxCNC, making life easier
171 for both me and all the others that want to use LinuxCNC but lack the
172 in depth knowledge needed to tune PID controllers well.</p>
173
174 <p>As usual, if you use Bitcoin and want to show your support of my
175 activities, please send Bitcoin donations to my address
176 <b><a href="bitcoin:15oWEoG9dUPovwmUL9KWAnYRtNJEkP1u1b">15oWEoG9dUPovwmUL9KWAnYRtNJEkP1u1b</a></b>.</p>