init_model.m 5.5 KB

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