我不确定为什么此代码无法正常工作:
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];
这是一个非常简单的代码,它会生成一个步进信号(在step_fun中),然后运行仿真mdl,该仿真mdl是具有步进功能作为输入的PID控制器。然后我正在运行我的PID函数,稍后将通过变量(P,I,D)对其进行说明,并且在集成中,我将成为模拟和mfile之间的不同结果?你能帮我吗?结果是:
% 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
以下是一些带有结果的数字:
最好的问候和提前
所以最后我确实回答了我自己的问题:)
模拟对当前t(i-1)之前的点进行积分,而cumtrapz对当前t(i)进行积分,这就是不同之处。
结果是我将cumtrapz更改为cumtrapzt,在那里我计算了给定数据集(t(end-1))的diff(t)直到t(i-1)。
无论如何要寻求帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句