kpzc_001 发表于 2008-3-31 10:56

请教SIMULINK仿真:during flag=3 call must be a real vector of length 3.

我用SIMULINK仿真三关节机器手,用的是拉格朗日动力学方程建模、滑模控制算法,模型中的矩阵给一些简单的值时仿真程序可以调的通,但是当把实际推导出来的公式代入模型中的矩阵时,程序调不通。
出现的错误为:
Output returned by S-function 'sgj_s' in 'sgj/S-Function' during flag=3 call must be a real vector of length 3.
还有
Warning: Unable to reduce the step size without violating minimum step size of 1.028677501121793e-015 at time 0.2895469756710336.Continuing simulation with the step size restricted to 1.028677501121793e-015 and using an effective relative error tolerance of 1.845523258936908, which is greater than the specified relative error tolerance of 0.1.
S函数如下:(望高手指点,不胜感激)
function = s_function(t,x,u,flag)
switch flag,

case 0,
    =mdlInitializeSizes;
    case 3,
    sys=mdlOutputs(t,x,u);

case {2,4,9}
    sys=[];

otherwise
    error(['Unhandled flag = ',num2str(flag)]);
end
% end sfuntmpl
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function =mdlInitializeSizes

sizes = simsizes;
sizes.NumContStates= 0;
sizes.NumDiscStates= 0;
sizes.NumOutputs   = 3;
sizes.NumInputs      = 15;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 0;   % at least one sample time is needed
sys = simsizes(sizes);

x0= [];

str = [];

ts= [];
% end mdlInitializeSizes

%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
%
function sys=mdlOutputs(t,x,u)
q1d=sin(pi*t);
dq1d=pi*cos(pi*t);
ddq1d=-pi^2*sin(pi*t);
dddq1d=-pi^3*cos(pi*t);
q2d=cos(pi*t);
dq2d=-pi*sin(pi*t);
ddq2d=-pi^2*cos(pi*t);
dddq2d=pi^3*sin(pi*t);
q3d=sin(pi*t);
dq3d=pi*cos(pi*t);
ddq3d=-pi^2*sin(pi*t);
dddq3d=-pi^3*cos(pi*t);
ddqd=;
dddqd=;
x=;
dx=;
e1=x(1)-q1d;
de1=x(2)-dq1d;
dde1=dx(2)-ddq1d;
e2=x(3)-q2d;
de2=x(4)-dq2d;
dde2=dx(4)-ddq2d;
e3=x(5)-q3d;
de3=x(6)-dq3d;
dde3=dx(6)-ddq3d;
e=;
de=;
dde=;
n1=;
n2=;
n=;
s=dde+n1*de+n2*e;
g=9.8;
M11=3.67+3*cos(x(3))+cos(x(5))+cos(x(3)+x(5));
M12=2.33+1.5*cos(x(3))+cos(x(5))+0.5*cos(x(3)+x(5));
M13=0.33+0.5*cos(x(5))+0.5*cos(x(3)+x(5));
M21=M12;
M22=1.67+cos(x(5));
M23=0.33+0.5*cos(x(5));
M31=M13;
M32=M23;
M33=0.33;
M=;
dM11=-3*sin(x(3))*x(4)-sin(x(5))*x(6)-sin(x(3)+x(5))*(x(4)+x(6));
dM12=-1.5*sin(x(3))*x(4)-sin(x(5))*x(6)-0.5*sin(x(3)+x(5))*(x(4)+x(6));
dM13=-0.5*sin(x(5))*x(6)-0.5*sin(x(3)+x(5))*(x(4)+x(6));
dM21=dM12;
dM22=-sin(x(5))*x(6);
dM23=-0.5*sin(x(5))*x(6);
dM31=dM13;
dM32=dM23;
dM33=0;
dM=;

C11=-(3*sin(x(3))-sin(x(3)+x(5)))*x(4);
C12=-(1.5*sin(x(3))+0.5*sin(x(3)+x(5)))*x(3)-(sin(x(5))+sin(x(3)+x(5)))*x(6);
C13=-0.5*(sin(x(5))+sin(x(3)+x(5)))*x(6);
C21=(0.5*sin(x(3)+x(5))+sin(x(3)))*x(2);
C22=-0.5*sin(x(5))*x(4);
C23=-0.5*sin(x(5))*x(6)-sin(x(5))*x(2);
C31=0.5*(sin(x(5))+sin(x(3)+x(5)))*x(2)+sin(x(5))*x(4);
C32=0.5*sin(x(5))*x(4);
C33=0;
C=;

dC11=-(3*cos(x(3))*x(4)+cos(x(3)+x(5))*(x(4)+x(6)))*x(4)-(sin(x(3))+sin(x(3)+x(5)))*dx(4);
dC12=-(1.5*cos(x(3))*x(4)+0.5*cos(x(3)+x(5))*(x(4)+x(6)))*x(3)-(1.5*sin(x(3))+0.5*sin(x(3)+x(5)))*x(4)-(cos(x(5))*x(6)+cos(x(3)+x(5))*(x(4)+x(6)))*x(6)-(sin(x(5))+sin(x(3)+x(5)))*dx(6);
dC13=-0.5*(cos(x(5))*x(6)+cos(x(3)+x(5))*(x(4)+x(6)))*x(6)-0.5*(sin(x(5))+sin(x(3)+x(5)))*dx(6);
dC21=(0.5*cos(x(3)+x(5))*(x(4)+x(6))+cos(x(3))*x(4))*x(2)+(0.5*sin(x(3)+x(5))+sin(x(3)))*dx(2);
dC22=-0.5*(cos(x(5))*x(6)*x(4)-0.5*sin(x(5))*dx(4));
dC23=-0.5*(cos(x(5))*x(6)*x(6)+sin(x(5))*dx(6))-(cos(x(5))*x(6)*x(2)+sin(x(5))*dx(2));
dC31=0.5*(cos(x(5))*x(6)+cos(x(3)+x(5))*(x(4)+x(6)))*x(2)+0.5*(sin(x(5))+sin(x(3)+x(5)))*dx(2)+cos(x(5))*x(6)*x(4)+sin(x(5))*dx(4);
dC32=0.5*(cos(x(5))*x(6)*x(4)+sin(5)*dx(4));
dC33=0;
dC=;

G1=2.5*g*cos(x(1))+0.5*g*cos(x(1)+x(3))+0.5*g*cos(x(1)+x(3)+x(5));
G2=1.5*g*cos(x(1)+x(3))+0.5*g*cos(x(1)+x(3)+x(5));
G3=0.5*g*cos(x(1)+x(3)+x(5));
G=;
dG1=-2.5*g*sin(x(1))*x(2)-0.5*g*sin(x(1)+x(3))*(x(2)+x(4))-0.5*g*sin(x(1)+x(3)+x(5))*(x(2)+x(4)+x(6));
dG2=-1.5*g*sin(x(1)+x(3))*(x(2)+x(4))-0.5*g*sin(x(1)+x(3)+x(5))*(x(2)+x(4)+x(6));
dG3=-0.5*g*sin(x(1)+x(3)+x(5))*(x(2)+x(4)+x(6));
dG=;

H0=M*(n1*dde+n2*de-dddqd)+C*(n1*de+n2*e-ddqd)-(dM+n*M)*-(dC+n*C)*-(dG+n*G);
xite=;
ut=-inv(n)*(H0+xite*sign(s));
sys(1)=ut(1);
sys(2)=ut(2);
sys(3)=ut(3);

% end mdlOutputs
页: [1]
查看完整版本: 请教SIMULINK仿真:during flag=3 call must be a real vector of length 3.