torque.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /*
  11. 通过查表获取对应扭矩和速度时的Id和IQ的分配
  12. */
  13. static torque_lut_t *_trq_tbl = NULL;
  14. static torque_manager_t g_trq_mn;
  15. void torque_init(void) {
  16. _trq_tbl = nv_get_trq_tlb();
  17. torque_reset();
  18. }
  19. void torque_reset(void) {
  20. memset(&g_trq_mn, 0, sizeof(g_trq_mn));
  21. }
  22. void torque_get_idq(float torque, float rpm, DQ_t *dq_out) {
  23. if ((_trq_tbl == NULL) || (torque < 0 || rpm < 0)) {
  24. dq_out->d = 0;
  25. dq_out->q = torque;
  26. return;
  27. }
  28. int trq_idx = (int)torque / TBL_TRQ_INTVAL;
  29. int rpm_idx = (int)rpm / TBL_SPD_INTVAL;
  30. if (trq_idx >= MAX_TRQ_POINTS) {
  31. trq_idx = MAX_TRQ_POINTS -1;
  32. }
  33. if (rpm_idx >= MAX_SPD_POINTS) {
  34. rpm_idx = MAX_SPD_POINTS -1;
  35. }
  36. s16 d = _trq_tbl->dq[trq_idx][rpm_idx].d;
  37. s16 q = _trq_tbl->dq[trq_idx][rpm_idx].q;
  38. if (trq_idx < MAX_TRQ_POINTS - 1) {
  39. trq_idx += 1;
  40. }
  41. if (rpm_idx < MAX_SPD_POINTS - 1) {
  42. rpm_idx += 1;
  43. }
  44. s16 d_delta = _trq_tbl->dq[trq_idx][rpm_idx].d - d;
  45. s16 q_delta = _trq_tbl->dq[trq_idx][rpm_idx].q - q;
  46. 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);
  47. dq_out->d = d + d_delta * comp_ceof;
  48. dq_out->q = q + q_delta * comp_ceof;
  49. }
  50. /* 获取油门开度 */
  51. static float throttle_ration(float f_throttle) {
  52. if (f_throttle <= nv_get_foc_params()->n_minThroVol) {
  53. return 0;
  54. }
  55. float delta = f_throttle - (nv_get_foc_params()->n_minThroVol);
  56. int ration = (delta * 1000.0f) / (nv_get_foc_params()->n_maxThroVol - nv_get_foc_params()->n_minThroVol);
  57. return ((float)ration)/1000.0f;
  58. }
  59. float throttle_to_speed(float f_throttle) {
  60. return (PMSM_FOC_GetSpeedLimit() * throttle_ration(f_throttle));
  61. }
  62. float throttle_to_torque(float f_throttle) {
  63. return PMSM_FOC_GetTorqueLimit() * throttle_ration(f_throttle);
  64. }
  65. #define REAL_DQ_CEOF 0.9f
  66. #define FINAL_DQ_CEFO 1.1F
  67. void request_torque(float thro_ration) {
  68. float curr_rpm = PMSM_FOC_GetSpeed();
  69. if (curr_rpm == 0) {
  70. g_trq_mn.spd_filted = 0;
  71. }else {
  72. LowPass_Filter(g_trq_mn.spd_filted, curr_rpm, 0.5f);
  73. }
  74. float trq_map = (float)get_max_torque_for_rpm((s16)g_trq_mn.spd_filted);
  75. float trq_lim = PMSM_FOC_GetTorqueLimit();
  76. float max_trq = min(trq_map, trq_lim);
  77. float ref_trq = max_trq * thro_ration;
  78. if ((mc_throttle_released()) && eCtrl_enable_eBrake(true)) {
  79. g_trq_mn.thro_ration = 0;
  80. return;
  81. }
  82. if (!mc_throttle_released()) {
  83. if (curr_rpm <= CONFIG_ZERO_SPEED_RPM) {
  84. ref_trq = MAX(eCtrl_get_FinalTorque() * FINAL_DQ_CEFO, ref_trq );
  85. }
  86. PMSM_FOC_Set_Torque(ref_trq );
  87. }else if (mc_throttle_released() && eCtrl_get_FinalTorque() != 0){
  88. float real_trq = PMSM_FOC_Get_Real_Torque() * REAL_DQ_CEOF;
  89. float ref_now = min(real_trq, eCtrl_get_RefTorque());
  90. eCtrl_reset_Torque(ref_now);
  91. PMSM_FOC_Set_Torque(0);
  92. g_trq_mn.thro_ration = 0;
  93. }
  94. }
  95. void request_speed(float thro_ration) {
  96. float speed_Ref = PMSM_FOC_GetSpeedLimit() * thro_ration;
  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. float thro_value = throttle_ration(f_throttle);
  102. g_trq_mn.thro_ration = LowPass_Filter(g_trq_mn.thro_ration, thro_value, THRO_REF_LP_CEOF);
  103. if (run_mode == CTRL_MODE_TRQ) {
  104. request_torque(g_trq_mn.thro_ration);
  105. }else if (run_mode == CTRL_MODE_SPD) {
  106. request_speed(g_trq_mn.thro_ration);
  107. }else if (run_mode == CTRL_MODE_CURRENT_BRK) {
  108. eCtrl_reset_Torque(0);
  109. if (eCtrl_get_FinalCurrent() < 0.0001f && PMSM_FOC_GetSpeed() < CONFIG_MIN_RPM_EXIT_EBRAKE) {
  110. eCtrl_enable_eBrake(false);
  111. }
  112. if (!mc_throttle_released() || (mc_throttle_released() && (PMSM_FOC_GetSpeed() == 0.0f))) {
  113. eCtrl_enable_eBrake(false);
  114. }
  115. }
  116. }