PID is an acronym for "Proportional, Integral, Derivative". These are in fact 3 algorithms that work together to form a basic "control system".
There are several keywords that I have to introduce before we go on to tuning.
- Plant - the thing that you control. In our case, it's an aeroplane or multicopter.
- Plant Output - normally it would be just plain fun but for the purpose of this tutorial, it's things like pitch, roll and yaw.
- Output rate - how quickly, in a unit of time, the output changes. So yaw rate is how fast does your aircraft spin.
- Setpoint : the intended value of the output. Let's say, that you want your multicopter not to spin - just keep the current heading. This means that your setpoint for the yaw rate is zero.
- Error. This is the important one. It's not that your quadcopter makes errors. Well it kind of does, but who doesn't? For this tutorial, error is the difference between setpoint and actual plant output.
- Control Action. This is also the plant input. Basically it is what the control algorithm calculates the plant has to do. For example, if you were to control the multicopter yourself, you would apply rudder to correct for the yaw rate.
To have any kind of control over the plant, one has to be able to measure plant output (yaw rate), estimate the error, apply a "control algorithm", to the error signal, and eventually, apply the Control Action to the plant.
Ok, the above terms are clear, but how do I tune the PID?
There is a number of scientific methods to do that, but here I will present a very simple "do it yourself" method - no maths! With a bit of practice, it should give satisfactory results. Remember, read first, then do.
OpenPilot uses a 'dual loop' controller, with internal PI loop and outer PI loop. the "D" part is not used by default.
- Start by switching to the "rate" mode in the Input Configuration - Flight Mode Switch tab. In this mode, attitude stabilisation is not used and you can safely ignore it's settings.
- Set the settings to default. If they don't work for you, set the Proportional setting to a low value like 10, and Integral value to zero.
- Set Proportional gain to a small value. Try to fly. It will not feel well - it will feel 'unstabilised' and uncontrollable.
P is "proportional". The control action will be proportional to the error. This simple method gives suprisingly good results for many plants.
Increase the P by 15%. Increasing by less will probably not make much difference, and increasing more risks you to overdo it. If the P gain is at appropriate value, the plant will begin to feel "responsive" to your setpoint.
- Continue with point 4 until given output begins to "oscillate". If it feels like oscillating, or the motors yell too much, reduce P by 10% and save the result.
- If you find yourself stepping by less then 5% it is probably not worth it - better to have the "P" term a bit low rather than too high.This applies to other terms too.
- The goal should be to obtain as 'stiff' or 'responsive' response to the setpoint as possible, but without introducing oscillations. Oscillations are bad.
- Then goes the "I" gain. I stands for integral. I trust that it was zero for you while tuning "P". It's a parameter that corresponds to a kind of memory of error over time. Let's say, that error is, eg. 5 degrees, and it does not decrease despite corrective action from Proportional algorithm. This error gets accumulated in a special accumulator - and as this accumulator fills up, the Integral algorithm will add more and more to the corrective action. This way some imperfections of balance of your airframe can be corrected, for example if the battery is not central, or something has landed on one of the arms It might also help to fly straight in windy conditions. Be the more carefull with this parameter though. When set too high, it can introduce strange, slow (low frequency) oscillations that will be annoying for you to compensate with your sticks.
- By this point you probably know what to do - start with a small value, and increase slowly. 20% steps are OK. Do more testing this time - as the effects of I parameter are rather subtle, unless you overdo it, in which case it becomes annoying.
- When you are happy with the response of the multicopter in the rate mode, you can start to tune outer control loop - Attitude stabilisation. However, remember that it will not work correctly if the inner loop is not tuned well.
- Start by setting the Integral term to zero and Proportional to a small value. Note that in the Attitude stabilisation mode, the effect of the control sticks is very different from the rate mode - as you are now controlling the attitude and not the rate. Kind of obvious but might be surprising to newcomers.
- As before, increase the proportional term by 10-15% and try flying. The multicopter will become responsive and eventually controllable. Again, if the propotional gain is too high, you will get oscillations but they will be kind of slower this time. In such case back off the Proportional term a step back.
- Same as before goes for the Integral parameter in the Attitude stabilisation mode. You should generally need less or zero of it in the outer loop.
- Finally, if you feel like an expert, you can try the D (derivative) parameter.
D is for "derivative" over time. Some people call it the accelerator parameter, because it will try to predict what the plant (or you) is trying to do and magnify the Control Action. In other words, if the error is increasing, it will add more to the Control Action. If the error is decreasing fast, it will reduce the Control action, to make the error approach zero more carefully. This parameter can have positive effect on overall stability of the plant, eg. by helping to overcome the inertia of ESC and motor/propeller faster. But a bit of warning here. D parameter makes the plant more sensitive - if your multirotor oscillates, "D" will make oscillations worse. "D" can amplify the measurement noise (eg. when the gyro is not measuring the pitch accurately enough). It can also get into a complicated interaction with P and I terms. If use D, you might need to re-tune P and I to keep the plant well stabilised. So, all in all - use carefully. In any case, remember to start in the "rate" mode (inner loop only), as the Attitude mode (outer loop) cannot correct for incorrect operation of the inner loop.
There are 3 plant outputs to control - Pitch, Roll and Yaw, how to tune all of them? In a sequence?
The answer depends on the airframe you have. For quadcopters, they are quite symmetric, so you can set the same PID parameters to Pitch, and Roll, and Yaw at the same time. Yaw is kind of safer than Pitch and Roll so it's probably OK for a start with the same values as for Pitch/Roll. Then, when your quad is relatively stable, you can alter the Yaw gains independently. For non-quad frames like hexa or tricopter, you might want to fine tune the pitch and roll separately, after you have some flight experience.
For other airframes, in particular the ones that can be stable with no on-board controller - start with the most 'uncontrollable' output first, while leaving other at zero.
To speed up the tuning process, you can use the TxPID module in the GCS if your transmitter is capable of it.
If this tutorial is not enough, and you want more details - take a look at the OpenPilot TV, episode 2, in which excellent demonstration is given, followed by some theory:
by Jurek, 06-2012