foc_stm.c 2.5 KB

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