thro_torque.c 5.5 KB

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