How to keep track of accumulated error for integral term in a PID controller function?
18 次查看(过去 30 天)
显示 更早的评论
This is my PI controller function:
function [ control ] = my_fastcontrol(t, v)
Kp = 20;
K_i = 10;
v_desired = 20;
error = v_desired - v ;
control(2) = Kp*error + Ki(int(error, 0, t));
end
It is called by this line in another file:
[t1, y1] = ode45(@(tau, y1) my_dynamicvehicle(tau, y1, @(tau) my_fastcontrol(tau, y1(2)), friction_factors(j)), [t0 tf], z0);
where, y1(2) is the velocity. my_dynamicvehicle() is also a separate file that contains the differential equations that models the behaviors of a car.
My question is I am trying to take the integral of all the error from 0 to t. The proportional term has no problem as it only cares about the velocity that gets passed to the my_fastcontrol() function at the present instant, but the integral terms need a history of all the velocities at the previous times. How does my_fastcontrol() gets access to that history?
0 个评论
采纳的回答
Sam Chak
2023-10-31
Hi @David
This simple example shows how to obtain the integral error signal by adding an auxiliary state in the my_Dynamics() function. The example uses a first-order system and is given by . Two systems are used in the simulation for comparison purposes. The first one is approached using a transfer function, while the second one employs the ode45 method.
%% System 1
Gp = tf(1, [1 1]); % 1st-order plant
Gc = pid(1, 1) % PI controller
Gcl = feedback(Gc*Gp, 1); % closed-loop feedback system
subplot(211)
step(Gcl, 10), grid on % step response
%% System 2
tspan = [0 10]; % integration time interval
v0 = [0; 0]; % initial values, incl the auxiliary state
[t, v] = ode45(@my_Dynamics, tspan, v0);
% Plot result
subplot(212)
plot(t, v(:,2)), grid on % plot the original system state, v(2)
yticks([0 0.5 1])
xlabel('t'), ylabel('v'), title('Result from ode45')
% Main function
function dvdt = my_Dynamics(t, v)
dvdt = zeros(2, 1);
v_desired = 1;
error = v_desired - v(2);
dvdt(1) = error; % add an auxiliary state that returns v(1) = int(error)
dvdt(2) = - v(2) + my_PIcontrol(v); % <-- calling PI control signal from my_PIcontrol()
end
% Local function (to be used in the Main function)
function control = my_PIcontrol(v)
Kp = 1; % proportional gain
Ki = 1; % integral gain
v_desired = 1; % desired steady-state value for v(2)
error = v_desired - v(2);
control = Kp*error + Ki*v(1); % remember that v(1) = int(error)
end
2 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Model Type and Other Transformations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!