init_model.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. % This file is part of the hoverboard-new-firmware-hack-FOC project
  3. % Compared to previouse commutation method, this project implements
  4. % FOC (Field Oriented Control) for BLDC motors with Hall sensors.
  5. % The new control methods offers superior performanace
  6. % compared to previous method featuring:
  7. % >> reduced noise and vibrations
  8. % >> smooth torque output
  9. % >> improved motor efficiency -> lower energy consumption
  10. %
  11. % Author: Emanuel FERU
  12. % Copyright � 2019-2021 Emanuel FERU <aerdronix@gmail.com>
  13. %
  14. % This program is free software: you can redistribute it and/or modify
  15. % it under the terms of the GNU General Public License as published by
  16. % the Free Software Foundation, either version 3 of the License, or
  17. % (at your option) any later version.
  18. %
  19. % This program is distributed in the hope that it will be useful,
  20. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. % GNU General Public License for more details.
  23. %
  24. % You should have received a copy of the GNU General Public License
  25. % along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  27. % Clear workspace
  28. close all
  29. clear
  30. clc
  31. % Load model parameters
  32. Ts = 5e-6; % [s] Model sampling time (1 MHz)
  33. f_ctrl = 20e3; % [Hz] Controller frequency = 1/Ts_ctrl (16 kHz)
  34. Ts_ctrl = 1/f_ctrl; % [s] Controller sampling time (~16 kHz)
  35. speed_ctrl = 20; %delay for f_ctrl
  36. i_pwm_count = f_ctrl/4;
  37. i_dead = 10;
  38. i_adc = 10;
  39. i_sample_before = 10;
  40. i_Udc = 48;
  41. i_half_pwm_count = i_pwm_count;
  42. i_sample_min = (i_dead + i_adc + i_sample_before);
  43. i_hall_count_max = 1/Ts;
  44. % Motor Paramaters
  45. Rs = 0.0918;
  46. Ld = 0.0009262;
  47. Lq = 0.001024;
  48. band_current = 3.14 * 2 * 4000;
  49. % Kp = 0.0009262 * 3.14 * 8000 = 26
  50. % Ki = 0.0918/0.0009262 = 99.11
  51. % Kserial = 99.11 / 0.5816 = 170.42
  52. % Current sample parameters
  53. n_adc_max = 4096;
  54. n_resistance = 0.0005;
  55. n_ref_vol = 3.3;
  56. n_gain = 17.1;
  57. %VBUS sample parameters
  58. b_start_with_commutation = 0;
  59. % Sine/Cosine wave look-up table
  60. res_elecAngle = 1;
  61. a_elecAngle_XA = 0:res_elecAngle:360; % [deg] Electrical angle grid
  62. r_sin_M1 = sin((a_elecAngle_XA)*(pi/180));
  63. r_cos_M1 = cos((a_elecAngle_XA)*(pi/180));
  64. % Speed limitations
  65. n_max = 5000; % [rpm] Maximum motor speed: [-5000, 5000]
  66. % open loop speed -> voltage lookup table
  67. res_rpm_openloop = 100;
  68. delta_rpm_openloop = 100; % delta rpm that when speed decreased, enter open loop
  69. min_start_voltage_openloop = 10;
  70. min_openVol = 10;
  71. a_rpm_step = i_Udc / (n_max / res_rpm_openloop);
  72. % Motor parameters
  73. n_polePairs = 4; % [-] Number of motor pole pairs
  74. a_elecPeriod = 360; % [deg] Electrical angle period
  75. a_elecDeltaAngle = 60; % [deg] Electrical angle between two Hall sensor changing events
  76. a_mechAngle = a_elecDeltaAngle / n_polePairs; % [deg] Mechanical angle between two Hall sensor changing events
  77. r_whl = 6.5 * 2.54 * 1e-2 / 2; % [m] Wheel radius. Diameter = 6.5 inch (1 inch = 2.54 cm): Speed[kph] = rpm*(pi/30)*r_whl*3.6
  78. f_lpf_coeff = 0.4;
  79. %% F02_Diagnostics
  80. t_errQual = 0.24 * f_ctrl/3; % [s] Error qualification time
  81. t_errDequal = 1.80 * f_ctrl/3; % [s] Error dequalification time
  82. r_errInpTgtThres = 15; % [-] Error input target threshold (for "Blocked motor" detection)
  83. %hall, [4,6,2,3,1,5,4] [ 3,2,6,4,5,1]
  84. vec_hallToPos = [7 5 1 0 3 4 2 7]; % [-] Mapping Hall signal to position
  85. i_hall_offset = 60;%-30;
  86. % Speed Calculation Parameters
  87. cf_speedCoef = (i_hall_count_max * a_mechAngle * (pi/180) * (30/pi)); % [-] Speed calculation coefficient (factors are due to conversions rpm <-> rad/s)
  88. z_maxCntRst = i_hall_count_max; % [-] Maximum counter value for reset
  89. z_maxStillSecond = 2; %(second, also as time-out to detect standing still)
  90. n_commDeacvHi = 30; % [rpm] Commutation method deactivation speed high
  91. n_commAcvLo = 15; % [rpm] Commutation method activation speed low
  92. dz_cntTrnsDetHi = 140; % [-] Counter gradient High for transient behavior detection (used for speed estimation)
  93. dz_cntTrnsDetLo = 100; % [-] Counter gradient Low for steady state detection (used for speed estimation)
  94. n_stdStillDet = 3; % [rpm] Speed threshold for Stand still detection
  95. n_SpeedModeLo = 200; % min speed for exit speed ctrl mode
  96. n_SpeedModeHi = 300; % when speed is Hi can into speed ctrl mode
  97. % Motor Angle Measurement (e.g. using an encoder)
  98. b_angleMeasEna = 0; % [-] Enable flag for external mechanical motor angle sensor: 0 = estimated (default), 1 = measured
  99. % Control model request
  100. OPEN_MODE = 0; % [-] Open mode
  101. SPD_MODE = 1; % [-] Speed mode
  102. TRQ_MODE = 2; % [-] Torque mode
  103. z_ctrlModReq = TRQ_MODE; % [-] Control Mode Request (default)
  104. % Cruise control
  105. b_cruiseCtrlEna = 0; % [-] Cruise control enable flag: 0 = disable (default), 1 = enable
  106. n_cruiseMotTgt = 0; % [-] Cruise control motor speed target
  107. %% F04_Field_Weakening
  108. b_fieldWeakEna = 0; % [-] Field weakening enable flag: 0 = disable (default), 1 = enable
  109. r_fieldWeakHi = 1000; % [1000, 1500] Input target High threshold for reaching maximum Field Weakening / Phase Advance
  110. r_fieldWeakLo = 750; % [ 500, 1000] Input target Low threshold for starting Field Weakening / Phase Advance
  111. n_fieldWeakAuthHi = 400; % [rpm] Motor speed High for field weakening authorization
  112. n_fieldWeakAuthLo = 300; % [rpm] Motor speed Low for field weakening authorization
  113. % FOC method
  114. id_fieldWeakMax = 5; % [A] Field weakening maximum current
  115. % Voltage Limitations
  116. V_modulation = 0.95; % [-] Voltage margin to make sure that there is a sufficiently wide pulse for a good phase current measurement
  117. Vd_max = i_Udc * V_modulation;
  118. Vq_max_XA = 0:1:Vd_max;
  119. Vq_max_M1 = sqrt(Vd_max^2 - Vq_max_XA.^2); % Circle limitations look-up table
  120. i_sca = 1; % [-] [not tunable] Scalling factor A to int16 (50 = 1/0.02)
  121. % Current Limitations
  122. i_max = 120; % [A] Maximum allowed motor current (continuous)
  123. i_max = i_max * i_sca;
  124. iq_maxSca_XA = 0:0.02:0.99;
  125. iq_maxSca_XA = fixpt_evenspace_cleanup(iq_maxSca_XA, ufix(16), 2^-16); % Make sure the data is evely spaced up to the last bit
  126. iq_maxSca_M1 = sqrt(1 - iq_maxSca_XA.^2); % Current circle limitations map
  127. % D axis control gains
  128. cf_idKp = 4.0; % [-] P gain
  129. cf_idKi = 0.05; % [-] I gain
  130. cf_idKb = 1.0;
  131. % Q axis control gains
  132. cf_iqKp = 4.0; % [-] P gain
  133. cf_iqKi = 0.05; % [-] I gain
  134. cf_iqKb = 1.0;
  135. % Speed control gains
  136. cf_nKp = 3.1; % [-] P gain
  137. cf_nKi = 0.01;% [-] I gain
  138. cf_nKb = 0.02;
  139. % Torque iq limit
  140. cf_torqKLimHi = 0.8;
  141. cf_torqKLimLo = -0.1;