Hello, I have problem with my s-function
The errors are:Error in 'Hexacopter_1/S-Function' while executing MATLAB S-function 'Trial_2', flag = 3 (output), at time 0.0.Attempted to access x(2); index out of bounds because numel(x)=1.
And this is my s-function:
function [sys,x0,str,ts] = Trial_2(t,x,u,flag,xini,m,l,Ix,Iy,Iz,IR,g,omegaD)% The following outlines the general structure of an S-function.
switch flag, case 0, [sys,x0,str,ts]=mdlInitializeSizes(xini); case 1, sys=mdlDerivatives(t,x,u,m,l,Ix,Iy,Iz,IR,g,omegaD); case 2, sys=mdlUpdate(t,x,u); case 3, sys=mdlOutputs(t,x,u); case 4, sys=mdlGetTimeOfNextVarHit(t,x,u); case 9, sys=mdlTerminate(t,x,u); otherwise error(['Unhandled flag = ',num2str(flag)]);end%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================%function [sys,x0,str,ts]=mdlInitializeSizes(xini)sizes = simsizes;sizes.NumContStates = 12; %x(1) bis x(12)
sizes.NumDiscStates = 0;sizes.NumOutputs = 12; %x,y,z,roll,pitch and yaw
sizes.NumInputs = 4; %u(1),u(2),u(3) und u(4)
sizes.DirFeedthrough = 0;sizes.NumSampleTimes = 1; % at least one sample time is needed
sys = simsizes(sizes);%% initialize the initial conditions
x0 = xini;%% str is always an empty matrix
%str = [];%% initialize the array of sample times
%ts = [0 0];% end mdlInitializeSizes
%%=============================================================================% mdlDerivatives
% Return the derivatives for the continuous states.
%=============================================================================%function sys=mdlDerivatives(t,x,u,m,l,Ix,Iy,Iz,IR,g,omegaD)%____State variables__%
x=x(1); xdot=x(2); y=x(3); ydot=x(4); z=x(5); zdot=x(6); roll=x(7); rolldot=x(8); pitch=x(9); pitchdot=x(10); yaw=x(11); yawdot=x(12); dxdt=[xdot, (cos(roll)*sin(pitch)*cos(yaw)+sin(roll)*sin(yaw))*u(1)/m, ydot, (cos(roll)*sin(pitch)*sin(yaw)-sin(roll)*cos(yaw))*u(1)/m, zdot, -g+((cos(roll))*cos(pitch)*u(1)/m), rolldot, (yawdot*pitchdot*(Iy-Iz)/Ix)-(IR/Ix*pitchdot*omegaD)+(l/Ix*u(2)), pitchdot,(yawdot*rolldot*(Iz-Ix)/Iy)+(IR/Iy*rolldot*omegaD)+(l/Iy*u(3)), yawdot, pitchdot*rolldot*(Ix-Iy)/Iz+(l/Iz*u(4))];sys =dxdt;% end mdlDerivatives
%%=============================================================================% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=============================================================================%function sys=mdlUpdate(t,x,u)sys = [];% end mdlUpdate
%%=============================================================================% mdlOutputs
% Return the block outputs.
%=============================================================================%function sys=mdlOutputs(t,x,u)x=x(1); xdot=x(2); y=x(3); ydot=x(4); z=x(5); zdot=x(6); roll=x(7); rolldot=x(8); pitch=x(9); pitchdot=x(10); yaw=x(11); yawdot=x(12);sys = [x xdot y ydot z zdot roll rolldot pitch pitchdot yaw yawdot];% 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 = 1; % 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
And this is the m-file for my parameters:
m=2.5;l=0.5;Ix=0.17;Iy=0.17;Iz=0.3;IR=1;b=1;d=1;omegaD=1;g=9.8;xini=[0 0 0 0 0 0 0.1745 0 0.1745 0 0.1745 0];
I have programmed with s-function twice for pendulum model and Gnatry crane model but i have no problem with it. Could you please help me with my problem. TQ
Best Answer