torque.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. void torque_init(void) {
  13. _trq_tbl = nv_get_trq_tlb();
  14. }
  15. void torque_get_idq(float torque, float rpm, DQ_t *dq_out) {
  16. if ((_trq_tbl == NULL) || (torque < 0 || rpm < 0)) {
  17. dq_out->d = 0;
  18. dq_out->q = torque;
  19. return;
  20. }
  21. int trq_idx = (int)torque / TBL_TRQ_INTVAL;
  22. int rpm_idx = (int)rpm / TBL_SPD_INTVAL;
  23. if (trq_idx >= MAX_TRQ_POINTS) {
  24. trq_idx = MAX_TRQ_POINTS -1;
  25. }
  26. if (rpm_idx >= MAX_SPD_POINTS) {
  27. rpm_idx = MAX_SPD_POINTS -1;
  28. }
  29. s16 d = _trq_tbl->dq[trq_idx][rpm_idx].d;
  30. s16 q = _trq_tbl->dq[trq_idx][rpm_idx].q;
  31. if (trq_idx < MAX_TRQ_POINTS - 1) {
  32. trq_idx += 1;
  33. }
  34. if (rpm_idx < MAX_SPD_POINTS - 1) {
  35. rpm_idx += 1;
  36. }
  37. s16 d_delta = _trq_tbl->dq[trq_idx][rpm_idx].d - d;
  38. s16 q_delta = _trq_tbl->dq[trq_idx][rpm_idx].q - q;
  39. 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);
  40. dq_out->d = d + d_delta * comp_ceof;
  41. dq_out->q = q + q_delta * comp_ceof;
  42. }
  43. float speed_target_from_throttle(float f_throttle) {
  44. if (f_throttle <= nv_get_foc_params()->n_minThroVol) {
  45. return 0;
  46. }
  47. float delta = f_throttle - (nv_get_foc_params()->n_minThroVol);
  48. float ration = delta / (nv_get_foc_params()->n_maxThroVol - nv_get_foc_params()->n_minThroVol);
  49. return (PMSM_FOC_GetSpeedLimit() * ration);
  50. }
  51. float torque_target_from_throttle(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. float ration = delta / (nv_get_foc_params()->n_maxThroVol - nv_get_foc_params()->n_minThroVol);
  57. return (PMSM_FOC_GetTorqueLimit() * ration);
  58. }
  59. void torque_speed_target(u8 run_mode, float f_throttle) {
  60. if (run_mode == CTRL_MODE_SPD) {
  61. float speed_Ref = speed_target_from_throttle(f_throttle);
  62. if (mc_is_epm()) {
  63. if (speed_Ref == 0.0f) {
  64. mc_throttle_epm_move(EPM_Dir_None);
  65. }else {
  66. mc_throttle_epm_move(EPM_Dir_Forward);
  67. }
  68. }else {
  69. PMSM_FOC_Set_Speed(speed_Ref);
  70. }
  71. }else if (run_mode == CTRL_MODE_TRQ) {
  72. if (mc_throttle_released()) {
  73. eCtrl_enable_eBrake(true);
  74. PMSM_FOC_Set_Torque(0);
  75. }else {
  76. float torque = torque_target_from_throttle(f_throttle);
  77. eCtrl_enable_eBrake(false);
  78. PMSM_FOC_Set_Torque(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. }