init_model.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. % Clear workspace
  2. close all
  3. clear
  4. clc
  5. % Load model parameters
  6. Ts = 5e-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)
  9. f_speed_ctrl = 1e3; % [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 = f_ctrl/4;
  12. i_dead = 10;
  13. i_adc = 10;
  14. i_sample_before = 10;
  15. i_Udc = 48;
  16. i_half_pwm_count = i_pwm_count;
  17. i_sample_min = (i_dead + i_adc + i_sample_before);
  18. i_hall_count_max = 1/Ts;
  19. %Current sample hw parameters
  20. n_adc_max = 4096;
  21. n_resistance = 0.0005;
  22. n_ref_vol = 3.3;
  23. n_gain = 17.1;
  24. %VBUS sample parameters
  25. b_start_with_commutation = 0;
  26. % Sine/Cosine wave look-up table
  27. res_elecAngle = 0.25;
  28. a_elecAngle_XA = 0:res_elecAngle:360; % [deg] Electrical angle grid
  29. a_elecAngle_XA = fixpt_evenspace_cleanup(a_elecAngle_XA, sfix(16), 2^-3); % Make sure the data is evely spaced up to the last bit
  30. r_sin_M1 = sin((a_elecAngle_XA)*(pi/180));
  31. r_cos_M1 = cos((a_elecAngle_XA)*(pi/180));
  32. % Speed limitations
  33. n_max = 5000; % [rpm] Maximum motor speed: [-5000, 5000]
  34. % open loop speed -> voltage lookup table
  35. min_openVol = 10;
  36. % Motor parameters
  37. n_polePairs = 4; % [-] Number of motor pole pairs
  38. a_elecPeriod = 360; % [deg] Electrical angle period
  39. a_elecDeltaAngle = 60; % [deg] Electrical angle between two Hall sensor changing events
  40. a_mechAngle = a_elecDeltaAngle / n_polePairs; % [deg] Mechanical angle between two Hall sensor changing events
  41. 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
  42. f_lpf_coeff = 0.4;
  43. %% F02_Diagnostics
  44. t_errQual = 0.24 * f_ctrl/3; % [s] Error qualification time
  45. t_errDequal = 1.80 * f_ctrl/3; % [s] Error dequalification time
  46. r_errInpTgtThres = 15; % [-] Error input target threshold (for "Blocked motor" detection)
  47. %hall, [4,6,2,3,1,5,4] [ 3,2,6,4,5,1]
  48. vec_hallToPos = [7 5 1 0 3 4 2 7]; % [-] Mapping Hall signal to position
  49. i_hall_offset = 60;%-30;
  50. % Speed Calculation Parameters
  51. cf_speedCoef = (i_hall_count_max * a_mechAngle * (pi/180) * (30/pi)); % [-] Speed calculation coefficient (factors are due to conversions rpm <-> rad/s)
  52. z_maxCntRst = i_hall_count_max; % [-] Maximum counter value for reset
  53. z_maxStillSecond = 2; %(second, also as time-out to detect standing still)
  54. n_commDeacvHi = 30; % [rpm] Commutation method deactivation speed high
  55. n_commAcvLo = 15; % [rpm] Commutation method activation speed low
  56. dz_cntTrnsDetHi = 140; % [-] Counter gradient High for transient behavior detection (used for speed estimation)
  57. dz_cntTrnsDetLo = 100; % [-] Counter gradient Low for steady state detection (used for speed estimation)
  58. n_stdStillDet = 3; % [rpm] Speed threshold for Stand still detection
  59. n_SpeedModeLo = 200; % min speed for exit speed ctrl mode
  60. n_SpeedModeHi = 300; % when speed is Hi can into speed ctrl mode
  61. % Motor Angle Measurement (e.g. using an encoder)
  62. b_angleMeasEna = 0; % [-] Enable flag for external mechanical motor angle sensor: 0 = estimated (default), 1 = measured
  63. % Control model request
  64. OPEN_MODE = 0; % [-] Open mode
  65. SPD_MODE = 1; % [-] Speed mode
  66. TRQ_MODE = 2; % [-] Torque mode
  67. z_ctrlModReq = TRQ_MODE; % [-] Control Mode Request (default)
  68. % Cruise control
  69. b_cruiseCtrlEna = 0; % [-] Cruise control enable flag: 0 = disable (default), 1 = enable
  70. n_cruiseMotTgt = 0; % [-] Cruise control motor speed target
  71. %% F04_Field_Weakening
  72. b_fieldWeakEna = 0; % [-] Field weakening enable flag: 0 = disable (default), 1 = enable
  73. r_fieldWeakHi = 1000; % [1000, 1500] Input target High threshold for reaching maximum Field Weakening / Phase Advance
  74. r_fieldWeakLo = 750; % [ 500, 1000] Input target Low threshold for starting Field Weakening / Phase Advance
  75. n_fieldWeakAuthHi = 400; % [rpm] Motor speed High for field weakening authorization
  76. n_fieldWeakAuthLo = 300; % [rpm] Motor speed Low for field weakening authorization
  77. % FOC method
  78. id_fieldWeakMax = 5; % [A] Field weakening maximum current
  79. % Voltage Limitations
  80. V_modulation = 0.95; % [-] Voltage margin to make sure that there is a sufficiently wide pulse for a good phase current measurement
  81. Vd_max = i_Udc * V_modulation;
  82. Vq_max_XA = 0:1:Vd_max;
  83. Vq_max_M1 = sqrt(Vd_max^2 - Vq_max_XA.^2); % Circle limitations look-up table
  84. i_sca = 1; % [-] [not tunable] Scalling factor A to int16 (50 = 1/0.02)
  85. % Current Limitations
  86. i_max = 120; % [A] Maximum allowed motor current (continuous)
  87. i_max = i_max * i_sca;
  88. iq_maxSca_XA = 0:0.02:0.99;
  89. 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
  90. iq_maxSca_M1 = sqrt(1 - iq_maxSca_XA.^2); % Current circle limitations map
  91. % D axis control gains
  92. cf_idKp = 4.0; % [-] P gain
  93. cf_idKi = 0.05; % [-] I gain
  94. cf_idKb = 1.0;
  95. % Q axis control gains
  96. cf_iqKp = 4.0; % [-] P gain
  97. cf_iqKi = 0.05; % [-] I gain
  98. cf_iqKb = 1.0;
  99. % Speed control gains
  100. cf_nKp = 3.1; % [-] P gain
  101. cf_nKi = 0.01;% [-] I gain
  102. cf_nKb = 0.02;
  103. % Torque iq limit
  104. cf_torqKLimHi = 0.8;
  105. cf_torqKLimLo = -0.1;