Hallo all,
I am not sure why this code is not working:
if true %%PID Test file
% Start the script
clearclckp = 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')endx_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 offsubplot(3,1,2);plot(time,I);grid on;hold on; plot(Compare.time,Compare.signals(1,2).values,'--r');hold offsubplot(3,1,3);plot(time,D);grid on;hold on; plot(Compare.time,Compare.signals(1,3).values,'--r');hold offTest = [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];end
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:
if true % code
% My integration results of the input signal when kI = 3200;
mfile simulation0 00 00 00 00 00 00 00 00 00 0160 0480 320800 6401120 9601440 12801760 16002080
1920 2400 2240 2720 2560 3040 2880 3360 3200 end here is some figures with the results:
Best Regards and thx in advance
Best Answer