From 85fe36d7c18aa8ccec0f3f961332cc4c677f7cfe Mon Sep 17 00:00:00 2001 From: XiNGRZ Date: Thu, 12 Oct 2023 17:50:56 +0800 Subject: [PATCH] knob: No need to reset motor_control in every profile enable Instead, set it every tick. --- .../sensor/knob/include/knob/drivers/profile.h | 6 +++--- config/drivers/sensor/knob/knob.c | 3 +-- config/drivers/sensor/knob/profile/damped.c | 6 +----- config/drivers/sensor/knob/profile/disable.c | 3 ++- config/drivers/sensor/knob/profile/encoder.c | 8 +++----- config/drivers/sensor/knob/profile/inertia.c | 8 +++----- config/drivers/sensor/knob/profile/ratchet.c | 15 +++++---------- config/drivers/sensor/knob/profile/spin.c | 10 ++++------ config/drivers/sensor/knob/profile/spring.c | 9 ++++----- config/drivers/sensor/knob/profile/switch.c | 7 +++---- 10 files changed, 29 insertions(+), 46 deletions(-) diff --git a/config/drivers/sensor/knob/include/knob/drivers/profile.h b/config/drivers/sensor/knob/include/knob/drivers/profile.h index 64fc66f0..981c193f 100644 --- a/config/drivers/sensor/knob/include/knob/drivers/profile.h +++ b/config/drivers/sensor/knob/include/knob/drivers/profile.h @@ -35,13 +35,13 @@ extern "C" { #endif struct knob_profile_api { - int (*enable)(const struct device *dev, struct motor_control *mc); + int (*enable)(const struct device *dev); int (*update_params)(const struct device *dev, struct knob_params params); int (*tick)(const struct device *dev, struct motor_control *mc); int (*report)(const struct device *dev, int32_t *val); }; -static inline int knob_profile_enable(const struct device *dev, struct motor_control *mc) +static inline int knob_profile_enable(const struct device *dev) { const struct knob_profile_api *api = dev->api; @@ -49,7 +49,7 @@ static inline int knob_profile_enable(const struct device *dev, struct motor_con return -ENOTSUP; } - return api->enable(dev, mc); + return api->enable(dev); } static inline int knob_profile_update_params(const struct device *dev, struct knob_params params) diff --git a/config/drivers/sensor/knob/knob.c b/config/drivers/sensor/knob/knob.c index 40b3344d..9f9f8ac8 100644 --- a/config/drivers/sensor/knob/knob.c +++ b/config/drivers/sensor/knob/knob.c @@ -102,7 +102,7 @@ void knob_set_mode(const struct device *dev, enum knob_mode mode) if (data->profile != NULL) { knob_profile_update_params(data->profile, data->params); - knob_profile_enable(data->profile, data->mc); + knob_profile_enable(data->profile); } } @@ -206,7 +206,6 @@ static void knob_thread(void *p1, void *p2, void *p3) motor_tick(config->motor); data->delta = 0; - if (data->encoder_report && knob_profile_report(data->profile, &data->delta) == 0 && data->delta != 0) { diff --git a/config/drivers/sensor/knob/profile/damped.c b/config/drivers/sensor/knob/profile/damped.c index 38be305f..bd9a7513 100644 --- a/config/drivers/sensor/knob/profile/damped.c +++ b/config/drivers/sensor/knob/profile/damped.c @@ -25,14 +25,12 @@ struct knob_damped_data { float position_max; }; -static int knob_damped_enable(const struct device *dev, struct motor_control *mc) +static int knob_damped_enable(const struct device *dev) { const struct knob_damped_config *cfg = dev->config; motor_set_torque_limit(cfg->motor, KNOB_PROFILE_TORQUE_LIMIT); - mc->mode = VELOCITY; - #if KNOB_PROFILE_HAS_VELOCITY_PID motor_set_velocity_pid(cfg->motor, KNOB_PROFILE_VELOCITY_PID); #endif /* KNOB_PROFILE_HAS_VELOCITY_PID */ @@ -41,8 +39,6 @@ static int knob_damped_enable(const struct device *dev, struct motor_control *mc motor_set_angle_pid(cfg->motor, KNOB_PROFILE_ANGLE_PID); #endif /* KNOB_PROFILE_HAS_ANGLE_PID */ - mc->target = 0.0f; - return 0; } diff --git a/config/drivers/sensor/knob/profile/disable.c b/config/drivers/sensor/knob/profile/disable.c index b5924a8d..f99ce325 100644 --- a/config/drivers/sensor/knob/profile/disable.c +++ b/config/drivers/sensor/knob/profile/disable.c @@ -27,7 +27,7 @@ struct knob_disable_data { int32_t reported_pulses; }; -static int knob_disable_enable(const struct device *dev, struct motor_control *mc) +static int knob_disable_enable(const struct device *dev) { const struct knob_disable_config *cfg = dev->config; struct knob_disable_data *data = dev->data; @@ -52,6 +52,7 @@ static int knob_disable_tick(const struct device *dev, struct motor_control *mc) { const struct knob_disable_config *cfg = dev->config; struct knob_disable_data *data = dev->data; + ARG_UNUSED(mc); float dp = knob_get_position(cfg->knob) - data->last_angle; float rpp = data->encoder_rpp; diff --git a/config/drivers/sensor/knob/profile/encoder.c b/config/drivers/sensor/knob/profile/encoder.c index 1bc7919e..3bbe33c1 100644 --- a/config/drivers/sensor/knob/profile/encoder.c +++ b/config/drivers/sensor/knob/profile/encoder.c @@ -27,15 +27,13 @@ struct knob_encoder_data { int32_t reported_pulses; }; -static int knob_encoder_enable(const struct device *dev, struct motor_control *mc) +static int knob_encoder_enable(const struct device *dev) { const struct knob_encoder_config *cfg = dev->config; struct knob_encoder_data *data = dev->data; motor_set_torque_limit(cfg->motor, KNOB_PROFILE_TORQUE_LIMIT); - mc->mode = ANGLE; - #if KNOB_PROFILE_HAS_VELOCITY_PID motor_set_velocity_pid(cfg->motor, KNOB_PROFILE_VELOCITY_PID); #endif /* KNOB_PROFILE_HAS_VELOCITY_PID */ @@ -48,8 +46,6 @@ static int knob_encoder_enable(const struct device *dev, struct motor_control *m data->pulses = 0; data->reported_pulses = 0; - mc->target = data->last_angle; - return 0; } @@ -67,6 +63,8 @@ static int knob_encoder_tick(const struct device *dev, struct motor_control *mc) const struct knob_encoder_config *cfg = dev->config; struct knob_encoder_data *data = dev->data; + mc->mode = ANGLE; + float dp = knob_get_position(cfg->knob) - data->last_angle; float rpp = data->encoder_rpp; float rpp_4 = rpp / 4.0f; diff --git a/config/drivers/sensor/knob/profile/inertia.c b/config/drivers/sensor/knob/profile/inertia.c index e627e50d..1e19031d 100644 --- a/config/drivers/sensor/knob/profile/inertia.c +++ b/config/drivers/sensor/knob/profile/inertia.c @@ -30,15 +30,13 @@ struct knob_inertia_data { float max_velocity; }; -static int knob_inertia_enable(const struct device *dev, struct motor_control *mc) +static int knob_inertia_enable(const struct device *dev) { const struct knob_inertia_config *cfg = dev->config; struct knob_inertia_data *data = dev->data; motor_set_torque_limit(cfg->motor, KNOB_PROFILE_TORQUE_LIMIT); - mc->mode = VELOCITY; - #if KNOB_PROFILE_HAS_VELOCITY_PID motor_set_velocity_pid(cfg->motor, KNOB_PROFILE_VELOCITY_PID); #endif /* KNOB_PROFILE_HAS_VELOCITY_PID */ @@ -47,8 +45,6 @@ static int knob_inertia_enable(const struct device *dev, struct motor_control *m motor_set_angle_pid(cfg->motor, KNOB_PROFILE_ANGLE_PID); #endif /* KNOB_PROFILE_HAS_ANGLE_PID */ - mc->target = 0.0f; - data->last_angle = knob_get_position(cfg->knob); data->pulses = 0; data->reported_pulses = 0; @@ -73,6 +69,8 @@ static int knob_inertia_tick(const struct device *dev, struct motor_control *mc) const struct knob_inertia_config *cfg = dev->config; struct knob_inertia_data *data = dev->data; + mc->mode = VELOCITY; + float dp = knob_get_position(cfg->knob) - data->last_angle; float rpp = data->encoder_rpp; float rpp_2 = rpp / 2.0f; diff --git a/config/drivers/sensor/knob/profile/ratchet.c b/config/drivers/sensor/knob/profile/ratchet.c index 359a2ac9..1c79642b 100644 --- a/config/drivers/sensor/knob/profile/ratchet.c +++ b/config/drivers/sensor/knob/profile/ratchet.c @@ -24,15 +24,13 @@ struct knob_ratchet_data { float last_angle; }; -static int knob_ratchet_enable(const struct device *dev, struct motor_control *mc) +static int knob_ratchet_enable(const struct device *dev) { const struct knob_ratchet_config *cfg = dev->config; struct knob_ratchet_data *data = dev->data; motor_set_torque_limit(cfg->motor, KNOB_PROFILE_TORQUE_LIMIT); - mc->mode = ANGLE; - #if KNOB_PROFILE_HAS_VELOCITY_PID motor_set_velocity_pid(cfg->motor, KNOB_PROFILE_VELOCITY_PID); #endif /* KNOB_PROFILE_HAS_VELOCITY_PID */ @@ -43,8 +41,6 @@ static int knob_ratchet_enable(const struct device *dev, struct motor_control *m data->last_angle = knob_get_position(cfg->knob); - mc->target = data->last_angle; - return 0; } @@ -61,13 +57,12 @@ static int knob_ratchet_tick(const struct device *dev, struct motor_control *mc) const struct knob_ratchet_config *cfg = dev->config; struct knob_ratchet_data *data = dev->data; + mc->mode = ANGLE; + mc->target = data->last_angle; + float dp = knob_get_position(cfg->knob) - data->last_angle; float rpp = PI2 / 12; - if (dp < 0) { - mc->target = data->last_angle; - } else if (dp < rpp) { - mc->target = data->last_angle + dp - dp * 0.5f; - } else { + if (dp > rpp) { data->last_angle += rpp; } diff --git a/config/drivers/sensor/knob/profile/spin.c b/config/drivers/sensor/knob/profile/spin.c index 0c814654..4d7f959d 100644 --- a/config/drivers/sensor/knob/profile/spin.c +++ b/config/drivers/sensor/knob/profile/spin.c @@ -20,14 +20,12 @@ struct knob_spin_config { KNOB_PROFILE_CFG_ROM; }; -static int knob_spin_enable(const struct device *dev, struct motor_control *mc) +static int knob_spin_enable(const struct device *dev) { const struct knob_spin_config *cfg = dev->config; motor_set_torque_limit(cfg->motor, KNOB_PROFILE_TORQUE_LIMIT); - mc->mode = VELOCITY; - #if KNOB_PROFILE_HAS_VELOCITY_PID motor_set_velocity_pid(cfg->motor, KNOB_PROFILE_VELOCITY_PID); #endif /* KNOB_PROFILE_HAS_VELOCITY_PID */ @@ -36,8 +34,6 @@ static int knob_spin_enable(const struct device *dev, struct motor_control *mc) motor_set_angle_pid(cfg->motor, KNOB_PROFILE_ANGLE_PID); #endif /* KNOB_PROFILE_HAS_ANGLE_PID */ - mc->target = 20.0f; - return 0; } @@ -52,7 +48,9 @@ static int knob_spin_update_params(const struct device *dev, struct knob_params static int knob_spin_tick(const struct device *dev, struct motor_control *mc) { ARG_UNUSED(dev); - ARG_UNUSED(mc); + + mc->mode = VELOCITY; + mc->target = 20.0f; return 0; } diff --git a/config/drivers/sensor/knob/profile/spring.c b/config/drivers/sensor/knob/profile/spring.c index aec623dc..39f89074 100644 --- a/config/drivers/sensor/knob/profile/spring.c +++ b/config/drivers/sensor/knob/profile/spring.c @@ -29,14 +29,12 @@ struct knob_spring_data { int32_t last_report; }; -static int knob_spring_enable(const struct device *dev, struct motor_control *mc) +static int knob_spring_enable(const struct device *dev) { const struct knob_spring_config *cfg = dev->config; motor_set_torque_limit(cfg->motor, KNOB_PROFILE_TORQUE_LIMIT); - mc->mode = ANGLE; - #if KNOB_PROFILE_HAS_VELOCITY_PID motor_set_velocity_pid(cfg->motor, KNOB_PROFILE_VELOCITY_PID); #endif /* KNOB_PROFILE_HAS_VELOCITY_PID */ @@ -45,8 +43,6 @@ static int knob_spring_enable(const struct device *dev, struct motor_control *mc motor_set_angle_pid(cfg->motor, KNOB_PROFILE_ANGLE_PID); #endif /* KNOB_PROFILE_HAS_ANGLE_PID */ - mc->target = CENTER; - return 0; } @@ -73,6 +69,9 @@ static int knob_spring_tick(const struct device *dev, struct motor_control *mc) data->value = 0; } + mc->mode = ANGLE; + mc->target = CENTER; + return 0; } diff --git a/config/drivers/sensor/knob/profile/switch.c b/config/drivers/sensor/knob/profile/switch.c index 954b49b7..7870a0dc 100644 --- a/config/drivers/sensor/knob/profile/switch.c +++ b/config/drivers/sensor/knob/profile/switch.c @@ -29,15 +29,13 @@ struct knob_switch_data { bool last_report; }; -static int knob_switch_enable(const struct device *dev, struct motor_control *mc) +static int knob_switch_enable(const struct device *dev) { const struct knob_switch_config *cfg = dev->config; struct knob_switch_data *data = dev->data; motor_set_torque_limit(cfg->motor, KNOB_PROFILE_TORQUE_LIMIT); - mc->mode = ANGLE; - #if KNOB_PROFILE_HAS_VELOCITY_PID motor_set_velocity_pid(cfg->motor, KNOB_PROFILE_VELOCITY_PID); #endif /* KNOB_PROFILE_HAS_VELOCITY_PID */ @@ -46,7 +44,6 @@ static int knob_switch_enable(const struct device *dev, struct motor_control *mc motor_set_angle_pid(cfg->motor, KNOB_PROFILE_ANGLE_PID); #endif /* KNOB_PROFILE_HAS_ANGLE_PID */ - mc->target = OFF; data->state = false; return 0; @@ -65,6 +62,8 @@ static int knob_switch_tick(const struct device *dev, struct motor_control *mc) const struct knob_switch_config *cfg = dev->config; struct knob_switch_data *data = dev->data; + mc->mode = ANGLE; + float p = knob_get_position(cfg->knob); if (p < ON + (CENTER - ON) / 2.0f) { mc->target = ON;