torque.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include "foc/core/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 rpm_trq_map_t gear_torques[5][5] = {
  11. [0] = {{500, 100}, {1200, 100}, {1700, 80}, {2200, 75}, {2800, 50},},
  12. [1] = {{800, 130}, {1600, 120}, {2400, 110}, {3200, 80}, {4300, 60},},
  13. [2] = {{1200, 150}, {2200, 140}, {3200, 120}, {4200, 80}, {5300, 70},},
  14. [3] = {{3000, 200}, {4740, 150}, {5050, 110}, {5200, 85}, {5300, 70},},
  15. [4] = {{4500, 200}, {4740, 150}, {5050, 110}, {5200, 85}, {5300, 70},},
  16. };
  17. /*
  18. 通过查表获取对应扭矩和速度时的Id和IQ的分配
  19. */
  20. static torque_lut_t *torque_lkup_table = NULL;
  21. static torque_manager_t torque_ctrl;
  22. void torque_init(void) {
  23. torque_lkup_table = nv_get_trq_tlb();
  24. torque_reset();
  25. }
  26. void torque_reset(void) {
  27. memset(&torque_ctrl, 0, sizeof(torque_ctrl));
  28. }
  29. void torque_get_idq(float torque, float rpm, DQ_t *dq_out) {
  30. if ((torque_lkup_table == NULL) || (torque < 0 || rpm < 0)) {
  31. dq_out->d = 0;
  32. dq_out->q = torque;
  33. return;
  34. }
  35. int trq_idx = (int)torque / TBL_TRQ_INTVAL;
  36. int rpm_idx = (int)rpm / TBL_SPD_INTVAL;
  37. if (trq_idx >= MAX_TRQ_POINTS) {
  38. trq_idx = MAX_TRQ_POINTS -1;
  39. }
  40. if (rpm_idx >= MAX_SPD_POINTS) {
  41. rpm_idx = MAX_SPD_POINTS -1;
  42. }
  43. s16 d = torque_lkup_table->dq[trq_idx][rpm_idx].d;
  44. s16 q = torque_lkup_table->dq[trq_idx][rpm_idx].q;
  45. if (trq_idx < MAX_TRQ_POINTS - 1) {
  46. trq_idx += 1;
  47. }
  48. if (rpm_idx < MAX_SPD_POINTS - 1) {
  49. rpm_idx += 1;
  50. }
  51. s16 d_delta = torque_lkup_table->dq[trq_idx][rpm_idx].d - d;
  52. s16 q_delta = torque_lkup_table->dq[trq_idx][rpm_idx].q - q;
  53. float comp_ceof = 0.5f * ((torque - torque/TBL_TRQ_INTVAL*TBL_TRQ_INTVAL)/(float)TBL_TRQ_INTVAL + (rpm - rpm/TBL_SPD_INTVAL*TBL_SPD_INTVAL)/(float)TBL_SPD_INTVAL);
  54. dq_out->d = d + d_delta * comp_ceof;
  55. dq_out->q = q + q_delta * comp_ceof;
  56. }
  57. float torque_max_from_gear_rpm(void) {
  58. u8 gear = mc_get_gear();
  59. if (gear > 4) {
  60. gear = 0;
  61. }
  62. rpm_trq_map_t *map = &gear_torques[gear][0];
  63. s16 rpm = (s16)torque_ctrl.spd_filted;
  64. if (rpm <= map[0].rpm) {
  65. return (float)map[0].torque;
  66. }
  67. int map_size = 5;
  68. for (int i = 1; i < map_size; i++) {
  69. if (rpm <= map[i].rpm) { //线性插值
  70. float trq1 = map[i-1].torque;
  71. float min_rpm = map[i-1].rpm;
  72. float trq2 = map[i].torque;
  73. float max_rpm = map[i].rpm;
  74. if (trq1 > trq2) {
  75. return f_map_inv((float)rpm, min_rpm, max_rpm, trq2, trq1);
  76. }else {
  77. return f_map((float)rpm, min_rpm, max_rpm, trq1, trq2);
  78. }
  79. }
  80. }
  81. return (float)map[map_size-1].torque;
  82. }
  83. /* 获取油门开度 */
  84. static float throttle_ration(float f_throttle) {
  85. if (f_throttle <= nv_get_foc_params()->n_minThroVol) {
  86. return 0;
  87. }
  88. float delta = f_throttle - (nv_get_foc_params()->n_minThroVol);
  89. int ration = (delta * 100.0f) / (nv_get_foc_params()->n_maxThroVol - nv_get_foc_params()->n_minThroVol);
  90. return ((float)ration)/100.0f;
  91. }
  92. float throttle_to_speed(float f_throttle) {
  93. return (PMSM_FOC_GetSpeedLimit() * throttle_ration(f_throttle));
  94. }
  95. float throttle_to_torque(float f_throttle) {
  96. return PMSM_FOC_GetTorqueLimit() * throttle_ration(f_throttle);
  97. }
  98. #define FUNC_SEG1_X_END 0.5F
  99. #define FUNC_SEG1_Y_END 0.25F
  100. #define FUNC_SEG1_K (FUNC_SEG1_Y_END/FUNC_SEG1_X_END)
  101. #define FUNC_SEG2_X_OFF FUNC_SEG1_X_END
  102. #define FUNC_SEG2_Y_OFF FUNC_SEG1_Y_END
  103. #define FUNC_SEG2_K ((1.0F-FUNC_SEG1_Y_END)/(1.0F - FUNC_SEG1_X_END))
  104. static float __INLINE unline_func(float x) {
  105. if (x <= FUNC_SEG1_X_END) {
  106. return x * FUNC_SEG1_K;
  107. }
  108. return (x - FUNC_SEG1_X_END) * FUNC_SEG2_K + FUNC_SEG2_Y_OFF;
  109. }
  110. #define REAL_DQ_CEOF 0.9f
  111. #define FINAL_DQ_CEFO 1.1F
  112. void request_torque(float thro_ration) {
  113. float curr_rpm = PMSM_FOC_GetSpeed();
  114. if (curr_rpm == 0) {
  115. torque_ctrl.spd_filted = 0;
  116. }else {
  117. LowPass_Filter(torque_ctrl.spd_filted, curr_rpm, 0.01f);
  118. }
  119. float torque_map = torque_max_from_gear_rpm();// (float)get_max_torque_for_rpm((s16)torque_ctrl.spd_filted);
  120. float torque_lim = PMSM_FOC_GetTorqueLimit();
  121. float max_torque = min(torque_map, torque_lim);
  122. float ref_torque = max_torque * unline_func(thro_ration);
  123. if ((mc_throttle_released()) && eCtrl_enable_eBrake(true)) {
  124. return;
  125. }
  126. if (!mc_throttle_released()) {
  127. if (curr_rpm <= CONFIG_ZERO_SPEED_RPM) {
  128. torque_ctrl.torque_req = eCtrl_get_FinalTorque() * FINAL_DQ_CEFO;
  129. ref_torque = MAX(eCtrl_get_FinalTorque() * FINAL_DQ_CEFO, ref_torque);
  130. }
  131. if (ref_torque > torque_ctrl.torque_req) { //加扭矩step给定
  132. step_towards(&torque_ctrl.torque_req, ref_torque, 1.0f);
  133. }else { //减扭矩,直接给定
  134. torque_ctrl.torque_req = ref_torque;
  135. }
  136. PMSM_FOC_Set_Torque(torque_ctrl.torque_req);
  137. }else if (mc_throttle_released() && eCtrl_get_FinalTorque() != 0){
  138. float real_trq = PMSM_FOC_Get_Real_Torque() * REAL_DQ_CEOF;
  139. float ref_now = min(real_trq, eCtrl_get_RefTorque());
  140. eCtrl_reset_Torque(ref_now);
  141. PMSM_FOC_Set_Torque(0);
  142. }
  143. }
  144. void request_speed(float thro_ration) {
  145. float speed_Ref = PMSM_FOC_GetSpeedLimit() * thro_ration;
  146. PMSM_FOC_Set_Speed(speed_Ref);
  147. }
  148. #define THRO_REF_LP_CEOF 0.2f
  149. void throttle_process(u8 run_mode, float f_throttle) {
  150. if (mc_throttle_released()){
  151. torque_ctrl.throttle_value = 0;
  152. torque_ctrl.torque_req = 0;
  153. }else {
  154. LowPass_Filter(torque_ctrl.throttle_value, f_throttle, THRO_REF_LP_CEOF);
  155. }
  156. torque_ctrl.thro_ration = throttle_ration(f_throttle);
  157. if (run_mode == CTRL_MODE_TRQ) {
  158. request_torque(torque_ctrl.thro_ration);
  159. }else if (run_mode == CTRL_MODE_SPD) {
  160. request_speed(torque_ctrl.thro_ration);
  161. }else if (run_mode == CTRL_MODE_CURRENT_BRK) {
  162. eCtrl_reset_Torque(0);
  163. if (eCtrl_get_FinalCurrent() < 0.0001f && PMSM_FOC_GetSpeed() < CONFIG_MIN_RPM_EXIT_EBRAKE) {
  164. eCtrl_enable_eBrake(false);
  165. }
  166. if (!mc_throttle_released() || (mc_throttle_released() && (PMSM_FOC_GetSpeed() == 0.0f))) {
  167. eCtrl_enable_eBrake(false);
  168. }
  169. }
  170. }