# Tuning a PID regulator

In modern industry, robots are replacing humans in various tasks whose complexity is getting higher and higher, and as a result, the performance of industrial robots is growing exponentially. With that being said, automatic control algorithms are getting more complex, but the basics remain the same. In this article, we will discuss what is a P-proportional I-integrative D-derivative controller and what are the best practices when it comes to tuning procedure.

#### Process Overview

To implement a PID controller, first of all, we need to find a process that we would like to control. The process of heating an oven is a trivial example which is easy to understand, so the oven is our Plant. To control the temperature, we need feedback on the actual temperature in the oven, and a thermocouple will serve as a Feedback Element. The Controller itself is the PID that we are going to discuss further.

All those components together form a closed-loop control system which controls its output signal by adjusting the input signal of the system according to its error, which is why it is also called an automatic control system. Its basic structure is represented in the image below. Fig. 1 Automatic closed loop control system block diagram

• Input – setpoint given by the operator (%, t°, height, voltage etc.);
• Actuating Signal – actual signal which controls the process (voltage, torque etc.);
• Output – output value of the system (t°, rpm, voltage etc.);
• Feedback Signal – output signal converted in the same units as the Input;
• Error Signal – the difference between the Input and Feedback Signal.

We are going to approximate the behavior of our Plant by using a mathematical model, this way we can simulate different types of controllers and analyze their influence on the process without damaging the real equipment. All simulations are going to be performed in MATLAB Simulink. If you don’t know what a mathematical model is or what is MATLAB you can check Alexandru’s article.

Obtaining the mathematical model of the real process is called identification. Let’s assume that we’ve already done that and the step response curve of our process is the same as in Fig. 2. The step response of the system can be obtained by just turning on our oven and measuring the temperature over time until it reaches its maximum value. Fig. 2 Idealized step response of our Plant

You can notice the dead time τ caused by the fact that the temperature doesn’t start rising as soon as we started our oven, and also the rise time T of our process which represents the time it takes for the system to pass from 10% to 90% of the output value. The settling time is characterized by Ts as the time it takes for the process to reach the steady-state with its corresponding error margins (dashed lines ±5%). With these data, we can model our process as a first-order transfer function in the form of the Laplace transform: From the step response of our process, we can determine the values of the parameters as follows: τ=1 sec., T= 3 sec. Fig. 3 Simulation block diagram in MATLAB Simulink

#### PID controller

We will focus our attention on the controller part which takes as input the Error Signal and has as output the Actuating Signal. The most used control algorithm is PID or its variations as P, PI, PD. To understand it better, let’s discuss each component separately.

###### P – proportional

The proportional regulator – Gain – in our block diagram, is the simplest one and very easy to understand. It scales the error by multiplying it with kpproportional coefficient and applies the result to our Plant. The equation and the transfer function are represented below:  Fig. 4 Step response of the system with proportional control: blue kp=1, orange kp=2, yellow kp=3

We can observe that increasing kp lowers the steady-state error and also decreases the rise time, which means that we are getting closer to our setpoint (purple line) and we are doing it faster. But the drawback of this regulator is that this doesn’t eliminate at all the steady-state error and also induces oscillations and overshoot when kp=3.

###### I – integrative

We need to eliminate the steady-state error, and for that, the integrative component comes in. It could be treated as the sum of all errors computed at each iteration multiplied by the kiintegrative coefficient. It may seem that if this component sums up all errors it will rise to infinity, but as soon as we get just a bit above the setpoint, the error becomes negative and the integrative component goes down. The equation and the transfer function of this component are:  Fig. 5 Step response of the system with integral control: blue ki=0.2, orange ki=0.65, yellow ki=1.5

We can see that the integral control always reaches the setpoint, but the rise time is too large for a small ki(blue line). Increasing the integral coefficient gives us a smaller rise time however, it also adds oscillation and overshoot, which are not desirable, for example, if we would use our oven for baking a semiconductor crystal.

###### D – derivative

The derivative component tracks the error speed, in other words how fast the error is changing. It is calculated by subtracting the current error from the previous error. It gives us information about how fast the temperature is rising or decreasing, so we need an appropriate response to that. This component eliminates the overshoot and decreases the settling time. The derivative control can’t be implemented alone because it doesn’t compensate for the steady-state error and thus we will never reach the setpoint value.

Next table summarizes all you need to know about the relation between PID components and how they affect the performance of the system. Now that we know what is a PID controller let’s get to the tuning part.

#### Tuning the PID controller

The process of finding the PID coefficients kp, ki, kd for a physical process is quite a difficult task and it is based on the trial and error method in most of the cases, even if finding those values for a mathematical model is not rocket science.

For our oven we are going to determine the PID coefficients for the mathematical model using the Ziegler-Nichols method, which is described below:

1. Set the integral and derivative coefficients to zero, ki=0, kd=0.
2. Start increasing the proportional coefficient until the step response of the process reaches stable oscillations. Fig. 6 Step response of the system with proportional control reaching stable oscillations: yellow kp=5.34, blue kp=2.1, orange kp=3.5

Those are called critical oscillations because increasing the kp more would cause our system to be unstable.

1. From this condition, we can measure the critical oscillation period Tcr=3.58s obtained with the critical proportional coefficient of kcr=5.34.
2. Calculate the PID coefficients according to the Zeigler-Nichols estimation table. Using the method, we can determine that the best values for our PID which controls the oven are: kp=4, ki=0.47, kd=0.36. Fig. 7 Step response of the system with PID control tuned with Zeigler-Nichols method: kp=4, ki=0.47, kd=0.36

The results are not very impressive, as we have 40% overshoot, a few oscillations and a settling time of approximately 18 seconds. But don’t get upset, as the heuristic method is used only for getting the start values for our controller. Now that we understand how each component affects the performance, we can tune it manually by adjusting these parameters according to the instructions from Table 1. Fig. 8 Step response of the system with PID control with adjusted parameters: kp=2, ki=0.55, kd=0.58

After playing around a bit with these values, our oven reaches the desired temperature within 3.2 seconds (settling time), without any overheating (overshoot) and oscillations.

Here is an example, written in C, on how to implement a PID regulator as an algorithm which could be loaded into a microcontroller and used to control any process. Special attention should be paid to the value used inside the delay() function which represents the time interval between two data processing cycles and should be selected according to the characteristics of the process. For our oven, 10 cycles per second are enough for tracking the temperature and set the appropriate output control signal, but for example, to control a quadcopter this will be too slow.

```int set_point;          // data from operator
int feedback;           // data from sensor
int output;             // data to active element (heater, actuator etc.)

int error = 0;
int prev_error = 0;
int integral = 0;
int derivative = 0;
int k_p, k_i, k_d;

while (1)
{
error = set_point - feedback;                       // compute current error

integral = inegral + error;                         // compute integral component

derivative = error - prev_error;                    // compute derivative component

output = k_p*error + k_i*integral + k_d*derivative; // compute output value

prev_error = error;

delay(100);                                         // delay in ms
}

```

Let’s summarize what we have done so far. We’ve used the Ziegler-Nichols method to get the start values and then adjusted them manually for our PID regulator, to control the heating process presented as a mathematical model. The results obtained from the simulation are very good, but since the mathematical model is just an approximation of the real process, using the computed coefficients kp, ki, kd for the real process wouldn’t guarantee the same performance, additional adjustments could be necessary.

If you are tired of adjusting the water temperature in your shower, or maybe you are annoyed by your computer fan noise each time you open some software, you can now get rid of all these problems by using the PID control.