
Jun 29, 2013, 12:41 PM  
United States, NY, Poughkeepsie
Joined May 2002
231 Posts

Discussion
Auto PID Tuning Theory
I just released my open source MultiCopter software (called BradWii). You can follow the thread here:
http://www.rcgroups.com/forums/showthread.php?t=1922403 BradWii includes PID autotuning. In this thread, I'll explain the theory used. Maybe others will want to incorporate it into their own projects. For an overview of PID, check out my other thread: http://www.rcgroups.com/forums/showthread.php?t=1890703 OK, so here's how it works. We start with two input parameters. The first is our target angle. By default it's 20 degrees, so we will bank to 20 degrees, see how the system reacts, then bank to 20 degrees and evaluate again. We repeat this over and over. The second input is how much oscillation we can tolerate. This can range from 2 degrees to 5 or more degrees. This defaults to 4 degrees. The higher this value is, the more agressive the result of the tuning will be. First, we turn the I gain down to zero so that we don't have to try to figure out how much overshoot is caused by the I term vs. the P term. Then, we move to the target of 20 degrees and analyze the results. Our goal is to have no overshoot and to keep the bounce back within the 4 degrees. By working to get zero overshoot, we can isolate the effects of the P and D terms. If we don't overshoot, then the P term never works in the opposite direction, so we know that any bounce we get is caused by the D term. If we overshoot the target 20 degrees, then we know our P term is too high or our D term is too low. We can determine which one to change by looking at how much bounce back (or the amplitude of the oscillation) we get. If it bounces back more than the 4 degrees, then our D is already too high, so we can't increase it, so instead we decrease P. If we undershoot, then either our P is too low or our D is too high. Again, we can determine which to change by looking at how much bounce we get. Once we have the P and D terms set, we then set the I term by repeating the same test above and measuring the overshoot. If our maximum oscillation is set to 4 degrees, then we keep increasing the I until we get an overshoot of 2 degrees, so that our oscillations are now centered around our target (in theory). In the BradWii software, it alternates between doing the P and D step and doing the I step so you can quit whenever you want without having to tell it specifically to do the I term. The sequence is actually P&D, P&D, I, P&D, P&D, I... If you watch the autotune part of the video in the link above, you should be able to see the corresponding motions. 

Jul 12, 2013, 08:12 PM  
United States, NY, Poughkeepsie
Joined May 2002
231 Posts

OK, here's a graph to show what we are doing, assuming our target is 20 degrees and our bounce back is 4 degrees.
You need to do more study on what each of the PID terms represents. I can cause overshoot, but we turn that off. If we don't overshoot, as is shown in the graph, then P doesn't contribute to the bounce back because P ALWAYS pulls TOWARD the target. 
Jul 14, 2013, 11:41 AM  
Joined Feb 2011
12 Posts

Hi Brad,
In my autopilot code I have a different auto tuning procedure implemented which I have never actually tried out. Wondering if you want to try it or have any comments. First the I is set to a very low value and I think it should stay there so that's not something I want to make tunable. The user shouldn't even worry about it. Next the only parameter the user sets is the P, or alternatively the time in which it wants an error to be corrected (and we can set the P accordingly). Say the user wants his copter to become level in approx 0.1s from whatever position it has at the moment, and we will set a P that's inverse proportional to that time. Now the only parameter we need to tune is the D and by my understanding there always exists a D that will work. So what we do to find the D is we first set it to 0 and let the copter swing 34 times and the code observes the oscillation period. Every swing should take about the same amount of time although the amplitude will be increasing or decreasing. Say every swing took 0.25s. Now what we do is we take half the average oscillation period from those few swings and we set the D such that the PID value will be what the PI (i.e. D=0) value would have been in half that period from now assuming the angular speed doesn't change. So your D will be 0.125 so that if you multiply it by the current rotation rate from the gyro in degrees/second you'll get the number of degrees the copter will turn in 0.125s. With this the oscillation should disappear (hopefully before the copter hits the ground) and it should be just the right value, i.e. any lower value will cause the oscillation to reappear and any higher value will slow down the copter's reactions. 
Jul 14, 2013, 12:08 PM  
United States, NY, Poughkeepsie
Joined May 2002
231 Posts

I doubt very much you could get your method to work.
First, you can't just let the person choose a correction time because the lowest obtainable correction time without becoming unstable is based on the physical/electronic system. Second, the chances of creating observable oscillations without the copter flipping around wildly would be slim. Third, I can't understand your math, but I don't think it's right. Let us know when you've actually got it working. 
Thread Tools  
Similar Threads  
Category  Thread  Thread Starter  Forum  Replies  Last Post 
Discussion  PID Tuning multiwii  Take It Outside  Multirotor Electronics  0  Apr 28, 2013 07:23 PM 
Help!  pid tuning  dewy14  Multirotor Electronics  0  Mar 19, 2013 03:27 PM 
Discussion  Tuning PID on new hex  lordofscones  Multirotor Talk  0  Mar 09, 2013 03:36 AM 
Rant  Can you say tune your PID's?  Disp0sablehero  Multirotor Talk  6  Mar 01, 2013 02:42 PM 