foc_stm.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_stm_state(void){
  9. return g_foc.state;
  10. }
  11. foc_fault_t foc_stm_nextstate(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_Normal_Task(motor_foc_t *foc) {
  48. switch (foc->state) {
  49. case IDLE:
  50. foc->mode = FOC_MODE_OPEN_LOOP;
  51. break;
  52. case START:
  53. pwm_turn_on_low_side();
  54. foc_stm_nextstate(CURRENT_CALIBRATE);
  55. break;
  56. case CURRENT_CALIBRATE:
  57. foc_current_calibrate();
  58. foc_stm_nextstate(READY_TO_RUN);
  59. break;
  60. case READY_TO_RUN:
  61. foc_pwm_start(true);
  62. //hall_sensor_calibrate(1.0f, NULL);
  63. foc_stm_nextstate(RAMPING_START);
  64. //ramp_exc(&foc->voltage_ramp);
  65. foc_set_voltage_ramp(1.0f);
  66. break;
  67. case RAMPING_START:
  68. foc_set_dq_command(0.0f, ramp_get_target(&foc->voltage_ramp));
  69. //printf("target %f\n", ramp_get_target(&foc->voltage_ramp));
  70. if (foc_get_speed() >= RPM_FOR_CLOSE_LOOP){
  71. //foc_stm_nextstate(CLOSED_LOOP);
  72. //foc_overide_vdq(false);
  73. }
  74. break;
  75. // case CLOSED_LOOP:
  76. // Foc_Speed_Ramp(foc);
  77. // foc->mode = FOC_MODE_PI_FULL;
  78. // foc_stm_nextstate(RUNNING);
  79. // ramp_clear(&foc->voltage_ramp);
  80. // break;
  81. case RUNNING:
  82. Foc_Speed_Ramp(foc);
  83. Foc_Calc_Current_Ref(foc);
  84. if (foc->foc_fault == foc_brake_error){
  85. foc_stm_nextstate(ANY_STOP);
  86. break;
  87. }
  88. /*
  89. if (foc_get_speed() <= RPM_FOR_CLOSE_LOOP){
  90. foc_set_voltage_ramp(speed_to_voltage(foc_get_speed()));
  91. ramp_exc(&foc->voltage_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. }