thro_torque.c 8.6 KB

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