]> pere.pagekite.me Git - homepage.git/commitdiff
Draft LinuxCNC blog post.
authorPetter Reinholdtsen <pere@hungry.com>
Sat, 16 Jul 2022 18:41:46 +0000 (20:41 +0200)
committerPetter Reinholdtsen <pere@hungry.com>
Sat, 16 Jul 2022 18:41:46 +0000 (20:41 +0200)
blog/Automatic_LinuxCNC_servo_PID_tuning_.html [new file with mode: 0644]
blog/data/2022-07-16-linuxcnc-pid-autotuning.txt [new file with mode: 0644]

diff --git a/blog/Automatic_LinuxCNC_servo_PID_tuning_.html b/blog/Automatic_LinuxCNC_servo_PID_tuning_.html
new file mode 100644 (file)
index 0000000..b1a2a81
--- /dev/null
@@ -0,0 +1,727 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
+  <head>
+    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
+    <title>Petter Reinholdtsen: Automatic LinuxCNC servo PID tuning?</title>
+    <link rel="stylesheet" type="text/css" media="screen" href="https://people.skolelinux.org/pere/blog/style.css" />
+    <link rel="stylesheet" type="text/css" media="screen" href="https://people.skolelinux.org/pere/blog/vim.css" />
+
+
+  </head>
+  <body>
+    <div class="title">
+ <h1>
+     <a href="https://people.skolelinux.org/pere/blog/">Petter Reinholdtsen</a>
+     
+ </h1>
+</div>
+
+
+    <div class="entry">
+      <div class="title">Automatic LinuxCNC servo PID tuning?</div>
+      <div class="date">16th July 2022</div>
+      <div class="body"><p>While working on a CNC with servo motors controlled by the
+<a href="https://en.wikipedia.org/wiki/LinuxCNC">LinuxCNC</a>
+<a href="https://en.wikipedia.org/wiki/PID_controller">PID
+controller</a>, I had to learn how to tune the collection of values
+that control this mathematical machinery.  It proved to be a lot
+harder than I hoped, and I still have not succeeded in getting the Z
+PID controller to successfully defy gravity.  But while climbing up
+this rather steep learning curve, I discovered that some motor control
+systems are able to tune their PID controllers.  I got the impression
+from the documentation that LinuxCNC were not.  This proved to be not
+true</p>
+
+<p>The LinuxCNC
+<a href="http://linuxcnc.org/docs/html/man/man9/pid.9.html">pid
+component</a> is the recommended PID controller to use.  It uses eight
+values Pgain, Igain, Dgain, bias, FF0, FF1, FF2 and FF3 to calculate
+the output value, and all of these need to have a sensible value for
+the controller to behave properly.  Note, there are even more values
+involved in more edge cases.  In my case I need the X, Y and Z axes to
+follow the requested path with little error.  This has proved quite a
+challenge for someone who have never tuned a PID controller before,
+but there is some help to be found.
+
+<p>I discovered that included in LinuxCNC was this old PID component
+at_pid claiming to have auto tuning capabilities.  Sadly it had been
+neglected since 2011, and could not be used as a plug in replacement
+for the default pid component.  One would have to rewriting the
+LinuxCNC HAL setup to test at_pid.  This was rather sad, when I wanted
+to quickly test auto tuning to see if it did a better job than me at
+figuring out good P, I and D values to use.</p>
+
+<p>So I decided to have a look if the situation could be improved.
+This involved trying to understand the code and history of the pid and
+at_pid components.  Apparently they had a common ancestor, as code
+structure, comments and variable names were quite close to each other.
+But this was not reflected in the git history.  My guess is that the
+author of at_pid took a version of pid.c, rewrote it to follow the
+structure he wished pid.c to have, then added support for auto tuning
+and then committed it into the LinuxCNC repository.  This made it
+harder to figure out which part of the code were relevant to the auto
+tuning, and which part of the code needed to be updated to work the
+same way as the current pid.c implementation.  I started by trying to
+isolate relevant changes in pid.c, and applying them to at_pid.  My
+aim was to make sure the at_pid component could replace the pid
+component with a simple change in the HAL setup loadrt line, without
+having to "rewire" the rest of the HAL configuration.  After a few
+hours following this approach, I had learned quite a lot about the
+code structure of both components, while concluding I was heading down
+the wrong rabbit hole, and should get back to the surface and find a
+different path.</p>
+
+<p>For the second attempt, I decided to throw away all the PID control
+related part of the original at_pid.c, and instead isolate and lift
+the auto tuning part of the code and inject it into a copy of pid.c.
+This ensured compatibility with the current pid component, while
+adding auto tuning as a run time option.  To make it easier to identify
+the relevant parts in the future, I wrapped all the auto tuning code
+with '#ifdef AUTO_TUNER'.  The end result behave just like the current
+pid component by default, as the code is identical.  The
+<a href="https://github.com/LinuxCNC/linuxcnc/pull/1820">end result
+entered the LinuxCNC master branch</a> a few days ago.  To enable auto
+tuning, one need to set a few HAL pins in the PID component.  The most
+important ones are tune-effort, tune-mode and tune-start.  But lets
+take a step back, and see what the auto tuning code will do.</p>
+
+<p>I do not know the mathematical foundation of the at_pid algorithm,
+but from observation I can tell that the algorithm will, when enabled,
+produce a square wave pattern centered around the bias value on the
+output pin of the PID controller.  This can be seen using the HAL
+Scope provided by LinuxCNC.  In my case, this is translated into
+voltage (+-10V) sent to the motor controller, which in turn is
+translated into motor speed.  So it will ask the motor to move the
+axis back and forth.  The number of cycles in the pattern is
+controlled by the tune-cycles, and the extremes of the wave pattern is
+controlled by the tune-effort pin.  Of course, trying to change the
+direction of a physical object instantly (as in going directly from a
+positive voltage to the equivalent negative voltage) do not work, and
+it take some time for the object to slow down and move in the opposite
+direction.  This result in more smooth movement wave form, as the axis
+in question were vibrating back and forth.  When the axis reached the
+target speed in the opposing direction, the auto tuner change
+direction again.  After several of these, the average time delay
+between the 'peaks' and 'valleys' of this movement graph is then used
+to calculate proposed values for Pgain, Igain and Dgain, and insert
+them into the HAL model to use by the pid controller.  The end result
+is not great, but it work a lot better than the values I had been able
+to cook up on my own, at least for the horizontal X and Y axis.  I've
+been less lucky with the Z axis, which is moving a heavy object up and
+down, and seem to confuse the algorithm.  It became a lot better when
+I introduced a bias value to counter the gravitational drag, but I
+will have to work a lot more on the Z axis PID values.</p>
+
+<p>Armed with this knowledge, it is time to look at how to do the
+tuning.  Lets say the HAL configuration in question load the PID
+component for X, Y and Z like this:</p>
+<blockquote><pre>
+loadrt pid names=pid.x,pid.y,pid.z
+</pre></blockquote>
+
+<p>Armed with the new and improved at_pid component, the new line will
+look like this:</p>
+
+
+<blockquote><pre>
+loadrt at_pid names=pid.x,pid.y,pid.z
+</pre></blockquote>
+
+<p>The rest of the HAL setup can stay the same.</p>
+
+<p>So, to start tuning the X axis, move the axis to the middle of its
+range, to make sure it do not hit anything when it start moving back
+and forth.  Next, set the pid.x.tune-effort to a low number in the
+output range.  I used 0.1 as my initial value.  Next, assign 1 to the
+tune-mode value. Note, this will disable the pid controlling part and
+feed 0 to the output pin, which in my case initially caused a lot of
+drift.  In my case it proved to be a good idea with X and Y to tune
+the motor driver to make sure 0 voltage stopped the motor rotation.
+On the other hand, for the Z axis this proved to be a bad idea, so it
+will depend on your setup.  It might help to set the bias value to a
+output value that reduce or eliminate the axis drift.  Finally, after
+setting tune-mode, set tune-start to 1 to activate the auto tuning.
+If all go well, your axis will vibrate for a few seconds and when it
+is done, new values for Pgain, Igain and Dgain will be active.  To
+test them, change tune-mode back to 0.  Note that this might cause the
+machine to suddenly jerk as it bring the axis back to its commanded
+position, which it might have drifted away from during tuning.  To
+summarize with some halcmd lines:</p>
+
+<blockquote><pre>
+setp pid.x.tune-effort 0.1
+setp pid.x.tune-mode 1
+setp pid.x.tune-start 1
+# wait for the tuning to complete
+setp pid.x.tune-mode 0
+</pre></blockquote>
+
+<p>After doing this task quite a few times while trying to figure out
+how to properly tune the PID controllers on the machine in, I decided
+to figure out if this process could be automated, and wrote a script
+to do the entire tuning process from power on.  The end result will
+ensure the machine is powered on and ready to run, home all axis if it
+is not already done, check that the extra tuning pins are available,
+move the axis to its mid point, run the auto tuning and re-enable the
+pid controller when it is done.  It can be run several times.  Check
+out the
+<a href="https://github.com/SebKuzminsky/MazakVQC1540/blob/bon-dev/scripts/run-auto-pid-tuner">run-auto-pid-tuner</a>
+script on github if you want to learn how it is done.</p>
+
+<p>My hope is that this little adventure can inspire someone who know
+more about motor PID controller tuning can implement even better
+algorithms for automatic PID tuning in LinuxCNC, making life easier
+for both me and all the others that want to use LinuxCNC but lack the
+in depth knowledge needed to tune PID controllers well.</p>
+
+<p>As usual, if you use Bitcoin and want to show your support of my
+activities, please send Bitcoin donations to my address
+<b><a href="bitcoin:15oWEoG9dUPovwmUL9KWAnYRtNJEkP1u1b">15oWEoG9dUPovwmUL9KWAnYRtNJEkP1u1b</a></b>.</p>
+</div>
+      
+      <div class="tags">Tags: <a href="https://people.skolelinux.org/pere/blog/tags/3d-printer">3d-printer</a>, <a href="https://people.skolelinux.org/pere/blog/tags/debian">debian</a>, <a href="https://people.skolelinux.org/pere/blog/tags/english">english</a>, <a href="https://people.skolelinux.org/pere/blog/tags/robot">robot</a>.</div>
+      
+      
+    </div>
+    
+
+    
+
+    <div id="sidebar">
+      
+
+
+<h2>Archive</h2>
+<ul>
+
+<li>2022
+<ul>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2022/02/">February (1)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2022/03/">March (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2022/04/">April (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2022/06/">June (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2022/07/">July (1)</a></li>
+
+</ul></li>
+
+<li>2021
+<ul>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2021/01/">January (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2021/02/">February (1)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2021/05/">May (1)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2021/06/">June (1)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2021/07/">July (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2021/08/">August (1)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2021/09/">September (1)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2021/10/">October (1)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2021/12/">December (1)</a></li>
+
+</ul></li>
+
+<li>2020
+<ul>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2020/02/">February (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2020/03/">March (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2020/04/">April (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2020/05/">May (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2020/06/">June (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2020/07/">July (1)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2020/09/">September (1)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2020/10/">October (1)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2020/11/">November (1)</a></li>
+
+</ul></li>
+
+<li>2019
+<ul>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2019/01/">January (4)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2019/02/">February (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2019/03/">March (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2019/05/">May (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2019/06/">June (5)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2019/07/">July (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2019/08/">August (1)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2019/09/">September (1)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2019/11/">November (1)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2019/12/">December (4)</a></li>
+
+</ul></li>
+
+<li>2018
+<ul>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2018/01/">January (1)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2018/02/">February (5)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2018/03/">March (5)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2018/04/">April (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2018/06/">June (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2018/07/">July (5)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2018/08/">August (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2018/09/">September (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2018/10/">October (5)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2018/11/">November (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2018/12/">December (4)</a></li>
+
+</ul></li>
+
+<li>2017
+<ul>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2017/01/">January (4)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2017/02/">February (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2017/03/">March (5)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2017/04/">April (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2017/06/">June (5)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2017/07/">July (1)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2017/08/">August (1)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2017/09/">September (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2017/10/">October (5)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2017/11/">November (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2017/12/">December (4)</a></li>
+
+</ul></li>
+
+<li>2016
+<ul>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2016/01/">January (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2016/02/">February (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2016/03/">March (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2016/04/">April (8)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2016/05/">May (8)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2016/06/">June (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2016/07/">July (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2016/08/">August (5)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2016/09/">September (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2016/10/">October (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2016/11/">November (8)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2016/12/">December (5)</a></li>
+
+</ul></li>
+
+<li>2015
+<ul>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2015/01/">January (7)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2015/02/">February (6)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2015/03/">March (1)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2015/04/">April (4)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2015/05/">May (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2015/06/">June (4)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2015/07/">July (6)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2015/08/">August (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2015/09/">September (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2015/10/">October (9)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2015/11/">November (6)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2015/12/">December (3)</a></li>
+
+</ul></li>
+
+<li>2014
+<ul>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2014/01/">January (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2014/02/">February (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2014/03/">March (8)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2014/04/">April (7)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2014/05/">May (1)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2014/06/">June (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2014/07/">July (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2014/08/">August (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2014/09/">September (5)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2014/10/">October (6)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2014/11/">November (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2014/12/">December (5)</a></li>
+
+</ul></li>
+
+<li>2013
+<ul>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2013/01/">January (11)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2013/02/">February (9)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2013/03/">March (9)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2013/04/">April (6)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2013/05/">May (9)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2013/06/">June (10)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2013/07/">July (7)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2013/08/">August (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2013/09/">September (5)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2013/10/">October (7)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2013/11/">November (9)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2013/12/">December (3)</a></li>
+
+</ul></li>
+
+<li>2012
+<ul>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2012/01/">January (7)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2012/02/">February (10)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2012/03/">March (17)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2012/04/">April (12)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2012/05/">May (12)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2012/06/">June (20)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2012/07/">July (17)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2012/08/">August (6)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2012/09/">September (9)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2012/10/">October (17)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2012/11/">November (10)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2012/12/">December (7)</a></li>
+
+</ul></li>
+
+<li>2011
+<ul>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2011/01/">January (16)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2011/02/">February (6)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2011/03/">March (6)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2011/04/">April (7)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2011/05/">May (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2011/06/">June (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2011/07/">July (7)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2011/08/">August (6)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2011/09/">September (4)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2011/10/">October (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2011/11/">November (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2011/12/">December (1)</a></li>
+
+</ul></li>
+
+<li>2010
+<ul>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2010/01/">January (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2010/02/">February (1)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2010/03/">March (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2010/04/">April (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2010/05/">May (9)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2010/06/">June (14)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2010/07/">July (12)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2010/08/">August (13)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2010/09/">September (7)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2010/10/">October (9)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2010/11/">November (13)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2010/12/">December (12)</a></li>
+
+</ul></li>
+
+<li>2009
+<ul>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2009/01/">January (8)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2009/02/">February (8)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2009/03/">March (12)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2009/04/">April (10)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2009/05/">May (9)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2009/06/">June (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2009/07/">July (4)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2009/08/">August (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2009/09/">September (1)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2009/10/">October (2)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2009/11/">November (3)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2009/12/">December (3)</a></li>
+
+</ul></li>
+
+<li>2008
+<ul>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2008/11/">November (5)</a></li>
+
+<li><a href="https://people.skolelinux.org/pere/blog/archive/2008/12/">December (7)</a></li>
+
+</ul></li>
+
+</ul>
+
+
+
+<h2>Tags</h2>
+<ul>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/3d-printer">3d-printer (19)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/amiga">amiga (1)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/aros">aros (1)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/bankid">bankid (4)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/betalkontant">betalkontant (9)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/bitcoin">bitcoin (12)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/bootsystem">bootsystem (17)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/bsa">bsa (2)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/chrpath">chrpath (2)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/debian">debian (181)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/debian edu">debian edu (159)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/debian-handbook">debian-handbook (8)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/digistan">digistan (11)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/dld">dld (18)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/docbook">docbook (30)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/drivstoffpriser">drivstoffpriser (4)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/english">english (437)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/fiksgatami">fiksgatami (23)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/fildeling">fildeling (14)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/freeculture">freeculture (34)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/freedombox">freedombox (9)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/frikanalen">frikanalen (20)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/h264">h264 (20)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/intervju">intervju (43)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/isenkram">isenkram (16)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/kart">kart (23)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/kodi">kodi (4)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/ldap">ldap (9)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/lego">lego (5)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/lenker">lenker (8)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/lsdvd">lsdvd (2)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/ltsp">ltsp (1)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/madewithcc">madewithcc (3)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/mesh network">mesh network (8)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/multimedia">multimedia (42)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/nice free software">nice free software (13)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/noark5">noark5 (23)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/norsk">norsk (320)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/nuug">nuug (198)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/offentlig innsyn">offentlig innsyn (40)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/open311">open311 (2)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/opphavsrett">opphavsrett (75)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/personvern">personvern (114)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/raid">raid (2)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/reactos">reactos (1)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/reprap">reprap (11)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/rfid">rfid (3)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/robot">robot (16)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/rss">rss (1)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/ruter">ruter (7)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/scraperwiki">scraperwiki (2)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/sikkerhet">sikkerhet (59)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/sitesummary">sitesummary (4)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/skepsis">skepsis (5)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/standard">standard (72)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/stavekontroll">stavekontroll (7)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/stortinget">stortinget (14)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/surveillance">surveillance (62)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/sysadmin">sysadmin (5)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/usenix">usenix (2)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/valg">valg (9)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/verkidetfri">verkidetfri (20)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/video">video (77)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/vitenskap">vitenskap (4)</a></li>
+
+ <li><a href="https://people.skolelinux.org/pere/blog/tags/web">web (42)</a></li>
+
+</ul>
+
+
+    </div>
+    <p style="text-align: right">
+ Created by <a href="http://steve.org.uk/Software/chronicle">Chronicle v4.6</a>
+</p>
+
+  </body>
+</html>
diff --git a/blog/data/2022-07-16-linuxcnc-pid-autotuning.txt b/blog/data/2022-07-16-linuxcnc-pid-autotuning.txt
new file mode 100644 (file)
index 0000000..0263744
--- /dev/null
@@ -0,0 +1,163 @@
+Title: Automatic LinuxCNC servo PID tuning?
+Tags: english, debian, 3d-printer, robot
+Date: 2022-07-16 21:00
+Publish: 2022-07-16 21:00
+
+<p>While working on a CNC with servo motors controlled by the
+<a href="https://en.wikipedia.org/wiki/LinuxCNC">LinuxCNC</a>
+<a href="https://en.wikipedia.org/wiki/PID_controller">PID
+controller</a>, I had to learn how to tune the collection of values
+that control this mathematical machinery.  It proved to be a lot
+harder than I hoped, and I still have not succeeded in getting the Z
+PID controller to successfully defy gravity.  But while climbing up
+this rather steep learning curve, I discovered that some motor control
+systems are able to tune their PID controllers.  I got the impression
+from the documentation that LinuxCNC were not.  This proved to be not
+true</p>
+
+<p>The LinuxCNC
+<a href="http://linuxcnc.org/docs/html/man/man9/pid.9.html">pid
+component</a> is the recommended PID controller to use.  It uses eight
+values Pgain, Igain, Dgain, bias, FF0, FF1, FF2 and FF3 to calculate
+the output value, and all of these need to have a sensible value for
+the controller to behave properly.  Note, there are even more values
+involved in more edge cases.  In my case I need the X, Y and Z axes to
+follow the requested path with little error.  This has proved quite a
+challenge for someone who have never tuned a PID controller before,
+but there is some help to be found.
+
+<p>I discovered that included in LinuxCNC was this old PID component
+at_pid claiming to have auto tuning capabilities.  Sadly it had been
+neglected since 2011, and could not be used as a plug in replacement
+for the default pid component.  One would have to rewriting the
+LinuxCNC HAL setup to test at_pid.  This was rather sad, when I wanted
+to quickly test auto tuning to see if it did a better job than me at
+figuring out good P, I and D values to use.</p>
+
+<p>So I decided to have a look if the situation could be improved.
+This involved trying to understand the code and history of the pid and
+at_pid components.  Apparently they had a common ancestor, as code
+structure, comments and variable names were quite close to each other.
+But this was not reflected in the git history.  My guess is that the
+author of at_pid took a version of pid.c, rewrote it to follow the
+structure he wished pid.c to have, then added support for auto tuning
+and then committed it into the LinuxCNC repository.  This made it
+harder to figure out which part of the code were relevant to the auto
+tuning, and which part of the code needed to be updated to work the
+same way as the current pid.c implementation.  I started by trying to
+isolate relevant changes in pid.c, and applying them to at_pid.  My
+aim was to make sure the at_pid component could replace the pid
+component with a simple change in the HAL setup loadrt line, without
+having to "rewire" the rest of the HAL configuration.  After a few
+hours following this approach, I had learned quite a lot about the
+code structure of both components, while concluding I was heading down
+the wrong rabbit hole, and should get back to the surface and find a
+different path.</p>
+
+<p>For the second attempt, I decided to throw away all the PID control
+related part of the original at_pid.c, and instead isolate and lift
+the auto tuning part of the code and inject it into a copy of pid.c.
+This ensured compatibility with the current pid component, while
+adding auto tuning as a run time option.  To make it easier to identify
+the relevant parts in the future, I wrapped all the auto tuning code
+with '#ifdef AUTO_TUNER'.  The end result behave just like the current
+pid component by default, as the code is identical.  The
+<a href="https://github.com/LinuxCNC/linuxcnc/pull/1820">end result
+entered the LinuxCNC master branch</a> a few days ago.  To enable auto
+tuning, one need to set a few HAL pins in the PID component.  The most
+important ones are tune-effort, tune-mode and tune-start.  But lets
+take a step back, and see what the auto tuning code will do.</p>
+
+<p>I do not know the mathematical foundation of the at_pid algorithm,
+but from observation I can tell that the algorithm will, when enabled,
+produce a square wave pattern centered around the bias value on the
+output pin of the PID controller.  This can be seen using the HAL
+Scope provided by LinuxCNC.  In my case, this is translated into
+voltage (+-10V) sent to the motor controller, which in turn is
+translated into motor speed.  So it will ask the motor to move the
+axis back and forth.  The number of cycles in the pattern is
+controlled by the tune-cycles, and the extremes of the wave pattern is
+controlled by the tune-effort pin.  Of course, trying to change the
+direction of a physical object instantly (as in going directly from a
+positive voltage to the equivalent negative voltage) do not work, and
+it take some time for the object to slow down and move in the opposite
+direction.  This result in more smooth movement wave form, as the axis
+in question were vibrating back and forth.  When the axis reached the
+target speed in the opposing direction, the auto tuner change
+direction again.  After several of these, the average time delay
+between the 'peaks' and 'valleys' of this movement graph is then used
+to calculate proposed values for Pgain, Igain and Dgain, and insert
+them into the HAL model to use by the pid controller.  The end result
+is not great, but it work a lot better than the values I had been able
+to cook up on my own, at least for the horizontal X and Y axis.  I've
+been less lucky with the Z axis, which is moving a heavy object up and
+down, and seem to confuse the algorithm.  It became a lot better when
+I introduced a bias value to counter the gravitational drag, but I
+will have to work a lot more on the Z axis PID values.</p>
+
+<p>Armed with this knowledge, it is time to look at how to do the
+tuning.  Lets say the HAL configuration in question load the PID
+component for X, Y and Z like this:</p>
+<blockquote><pre>
+loadrt pid names=pid.x,pid.y,pid.z
+</pre></blockquote>
+
+<p>Armed with the new and improved at_pid component, the new line will
+look like this:</p>
+
+
+<blockquote><pre>
+loadrt at_pid names=pid.x,pid.y,pid.z
+</pre></blockquote>
+
+<p>The rest of the HAL setup can stay the same.</p>
+
+<p>So, to start tuning the X axis, move the axis to the middle of its
+range, to make sure it do not hit anything when it start moving back
+and forth.  Next, set the pid.x.tune-effort to a low number in the
+output range.  I used 0.1 as my initial value.  Next, assign 1 to the
+tune-mode value. Note, this will disable the pid controlling part and
+feed 0 to the output pin, which in my case initially caused a lot of
+drift.  In my case it proved to be a good idea with X and Y to tune
+the motor driver to make sure 0 voltage stopped the motor rotation.
+On the other hand, for the Z axis this proved to be a bad idea, so it
+will depend on your setup.  It might help to set the bias value to a
+output value that reduce or eliminate the axis drift.  Finally, after
+setting tune-mode, set tune-start to 1 to activate the auto tuning.
+If all go well, your axis will vibrate for a few seconds and when it
+is done, new values for Pgain, Igain and Dgain will be active.  To
+test them, change tune-mode back to 0.  Note that this might cause the
+machine to suddenly jerk as it bring the axis back to its commanded
+position, which it might have drifted away from during tuning.  To
+summarize with some halcmd lines:</p>
+
+<blockquote><pre>
+setp pid.x.tune-effort 0.1
+setp pid.x.tune-mode 1
+setp pid.x.tune-start 1
+# wait for the tuning to complete
+setp pid.x.tune-mode 0
+</pre></blockquote>
+
+<p>After doing this task quite a few times while trying to figure out
+how to properly tune the PID controllers on the machine in, I decided
+to figure out if this process could be automated, and wrote a script
+to do the entire tuning process from power on.  The end result will
+ensure the machine is powered on and ready to run, home all axis if it
+is not already done, check that the extra tuning pins are available,
+move the axis to its mid point, run the auto tuning and re-enable the
+pid controller when it is done.  It can be run several times.  Check
+out the
+<a href="https://github.com/SebKuzminsky/MazakVQC1540/blob/bon-dev/scripts/run-auto-pid-tuner">run-auto-pid-tuner</a>
+script on github if you want to learn how it is done.</p>
+
+<p>My hope is that this little adventure can inspire someone who know
+more about motor PID controller tuning can implement even better
+algorithms for automatic PID tuning in LinuxCNC, making life easier
+for both me and all the others that want to use LinuxCNC but lack the
+in depth knowledge needed to tune PID controllers well.</p>
+
+<p>As usual, if you use Bitcoin and want to show your support of my
+activities, please send Bitcoin donations to my address
+<b><a href="bitcoin:15oWEoG9dUPovwmUL9KWAnYRtNJEkP1u1b">15oWEoG9dUPovwmUL9KWAnYRtNJEkP1u1b</a></b>.</p>