フィルターのクリア

How to keep track of accumulated error for integral term in a PID controller function?

13 ビュー (過去 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?

採用された回答

Sam Chak
Sam Chak 2023 年 10 月 31 日
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
Gc = 1 Kp + Ki * --- s with Kp = 1, Ki = 1 Continuous-time PI controller in parallel form.
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 件のコメント
David
David 2023 年 10 月 31 日
This works. Thank you.
Sam Chak
Sam Chak 2023 年 10 月 31 日
@David, I'm glad that it works for you. If you find the solution helpful, please consider clicking 'Accept' ✔ on the answer and voting 👍 for it.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGeneral Applications についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by