thro_torque.c 8.6 KB

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