s_function 2dof

1 次查看(过去 30 天)
James Patrick
James Patrick 2020-7-31
Hello!!!!
l try to use s_function in simulink and I get this error:
Error in 'BRAS_2DOF/S-Function1' while executing MATLAB S-function 'Dynamique2DOF', flag = 0 (initialize), at start of simulation.
Caused by:
Subscript indices must either be real positive integers or logicals.
this is my code in joint piece

回答(1 个)

ag
ag 2024-7-16
编辑:ag 2024-7-17
Hi James,
The error message that you are facing is caused by the below line:
M = [m1*lc1^2+m2(l1^2+lc2^2+2*l1+lc2*cos(q2))+I1+I2, m2(lc2^2+l1*lc2*cos(q2));m2lc2^2+l1*lc2*cos(q2)+I2, m2lc2^2+I2];
As m2 is defined as a scalar in the script, using indexing in the following way "m2(l1^2+lc2^2+2*l1+lc2*cos(q2))" is not supported.
There are a few more syntax errors that I obsereved in the code provided. Below is the code after correcting the syntax errors with the comments explaining the changes.
function[sys,x0,str,ts] = Dynamique2DOF(t,x,u,flag)
m1=9.5; m2=5;
l1=0.25; l2=0.16;
lc1=0.20;lc2=0.14;
I1=4.3*10^(-3); I2=6.1*10^(-3);
g=9.8;
q2=1;
%providing a temporary definition for M, C and G
M = [1, 2; 3, 4]
C = [1, 2; 3, 4]
G = [1, 2; 3, 4]
%M = [m1*lc1^2+m2(l1^2+lc2^2+2*l1+lc2*cos(q2))+I1+I2, m2(lc2^2+l1*lc2*cos(q2));m2lc2^2+l1*lc2*cos(q2)+I2, m2lc2^2+I2];
%to avoid the error "Subscript indices must either be real positive integers or logicals."
%C = [-m2*l1*lc2*sin(q2)*dq2, -m2*l1*lc2*sin(q2)*(dq1+dq2); m2*l1*lc2*sin(q2)*dq2, 0];
%dq2 is undefined
%G = [(m1*lc1+m2*l1)*g*cos(q1)+m2*lc2*g*cos(q1+q2); m2*lc2*g*cos(q1+q2)];
%q1 is undefined
sys = 0, x0 = 0, str = 0, ts = 0; %these variables must be initialized irrespective of the "flag", as these are returned by the function "Dynamique2DOF"
switch flag %comma is not allowed here
case 0
[sys,x0,str,ts]=mdlInitializeSizes;
case 1
sys = mdlDerivatives(t,x,u);
case 3
sys = mdlOutputs(t,x,u);
case { 2, 4, 9 }
sys = [];
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
end %ending a function is necessary
function [sys,x0,str,ts]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 2;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 4;
sizes.NumInputs = 2;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;
sys=simsizes(sizes);
x0=[1 0.8 0 0];
str=[];
ts=[1 0];
end %ending a function is necessary
function sys=mdlDerivatives(t,x,u) %semicolon is not allowed here
dq1 = q2;
dq2 = (tau - C*dq2 + G)/M;
sys(1)=dq1;
sys(2)=dq2;
sys(3)=ddq1;
sys(4)=ddq2;
end %ending a function is necessary
function sys=mdlOutputs(t,x,u)
sys(1)=q1;
sys(2)=q2;
sys(3)=q3;
sys(4)=q4;
end %ending a function is necessary
Hope this helps!
  1 个评论
Walter Roberson
Walter Roberson 2024-7-17
MATLAB does not support implied multiplication at all. m2(expression) is treated as an indexing request, not as a multiplication request.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Block and Blockset Authoring 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by