Warning:

This guide is specifically for the usage of the PID function within SimplePlanes. Do not use this guide as a reference for PID tuning in other contexts.

1. Introduction


"A proportional–integral–derivative controller (PID controller or three-term controller) is a control loop mechanism employing feedback that is widely used in industrial control systems and a variety of other applications requiring continuously modulated control. (Wikipedia)"

That probably doesn’t help, so in simpler terms, a PID controller is a system that calculates an output to match certain requirements. It’s especially useful when you have fluctuating variables, because unlike fixed algorithms, a PID is very flexible and automatically adjusts to get the desired result quickly and accurately.


2. When should I use a PID controller?


If you’re just starting out with this stuff, chances are you aren’t quite sure if you have to use a PID or not. To put simply, if the system you’re trying to use the PID on has a constantly changing environment and there isn’t a specific or exact method to program the system available, you should use a PID.

For example, a situation in which a PID would be desirable is when making a hovering system to maintain a constant aircraft altitude. An example where you would not need one is when creating a system to simply apply a suicide burn while falling below a certain altitude.


3. Understanding the PID controller


A PID controller is one of the more complex tools you have access to in the Funky Trees system. As with any other function offered in FT, one must first understand how the function is structured and what each parameter signifies. The PID function is structured as follows:

PID(target, current, p, i, d)

Conceptually, there’s a complex mathematical equation that’s supposed to do this, but for the purposes of SimplePlanes there is no real need to understand this form.

Back to the FT form of PID, here’s an explanation of each variable:

  • Target: The target variable is the end goal. This value is what you want your current variable to be equal to.
  • Current: The current variable is the observed status, which is compared to the target variable in order to match the current as best as possible to the target.
  • P: Proportional. This is a constant variable that determines how much the PID should output in order to match the current to the target.
  • I: Integral. This is a constant variable that determines how quickly the PID should attempt to match the target.
  • D: Derivative. This is a constant variable that determines how careful the PID should be, that is, to not overcorrect and end up overshooting the target.

Everything about a PID controller is incredibly case-specific, so it's difficult to give an example, but if we were to take the example of the hovering system earlier, we might set target to 5 and current to AltitudeAgl, which would mean that the PID would attempt to keep your craft at 5 meters above ground level.


4. Tuning a PID controller


Now for tuning your PID controller. Tuning a PID is a skill that requires some practice, and even with experience it will take a good chunk of time to get good parameters. Just keep this in mind! It's also exceedingly difficult to get parameters that are optimal, so what we're looking for here is a set of numbers that will get the job done well enough. There's largely two methods to tuning a PID controller in SimplePlanes. One is taking a guess at what the parameters might need to be, putting them in the PID function, and trying it out in the sandbox. Unless you have an otherworldly sense of what PID parameters need to be, this is impractical and will consume a lot of time. The second method is an express tuning method that allows you to adjust PID's while on the fly in the sandbox, making tuning flexible and quicker.

  1. Snow's Express PID Tuning Method

    1. Setup

    First, identify your target and current variables. If you've understood how a PID works from the above sections, you should be identify these with ease. Then, set up your PID as follows:

    PID(yourtarget, yourcurrent, sum(Pitch/250), sum(Roll/250), sum(Yaw/250))

    Obviously, yourtarget and yourcurrent should have been replaced by whatever your target and current variables are. Now, once this is inside the inputController of the part you need the PID for, you can now load in the craft to the sandbox. When you've loaded in, set up debug consoles--DebugExpression--for each of the following code snippets: sum(Pitch/250), sum(Roll/250), and sum(Yaw/250). You should a total of three items for your debug console.

    2. Tuning Methods

    The two methods I will outline in this article are both experimental methods. They depend on your sense for observing the situation at hand. Anyhow, below are the outlines of the two methods.

    Method 1 Method 2
    Begin by making sure all values in the Debug Console are set to 0. Begin by making sure all values in the Debug Console are set to 0.
    Slowly manipulate Pitch in order to increase the P gain. Repeat until the response to disturbances are steady oscillations. Slowly manipulate Pitch in order to increase the P gain. Repeat until the response to disturbances are steady oscillations.
    Slowly increase D (by manipulating Yaw) until the oscillations cease. Halve your current P value.
    Repeat the process of increasing P and D until increases in D cannot fix the oscillations of the system. Increase I (by manipulating Roll) until your system is responding quickly enough for your needs.
    Return the P and D values to the last values used before the previous step. The I gain can be increased for your needs but typically is zero for this method. Some overshoot will occur, so increase D until the amount of overshoot is acceptable.
    3. Clarification & Details

    Disturbances

    The methodology of both methods are very straightforward and requires simply observing your system. To clarify with an example however, if we were to build a hover-engine system as we discussed earlier in this article, giving a slight change to the height of the craft would be a "disturbance". In such a case, I would likely do something like set the target--the altitude of the craft--to something like 10 meters above ground, and create a location that is 5 meters above ground to see how the PID responds to the change. What the "disturbance" is will very much depend on the usage scenario, so you need to fundamentally understand what you will use the PID for in order to effectivley tune one.

    Usage Needs

    Unless you're a professional, trying to obtain optimal parameters is extremely difficult. As a result, your results will always be a result of various trade offs. Typically, by increasing system stability you increase response time and vice versa. As a result, the questions to ask are, "Do I need my system to respond quickly with less emphasis on precision", or "Do I need my system to be extremely accurate, albeit slightly slow?" In totality, PID tuning is quite an expansive process, and experience is key when trying to obtain best results. I hope this article helps you get started on PID tuning in SimplePlanes. Good luck, and if you need any other clarifications forward them to me so I can add an extra paragraph or two to cover your concerns.