%%
%------HOBM S BLDC MOTOREM------%
%

%%
%pozn.:

    %---rovnice BLDC motoru--%
    %{
       %Napěťová rovnice
           u(t) = 2*R_a*i(t) + 2*L_a*(di(t)/dt) + u_i(t)
           u(t) = 2*R_a*i(t) + 2*L_a*(di(t)/dt) + C_fi*w(t)
       %Momentová rovnice
           M(t) = J(dw(t)/dt) + B*w(t) + M_0
           M(t) = C_fi*i(t)
    %}
    
%%
%DATASHEETS AND OTHERS
    %whole set
        %https://www.maxongroup.com/maxon/view/service_search?query=248999
        %part number: 248999
    
      
    %----Motor info---- 
        %https://www.maxongroup.com/maxon/view/product/motor/ecmotor/ec/ec45/136196?etcc_cu=onsite&etcc_med=Header%20Suche&etcc_cmp=mit%20Ergebnis&etcc_ctv=Layer&query=EC%20150
        %part number: 136198
        
        %Nominal voltage:                           24      V
        %No load speed:                             6090    rpm
        %No load current:                           328     mA
        %Nominal speed:                             4840    rpm
        %Nominal torque (max. countinuous torque):  183     mNm
        %Nominal current (max. continuous current): 5.13    A
        %Stall torque:                              952     mNm
        %Stall current:                             25.6    A
        %Max. efficiency:                           79      %
        
        %Characteristics:
            %Terminal resistance phase to phase:    0.936   ohm
            %Terminal inductance pahse to phase:    0.275   mH
            %Torque constant:                       37.1    mNm/A
            %Speed constant:                        257     rpm/V
            %Speed/torque gradient:                 6.49    rpm/mNm
            %Mechanical time constant:              8.08    ms
            %Rotor inertia:                         119     gcm^2
        
    %----Encoder----
        %https://www.maxongroup.com/maxon/view/product/137959?fbclid=IwAR2HMVj-urgFbWpDbkQUuKsUrMYakHKm16gF2wuRqy7sBowdOrZ4Rha4N8w
        %part number: 137959
        
        %counts per turn: 500
        %quadratic per turn: 4*500 = 2000
        
    %----ESCON 50/5----
        %https://www.maxongroup.com/medias/sys_master/root/8834332262430/409510-ESCON-50-5-Hardware-Reference-En.pdf
%%
clear all; clc;

%%
%BASIC VARIABLES
    m = 1;                          %[kg]
    dead_zone = 0.2;                %[kg]
    start_pos = 0;                  %[mm]
    g = 9.81;                       %[m/s^2]
    
%%
%WINCH VARIABLES
    J_w = 0;                        %[kg*m^2]
    r_w = 5.1;                      %[mm]
    
%%
%MOTOR CONSTANTS
    Speed_const = 274.2;            %[rpm/V]
    U_max = 24;                     %[V]
    C_fi = 1/(Speed_const*2*pi/60); %[N*m/A] (pozn. buď z datasheet-u nebo přímo změžené z Escon studia)
    R_a = 0.936/2;                  %[ohm]
    L_a = (0.275e-3)/2;             %[H]
    T_el = L_a/R_a;                 %[m+s]
    T_mech = 8.08;                  %[ms]
    J_m = 119e-7;                   %[kg*m^2]
    %T_mech = J/B => B = J/T_mech
    B_m = J_m/(T_mech/1000);        %[N*m*s]
    M_stall = 952e-3;               %[N*m]
    I_stall = 25.6;                 %[A]
    I_max = I_stall;                %[A]
    M_max = M_stall;                %[Nm]
    
%%
%GEAR BOX
    reduct = 1;                     %[-]
    B_gb = 0;                       %[N*m*s]
    J_gb = 0;                       %[kg*m^2]

%%
%MAX MASS
    sv_coef = 0.25;
    m_Ms_max = sv_coef*M_stall/(g*r_w/1000);
    m_Is_max = sv_coef*I_stall*C_fi/(g*r_w/1000);
    
    m_max = 0;
    if(m_Ms_max > m_Is_max)
        m_max = m_Is_max;
    else
        m_max = m_Ms_max;
    end
    
    w_Ms_max = (M_stall - (m*g*r_w/1000))/(B_m + B_gb);
    w_Is_max = (I_stall*C_fi - (m*g*r_w/1000))/(B_m + B_gb);
    
    w_max = 0;
    if(w_Ms_max >= w_Is_max)
        w_max = w_Is_max;
    else
        w_max = w_Ms_max;
    end   
    n_max = w_max*60/(2*pi);   
    v_max = w_max*r_w/1000;
    
    fprintf('\t\t\t----Aktuální hodnoty----\n');
    fprintf('Hmotnost závaží na navijáku:\t\t\t%f\t[kg]\n', m);
    fprintf('Dead zone:\t\t\t\t\t\t\t\t%f\t[kg]\n', dead_zone);
    fprintf('Poloměr navijáku:\t\t\t\t\t\t%f\t[mm]\n', r_w);
    fprintf('Převodový poměr:\t\t\t\t\t\t%f\t[-]\n', reduct);
    
    fprintf('\n\t\t\t----Maximální hodnoty----\n');
    fprintf('Maximální napětí:\t\t\t%f\t[V]\n', U_max);
    fprintf('Maximální proud:\t\t\t%f\t[A]\n', I_max);
    fprintf('Maximální moment:\t\t\t%f\t[Nm]\n', M_max);
    fprintf('Maximální hmotnost:\t\t\t%f\t[kg]', m_max);
    fprintf('\nMaximální úhlová rychlost:\t%f\t[rad/s]', w_max);
    fprintf('\nMaximální rychlost:\t\t\t%f\t[ot/min]', n_max);
    fprintf('\nMaximální rychlost:\t\t\t%f\t[m/s]\n\n', v_max);
    
%%
%OPERATIONS

    %SIMULATION
        %u(t) = 2*R_a*i(t) + 2*L_a*(di(t)/dt) + C_fi*w(t)
        %M(t) = J(dw(t)/dt) + B*w + M_0
        %M(t) = C_fi*i(t)
        
        %u(t) = 2*R_a*i(t) + 2*L_a*(di(t)/dt) + C_fi*w(t)
        %M(t) = (J_m + J_rb)(dw(t)/dt) + (B_m + B_gb)*w + (m*g*r/1000)
        %M(t) = C_fi*i(t)
        
    %MASS MEASURING
    
        %STATIC MEASURING => (w = 0, pozn. problém se suchym třením)
            %M(t) = C_fi*i(t)
            %M(t) = M_0 = m*g*r/1000
            %m = C_fi*i(t)*1000/(g/r)
        
        %DYNAMIC MEASURING => (w = 0, pozn. problém se suchym třením)
            %M(t) = J(dw(t)/dt) + B*w + M_0
            %M(t) = C_fi*i(t)
            %C_fi*i(t) - J(dw(t)/dt) - B*w = m*g*r/1000
            %m = (C_fi*i(t) - J(dw(t)/dt) - B*w)*1000/(g*r)