马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
各位大侠,本人是s函数的初学者,编了个pid控制的S函数,运行怎么不对啊?
代码如下:
function [sys,x0,str,ts] = simpid(t,x,u,flag,kp,ki,kd)
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
case 1,
sys=mdlDerivatives(t,x,u);
case 2,
sys=mdlUpdate(t,x,u);
case 3,
sys=mdlOutputs(t,x,u,kp,ki,kd);
case 9,
sys=mdlTerminate(t,x,u);
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
function [sys,x0,str,ts]=mdlInitializeSizes(t,x,u,flag,kp,ki,kd)
sizes = simsizes;
sizes.NumContStates = 3;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 1;
sizes.NumInputs = 1;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1; % at least one sample time is needed
sys = simsizes(sizes);
x0 = [0;u;0];
str = [];
ts = [0 0];
function sys=mdlDerivatives(t,x,u)
x(2)=u;
x1dot=x(2);
x2dot=x(3);
sys =[x(1);x1dot;x2dot];
function sys=mdlUpdate(t,x,u)
sys = [];
function sys=mdlOutputs(t,x,u,kp,ki,kd)
sys =kp*x(2)+ki*x(1)+kd*x(3);
function sys=mdlTerminate(t,x,u)
sys = []; |