motor.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "foc/motor/motor.h"
  2. #include "foc/motor/current.h"
  3. #include "foc/core/foc_core.h"
  4. #include "foc/foc_config.h"
  5. #include "foc/samples.h"
  6. #include "math/fast_math.h"
  7. #include "bsp/delay.h"
  8. #include "bsp/bsp.h"
  9. #include "bsp/adc.h"
  10. #include "bsp/pwm.h"
  11. static bool _motor_started = false;
  12. static float _motor_throttle = 0;
  13. bool mc_start(u8 mode) {
  14. if (_motor_started) {
  15. return true;
  16. }
  17. if (mode > TRQ_MODE) {
  18. PMSM_FOC_SetErrCode(FOC_Param_Err);
  19. return false;
  20. }
  21. if (PMSM_FOC_GetSpeed() > 10) {
  22. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  23. return false;
  24. }
  25. if (!mc_throttle_released()) {
  26. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  27. return false;
  28. }
  29. pwm_turn_on_low_side();
  30. task_udelay(500);
  31. phase_current_calibrate();
  32. PMSM_FOC_Start(mode);
  33. pwm_start();
  34. adc_start_convert();
  35. _motor_throttle = 0;
  36. _motor_started = true;
  37. return true;
  38. }
  39. bool mc_stop(void) {
  40. if (!_motor_started) {
  41. return true;
  42. }
  43. if (PMSM_FOC_GetSpeed() > 10) {
  44. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  45. return false;
  46. }
  47. if (!mc_throttle_released()) {
  48. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  49. return false;
  50. }
  51. adc_stop_convert();
  52. pwm_stop();
  53. PMSM_FOC_Stop();
  54. _motor_started = false;
  55. return true;
  56. }
  57. bool mc_lock_motor(bool lock) {
  58. if (lock && (PMSM_FOC_GetSpeed() > 10)) {
  59. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  60. return false;
  61. }
  62. if (lock) {
  63. adc_stop_convert();
  64. pwm_stop();
  65. }else {
  66. pwm_start();
  67. adc_start_convert();
  68. }
  69. return true;
  70. }
  71. bool mc_throttle_released(void) {
  72. return get_throttle_float() < THROTTLE_LOW_VALUE;
  73. }
  74. static float _get_speed_from_throttle(float throttle) {
  75. float f_throttle = (throttle);
  76. if (f_throttle <= (THROTTLE_LOW_VALUE)) {
  77. return 0;
  78. }
  79. float delta = f_throttle - (THROTTLE_LOW_VALUE);
  80. float ration = delta / (THROTTLE_MAX_VALUE - THROTTLE_LOW_VALUE);
  81. return (THROTTLE_MIN_RPM + PMSM_FOC_GetSpeedLimit() * ration);
  82. }
  83. static float _get_idq_from_throttle(float throttle) {
  84. float f_throttle = (throttle);
  85. if (f_throttle <= (THROTTLE_LOW_VALUE)) {
  86. return 0;
  87. }
  88. float delta = f_throttle - (THROTTLE_LOW_VALUE);
  89. float ration = delta / (THROTTLE_MAX_VALUE - THROTTLE_LOW_VALUE);
  90. return (THROTTLE_MIN_IDQ + MAX_iDQ * ration);
  91. }
  92. static void _brake_io_process(void) {
  93. bool brake = gpio_get_brake();
  94. for (int i = 0; i < 10; i++) {
  95. if (brake != gpio_get_brake()) {
  96. return;
  97. }
  98. }
  99. PMSM_FOC_Brake(brake);
  100. }
  101. u32 mc_main_task(void *param) {
  102. if (_motor_started) {
  103. if (get_throttle_float() != _motor_throttle) {
  104. _motor_throttle = get_throttle_float();
  105. float speed_Ref = _get_speed_from_throttle(_motor_throttle);
  106. PMSM_FOC_Set_Speed(speed_Ref);
  107. float torque_idq = _get_idq_from_throttle(_motor_throttle);
  108. PMSM_FOC_Set_Current(torque_idq);
  109. }
  110. _brake_io_process();
  111. }
  112. return 0;
  113. }