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 
9 #pragma once
10 
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 #define MOTOR_DRIVER_DEV(x) (x)
110 
114 typedef enum {
115  MOTOR_DRIVER_2_DIRS = 0,
117  MOTOR_DRIVER_1_DIR = 1,
122 
126 typedef enum {
130 
134 typedef enum {
135  MOTOR_CW = 0,
136  MOTOR_CCW = 1,
138 
142 typedef struct {
144  gpio_t gpio_enable;
145  gpio_t gpio_dir0;
150 } motor_t;
151 
155 typedef unsigned int motor_driver_t;
156 
160 typedef void (*motor_driver_cb_t)(const motor_driver_t motor_driver,
161  uint8_t motor_id,
162  int32_t pwm_duty_cycle);
163 
167 typedef struct {
172  uint32_t pwm_frequency;
173  uint32_t pwm_resolution;
174  uint8_t nb_motors;
178 
187 int motor_driver_init(const motor_driver_t motor_driver);
188 
199 int motor_set(const motor_driver_t motor_driver, uint8_t motor_id, \
200  int32_t pwm_duty_cycle);
201 
211 int motor_brake(const motor_driver_t motor_driver, uint8_t motor_id);
212 
219 void motor_enable(const motor_driver_t motor_driver, uint8_t motor_id);
220 
227 void motor_disable(const motor_driver_t motor_driver, uint8_t motor_id);
228 
229 #ifdef __cplusplus
230 }
231 #endif
232 
pwm_mode_t
Definition: periph_conf.h:219
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_brake(const motor_driver_t motor_driver, uint8_t motor_id)
Brake the motor of a given motor driver.
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:160
motor_direction_t
Describe DC motor direction states.
Definition: motor_driver.h:134
unsigned int motor_driver_t
Default motor driver type definition.
Definition: motor_driver.h:155
motor_driver_mode_t
Describe DC motor driver modes.
Definition: motor_driver.h:114
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.
void motor_enable(const motor_driver_t motor_driver, uint8_t motor_id)
Enable a motor of a given motor driver.
int motor_driver_init(const motor_driver_t motor_driver)
Initialize DC motor driver board.
motor_driver_mode_brake_t
Describe DC motor driver brake modes.
Definition: motor_driver.h:126
@ MOTOR_CW
clockwise
Definition: motor_driver.h:135
@ MOTOR_CCW
counter clockwise
Definition: motor_driver.h:136
@ MOTOR_DRIVER_1_DIR
Single GPIO for direction, \ no BRAKE.
Definition: motor_driver.h:117
@ MOTOR_DRIVER_1_DIR_BRAKE
Single GPIO for direction, \ Single GPIO for BRAKE.
Definition: motor_driver.h:119
@ MOTOR_DRIVER_2_DIRS
2 GPIOS for direction, \ handling BRAKE
Definition: motor_driver.h:115
@ MOTOR_BRAKE_LOW
Low stage brake.
Definition: motor_driver.h:127
@ MOTOR_BRAKE_HIGH
High stage brake.
Definition: motor_driver.h:128
uint_fast8_t pwm_t
Default PWM type definition.
Definition: pwm.h:91
Low-level PWM peripheral driver interface definitions.
Describe DC motor driver with PWM device and motors array.
Definition: motor_driver.h:167
uint32_t pwm_frequency
PWM device frequency.
Definition: motor_driver.h:172
pwm_mode_t pwm_mode
PWM mode.
Definition: motor_driver.h:171
motor_driver_mode_brake_t mode_brake
driver brake mode
Definition: motor_driver.h:170
motor_driver_cb_t cb
callback on motor_set
Definition: motor_driver.h:176
motor_driver_mode_t mode
driver mode
Definition: motor_driver.h:169
uint8_t nb_motors
number of moros
Definition: motor_driver.h:174
uint32_t pwm_resolution
PWM device resolution.
Definition: motor_driver.h:173
pwm_t pwm_dev
PWM device driving motors.
Definition: motor_driver.h:168
Describe DC motor with PWM channel and GPIOs.
Definition: motor_driver.h:142
uint8_t gpio_enable_invert
flag to set enable GPIO inverted mode
Definition: motor_driver.h:148
gpio_t gpio_dir0
GPIO to control rotation direction.
Definition: motor_driver.h:145
uint8_t gpio_brake_invert
flag to make brake active low
Definition: motor_driver.h:149
int pwm_channel
PWM channel the motor is connected to.
Definition: motor_driver.h:143
gpio_t gpio_enable
GPIO to enable/disable motor.
Definition: motor_driver.h:144
uint8_t gpio_dir_reverse
flag to reverse direction
Definition: motor_driver.h:147
gpio_t gpio_dir1_or_brake
GPIO to control rotation direction.
Definition: motor_driver.h:146