torque.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. memset(&g_trq_mn, 0, sizeof(g_trq_mn));
  16. }
  17. void torque_get_idq(float torque, float rpm, DQ_t *dq_out) {
  18. if ((_trq_tbl == NULL) || (torque < 0 || rpm < 0)) {
  19. dq_out->d = 0;
  20. dq_out->q = torque;
  21. return;
  22. }
  23. int trq_idx = (int)torque / TBL_TRQ_INTVAL;
  24. int rpm_idx = (int)rpm / TBL_SPD_INTVAL;
  25. if (trq_idx >= MAX_TRQ_POINTS) {
  26. trq_idx = MAX_TRQ_POINTS -1;
  27. }
  28. if (rpm_idx >= MAX_SPD_POINTS) {
  29. rpm_idx = MAX_SPD_POINTS -1;
  30. }
  31. s16 d = _trq_tbl->dq[trq_idx][rpm_idx].d;
  32. s16 q = _trq_tbl->dq[trq_idx][rpm_idx].q;
  33. if (trq_idx < MAX_TRQ_POINTS - 1) {
  34. trq_idx += 1;
  35. }
  36. if (rpm_idx < MAX_SPD_POINTS - 1) {
  37. rpm_idx += 1;
  38. }
  39. s16 d_delta = _trq_tbl->dq[trq_idx][rpm_idx].d - d;
  40. s16 q_delta = _trq_tbl->dq[trq_idx][rpm_idx].q - q;
  41. 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);
  42. dq_out->d = d + d_delta * comp_ceof;
  43. dq_out->q = q + q_delta * comp_ceof;
  44. }
  45. float throttle_to_speed(float f_throttle) {
  46. if (f_throttle <= nv_get_foc_params()->n_minThroVol) {
  47. return 0;
  48. }
  49. float delta = f_throttle - (nv_get_foc_params()->n_minThroVol);
  50. float ration = delta / (nv_get_foc_params()->n_maxThroVol - nv_get_foc_params()->n_minThroVol);
  51. return (PMSM_FOC_GetSpeedLimit() * ration);
  52. }
  53. float throttle_to_torque(float f_throttle) {
  54. if (f_throttle <= (nv_get_foc_params()->n_minThroVol)) {
  55. return 0;
  56. }
  57. float delta = f_throttle - (nv_get_foc_params()->n_minThroVol);
  58. float ration = delta / (nv_get_foc_params()->n_maxThroVol - nv_get_foc_params()->n_minThroVol);
  59. int torque = PMSM_FOC_GetTorqueLimit() * ration * 10.0f;
  60. return ((float)torque / 10.0f);
  61. }
  62. void torque_manager(u8 run_mode, float f_throttle) {
  63. if ((g_trq_mn.count++ % 20) != 0) {
  64. return;
  65. }
  66. if (run_mode == CTRL_MODE_SPD) {
  67. float speed_Ref = throttle_to_speed(f_throttle);
  68. PMSM_FOC_Set_Speed(speed_Ref);
  69. }else if (run_mode == CTRL_MODE_TRQ) {
  70. if (mc_throttle_released()) {
  71. eCtrl_enable_eBrake(true);
  72. PMSM_FOC_Set_Torque(0);
  73. g_trq_mn.torque_prev = 0;
  74. }else {
  75. float torque = throttle_to_torque(f_throttle);
  76. eCtrl_enable_eBrake(false);
  77. PMSM_FOC_Set_Torque(torque);
  78. g_trq_mn.torque_prev = torque;
  79. }
  80. }else if (run_mode == CTRL_MODE_CURRENT_BRK) {
  81. if (!mc_throttle_released() || (mc_throttle_released() && (PMSM_FOC_GetSpeed() == 0.0f))) {
  82. eCtrl_enable_eBrake(false);
  83. }
  84. }
  85. }