torque.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. /*
  7. 通过查表获取对应扭矩和速度时的Id和IQ的分配
  8. */
  9. void torque_init(void) {
  10. }
  11. void torque_get_idq(float torque, float rpm, DQ_t *dq_out) {
  12. dq_out->d = 0;
  13. dq_out->q = torque;
  14. }
  15. float speed_target_from_throttle(float f_throttle) {
  16. if (f_throttle <= (CONFIG_THROTTLE_LOW_VALUE)) {
  17. return 0;
  18. }
  19. float delta = f_throttle - (CONFIG_THROTTLE_LOW_VALUE);
  20. float ration = delta / (CONFIG_THROTTLE_MAX_VALUE - CONFIG_THROTTLE_LOW_VALUE);
  21. return (PMSM_FOC_GetSpeedLimit() * ration);
  22. }
  23. float torque_target_from_throttle(float f_throttle) {
  24. if (f_throttle <= (CONFIG_THROTTLE_LOW_VALUE)) {
  25. return 0;
  26. }
  27. float delta = f_throttle - (CONFIG_THROTTLE_LOW_VALUE);
  28. float ration = delta / (CONFIG_THROTTLE_MAX_VALUE - CONFIG_THROTTLE_LOW_VALUE);
  29. return (PMSM_FOC_GetTorqueLimit() * ration);
  30. }
  31. void torque_speed_target(u8 run_mode, float f_throttle) {
  32. if (run_mode == CTRL_MODE_SPD) {
  33. float speed_Ref = speed_target_from_throttle(f_throttle);
  34. if (PMSM_FOC_is_epmMode()) {
  35. if (speed_Ref == 0.0f) {
  36. PMSM_FOC_Start_epmMove(false, EPM_Dir_None);
  37. }else {
  38. PMSM_FOC_Start_epmMove(true, EPM_Dir_Forward);
  39. }
  40. }
  41. PMSM_FOC_Set_Speed(speed_Ref);
  42. }else if (run_mode == CTRL_MODE_TRQ) {
  43. if (mc_throttle_released()) {
  44. eCtrl_enable_eBrake(true);
  45. PMSM_FOC_Set_Torque(0);
  46. }else {
  47. float torque = torque_target_from_throttle(f_throttle);
  48. eCtrl_enable_eBrake(false);
  49. PMSM_FOC_Set_Torque(torque);
  50. }
  51. }else if (run_mode == CTRL_MODE_CURRENT_BRK) {
  52. if (!mc_throttle_released() || (mc_throttle_released() && (PMSM_FOC_GetSpeed() == 0.0f))) {
  53. eCtrl_enable_eBrake(false);
  54. }
  55. }
  56. }