torque.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. /* 软件设置油门开度,调试使用 */
  65. void throttle_set_ration(float r) {
  66. g_trq_mn.thro_value = r;
  67. }
  68. #define REAL_DQ_CEOF 0.9f
  69. #define FINAL_DQ_CEFO 1.1F
  70. void torque_mode_process(void) {
  71. float ref_trq = PMSM_FOC_GetTorqueLimit() * g_trq_mn.thro_value;
  72. if ((mc_throttle_released()) && eCtrl_enable_eBrake(true)) {
  73. g_trq_mn.thro_value = 0;
  74. g_trq_mn.torque_prev = 0;
  75. g_trq_mn.torque_base = 0;
  76. return;
  77. }
  78. if (!mc_throttle_released()) {
  79. if (PMSM_FOC_GetSpeed() <= CONFIG_ZERO_SPEED_RPM) {
  80. ref_trq = MAX(eCtrl_get_FinalTorque() * FINAL_DQ_CEFO, ref_trq );
  81. g_trq_mn.torque_base = ref_trq;
  82. }
  83. PMSM_FOC_Set_Torque(ref_trq );
  84. g_trq_mn.torque_prev = ref_trq;
  85. }else if (mc_throttle_released() && eCtrl_get_FinalTorque() != 0){
  86. float real_trq = PMSM_FOC_Get_Real_Torque() * REAL_DQ_CEOF;
  87. float ref_now = min(real_trq, eCtrl_get_RefTorque());
  88. eCtrl_reset_Torque(ref_now);
  89. PMSM_FOC_Set_Torque(0);
  90. g_trq_mn.thro_value = 0;
  91. g_trq_mn.torque_prev = 0;
  92. g_trq_mn.torque_base = 0;
  93. }
  94. }
  95. void speed_mode_process(void) {
  96. float speed_Ref = g_trq_mn.spd_ref;
  97. PMSM_FOC_Set_Speed(speed_Ref);
  98. }
  99. #define THRO_REF_LP_CEOF 0.2f
  100. void throttle_process(u8 run_mode, float f_throttle) {
  101. if (run_mode == CTRL_MODE_TRQ) {
  102. float thro_value = throttle_ration(f_throttle);
  103. g_trq_mn.thro_value = LowPass_Filter(g_trq_mn.thro_value, thro_value, THRO_REF_LP_CEOF);
  104. if ((g_trq_mn.count++ % 20) == 0) {
  105. torque_mode_process();
  106. }
  107. }else if (run_mode == CTRL_MODE_SPD) {
  108. float spd_ref = throttle_to_speed(f_throttle);
  109. g_trq_mn.spd_ref = LowPass_Filter(g_trq_mn.spd_ref, spd_ref, THRO_REF_LP_CEOF);
  110. if ((g_trq_mn.count++ % 20) == 0) {
  111. speed_mode_process();
  112. }
  113. }else if (run_mode == CTRL_MODE_CURRENT_BRK) {
  114. eCtrl_reset_Torque(0);
  115. if (eCtrl_get_FinalCurrent() < 0.0001f && PMSM_FOC_GetSpeed() < CONFIG_MIN_RPM_EXIT_EBRAKE) {
  116. eCtrl_enable_eBrake(false);
  117. }
  118. if (!mc_throttle_released() || (mc_throttle_released() && (PMSM_FOC_GetSpeed() == 0.0f))) {
  119. eCtrl_enable_eBrake(false);
  120. }
  121. }
  122. }