thro_torque.c 6.0 KB

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