Skip to content

Commit

Permalink
knob: No need to reset motor_control in every profile enable
Browse files Browse the repository at this point in the history
Instead, set it every tick.
  • Loading branch information
xingrz committed Oct 12, 2023
1 parent a27909c commit 85fe36d
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 46 deletions.
6 changes: 3 additions & 3 deletions config/drivers/sensor/knob/include/knob/drivers/profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ 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;

if (api->enable == NULL) {
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)
Expand Down
3 changes: 1 addition & 2 deletions config/drivers/sensor/knob/knob.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 1 addition & 5 deletions config/drivers/sensor/knob/profile/damped.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion config/drivers/sensor/knob/profile/disable.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 3 additions & 5 deletions config/drivers/sensor/knob/profile/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
}

Expand All @@ -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;
Expand Down
8 changes: 3 additions & 5 deletions config/drivers/sensor/knob/profile/inertia.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
Expand All @@ -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;
Expand Down
15 changes: 5 additions & 10 deletions config/drivers/sensor/knob/profile/ratchet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
10 changes: 4 additions & 6 deletions config/drivers/sensor/knob/profile/spin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
}

Expand All @@ -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;
}
Expand Down
9 changes: 4 additions & 5 deletions config/drivers/sensor/knob/profile/spring.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
7 changes: 3 additions & 4 deletions config/drivers/sensor/knob/profile/switch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 85fe36d

Please sign in to comment.