Robot Control Library
motor.h
Go to the documentation of this file.
1 /**
2  * <rc/motor.h>
3  *
4  * @brief Control 4 DC motor Channels
5  *
6  * The robotics cape can drive 4 DC motors bidirectionally powered only from a
7  * 2-cell lithium battery pack connected to the cape. The motors will not draw
8  * power from USB or the 9-18v DC Jack. Each channel can support 1.2A continuous
9  * and the user must be careful to choose motors which will not exceed this
10  * rating when stalled. Each channel is broken out on an independent 2-pin JST
11  * ZH connector.
12  *
13  * @author James Strawson
14  * @date 1/31/2018
15  *
16  * @addtogroup Motor
17  * @{
18  */
19 
20 #ifndef RC_MOTOR_H
21 #define RC_MOTOR_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #define RC_MOTOR_DEFAULT_PWM_FREQ 25000 ///< 25kHz
28 
29 
30 /**
31  * @brief Initializes all 4 motors and leaves them in a free-spin (0
32  * throttle) state.
33  *
34  * Note, if the user is optionally using the rc_motor_standby functionality they
35  * should be aware that rc_motor_init starts standby mode in a disabled state so
36  * it is not necessary for the majority of users who are not interested in
37  * standby mode.
38  *
39  * This starts the motor drivers at RC_MOTOR_DEFAULT_PWM_FREQ. To use another
40  * frequency initialize with rc_motor_init_freq instead.
41  *
42  * @return 0 on success, -1 on failure which is usually due to lack of user
43  * permissions to access the gpio and pwm systems.
44  */
45 int rc_motor_init(void);
46 
47 
48 /**
49  * @brief Just like rc_motor_init but allows the user to set the pwm
50  * frequency
51  *
52  * RC_MOTOR_DEFAULT_PWM_FREQ is a good frequency to start at.
53  *
54  * Note, if the user is optionally using the rc_motor_standby functionality they
55  * should be aware that rc_motor_init starts standby mode in a disabled state so
56  * it is not necessary for the majority of users who are not interested in
57  * standby mode.
58  *
59  * @param[in] pwm_frequency_hz The pwm frequency in hz
60  *
61  * @return 0 on success, -1 on failure which is usually due to lack of user
62  * permissions to access the gpio and pwm systems.
63  */
64 int rc_motor_init_freq(int pwm_frequency_hz);
65 
66 
67 /**
68  * @brief Puts all 4 motors into a free-spin (0 throttle) state, puts the
69  * h-bridges into standby mode, and closes all file pointers to GPIO and PWM
70  * systems.
71  *
72  * @return 0 on success, -1 on failure.
73  */
74 int rc_motor_cleanup(void);
75 
76 
77 /**
78  * @brief Toggle the H-bridges in and out of low-power standby mode.
79  *
80  * This is an entirely optional function as calling rc_motor_cleanup when your
81  * program closes will put the h-bridges into standby mode and then calling
82  * rc_motor_init at the beginning of your program will take them out of standby
83  * mode. However, if the user wishes to toggle in and out of standby mode in
84  * their program (for example while paused) then they can use this function.
85  *
86  * @param[in] standby_en 1 to enable standby mode, 0 to disable
87  *
88  * @return 0 on success, -1 on failure.
89  */
90 int rc_motor_standby(int standby_en);
91 
92 
93 /**
94  * @brief Sets the bidirectional duty cycle (power) to a single motor or
95  * all motors if 0 is provided as a channel.
96  *
97  * @param[in] ch The motor channel (1-4) or 0 for all channels.
98  * @param[in] duty Duty cycle, -1.0 for full reverse, 1.0 for full forward
99  *
100  * @return 0 on success, -1 on failure
101  */
102 int rc_motor_set(int ch, double duty);
103 
104 
105 /**
106  * @brief Puts a motor into a zero-throttle state allowing it to spin
107  * freely.
108  *
109  * This is accomplished by putting both motor terminals connected to the
110  * h-bridge into a high-impedance state.
111  *
112  * @param[in] ch The motor channel (1-4) or 0 for all channels.
113  *
114  * @return 0 on success, -1 on failure
115  */
116 int rc_motor_free_spin(int ch);
117 
118 
119 /**
120  * @brief Connects the motor terminal pairs together which makes the motor
121  * fight against its own back EMF turning it into a brake resisting rotation.
122  *
123  * @param[in] ch The motor channel (1-4) or 0 for all channels.
124  *
125  * @return 0 on success, -1 on failure
126  */
127 int rc_motor_brake(int ch);
128 
129 
130 
131 #ifdef __cplusplus
132 }
133 #endif
134 
135 #endif // RC_MOTOR_H
136 
137 /** @} end group Motor */
int rc_motor_brake(int ch)
Connects the motor terminal pairs together which makes the motor fight against its own back EMF turni...
int rc_motor_set(int ch, double duty)
Sets the bidirectional duty cycle (power) to a single motor or all motors if 0 is provided as a chann...
int rc_motor_free_spin(int ch)
Puts a motor into a zero-throttle state allowing it to spin freely.
int rc_motor_init_freq(int pwm_frequency_hz)
Just like rc_motor_init but allows the user to set the pwm frequency.
int rc_motor_init(void)
Initializes all 4 motors and leaves them in a free-spin (0 throttle) state.
int rc_motor_standby(int standby_en)
Toggle the H-bridges in and out of low-power standby mode.
int rc_motor_cleanup(void)
Puts all 4 motors into a free-spin (0 throttle) state, puts the h-bridges into standby mode...