diff --git a/examples/foc/Kconfig b/examples/foc/Kconfig index 4b829ed84..35b88a589 100644 --- a/examples/foc/Kconfig +++ b/examples/foc/Kconfig @@ -175,6 +175,7 @@ endif # EXAMPLES_FOC_SENSORED config EXAMPLES_FOC_HAVE_OPENLOOP bool "FOC example have open-loop controller" select INDUSTRY_FOC_ANGLE_OPENLOOP + select EXAMPLES_FOC_HAVE_ALIGN default EXAMPLES_FOC_SENSORLESS config EXAMPLES_FOC_HAVE_TORQ diff --git a/examples/foc/foc_motor_b16.c b/examples/foc/foc_motor_b16.c index 06a7201d2..36c66ff21 100644 --- a/examples/foc/foc_motor_b16.c +++ b/examples/foc/foc_motor_b16.c @@ -577,6 +577,40 @@ errout: return ret; } +/**************************************************************************** + * Name: foc_motor_vel_reset + ****************************************************************************/ + +static int foc_motor_vel_reset(FAR struct foc_motor_b16_s *motor) +{ + int ret = OK; + + /* Reset velocity observer */ + +#ifdef CONFIG_EXAMPLES_FOC_VELOBS_DIV + ret = foc_velocity_zero_b16(&motor->vel_div); + if (ret < 0) + { + PRINTF("ERROR: foc_velocity_zero failed %d\n", ret); + goto errout; + } +#endif + +#ifdef CONFIG_EXAMPLES_FOC_VELOBS_PLL + ret = foc_velocity_zero_b16(&motor->vel_pll); + if (ret < 0) + { + PRINTF("ERROR: foc_velocity_zero failed %d\n", ret); + goto errout; + } +#endif + +#ifdef CONFIG_EXAMPLES_FOC_VELOBS + errout: +#endif + return ret; +} + /**************************************************************************** * Name: foc_motor_state ****************************************************************************/ @@ -637,6 +671,27 @@ static int foc_motor_state(FAR struct foc_motor_b16_s *motor, int state) } } +#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP + /* Re-align motor if we change mode from FREE/STOP to CW/CCW otherwise, + * the open-loop may fail because the rotor position at the start is + * random. + */ + + if ((motor->mq.app_state == FOC_EXAMPLE_STATE_FREE || + motor->mq.app_state == FOC_EXAMPLE_STATE_STOP) && + (state == FOC_EXAMPLE_STATE_CW || + state == FOC_EXAMPLE_STATE_CCW)) + { + motor->ctrl_state = FOC_CTRL_STATE_ALIGN; + motor->align_done = false; + motor->angle_now = 0; + + /* Reset velocity observer */ + + foc_motor_vel_reset(motor); + } +#endif + /* Reset current setpoint */ #ifdef CONFIG_EXAMPLES_FOC_HAVE_TORQ @@ -706,29 +761,10 @@ static int foc_motor_run_init(FAR struct foc_motor_b16_s *motor) { int ret = OK; - /* Reset velocity observer */ - -#ifdef CONFIG_EXAMPLES_FOC_VELOBS_DIV - ret = foc_velocity_zero_b16(&motor->vel_div); - if (ret < 0) - { - PRINTF("ERROR: foc_velocity_zero failed %d\n", ret); - goto errout; - } -#endif - -#ifdef CONFIG_EXAMPLES_FOC_VELOBS_PLL - ret = foc_velocity_zero_b16(&motor->vel_pll); - if (ret < 0) - { - PRINTF("ERROR: foc_velocity_zero failed %d\n", ret); - goto errout; - } -#endif - #ifdef CONFIG_EXAMPLES_FOC_VELOBS -errout: + ret = foc_motor_vel_reset(motor); #endif + return ret; } diff --git a/examples/foc/foc_motor_f32.c b/examples/foc/foc_motor_f32.c index da6bdcab4..07cf16d9d 100644 --- a/examples/foc/foc_motor_f32.c +++ b/examples/foc/foc_motor_f32.c @@ -555,6 +555,40 @@ errout: return ret; } +/**************************************************************************** + * Name: foc_motor_vel_reset + ****************************************************************************/ + +static int foc_motor_vel_reset(FAR struct foc_motor_f32_s *motor) +{ + int ret = OK; + + /* Reset velocity observer */ + +#ifdef CONFIG_EXAMPLES_FOC_VELOBS_DIV + ret = foc_velocity_zero_f32(&motor->vel_div); + if (ret < 0) + { + PRINTF("ERROR: foc_velocity_zero failed %d\n", ret); + goto errout; + } +#endif + +#ifdef CONFIG_EXAMPLES_FOC_VELOBS_PLL + ret = foc_velocity_zero_f32(&motor->vel_pll); + if (ret < 0) + { + PRINTF("ERROR: foc_velocity_zero failed %d\n", ret); + goto errout; + } +#endif + +#ifdef CONFIG_EXAMPLES_FOC_VELOBS + errout: +#endif + return ret; +} + /**************************************************************************** * Name: foc_motor_state ****************************************************************************/ @@ -614,6 +648,27 @@ static int foc_motor_state(FAR struct foc_motor_f32_s *motor, int state) } } +#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP + /* Re-align motor if we change mode from FREE/STOP to CW/CCW otherwise, + * the open-loop may fail because the rotor position at the start is + * random. + */ + + if ((motor->mq.app_state == FOC_EXAMPLE_STATE_FREE || + motor->mq.app_state == FOC_EXAMPLE_STATE_STOP) && + (state == FOC_EXAMPLE_STATE_CW || + state == FOC_EXAMPLE_STATE_CCW)) + { + motor->ctrl_state = FOC_CTRL_STATE_ALIGN; + motor->align_done = false; + motor->angle_now = 0.0f; + + /* Reset velocity observer */ + + foc_motor_vel_reset(motor); + } +#endif + /* Reset current setpoint */ #ifdef CONFIG_EXAMPLES_FOC_HAVE_TORQ @@ -690,29 +745,10 @@ static int foc_motor_run_init(FAR struct foc_motor_f32_s *motor) { int ret = OK; - /* Reset velocity observer */ - -#ifdef CONFIG_EXAMPLES_FOC_VELOBS_DIV - ret = foc_velocity_zero_f32(&motor->vel_div); - if (ret < 0) - { - PRINTF("ERROR: foc_velocity_zero failed %d\n", ret); - goto errout; - } -#endif - -#ifdef CONFIG_EXAMPLES_FOC_VELOBS_PLL - ret = foc_velocity_zero_f32(&motor->vel_pll); - if (ret < 0) - { - PRINTF("ERROR: foc_velocity_zero failed %d\n", ret); - goto errout; - } -#endif - #ifdef CONFIG_EXAMPLES_FOC_VELOBS -errout: + ret = foc_motor_vel_reset(motor); #endif + return ret; }