stem limitations…all right… we will stop the trolley as rapidly as we can, ok!?</p><p id="641c">Here is a graph animation trying to convey this idea of a pendulum(sorry for the imperfections…)</p><figure id="ef4c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*XtFH8mTQbaxfbJi0jM_ZsA.gif"><figcaption>Fig 4. Typical pendulum curve</figcaption></figure><p id="363b">And here is a free graphics Roller coaster Speed x Time, which simulates mathematically the motion observed above; You can<a href="https://docs.google.com/spreadsheets/d/1CopMKO_XoRV9xh5eOp3mZ-yKw32tZ3Ea2Nraw_bXTQ0/edit?usp=sharing"> download the worksheet</a> now to study PID with me!</p><figure id="9f9a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*xlZ3429u9iUjSm5tGIeDsg.png"><figcaption>Fig 5. Roller coaster speed x time graphic</figcaption></figure><p id="b7b7">Please, see that the system gets an amazing speed in the first few seconds.</p><p id="cc4a">It is a huge linear acquisition, right?</p><p id="6342">Then the question is:</p><p id="10ea">How much force should I apply to the brakes?</p><p id="abab">Let’s make an attempt here: hit hard the brakes, say, 50% proportional to the amount of the speed; This stands for Proportional correction proposal. See the green proportional line on the graph:</p><figure id="f6f7"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Df_hy7ebpjHqRFnOb1MzSg.png"><figcaption>Fig 6. Proportional correction (green curve)</figcaption></figure><p id="7530">As soon as the errors occur as soon as the proportional correction is triggered.</p><p id="3af7"><i>Proportional cares for the present!</i></p><p id="7c68">But wait! Take a close look at the graph above, my friend…Probably there is no time to make the correction…right at the start, the speed is tremendous!</p><p id="1a59">Do we have how to anticipate this excessive speed gain or direction change?</p><p id="1a57">Sure and the trick is the D; add the D part of the equation; the Derivative correction for the system follows; this makes an anticipate rection!</p><figure id="a6f1"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*K8Fm0KzU6Dnkmyilf-WmEA.png"><figcaption>Fig 7. Derivative correction (blue curve)</figcaption></figure><p id="593a">If you observe the behavior of the derivative blue curve you will see that it is greatly increased when the speed direction initializes or changes (focus on those two peaks depicted by the yellow arrows above).</p><p id="7895">In fact, the derivative calculates the rate of change of the variable, as said above, and appears on the curve thousandths of seconds before even the actual change of direction or speed, even before those changes become discernible by the system and that’s just what we need now \o/</p><p id="8c89"><i>Derivative cares for the future!</i></p><p id="b571">And finally let’s apply the Integral correction:</p><figure id="4b06"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*jSqbRA92VbfBYGE9opjN6A.png"><figcaption>Fig 8. Integral correction (orange curve)</figcaption></figure><p id="d81f">The integral simply make the sum of all values (errors, for the sake of this example) during the time lapse. See that the orange line is almost zero because the areas drawn by the curve tend to balance itself, but due to the friction, the original loop no longer repeats, hence the errors accumulate a little.</p><p id="e617"><i>Integral cares for the past!</i></p><p id="c4ca">To understand, please calculate mentally the areas under the red curve and make the sum of them. If the graph were perfectly symmetric, based on the x-axis, the values would be equal and the integral (orange curve) would be zero. But we have factors that make the system lose momentum and these are frictions generated thought metal contacts and yet by the very movement of your body (mainly your neck) when subjected to such great changes of speed and directions (g-force); these minors error are treated by the integral equation.</p><p id="561c">Now adds all corrections curves together and you will have a mathematical function as a result (pid_output); and believe me, my friend…your problem is over! Your trolley will be at your command in just 100 us, right on the ground zero!</p><figure id="b3bb"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*KsUCWYhPVXhg4B5Diyx0xA.png"><figcaption>Fig 9. Pid resolution (dark green curve)</figcaption></figure><p id="8cef">Congratulations, your PID is working \o/</p><p id="cb8d">I hope you understood the big picture of PID by now, although superficially, of course;)</p><p id="b11c">ln short, PID it is an algorithm that controls a plant by using some system feedback and a given goal or set point, or target, whatever... This is all you need to understand by now:)</p><p id="dbf6">Let's jump right into performing our LEGO ultimate PID line follower robot algorithm!</p><h2 id="1f46">Our Target</h2><figure id="47bc"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*fVDJGgiE71LkxU29rHOibw.png"><figcaption>Fig. 10— Color Sensor reading at different positions as shown in <a href="https://readmedium.com/lego-sensor-color-5db4d42e0203">Episode#07</a>; the less light reflection, the lower the sensor’s value.</figcaption></figure><p id="cef2">If we find the center point, we would have 72 + 8 divided by 2 would give us 40; let’s force a little more towards the black line and use the number 35, okay?</p><p id="2956">I’m going to say that 35 is the light sensor reading for our target. With this value, the Tribot must follow the Good Zone ideally.</p><h2 id="89b4">P for Proportional</h2><p id="1e16">Proportional measures deviation from the target path that we’ve established here at 35.</p><div id="40de"><pre>Proportional (kp)
Measures deviation <span class="hljs-built_in">from</span> <span class="hljs-keyword">the</span> target path (error) <span class="hljs-keyword">using</span> <span class="hljs-keyword">the</span> light sensor</pre></div><div id="203c"><pre><span class="hljs-attr">target</span> = <span class="hljs-number">35</span> (between the black&white line)</pre></div><div id="3d88"><pre><span class="hljs-attr">error_correction</span> = target - sensor_value</pre></div><div id="9a65"><pre><span class="hljs-attr">proportional_gain</span> = error . kp</pre></div><div id="8880"><pre><span class="hljs-attr">p_gain</span> = (target - value) . kp</pre></div><div id="29db"><pre>kp <span class="hljs-keyword">is</span> an arbitrary value</pre></div><p id="0e4c">Translation to Lego’s G-EV3:</p><figure id="ba1b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*cFXRPt9C2QO-5rDRNVfo2g.png"><figcaption>Fig 11. <b>PID_01.ev3</b> code — P for the proportional algorithm (working code)</figcaption></figure><h2 id="af27">PI for Proportional & Integral</h2><div id="4705"><pre>Integral (ki)
The <span class="hljs-built_in">sum</span> of <span class="hljs-built_in">all</span> <span class="hljs-string">'error'</span> values</pre></div><div id="dd8d"><pre><span class="hljs-built_in">int</span>egral= <span class="hljs-built_in">int</span>egral + error</pre></div><div id="f66e"><pre>ki <span class="hljs-keyword">is</span> an arbitrary value</pre></div><p id="4e0d">Translation to Lego’s G-EV3:</p><figure id="ac19"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*QZiJmLzEqJ0FDgDTKoagEA.png"><figcaption>Fig 12. <b>PID_02.ev3</b> code — PI for the Proportional and Integral algorithm</figcaption></figure><h2 id="2c6a">PID for Proportional & Integral & Derivative</h2><div id="e71a"><pre>PID = (<span class="hljs-keyword">target</span>-value).kp + (integral+<span class="hljs-keyword">error</span>).ki+(<span class="hljs-keyword">error</span>-last_error).kd</pre></div><div id="cd1b"><pre><span class="hljs-attr">PID</span> = error.kp + integral.ki + derivative.kd</pre></div><div id="05f3"><pre><span class="hljs-attr">PID</span> = proportional_gain + integral_gain + derivative_gain</pre></div><div id="e260"><pre><span class="hljs-attr">PID</span> = p_gain + i_gain + d_gain</pre></div><p id="a53b">Translation to Lego’s G-EV3:</p><figure id="7c13"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*hPwUggt4MHxiGgjpVdDo9g.png"><figcaption>Fig 13. <b>PID_03.ev3</b> code — PID algorithm working code</figcaption></figure><p id="b057"><b>My Block PID</b></p><figure id="8f2b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*lK1BLx2PTiu56LGE5ItXrw.png"><figcaption>Fig 14. MyBlock: PID</figcaption></figure><p id="4313"><b>More PID Theory</b></p><p id="d626">Thank God we have a PID technology!</p><p id="5528">Let's see what is the term closed loop, that you may find in the literature:</p><div id="a6d8"><pre>Closed Loop Control System (CLCS) is <span class="hljs-keyword">the</span> <span class="hljs-built_in">process</span> <span class="hljs-keyword">of</span> reading sensors <span class="hljs-built_in">to</span> provide <span class="hljs-built_in">constant</span> feedback <span class="hljs-keyword">and</span> calculating <span class="hljs-keyword">the</span> desired actuator output is repeated continuously <span class="hljs-keyword">and</span> <span class="hljs-keyword">at</span> <span class="hljs-keyword">a</span> fixed loop rate .</pre></div><p id="839a">Our goal is to tune a <b>PID </b>controller to reduce overshoot in reference tracking or to improve rejection of a <b>disturbance </b>at the plant input.</p><p id="e883">What is <b>disturbance rejection</b>? From MacMynowski:</p><div id="7bd4"><pre>The term disturbance rejection was used <span class="hljs-keyword">a</span> <span class="hljs-built_in">number</span> <span class="hljs-keyword">of</span> times <span class="hljs-keyword">in</span> lecture today <span class="hljs-built_in">to</span> describe <span class="hljs-keyword">the</span> purpose <span class="hljs-keyword">of</span> feedback. With feedback, <span class="hljs-keyword">the</span> controller is able <span class="hljs-built_in">to</span> use <span class="hljs-keyword">the</span> output <span class="hljs-built_in">to</span> shape <span class="hljs-keyword">the</span> input <span class="hlj
Options
s-keyword">of</span> <span class="hljs-keyword">the</span> <span class="hljs-keyword">system</span>.</pre></div><p id="5591">And how about Nonlinear System:</p><div id="5c93"><pre>Nonlinear System (NLS) is <span class="hljs-keyword">a</span> <span class="hljs-keyword">system</span> <span class="hljs-keyword">in</span> which <span class="hljs-keyword">the</span> control parameters that produce <span class="hljs-keyword">a</span> desired response <span class="hljs-keyword">at</span> <span class="hljs-literal">one</span> operating point might <span class="hljs-keyword">not</span> produce <span class="hljs-keyword">a</span> satisfactory response <span class="hljs-keyword">at</span> another operating point.</pre></div><p id="8f3d">I read that simulation is an important tool for getting PID right.</p><p id="3de3">I already did this in the <a href="https://readmedium.com/jaythree-balancing-car-project-part-4-5-4964ab5af128">previous post</a>.</p><p id="0665">The Setpoint (SP) is the value that we want the process to be.</p><p id="48e4">The PID controller looks at the setpoint and compares it with the actual value of the Process Variable (PV).</p><h2 id="5526">Modes of Control</h2><p id="ca4d">There are various modes out there, here are some combinations:</p><p id="6a0e">P — Sometimes used</p><p id="92e1">PI — Most often used</p><p id="98fd">PID — Sometimes used</p><p id="d7b1">PD — rare as hen’s teeth but can be useful for controlling servo motors.</p><p id="9573"><b>Under The Hood Of The PID Controller — Tuning</b></p><p id="7dee">The process of setting the optimal gains for P, I and D to get an ideal response from a control system is called tuning.</p><p id="e440">The gains of a PID controller can be obtained by trial and error method. This is my approach to the solution.</p><p id="eed3">I started with PI. This way is the most common occurrences in the literature.</p><p id="0778">The tricky thing about Integral Action is that it will really screw up your process unless you know exactly how much Integral action to apply.</p><p id="07c3">The way to adjust how much Integral Action you have is by adjusting a term called <i>minutes per repeat</i>.</p><p id="9cb2">So where does this strange name come from? It is a measure of how long it will take for the Integral Action to match the Proportional Action.</p><p id="52ec">In other words, if the output of the proportional box on the diagram above is 20%, the repeat time is the time it will take for the output of the Integral box to get to 20% too.</p><p id="99b9">Derivative action improves the controller action because it predicts what is yet to happen. Of course, you can try and filter the noise out, but my advice is that, unless PI control is really slow, don’t worry about switching D on.</p><p id="c876"><b>Another Tunning Method:</b></p><p id="7949">The <b>Ziegler-Nichols method </b>is another popular method of tuning a PID controller. It is very similar to the trial and error method wherein I and D are set to zero and P is increased until the loop starts to oscillate. Once oscillation starts, the critical gain Kc and the period of oscillations Pc are noted. The P, I and D are then adjusted in a tabular column.</p><p id="5b52">And that’s all, folks!</p><p id="28f5">Thanks for reading my episode this week; if you found it helpful be sure to subscribe for more tutorials and if you like this share with your friend.</p><p id="6321">And if you have an idea for a tutorial be sure to submit it in the comment section below.</p><p id="a584">Thank you and I’ll see you next time. bye o/</p><p id="43fa"><a href="https://drive.google.com/open?id=1bi15yQlHbgeHKMhY9qtPiiAjY8J87oV-">Download All Files For This Project</a></p><h1 id="64f4">Related Posts:</h1><p id="3566">23° ArduSeries —<b> <a href="https://readmedium.com/pid-sample-for-arduino-260eae8975a4">PID sample for Arduino</a> </b>— HowTo Control devices with PIDLibrary</p><p id="1477">5/5 Part JayThree Balancing Car Project — <a href="https://readmedium.com/jaythree-balancing-car-project-part-5-5-2aaa48924428"><b>PID without a PhD</b></a></p><p id="ca95">01º Lego Episode — <a href="https://readmedium.com/lego-our-startups-journey-2db83f9e92a4"><b>Our Startup’s Journey</b> </a>— Invaders and Invasions?</p><p id="da00">02º Lego Episode — <a href="https://readmedium.com/timmyton-lego-adventures-02-faae7d8c6fea"><b>Timmyton </b>— <b>Lego-Learning-By-Playing — L2BP Series</b></a></p><p id="b1d2">04º Lego Episode — <a href="https://readmedium.com/lego-motions-episode-04-41dff47e8834"><b>Lego Motions</b> — <b>Tribot v 1.0</b> — <b>Seeing Your Creation Move</b> </a>— Move Steering Block</p><p id="0d41">05º Lego Episode — <a href="https://readmedium.com/lego-motions-move-tribot-around-deb227946425"><b>Lego Motions </b>— <b>Move Tribot Around</b> </a>— And Backward…Five Programs Files</p><p id="98f9">06º Lego Episode — <a href="https://readmedium.com/lego-sensors-touch-n-color-edbe0f6642f"><b>Lego Sensors</b> — <b>Touch N Color</b></a> — Two out of five human senses — Touch N Sight</p><p id="3976">07º Lego Episode — <a href="https://readmedium.com/lego-sensor-color-5db4d42e0203"><b>Lego Sensor </b>— <b>LineFollower</b> </a>— Line Follower Tribot v1.0</p><p id="5939">08º Lego Episode — <a href="https://readmedium.com/maze-solving-tribot-v1-0-a29963f8742a"><b>Maze Solving Robot v1</b></a><b> </b>— Lego Solution Right-Wall-Follower-Robot</p><p id="b7c2">09° Lego Episode — <a href="https://readmedium.com/lego-gettle-sound-bots-5fb40f6d84df"><b>Gettle_&_Sound_Bots</b></a> — How gentle can a robot be? What is the audible range of the human ear? How deep can we dive?</p><p id="9a96">10° Lego Episode — <a href="https://readmedium.com/lego-data-logging-a2fd59dd7933"><b>Data Logging</b></a> — Data Collection and the EV3</p><p id="14df">11º Lego Episode — <a href="https://readmedium.com/lego-binning-the-linefollower-code-5562e059237e"><b>Binning the LineFollower Code </b></a>— Binning: Arithmetic To Map Sensor Reading</p><p id="2380">12º Lego Episode — <a href="https://readmedium.com/lego-a-proportional-linefollower-robot-d9a951debbef"><b>A Proportional LineFollower Robot </b></a>— Advanced Math To Improve Your Robot’s Steering</p><p id="8b82">8th KidSeries — <a href="https://readmedium.com/j3-folower-line-robot-v1-0-7ad68256de83"><b>J3 Follower Line Robot v1.0</b></a> — The Simplest Follower Line Robot</p><p id="8ab6">13º LEGO Theory — <a href="https://readmedium.com/lego-theory-multitasking-2f060d2e3678"><b>Multitasking </b></a>— A very Useful Programming Technique</p><p id="c586">14º LEGO formula — <a href="https://readmedium.com/lego-formula-normalizing-data-66fefd9eb90e?postPublishedType=repub"><b>Normalizing Data</b></a> — Converting Data to Use The Same Range</p><p id="e388">15 º Lego Episode —<b> <a href="https://readmedium.com/lego-pid-the-ultimate-line-follower-45d4e517572b">PID — The Ultimate Line Follower </a></b>— Algorithm for your EV3 PID Line Follower Robot</p><p id="5b03">16° <a href="https://readmedium.com/lego-meets-pixy-8f8ea7677114?postPublishedType=repub"><b>Lego Meets Pixy Episode</b></a> — How to Connect Your Inexpensive Camera Module to Lego</p><p id="6080">18° Lego Episode — <a href="https://readmedium.com/lego-gears-worms-bf8ef3280d0e"><b>GEARS & WORMS</b></a> — Geartrains & Worm & Clutch Gears</p><p id="1fda">23° Lego Episode — <a href="https://readmedium.com/lego-differential-explained-12fa1865f3db"><b>Differential Explained</b></a><b> </b>— How Differential Works?</p><p id="c21a">24° Lego Episode — <a href="https://readmedium.com/pitbot-a-star-is-born-e04a0f305fb6?source=post_page---------------------------"><b>PitBot — A Star Is Born </b></a>— Working at The First Structure in Our Sparring Robot</p><p id="a6d9">25° Lego Episode — <a href="https://readmedium.com/pitbot-is-agressive-well-no-worries-c801adba4b4c?source=post_page---------------------------"><b>PitBot Is Agressive? Well, No Worries! </b></a>— Making PitBot bite!</p><p id="fe48">26° Lego Episode — <a href="https://readmedium.com/dancing-good-w-pitbot-16b5ef035077"><b>Dancing Good w/ PitBot</b></a> — All The Secret for Replicate This Awesome Robot</p><p id="cf34">27 ° LEGO Episode — <a href="https://readmedium.com/lego-sumo-arena-is-ready-64a59e8bf987"><b>Sumo Arena is Ready! </b></a>— Here is the playing arena for Arduino x Lego</p><p id="db29">28 ° LEGO Episode — <a href="https://readmedium.com/pick-pitbot-up-7dbfa4edc12e"><b>Pick Pitbot Up!</b></a> — Our Robot Are Leaving Body & Paint Shop</p><p id="afcf">28 ° LEGO — B — Episode — <a href="https://readmedium.com/pitbot-battery-sensor-setup-4f572fedfe20"><b>Pitbot Battery & Sensor Setup</b></a> — Preparing The infrastructure for running Arduino code</p><p id="c609">29 ° LEGO Episode — <a href="https://readmedium.com/bridging-all-sensors-together-pitbot-ca8803bf9cb"><b>Bridging All Sensors Together</b></a> — Pitbot — Collecting All Codes for the Final Act of Giving Behaviors to Robot</p><h1 id="546a">Credits & References</h1><p id="6376"><a href="https://g.co/kgs/6kLgmu">Book: The Art of Lego Mindstorms EV3 Programming by Terry Griffin</a></p><p id="d72b"><a href="https://g.co/kgs/D6F5CU">Building Smart LEGO MINDSTORMS EV3 Robots</a> by Kyle Markland (thank you, dude:)</p><p id="00e9"><a href="https://builderdude35.com/downloads-2/">Builderdude35</a></p><p id="cc47"><a href="https://youtu.be/tscYgpD3m7g">EV3 Large and NXT Motors — The Differences Explained</a></p><p id="1240"><a href="https://education.lego.com/en-au/support/mindstorms-ev3/building-instructions">Building Instructions & Program Descriptions</a></p><p id="7e3d"><a href="http://www.philohome.com/motors/motorcomp.htm">LEGO® 9V Technic Motors compared characteristics</a></p><p id="061a"><a href="https://robotics4children.blogspot.com/">Robotics for Children (& Parents (& Grandparents))</a></p><p id="87c2"><a href="https://le-www-live-s.legocdn.com/sc/media/lessons/mindstorms-ev3/building-instructions/ev3-rem-driving-base-79bebfc16bd491186ea9c9069842155e.pdf">Tribot v 1.0 .pdf File</a></p><p id="0763"><a href="https://www.virtualroboticstoolkit.com/documentation/sections/9/articles/39">Introducing LEGO Digital Designer</a></p></article></body>
Lego PID — The Ultimate Line Follower
Algorithm for your EV3 PID Line Follower Robot — Episode#15
Hey everyone! in this LEGO’s episode, I’m going to teach you how to make the ultimate line follower for your EV3 robot: a PID line follower!
Jump to videos? to see my Lego’s youtube playlist — click here;)
A PID line follower is the most radical and complex type of line follower there is but it’s very rewarding because you’ll have the most sophisticated the most advanced the smoothest line follower you could possibly make and it’s definitely worth the effort because you’re going to have a flawless bulletproof line follower that’s very reliable and will work every time.
It’s the most refined and the fluid line follower you could possibly build.
I going to explain everything from the very beginning.
Let’s start by what is a PID control?
Proportional-Integral-Derivative (PID) control is the most common control algorithm used in industry and has been universally accepted in industrial control.
What’s behind a PID Controller?
The basic idea behind a PID controller is toreada sensor, then compute the desired actuator output by calculating proportional, integral, and derivative responses and summing those three components to compute the output.
An appliance using PID, for instance, an air conditioner inverter system can lower the energy consumption and increased temperature control for better thermal comfort compared to on/off controller. In this case, the PID control strategy is focused on low energy consumption and thermal comfort for the indoor user. Back to Lego, though…
Our purpose is to make Tribot follows a black line as fast as it can!
A Lego PID line follower has three parts to it; The first one is Proportional:
Proportional (P): simply multiplies the Error bythe Proportional Gain (Kp) togetthe controller output. In general, increasing the proportional gain will increase the speed ofthe control system response. However, ifthe proportional gain is too large, theprocessvariable will begin to oscillate and your plant becames unsteady:/
In dealing with Proportional, which is the P stands for, you will find a post sent by me in previous Lego’s Episode #12 where I’ve covered that, so maybe you might want to read this article before continuing to read this one.
The second part is Integral:
Integral (I): from school, turns out to be the area under the curve. The integral component sums theerror term overtime. The resultisthat even a small error term will cause the integral component to increase slowly. The integral response will continually increase overtime unless theerroris zero, so the effect isto drive the Steady-State-Error-To-Zero (2_SET_z).
and the third part is the D that stands for Derivative:
Derivative (D): is just a mathematical term meaning rate-of-change. The derivative component causes the output to decrease iftheprocessvariable is increasing rapidly. The derivative response is proportional tothe rate of change oftheprocessvariable. Increasing the derivative time - de(t) - parameter will cause the control systemto react more strongly to changes inthe error term and will increase the speed ofthe overall control system response.
Fig 1. Pure PID
But don’t worry by now with all these concepts, and images… I’m going to explain to you what each of these three parts means individually and show you how we can translate this easily to your Lego Tribot v1.0:)
So to start it off, here is the general formula for a PID life:
This is the math function that we’re going to be incorporating into our program in order to make your EV3 Robot Follow The Line using a PID algorithm.
Now I know this math looks really scary right now and some of you might want to throw it up but I promise that this math is not nearly as difficult as it may seem at first and I’m going to breaking it down step by step to make each part of it more digestible.
However, before we actually jump right into building our Lego program, let’s understand by real example what PID really is like:
Oops, it passes from the point (or better not even reached) where you would like to be, right?:(
Just for the sake of this example let’s assume that you would like to stop well at breakeven on the ground at roughly 100 us; This is your target, right?
Fig 3. Target Point
Here is what you need to do: First, obviously, implementing a braking mechanism; you will need to use the trolley brake and stop it, well, at break-even point ground zero, right? But, how?
It passes from the point initially, and gradually it finds the balance at the ground (zero on the graph above) like a pendulum… The shorter the time to reach the rest point the most perfect is your PID algorithm!
It seems to me that physically it is impossible to stop the trolley at the first turn. There is material stress limit, called by engineer Ultimate tensile strength (UTS), restriction of the human ability to withstand g-force, biologic and other system limitations…all right… we will stop the trolley as rapidly as we can, ok!?
Here is a graph animation trying to convey this idea of a pendulum(sorry for the imperfections…)
Fig 4. Typical pendulum curve
And here is a free graphics Roller coaster Speed x Time, which simulates mathematically the motion observed above; You can download the worksheet now to study PID with me!
Fig 5. Roller coaster speed x time graphic
Please, see that the system gets an amazing speed in the first few seconds.
It is a huge linear acquisition, right?
Then the question is:
How much force should I apply to the brakes?
Let’s make an attempt here: hit hard the brakes, say, 50% proportional to the amount of the speed; This stands for Proportional correction proposal. See the green proportional line on the graph:
Fig 6. Proportional correction (green curve)
As soon as the errors occur as soon as the proportional correction is triggered.
Proportional cares for the present!
But wait! Take a close look at the graph above, my friend…Probably there is no time to make the correction…right at the start, the speed is tremendous!
Do we have how to anticipate this excessive speed gain or direction change?
Sure and the trick is the D; add the D part of the equation; the Derivative correction for the system follows; this makes an anticipate rection!
Fig 7. Derivative correction (blue curve)
If you observe the behavior of the derivative blue curve you will see that it is greatly increased when the speed direction initializes or changes (focus on those two peaks depicted by the yellow arrows above).
In fact, the derivative calculates the rate of change of the variable, as said above, and appears on the curve thousandths of seconds before even the actual change of direction or speed, even before those changes become discernible by the system and that’s just what we need now \o/
Derivative cares for the future!
And finally let’s apply the Integral correction:
Fig 8. Integral correction (orange curve)
The integral simply make the sum of all values (errors, for the sake of this example) during the time lapse. See that the orange line is almost zero because the areas drawn by the curve tend to balance itself, but due to the friction, the original loop no longer repeats, hence the errors accumulate a little.
Integral cares for the past!
To understand, please calculate mentally the areas under the red curve and make the sum of them. If the graph were perfectly symmetric, based on the x-axis, the values would be equal and the integral (orange curve) would be zero. But we have factors that make the system lose momentum and these are frictions generated thought metal contacts and yet by the very movement of your body (mainly your neck) when subjected to such great changes of speed and directions (g-force); these minors error are treated by the integral equation.
Now adds all corrections curves together and you will have a mathematical function as a result (pid_output); and believe me, my friend…your problem is over! Your trolley will be at your command in just 100 us, right on the ground zero!
Fig 9. Pid resolution (dark green curve)
Congratulations, your PID is working \o/
I hope you understood the big picture of PID by now, although superficially, of course;)
ln short, PID it is an algorithm that controls a plant by using some system feedback and a given goal or set point, or target, whatever... This is all you need to understand by now:)
Let's jump right into performing our LEGO ultimate PID line follower robot algorithm!
Our Target
Fig. 10— Color Sensor reading at different positions as shown in Episode#07; the less light reflection, the lower the sensor’s value.
If we find the center point, we would have 72 + 8 divided by 2 would give us 40; let’s force a little more towards the black line and use the number 35, okay?
I’m going to say that 35 is the light sensor reading for our target. With this value, the Tribot must follow the Good Zone ideally.
P for Proportional
Proportional measures deviation from the target path that we’ve established here at 35.
Fig 13. PID_03.ev3 code — PID algorithm working code
My Block PID
Fig 14. MyBlock: PID
More PID Theory
Thank God we have a PID technology!
Let's see what is the term closed loop, that you may find in the literature:
Closed Loop Control System (CLCS) is theprocessof reading sensors to provide constant feedback and calculating the desired actuator output is repeated continuously andata fixed loop rate .
Our goal is to tune a PID controller to reduce overshoot in reference tracking or to improve rejection of a disturbance at the plant input.
What is disturbance rejection? From MacMynowski:
The term disturbance rejection was used anumberof times in lecture today to describe the purpose of feedback. With feedback, the controller is able to use the output to shape the input ofthesystem.
And how about Nonlinear System:
Nonlinear System (NLS) is asystemin which the control parameters that produce a desired response atone operating point might not produce a satisfactory response at another operating point.
I read that simulation is an important tool for getting PID right.
The Setpoint (SP) is the value that we want the process to be.
The PID controller looks at the setpoint and compares it with the actual value of the Process Variable (PV).
Modes of Control
There are various modes out there, here are some combinations:
P — Sometimes used
PI — Most often used
PID — Sometimes used
PD — rare as hen’s teeth but can be useful for controlling servo motors.
Under The Hood Of The PID Controller — Tuning
The process of setting the optimal gains for P, I and D to get an ideal response from a control system is called tuning.
The gains of a PID controller can be obtained by trial and error method. This is my approach to the solution.
I started with PI. This way is the most common occurrences in the literature.
The tricky thing about Integral Action is that it will really screw up your process unless you know exactly how much Integral action to apply.
The way to adjust how much Integral Action you have is by adjusting a term called minutes per repeat.
So where does this strange name come from? It is a measure of how long it will take for the Integral Action to match the Proportional Action.
In other words, if the output of the proportional box on the diagram above is 20%, the repeat time is the time it will take for the output of the Integral box to get to 20% too.
Derivative action improves the controller action because it predicts what is yet to happen. Of course, you can try and filter the noise out, but my advice is that, unless PI control is really slow, don’t worry about switching D on.
Another Tunning Method:
The Ziegler-Nichols method is another popular method of tuning a PID controller. It is very similar to the trial and error method wherein I and D are set to zero and P is increased until the loop starts to oscillate. Once oscillation starts, the critical gain Kc and the period of oscillations Pc are noted. The P, I and D are then adjusted in a tabular column.
And that’s all, folks!
Thanks for reading my episode this week; if you found it helpful be sure to subscribe for more tutorials and if you like this share with your friend.
And if you have an idea for a tutorial be sure to submit it in the comment section below.