MotorController
motor.h
Go to the documentation of this file.
1 /** @file */
2 
3 #ifndef MOTOR_H
4 #define MOTOR_H
5 
6 #include "hal.h"
7 
8 /** Minimal value that makes the robot move */
9 #define MIN_COMMAND 1
10 /** Maximum possible value for commands */
11 #define MAX_COMMAND 100
12 
13 /**
14  * @brief Alias to select the motor to work on.
15  *
16  * @remark The meaning of the value "LEFT" and "RIGHT" is not necessarily
17  * coherent with the actual mechanical organization of the robot, it
18  * depends on initial user choice.
19  */
20 typedef enum {
21  MOTOR_LEFT = 0U, /**< Left motor */
22  MOTOR_RIGHT = 1U /**< Right motor */
23 } motor_t;
24 
25 /**
26  * @brief Alias for motor rotation sense.
27  */
28 typedef enum {
29  FORWARD = 0U, /**< Motor turns so that robot moves forward */
30  BACKWARD = 1U /**< Motor turns so that robot moves backward */
32 
33 /**
34  * @brief Alias for motor orientation.
35  *
36  * @remark These values allow to hide mechanical differences in motor installation
37  * so that a FORWARD move makes the motor turn in the proper sense.
38  * Values are totally arbitrary.
39  */
40 typedef enum {
41  DIRECTION_1 = 0U, /**< Value 1*/
42  DIRECTION_2 = 1U /**< Value 2*/
44 
45 /**
46  * @brief Initialise the motor driver.
47  *
48  * @details This function is in charge of enabling all the low-level components
49  * used to control the motors. It configures the timers used to
50  * generate the PWM and starts them. It also sets the direction pins
51  * so that the motors turn forward as defined by the parameters.
52  *
53  * @param[in] motor_left_forward_sense The rotation direction which corresponds
54  * to a forward movement of the left motor.
55  * @param[in] motor_right_forward_sense The rotation direction which corresponds
56  * to a forward movement of the right motor.
57  *
58  * @note To provide a high-level interface to control the motors, this driver
59  * uses the values FORWARD and BACKWARD to describe the rotation direction
60  * of the motors. However, these directions depend on how the motors are
61  * placed in the robot and how they are wired on the H-bridge. In order to
62  * foster reusability of this driver, the mapping between pins state and
63  * rotation direction is settable.
64  * The 2 parameters of this init function are for this purpose.
65  * DIRECTION_1 corresponds to PIN_A at '1' (+3V3) and PIN_B at '0' (GND).
66  * DIRECTION_2 corresponds to PIN_A at '0' (GND) and PIN_B at '1' (+3V3).
67  *
68  * @warning This function must be called first to start the motor driver.
69  */
70 extern void motor_init(motor_sense_t motor_left_forward_sense, motor_sense_t motor_right_forward_sense);
71 
72 /**
73  * @brief Set the rotation speed of the specified motor.
74  *
75  * @param[in] motor The motor to change the speed of.
76  * @param[in] speed The requested speed. Allowed range is [0,MAX_COMMAND]. Be careful
77  * that a minimum non-zero value (MIN_COMMAND) is required for the robot to
78  * move.
79  *
80  * @return An int indicating success, else an error code.
81  * @retval 0 Success.
82  * @retval -1 Invalid 'motor' parameter.
83  * @retval -2 Invalid 'speed' parameter (out of range).
84  */
85 extern int motor_set_speed(motor_t motor, uint8_t speed);
86 
87 /**
88  * @brief Get the rotation speed of the specified motor.
89  *
90  * @param[in] motor The motor to get the speed of.
91  *
92  * @return The rotation speed of the specified motor or -1 in case of error.
93  */
94 extern int8_t motor_get_speed(motor_t motor);
95 
96 /**
97  * @brief Get the current rotation direction of the specified motor.
98  *
99  * @param[in] motor The motor to look at.
100  *
101  * @return The current rotation direction of the specified motor.
102  * @retval FORWARD/BACKWARD
103  * @retval -1 Invalid 'motor' parameter.
104  */
106 
107 /**
108  * @brief Set the rotation direction of the specified motor.
109  *
110  * @param[in] motor To motor to set the direction of.
111  * @param[in] direction The requested rotation direction.
112  *
113  * @return An int indicating success, else an error code.
114  * @retval 0 Success.
115  * @retval -1 Invalid 'motor' parameter.
116  * @retval -2 Invalid 'direction' parameter (out of range).
117  */
118 extern int motor_set_direction(motor_t motor, motor_direction_t direction);
119 
120 /**
121  * @brief Revert the rotation direction of the specified motor.
122  *
123  * @param[in] motor To motor to change the rotation direction of.
124  */
125 extern void motor_toggle_direction(motor_t motor);
126 
127 #endif /* MOTOR_H */
int motor_set_speed(motor_t motor, uint8_t speed)
Set the rotation speed of the specified motor.
Definition: motor.c:77
int motor_set_direction(motor_t motor, motor_direction_t direction)
Set the rotation direction of the specified motor.
Definition: motor.c:123
motor_direction_t
Alias for motor rotation sense.
Definition: motor.h:28
Definition: motor.h:30
motor_t
Alias to select the motor to work on.
Definition: motor.h:20
motor_sense_t
Alias for motor orientation.
Definition: motor.h:40
void motor_init(motor_sense_t motor_left_forward_sense, motor_sense_t motor_right_forward_sense)
Initialise the motor driver.
Definition: motor.c:48
void motor_toggle_direction(motor_t motor)
Revert the rotation direction of the specified motor.
Definition: motor.c:145
int8_t motor_get_speed(motor_t motor)
Get the rotation speed of the specified motor.
Definition: motor.c:103
Definition: motor.h:29
motor_direction_t motor_get_direction(motor_t motor)
Get the current rotation direction of the specified motor.
Definition: motor.c:114