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