Skip to content

Commit

Permalink
knob: Move position limit out of damped profile
Browse files Browse the repository at this point in the history
Should be common to all profiles
  • Loading branch information
xingrz committed Oct 12, 2023
1 parent 85fe36d commit 572afb0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 44 deletions.
10 changes: 10 additions & 0 deletions config/app/usb_comm/handler/handler_knob.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* SPDX-License-Identifier: MIT
*/

#include <arm_math.h>

#include "handler.h"
#include "usb_comm.pb.h"

Expand All @@ -17,6 +19,8 @@
#define KNOB_NODE DT_ALIAS(knob)
#define MOTOR_NODE DT_PHANDLE(KNOB_NODE, motor)

#define DEG(deg) (deg / 360.0f * (PI * 2.0f))

#define PROFILE_TORQUE_LIMIT(node) (float)DT_PROP_OR(node, torque_limit_mv, 0) / 1000.0f,

static const struct device *knob = DEVICE_DT_GET(KNOB_NODE);
Expand Down Expand Up @@ -125,7 +129,13 @@ static bool handle_knob_set_config(const usb_comm_MessageH2D *h2d, usb_comm_Mess
if (req->demo) {
knob_app_set_demo(true);
knob_set_mode(knob, (enum knob_mode)req->mode);
if (req->mode == usb_comm_KnobConfig_Mode_DAMPED) {
knob_set_position_limit(knob, DEG(110.0f), DEG(250.0f));
} else {
knob_set_position_limit(knob, 0.0f, 0.0f);
}
} else {
knob_set_position_limit(knob, 0.0f, 0.0f);
knob_app_set_demo(false);
}

Expand Down
2 changes: 0 additions & 2 deletions config/drivers/sensor/knob/include/knob/drivers/knob.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ enum knob_mode {

struct knob_params {
int ppr;
float position_min;
float position_max;
};

void knob_set_mode(const struct device *dev, enum knob_mode mode);
Expand Down
41 changes: 29 additions & 12 deletions config/drivers/sensor/knob/knob.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ struct knob_data {

struct knob_params params;

float position_min;
float position_max;

bool encoder_report;
int encoder_ppr;
};
Expand Down Expand Up @@ -153,20 +156,15 @@ int knob_get_encoder_ppr(const struct device *dev)
void knob_set_position_limit(const struct device *dev, float min, float max)
{
struct knob_data *data = dev->data;

data->params.position_min = min;
data->params.position_max = max;

if (data->profile != NULL) {
knob_profile_update_params(data->profile, data->params);
}
data->position_min = min;
data->position_max = max;
}

void knob_get_position_limit(const struct device *dev, float *min, float *max)
{
struct knob_data *data = dev->data;
*min = data->params.position_min;
*max = data->params.position_max;
*min = data->position_min;
*max = data->position_max;
}

float knob_get_position(const struct device *dev)
Expand Down Expand Up @@ -200,9 +198,28 @@ static void knob_thread(void *p1, void *p2, void *p3)
struct knob_data *data = dev->data;
const struct knob_config *config = dev->config;

float p;
bool limited = false;

while (1) {
if (data->profile != NULL) {
knob_profile_tick(data->profile, data->mc);
limited = false;
if (data->position_min != data->position_max) {
p = knob_get_position(dev);
if (p > data->position_max) {
data->mc->mode = ANGLE;
data->mc->target = data->position_max;
limited = true;
} else if (p < data->position_min) {
data->mc->mode = ANGLE;
data->mc->target = data->position_min;
limited = true;
}
}
if (!limited) {
knob_profile_tick(data->profile, data->mc);
}

motor_tick(config->motor);

data->delta = 0;
Expand Down Expand Up @@ -236,8 +253,6 @@ int knob_init(const struct device *dev)
data->mc = motor_get_control(config->motor);

data->params.ppr = data->encoder_ppr;
data->params.position_min = deg_to_rad(110);
data->params.position_max = deg_to_rad(250);

k_thread_create(&data->thread, data->thread_stack, CONFIG_KNOB_THREAD_STACK_SIZE,
(k_thread_entry_t)knob_thread, (void *)dev, 0, NULL,
Expand All @@ -253,6 +268,8 @@ int knob_init(const struct device *dev)
#define KNOB_INST(n) \
static struct knob_data knob_data_##n = { \
.mode = KNOB_DISABLE, \
.position_min = 0, \
.position_max = 0, \
.encoder_report = false, \
.encoder_ppr = DT_INST_PROP(n, ppr), \
}; \
Expand Down
37 changes: 7 additions & 30 deletions config/drivers/sensor/knob/profile/damped.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ struct knob_damped_config {
KNOB_PROFILE_CFG_ROM;
};

struct knob_damped_data {
float position_min;
float position_max;
};

static int knob_damped_enable(const struct device *dev)
{
const struct knob_damped_config *cfg = dev->config;
Expand All @@ -44,34 +39,18 @@ static int knob_damped_enable(const struct device *dev)

static int knob_damped_update_params(const struct device *dev, struct knob_params params)
{
struct knob_damped_data *data = dev->data;

data->position_min = params.position_min;
data->position_max = params.position_max;
ARG_UNUSED(dev);
ARG_UNUSED(params);

return 0;
}

static int knob_damped_tick(const struct device *dev, struct motor_control *mc)
{
const struct knob_damped_config *cfg = dev->config;
struct knob_damped_data *data = dev->data;

if (data->position_min == data->position_max) {
return 0;
}

float p = knob_get_position(cfg->knob);
if (p > data->position_max) {
mc->mode = ANGLE;
mc->target = data->position_max;
} else if (p < data->position_min) {
mc->mode = ANGLE;
mc->target = data->position_min;
} else {
mc->mode = VELOCITY;
mc->target = 0.0f;
}
ARG_UNUSED(dev);

mc->mode = VELOCITY;
mc->target = 0.0f;

return 0;
}
Expand All @@ -89,9 +68,7 @@ static const struct knob_profile_api knob_damped_api = {
.tick = knob_damped_tick,
};

static struct knob_damped_data knob_damped_data;

static const struct knob_damped_config knob_damped_cfg = { KNOB_PROFILE_CFG_INIT };

DEVICE_DT_INST_DEFINE(0, knob_damped_init, NULL, &knob_damped_data, &knob_damped_cfg, POST_KERNEL,
DEVICE_DT_INST_DEFINE(0, knob_damped_init, NULL, NULL, &knob_damped_cfg, POST_KERNEL,
CONFIG_SENSOR_INIT_PRIORITY, &knob_damped_api);

0 comments on commit 572afb0

Please sign in to comment.