init_model.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 = 6.2e-6; % [s] Model sampling time (1 MHz)
  33. Ts_ctrl = 62e-6; % [s] Controller sampling time (~16 kHz)
  34. f_ctrl = 16e3; % [Hz] Controller frequency = 1/Ts_ctrl (16 kHz)
  35. i_pwm_count = 4000;
  36. i_dead = 10;
  37. i_adc = 10;
  38. i_sample_before = 10;
  39. i_Udc = 560;
  40. i_half_pwm_count = i_pwm_count;
  41. i_sample_min = (i_dead + i_adc + i_sample_before);
  42. i_hall_count_max = 1/Ts;
  43. % Current sample parameters
  44. n_adc_max = 4096;
  45. n_resistance = 0.005;
  46. n_ref_vol = 3.3;
  47. n_gain = 17.1;
  48. %VBUS sample parameters
  49. b_start_with_commutation = 0;
  50. % Sine/Cosine wave look-up table
  51. res_elecAngle = 1;
  52. a_elecAngle_XA = 0:res_elecAngle:360; % [deg] Electrical angle grid
  53. r_sin_M1 = sin((a_elecAngle_XA)*(pi/180));
  54. r_cos_M1 = cos((a_elecAngle_XA)*(pi/180));
  55. % Motor parameters
  56. n_polePairs = 4; % [-] Number of motor pole pairs
  57. a_elecPeriod = 360; % [deg] Electrical angle period
  58. a_elecDeltaAngle = 60; % [deg] Electrical angle between two Hall sensor changing events
  59. a_mechAngle = a_elecDeltaAngle / n_polePairs; % [deg] Mechanical angle between two Hall sensor changing events
  60. 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
  61. f_lpf_coeff = 0.2;
  62. %hall, [4,6,2,3,1,5,4] [ 3,2,6,4,5,1]
  63. vec_hallToPos = [7 5 1 0 3 4 2 7]; % [-] Mapping Hall signal to position
  64. i_hall_offset = -30;
  65. % Speed Calculation Parameters
  66. cf_speedCoef = (i_hall_count_max * a_mechAngle * (pi/180) * (30/pi)); % [-] Speed calculation coefficient (factors are due to conversions rpm <-> rad/s)
  67. z_maxCntRst = i_hall_count_max; % [-] Maximum counter value for reset (works also as time-out to detect standing still)
  68. n_commDeacvHi = 30; % [rpm] Commutation method deactivation speed high
  69. n_commAcvLo = 15; % [rpm] Commutation method activation speed low
  70. dz_cntTrnsDetHi = 40; % [-] Counter gradient High for transient behavior detection (used for speed estimation)
  71. dz_cntTrnsDetLo = 20; % [-] Counter gradient Low for steady state detection (used for speed estimation)
  72. n_stdStillDet = 3; % [rpm] Speed threshold for Stand still detection
  73. % Motor Angle Measurement (e.g. using an encoder)
  74. b_angleMeasEna = 0; % [-] Enable flag for external mechanical motor angle sensor: 0 = estimated (default), 1 = measured
  75. % Control model request
  76. OPEN_MODE = 0; % [-] Open mode
  77. VLT_MODE = 1; % [-] Voltage mode
  78. SPD_MODE = 2; % [-] Speed mode
  79. TRQ_MODE = 3; % [-] Torque mode
  80. z_ctrlModReq = VLT_MODE; % [-] Control Mode Request (default)
  81. % Cruise control
  82. b_cruiseCtrlEna = 0; % [-] Cruise control enable flag: 0 = disable (default), 1 = enable
  83. n_cruiseMotTgt = 0; % [-] Cruise control motor speed target
  84. %% F04_Field_Weakening
  85. b_fieldWeakEna = 0; % [-] Field weakening enable flag: 0 = disable (default), 1 = enable
  86. r_fieldWeakHi = 1000; % [1000, 1500] Input target High threshold for reaching maximum Field Weakening / Phase Advance
  87. r_fieldWeakLo = 750; % [ 500, 1000] Input target Low threshold for starting Field Weakening / Phase Advance
  88. n_fieldWeakAuthHi = 400; % [rpm] Motor speed High for field weakening authorization
  89. n_fieldWeakAuthLo = 300; % [rpm] Motor speed Low for field weakening authorization
  90. % FOC method
  91. id_fieldWeakMax = 5; % [A] Field weakening maximum current
  92. %% F05_Field_Oriented_Control
  93. z_selPhaCurMeasABC = 0; % [-] Select measured current phases: {iA,iB} = 0; {iB,iC} = 1; {iA,iC} = 2
  94. % Motor Limitations Calibratables
  95. cf_iqKiLimProt = 60 / (f_ctrl/3); % [-] Current limit protection integral gain (only used in VLT_MODE and SPD_MODE)
  96. cf_nKiLimProt = 20 / (f_ctrl/3); % [-] Speed limit protection integral gain (only used in VLT_MODE and TRQ_MODE)
  97. cf_KbLimProt = 1000 / (f_ctrl/3);% [-] Back calculation gain for integral anti-windup
  98. % Voltage Limitations
  99. V_margin = 0.95; % [-] Voltage margin to make sure that there is a sufficiently wide pulse for a good phase current measurement
  100. Vd_max = 560 * V_margin;
  101. Vq_max_XA = 0:1:Vd_max;
  102. Vq_max_M1 = sqrt(Vd_max^2 - Vq_max_XA.^2); % Circle limitations look-up table
  103. % Speed limitations
  104. n_max = 1000; % [rpm] Maximum motor speed: [-1500, 1500]
  105. i_sca = 20; % [-] [not tunable] Scalling factor A to int16 (50 = 1/0.02)
  106. % Current Limitations
  107. i_max = 100; % [A] Maximum allowed motor current (continuous)
  108. i_max = i_max * i_sca;
  109. iq_maxSca_XA = 0:0.02:0.99;
  110. 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
  111. iq_maxSca_M1 = sqrt(1 - iq_maxSca_XA.^2); % Current circle limitations map
  112. %% F06_Control_Type_Management
  113. % Commutation method
  114. z_commutMap_M1 = [1 0 -1 -1 0 1; % Phase A
  115. 0 1 1 0 -1 -1; % Phase B
  116. -1 -1 0 1 1 0]; % Phase C [-] Commutation method map
  117. % Q axis control gains
  118. cf_iqKp = 0.3; % [-] P gain
  119. cf_iqKi = 100 / (f_ctrl/3); % [-] I gain
  120. % D axis control gains
  121. cf_idKp = 0.9; % [-] P gain
  122. cf_idKi = 1.071; % [-] I gain
  123. % Speed control gains
  124. cf_nKp = 1.18; % [-] P gain
  125. cf_nKi = 20.4 / (f_ctrl/3);% [-] I gain