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