PID stands for proportional integral derivative. PID controller is a common method of controlling robots. It is especially important for people who has no knowledge in control theory. This tutorial will teach you the basic of the PID theory and will help you design better control system for your robots.

The Application of this system is very wide, from robot that rides bikes, self-balancing robot to flying drone.

# Brief Introduction of A Control System

PID controller is a control system. Shown here is the basic closed-loop (a complete cycle) control system diagram:

The control systems are used to get your robot to react the way you want it to be given the change of environment. The sensor will determine the change of the environment, the program you write defines what the final result should be, and the actuator actually makes the change.

# So What’s PID Controller

The PID controller calculation algorithm involves three separate constant parameters, and is accordingly sometimes called three-term control: the **proportional (P)**, the **integral (I)** and **derivative (D) **values.

- Proportional (P) – The proportional term is typically the error. This is usually the distance you want the robot to travel, or perhaps a temperature you want something to be at. The robot is at position A, but wants to be at B, so the P term is A – B.
- Integral (I) – The integral term is the accumulation of past errors over time (t). For example, your robot continually is on average off by a certain amount all the time, the I term will catch it. Lets say at t1 the error was A, at t2 it was B, and at t3 it was C. The integral term would be A/t1 + B/t2 + C/t3.
- Derivative (D) – The derivative term is the change of error over time (t). For example, the error was C before and now its D, and t time has passed, then the derivative term is (C-D)/t. Use the timer on your microcontroller to determine the time passed.

The weighted sum of these three actions is used to adjust the process via a control element such as the position of a control valve, a damper, or the power supplied to a heating element.

# PID Controller Explained

There are a few terminology you should know before we proceed.

## Terminology

**Error** – The error is the amount at which your device is not doing something right. For example, if your robot is going 3 mph but you want it to go 2 mph, the error is 3 mph-2 mph = 1 mph. Or suppose your robot is located at x=5 but you want it at x=7, then the error is 2. A control system cannot do anything if there is no error – think about it, if your robot is doing what you want, it would not need control!

**Constant (gain)** – Each term (P, I, D) will need to be tweaked in your code. There are many things about a robot that is very difficult to model mathematically (ground friction, motor inductance, center of mass, duck-tape holding your robot together, etc.). So often times it is better to just build the robot, implement a control equation, then tweak the equation until it works properly. A tweak constant is just a guessed number that you multiple each term with. For example, Kd is the derivative constant. Ideally you want the tweak constant high enough that your settling time is minimal but low enough so that there is no overshoot.

## Basic Equation

**P*Kp + I*Ki + D*Kd**

What you see in this image is typically what will happen with your PID robot. It will start with some error and the actuator output will change until the error goes away (near the final value).

The time it takes for this to happen is called the settling time. Shorter settling times are almost always better. Often times you might not design the system properly and the system will change so fast that it overshoots (bad!), causing some oscillation until the system settles.

And there will usually be some error band. The error band is dependent on how fine a control your design is capable of – you will have to program your robot to ignore error within the error band or it will probably oscillate. There will always be an error band, no matter how advanced the system.

To ignore acceptable error band, for example you can do this:

if error <= .000001 //subjectively determined acceptable then error = 0; //ignore it

The Effect of the Gains will result in something like this:

# Complete PID Equation

Combining everything from above, here is the complete PID equation:

Actuator_Output = Kp*P + Ki*I + Kd*D

or in easy to understand terms:

Actuator_Output = Kp * (distance from goal) + Ki * (accumulative error) + Kd* (change in error)

# Simplification

Rarely will you need the integral term so you can just delete and ignore it. The only time you will need this term is when acceleration plays a big factor with your robot. If your robot is really heavy, or gravity is not on it’s side (such as steep hills), then you will need the integral term. Control without the integral term is commonly referred to as simply PD control.

There are also times when you do not require a derivative term, but usually only when the device mechanical stabilizes itself, works at very low speeds so that overshoot just does not happen, or you simply do not require good precision.

# Sampling Rate Issues

The sampling rate is the speed at which your control algorithm can update itself. The faster the sampling rate, the higher precision control your robot will have. Slower sampling rates will result in higher settling times and an increased chance of overshoot (bad). Sampling Rate is largely to do with micro-controller timer.

To increase sampling rate, you want an even faster update of sensor readings, and minimal delay in your program loop. It is good to have the robot react to a changing environment before it drives off the table, anyway.

The rule of thumb is that the sample time should be between 1/10th and 1/100th of the desired system settling time.

Pingback: Raspberry Pi Color Tracking Using PID - OscarLiang.net