torque.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /*
  9. 通过查表获取对应扭矩和速度时的Id和IQ的分配
  10. */
  11. static torque_lut_t *_trq_tbl = NULL;
  12. static torque_manager_t g_trq_mn;
  13. void torque_init(void) {
  14. _trq_tbl = nv_get_trq_tlb();
  15. torque_reset();
  16. }
  17. void torque_reset(void) {
  18. memset(&g_trq_mn, 0, sizeof(g_trq_mn));
  19. }
  20. void torque_get_idq(float torque, float rpm, DQ_t *dq_out) {
  21. if ((_trq_tbl == NULL) || (torque < 0 || rpm < 0)) {
  22. dq_out->d = 0;
  23. dq_out->q = torque;
  24. return;
  25. }
  26. int trq_idx = (int)torque / TBL_TRQ_INTVAL;
  27. int rpm_idx = (int)rpm / TBL_SPD_INTVAL;
  28. if (trq_idx >= MAX_TRQ_POINTS) {
  29. trq_idx = MAX_TRQ_POINTS -1;
  30. }
  31. if (rpm_idx >= MAX_SPD_POINTS) {
  32. rpm_idx = MAX_SPD_POINTS -1;
  33. }
  34. s16 d = _trq_tbl->dq[trq_idx][rpm_idx].d;
  35. s16 q = _trq_tbl->dq[trq_idx][rpm_idx].q;
  36. if (trq_idx < MAX_TRQ_POINTS - 1) {
  37. trq_idx += 1;
  38. }
  39. if (rpm_idx < MAX_SPD_POINTS - 1) {
  40. rpm_idx += 1;
  41. }
  42. s16 d_delta = _trq_tbl->dq[trq_idx][rpm_idx].d - d;
  43. s16 q_delta = _trq_tbl->dq[trq_idx][rpm_idx].q - q;
  44. 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);
  45. dq_out->d = d + d_delta * comp_ceof;
  46. dq_out->q = q + q_delta * comp_ceof;
  47. }
  48. /* 获取油门开度 */
  49. static float throttle_ration(float f_throttle) {
  50. if (f_throttle <= nv_get_foc_params()->n_minThroVol) {
  51. return 0;
  52. }
  53. float delta = f_throttle - (nv_get_foc_params()->n_minThroVol);
  54. int ration = (delta * 100.0f) / (nv_get_foc_params()->n_maxThroVol - nv_get_foc_params()->n_minThroVol);
  55. return ((float)ration)/100.0f;
  56. }
  57. float throttle_to_speed(float f_throttle) {
  58. return (PMSM_FOC_GetSpeedLimit() * throttle_ration(f_throttle));
  59. }
  60. float throttle_to_torque(float f_throttle) {
  61. return PMSM_FOC_GetTorqueLimit() * throttle_ration(f_throttle);
  62. }
  63. void torque_mode_process(float f_throttle) {
  64. float thro_curr = throttle_ration(f_throttle);
  65. float thro_prev = g_trq_mn.thro_prev;
  66. float trq_ref = throttle_to_torque(f_throttle);
  67. if (mc_throttle_released()) {
  68. if ((eCtrl_get_FinalTorque() > 0.0f)) {
  69. eCtrl_enable_eBrake(true);
  70. float ref_now = min(PMSM_FOC_Get_Real_Torque() * 0.9f, eCtrl_get_RefTorque());
  71. eCtrl_reset_Torque(ref_now);
  72. PMSM_FOC_Set_Torque(0);
  73. g_trq_mn.accl = false;
  74. g_trq_mn.thro_prev = 0.0f;
  75. }
  76. return;
  77. }
  78. if (thro_curr > thro_prev) {
  79. if (PMSM_FOC_GetSpeed() == 0.0f) {
  80. g_trq_mn.accl_ref = MAX(eCtrl_get_FinalTorque(), trq_ref); //不能小于autohold产生的扭矩
  81. }else if (!g_trq_mn.accl){
  82. g_trq_mn.accl_ref = eCtrl_get_RefTorque();
  83. }
  84. if (g_trq_mn.accl_ref > PMSM_FOC_GetTorqueLimit()) {
  85. g_trq_mn.accl_ref = PMSM_FOC_GetTorqueLimit();
  86. }
  87. trq_ref = g_trq_mn.accl_ref + thro_curr * (PMSM_FOC_GetTorqueLimit() - g_trq_mn.accl_ref);
  88. g_trq_mn.accl = true;
  89. }else if (thro_curr < thro_prev){
  90. if (g_trq_mn.accl) {
  91. g_trq_mn.accl_ref = min(PMSM_FOC_Get_Real_Torque() * 0.9f, eCtrl_get_RefTorque());
  92. eCtrl_reset_Torque(g_trq_mn.accl_ref);
  93. }
  94. trq_ref = thro_curr * g_trq_mn.accl_ref;
  95. g_trq_mn.accl = false;
  96. }
  97. PMSM_FOC_Set_Torque(trq_ref);
  98. g_trq_mn.thro_prev = thro_curr;
  99. }
  100. void speed_mode_process(float f_throttle) {
  101. float speed_Ref = throttle_to_speed(f_throttle);
  102. PMSM_FOC_Set_Speed(speed_Ref);
  103. }
  104. void throttle_process(u8 run_mode, float f_throttle) {
  105. if ((g_trq_mn.count++ % 20) != 0) {
  106. return;
  107. }
  108. if (run_mode == CTRL_MODE_TRQ) {
  109. torque_mode_process(f_throttle);
  110. }else if (run_mode == CTRL_MODE_SPD) {
  111. speed_mode_process(f_throttle);
  112. }else if (run_mode == CTRL_MODE_CURRENT_BRK) {
  113. if (!mc_throttle_released() || (mc_throttle_released() && (PMSM_FOC_GetSpeed() == 0.0f))) {
  114. eCtrl_enable_eBrake(false);
  115. }
  116. }
  117. }
  118. /*
  119. void torque_manager(u8 run_mode, float f_throttle) {
  120. if ((g_trq_mn.count++ % 20) != 0) {
  121. return;
  122. }
  123. if (run_mode == CTRL_MODE_SPD) {
  124. float speed_Ref = throttle_to_speed(f_throttle);
  125. PMSM_FOC_Set_Speed(speed_Ref);
  126. }else if (run_mode == CTRL_MODE_TRQ) {
  127. if (mc_throttle_released()) {
  128. eCtrl_enable_eBrake(true);
  129. PMSM_FOC_Set_Torque(0);
  130. g_trq_mn.torque_prev = 0;
  131. }else {
  132. float torque = throttle_to_torque(f_throttle);
  133. eCtrl_enable_eBrake(false);
  134. PMSM_FOC_Set_Torque(torque);
  135. g_trq_mn.torque_prev = torque;
  136. }
  137. }else if (run_mode == CTRL_MODE_CURRENT_BRK) {
  138. if (!mc_throttle_released() || (mc_throttle_released() && (PMSM_FOC_GetSpeed() == 0.0f))) {
  139. eCtrl_enable_eBrake(false);
  140. }
  141. }
  142. } */