diff --git a/examples/foc/Kconfig b/examples/foc/Kconfig index 715455812..1bd955ca8 100644 --- a/examples/foc/Kconfig +++ b/examples/foc/Kconfig @@ -75,6 +75,24 @@ config EXAMPLES_FOC_STATE_USE_MODEL_PMSM ---help--- Use PMSM model instead of real hardware +choice + prompt "FOC sensored or sensorless configuration" + default EXAMPLES_FOC_SENSORLESS + +config EXAMPLES_FOC_SENSORLESS + bool "FOC example sensorless configuration" + +config EXAMPLES_FOC_SENSORED + bool "FOC example sensored configuration" + +endchoice # + +config EXAMPLES_FOC_HAVE_OPENLOOP + bool "FOC example have open-loop controller" + select INDUSTRY_FOC_ANGLE_OPENLOOP + default y if EXAMPLES_FOC_SENSORLESS + default n + menu "FOC user input" config EXAMPLES_FOC_HAVE_ADC @@ -201,6 +219,7 @@ config EXAMPLES_FOC_OPMODE config EXAMPLES_FOC_OPENLOOP_Q int "FOC open-loop Vq/Iq setting [x1000]" default 200 + depends on EXAMPLES_FOC_HAVE_OPENLOOP config EXAMPLES_FOC_IDQ_KP int "FOC PI controller Kp gain [x1000]" diff --git a/examples/foc/foc_cfg.h b/examples/foc/foc_cfg.h index f8ec1f8e2..a55902dd3 100644 --- a/examples/foc/foc_cfg.h +++ b/examples/foc/foc_cfg.h @@ -31,6 +31,20 @@ * Pre-processor Definitions ****************************************************************************/ +/* For now only open-loop supported */ + +#ifndef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP +# error For now only open-loop supported +#endif + +/* Open-loop configuration */ + +#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP +# ifndef CONFIG_INDUSTRY_FOC_ANGLE_OPENLOOP +# error +# endif +#endif + /* Velocity ramp must be configured */ #if (CONFIG_EXAMPLES_FOC_RAMP_THR == 0) diff --git a/examples/foc/foc_fixed16_thr.c b/examples/foc/foc_fixed16_thr.c index 979b3d4b8..ccff7e9b9 100644 --- a/examples/foc/foc_fixed16_thr.c +++ b/examples/foc/foc_fixed16_thr.c @@ -55,10 +55,6 @@ # error #endif -#ifndef CONFIG_INDUSTRY_FOC_ANGLE_OPENLOOP -# error For now only open-loop supported -#endif - /**************************************************************************** * Private Type Definition ****************************************************************************/ @@ -69,13 +65,14 @@ struct foc_motor_b16_s { FAR struct foc_ctrl_env_s *envp; /* Thread env */ bool fault; /* Fault flag */ -#ifdef CONFIG_INDUSTRY_FOC_ANGLE_OPENLOOP +#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP bool openloop_now; /* Open-loop now */ + b16_t angle_ol; /* Phase angle open-loop */ + foc_angle_b16_t openloop; /* Open-loop angle handler */ #endif int foc_mode; /* FOC mode */ b16_t vbus; /* Power bus voltage */ b16_t angle_now; /* Phase angle now */ - b16_t angle_ol; /* Phase angle open-loop */ b16_t vel_set; /* Velocity setting now */ b16_t vel_now; /* Velocity now */ b16_t vel_des; /* Velocity destination */ @@ -91,9 +88,6 @@ struct foc_motor_b16_s struct foc_state_s dev_state; /* FOC dev state */ struct foc_params_s dev_params; /* FOC dev params */ struct foc_ramp_b16_s ramp; /* Velocity ramp data */ -#ifdef CONFIG_INDUSTRY_FOC_ANGLE_OPENLOOP - foc_angle_b16_t openloop; /* Open-loop angle handler */ -#endif #ifdef CONFIG_EXAMPLES_FOC_STATE_USE_MODEL_PMSM struct foc_model_b16_s model; /* Model handler */ struct foc_model_state_b16_s model_state; /* PMSM model state */ @@ -111,7 +105,7 @@ struct foc_motor_b16_s static int foc_motor_init(FAR struct foc_motor_b16_s *motor, FAR struct foc_ctrl_env_s *envp) { -#ifdef CONFIG_INDUSTRY_FOC_ANGLE_OPENLOOP +#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP struct foc_openloop_cfg_b16_s ol_cfg; #endif int ret = OK; @@ -141,7 +135,7 @@ static int foc_motor_init(FAR struct foc_motor_b16_s *motor, motor->per = b16divi(b16ONE, CONFIG_EXAMPLES_FOC_NOTIFIER_FREQ); motor->iphase_adc = ftob16((CONFIG_EXAMPLES_FOC_IPHASE_ADC) / 100000.0f); -#ifdef CONFIG_INDUSTRY_FOC_ANGLE_OPENLOOP +#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP /* Initialize open-loop angle handler */ foc_angle_init_b16(&motor->openloop, @@ -173,7 +167,7 @@ static int foc_mode_init(FAR struct foc_motor_b16_s *motor) break; } -#ifdef CONFIG_INDUSTRY_FOC_ANGLE_OPENLOOP +#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP case FOC_OPMODE_OL_V_VEL: { motor->foc_mode = FOC_HANDLER_MODE_VOLTAGE; @@ -552,7 +546,7 @@ static int foc_motor_run(FAR struct foc_motor_b16_s *motor) ain.angle = motor->angle_now; ain.dir = motor->dir; -#ifdef CONFIG_INDUSTRY_FOC_ANGLE_OPENLOOP +#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP foc_angle_run_b16(&motor->openloop, &ain, &aout); /* Store open-loop angle */ diff --git a/examples/foc/foc_float_thr.c b/examples/foc/foc_float_thr.c index 6491bb477..c0912118d 100644 --- a/examples/foc/foc_float_thr.c +++ b/examples/foc/foc_float_thr.c @@ -55,10 +55,6 @@ # error #endif -#ifndef CONFIG_INDUSTRY_FOC_ANGLE_OPENLOOP -# error For now only open-loop supported -#endif - /**************************************************************************** * Private Type Definition ****************************************************************************/ @@ -69,13 +65,14 @@ struct foc_motor_f32_s { FAR struct foc_ctrl_env_s *envp; /* Thread env */ bool fault; /* Fault flag */ -#ifdef CONFIG_INDUSTRY_FOC_ANGLE_OPENLOOP +#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP bool openloop_now; /* Open-loop now */ + float angle_ol; /* Phase angle open-loop */ + foc_angle_f32_t openloop; /* Open-loop angle handler */ #endif int foc_mode; /* FOC mode */ float vbus; /* Power bus voltage */ float angle_now; /* Phase angle now */ - float angle_ol; /* Phase angle open-loop */ float vel_set; /* Velocity setting now */ float vel_now; /* Velocity now */ float vel_des; /* Velocity destination */ @@ -91,9 +88,6 @@ struct foc_motor_f32_s struct foc_state_s dev_state; /* FOC dev state */ struct foc_params_s dev_params; /* FOC dev params */ struct foc_ramp_f32_s ramp; /* Velocity ramp data */ -#ifdef CONFIG_INDUSTRY_FOC_ANGLE_OPENLOOP - foc_angle_f32_t openloop; /* Open-loop angle handler */ -#endif #ifdef CONFIG_EXAMPLES_FOC_STATE_USE_MODEL_PMSM struct foc_model_f32_s model; /* Model handler */ struct foc_model_state_f32_s model_state; /* PMSM model state */ @@ -111,7 +105,7 @@ struct foc_motor_f32_s static int foc_motor_init(FAR struct foc_motor_f32_s *motor, FAR struct foc_ctrl_env_s *envp) { -#ifdef CONFIG_INDUSTRY_FOC_ANGLE_OPENLOOP +#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP struct foc_openloop_cfg_f32_s ol_cfg; #endif int ret = OK; @@ -141,7 +135,7 @@ static int foc_motor_init(FAR struct foc_motor_f32_s *motor, motor->per = (float)(1.0f / CONFIG_EXAMPLES_FOC_NOTIFIER_FREQ); motor->iphase_adc = ((CONFIG_EXAMPLES_FOC_IPHASE_ADC) / 100000.0f); -#ifdef CONFIG_INDUSTRY_FOC_ANGLE_OPENLOOP +#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP /* Initialize open-loop angle handler */ foc_angle_init_f32(&motor->openloop, @@ -173,7 +167,7 @@ static int foc_mode_init(FAR struct foc_motor_f32_s *motor) break; } -#ifdef CONFIG_INDUSTRY_FOC_ANGLE_OPENLOOP +#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP case FOC_OPMODE_OL_V_VEL: { motor->foc_mode = FOC_HANDLER_MODE_VOLTAGE; @@ -551,7 +545,7 @@ static int foc_motor_run(FAR struct foc_motor_f32_s *motor) ain.angle = motor->angle_now; ain.dir = motor->dir; -#ifdef CONFIG_INDUSTRY_FOC_ANGLE_OPENLOOP +#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP foc_angle_run_f32(&motor->openloop, &ain, &aout); /* Store open-loop angle */ diff --git a/examples/foc/foc_main.c b/examples/foc/foc_main.c index 5277a888a..66e6bbbbd 100644 --- a/examples/foc/foc_main.c +++ b/examples/foc/foc_main.c @@ -131,8 +131,10 @@ static void init_args(FAR struct args_s *args) (args->time == 0 ? CONFIG_EXAMPLES_FOC_TIME_DEFAULT : args->time); args->mode = (args->mode == 0 ? CONFIG_EXAMPLES_FOC_OPMODE : args->mode); +#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP args->qparam = (args->qparam == 0 ? CONFIG_EXAMPLES_FOC_OPENLOOP_Q : args->qparam); +#endif args->pi_kp = (args->pi_kp == 0 ? CONFIG_EXAMPLES_FOC_IDQ_KP : args->pi_kp); args->pi_ki = @@ -606,7 +608,9 @@ int main(int argc, char *argv[]) { /* Get configuration */ +#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP foc[i].qparam = args.qparam; +#endif foc[i].mode = args.mode; foc[i].pi_kp = args.pi_kp; foc[i].pi_ki = args.pi_ki; diff --git a/examples/foc/foc_parseargs.c b/examples/foc/foc_parseargs.c index 3f2d0e27a..c945e8394 100644 --- a/examples/foc/foc_parseargs.c +++ b/examples/foc/foc_parseargs.c @@ -80,7 +80,9 @@ static void foc_help(void) PRINTF(" 3 - motor CW\n"); PRINTF(" 4 - motor CCW\n"); PRINTF(" [-j] enable specific instnaces\n"); +#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP PRINTF(" [-o] openloop Vq/Iq setting [x1000]\n"); +#endif PRINTF(" [--fki] PI Kp coefficient [x1000]\n"); PRINTF(" [--fkp] PI Ki coefficient [x1000]\n"); } @@ -158,11 +160,13 @@ void parse_args(FAR struct args_s *args, int argc, FAR char **argv) break; } +#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP case 'o': { args->qparam = atoi(optarg); break; } +#endif case '?': default: diff --git a/examples/foc/foc_parseargs.h b/examples/foc/foc_parseargs.h index a1f44b673..ecbc6feb6 100644 --- a/examples/foc/foc_parseargs.h +++ b/examples/foc/foc_parseargs.h @@ -43,7 +43,9 @@ struct args_s { int time; /* Run time limit in sec, -1 if forever */ int mode; /* Operation mode */ +#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP int qparam; /* Open-loop Q setting (x1000) */ +#endif uint32_t pi_kp; /* PI Kp (x1000) */ uint32_t pi_ki; /* PI Ki (x1000) */ uint32_t velmax; /* Velocity max (x1000) */ diff --git a/examples/foc/foc_thr.h b/examples/foc/foc_thr.h index 5bb9e5426..0797fbb53 100644 --- a/examples/foc/foc_thr.h +++ b/examples/foc/foc_thr.h @@ -75,7 +75,9 @@ struct foc_ctrl_env_s int id; /* FOC device id */ int inst; /* Type specific instance counter */ int type; /* Controller type */ +#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP int qparam; /* Open-loop Q setting (x1000) */ +#endif int mode; /* Operation mode */ uint32_t pi_kp; /* FOC PI Kp (x1000) */ uint32_t pi_ki; /* FOC PI Ki (x1000) */