S-function error: sizes vector

11 次查看(过去 30 天)
liang
liang 2020-4-19
回答: liang 2020-4-19
There exists error:
Sizes vector returned by MATLAB S-function 'vehicle_model_verification_Sfunction' in 'vehicle_model_verification/S-Function' must be a real vector consisting of integer value of length 7
when I run the S-function code:
function [sys,x0,str,ts,simStateCompliance] = vehicle_model_verification_Sfunction(t,x,u,flag)
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes;
%%%%%%%%%%%%%%%
% Derivatives %
%%%%%%%%%%%%%%%
case 1,
sys=mdlDerivatives(t,x,u);
%%%%%%%%%%
% Update %
%%%%%%%%%%
case 2,
sys=mdlUpdate(t,x,u);
%%%%%%%%%%%
% Outputs %
%%%%%%%%%%%
case 3,
sys=mdlOutputs(t,x,u);
%%%%%%%%%%%%%%%%%%%%%%%
% GetTimeOfNextVarHit %
%%%%%%%%%%%%%%%%%%%%%%%
case 4,
sys=mdlGetTimeOfNextVarHit(t,x,u);
%%%%%%%%%%%%%
% Terminate %
%%%%%%%%%%%%%
case 9,
sys=mdlTerminate(t,x,u);
%%%%%%%%%%%%%%%%%%%%
% Unexpected flags %
%%%%%%%%%%%%%%%%%%%%
otherwise
DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end
% end sfuntmpl
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes
%
% call simsizes for a sizes structure, fill it in and convert it to a
% sizes array.
%
% Note that in this example, the values are hard coded. This is not a
% recommended practice as the characteristics of the block are typically
% defined by the S-function parameters.
%
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 6;
sizes.NumOutputs = 6;
sizes.NumInputs = 2;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1; % at least one sample time is needed
sys = simsizes(sizes);
%
% initialize the initial conditions
%
x0 = zeros(6,1);
Cf = -2*62610;
Cr = -2*74076;
Bs = 0.8;
Js = 0.1;
u = 10;
m = 1673;
a = 1.413;
b = 1.53;
Izz = 2750;
i0 = 16;
Kalpha = 2*39;
Ac = [0 1 0 0 0 0
-Kalpha/i0/Js -Bs/Js Kalpha/u/Js Kalpha/u/Js*a 0 0
-Cf/m/i0 0 (Cf+Cr)/u/m (a*Cf-b*Cr)/u/m-u 0 0
-a*Cr/Izz/i0 0 (a*Cf-b*Cr)/u/Izz (a^2*Cf+b^2*Cr)/u/Izz 0 0
0 0 1 0 0 u
0 0 0 1 0 0];
B1c = [0 0
1/Js 0
0 -Cr/m
0 b*Cr/Izz
0 0
0 0];
Cc = eye(6);
Dc = zeros(6, 2);
sys = ss(Ac, B1c, Cc, Dc);
sysd = c2d( sys, 0.01 );
global Ad; Ad = sysd.a;
global Bd; Bd = sysd.b;
%
% str is always an empty matrix
%
str = [];
%
% initialize the array of sample times
%
ts = [0.01 0];
% Specify the block simStateCompliance. The allowed values are:
% 'UnknownSimState', < The default setting; warn and assume DefaultSimState
% 'DefaultSimState', < Same sim state as a built-in block
% 'HasNoSimState', < No sim state
% 'DisallowSimState' < Error out when saving or restoring the model sim state
simStateCompliance = 'UnknownSimState';
% end mdlInitializeSizes
%
%=============================================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
%=============================================================================
%
function sys=mdlDerivatives(t,x,u)
sys = [];
% end mdlDerivatives
%
%=============================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=============================================================================
%
function sys=mdlUpdate(t,x,u)
global Ad;
global Bd;
sys = Ad*x+Bd*u;
% end mdlUpdate
%
%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
%
function sys=mdlOutputs(t,x,u)
sys = eye(6) * x;
% end mdlOutputs
%
%=============================================================================
% mdlGetTimeOfNextVarHit
% Return the time of the next hit for this block. Note that the result is
% absolute time. Note that this function is only used when you specify a
% variable discrete-time sample time [-2 0] in the sample time array in
% mdlInitializeSizes.
%=============================================================================
%
function sys=mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 0.01; % Example, set the next hit to be one second later.
sys = t + sampleTime;
% end mdlGetTimeOfNextVarHit
%
%=============================================================================
% mdlTerminate
% Perform any end of simulation tasks.
%=============================================================================
%
function sys=mdlTerminate(t,x,u)
sys = [];
% end mdlTerminate

回答(1 个)

liang
liang 2020-4-19
Oh, I just found out the reason: some variables such as u and sys were named the same as those predefined by the S-function.

类别

Help CenterFile Exchange 中查找有关 Programmatic Model Editing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by