function AS_motor_5f(block)

setup(block);
  
%endfunction

function setup(block)
  
  % Register number of ports
  block.NumInputPorts  = 2;
  block.NumOutputPorts = 6;

  % Setup port properties to be inherited or dynamic
  block.SetPreCompInpPortInfoToDynamic;
  block.SetPreCompOutPortInfoToDynamic;


  % Register parameters
  block.NumDialogPrms = 8;
  
   % Setup the continous state
  block.NumContStates = 5;

  % Register sample times
  %  [0 offset]            : Continuous sample time
  %  [positive_num offset] : Discrete sample time
  %
  %  [-1, 0]               : Inherited sample time
  %  [-2, 0]               : Variable sample time
  
  block.SampleTimes = [0 0];


  % Register block methods
  block.RegBlockMethod('InitializeConditions',    @InitConditions);
  block.RegBlockMethod('Outputs',                 @Output);  
  block.RegBlockMethod('Derivatives',             @Derivatives);
  block.RegBlockMethod('SetInputPortSamplingMode',@SetInpPortFrameData);
  
%endfunction

function InitConditions(block)

  % poè. hodnota
  
  block.ContStates.Data(1) = 0;
  block.ContStates.Data(2) = 0;
  block.ContStates.Data(3) = 0;
  block.ContStates.Data(4) = 0;
  block.ContStates.Data(5) = 0;
  
%endfunction

function Output(block)
% 

  block.OutputPort(1).Data = block.ContStates.Data(1);
  block.OutputPort(2).Data = block.ContStates.Data(2);
  block.OutputPort(3).Data = block.ContStates.Data(3);
  block.OutputPort(4).Data = block.ContStates.Data(4);
  
  
  
  Lh = block.DialogPrm(3).Data;
  p    = block.DialogPrm(6).Data;
  
  Isalfa = block.ContStates.Data(1);
  Isbeta = block.ContStates.Data(2);
  Iralfa = block.ContStates.Data(3);
  Irbeta = block.ContStates.Data(4);
  
  block.OutputPort(5).Data = 10/4 * p * Lh *(Isbeta*Iralfa-Isalfa*Irbeta);
  
  block.OutputPort(6).Data = block.ContStates.Data(5);
  
%endfunction

function Derivatives(block)
% 
    Ls   = block.DialogPrm(1).Data;
    Lr   = block.DialogPrm(2).Data;
    Lh   = block.DialogPrm(3).Data;
    Rs   = block.DialogPrm(4).Data;
    Rr   = block.DialogPrm(5).Data;
    p    = block.DialogPrm(6).Data;
    Mzat = block.DialogPrm(7).Data;
    J    = block.DialogPrm(8).Data;
    

    
    Usalfa = block.InputPort(1).Data;
    Usbeta = block.InputPort(2).Data;
    
    Isalfa = block.ContStates.Data(1);
    Isbeta = block.ContStates.Data(2);
    Iralfa = block.ContStates.Data(3);
    Irbeta = block.ContStates.Data(4);
    w      = block.ContStates.Data(5);
    
    M = 10/4 * p * Lh * (Isbeta*Iralfa  - Isalfa*Irbeta);
    
    %dIsalfa/dt
    block.Derivatives.Data(1) = (-Iralfa*Lh*Rr+Isalfa*Lr*Rs-Lr*Usalfa-Isbeta*Lh*Lh*w-Irbeta*Lh*Lr*w)/(Lh*Lh-Lr*Ls);
    
    %dIsbeta/dt
    block.Derivatives.Data(2) = (-Irbeta*Lh*Rr+Isbeta*Lr*Rs-Lr*Usbeta+Isalfa*Lh*Lh*w+Iralfa*Lh*Lr*w)/(Lh*Lh-Lr*Ls);
    %block.Derivatives.Data(2) = (Usbeta - Rs*Isbeta - Lh*block.Derivatives.Data(4))/Ls;
    
    %dIralfa/dt
    block.Derivatives.Data(3) = (Iralfa*Ls*Rr-Isalfa*Lh*Rs+Lh*Usalfa+Isbeta*Lh*Ls*w+Irbeta*Lr*Ls*w)/(Lh*Lh-Lr*Ls);
    %block.Derivatives.Data(3) = (-Rr*Iralfa-Lh*block.Derivatives.Data(1)-w*Lr*Irbeta-w*Lh*Isbeta)/Lr;
    
    %dIrbeta/dt
    block.Derivatives.Data(4) = (Irbeta*Ls*Rr-Isbeta*Lh*Rs+Lh*Usbeta-Isalfa*Lh*Ls*w-Iralfa*Lr*Ls*w)/(Lh*Lh-Lr*Ls);
    %block.Derivatives.Data(4) = (-Rr*Irbeta-Lh*block.Derivatives.Data(2)-w*Lr*Iralfa-w*Lh*Isalfa)/Lr;
    
    %dw/dt
    block.Derivatives.Data(5) = ((M-Mzat)*p)/J;






   
%endfunction

function SetInpPortFrameData(block,idx,fd)

block.InputPort(idx).SamplingMode = fd;
for i = 1:block.NumOutputPorts
    block.OutputPort(i).SamplingMode = fd;
end
%endfunction