foc_fsm.c 2.5 KB

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