torque.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "foc/core/torque.h"
  2. #include "foc/foc_config.h"
  3. #include "foc/motor/motor.h"
  4. #include "foc/core/e_ctrl.h"
  5. #include "foc/core/PMSM_FOC_Core.h"
  6. #include "app/nv_storage.h"
  7. #include "libs/logger.h"
  8. #include "prot/can_foc_msg.h"
  9. /*
  10. 通过查表获取对应扭矩和速度时的Id和IQ的分配
  11. */
  12. static torque_lut_t *_trq_tbl = NULL;
  13. static torque_manager_t g_trq_mn;
  14. void torque_init(void) {
  15. _trq_tbl = nv_get_trq_tlb();
  16. torque_reset();
  17. }
  18. void torque_reset(void) {
  19. memset(&g_trq_mn, 0, sizeof(g_trq_mn));
  20. }
  21. void torque_get_idq(float torque, float rpm, DQ_t *dq_out) {
  22. if ((_trq_tbl == NULL) || (torque < 0 || rpm < 0)) {
  23. dq_out->d = 0;
  24. dq_out->q = torque;
  25. return;
  26. }
  27. int trq_idx = (int)torque / TBL_TRQ_INTVAL;
  28. int rpm_idx = (int)rpm / TBL_SPD_INTVAL;
  29. if (trq_idx >= MAX_TRQ_POINTS) {
  30. trq_idx = MAX_TRQ_POINTS -1;
  31. }
  32. if (rpm_idx >= MAX_SPD_POINTS) {
  33. rpm_idx = MAX_SPD_POINTS -1;
  34. }
  35. s16 d = _trq_tbl->dq[trq_idx][rpm_idx].d;
  36. s16 q = _trq_tbl->dq[trq_idx][rpm_idx].q;
  37. if (trq_idx < MAX_TRQ_POINTS - 1) {
  38. trq_idx += 1;
  39. }
  40. if (rpm_idx < MAX_SPD_POINTS - 1) {
  41. rpm_idx += 1;
  42. }
  43. s16 d_delta = _trq_tbl->dq[trq_idx][rpm_idx].d - d;
  44. s16 q_delta = _trq_tbl->dq[trq_idx][rpm_idx].q - q;
  45. 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);
  46. dq_out->d = d + d_delta * comp_ceof;
  47. dq_out->q = q + q_delta * comp_ceof;
  48. }
  49. /* 获取油门开度 */
  50. static float throttle_ration(float f_throttle) {
  51. if (f_throttle <= nv_get_foc_params()->n_minThroVol) {
  52. return 0;
  53. }
  54. float delta = f_throttle - (nv_get_foc_params()->n_minThroVol);
  55. int ration = (delta * 1000.0f) / (nv_get_foc_params()->n_maxThroVol - nv_get_foc_params()->n_minThroVol);
  56. return ((float)ration)/1000.0f;
  57. }
  58. float throttle_to_speed(float f_throttle) {
  59. return (PMSM_FOC_GetSpeedLimit() * throttle_ration(f_throttle));
  60. }
  61. float throttle_to_torque(float f_throttle) {
  62. return PMSM_FOC_GetTorqueLimit() * throttle_ration(f_throttle);
  63. }
  64. #define REAL_DQ_CEOF 0.9f
  65. #define FINAL_DQ_CEFO 1.1F
  66. #define START_RPM_FOR_AUDOHOLD 10.0F
  67. #if 1
  68. void torque_mode_process(void) {
  69. float ref_trq = PMSM_FOC_GetTorqueLimit() * g_trq_mn.thro_value;
  70. if ((mc_throttle_released()) && eCtrl_enable_eBrake(true)) {
  71. g_trq_mn.thro_value = 0;
  72. g_trq_mn.torque_prev = 0;
  73. g_trq_mn.torque_base = 0;
  74. return;
  75. }
  76. if (!mc_throttle_released()) {
  77. if (PMSM_FOC_GetSpeed() <= START_RPM_FOR_AUDOHOLD) {
  78. ref_trq = MAX(eCtrl_get_FinalTorque() * FINAL_DQ_CEFO, ref_trq );
  79. g_trq_mn.torque_base = ref_trq;
  80. }
  81. PMSM_FOC_Set_Torque(ref_trq );
  82. g_trq_mn.torque_prev = ref_trq;
  83. }else if (mc_throttle_released() && eCtrl_get_FinalTorque() != 0){
  84. float real_trq = PMSM_FOC_Get_Real_Torque() * REAL_DQ_CEOF;
  85. float ref_now = min(real_trq, eCtrl_get_RefTorque());
  86. eCtrl_reset_Torque(ref_now);
  87. PMSM_FOC_Set_Torque(0);
  88. g_trq_mn.thro_value = 0;
  89. g_trq_mn.torque_prev = 0;
  90. g_trq_mn.torque_base = 0;
  91. }
  92. }
  93. #else
  94. void torque_mode_process(void) {
  95. float thro_curr = g_trq_mn.thro_value;
  96. float thro_prev = g_trq_mn.thro_prev;
  97. float trq_ref = PMSM_FOC_GetTorqueLimit() * thro_curr;
  98. if ((thro_curr == 0.0f) && eCtrl_enable_eBrake(true)) {
  99. g_trq_mn.thro_value = 0;
  100. g_trq_mn.accl = false;
  101. g_trq_mn.thro_prev = 0.0f;
  102. return;
  103. }
  104. float real_trq = eCtrl_get_FinalTorque() * REAL_DQ_CEOF;
  105. if (trq_ref > 0) {
  106. if (PMSM_FOC_GetSpeed() <= 10.0f) {
  107. g_trq_mn.accl_ref = MAX(real_trq, trq_ref); //不能小于autohold产生的扭矩
  108. trq_ref = g_trq_mn.accl_ref;
  109. }else {
  110. if (thro_curr > thro_prev) {
  111. if (!g_trq_mn.accl){
  112. g_trq_mn.accl_ref = eCtrl_get_RefTorque();
  113. }
  114. if (g_trq_mn.accl_ref > PMSM_FOC_GetTorqueLimit()) {
  115. g_trq_mn.accl_ref = PMSM_FOC_GetTorqueLimit();
  116. }
  117. trq_ref = g_trq_mn.accl_ref + thro_curr * (PMSM_FOC_GetTorqueLimit() - g_trq_mn.accl_ref);
  118. g_trq_mn.accl = true;
  119. }else if (thro_curr < thro_prev){
  120. if (g_trq_mn.accl) {
  121. g_trq_mn.accl_ref = min(real_trq, eCtrl_get_RefTorque());
  122. eCtrl_reset_Torque(g_trq_mn.accl_ref);
  123. }
  124. trq_ref = thro_curr * g_trq_mn.accl_ref;
  125. g_trq_mn.accl = false;
  126. }else {
  127. if (g_trq_mn.accl) {
  128. trq_ref = g_trq_mn.accl_ref + thro_curr * (PMSM_FOC_GetTorqueLimit() - g_trq_mn.accl_ref);
  129. }else {
  130. trq_ref = thro_curr * g_trq_mn.accl_ref;
  131. }
  132. }
  133. }
  134. PMSM_FOC_Set_Torque(trq_ref);
  135. }
  136. else if (eCtrl_get_FinalTorque() != 0){
  137. float ref_now = min(real_trq, eCtrl_get_RefTorque());
  138. eCtrl_reset_Torque(ref_now);
  139. PMSM_FOC_Set_Torque(0);
  140. g_trq_mn.thro_value = 0;
  141. }
  142. g_trq_mn.thro_prev = thro_curr;
  143. }
  144. #endif
  145. void speed_mode_process(void) {
  146. float speed_Ref = g_trq_mn.spd_ref;
  147. PMSM_FOC_Set_Speed(speed_Ref);
  148. }
  149. #define THRO_REF_LP_CEOF 0.2f
  150. void throttle_process(u8 run_mode, float f_throttle) {
  151. if (run_mode == CTRL_MODE_TRQ) {
  152. float thro_value = throttle_ration(f_throttle);
  153. g_trq_mn.thro_value = LowPass_Filter(g_trq_mn.thro_value, thro_value, THRO_REF_LP_CEOF);
  154. if ((g_trq_mn.count++ % 20) == 0) {
  155. torque_mode_process();
  156. }
  157. }else if (run_mode == CTRL_MODE_SPD) {
  158. float spd_ref = throttle_to_speed(f_throttle);
  159. g_trq_mn.spd_ref = LowPass_Filter(g_trq_mn.spd_ref, spd_ref, THRO_REF_LP_CEOF);
  160. if ((g_trq_mn.count++ % 20) == 0) {
  161. speed_mode_process();
  162. }
  163. }else if (run_mode == CTRL_MODE_CURRENT_BRK) {
  164. eCtrl_reset_Torque(0);
  165. if (eCtrl_get_FinalCurrent() < 0.0001f && PMSM_FOC_GetSpeed() < CONFIG_MIN_RPM_EXIT_EBRAKE) {
  166. eCtrl_enable_eBrake(false);
  167. }
  168. if (!mc_throttle_released() || (mc_throttle_released() && (PMSM_FOC_GetSpeed() == 0.0f))) {
  169. eCtrl_enable_eBrake(false);
  170. }
  171. }
  172. }
  173. /*
  174. void torque_manager(u8 run_mode, float f_throttle) {
  175. if ((g_trq_mn.count++ % 20) != 0) {
  176. return;
  177. }
  178. if (run_mode == CTRL_MODE_SPD) {
  179. float speed_Ref = throttle_to_speed(f_throttle);
  180. PMSM_FOC_Set_Speed(speed_Ref);
  181. }else if (run_mode == CTRL_MODE_TRQ) {
  182. if (mc_throttle_released()) {
  183. eCtrl_enable_eBrake(true);
  184. PMSM_FOC_Set_Torque(0);
  185. g_trq_mn.torque_prev = 0;
  186. }else {
  187. float torque = throttle_to_torque(f_throttle);
  188. eCtrl_enable_eBrake(false);
  189. PMSM_FOC_Set_Torque(torque);
  190. g_trq_mn.torque_prev = torque;
  191. }
  192. }else if (run_mode == CTRL_MODE_CURRENT_BRK) {
  193. if (!mc_throttle_released() || (mc_throttle_released() && (PMSM_FOC_GetSpeed() == 0.0f))) {
  194. eCtrl_enable_eBrake(false);
  195. }
  196. }
  197. } */