motor_driver.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Gilles DOFFE <g.doffe@gmail.com>
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
84 #ifndef MOTOR_DRIVER_H
85 #define MOTOR_DRIVER_H
86 
87 #include "periph/pwm.h"
88 #include "periph/gpio.h"
89 
90 #ifdef __cplusplus
91 extern "C" {
92 #endif
93 
102 #ifndef CONFIG_MOTOR_DRIVER_MAX
103 #define CONFIG_MOTOR_DRIVER_MAX (2)
104 #endif
105 
110 #define MOTOR_DRIVER_DEV(x) (x)
111 
115 typedef enum {
123 
127 typedef enum {
131 
135 typedef enum {
136  MOTOR_CW = 0,
137  MOTOR_CCW = 1,
139 
143 typedef struct {
145  gpio_t gpio_enable;
146  gpio_t gpio_dir0;
151 } motor_t;
152 
156 typedef unsigned int motor_driver_t;
157 
161 typedef void (*motor_driver_cb_t)(const motor_driver_t motor_driver,
162  uint8_t motor_id,
163  int32_t pwm_duty_cycle);
164 
168 typedef struct {
173  uint32_t pwm_frequency;
174  uint32_t pwm_resolution;
175  uint8_t nb_motors;
179 
188 int motor_driver_init(const motor_driver_t motor_driver);
189 
200 int motor_set(const motor_driver_t motor_driver, uint8_t motor_id, \
201  int32_t pwm_duty_cycle);
202 
212 int motor_brake(const motor_driver_t motor_driver, uint8_t motor_id);
213 
222 void motor_enable(const motor_driver_t motor_driver, uint8_t motor_id);
223 
232 void motor_disable(const motor_driver_t motor_driver, uint8_t motor_id);
233 
234 #ifdef __cplusplus
235 }
236 #endif
237 
238 #endif /* MOTOR_DRIVER_H */
239 
motor_driver_mode_brake_t
motor_driver_mode_brake_t
Describe DC motor driver brake modes.
Definition: motor_driver.h:127
motor_driver_config_t::pwm_dev
pwm_t pwm_dev
PWM device driving motors.
Definition: motor_driver.h:169
motor_t
Describe DC motor with PWM channel and GPIOs.
Definition: motor_driver.h:143
motor_driver_cb_t
void(* motor_driver_cb_t)(const motor_driver_t motor_driver, uint8_t motor_id, int32_t pwm_duty_cycle)
Motor callback.
Definition: motor_driver.h:161
motor_t::pwm_channel
int pwm_channel
PWM channel the motor is connected to.
Definition: motor_driver.h:144
MOTOR_BRAKE_HIGH
@ MOTOR_BRAKE_HIGH
High stage brake.
Definition: motor_driver.h:129
motor_disable
void motor_disable(const motor_driver_t motor_driver, uint8_t motor_id)
Disable a motor of a given motor driver.
MOTOR_DRIVER_1_DIR
@ MOTOR_DRIVER_1_DIR
Single GPIO for direction, \ no BRAKE.
Definition: motor_driver.h:118
MOTOR_BRAKE_LOW
@ MOTOR_BRAKE_LOW
Low stage brake.
Definition: motor_driver.h:128
motor_t::gpio_dir_reverse
uint8_t gpio_dir_reverse
flag to reverse direction
Definition: motor_driver.h:148
motor_set
int motor_set(const motor_driver_t motor_driver, uint8_t motor_id, int32_t pwm_duty_cycle)
Set motor speed and direction.
MOTOR_CCW
@ MOTOR_CCW
counter clockwise
Definition: motor_driver.h:137
motor_driver_config_t::mode
motor_driver_mode_t mode
driver mode
Definition: motor_driver.h:170
motor_driver_config_t::pwm_frequency
uint32_t pwm_frequency
PWM device frequency.
Definition: motor_driver.h:173
motor_driver_t
unsigned int motor_driver_t
Default motor driver type definition.
Definition: motor_driver.h:156
motor_direction_t
motor_direction_t
Describe DC motor direction states.
Definition: motor_driver.h:135
motor_driver_config_t::pwm_mode
pwm_mode_t pwm_mode
PWM mode.
Definition: motor_driver.h:172
motor_enable
void motor_enable(const motor_driver_t motor_driver, uint8_t motor_id)
Enable a motor of a given motor driver.
MOTOR_DRIVER_2_DIRS
@ MOTOR_DRIVER_2_DIRS
2 GPIOS for direction, \ handling BRAKE
Definition: motor_driver.h:116
motor_driver_config_t::pwm_resolution
uint32_t pwm_resolution
PWM device resolution.
Definition: motor_driver.h:174
motor_driver_config_t
Describe DC motor driver with PWM device and motors array.
Definition: motor_driver.h:168
motor_driver_init
int motor_driver_init(const motor_driver_t motor_driver)
Initialize DC motor driver board.
MOTOR_DRIVER_1_DIR_BRAKE
@ MOTOR_DRIVER_1_DIR_BRAKE
Single GPIO for direction, \ Single GPIO for BRAKE.
Definition: motor_driver.h:120
motor_driver_config_t::cb
motor_driver_cb_t cb
callback on motor_set
Definition: motor_driver.h:177
motor_brake
int motor_brake(const motor_driver_t motor_driver, uint8_t motor_id)
Brake the motor of a given motor driver.
motor_t::gpio_enable_invert
uint8_t gpio_enable_invert
flag to set enable GPIO inverted mode
Definition: motor_driver.h:149
gpio.h
Low-level GPIO peripheral driver interface definitions.
motor_driver_mode_t
motor_driver_mode_t
Describe DC motor driver modes.
Definition: motor_driver.h:115
motor_t::gpio_dir1_or_brake
gpio_t gpio_dir1_or_brake
GPIO to control rotation direction.
Definition: motor_driver.h:147
motor_driver_config_t::mode_brake
motor_driver_mode_brake_t mode_brake
driver brake mode
Definition: motor_driver.h:171
CONFIG_MOTOR_DRIVER_MAX
#define CONFIG_MOTOR_DRIVER_MAX
Maximum number of motors by motor driver.
Definition: motor_driver.h:103
motor_t::gpio_dir0
gpio_t gpio_dir0
GPIO to control rotation direction.
Definition: motor_driver.h:146
pwm.h
Low-level PWM peripheral driver interface definitions.
pwm_t
unsigned int pwm_t
Default PWM type definition.
Definition: pwm.h:92
pwm_mode_t
pwm_mode_t
Definition: periph_cpu.h:159
motor_t::gpio_enable
gpio_t gpio_enable
GPIO to enable/disable motor.
Definition: motor_driver.h:145
motor_t::gpio_brake_invert
uint8_t gpio_brake_invert
flag to make brake active low
Definition: motor_driver.h:150
motor_driver_config_t::nb_motors
uint8_t nb_motors
number of moros
Definition: motor_driver.h:175
MOTOR_CW
@ MOTOR_CW
clockwise
Definition: motor_driver.h:136