torque.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /*
  8. 通过查表获取对应扭矩和速度时的Id和IQ的分配
  9. */
  10. static torque_lut_t *_trq_tbl = NULL;
  11. void torque_init(void) {
  12. _trq_tbl = nv_get_trq_tlb();
  13. }
  14. void torque_get_idq(float torque, float rpm, DQ_t *dq_out) {
  15. if ((_trq_tbl == NULL) || (torque < 0 || rpm < 0)) {
  16. dq_out->d = 0;
  17. dq_out->q = torque;
  18. return;
  19. }
  20. int trq_idx = (int)torque / TBL_TRQ_INTVAL;
  21. int rpm_idx = (int)rpm / TBL_SPD_INTVAL;
  22. if (trq_idx >= MAX_TRQ_POINTS) {
  23. trq_idx = MAX_TRQ_POINTS -1;
  24. }
  25. if (rpm_idx >= MAX_SPD_POINTS) {
  26. rpm_idx = MAX_SPD_POINTS -1;
  27. }
  28. s16 d = _trq_tbl->dq[trq_idx][rpm_idx].d;
  29. s16 q = _trq_tbl->dq[trq_idx][rpm_idx].q;
  30. if (trq_idx < MAX_TRQ_POINTS - 1) {
  31. trq_idx += 1;
  32. }
  33. if (rpm_idx < MAX_SPD_POINTS - 1) {
  34. rpm_idx += 1;
  35. }
  36. s16 d_delta = _trq_tbl->dq[trq_idx][rpm_idx].d - d;
  37. s16 q_delta = _trq_tbl->dq[trq_idx][rpm_idx].q - q;
  38. 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);
  39. dq_out->d = d + d_delta * comp_ceof;
  40. dq_out->q = q + q_delta * comp_ceof;
  41. }
  42. float speed_target_from_throttle(float f_throttle) {
  43. if (f_throttle <= (CONFIG_THROTTLE_LOW_VALUE)) {
  44. return 0;
  45. }
  46. float delta = f_throttle - (CONFIG_THROTTLE_LOW_VALUE);
  47. float ration = delta / (CONFIG_THROTTLE_MAX_VALUE - CONFIG_THROTTLE_LOW_VALUE);
  48. return (PMSM_FOC_GetSpeedLimit() * ration);
  49. }
  50. float torque_target_from_throttle(float f_throttle) {
  51. if (f_throttle <= (CONFIG_THROTTLE_LOW_VALUE)) {
  52. return 0;
  53. }
  54. float delta = f_throttle - (CONFIG_THROTTLE_LOW_VALUE);
  55. float ration = delta / (CONFIG_THROTTLE_MAX_VALUE - CONFIG_THROTTLE_LOW_VALUE);
  56. return (PMSM_FOC_GetTorqueLimit() * ration);
  57. }
  58. void torque_speed_target(u8 run_mode, float f_throttle) {
  59. if (run_mode == CTRL_MODE_SPD) {
  60. float speed_Ref = speed_target_from_throttle(f_throttle);
  61. if (mc_is_epm()) {
  62. if (speed_Ref == 0.0f) {
  63. mc_throttle_epm_move(EPM_Dir_None);
  64. }else {
  65. mc_throttle_epm_move(EPM_Dir_Forward);
  66. }
  67. }else {
  68. PMSM_FOC_Set_Speed(speed_Ref);
  69. }
  70. }else if (run_mode == CTRL_MODE_TRQ) {
  71. if (mc_throttle_released()) {
  72. eCtrl_enable_eBrake(true);
  73. PMSM_FOC_Set_Torque(0);
  74. }else {
  75. float torque = torque_target_from_throttle(f_throttle);
  76. eCtrl_enable_eBrake(false);
  77. PMSM_FOC_Set_Torque(torque);
  78. }
  79. }else if (run_mode == CTRL_MODE_CURRENT_BRK) {
  80. if (!mc_throttle_released() || (mc_throttle_released() && (PMSM_FOC_GetSpeed() == 0.0f))) {
  81. eCtrl_enable_eBrake(false);
  82. }
  83. }
  84. }