HobbyKing.com New Products Flash Sale
Reply
Thread Tools
Old Jun 29, 2013, 12:41 PM
Registered User
United States, NY, Poughkeepsie
Joined May 2002
231 Posts
Discussion
Auto PID Tuning Theory

I just released my open source Multi-Copter 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.
bradquick is offline Find More Posts by bradquick
Reply With Quote
Sign up now
to remove ads between posts
Old Jul 02, 2013, 09:08 AM
Registered User
NunoFlyer's Avatar
Portugal, Aveiro
Joined Aug 2010
86 Posts
Edited, resolved.
NunoFlyer is offline Find More Posts by NunoFlyer
Last edited by NunoFlyer; Jul 02, 2013 at 09:23 AM.
Reply With Quote
Old Jul 02, 2013, 09:11 AM
Registered User
United States, NY, Poughkeepsie
Joined May 2002
231 Posts
In my code, you set the 20 degrees and the 4 degrees in config.h
bradquick is offline Find More Posts by bradquick
Reply With Quote
Old Jul 02, 2013, 09:17 AM
Registered User
NunoFlyer's Avatar
Portugal, Aveiro
Joined Aug 2010
86 Posts
Thanks.

Had seen these lines but thought it was not.
Soon I will test my Multiwii SE v2.0
Quote:
#define AUTOTUNE_MAX_OSCILLATION 4.0
#define AUTOTUNE_TARGET_ANGLE 20.0
NunoFlyer is offline Find More Posts by NunoFlyer
Reply With Quote
Old Jul 02, 2013, 04:06 PM
Registered User
NunoFlyer's Avatar
Portugal, Aveiro
Joined Aug 2010
86 Posts
The autoTuning PID is activated with a aux, right?
NunoFlyer is offline Find More Posts by NunoFlyer
Reply With Quote
Old Jul 02, 2013, 04:22 PM
Registered User
United States, NY, Poughkeepsie
Joined May 2002
231 Posts
Yes
bradquick is offline Find More Posts by bradquick
Reply With Quote
Old Jul 11, 2013, 07:54 AM
Registered User
Joined Apr 2013
4 Posts
Hi Bradquick,

I am looking at your autotuning code.
i understand that Yaw is a P-only control , not PID, is that right ?
Setting Pyaw = 2 * Proll gives you always good result ?

During the autotuning procedure, I guess the quad is moving randomly around the field. Based on your experience what kind of drift do you expect ? a few meters ?

Can you correct the multicopter position with stick ( to avoid crashing in the trees or to correct the altitude ) while autotuning, or is it completely on "autopilot" during the procedure ?

From you code i understand that the tuning is not stopping by itself ? Am i right ?
So it is looping forever until you decide to stop ? There is no way to know if the optimum setting have been reached ?
grostim is offline Find More Posts by grostim
Reply With Quote
Old Jul 11, 2013, 10:00 AM
Registered User
United States, NY, Poughkeepsie
Joined May 2002
231 Posts
Yaw does not really autotune. I just set yaw proportional to roll, just to try to get into the ballpark. This may not always give great results.

I found it difficult to autotune yaw because my technique requires some bounce back and when I got the yaw D term high enough to get bounce back, it became two sensitive to outside influences.

Yes, you can correct the position with the stick but it's best to let it do it's own thing as much as possible. You are best autotuning in a place without trees nearby.

It doen't stop by itself. Eventually it will zero in on a setting and stay around there.
bradquick is offline Find More Posts by bradquick
Reply With Quote
Old Jul 11, 2013, 12:32 PM
OlliW
Joined Sep 2009
1,585 Posts
hey Brad

I am trying to follow your autotuning recipe, but from the above I find it impossible to understand it... it sounds as if there is a means to distinguish between "oscillation" and "overshoot"... this doesn't make sense, because overshoot is just so to say a damped version of oscillation. How is it possible to determine from the fact that overshoot exceeded 4deg if that's due to P or D? Moreover, the I will also affect the overshoot/oscillation and not only "center" the oscillation, how is this dealt with?

Anyway, great work.

Olli
OlliW is offline Find More Posts by OlliW
Reply With Quote
Old Jul 12, 2013, 12:21 AM
Registered User
United States, NY, Poughkeepsie
Joined May 2002
231 Posts
Olli,

We aim for zero overshoot. This is critical, because it allows us to look at the bounce back and know that all of the bounce back is caused by the D term.

So our target position is 20 degrees. We aim for a situation where we get to exactly 20 degrees, the bounce back to 16 degrees (4 degrees of bounce), then we don't really care what happens after that.
bradquick is offline Find More Posts by bradquick
Reply With Quote
Old Jul 12, 2013, 12:36 AM
OlliW
Joined Sep 2009
1,585 Posts
thx Brad for your effort to explain it again

I'm still there I was before... with zero overshoot there would be also zero bounce back, by definition.... furthermore, according to standard PID theory all three PID terms may generate overshoot, bounce back, oscillation, so all three "features" are mutually linked with all three "terms"...

so, thanks again for your effort, but I guess it would be wasted effort to try to explain it further to me


EDIT:
OK, I went into your code, and found another explanation there which is MUCH better. I hence copy it to here, maybe others like to see it too
Quote:
// The theory behind auto tuning is that we generate a step in the setpoint, then analyze how the system reacts.
// First, we set the I term to zero and work on only the P and D terms.
// The target angle alternates from negative to posive FPAUTOTUNETARGETANGLE degrees. We set the target, then wait until
// we cross the zero angle, just to make sure our angular velocity is in the right direction, then we look for the peak
// which we recognize by observing the angular velocity switching direction.
// Our goal is to get zero overshoot. By requiring zero overshoot, we can assume that any subsequent oscillations are
// caused by our D term, since at zero overshoot, the P term doesn't have any effect. Once we see the first peak, we then
// start a timer and look for a second peak in the negative direction. The difference between the first and second peak will
// tell us the effect the D term is having. We will then try to keep this oscillation amplitude within a predeterrmined range.
//
// So, after we reach the target, then wait a little longer to record the peak oscillation, we have four choices:
// if (overshoot)
// {
// if (too much oscillation) decrease P
// else increase D
// }
// else // undershoot
// {
// if (too much oscillation) decrease D
// else increase P
// }
So, you in fact detect TWO peaks, the first one and second one. This addresses my first point. The second is still left open, but as I said it's maybe not worthwhile to further elaborate on that
OlliW is offline Find More Posts by OlliW
Last edited by OlliW; Jul 12, 2013 at 03:46 AM.
Reply With Quote
Old Jul 12, 2013, 08:12 PM
Registered User
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.
bradquick is offline Find More Posts by bradquick
Last edited by bradquick; Jul 14, 2013 at 11:59 AM. Reason: Add the graph
Reply With Quote
Old Jul 14, 2013, 11:41 AM
Registered User
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 3-4 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 P-I (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 re-appear and any higher value will slow down the copter's reactions.
balrogk1n is offline Find More Posts by balrogk1n
Last edited by balrogk1n; Jul 14, 2013 at 11:45 AM. Reason: use better example
Reply With Quote
Old Jul 14, 2013, 12:08 PM
Registered User
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.
bradquick is offline Find More Posts by bradquick
Reply With Quote
Old Jul 14, 2013, 12:36 PM
OlliW
Joined Sep 2009
1,585 Posts
to be honest, I think you should first get your own method to work right (so far it's proven to not converge) before you come down onto others that harsh...

TO
OlliW is offline Find More Posts by OlliW
Reply With Quote
Reply


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