| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- #include <string.h>
- #include "bsp/bsp.h"
- #include "bsp/pwm.h"
- #include "foc/foc_api.h"
- #include "foc/foc_core.h"
- #include "foc/hall_sensor.h"
- extern motor_foc_t g_foc;
- foc_state_t foc_fsm_state(void){
- return g_foc.state;
- }
- foc_fault_t foc_fsm_next_state(foc_state_t state) {
- bool changed = false;
- if (state == g_foc.state) {
- return foc_success;
- }
- if (state == START) {
- if (g_foc.state == IDLE) {
- changed = true;
- }
- }else if (state == IDLE) {
- if (g_foc.state == ANY_STOP) {
- changed = true;
- }
- }else if (state == ANY_STOP) {
- if (g_foc.state != IDLE) {
- changed = true;
- }
- }else if (state == CURRENT_CALIBRATE) {
- if (g_foc.state == START) {
- changed = true;
- }
- }else if (state == READY_TO_RUN) {
- if (g_foc.state == CURRENT_CALIBRATE) {
- changed = true;
- }
- }else if (state == RAMPING_START) {
- if (g_foc.state == READY_TO_RUN || g_foc.state == RUNNING) {
- changed = true;
- }
- }
- if (changed) {
- g_foc.state = state;
- return foc_success;
- }
- return foc_not_allowed;
- }
- void foc_fsm(motor_foc_t *foc) {
- switch (foc->state) {
- case IDLE:
- foc->mode = FOC_MODE_OPEN_LOOP;
- break;
- case START:
- foc_clear();
- pwm_turn_on_low_side();
- foc_fsm_next_state(CURRENT_CALIBRATE);
- break;
- case CURRENT_CALIBRATE:
- foc_current_calibrate();
- foc_fsm_next_state(READY_TO_RUN);
- break;
- case READY_TO_RUN:
- foc_pwm_start(true);
- foc_fsm_next_state(RAMPING_START);
- break;
- case RAMPING_START:
- foc_set_dq_command(0.0f, ramp_get_target(&foc->voltage_ramp));
- //printf("target %f\n", ramp_get_target(&foc->voltage_ramp));
- if (foc_get_speed() >= RPM_FOR_CLOSE_LOOP){
- //foc_fsm_next_state(CLOSED_LOOP);
- //foc_overide_vdq(false);
- }
- break;
- // case CLOSED_LOOP:
- // foc_speed_ramp(foc);
- // foc->mode = FOC_MODE_PI_FULL;
- // foc_fsm_next_state(RUNNING);
- // ramp_clear(&foc->voltage_ramp);
- // break;
- case RUNNING:
- foc_speed_ramp(foc);
- foc_calc_current(foc);
- if (foc->foc_fault == foc_brake_error){
- foc_fsm_next_state(ANY_STOP);
- break;
- }
- /*
- if (foc_get_speed() <= RPM_FOR_CLOSE_LOOP){
- foc_set_voltage_ramp(speed_to_voltage(foc_get_speed()));
- ramp_exc(&foc->voltage_ramp);
- foc_fsm_next_state(RAMPING_START);
- foc->mode = FOC_MODE_OPEN_LOOP;
- foc_overide_vdq(true);
- }*/
- break;
- case ANY_STOP:
- foc_stop();
- foc_fsm_next_state(IDLE);
- break;
- default:
- break;
- }
- }
|