Projects / [WIP] PID Control of an Inverted Pendulum / Guidance, Navigation & Control
A simple proportional, integral, derivative (PID) controller for an inverted pendulum.
Settings
Loading simulation…
I wrote this article to those who may not be familiar with control theory but have a basic understanding of calculus, enjoy!
Above we have a pendulum that is allowed to freefall after starting in an arbitrary upright position. Suppose our goal is to control the system such that the pendulum stays upright without falling.
Our first task would be to find the mathematical equation(s) that tell us how this system moves, what forces it’s under, what variables are relevant, etc. Perhaps we could call it…
The Equation of Motion
An equation of motion describes the dynamics of a system; how it changes in time. In the pendulum’s case, it describes how it swings, or its angular motion. Angular force is called torque.
The equation of motion for the pendulum is simply,
I’ll leave the derivation as an exercise to the reader, but more importantly here,
- is the mass of the pendulum,
- is acceleration due to gravity,
- is the length of the pendulum’s arm,
- is the angle, measured clockwise-positive from the vertical,
- is the rotational damping coefficient, and
- is the applied torque.
Each “dot” above a variable represents its derivative with respect to time.
Notice that for the right-hand side of the equation, the first two terms are determined by physics, whereas the third — the applied torque, is what we will be able to control.
The goal therefore, is to drive our angular position to zero by applying such that angular velocity pushes the pendulum upright, which itself is driven by angular acceleration .
The Control Law
A PID controller operates on three constants:
- the proportional gain,
- the integral gain,
- the derivative gain.
each has a slightly different purpose, but together they create the simple equation,
Where is the control output and is the error.
This may seem complicated, but may become more intuitive explained piecewise.
P: Current Error
The proportional term is, in my opinion, the most straightforward. In layman’s terms it says: “the further away I am from my target, the harder I should try to get there.”
Imagine you are driving down the highway and drifted slightly out of your lane, the proportional term tells you how aggressively to turn the steering wheel in response to that error.
The larger your , the greater the effort. Make it too large, and even a small error will cause a huge correction — often leading to undesirable overshoot. You’ve swung your steering wheel all the way over when drifting out of your lane by only an inch… not ideal.
I: Accumulated Error
The integral term exists, in short, to reduce persistent errors over time that may not be able to eliminate on its own. This is called steady-state error and exists in many systems for a variety of reasons: hysteresis, sensor bias, static friction, to name a few.
Suppose now your car is being struck by a constant crosswind. alone has succeeded in preventing you from drifting further, but still you remain slightly off-center in your lane. notices the error and slowly turns your wheel towards the center, the longer the error persists, the more it will push to correct it. Once it centers you, it retains that non-zero amount of control (steering) to steadily correct for the constant wind.
Integral terms are susceptible to something called integral windup, where an actuator reaches its limit but error continues to accumulate. This is an important issue to be aware of, but solving it is outside our scope for now.
Note: an idealized pendulum as seen above likely wouldn’t need an integral term, as the system is naturally unstable outside of its equilibrium point.
D: Changing Error
Finally, the derivative term at its highest level exists as a way to shape the response (in the form of damping). It says that if the error is growing very quickly, to respond more aggressively and vice versa. Notice that it actually becomes negative if error is shrinking, which is useful to avoid overshoot!
Once more to the car analogy, if responds to how far you’ve drifted out of your lane; responds to the speed of that drifting. Drifting rapidly? Add more steering correction. Already heading back toward the center quickly? Ease off the wheel so you don’t cross to the other side.
This article was written by hand, no AI!