init_model.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. % Clear workspace
  2. close all
  3. clear
  4. clc
  5. % Load model parameters
  6. Ts = 1e-6; % [s] Model sampling time (200 KHz)
  7. f_ctrl = 20e3; % [Hz] Controller frequency = 1/Ts_ctrl (20 kHz)
  8. Ts_ctrl = 1/f_ctrl; % [s] Controller sampling time (50us)5e-5
  9. f_speed_ctrl = 5e2; % [Hz] Speed/torque Controller frequency = (1 kHz)
  10. speed_ctrl = f_ctrl/f_speed_ctrl; % [count] Delay for f_speed_ctrl of the FOC controller
  11. i_pwm_count = 3000;
  12. i_Udc = 300;
  13. i_half_pwm_count = i_pwm_count;
  14. n_hall_count_ps = 1/Ts; % counts of per second
  15. %Current sample hw parameters
  16. n_adc_max = 4096;
  17. n_resistance = 0.0005;
  18. n_ref_vol = 3.3;
  19. n_gain = 17.1;
  20. f_lpf_coeff = 0.4; %Phase Current LowPass filter
  21. % Motor parameters
  22. n_polePairs = 4; % [-] Number of motor pole pairs
  23. PM = 0.1688; %0.03; % Permanent magnet flux linkage,
  24. Ld = 0.9262e-3;%2e-4; % d-axis inductance,
  25. Lq = 1.024e-3; %2e-4; % q-axis inductance,
  26. Rs = 0.0918; %0.013; % Stator resistance,
  27. J = 0.03945; %0.2; % Moment of inertia,
  28. %Hall parameters
  29. n_max = 8000; % [rpm] Maximum motor speed: [-5000, 5000]
  30. a_elecPeriod = 360; % [deg] Electrical angle period
  31. a_elecDeltaAngle = 60; % [deg] Electrical angle between two Hall sensor changing events
  32. a_mechAngle = a_elecDeltaAngle / n_polePairs; % [deg] Mechanical angle between two Hall sensor changing events
  33. vec_hallToPos = [7 5 1 0 3 4 2 7]; % [-] Mapping Hall signal to position
  34. i_hall_offset = -30; %60;%-30;
  35. b_angleMeasEna = 0;
  36. % Sine/Cosine wave look-up table
  37. res_elecAngle = 1;
  38. a_elecAngle_XA = 0:res_elecAngle:360; % [deg] Electrical angle grid
  39. r_sin_M1 = sin((a_elecAngle_XA)*(pi/180));
  40. r_cos_M1 = cos((a_elecAngle_XA)*(pi/180));
  41. %% F02_Diagnostics
  42. t_errQual = 0.24 * f_ctrl/3; % [s] Error qualification time
  43. t_errDequal = 1.80 * f_ctrl/3; % [s] Error dequalification time
  44. r_errInpTgtThres = 310; % [-] Error input target threshold (for "Blocked motor" detection)
  45. % Speed Calculation Parameters
  46. cf_speedCoef = (n_hall_count_ps * a_mechAngle * (pi/180) * (30/pi)); % [-] Speed calculation coefficient (factors are due to conversions rpm <-> rad/s)
  47. z_maxStillSecond = 2; %(second, also as time-out to detect standing still)
  48. n_commDeacvHi = 30; % [rpm] Commutation method deactivation speed high
  49. n_commAcvLo = 15; % [rpm] Commutation method activation speed low
  50. dz_cntTrnsDetHi = 140; % [-] Counter gradient High for transient behavior detection (used for speed estimation)
  51. dz_cntTrnsDetLo = 100; % [-] Counter gradient Low for steady state detection (used for speed estimation)
  52. n_stdStillDet = 3; % [rpm] Speed threshold for Stand still detection
  53. n_SpeedModeLo = 200; % min speed for exit speed ctrl mode
  54. n_SpeedModeHi = 300; % when speed is Hi can into speed ctrl mode
  55. % Control model request
  56. OPEN_MODE = 0; % [-] Open mode
  57. SPD_MODE = 1; % [-] Speed mode
  58. TRQ_MODE = 2; % [-] Torque mode
  59. z_ctrlModReq = TRQ_MODE; % [-] Control Mode Request (default)
  60. % MTPA process
  61. b_MTPA_Enable = 0; % MTPA enable flag: 0 = disable, 1 = enable
  62. cf_MTPA_ldqdiff = PM/(Lq - Ld);
  63. % Field_Weakening
  64. b_fieldWeakEna = 0; % [-] Field weakening enable flag: 0 = disable (default), 1 = enable
  65. cf_Fw_Ki = 2;
  66. cf_Fw_Kb = 0.1;
  67. id_fieldWeakMax = -50; % [A] Field weakening maximum current
  68. % Voltage Limitations
  69. V_modulation = 0.95; % [-] Voltage margin to make sure that there is a sufficiently wide pulse for a good phase current measurement
  70. Vq_max = i_Udc * V_modulation;
  71. Vd_max = Vq_max;
  72. Vq_max_XA = 0:1:Vd_max;
  73. %Vq_max_XA = fixpt_evenspace_cleanup(Vq_max_XA, ufix(16), 2^-5);
  74. Vq_max_M1 = sqrt(Vd_max^2 - Vq_max_XA.^2); % Circle limitations look-up table
  75. % Current Limitations
  76. i_dqMax = 200; % [A] Maximum allowed motor current (continuous)
  77. % Current loop FeedForward
  78. b_FF_Enable = 0;
  79. % D axis control gains
  80. %https://www.it610.com/article/1282852898983657472.htm
  81. %https://e2echina.ti.com/support/microcontrollers/c2000/f/c2000-microcontrollers-forum/109265/lab05a-fast-pid
  82. cf_iGain = 1;
  83. cf_iBand = 2 * pi * f_ctrl / 20 * 1.7;
  84. cf_idKp = Ld * cf_iBand; %/ (1 * Ts_ctrl); % [-] P gain
  85. cf_idKi = Rs/(Ld * f_ctrl) * cf_iGain;%%%0.05; % [-] I gain
  86. cf_idKb = 0.1;
  87. % Q axis control gains
  88. cf_iqKp = Lq * cf_iBand; %/ (1.5 * Ts_ctrl); % [-] P gain
  89. cf_iqKi = Rs/(Lq * f_ctrl) * cf_iGain; % [-] I gain
  90. cf_iqKb = 0.1;
  91. % Speed control gains
  92. cf_nKp = 0.2; % [-] P gain
  93. cf_nKi = 200 * Ts_ctrl; % [-] I gain
  94. cf_nKb = cf_nKi;
  95. cf_lastIqGain = 0.5; % when swtich toqure to speed mode, use the last iq as the speed PI init value
  96. % Torque iq limit
  97. cf_TrqLimKp = 0.3; % [-] P gain
  98. cf_TrqLimKi = 0.02; % [-] I gain
  99. cf_TrqLimKb = 0.1;