How to control a valve with PID
Show older comments
In order to maintain a constant pressure in a system, I need to control a valve opening. If the pressure is more than the required value, increase the valve opening and vice versa. How can I control this using a PID controller so that the pressure remains at the required value and not overshoot or undershoot?
5 Comments
Aquatris
on 18 Jul 2018
How much do you know of PID (proportioal-integral-derivative) controller?
In your system, you need to create a feedback loop. You will take the pressure measurement, substract it from the desired pressure value, which is called the error. The error is then needs to be fed to the PID controller, where a simple algebraic control law calculates required input to the valves (can be either voltage or current depending on whether the valves are voltage or current controlled). Then with that input to the valves, the valves will open or close and change the pressure. Then pressure is measured again and cycle continues.
On a high level, proportional gain determines how fast your system will respond to changes, whether it is a disturbance or set point change. Derivative gain determines the amount of overshoot. Integral gain gets rid of any steady state error. What part do you need explanation?
Gustav Ngemasong Chafac
on 22 Jul 2018
Aquatris
on 22 Jul 2018
First you will calculate the error while storing the previous time step error (for derivative term) and calculating cummulative error (for integral term);
error = desiredPressure - measuredPressure;
errorC = errorC + error; % cumulative error term
... % your whole code here
errorP = error; % at the end of the code you
% will store the previous error
Then you will feed the error and errorP to the controller where PID law will result in the required current;
Kp = 10; Ki = 20; Kd = 1; % controller gains to be tuned
current = error*Kp + (error-errorP)/timeStep *Kd + errorC*Ki;
Thats about it.
If you are using transfer functions, then transfer function of PID controller looks like;
controller = Kp + Kd*s + Ki/s;
Gustav Ngemasong Chafac
on 15 Aug 2018
Aquatris
on 16 Aug 2018
errorC is cumulative error and yes you will start with errorC = 0 and then each step you add the new error. This serves as a simple integral.
errorP is the previous error and it is used to find the derivative of the error signal, or the rate of change of the error.
The idea stays the same whether it is real life or simulation.
Here is a simple mass-spring example;
% Simple mass spring example
% actuator applies force to the mass
% sensor sense the position of the mass
% control drives the mass to desired position
% /| _____
% /| k | |
% /|---/\/\/\/--| m |
% /| |_____|
% /|
%
% mass-spring parameters
k = 10; % [N/m]
m = 2; % [kg]
% system equation of motion
% xd = Ax + Bu
% y = Cx + Du
A = [0 1;
-k/m 0];
B = [0
1/m];
C = [1 0];
D = 0;
sys = ss(A,B,C,D); % marginally stable open loop
% input is force, output is position
% controller
Kp = 10; Ki = 8; Kd = 5; % parameters to be tuned
s = tf('s');
K = Kp + Ki/s + Kd*s;
% closed loop system
sysCL = feedback(sys*K,1);
% simulate response of the system
step(sysCL)
Or if you are more of an equation type of guy;
dt = 1e-3; % time step size, the smaller the number is,
% the more realistic the outcome is due to integration
t = 0:dt:15;% time vector
x(1) = 0; % initial position
xd(1) = 0; % initial velocity
xdd(1) = 0; % initial acceleration
u(1) = 0; % initial control input
eC = 0; % errorC, cumulative error for integral control
eP = 0; % errorP, previous error
desiredX = 1; % desired position of the mass
% can be time dependent function instead of a constant
for i = 1:length(t)-1
e = desiredX - x(i); % calculate error
eC = eC+e;
u(i) = Kp*e + Ki*eC*dt + Kd*(e-eP)/dt; % control law right here
xdd(i) = 1/m*(-k*x(i) + u(i)); % accel = totalForce / mass
xd(i+1) = xd(i)+xdd(i)*dt; % get velocity via integration of accel
x(i+1) = x(i)+xd(i)*dt; % get position via integration of vel
eP = e; % store current error as previous error
% for the next iteration of the loop
end
% plot the system response
plot(t,x)
Answers (0)
Categories
Find more on PID Controller Tuning in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!