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 
81 #include <stdbool.h>
82 
83 #include "periph/pwm.h"
84 #include "periph/gpio.h"
85 
86 #ifdef __cplusplus
87 extern "C" {
88 #endif
89 
98 #ifndef CONFIG_MOTOR_DRIVER_MAX
99 # define CONFIG_MOTOR_DRIVER_MAX (2)
100 #endif
106 typedef enum {
107  MOTOR_DRIVER_2_DIRS = 0,
109  MOTOR_DRIVER_1_DIR = 1,
114 
118 typedef enum {
119  MOTOR_CW = 0,
120  MOTOR_CCW = 1,
122 
126 typedef struct {
128  gpio_t gpio_enable;
129  gpio_t gpio_dir0;
130  union {
131  gpio_t gpio_dir1;
132  gpio_t gpio_brake;
133  };
135 } motor_t;
136 
140 typedef struct _motor_driver_t motor_driver_t;
141 
145 typedef void (*motor_set_post_cb_t)(const motor_driver_t *motor_driver,
146  uint8_t motor_id,
147  int32_t pwm_duty_cycle);
148 
152 typedef void (*motor_set_cb_t)(const motor_t *motor,
153  motor_direction_t direction);
154 
158 typedef void (*motor_brake_cb_t)(const motor_t *motor,
159  bool brake);
160 
164 typedef struct {
168  uint32_t pwm_frequency;
169  uint32_t pwm_resolution;
172  uint8_t nb_motors;
176 
182 };
183 
194 int motor_driver_init(motor_driver_t *motor_driver, const motor_driver_params_t *params);
195 
206 int motor_set(const motor_driver_t *motor_driver, uint8_t motor_id, \
207  int32_t pwm_duty_cycle);
208 
218 int motor_brake(const motor_driver_t *motor_driver, uint8_t motor_id);
219 
226 void motor_enable(const motor_driver_t *motor_driver, uint8_t motor_id);
227 
234 void motor_disable(const motor_driver_t *motor_driver, uint8_t motor_id);
235 
236 #ifdef __cplusplus
237 }
238 #endif
239 
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:99
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:118
motor_driver_mode_t
Describe DC motor driver modes.
Definition: motor_driver.h:106
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:152
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:158
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:145
@ MOTOR_CW
clockwise
Definition: motor_driver.h:119
@ MOTOR_CCW
counter clockwise
Definition: motor_driver.h:120
@ MOTOR_DRIVER_1_DIR
single GPIO for direction, \ no brake
Definition: motor_driver.h:109
@ MOTOR_DRIVER_1_DIR_BRAKE
single GPIO for direction, \ single GPIO for brake
Definition: motor_driver.h:111
@ MOTOR_DRIVER_2_DIRS
2 GPIOs for direction, \ handling brake
Definition: motor_driver.h:107
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:180
const motor_driver_params_t * params
parameters
Definition: motor_driver.h:181
Describe DC motor driver with PWM device and motors array.
Definition: motor_driver.h:164
motor_driver_mode_t mode
driver mode
Definition: motor_driver.h:165
uint8_t nb_motors
number of motors
Definition: motor_driver.h:172
pwm_t pwm_dev
PWM device driving motors.
Definition: motor_driver.h:166
pwm_mode_t pwm_mode
PWM mode.
Definition: motor_driver.h:167
bool enable_inverted
if false, enable high (1), low (0) otherwise
Definition: motor_driver.h:171
bool brake_inverted
if false, brake high (1), low (0) otherwise
Definition: motor_driver.h:170
motor_set_post_cb_t motor_set_post_cb
callback post to motor_set
Definition: motor_driver.h:174
uint32_t pwm_frequency
PWM device frequency.
Definition: motor_driver.h:168
uint32_t pwm_resolution
PWM device resolution.
Definition: motor_driver.h:169
Describe DC motor with PWM channel and GPIOs.
Definition: motor_driver.h:126
gpio_t gpio_dir0
GPIO to control direction.
Definition: motor_driver.h:129
int pwm_channel
PWM channel the motor is connected to.
Definition: motor_driver.h:127
gpio_t gpio_brake
GPIO to control brake.
Definition: motor_driver.h:132
gpio_t gpio_enable
GPIO to enable/disable motor.
Definition: motor_driver.h:128
gpio_t gpio_dir1
GPIO to control direction.
Definition: motor_driver.h:131
bool gpio_dir_reverse
flag to reverse direction
Definition: motor_driver.h:134