## Archive for the ‘Matlab’ Tag

### PID Controller Simplified

This is an attempt to explain PID controller with minimum use of maths.

A simple closed loop control system consisting of a controller and a process (or plant) is shown below.

The input to the system is the ‘setpoint’, ie the desired output. The input to the controller is the error.

Error = Present Output – Setpoint.

The two steps in the design of a control system are –

1. Mathematically model the plant to be controlled.

2. Design the Controller.

The block diagram of a PID controller is shown below –

A PID controller consists of a Proportional element, an Integral element and a Derivative element, all three connected in parallel. All of them take the error as input. Kp, Ki, Kd are the gains of P, I and D elements respectively.

The best way to understand something is by simulating it. So I simulated a PID controller in matlab. The matlab code is provided at the end of this article.

Let me assume a suitable mathematical model for the plant and then go ahead with designing the controller.

Let the transfer function of the plant be 1 / ( s^2 + 20s + 30 ).

The step response of a system is the output of the system when the input to the system is a unit step. The open loop step response of the above plant is –

(Click on the image to get an enlarged picture)

It can be seen that the step response output is close to 0.035. The steady state error = 1-0.035 = 0.965. That’s quite high! Also observe that the settling time is around 3 sec.

Now lets see what is the effect of PID controller on the system response.

Lets see the effect of proportional element on the system output.

Keeping Kp = 10, Ki = 0, Kd = 0 the step response of the system is

The output is now 0.25. Much better than the open loop response! (The curve in red shows the open loop step response of the plant)

Now let me increase the Kp further and observe the response.

Keeping Kp = 100, Ki = 0, Kd = 0 the step response of the system is

The output is now 0.77. So it’s clear now that increasing Kp will reduce the steady state error.

Keeping Kp = 200, Ki = 0, Kd = 0 the step response of the system is

The output is around 0.87. Also observe that the ripples have started appearing in the output. If Kp is increased further it will only lead to increase in ripples or overshoot. The rise time also has decreased. Also observe that there is a small steady state error (1 – 0.87 = 0.13).

**Conclusion –
Increasing Kp will reduce the steady state error.
After certain limit, increasing Kp will only increase overshoot.
Kp reduces rise time.**

Now lets keep Kp fixed. Lets start varying Ki.

Keeping Kp = 200, Ki = 10, Kd = 0 the step response of the system is

The output is now close to 0.99. That’s very close to the setpoint. But observe that settling time has increased.

Keeping Kp = 200, Ki = 200, Kd = 0 the step response of the system is

Observe that rise time has now reduced and steady state error is very small.

Keeping Kp = 200, Ki = 300, Kd = 0 the step response of the system is

Observe that steady state error is close to 0 now. But increasing Ki has resulted in overshoot.

Further increasing Ki will only increase overshoot.

**Conclusion –
Ki eliminates the steady state error.
After certain limit, increasing Ki will only increase overshoot.
Ki reduces rise time.**

Now lets Keep Ki fixed and start varying Kd.

Keeping Kp = 200, Ki = 300 and Kd = 10.

Wow! What a response! Where is the overshoot? It has disappeared. There is a reduction in settling time as well.

Increasing Kd further will only result in response getting worsened.

**Conclusion –
Kd decreases the overshoot.
Kd reduces settling time.**

So the ideal PID values for our plant is Kp = 200, Ki = 300 and Kd = 10.

The above process is known as manual tuning of PID.

Here is the matlab code used to simulate PID –

(The below code is written by me. So please let me know if you find any bugs!)

*num=1;
den=[1 20 30];
Plant = tf(num,den);
step(Plant,’r’);
hold on;*

Kp=200;

P_Sys = tf(Kp,1);

Ki=300;

den2=[1 0];

I_Sys=tf(Ki,den2);

Kd=10;

num3=[Kd 0];

D_Sys=tf(num3,1);

PI=parallel(P_Sys,I_Sys);

PID=parallel(PI,D_Sys);

OpenLoop=series(PID,Plant);

ClsdLoop = feedback(OpenLoop,[1]);

step(ClsdLoop,’b’);