foc_fsm.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <string.h>
  2. #include "bsp/bsp.h"
  3. #include "bsp/pwm.h"
  4. #include "foc/core/foc_api.h"
  5. #include "foc/core/foc_core.h"
  6. #include "foc/motor/hall.h"
  7. #include "foc/motor/motor.h"
  8. extern motor_foc_t g_foc;
  9. foc_state_t foc_fsm_state(void){
  10. return g_foc.state;
  11. }
  12. foc_fault_t foc_fsm_next_state(foc_state_t state) {
  13. bool changed = false;
  14. if (state == g_foc.state) {
  15. return foc_success;
  16. }
  17. if (state == START) {
  18. if (g_foc.state == IDLE) {
  19. changed = true;
  20. }
  21. }else if (state == IDLE) {
  22. if (g_foc.state == ANY_STOP) {
  23. changed = true;
  24. }
  25. }else if (state == ANY_STOP) {
  26. if (g_foc.state != IDLE) {
  27. changed = true;
  28. }
  29. }else if (state == CURRENT_CALIBRATE) {
  30. if (g_foc.state == START) {
  31. changed = true;
  32. }
  33. }else if (state == READY_TO_RUN) {
  34. if (g_foc.state == CURRENT_CALIBRATE) {
  35. changed = true;
  36. }
  37. }else if (state == RAMPING_START) {
  38. if (g_foc.state == READY_TO_RUN || g_foc.state == RUNNING) {
  39. changed = true;
  40. }
  41. }
  42. if (changed) {
  43. g_foc.state = state;
  44. return foc_success;
  45. }
  46. return foc_not_allowed;
  47. }
  48. void foc_fsm(motor_foc_t *foc) {
  49. switch (foc->state) {
  50. case IDLE:
  51. foc->mode = FOC_MODE_OPEN_LOOP;
  52. break;
  53. case START:
  54. foc_clear();
  55. motor_drvier_low_side(true);
  56. foc_fsm_next_state(CURRENT_CALIBRATE);
  57. break;
  58. case CURRENT_CALIBRATE:
  59. foc_current_calibrate();
  60. foc_fsm_next_state(READY_TO_RUN);
  61. break;
  62. case READY_TO_RUN:
  63. foc_pwm_start(true);
  64. foc_fsm_next_state(RAMPING_START);
  65. break;
  66. case RAMPING_START:
  67. foc_set_dq_command(0.0f, ramp_get_target(&foc->voltage_ramp));
  68. //printf("target %f\n", ramp_get_target(&foc->voltage_ramp));
  69. if (foc_get_speed() >= RPM_FOR_CLOSE_LOOP){
  70. //foc_fsm_next_state(CLOSED_LOOP);
  71. //foc_overide_vdq(false);
  72. }
  73. break;
  74. // case CLOSED_LOOP:
  75. // foc_speed_ramp(foc);
  76. // foc->mode = FOC_MODE_PI_FULL;
  77. // foc_fsm_next_state(RUNNING);
  78. // ramp_clear(&foc->voltage_ramp);
  79. // break;
  80. case RUNNING:
  81. foc_speed_ramp(foc);
  82. foc_calc_current(foc);
  83. if (foc->foc_fault == foc_brake_error){
  84. foc_fsm_next_state(ANY_STOP);
  85. break;
  86. }
  87. /*
  88. if (foc_get_speed() <= RPM_FOR_CLOSE_LOOP){
  89. foc_set_voltage_ramp(speed_to_voltage(foc_get_speed()));
  90. ramp_exc(&foc->voltage_ramp);
  91. foc_fsm_next_state(RAMPING_START);
  92. foc->mode = FOC_MODE_OPEN_LOOP;
  93. foc_overide_vdq(true);
  94. }*/
  95. break;
  96. case ANY_STOP:
  97. foc_stop();
  98. foc_fsm_next_state(IDLE);
  99. break;
  100. default:
  101. break;
  102. }
  103. }