Alex’s Secrets of the PID

by Alex “OnTheSnap” Rose

Alex’s Ten Minute Tip: Secrets of the PID

OK, so first lets define what we’re trying to solve.  Lets define what a “system” is.  Our example for this is a car.  The car, wheels, and steering wheel is our system.  Our goal is to keep the car centered on the road, on the right side, and centered in our lane.

So how do we do this?  First lets try the simplest approach. Lets make an “open loop” system. What does this mean?  Well we give an input to the steering wheel, it changes course.  But there is no feedback.  No way to know where the car is.  So this isn’t so useful. OK.

So now lets do better.  Lets implement a sensor. This sensor tells us how far from our “setpoint” we have deviated.  The farther we’ve deviated the higher the output from the sensor. This is a closed loop system.

OK, so now lets use this sensor. First lets take the sensor output, and just react 100% to correct the error.  Well now you can imaging the car violently zig zagging down the road.  We want to do better than this.  So lets introduce the idea of varying the response to the error proportionately to the size of the error.  If the car is way off course, we give a big correction.  If the error is small, we give a little correction.  This is way better!  Because the response is proportional, call it P. We can scale this value to adjust how quickly the system responds to try and find the best value for our system.

So this works ok, but wait there is a problem. Our mechanical systems have some “play” or noise in them. The sensor has some limited accuracy. The end result, is that we never quite hit our target perfect. Always some oscillation around the set point.  It doesn’t work great. We have to wait until the error is large enough to react, and then correct. If you’re a few inches off the target, you may turn a little to correct.  But the slop in the system doesn’t make the wheels turn. Since you are going in the right direction, you’ll just end up staying off course by a few inches forever.  Annoying!

So to fix this we introduce another parameter to our control system.  This parameter can look at the history of errors of our setpoint and provide a correction factor to eventually get us back on course. This in effect, holds our setpoint, or heading. And effectively cancels out the “play” in the mechanical system.

Great! So now we have something that reacts to errors, and pulls us back. And also works to keep us on our desired heading.  Lets call this I.  The Integral (summation) of the errors of our setpoint.

But wait, there is another problem. 🙁  Lets say a huge gust blows our car way off course.  Our response is strong, and the car yanks itself so hard back on course, the tires lift off the ground and we are in trouble.  OK, this is not good. So what do we do. We need a way to slow down the corrections.  The slowing effect should increase as the magnitude of the response increases.   So we calculate the Derivative of the error.  Call it D.  Now we can increase P for a fast response, but not overdrive the system so that it’s too quick when the errors get big.

This is great!  Now we can really dial in our system to smoothly drive the car down the road.

So now we understand the basics of PID and why it’s useful in a control system.  So what about helicopters?

Helicopters use a gyro.  The gyro detects speed of rotation. Not position. But we can use it to try and hold our “setpoint”. A heli has 3 gyros.  One for each axis.  Aileron, elevator, and rudder. With the absence of any input, the model has 3 PIDs, and 3 setpoints. The goal is to hold the heading so long as the pilot is not trying to change the setpoint. When the pilot gives a command, the system is told to change the setpoint.  While the command is still there, the system tries to maintain a rotation rate proportional to the input. When the pilot input suddenly goes away, a new static setpoint is set, and the system locks into the new setting.

Great! So now we understand the basic idea of what our flight control system is trying to do. So now what do we do with these PID settings??!

P – So here we are adjusting how fast the system is going to respond to an error. A low P gain and the system is slow and mushy.  A very high P gain, and the system responds too hard and over reacts.  Do we always want the maximum p gain without oscillation? No!  In fact there is a “feel factor” where lower p gains may give a preferred feel, and be less robotic.

I – So here is the factor that controls our heading hold effect. The higher the value, the more locked in the model feels. Too high, you will see some slow oscillations.  Too low and the model will tend to fall out of position. And not do a good job holding its “setpoint”.

D – Derivative gain really comes into play on stops.  Stops are just defining a new setpoint! A low D gain means the system will be coming into the stop VERY aggressively.  And will likely overshoot and “bounce back”. Too high of a D gain though can cause oscillations in the stop. Just right and the system drops right into the new setpoint cleanly.

If you’d like to hear the audio version, this content was discussed in the 2nd half of Episode 127.