【发布时间】:2015-10-11 18:19:03
【问题描述】:
我不确定为什么这段代码不起作用:
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,它是一个以阶跃函数作为输入的 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
这是一些带有结果的数字:
最好的问候和提前谢谢
【问题讨论】:
-
即使您的代码的非 Simulink 部分也无法运行,因此很难提供有用的帮助。这些图没有帮助,但从打印的数字来看,两个模拟可能在做同样的事情,只是时间上有一个偏移。您说 Simulink 使用固定时间步长,但随后您使用
diff计算向量dt。向量中的值应该接近,但可能存在数值错误。尝试使用实际的固定时间步长。什么决定了该步骤的开始时间?也许>需要是>=或类似的。 -
首先感谢重播。您无法运行非 Simulink 代码,因为您没有“step_fun”。对于这些数字,您将无法运行它们,因为它们是模拟结果和 mfile 结果之间的比较。所以在你的情况下,如果你只是想运行我的代码,我建议:1-将变量(步骤 = 1)设置为零,然后你将生成一个仍然无法与模拟进行比较的正弦波,因为你没有模拟模型只是一个 PID 块,其输入具有与 mfile 中生成的相同属性的正弦波或阶跃波。所以你可以自己做!!!!!!