thro_torque.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. #include "foc/core/etcs.h"
  11. #include "foc/motor/throttle.h"
  12. static thro_torque_t _torque;
  13. void thro_torque_reset(void) {
  14. _torque.accl = false;
  15. _torque.thro_filted = 0.0f;
  16. _torque.thro_ration = _torque.thro_ration_last = 0.0f;
  17. _torque.torque_req = _torque.torque_real = _torque.torque_acc_ = 0.0f;
  18. _torque.gear = mc_get_internal_gear();
  19. }
  20. void thro_torque_init(void) {
  21. thro_torque_reset();
  22. _torque.spd_filted = 0.0f;
  23. }
  24. static __INLINE float gear_rpm_torque(u8 torque, s16 max) {
  25. return (float)torque/100.0f * max;
  26. }
  27. float thro_torque_gear_map(s16 rpm, u8 gear) {
  28. gear_t *_current_gear = mc_get_gear_config_by_gear(gear);
  29. if (_current_gear == NULL) {
  30. return 0;
  31. }
  32. if (rpm > _current_gear->max_speed) {
  33. rpm = _current_gear->max_speed;
  34. }
  35. if (rpm <= 1000) {
  36. return gear_rpm_torque(_current_gear->torque[0], _current_gear->max_torque);
  37. }
  38. for (int i = 1; i < CONFIG_GEAR_SPEED_TRQ_NUM; i++) {
  39. if (rpm <= 1000 * (i + 1)) { //线性插值
  40. float trq1 = gear_rpm_torque(_current_gear->torque[i-1], _current_gear->max_torque);
  41. float min_rpm = (i * 1000);
  42. float trq2 = gear_rpm_torque(_current_gear->torque[i], _current_gear->max_torque);
  43. float max_rpm = (i + 1) * 1000;
  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 gear_rpm_torque(_current_gear->torque[CONFIG_GEAR_SPEED_TRQ_NUM-1], _current_gear->max_torque);
  52. }
  53. /* 获取油门开度 */
  54. float get_throttle_ration(float f_throttle) {
  55. if (f_throttle <= throttle_start_vol()) {
  56. return 0;
  57. }
  58. float delta = f_throttle - throttle_start_vol();
  59. int ration = (delta * 100.0f) / throttle_vol_range();
  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 = throttle_start_vol() + r * throttle_vol_range();
  67. return fclamp(vol, 0, throttle_end_vol());
  68. }
  69. static float _thro_torque_for_accelerate(float ration) {
  70. float max_torque = thro_torque_gear_map((s16)_torque.spd_filted, _torque.gear);
  71. float thro_torque = max_torque * ration;
  72. float acc_r = 1.0f;
  73. if (_torque.thro_ration_last < 1.0f) {
  74. acc_r = (ration - _torque.thro_ration_last)/ (1.0f - _torque.thro_ration_last);
  75. }
  76. acc_r = fclamp(acc_r, 0, 1.0f);
  77. float acc_torque = _torque.torque_real + acc_r * (max_torque - _torque.torque_real);
  78. if (acc_torque < 0) {
  79. acc_torque = 0;
  80. }
  81. /*
  82. 直接获取油门开度对应的加速扭矩thro_torque 不小于间接计算得到的 acc_torque
  83. */
  84. float torque_acc_ = thro_torque - acc_torque;
  85. float step = 0.0f;
  86. if (torque_acc_ > 0) {
  87. float acc_t = mc_gear_conf()->accl_time;
  88. step = torque_acc_ / (acc_t + 0.00001f);
  89. }else {
  90. torque_acc_ = 0;
  91. }
  92. step_towards(&_torque.torque_acc_, torque_acc_, step);
  93. return (acc_torque + _torque.torque_acc_);
  94. }
  95. static float thro_torque_for_accelerate(void) {
  96. return _thro_torque_for_accelerate(_torque.thro_ration);
  97. }
  98. static float thro_torque_for_decelerate(void) {
  99. if (_torque.thro_ration_last == 0.0f) {
  100. return 0;
  101. }
  102. float dec_r = _torque.thro_ration / _torque.thro_ration_last;
  103. dec_r = fclamp(dec_r, 0.0f, 1.0f);
  104. return dec_r * _torque.torque_real;
  105. }
  106. static void thro_torque_request(void) {
  107. #ifdef CONFIG_CRUISE_ENABLE_ACCL
  108. if (mc_is_cruise_enabled() && mc_throttle_released()) {
  109. return;
  110. }
  111. #endif
  112. if (mc_throttle_released() && eCtrl_enable_eBrake(true)) {
  113. return;
  114. }
  115. if (!mc_throttle_released()) {
  116. float curr_rpm = PMSM_FOC_GetSpeed();
  117. if (_torque.accl) { //加速需求
  118. float ref_torque = thro_torque_for_accelerate();
  119. float hold_torque = PMSM_FOC_Get()->out.f_autohold_trq;
  120. if (hold_torque < 0) { //下坡驻车,最小给0扭矩
  121. hold_torque = 0;
  122. }
  123. ref_torque = MAX(hold_torque, ref_torque);
  124. if (curr_rpm <= CONFIG_ZERO_SPEED_RPM) {//从静止开始加速
  125. if (_torque.torque_req < hold_torque) {
  126. _torque.torque_req = hold_torque;
  127. eCtrl_reset_Torque(hold_torque);
  128. }
  129. }else {
  130. PMSM_FOC_Get()->out.f_autohold_trq = 0;
  131. }
  132. /* 处理加速ramp时间的变化,需要缓慢变小,变大可以立即处理,
  133. * 加速时间缓慢变小可以防止突然大扭矩加速
  134. */
  135. u16 now_ramp_time = eCtrl_get_torque_acc_time();
  136. u16 next_ramp_time = mc_gear_conf()->accl_time;
  137. if (curr_rpm < CONFIG_ZERO_SPEED_RAMP_RMP) {
  138. next_ramp_time = mc_gear_conf()->zero_accl;
  139. }
  140. if (next_ramp_time == 0) {
  141. next_ramp_time = 100;
  142. }
  143. if (now_ramp_time != next_ramp_time) {
  144. if (next_ramp_time > now_ramp_time) {
  145. eCtrl_set_accl_time(next_ramp_time);
  146. }else {
  147. float f_now = (float)now_ramp_time;
  148. float f_next = (float)next_ramp_time;
  149. step_towards(&f_now, f_next, 0.5f);
  150. if (f_now <= 10) {
  151. f_now = 10;
  152. }
  153. eCtrl_set_accl_time((u16)f_now);
  154. }
  155. }
  156. _torque.torque_req = ref_torque;
  157. }else {
  158. float ref_torque = thro_torque_for_decelerate();
  159. /* autohold 启动的情况下,转把在0位置附近小幅抖动 */
  160. if (curr_rpm <= CONFIG_ZERO_SPEED_RPM) {
  161. float hold_torque = PMSM_FOC_Get()->out.f_autohold_trq;
  162. ref_torque = MAX(hold_torque, ref_torque);
  163. }
  164. _torque.torque_req = ref_torque;
  165. }
  166. etcs_set_torque(_torque.torque_req);
  167. }else if (mc_throttle_released() && eCtrl_get_FinalTorque() != 0){
  168. _torque.torque_req = 0.0f;
  169. etcs_set_torque(_torque.torque_req);
  170. }
  171. }
  172. #define THRO_RPM_LP_CEOF 0.01f
  173. void thro_torque_process(u8 run_mode, float f_throttle) {
  174. float thro_r = get_throttle_ration(f_throttle);
  175. _torque.gear = mc_get_internal_gear();
  176. LowPass_Filter(_torque.spd_filted, PMSM_FOC_GetSpeed(), THRO_RPM_LP_CEOF);
  177. if (thro_r > _torque.thro_ration) {
  178. if (!_torque.accl) {
  179. _torque.thro_ration_last = _torque.thro_ration;
  180. _torque.torque_real = PMSM_FOC_Get()->in.s_targetTorque;
  181. if (_torque.torque_real < 0) { //电子刹车的时候,扭矩可能为负
  182. _torque.torque_real = 0;
  183. }
  184. _torque.torque_acc_ = 0;
  185. }
  186. _torque.accl = true;
  187. }else if (thro_r < _torque.thro_ration) {
  188. if (_torque.accl) {
  189. _torque.thro_ration_last = _torque.thro_ration;
  190. _torque.torque_real = PMSM_FOC_Get()->in.s_targetTorque;
  191. if (_torque.torque_real < 0) { //电子刹车的时候,扭矩可能为负
  192. _torque.torque_real = 0;
  193. }
  194. }
  195. _torque.accl = false;
  196. }
  197. _torque.thro_ration = thro_r;
  198. if (run_mode == CTRL_MODE_TRQ) {
  199. thro_torque_request();
  200. }else if (run_mode == CTRL_MODE_SPD) {
  201. if (!mc_is_cruise_enabled()) {
  202. float speed_Ref = PMSM_FOC_GetSpeedLimit() * _torque.thro_ration;
  203. PMSM_FOC_Set_TgtSpeed(speed_Ref);
  204. }
  205. }else if (run_mode == CTRL_MODE_EBRAKE) {
  206. float vel = PMSM_FOC_GetSpeed();
  207. float ebrk_trq = motor_get_ebreak_toruqe(vel);
  208. if (ebrk_trq >= -PMSM_FOC_GetEbrkTorque()/2){
  209. eCtrl_Set_eBrk_RampTime(1);
  210. }
  211. if (ebrk_trq != 0) {
  212. eCtrl_set_TgtTorque(ebrk_trq);
  213. }
  214. if (eCtrl_get_FinalTorque() < 0.0001f && vel < CONFIG_MIN_RPM_EXIT_EBRAKE) {
  215. eCtrl_enable_eBrake(false);
  216. thro_torque_reset();
  217. }
  218. if (!mc_throttle_released() || (mc_throttle_released() && (vel == 0.0f))) {
  219. eCtrl_enable_eBrake(false);
  220. thro_torque_reset();
  221. }
  222. }
  223. }
  224. /* 定速巡航需要判断是否需要加速 */
  225. float get_user_request_torque(void) {
  226. if (_torque.accl) {
  227. return thro_torque_for_accelerate();
  228. }
  229. return thro_torque_for_decelerate();
  230. }
  231. void thro_torque_log(void) {
  232. sys_debug("accl %d, real %f, req %f\n", _torque.accl, _torque.torque_real, _torque.torque_req);
  233. sys_debug("ration %f - %f - %f - %d\n", _torque.thro_ration, _torque.thro_ration_last, thro_torque_for_accelerate(), _torque.gear);
  234. }