Skip to content

Commit 29ac065

Browse files
committed
Add latch functionality for stepper motor control
This commit introduces the latch functionality for stepper motors in the Zephyr motor driver framework. The latch feature allows the motor to hold its position after completing a movement, which is useful in applications that require precise position control without losing steps due to external forces. Changes include: - Added the `stepper_set_latch()` function to configure whether the motor should latch (hold position) after movement. - Added the `stepper_is_latch()` function to check the current latch configuration. - Updated the stepper driver API to support latch control. This functionality helps maintain the motor's position after movement, preventing unwanted drift when the motor is not in active movement. However, latching consumes more power as it keeps current flowing through the motor windings.
1 parent 6f64f77 commit 29ac065

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

drivers/stepper/gpio_stepper_controller.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct gpio_stepper_data {
3939
uint32_t delay_in_us;
4040
int32_t step_count;
4141
void *event_cb_user_data;
42+
bool latch;
4243
};
4344

4445
static int stepper_motor_set_coil_charge(const struct device *dev)
@@ -108,6 +109,11 @@ static void position_mode_task(const struct device *dev)
108109
update_coil_charge(dev);
109110
}
110111
update_remaining_steps(dev->data);
112+
if (data -> step_count == 0 && !data->latch) {
113+
for (uint8_t n_pin = 0; n_pin < NUM_CONTROL_PINS; n_pin++) {
114+
(void)gpio_pin_set_dt(&config->control_pins[n_pin], 0);
115+
}
116+
}
111117
}
112118

113119
static void velocity_mode_task(const struct device *dev)
@@ -296,6 +302,20 @@ static int gpio_stepper_enable(const struct device *dev, bool enable)
296302
return 0;
297303
}
298304

305+
static int gpio_stepper_set_latch(const struct device *dev, bool is_latch_after_movement)
306+
{
307+
struct gpio_stepper_data *data = dev->data;
308+
data->latch = is_latch_after_movement;
309+
return 0;
310+
}
311+
312+
static int gpio_stepper_is_latch(const struct device *dev, bool* latching_state)
313+
{
314+
struct gpio_stepper_data *data = dev->data;
315+
*latching_state = data->latch;
316+
return 0;
317+
}
318+
299319
static int gpio_stepper_motor_controller_init(const struct device *dev)
300320
{
301321
struct gpio_stepper_data *data = dev->data;
@@ -340,6 +360,8 @@ static int gpio_stepper_motor_controller_init(const struct device *dev)
340360
.enable_constant_velocity_mode = gpio_stepper_enable_constant_velocity_mode, \
341361
.set_micro_step_res = gpio_stepper_set_micro_step_res, \
342362
.get_micro_step_res = gpio_stepper_get_micro_step_res, \
363+
.set_latch = gpio_stepper_set_latch, \
364+
.is_latch = gpio_stepper_is_latch, \
343365
.set_event_callback = gpio_stepper_set_event_callback, };
344366

345367
#define GPIO_STEPPER_DEVICE_DEFINE(child) \

include/zephyr/drivers/stepper.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,22 @@ typedef void (*stepper_event_callback_t)(const struct device *dev, const enum st
184184
typedef int (*stepper_set_event_callback_t)(const struct device *dev,
185185
stepper_event_callback_t callback, void *user_data);
186186

187+
/**
188+
* @brief Set the stepper latch after movement.
189+
*
190+
* @see stepper_set_latch() for details.
191+
*/
192+
typedef int (*stepper_set_latch_t)(const struct device *dev,
193+
const bool is_latch_after_movement);
194+
195+
196+
/**
197+
* @brief Get the stepper latch state.
198+
*
199+
* @see stepper_is_latch() for details.
200+
*/
201+
typedef int (*stepper_is_latch_t)(const struct device *dev, bool* is_latch_after_movement);
202+
187203
/**
188204
* @brief Stepper Motor Controller API
189205
*/
@@ -199,6 +215,8 @@ __subsystem struct stepper_driver_api {
199215
stepper_is_moving_t is_moving;
200216
stepper_enable_constant_velocity_mode_t enable_constant_velocity_mode;
201217
stepper_set_event_callback_t set_event_callback;
218+
stepper_set_latch_t set_latch;
219+
stepper_is_latch_t is_latch;
202220
};
203221

204222
/**
@@ -462,6 +480,36 @@ static inline int z_impl_stepper_set_callback(const struct device *dev,
462480
return api->set_event_callback(dev, callback, user_data);
463481
}
464482

483+
/**
484+
* @brief Set whether the stepper motor should latch after movement.
485+
*
486+
* @param dev Pointer to the device structure for the stepper motor.
487+
* @param is_latch_after_movement Boolean indicating whether the motor should latch after movement.
488+
* @retval -EIO General input / output error
489+
* @retval 0 Success
490+
*/
491+
__syscall int stepper_set_latch(const struct device *dev, bool is_latch_after_movement);
492+
493+
static inline int z_impl_stepper_set_latch(const struct device *dev, bool is_latch_after_movement) {
494+
const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
495+
return api->set_latch(dev, is_latch_after_movement);
496+
}
497+
498+
/**
499+
* @brief Get the current latch configuration of the stepper motor.
500+
*
501+
* @param dev Pointer to the device structure for the stepper motor.
502+
* @param latching_state Pointer to a boolean where the latch state will be stored.
503+
* @retval -EIO General input / output error
504+
* @retval 0 Success
505+
*/
506+
__syscall int stepper_is_latch(const struct device *dev, bool* latching_state);
507+
508+
static inline int z_impl_stepper_is_latch(const struct device *dev, bool* latching_state) {
509+
const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
510+
return api->is_latch(dev, latching_state);
511+
}
512+
465513
/**
466514
* @}
467515
*/

0 commit comments

Comments
 (0)