init_model.m 6.1 KB

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