From 99513ac23e1779a564cfcf6b095427bc5fc4d77c Mon Sep 17 00:00:00 2001 From: raiden00pl Date: Mon, 13 Nov 2023 12:24:55 +0100 Subject: [PATCH] drivers/foc: return scaling factor for phase currents and BEMF via ioctl These values are board-specific properties that must be known on the application side. Until now, these values had to be hardcoded on the application side. --- boards/arm/stm32/b-g431b-esc1/src/stm32_foc.c | 13 +++++++++++- boards/arm/stm32/common/src/stm32_ihm07m1.c | 13 +++++++++++- boards/arm/stm32/common/src/stm32_ihm08m1.c | 13 +++++++++++- boards/arm/stm32/common/src/stm32_ihm16m1.c | 13 +++++++++++- .../stm32f7/steval-eth001v1/src/stm32_foc.c | 13 +++++++++++- drivers/motor/foc/foc_dummy.c | 16 +++++++++++---- include/nuttx/motor/foc/foc.h | 20 +++++++++++++++++++ 7 files changed, 92 insertions(+), 9 deletions(-) diff --git a/boards/arm/stm32/b-g431b-esc1/src/stm32_foc.c b/boards/arm/stm32/b-g431b-esc1/src/stm32_foc.c index 3ed3aa5307..d8b88e3e51 100644 --- a/boards/arm/stm32/b-g431b-esc1/src/stm32_foc.c +++ b/boards/arm/stm32/b-g431b-esc1/src/stm32_foc.c @@ -390,7 +390,7 @@ static struct stm32_foc_board_data_s g_stm32_foc_board_data = #ifdef CONFIG_MOTOR_FOC_BEMF_SENSE .vadc_cfg = &g_vadc_cfg, #endif - .pwm_dt = (PWM_DEADTIME), + .pwm_dt = PWM_DEADTIME }; /* Board specific configuration */ @@ -599,6 +599,17 @@ static int board_foc_info_get(struct foc_dev_s *dev, struct foc_info_s *info) info->hw_cfg.pwm_dt_ns = PWM_DEADTIME_NS; info->hw_cfg.pwm_max = MAX_DUTY_B16; + /* ADC BEMF */ + +#ifdef CONFIG_MOTOR_FOC_BEMF_SENSE + info->hw_cfg.bemf_scale = 0; /* TODO */ +#endif + + /* ADC Current - dynamic current scale not supported */ + + info->hw_cfg.iphase_max = 40000; + info->hw_cfg.iphase_scale = -2939; + return OK; } diff --git a/boards/arm/stm32/common/src/stm32_ihm07m1.c b/boards/arm/stm32/common/src/stm32_ihm07m1.c index 2717538139..e177cdb33e 100644 --- a/boards/arm/stm32/common/src/stm32_ihm07m1.c +++ b/boards/arm/stm32/common/src/stm32_ihm07m1.c @@ -146,7 +146,7 @@ static struct stm32_foc_board_ops_s g_stm32_foc_board_ops = static struct stm32_foc_board_data_s g_stm32_foc_board_data = { .adc_cfg = NULL, /* board-specific */ - .pwm_dt = (PWM_DEADTIME) + .pwm_dt = PWM_DEADTIME }; /* Board specific configuration */ @@ -307,6 +307,17 @@ static int board_foc_info_get(struct foc_dev_s *dev, info->hw_cfg.pwm_dt_ns = PWM_DEADTIME_NS; info->hw_cfg.pwm_max = MAX_DUTY_B16; + /* ADC BEMF */ + +#ifdef CONFIG_MOTOR_FOC_BEMF_SENSE + info->hw_cfg.bemf_scale = 0; /* TODO */ +#endif + + /* ADC Current - dynamic current scale not supported */ + + info->hw_cfg.iphase_max = 1400; + info->hw_cfg.iphase_scale = -160; + return OK; } diff --git a/boards/arm/stm32/common/src/stm32_ihm08m1.c b/boards/arm/stm32/common/src/stm32_ihm08m1.c index 486ad64759..1d87ca0061 100644 --- a/boards/arm/stm32/common/src/stm32_ihm08m1.c +++ b/boards/arm/stm32/common/src/stm32_ihm08m1.c @@ -154,7 +154,7 @@ static struct stm32_foc_board_ops_s g_stm32_foc_board_ops = static struct stm32_foc_board_data_s g_stm32_foc_board_data = { .adc_cfg = NULL, /* board-specific */ - .pwm_dt = (PWM_DEADTIME) + .pwm_dt = PWM_DEADTIME }; /* Board specific configuration */ @@ -276,6 +276,17 @@ static int board_foc_info_get(struct foc_dev_s *dev, info->hw_cfg.pwm_dt_ns = PWM_DEADTIME_NS; info->hw_cfg.pwm_max = MAX_DUTY_B16; + /* ADC BEMF */ + +#ifdef CONFIG_MOTOR_FOC_BEMF_SENSE + info->hw_cfg.bemf_scale = 0; /* TODO */ +#endif + + /* ADC Current - dynamic current scale not supported */ + + info->hw_cfg.iphase_max = 15000; + info->hw_cfg.iphase_scale = -1555; + return OK; } diff --git a/boards/arm/stm32/common/src/stm32_ihm16m1.c b/boards/arm/stm32/common/src/stm32_ihm16m1.c index 8155db5ac6..920a0d4e99 100644 --- a/boards/arm/stm32/common/src/stm32_ihm16m1.c +++ b/boards/arm/stm32/common/src/stm32_ihm16m1.c @@ -144,7 +144,7 @@ static struct stm32_foc_board_ops_s g_stm32_foc_board_ops = static struct stm32_foc_board_data_s g_stm32_foc_board_data = { .adc_cfg = NULL, /* board-specific */ - .pwm_dt = (PWM_DEADTIME) + .pwm_dt = PWM_DEADTIME }; /* Board specific configuration */ @@ -284,6 +284,17 @@ static int board_foc_info_get(struct foc_dev_s *dev, info->hw_cfg.pwm_dt_ns = PWM_DEADTIME_NS; info->hw_cfg.pwm_max = MAX_DUTY_B16; + /* ADC BEMF */ + +#ifdef CONFIG_MOTOR_FOC_BEMF_SENSE + info->hw_cfg.bemf_scale = 0; /* TODO */ +#endif + + /* ADC Current - dynamic current scale not supported */ + + info->hw_cfg.iphase_max = 1500; + info->hw_cfg.iphase_scale = -160; + return OK; } diff --git a/boards/arm/stm32f7/steval-eth001v1/src/stm32_foc.c b/boards/arm/stm32f7/steval-eth001v1/src/stm32_foc.c index cd2ec15e1a..6d19474784 100644 --- a/boards/arm/stm32f7/steval-eth001v1/src/stm32_foc.c +++ b/boards/arm/stm32f7/steval-eth001v1/src/stm32_foc.c @@ -260,7 +260,7 @@ static struct stm32_foc_board_ops_s g_stm32_foc_board_ops = static struct stm32_foc_board_data_s g_stm32_foc_board_data = { .adc_cfg = &g_adc_cfg, - .pwm_dt = (PWM_DEADTIME), + .pwm_dt = PWM_DEADTIME, }; /* Board specific configuration */ @@ -382,6 +382,17 @@ static int board_foc_info_get(struct foc_dev_s *dev, info->hw_cfg.pwm_dt_ns = PWM_DEADTIME_NS; info->hw_cfg.pwm_max = MAX_DUTY_B16; + /* ADC BEMF */ + +#ifdef CONFIG_MOTOR_FOC_BEMF_SENSE + info->hw_cfg.bemf_scale = 0; /* TODO */ +#endif + + /* ADC Current - dynamic current scale not supported */ + + info->hw_cfg.iphase_max = 40000; + info->hw_cfg.iphase_scale = -2287; + return OK; } diff --git a/drivers/motor/foc/foc_dummy.c b/drivers/motor/foc/foc_dummy.c index 43f4809f0f..9648feff6f 100644 --- a/drivers/motor/foc/foc_dummy.c +++ b/drivers/motor/foc/foc_dummy.c @@ -44,8 +44,11 @@ /* Board HW configuration */ -#define FOC_DUMMY_HW_PWM_NS (500) -#define FOC_DUMMY_HW_PWM_MAX (0.95f) +#define FOC_DUMMY_HW_PWM_NS (500) +#define FOC_DUMMY_HW_PWM_MAX (0.95f) +#define FOC_DUMMY_HW_BEMF_SCALE (1000) +#define FOC_DUMMY_HW_IPHASE_SCALE (1000) +#define FOC_DUMMY_HW_IPHASE_MAX (40000) /* Helper macros ************************************************************/ @@ -546,8 +549,13 @@ static int foc_dummy_info_get(FAR struct foc_dev_s *dev, { /* Get HW configuration */ - info->hw_cfg.pwm_dt_ns = FOC_DUMMY_HW_PWM_NS; - info->hw_cfg.pwm_max = ftob16(FOC_DUMMY_HW_PWM_MAX); + info->hw_cfg.pwm_dt_ns = FOC_DUMMY_HW_PWM_NS; + info->hw_cfg.pwm_max = ftob16(FOC_DUMMY_HW_PWM_MAX); +#ifdef CONFIG_MOTOR_FOC_BEMF_SENSE + info->hw_cfg.bemf_scale = FOC_DUMMY_HW_IPHASE_SCALE; +#endif + info->hw_cfg.iphase_max = FOC_DUMMY_HW_IPHASE_MAX; + info->hw_cfg.iphase_scale = FOC_DUMMY_HW_IPHASE_SCALE; return OK; } diff --git a/include/nuttx/motor/foc/foc.h b/include/nuttx/motor/foc/foc.h index fe9c5dbd20..497992f091 100644 --- a/include/nuttx/motor/foc/foc.h +++ b/include/nuttx/motor/foc/foc.h @@ -102,8 +102,28 @@ struct foc_params_s struct foc_info_hw_s { + /* PWM configuration */ + uint32_t pwm_dt_ns; /* PWM dead-time in nano seconds */ foc_duty_t pwm_max; /* Maximum PWM duty cycle */ + + /* ADC configuration for phase current sampling + * + * In most cases it will be: + * + * iphase_scale = iphase_ratio * adc_to_volt + * adc_to_volt = adc_ref_volt / adc_val_max + * for shunt sensors iphase_ratio = 1 / (R_shunt * gain) + */ + + int32_t iphase_scale; /* Current phase scale [x100000] */ + int32_t iphase_max; /* Maximum phase curretn [x1000] */ + + /* ADC configuration for BEMF sampling */ + +#ifdef CONFIG_MOTOR_FOC_BEMF_SENSE + int32_t bemf_scale; /* BEMF sampling scale [x1000] */ +#endif }; /* FOC driver info */