function [sys,x0,str,ts] = bldc(t,x,u,flag,R,L,J,P,kw)
% BLDC An example M-file S-function for defining a system of

% The expected input vector is:
% (1):  Wr
%(2):   theta



%OUTPUT VECTOR GENERATED BY THE SYSTEM:
   
%(1):  Emf_u
%(2):  Emf_v
%(3):  Emf_w
%(4):  Ha
%(5):  Hb
%(6):  Hc
%(7):  theta




% Dispatch the flag.

switch flag,
  case 0,
    [sys,x0,str,ts]=mdlInitializeSizes(R,L,J,P,kw); % Initialization
  case 1,
    sys = mdlDerivatives(t,x,u,R,L,J,P,kw); % Calculate derivatives
   case 2,                                                                                                    %model update
   sys = mdlUpdate(t,x,u,R,L,J,P,kw); % update the model
    case 3,
    sys = mdlOutputs(t,x,u,R,L,J,P,kw); % Calculate outputs
    case 4, % Unused flags
    sys = [];
    case 'reset'
       % name = 'mybldc_mdl2/mybldc/my state-space';
    %LocalResetController(name);
    case 9,
   % sys=mdlTerminate(t,x,u);
    

  %%%%%%%%%%%%%%%%%%%%
  % Unexpected flags %
  %%%%%%%%%%%%%%%%%%%%
  otherwise
    error(['Unhandled flag = ',num2str(flag)]);

end

% End of csfunc.
%==============================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the 
% S-function.
%==============================================================
%
function [sys,x0,str,ts] = mdlInitializeSizes(R,L,J,P,kw)
%
% Call simsizes for a sizes structure, fill it in and convert it 
% to a sizes array.
%


sizes = simsizes;
sizes.NumContStates  = 0;
sizes.NumDiscStates  = 0;
sizes.NumOutputs     = 7;
sizes.NumInputs      = 2;
sizes.DirFeedthrough = 1;     % Matrix D is nonempty. 
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
%
% Initialize the initial conditions.
%
x0 = [];
%
% str is an empty matrix.
%
str = [];
%
% Initialize the array of sample times; in this example the sample 
% time is continuous, so set ts to 0 and its offset to 0.
%
ts = [0 0];
% End of mdlInitializeSizes.
%


%==============================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
% Nothing needs to be done here.
%==============================================================
function sys = mdlDerivatives(t,x,u,R,L,J,P,kw)
sys=[];




%==============================================================
% mdlUpdate
% Update the model parameters.
% Nothing needs to be done here
%==============================================================
 function sys = mdlUpdate(t,x,u,R,L,J,P,kw); % update the model
 sys=[];

 
%==============================================================
% mdlOutput
% calculate the model outputs and update other blocks ( Adaptive coefficients)
%==============================================================
function sys = mdlOutputs(t,x,u,R,L,J,P,kw)
%u(2)=u(2)+40;    
theta = rem (u(2),2*pi);                % make theta betweeen 2*pi and -2*pi
sys(7)=theta;
%sys(11)=theta;                               %output normalised angular position

if  (theta>=0 & theta<pi/6)        % 0 TO  30 DEGREE
    LA= 6*theta/pi;
    LB=-1;
    LC=1;
    
    sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)=LB*kw*u(1);      %Back emf for phase V
    sys(3)=LC*kw*u(1);      %Back emf for phase W
   %-------------------------------------------------12  
    sys(4)= 5;                       %HALL SENSOR A
    sys(5)=0;                        %HALL SENSOR B
    sys(6)=0;                        %HALL SENSOR C
    
end;

    
if (theta>=pi/6 & theta<pi/3)        % 30 TO  60 DEGREE
    LA=1;
    LB=-1;
    LC=-6*(theta-(2*pi/6))/pi;
    
    sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)=LB*kw*u(1);      %Back emf for phase V
    sys(3)=LC*kw*u(1); %Back emf for phase W
    
    sys(4)= 5;                       %HALL SENSOR A
    sys(5)=0;                        %HALL SENSOR B
    sys(6)=0;                        %HALL SENSOR C      
    
    
   
    
end;

if (theta>=pi/3 & theta<pi/2)       % 60 TO  90 DEGREE
    LA=1;
    LB=-1;
    LC=-6*(theta-(2*pi/6))/pi;
    sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)=LB*kw*u(1);      %Back emf for phase V
    sys(3)=LC*kw*u(1); %Back emf for phase W
    
    sys(4)= 5;                        %HALL SENSOR A
    sys(5)=5;                        %HALL SENSOR B
    sys(6)=0;                        %HALL SENSOR C      
    

 %---------------------------------------------------- 10   
    
    
    
end;

if (theta>=pi/2 & theta<2*pi/3)      % 90 TO  120 DEGREE
     LA=1;
    LB=(  theta-(4*pi/6) )*6/pi;
    LC=-1;
    sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)=LB*kw*u(1);      %Back emf for phase V
    sys(3)=LC*kw*u(1); %Back emf for phase W
    
    sys(4)= 5;                        %HALL SENSOR A
    sys(5)=5;                        %HALL SENSOR B
    sys(6)=0;                        %HALL SENSOR C     
    

    %--------------------------------------------------- 9  

end;

if(theta>=2*pi/3 & theta<5*pi/6)    % 120 TO  150 DEGREE
   LA=1;
    LB=(  theta-(2*pi/3) )*6/pi;
    LC=-1;
    sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)=LB*kw*u(1);      %Back emf for phase V
    sys(3)=LC*kw*u(1);      %Back emf for phase W
    
    sys(4)= 0;                       %HALL SENSOR A
    sys(5)=5;                        %HALL SENSOR B
    sys(6)=0;                        %HALL SENSOR C     

    %------------------------------------------------- 8
    
    
    
end;

if (theta>=5*pi/6 & theta<pi)     % 150 TO  180 DEGREE
    LA=(pi-theta)*6/pi;
    LB=1;
    LC=-1;
    sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)=LB*kw*u(1);      %Back emf for phase V
    sys(3)=LC*kw*u(1); %Back emf for phase W
    
    sys(4)=0;                        %HALL SENSOR A
    sys(5)=5;                        %HALL SENSOR B
    sys(6)=0;                        %HALL SENSOR C 
    %----------------------------------------------- 7
    
    
end;

if(theta>=pi & theta<7*pi/6)    % 180 TO  210 DEGREE
     LA=(pi-theta)*6/pi; 
    LB=1;
    LC=-1;
    sys(1)= LA*kw*u(1);      % Back emf for phase U
    sys(2)=LB*kw*u(1);       %Back emf for phase V
    sys(3)=LC*kw*u(1);       %Back emf for phase W
    
    sys(4)= 0;                       %HALL SENSOR A
    sys(5)=5;                        %HALL SENSOR B
    sys(6)=5;                        %HALL SENSOR C 
    %----------------------------------------------6
    
    
end;

if(theta>=7*pi/6 & theta<4*pi/3)  % 210 TO  240 DEGREE
    LA= -1;
    LB=1;
    LC=( theta-(4*pi/3))*6/pi;
    sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)=LB*kw*u(1);      %Back emf for phase V
    sys(3)=LC*kw*u(1); %Back emf for phase W
    
    sys(4)= 0;                        %HALL SENSOR A
    sys(5)=5;                        %HALL SENSOR B
    sys(6)=5;                        %HALL SENSOR C  
    %-----------------------------------------------5
    
    
end;

if (theta>=4*pi/3 & theta<3*pi/2)   % 240 TO  270 DEGREE
   LA= -1;
   LB=1;
   LC=( theta -(4*pi/3))*6/pi; 
    sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)=LB*kw*u(1);      %Back emf for phase V
    sys(3)=LC*kw*u(1); %Back emf for phase W
    
    sys(4)= 0;                        %HALL SENSOR A
    sys(5)=0;                        %HALL SENSOR B
    sys(6)=5;                        %HALL SENSOR C 
    %-----------------------------------------------4
     
    
end;

if(theta>=3*pi/2 & theta<5*pi/3)   % 270 TO  300 DEGREE
    LA= -1;
    LB=((5*pi/3)-theta)*6/pi;
    LC=1;
    sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)=LB*kw*u(1);      %Back emf for phase V
    sys(3)=LC*kw*u(1); %Back emf for phase W
    
    sys(4)= 0;                       %HALL SENSOR A
    sys(5)=0;                        %HALL SENSOR B
    sys(6)=5;                        %HALL SENSOR C 
    %-----------------------------------------------3
   
    
end;

if(theta>=5*pi/3 & theta<11*pi/6)  % 300 TO  330 DEGREE
     LA= -1;
    LB=((5*pi/3)-theta)*6/pi;
    LC=1;    
    sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)=LB*kw*u(1);      %Back emf for phase V
    sys(3)=LC*kw*u(1);      %Back emf for phase W

    
    sys(4)= 5;                        %HALL SENSOR A
    sys(5)=0;                        %HALL SENSOR B
    sys(6)=5;                        %HALL SENSOR C     
    %--------------------------------------------------
  
    
end;

if(theta>=11*pi/6 & theta<2*pi)   % 330 TO  360 DEGREE
    LA= 6*(theta-2*pi)/pi;
    LB=-1;
    LC=1;
    sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)= LB*kw*u(1);     %Back emf for phase V
    sys(3)= LC*kw*u(1);     %Back emf for phase W
    
    sys(4)= 5;                        %HALL SENSOR A
    sys(5)=0;                        %HALL SENSOR B
    sys(6)=5;                        %HALL SENSOR C 
    %-----------------------------------------------
   
end;
%%%-------------CALCULATION FOR%%%SYS(4)<0---------------------------------------------------tt
if  (theta>=-2*pi  & theta<-11*pi/6) %-360 to -330
    LA= (theta+2*pi)*6/pi;
    LB=-1;
    LC=1;
    
    sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)=LB*kw*u(1);      %Back emf for phase V
    sys(3)=LC*kw*u(1);      %Back emf for phase W
    
    
    sys(4)= 5;                       %HALL SENSOR A
    sys(5)= 0;                       %HALL SENSOR B
    sys(6)= 5;                       %HALL SENSOR C 
    %----------------------------------------------------
  
    
end;

    
if (theta>=-11*pi/6 & theta<-5*pi/3)%-330 to -300
     LA=1;
    LB=-1;
    LC=-(theta+(10*pi/6))*6/pi;
    sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)=LB*kw*u(1);      %Back emf for phase V
    sys(3)=LC*kw*u(1);      %Back emf for phase W
    
    sys(4)= 5;                       %HALL SENSOR A
    sys(5)= 0;                        %HALL SENSOR B
    sys(6)= 5;                        %HALL SENSOR C 
    %-------------------------------------------------

    
end;

if (theta>=-5*pi/3 & theta<-3*pi/2)%-300 to -270
    LA=1;
    LB=-1;
    LC=-(theta+(10*pi/6))*6/pi;
   sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)=LB*kw*u(1);      %Back emf for phase V
    sys(3)=LC*kw*u(1); %Back emf for phase W
    
    sys(4)= 0;                        %HALL SENSOR A
    sys(5)=0;                        %HALL SENSOR B
    sys(6)=5;                        %HALL SENSOR C
    %----------------------------------------------

    
end;

if (theta>=-3*pi/2 & theta<-4*pi/3)%-270 to -240
   LA=1;
    LB=(  theta+(4*pi/3) )*6/pi;
    LC=-1;
    sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)=LB*kw*u(1);      %Back emf for phase V
    sys(3)=LC*kw*u(1); %Back emf for phase W
    
    sys(4)= 0;                        %HALL SENSOR A
    sys(5)=0;                        %HALL SENSOR B
    sys(6)=5;                        %HALL SENSOR C 
    %------------------------------------------------4
 
    
end;

if(theta>=-4*pi/3 & theta<-7*pi/6)%-240 to -210
    LA=1;
    LB=(  theta+(4*pi/3) )*6/pi;
    LC=-1;
 
    sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)=LB*kw*u(1);      %Back emf for phase V
    sys(3)=LC*kw*u(1);      %Back emf for phase W
    

    %-----------------------------------------------5
    sys(4)= 0;                        %HALL SENSOR A
    sys(5)=5;                        %HALL SENSOR B
    sys(6)=5;                        %HALL SENSOR C 
    
end;

if (theta>=-7*pi/6 & theta<-pi)%-210 to -180
      LA=-(theta+pi)*6/pi;
    LB=1;
    LC=-1;
   sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)=LB*kw*u(1);      %Back emf for phase V
    sys(3)=LC*kw*u(1); %Back emf for phase W
    
    sys(4)= 0;                        %HALL SENSOR A
    sys(5)=5;                        %HALL SENSOR B
    sys(6)=5;                        %HALL SENSOR C
    %----------------------------------------------6
 
    
    
end;

if(theta>=-pi & theta<-5*pi/6)%-180 to -150
     LA=-(theta+pi)*6/pi;
    LB=1;
    LC=-1;
    sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)=LB*kw*u(1);      %Back emf for phase V
    sys(3)=LC*kw*u(1); %Back emf for phase W
    

    %----------------------------------------------- 7
    sys(4)= 0;                        %HALL SENSOR A
    sys(5)=5;                        %HALL SENSOR B
    sys(6)=0;                        %HALL SENSOR C     
    
end;

if(theta>=-5*pi/6 & theta<-4*pi/6)%-150 to -120
    LA= -1;
    LB=1;
    LC=( theta+(4*pi/6))*6/pi;
    sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)=LB*kw*u(1);      %Back emf for phase V
    sys(3)=LC*kw*u(1); %Back emf for phase W
    
    sys(4)= 0;                        %HALL SENSOR A
    sys(5)=5;                        %HALL SENSOR B
    sys(6)=0;                        %HALL SENSOR C
    %------------------------------------------------- 8
     
    
end;

if (theta>=-4*pi/6 & theta<-3*pi/6)%-120 to -90
   LA= -1;
   LB=1;
   LC=( theta+(4*pi/6))*6/pi;
   sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)=LB*kw*u(1);      %Back emf for phase V
    sys(3)=LC*kw*u(1);     %Back emf for phase W
    
    sys(4)= 5;                        %HALL SENSOR A
    sys(5)=5;                        %HALL SENSOR B
    sys(6)=0;                        %HALL SENSOR C 
    %--------------------------------------------------- 9  
    
    
end;

if(theta>=-3*pi/6 & theta<-2*pi/6)%-90 to -60
    LA= -1;
    LB=-(theta+(2*pi/6))*6/pi;
    LC=1;
    sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)= LB*kw*u(1);     %Back emf for phase V
    sys(3)= LC*kw*u(1);     %Back emf for phase W
    
    sys(4)= 5;                        %HALL SENSOR A
    sys(5)=5;                        %HALL SENSOR B
    sys(6)=0;                        %HALL SENSOR C
 %---------------------------------------------------- 10   
     
    
end;

if(theta>=-2*pi/6 & theta<-pi/6)%-60 to -30
     LA= -1;
    LB=-(theta+(2*pi/6))*6/pi;
    LC=1;      
    sys(1)= LA*kw*u(1);     % Back emf for phase U
    sys(2)=LB*kw*u(1);      %Back emf for phase V
    sys(3)=LC*kw*u(1); %Back emf for phase W
    
    sys(4)= 5;                        %HALL SENSOR A
    sys(5)=0;                        %HALL SENSOR B
    sys(6)=0;                        %HALL SENSOR C
    %----------------------------------------------  11 
   
    
end;

if(theta>=-pi/6 & theta<0)%-30 to 0
    LA= 6*theta/pi;
    LB=-1;
    LC=1;
   sys(1)= LA*kw*u(1);     % Back emf for phase U
   sys(2)=LB*kw*u(1);      %Back emf for phase V
   sys(3)=LC*kw*u(1); %Back emf for phase W
    
    sys(4)= 5;                        %HALL SENSOR A
    sys(5)=0;                        %HALL SENSOR B
    sys(6)=0;                        %HALL SENSOR C 
    %--------------------------------------------------12
  
    
end;

                                            %output the friction generated

  


