examples/foc: refactor control loop
1. get FOC device state at the beginning of the loop 2. simplify if-else block
This commit is contained in:
parent
388256b7d7
commit
73ab7dedd4
@ -339,6 +339,18 @@ int foc_fixed16_thr(FAR struct foc_ctrl_env_s *envp)
|
|||||||
|
|
||||||
while (motor.mq.quit == false)
|
while (motor.mq.quit == false)
|
||||||
{
|
{
|
||||||
|
if (motor.mq.start == true)
|
||||||
|
{
|
||||||
|
/* Get FOC device state */
|
||||||
|
|
||||||
|
ret = foc_dev_state_get(&dev);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
PRINTF("ERROR: foc_dev_state_get failed %d!\n", ret);
|
||||||
|
goto errout;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
PRINTFV("foc_fixed16_thr %d %d\n", envp->id, motor.time);
|
PRINTFV("foc_fixed16_thr %d %d\n", envp->id, motor.time);
|
||||||
|
|
||||||
/* Handle mqueue */
|
/* Handle mqueue */
|
||||||
@ -376,17 +388,12 @@ int foc_fixed16_thr(FAR struct foc_ctrl_env_s *envp)
|
|||||||
motor.startstop = false;
|
motor.startstop = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Run control logic if controller started */
|
/* Ignore control logic if controller not started yet */
|
||||||
|
|
||||||
if (motor.mq.start == true)
|
if (motor.mq.start == false)
|
||||||
{
|
{
|
||||||
/* Get FOC device state */
|
usleep(1000);
|
||||||
|
continue;
|
||||||
ret = foc_dev_state_get(&dev);
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
PRINTF("ERROR: foc_dev_state_get failed %d!\n", ret);
|
|
||||||
goto errout;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CONFIG_EXAMPLES_FOC_STATE_USE_MODEL_PMSM
|
#ifdef CONFIG_EXAMPLES_FOC_STATE_USE_MODEL_PMSM
|
||||||
@ -487,11 +494,6 @@ int foc_fixed16_thr(FAR struct foc_ctrl_env_s *envp)
|
|||||||
PRINTF("TERMINATE CTRL THREAD\n");
|
PRINTF("TERMINATE CTRL THREAD\n");
|
||||||
goto errout;
|
goto errout;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
usleep(1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Increase counter */
|
/* Increase counter */
|
||||||
|
|
||||||
|
@ -352,6 +352,18 @@ int foc_float_thr(FAR struct foc_ctrl_env_s *envp)
|
|||||||
|
|
||||||
while (motor.mq.quit == false)
|
while (motor.mq.quit == false)
|
||||||
{
|
{
|
||||||
|
if (motor.mq.start == true)
|
||||||
|
{
|
||||||
|
/* Get FOC device state */
|
||||||
|
|
||||||
|
ret = foc_dev_state_get(&dev);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
PRINTF("ERROR: foc_dev_state_get failed %d!\n", ret);
|
||||||
|
goto errout;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
PRINTFV("foc_float_thr %d %d\n", envp->id, motor.time);
|
PRINTFV("foc_float_thr %d %d\n", envp->id, motor.time);
|
||||||
|
|
||||||
/* Handle mqueue */
|
/* Handle mqueue */
|
||||||
@ -389,17 +401,12 @@ int foc_float_thr(FAR struct foc_ctrl_env_s *envp)
|
|||||||
motor.startstop = false;
|
motor.startstop = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Run control logic if controller started */
|
/* Ignore control logic if controller not started yet */
|
||||||
|
|
||||||
if (motor.mq.start == true)
|
if (motor.mq.start == false)
|
||||||
{
|
{
|
||||||
/* Get FOC device state */
|
usleep(1000);
|
||||||
|
continue;
|
||||||
ret = foc_dev_state_get(&dev);
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
PRINTF("ERROR: foc_dev_state_get failed %d!\n", ret);
|
|
||||||
goto errout;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CONFIG_EXAMPLES_FOC_STATE_USE_MODEL_PMSM
|
#ifdef CONFIG_EXAMPLES_FOC_STATE_USE_MODEL_PMSM
|
||||||
@ -500,11 +507,6 @@ int foc_float_thr(FAR struct foc_ctrl_env_s *envp)
|
|||||||
PRINTF("TERMINATE CTRL THREAD\n");
|
PRINTF("TERMINATE CTRL THREAD\n");
|
||||||
goto errout;
|
goto errout;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
usleep(1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Increase counter */
|
/* Increase counter */
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user