I am not sure why this code is not working:
if true
%% PID Test file
% Start the script
clear
clc
kp = 180;
kI = 3200;
kD = 1;
start_time = 0;
step_time = 1;
end_time = 2;
Ts = 0.1;% Sample Time
step_value = 1;
initial_value = 0;
Fn = 50;
W = 2*pi*Fn;
Phase = 0;
Amp = 1;
steps = 1;
if steps
[t,y] = step_fun(start_time,step_time,end_time,Ts,step_value,initial_value);
sim('PID_Test_sim.mdl')
else
t = start_time:Ts:end_time;
y = sin(W*t);
sim('PID_Test_sim.mdl')
end
x_min = start_time;
x_max = end_time;
y_min = initial_value - 1;
y_max = step_value + 1;
figure();
subplot(2,1,1);
stairs(t,y);grid on;hold on; stairs(ScopeData.time,ScopeData.signals(1,2).values,'--r');hold off;%y([y_min y_max]);xlim([x_min x_max]);
subplot(2,1,2);
plot(t,y);grid on;hold on; plot(ScopeData.time,ScopeData.signals(1,2).values,'--r');hold off;%y([y_min y_max]);xlim([x_min x_max]);
Input = y;
time = t;
dt = diff(time);
D = [0,diff(Input.*kD) ./ dt]';
I = [cumtrapz(time,(Input.*kI))]';
%I = [0;(I(1:end-1)+I(2))];
I_test = I;
I_test(I_test>0) = (I(find(I>0,1,'first'))) + I(I>0);
I_test = [0,I_test(1:end-1)];
P = [Input*kp]';
Compare = ScopeData1;
figure();
subplot(3,1,1);
stairs(time,P);grid on;hold on; stairs(Compare.time,Compare.signals(1,1).values,'--r');hold off
subplot(3,1,2);
plot(time,I);grid on;hold on; plot(Compare.time,Compare.signals(1,2).values,'--r');hold off
subplot(3,1,3);
plot(time,D);grid on;hold on; plot(Compare.time,Compare.signals(1,3).values,'--r');hold off
Test = [I ScopeData1.signals(1,2).values time'];
[time,Output] = PID_fun(kp,kI,kD,Input,time,ScopeData1);
figure();
subplot(2,1,1);
stairs(t,y);grid on;hold on; stairs(ScopeData.time,ScopeData.signals(1,2).values,'--r');hold off;
subplot(2,1,2);
plot(time,Output);grid on;hold on; plot(ScopeData.time,ScopeData.signals(1,1).values,'--r');hold off;
Test = [I ScopeData1.signals(1,2).values I_test P ScopeData1.signals(1,1).values D ScopeData1.signals(1,3).values time' Output ScopeData.signals(1,1).values];
It is a very simple code that generate a step signal (in step_fun) and sfterword run the simulation mdl which is a PID controller with step function as input. and then i am running my PID function which is explained later by variables (P,I,D) and in the integration i am becomming different results between simulation and mfile? can you help me with this? ther results are:
% code
% My integration results of the input signal when kI = 3200;
mfile simulation
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
160 0
480 320
800 640
1120 960
1440 1280
1760 1600
2080 1920
2400 2240
2720 2560
3040 2880
3360 3200
here is some figures with the results:
Best Regards and thx in advance
so finally i did answer my own question :)
the simulation takes the integration on the point beofer the current one t(i-1) yet the cumtrapz takes the integration for the current point t(i) and that is the differents.
as a results i have changes my cumtrapz to cumtrapzt where i calculated the diff(t) till t(i-1) of the given data set (t(end-1))
thx for the help anyway