function [sys,x0,str,ts] = timestwo(t,x,u,flag,vmax)
% Dispatch the flag. The switch function controls the calls to
% S-function routines at each simulation stage.

%INPUT VECTOR

%u(1) Ha
%u(2) Hb
%u(3) Hc
%u(4) Vbatt

% output vector

%sys(1) Va
%sys(2) Vb
%sys(3) Vc



switch flag
case 0
[sys,x0,str,ts] = mdlInitializeSizes(t,x,u,flag,vmax); % Initialization

    case 1,
     sys = mdlderivatives(t,x,u,flag,vmax);   
case 3
sys = mdlOutputs(t,x,u,vmax); % Calculate outputs
case { 1, 2, 4, 9 }
sys = []; % Unused flags
otherwise
error(['Unhandled flag = ',num2str(flag)]); % Error handling
end;
% End of function timestwo.
%==============================================================
% Function mdlInitializeSizes initializes the states, sample
% times, state ordering strings (str), and sizes structure.
%==============================================================
function [sys,x0,str,ts] = mdlInitializeSizes(t,x,u,flag,vmax)
% Call function simsizes to create the sizes structure.
sizes = simsizes;
% Load the sizes structure with the initialization information.
sizes.NumContStates= 0;
sizes.NumDiscStates= 0;
sizes.NumOutputs= 3;
sizes.NumInputs= 4;
sizes.DirFeedthrough=1;
sizes.NumSampleTimes=1;
% Load the sys vector with the sizes information.
sys = simsizes(sizes);
%
x0 = []; % No continuous states
%
str = []; % No state ordering
%
ts = [-1 0]; % Inherited sample time

% End of mdlInitializeSizes.
%==============================================================
% Function mdlOutputs performs the calculations.
%==============================================================
function sys = mdlOutputs(t,x,u,vmax)
%a=2;b=3;c=4;
ha=u(1);% = a*u(1);
hb=u(2);%=b*u(2);
hc=u(3);%=c*u(3);
vb=u(4);
if(vb > vmax)
 vb=vmax;
end 
%sys = rccvmaxt(t,x,u);
% End of mdlOutputs.

if(ha>0 & hb==0 & hc==0)%100
   
 if(vb > vmax)
 vb=vmax;
 end     
 sys(1)=vb;   
 sys(2)=-vb; 
 sys(3)=0;
end
if(ha>0 & hb>0 & hc==0)%110
 
if(vb > vmax)
 vb=vmax;
end     
 sys(1)=vb;   
 sys(2)=0; 
 sys(3)=-vb;   
    
end
if(ha==0 & hb>0 & hc==0)%010
 if(vb > vmax)
 vb=vmax;
end 
 sys(1)=0;   
 sys(2)=vb; 
 sys(3)=-vb;   
    
end
if(ha==0 & hb>0 & hc>0)%011
 
if(vb > vmax)
 vb=vmax;
end     
 sys(1)=-vb;   
 sys(2)=vb; 
 sys(3)=0;   
    
end

if(ha==0 & hb==0 & hc>0)%001
 
  if(vb > vmax)
  vb=vmax;
  end    
  sys(1)=-vb;   
 sys(2)=0; 
 sys(3)=vb;   
    
end
if(ha>0 & hb==0 & hc>0)%101
 
 if(vb > vmax)
 vb=vmax;
end    
 sys(1)=0;   
 sys(2)=-vb; 
 sys(3)=vb;   
    
end

