# PID ControllersControl Theory 1

Prerequisites: CS 1

## Introduction

A control system is a device, or set of devices, that manages, commands, directs or regulates the behavior of other devices or systems. On our robotics team, we have used two types: open loop and closed loop. Open loop controllers don't incorporate feedback from its output. An example would be the driver controlling the robot's speed directly with joysticks. Closed loop controllers are used to automate processes so the driver doesn't have to control them manually. Our 2015 robot, Talos, used a closed loop controller to seek to different elevator level presets automatically. The driver just needed to provide a level number. We used both state machines and PID controllers in that control system.

## PID Basics and Theory

Since Wikipedia does a good job explaining this, I'll refer to their resources on the topic. Read the introduction of https://en.wikipedia.org/wiki/PID_controller first. Then read the control loop basics section for an example and further explanation. Continue to the next section on PID controller theory. You can ignore the small part about the transfer function for now. I'll cover that in a module on state space representation at some point.

## PID Types

PID controller inputs of different orders of derivatives, such as position and velocity, affect the system response differently. The position PID controller is defined as

$u(t) = K_p e(t) + K_i \int_{0}^{t}e(\tau)d\tau + K_d \frac{de}{dt}$

If a velocity is passed instead, which is a change in position, the equation becomes

$u'(t) = K_p \frac{de}{dt} + K_i \int_{0}^{t}\frac{de}{d\tau} d\tau + K_d \frac{d^{2}e}{dt^{2}}$

$u'(t) = K_p \frac{de}{dt} + K_i e(t) + K_d \frac{d^{2}e}{dt^{2}}$

This shows that Ki and Kp from the position controller act as proportional and derivative terms respectively in the velocity controller. Ki from the position controller has no equivalent in the velocity controller. If we were to implement one, it would use a double integral. However, it would be of limited use since the Ki term in the original equation also eliminates steady-state error for step changes in reference. Relabelling the coefficients to match the position PID controller gives

$u'(t) = K_p \int_{0}^{t}e(\tau) d\tau + K_d e(t)$

## Controller Tuning

Since the team started, we've been tuning PID controllers manually.

### Position PID tuning

1. Set Kp, Ki, and Kd to zero.
2. Increase Kp until the output starts to oscillate around the reference.
3. Increase Kd as much as possible without introducing jitteriness and instability in the response.
4. If the controller settles at an output above or below the reference, increase Ki such that the controller reaches the reference in a reasonable time.

Note: Adding an integral gain to the controller is an incorrect way to eliminate steady state error. A better approach would be to tune it with an integrator added to the plant, but this requires a system model. Since we are doing output-based rather than model-based control, our only option is to add an integrator to the controller.

Beware that if Ki is too large integral windup can occur. As the Modifications to the PID algorithm section of the Wikipedia page states:

"Following a large change in setpoint the integral term can accumulate an error larger than the maximal value for the regulation variable (windup), thus the system overshoots and continues to increase until this accumulated error is unwound."

Basically, a lot of error is accumulated and it takes a while for the error to unwind so the controller can start heading back toward the reference.

### Velocity PID tuning

1. Set Kp, Ki, and Kd to zero.
2. Increase Ki until the output starts to oscillate around the reference.
3. Increase Kp as much as possible without introducing jitteriness and instability in the response.

There is no equivalent to position PID's integral term in velocity PID, but one isn't needed. A velocity controller will not have steady state error for step inputs because the Ki introduces a pole in the controller.

### Other Notes

Tuning in this way can require some intuition as to what values produce the desired response. An alternative would be using an auto-tuner that follows something like the Ziegler-Nichols method.

We've used LiveGrapher in the past to visualize the system response so we can more effectively tune it. We've used it in a production environment with our robots since 2013. The server side of LiveGrapher is in the RobotModules repository.

## Limitations of PID control

Read Wikipedia's section on the limitations of PID control.

Continuing from the section to which I just linked, another example of a non-linear system is an elevator/carriage assembly which has to operate under gravity. One can have two sets of PID constants for travelling in each direction. We did this on our 2015 robot. A better approach would be applying a feedforward to compensate for gravity, thus making the system linear again. One just needs to determine an approximate motor value to add to the output to make the system maintain its position under load.

For fixing noise in the derivative, we've used a rolling average filter and a Kalman filter. The latter works better but requires tuning. I'll cover each in CT 5 and CT 4 respectively.

The section on modifications to the PID algorithm is interesting, but one won't need it unless one is modifying his or her PID controller. I've been working to get some of those modifications integrated into WPILib's PIDController class.