PID Controllers and their Elegant Approach for Implementing Autopilots in Phyics-based Games

Overview01I’ve been working on a new hobby game based on elements of the classic 80s game Carrier Command.  For the uninitiated this game sees you in charge of an aircraft carrier at war with an enemy in a local archipelago.  With your arsenal of AI-controlled VTOL aircraft and amphibious units your role is to capture the islands and convert them to friendly bases.  Apart from being highly addictive and fun any programmer would appreciate the technical aspect of the game particularly implementing the AI-controlled units and autopilots.

One of the first things I began to work on was the altitude autopilot.  With it the VTOL AI could take off and automatically fly to a pre-determined altitude.  My first approach was perhaps a little naïve being too “mechanical” in nature and not particularly accurate so I put it on hold for a while.

At some point I came across the PC game Stormworks which allows you to design and drive your own vehicles including custom microcontrollers. By learning the game’s various ways one can implement say heading autopilot on a boat, I graduated from a rudimentary:

If the target is to the left then 
    turn left by some scaled amount  
else if to the right then 
    turn right by some scaled amount

…form that tended to oversteer to something I’ve not heard of before but is common in the world of servo controllers and industrial control systems – PID controllers.

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. A PID controller continuously calculates an error value {\displaystyle e(t)}e(t) as the difference between a desired setpoint (SP) and a measured process variable (PV) and applies a correction based on proportional, integral, and derivative terms (denoted P, I, and D respectively), hence the name. Tell me more

Essentially a PID looks at where it currently is in terms of error, where it has been over time and a best estimate for the future [1]. It is the continuous feedback that makes PIDs great compared to other techniques that are simply reacting to a particular moment in time. Better still PIDs only take a few lines of code!

Image courtesy of Wikipedia [1]

Let’s see how this operates in action. The first image below, the VTOL situated on the deck of the aircraft carrier is beneath the desired altitude of 30 m.

  • The blue line represents the desired altitude
  • Green line represents current altitude
  • Red represents the current error or the raw value that is fed into control systems, in my case the vertical thruster that raises the aircraft.

This image shows the VTOL has reached the desired altitude and the PID is rapidly reducing output so as to avoid overshooting.

Eventually things stable out. Regular puffs from the thruster are needed to counter the effects of gravity, as can be seen by the red blips below:

As mentioned, there’s not much too it code-wise, here is a section from my PidController class:

var Kp = _settingsData.Kp; // error
var Ki = _settingsData.Ki; // past values compensator
var Kd = _settingsData.Kd; // best estimate of future trend

var error = SetPoint - PV;
_integral += error * dt;
var derivative = (error - _previousError) / dt;
Output = Kp * error + Ki * _integral + Kd * derivative;
_previousError = error;

Output is a public property which I access from my main VtolAutopilot in order to apply forces to the rigid body. It’s important to clamp any values from the PID so as to match the characteristics of my thruster.

var o = _pidController.Output;
var output = Mathf.Clamp(o, _minForce, _maxForce);
if (output == 0)
	return;

_rigidBody.AddForce(0, output, 0);

Update: I no longer directly connect the PID output into the force equation as you’ll see in my next post.

Citations

1. https://en.wikipedia.org/wiki/PID_controller

One thought on “PID Controllers and their Elegant Approach for Implementing Autopilots in Phyics-based Games

  1. Pingback: PID controller-VTOL-throttle Integration for the Control of Force in Satisfying Autopilot-governed Altitudes in Phyics-based Simulations | MickyD's Random Thoughts

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.