motor_driver.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2018 Gilles DOFFE <g.doffe@gmail.com>
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
84 #include <stdbool.h>
85 
86 #include "periph/pwm.h"
87 #include "periph/gpio.h"
88 
89 #ifdef __cplusplus
90 extern "C" {
91 #endif
92 
101 #ifndef CONFIG_MOTOR_DRIVER_MAX
102 # define CONFIG_MOTOR_DRIVER_MAX (2)
103 #endif
109 typedef enum {
110  MOTOR_DRIVER_2_DIRS = 0,
112  MOTOR_DRIVER_1_DIR = 1,
117 
121 typedef enum {
122  MOTOR_CW = 0,
123  MOTOR_CCW = 1,
125 
129 typedef struct {
131  gpio_t gpio_enable;
132  gpio_t gpio_dir0;
133  union {
134  gpio_t gpio_dir1;
135  gpio_t gpio_brake;
136  };
138 } motor_t;
139 
143 typedef struct _motor_driver_t motor_driver_t;
144 
148 typedef void (*motor_set_post_cb_t)(const motor_driver_t *motor_driver,
149  uint8_t motor_id,
150  int32_t pwm_duty_cycle);
151 
155 typedef void (*motor_set_cb_t)(const motor_t *motor,
156  motor_direction_t direction);
157 
161 typedef void (*motor_brake_cb_t)(const motor_t *motor,
162  bool brake);
163 
167 typedef struct {
171  uint32_t pwm_frequency;
172  uint32_t pwm_resolution;
175  uint8_t nb_motors;
179 
185 };
186 
197 int motor_driver_init(motor_driver_t *motor_driver, const motor_driver_params_t *params);
198 
209 int motor_set(const motor_driver_t *motor_driver, uint8_t motor_id, \
210  int32_t pwm_duty_cycle);
211 
221 int motor_brake(const motor_driver_t *motor_driver, uint8_t motor_id);
222 
229 void motor_enable(const motor_driver_t *motor_driver, uint8_t motor_id);
230 
237 void motor_disable(const motor_driver_t *motor_driver, uint8_t motor_id);
238 
239 #ifdef __cplusplus
240 }
241 #endif
242 
pwm_mode_t
Definition: periph_conf.h:216
Low-level GPIO peripheral driver interface definitions.
#define CONFIG_MOTOR_DRIVER_MAX
Maximum number of motors by motor driver.
Definition: motor_driver.h:102
int motor_set(const motor_driver_t *motor_driver, uint8_t motor_id, int32_t pwm_duty_cycle)
Set motor speed and direction.
void motor_disable(const motor_driver_t *motor_driver, uint8_t motor_id)
Disable a motor of a given motor driver.
int motor_driver_init(motor_driver_t *motor_driver, const motor_driver_params_t *params)
Initialize DC motor driver board.
motor_direction_t
Describe DC motor direction states.
Definition: motor_driver.h:121
motor_driver_mode_t
Describe DC motor driver modes.
Definition: motor_driver.h:109
void motor_enable(const motor_driver_t *motor_driver, uint8_t motor_id)
Enable a motor of a given motor driver.
void(* motor_set_cb_t)(const motor_t *motor, motor_direction_t direction)
Motor set callback.
Definition: motor_driver.h:155
int motor_brake(const motor_driver_t *motor_driver, uint8_t motor_id)
Brake the motor of a given motor driver.
void(* motor_brake_cb_t)(const motor_t *motor, bool brake)
Motor brake callback.
Definition: motor_driver.h:161
void(* motor_set_post_cb_t)(const motor_driver_t *motor_driver, uint8_t motor_id, int32_t pwm_duty_cycle)
Motor callback.
Definition: motor_driver.h:148
@ MOTOR_CW
clockwise
Definition: motor_driver.h:122
@ MOTOR_CCW
counter clockwise
Definition: motor_driver.h:123
@ MOTOR_DRIVER_1_DIR
single GPIO for direction, \ no brake
Definition: motor_driver.h:112
@ MOTOR_DRIVER_1_DIR_BRAKE
single GPIO for direction, \ single GPIO for brake
Definition: motor_driver.h:114
@ MOTOR_DRIVER_2_DIRS
2 GPIOs for direction, \ handling brake
Definition: motor_driver.h:110
uint_fast8_t pwm_t
Default PWM type definition.
Definition: pwm.h:91
Low-level PWM peripheral driver interface definitions.
Motor driver.
Definition: motor_driver.h:183
const motor_driver_params_t * params
parameters
Definition: motor_driver.h:184
Describe DC motor driver with PWM device and motors array.
Definition: motor_driver.h:167
motor_driver_mode_t mode
driver mode
Definition: motor_driver.h:168
uint8_t nb_motors
number of motors
Definition: motor_driver.h:175
pwm_t pwm_dev
PWM device driving motors.
Definition: motor_driver.h:169
pwm_mode_t pwm_mode
PWM mode.
Definition: motor_driver.h:170
bool enable_inverted
if false, enable high (1), low (0) otherwise
Definition: motor_driver.h:174
bool brake_inverted
if false, brake high (1), low (0) otherwise
Definition: motor_driver.h:173
motor_set_post_cb_t motor_set_post_cb
callback post to motor_set
Definition: motor_driver.h:177
uint32_t pwm_frequency
PWM device frequency.
Definition: motor_driver.h:171
uint32_t pwm_resolution
PWM device resolution.
Definition: motor_driver.h:172
Describe DC motor with PWM channel and GPIOs.
Definition: motor_driver.h:129
gpio_t gpio_dir0
GPIO to control direction.
Definition: motor_driver.h:132
int pwm_channel
PWM channel the motor is connected to.
Definition: motor_driver.h:130
gpio_t gpio_brake
GPIO to control brake.
Definition: motor_driver.h:135
gpio_t gpio_enable
GPIO to enable/disable motor.
Definition: motor_driver.h:131
gpio_t gpio_dir1
GPIO to control direction.
Definition: motor_driver.h:134
bool gpio_dir_reverse
flag to reverse direction
Definition: motor_driver.h:137