control/Control_Vectorial/simulado/Static_friction_torque.m
2025-09-02 13:01:03 +02:00

96 lines
2.4 KiB
Matlab

function [sys,x0,str,ts] = Static_friction_torque(t,x,u,flag)
%
% Dispatch the flag. The switch function controls the calls to
% S-function routines at each simulation stage of the S-function.
%
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
% Initialize the states, sample times, and state ordering strings.
case 0
[sys,x0,str,ts]=mdlInitializeSizes;
%%%%%%%%%%%
% Outputs %
%%%%%%%%%%%
% Return the outputs of the S-function block.
case 3
sys=mdlOutputs(t,x,u);
%%%%%%%%%%%%%%%%%%%
% Unhandled flags %
%%%%%%%%%%%%%%%%%%%
% There are no termination tasks (flag=9) to be handled.
% Also, there are no continuous or discrete states,
% so flags 1,2, and 4 are not used, so return an emptyu
% matrix
case { 1, 2, 4, 9 }
sys=[];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Unexpected flags (error handling)%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Return an error message for unhandled flag values.
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
% end timestwo
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts] = mdlInitializeSizes()
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 1; % dynamically sized
sizes.NumInputs = 3; % 1=signo veloc; 2=Par electromagnético; 3=P rozamiento
sizes.DirFeedthrough = 1; % has direct feedthrough
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
str = [];
x0 = [];
ts = [-1 0]; % inherited sample time
% end mdlInitializeSizes
%
%=============================================================================
% mdlOutputs
% Return the output vector for the S-function
%=============================================================================
%
function sys = mdlOutputs(t,x,u)
sig_v=sign(u(1)); % Signo de la velocidad
P_elm=u(2); % Par electromagnético
P_roz=u(3); % Par de rozamiento estático
if sig_v>0 % Si ¡Par_elect.¡ > Par_roz
sys = P_elm-P_roz;
elseif sig_v<0 % Si ¡Par_elect.¡ > Par_roz
sys = P_elm+P_roz;
else % Si ¡Par_elect.¡ > Par_roz
if abs(P_elm)>P_roz
sys = P_elm-sign(P_elm)*P_roz;
else
sys = 0;
end
end
% end mdlOutputs