thro_torque.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include "foc/core/thro_torque.h"
  2. #include "foc/foc_config.h"
  3. #include "foc/motor/motor.h"
  4. #include "foc/motor/motor_param.h"
  5. #include "foc/core/e_ctrl.h"
  6. #include "foc/core/PMSM_FOC_Core.h"
  7. #include "app/nv_storage.h"
  8. #include "libs/logger.h"
  9. #include "prot/can_foc_msg.h"
  10. static thro_torque_t _torque;
  11. static motor_map_t gear_torques[5][5] = {
  12. [0] = {{500, 100}, {1200, 100}, {1700, 80}, {2200, 75}, {2800, 50},},
  13. [1] = {{800, 130}, {1600, 120}, {2400, 110}, {3200, 80}, {4300, 60},},
  14. [2] = {{1200, 150}, {2200, 140}, {3200, 120}, {4200, 80}, {5300, 70},},
  15. [3] = {{3000, 200}, {4740, 150}, {5050, 110}, {5200, 85}, {5300, 70},},
  16. [4] = {{4500, 200}, {4740, 150}, {5050, 110}, {5200, 85}, {5300, 70},},
  17. };
  18. void thro_torque_reset(void) {
  19. _torque.accl = false;
  20. _torque.thro_filted = 0.0f;
  21. _torque.thro_ration = _torque.thro_ration_last = 0.0f;
  22. _torque.torque_req = _torque.torque_real = 0.0f;
  23. }
  24. void thro_torque_init(void) {
  25. thro_torque_reset();
  26. trq2dq_lookup_init();
  27. _torque.spd_filted = 0.0f;
  28. }
  29. static float thro_torque_gear_map(void) {
  30. u8 gear = mc_get_gear();
  31. if (gear > 4) {
  32. gear = 0;
  33. }
  34. motor_map_t *map = &gear_torques[gear][0];
  35. s16 rpm = (s16)_torque.spd_filted;
  36. if (rpm <= map[0].rpm) {
  37. return (float)map[0].torque;
  38. }
  39. int map_size = 5;
  40. for (int i = 1; i < map_size; i++) {
  41. if (rpm <= map[i].rpm) { //线性插值
  42. float trq1 = map[i-1].torque;
  43. float min_rpm = map[i-1].rpm;
  44. float trq2 = map[i].torque;
  45. float max_rpm = map[i].rpm;
  46. if (trq1 > trq2) {
  47. return f_map_inv((float)rpm, min_rpm, max_rpm, trq2, trq1);
  48. }else {
  49. return f_map((float)rpm, min_rpm, max_rpm, trq1, trq2);
  50. }
  51. }
  52. }
  53. return (float)map[map_size-1].torque;
  54. }
  55. /* 获取油门开度 */
  56. static float thro_ration(float f_throttle) {
  57. if (f_throttle <= nv_get_foc_params()->n_startThroVol) {
  58. return 0;
  59. }
  60. float delta = f_throttle - (nv_get_foc_params()->n_startThroVol);
  61. int ration = (delta * 100.0f) / (nv_get_foc_params()->n_endThroVol - nv_get_foc_params()->n_startThroVol);
  62. return ((float)ration)/100.0f;
  63. }
  64. float thro_ration_to_voltage(float r) {
  65. if (r == 0) {
  66. return 0;
  67. }
  68. float vol = nv_get_foc_params()->n_startThroVol + r * (nv_get_foc_params()->n_endThroVol - nv_get_foc_params()->n_startThroVol);
  69. return fclamp(vol, 0, nv_get_foc_params()->n_endThroVol);
  70. }
  71. static float thro_torque_for_accelerate(void) {
  72. float max_torque = thro_torque_gear_map();
  73. float acc_r = 1.0f;
  74. if (_torque.thro_ration_last < 1.0f) {
  75. acc_r = (_torque.thro_ration - _torque.thro_ration_last)/ (1.0f - _torque.thro_ration_last);
  76. }
  77. acc_r = fclamp(acc_r, 0, 1.0f);
  78. float acc_torque = _torque.torque_real + acc_r * (max_torque - _torque.torque_real);
  79. return acc_torque;
  80. }
  81. static float thro_torque_for_decelerate(void) {
  82. if (_torque.thro_ration_last == 0.0f) {
  83. return 0;
  84. }
  85. float dec_r = _torque.thro_ration / _torque.thro_ration_last;
  86. dec_r = fclamp(dec_r, 0.0f, 1.0f);
  87. return dec_r * _torque.torque_real;
  88. }
  89. static void thro_torque_request(void) {
  90. #ifdef CONFIG_CRUISE_ENABLE_ACCL
  91. if (mc_is_cruise_enabled() && mc_throttle_released()) {
  92. return;
  93. }
  94. #endif
  95. if (mc_throttle_released() && eCtrl_enable_eBrake(true)) {
  96. return;
  97. }
  98. if (!mc_throttle_released()) {
  99. if (_torque.accl) { //加速需求
  100. float ref_torque = thro_torque_for_accelerate();
  101. float curr_rpm = PMSM_FOC_GetSpeed();
  102. if (curr_rpm <= CONFIG_ZERO_SPEED_RPM) {//从静止开始加速
  103. float hold_torque = eCtrl_get_FinalTorque();
  104. ref_torque = MAX(hold_torque, ref_torque);
  105. }
  106. _torque.torque_req = ref_torque;
  107. }else {
  108. float ref_torque = thro_torque_for_decelerate();
  109. _torque.torque_req = ref_torque;
  110. }
  111. PMSM_FOC_Set_Torque(_torque.torque_req);
  112. }else if (mc_throttle_released() && eCtrl_get_FinalTorque() != 0){
  113. _torque.torque_req = 0.0f;
  114. PMSM_FOC_Set_Torque(_torque.torque_req);
  115. }
  116. }
  117. #define THRO_REF_LP_CEOF 0.2f
  118. static void thro_torque_filter(float f_throttle) {
  119. if (mc_throttle_released()){
  120. thro_torque_reset();
  121. }else {
  122. LowPass_Filter(_torque.thro_filted, f_throttle, THRO_REF_LP_CEOF);
  123. }
  124. float curr_rpm = PMSM_FOC_GetSpeed();
  125. if (curr_rpm == 0) {
  126. _torque.spd_filted = 0;
  127. }else {
  128. LowPass_Filter(_torque.spd_filted, curr_rpm, 0.01f);
  129. }
  130. }
  131. void thro_torque_process(u8 run_mode, float f_throttle) {
  132. thro_torque_filter(f_throttle);
  133. float thro_r = thro_ration(_torque.thro_filted);
  134. if (thro_r > _torque.thro_ration) {
  135. if (!_torque.accl) {
  136. _torque.thro_ration_last = _torque.thro_ration;
  137. _torque.torque_real = PMSM_FOC_Get()->in.s_targetTorque;
  138. }
  139. _torque.accl = true;
  140. }else if (thro_r < _torque.thro_ration) {
  141. if (_torque.accl) {
  142. _torque.thro_ration_last = _torque.thro_ration;
  143. _torque.torque_real = PMSM_FOC_Get()->in.s_targetTorque;
  144. }
  145. _torque.accl = false;
  146. }
  147. _torque.thro_ration = thro_r;
  148. if (run_mode == CTRL_MODE_TRQ) {
  149. thro_torque_request();
  150. }else if (run_mode == CTRL_MODE_SPD) {
  151. if (!mc_is_cruise_enabled()) {
  152. float speed_Ref = PMSM_FOC_GetSpeedLimit() * _torque.thro_ration;
  153. PMSM_FOC_Set_Speed(speed_Ref);
  154. }
  155. }else if (run_mode == CTRL_MODE_EBRAKE) {
  156. if (eCtrl_get_FinalTorque() < 0.0001f && PMSM_FOC_GetSpeed() < CONFIG_MIN_RPM_EXIT_EBRAKE) {
  157. eCtrl_enable_eBrake(false);
  158. }
  159. if (!mc_throttle_released() || (mc_throttle_released() && (PMSM_FOC_GetSpeed() == 0.0f))) {
  160. eCtrl_enable_eBrake(false);
  161. }
  162. }
  163. }
  164. /* 定速巡航需要判断是否需要加速 */
  165. float get_user_request_torque(void) {
  166. if (_torque.accl) {
  167. return thro_torque_for_accelerate();
  168. }
  169. return thro_torque_for_decelerate();
  170. }
  171. void thro_torque_log(void) {
  172. sys_debug("accl %d, real %f, req %f\n", _torque.accl, _torque.torque_real, _torque.torque_req);
  173. sys_debug("ration %f - %f - %f\n", _torque.thro_ration, _torque.thro_ration_last, thro_torque_for_accelerate());
  174. }