foc_stm.c 2.5 KB

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