drivers: stepper: refactor stepper move_to and move_by calls
Refactored stepper move_to calls and move_by calls. Now, relative movement required or absolute target position is calculated and then redirected to move_by or move_to calls respectively. Signed-off-by: Dipak Shetty <shetty.dipak@gmx.com>
This commit is contained in:
parent
efdca63c25
commit
8f6b0b2e2e
3 changed files with 24 additions and 74 deletions
|
@ -338,53 +338,6 @@ static int tmc50xx_stepper_is_moving(const struct device *dev, bool *is_moving)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int tmc50xx_stepper_move_by(const struct device *dev, const int32_t micro_steps)
|
|
||||||
{
|
|
||||||
const struct tmc50xx_stepper_config *config = dev->config;
|
|
||||||
struct tmc50xx_stepper_data *data = dev->data;
|
|
||||||
int err;
|
|
||||||
|
|
||||||
if (config->is_sg_enabled) {
|
|
||||||
err = stallguard_enable(dev, false);
|
|
||||||
if (err != 0) {
|
|
||||||
return -EIO;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t position;
|
|
||||||
|
|
||||||
err = stepper_get_actual_position(dev, &position);
|
|
||||||
if (err != 0) {
|
|
||||||
return -EIO;
|
|
||||||
}
|
|
||||||
int32_t target_position = position + micro_steps;
|
|
||||||
|
|
||||||
err = tmc50xx_write(config->controller, TMC50XX_RAMPMODE(config->index),
|
|
||||||
TMC5XXX_RAMPMODE_POSITIONING_MODE);
|
|
||||||
if (err != 0) {
|
|
||||||
return -EIO;
|
|
||||||
}
|
|
||||||
LOG_DBG("Stepper motor controller %s moved to %d by steps: %d", dev->name, target_position,
|
|
||||||
micro_steps);
|
|
||||||
err = tmc50xx_write(config->controller, TMC50XX_XTARGET(config->index), target_position);
|
|
||||||
if (err != 0) {
|
|
||||||
return -EIO;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config->is_sg_enabled) {
|
|
||||||
k_work_reschedule(&data->stallguard_dwork,
|
|
||||||
K_MSEC(config->sg_velocity_check_interval_ms));
|
|
||||||
}
|
|
||||||
#ifdef CONFIG_STEPPER_ADI_TMC50XX_RAMPSTAT_POLL
|
|
||||||
if (data->callback) {
|
|
||||||
k_work_reschedule(
|
|
||||||
&data->rampstat_callback_dwork,
|
|
||||||
K_MSEC(CONFIG_STEPPER_ADI_TMC50XX_RAMPSTAT_POLL_INTERVAL_IN_MSEC));
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int tmc50xx_stepper_set_max_velocity(const struct device *dev, uint32_t velocity)
|
int tmc50xx_stepper_set_max_velocity(const struct device *dev, uint32_t velocity)
|
||||||
{
|
{
|
||||||
const struct tmc50xx_stepper_config *config = dev->config;
|
const struct tmc50xx_stepper_config *config = dev->config;
|
||||||
|
@ -497,7 +450,7 @@ static int tmc50xx_stepper_get_actual_position(const struct device *dev, int32_t
|
||||||
|
|
||||||
static int tmc50xx_stepper_move_to(const struct device *dev, const int32_t micro_steps)
|
static int tmc50xx_stepper_move_to(const struct device *dev, const int32_t micro_steps)
|
||||||
{
|
{
|
||||||
LOG_DBG("Stepper motor controller %s set target position to %d", dev->name, micro_steps);
|
LOG_DBG("%s set target position to %d", dev->name, micro_steps);
|
||||||
const struct tmc50xx_stepper_config *config = dev->config;
|
const struct tmc50xx_stepper_config *config = dev->config;
|
||||||
struct tmc50xx_stepper_data *data = dev->data;
|
struct tmc50xx_stepper_data *data = dev->data;
|
||||||
int err;
|
int err;
|
||||||
|
@ -530,6 +483,22 @@ static int tmc50xx_stepper_move_to(const struct device *dev, const int32_t micro
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int tmc50xx_stepper_move_by(const struct device *dev, const int32_t micro_steps)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
int32_t position;
|
||||||
|
|
||||||
|
err = stepper_get_actual_position(dev, &position);
|
||||||
|
if (err != 0) {
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
int32_t target_position = position + micro_steps;
|
||||||
|
|
||||||
|
LOG_DBG("%s moved to %d by steps: %d", dev->name, target_position, micro_steps);
|
||||||
|
|
||||||
|
return tmc50xx_stepper_move_to(dev, target_position);
|
||||||
|
}
|
||||||
|
|
||||||
static int tmc50xx_stepper_run(const struct device *dev, const enum stepper_direction direction)
|
static int tmc50xx_stepper_run(const struct device *dev, const enum stepper_direction direction)
|
||||||
{
|
{
|
||||||
LOG_DBG("Stepper motor controller %s run", dev->name);
|
LOG_DBG("Stepper motor controller %s run", dev->name);
|
||||||
|
|
|
@ -223,23 +223,12 @@ static int gpio_stepper_get_actual_position(const struct device *dev, int32_t *p
|
||||||
static int gpio_stepper_move_to(const struct device *dev, int32_t micro_steps)
|
static int gpio_stepper_move_to(const struct device *dev, int32_t micro_steps)
|
||||||
{
|
{
|
||||||
struct gpio_stepper_data *data = dev->data;
|
struct gpio_stepper_data *data = dev->data;
|
||||||
|
int32_t steps_to_move;
|
||||||
|
|
||||||
if (!data->is_enabled) {
|
|
||||||
LOG_ERR("Stepper motor is not enabled");
|
|
||||||
return -ECANCELED;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data->delay_in_ns == 0) {
|
|
||||||
LOG_ERR("Step interval not set or invalid step interval set");
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
K_SPINLOCK(&data->lock) {
|
K_SPINLOCK(&data->lock) {
|
||||||
data->run_mode = STEPPER_RUN_MODE_POSITION;
|
steps_to_move = micro_steps - data->actual_position;
|
||||||
data->step_count = micro_steps - data->actual_position;
|
|
||||||
update_direction_from_step_count(dev);
|
|
||||||
(void)k_work_reschedule(&data->stepper_dwork, K_NO_WAIT);
|
|
||||||
}
|
}
|
||||||
return 0;
|
return gpio_stepper_move_by(dev, steps_to_move);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int gpio_stepper_is_moving(const struct device *dev, bool *is_moving)
|
static int gpio_stepper_is_moving(const struct device *dev, bool *is_moving)
|
||||||
|
|
|
@ -288,22 +288,14 @@ int step_dir_stepper_common_get_actual_position(const struct device *dev, int32_
|
||||||
int step_dir_stepper_common_move_to(const struct device *dev, const int32_t value)
|
int step_dir_stepper_common_move_to(const struct device *dev, const int32_t value)
|
||||||
{
|
{
|
||||||
struct step_dir_stepper_common_data *data = dev->data;
|
struct step_dir_stepper_common_data *data = dev->data;
|
||||||
const struct step_dir_stepper_common_config *config = dev->config;
|
int32_t steps_to_move;
|
||||||
|
|
||||||
if (data->microstep_interval_ns == 0) {
|
|
||||||
LOG_ERR("Step interval not set or invalid step interval set");
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/* Calculate the relative movement required */
|
||||||
K_SPINLOCK(&data->lock) {
|
K_SPINLOCK(&data->lock) {
|
||||||
data->run_mode = STEPPER_RUN_MODE_POSITION;
|
steps_to_move = value - data->actual_position;
|
||||||
data->step_count = value - data->actual_position;
|
|
||||||
config->timing_source->update(dev, data->microstep_interval_ns);
|
|
||||||
update_direction_from_step_count(dev);
|
|
||||||
config->timing_source->start(dev);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return step_dir_stepper_common_move_by(dev, steps_to_move);
|
||||||
}
|
}
|
||||||
|
|
||||||
int step_dir_stepper_common_is_moving(const struct device *dev, bool *is_moving)
|
int step_dir_stepper_common_is_moving(const struct device *dev, bool *is_moving)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue