-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathRedBotMotors.cpp
More file actions
313 lines (279 loc) · 7.38 KB
/
Copy pathRedBotMotors.cpp
File metadata and controls
313 lines (279 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/****************************************************************
Main CPP for RedBot motor control.
This code is beerware; if you use it, please buy me (or any other
SparkFun employee) a cold beverage next time you run into one of
us at the local.
21 Jan 2014- Mike Hord, SparkFun Electronics
Code developed in Arduino 1.0.5, on an SparkFun Redbot v12.
****************************************************************/
#include "RedBot.h"
#include <Arduino.h>
extern RedBotEncoder *encoderObject; // Declared in RedBotEncoder.cpp
// Constructor. Mostly for pin setup; note that it's not necessary to configure
// PWM pins as they will be automatically configured with the analogWrite()
// function is called.
RedBotMotors::RedBotMotors()
{
// The interface to the motor driver is kind of ugly. It's three pins per
// channel: two that define role (forward, reverse, stop, brake) and one
// PWM input for speed.
pinMode(R_CTRL_1, OUTPUT);
pinMode(R_CTRL_2, OUTPUT);
pinMode(L_CTRL_1, OUTPUT);
pinMode(L_CTRL_2, OUTPUT);
}
// stop() allows the motors to coast to a stop, rather than trying to stop them
// quickly. As will be the case with functions affecting both motors, the
// global stop just calls the individual stop functions for each wheel.
void RedBotMotors::stop()
{
leftStop();
rightStop();
}
// coast() is the same as stop() -- but more descriptive of what this method does.
// it allows the motors to coast to a stop, rather than trying to stop them
// quickly. As will be the case with functions affecting both motors, the
// global stop just calls the individual stop functions for each wheel. This is
// exactly the same as the stop() method. stop() is retained for backwards compatibilty
void RedBotMotors::coast()
{
leftStop();
rightStop();
}
// brake() effectively shorts the two leads of the motor together, which causes
// the motor to resist being turned. It stops quite quickly.
void RedBotMotors::brake()
{
leftBrake();
rightBrake();
}
// drive() starts both motors. It figures out whether the motors should go
// forward or reverse, then calls the appropriate individual functions. Note
// the use of a 16-bit integer for the speed input; an 8-bit integer doesn't
// have the range to reach full speed. The calls to the actual drive functions
// are only 8-bit, since we only have 8-bit PWM.
void RedBotMotors::drive(int speed)
{
if (speed > 0)
{
leftFwd((byte)(abs(speed)));
rightFwd((byte)(abs(speed)));
}
else
{
leftRev((byte)(abs(speed)));
rightRev((byte)(abs(speed)));
}
}
void RedBotMotors::drive(int speed, int duration)
{ // this variant of drive() integrates a delay duration to allow for single commmand instruction.
if (speed > 0)
{
leftFwd((byte)(abs(speed)));
rightFwd((byte)(abs(speed)));
}
else
{
leftRev((byte)(abs(speed)));
rightRev((byte)(abs(speed)));
}
delay(duration);
leftStop();
rightStop();
}
// pivot() is very much like drive(), except the motors are driven in opposite
// directions, so as to pivot the motor on it's central axis. Positive numbers
// turn / rotate the robot clockwise (to the right) -- assuming the motors are hooked up properly.
void RedBotMotors::pivot(int speed)
{
if (speed > 0)
{
leftFwd((byte)(abs(speed)));
rightRev((byte)(abs(speed)));
}
else
{
leftRev((byte)(abs(speed)));
rightFwd((byte)(abs(speed)));
}
}
void RedBotMotors::pivot(int speed, int duration)
{
if (speed > 0)
{
leftRev((byte)(abs(speed)));
rightFwd((byte)(abs(speed)));
}
else
{
leftFwd((byte)(abs(speed)));
rightRev((byte)(abs(speed)));
}
delay(duration);
leftStop();
rightStop();
}
// Basically the same as drive, but omitting the left motor.
void RedBotMotors::rightMotor(int speed)
{
if (speed > 0)
{
rightFwd((byte)(abs(speed)));
}
else
{
rightRev((byte)(abs(speed)));
}
}
void RedBotMotors::rightMotor(int speed, int duration)
{
if (speed > 0)
{
rightFwd((byte)(abs(speed)));
}
else
{
rightRev((byte)(abs(speed)));
}
delay(duration);
rightStop();
}
// Basically the same as drive(), but omitting the right motor.
void RedBotMotors::leftMotor(int speed)
{
if (speed > 0)
{
leftRev((byte)(abs(speed)));
}
else
{
leftFwd((byte)(abs(speed)));
}
}
void RedBotMotors::leftMotor(int speed, int duration)
{
if (speed > 0)
{
leftRev((byte)(abs(speed)));
}
else
{
leftFwd((byte)(abs(speed)));
}
delay(duration);
leftStop();
}
void RedBotMotors::rightDrive(int speed)
{
if (speed > 0)
{
rightFwd((byte)(abs(speed)));
}
else
{
rightRev((byte)(abs(speed)));
}
}void RedBotMotors::leftDrive(int speed)
{
if (speed > 0)
{
leftFwd((byte)(abs(speed)));
}
else
{
leftRev((byte)(abs(speed)));
}
}
// From here out, we deal with the nitty gritty details of telling the motor
// driver what to do. For more information about this, refer to the TB6612FNG
// datasheet.
void RedBotMotors::leftBrake()
{
digitalWrite(L_CTRL_1, HIGH);
digitalWrite(L_CTRL_2, HIGH);
analogWrite(PWM_L, 0);
}
void RedBotMotors::rightBrake()
{
digitalWrite(R_CTRL_1, HIGH);
digitalWrite(R_CTRL_2, HIGH);
analogWrite(PWM_R, 0);
}
void RedBotMotors::leftStop() // allows left motor to coast to a stop
{
digitalWrite(L_CTRL_1, LOW);
digitalWrite(L_CTRL_2, LOW);
analogWrite(PWM_L, 0);
}
void RedBotMotors::rightStop() // allows right motor to coast to a stop
{
digitalWrite(R_CTRL_1, LOW);
digitalWrite(R_CTRL_2, LOW);
analogWrite(PWM_R, 0);
}
void RedBotMotors::leftCoast() // same as rightStop()
{
digitalWrite(L_CTRL_1, LOW);
digitalWrite(L_CTRL_2, LOW);
analogWrite(PWM_L, 0);
}
void RedBotMotors::rightCoast() // same as leftStop()
{
digitalWrite(R_CTRL_1, LOW);
digitalWrite(R_CTRL_2, LOW);
analogWrite(PWM_R, 0);
}
/******************************************************************************
Private functions for RedBotMotor
******************************************************************************/
// These are the motor-driver level abstractions for turning a given motor the
// right direction. Users never see them, and *should* never see them, so we
// make them private.
void RedBotMotors::leftFwd(byte spd)
{
digitalWrite(L_CTRL_1, HIGH);
digitalWrite(L_CTRL_2, LOW);
analogWrite(PWM_L, spd);
// If we have an encoder in the system, we want to make sure that it counts
// in the right direction when ticks occur.
if (encoderObject != 0)
{
encoderObject->lDir = 1;
}
}
void RedBotMotors::leftRev(byte spd)
{
digitalWrite(L_CTRL_1, LOW);
digitalWrite(L_CTRL_2, HIGH);
analogWrite(PWM_L, spd);
// If we have an encoder in the system, we want to make sure that it counts
// in the right direction when ticks occur.
if (encoderObject != 0)
{
encoderObject->lDir = -1;
}
}
void RedBotMotors::rightFwd(byte spd)
{
digitalWrite(R_CTRL_1, HIGH);
digitalWrite(R_CTRL_2, LOW);
analogWrite(PWM_R, spd);
// If we have an encoder in the system, we want to make sure that it counts
// in the right direction when ticks occur.
if (encoderObject != 0)
{
encoderObject->rDir = 1;
}
}
void RedBotMotors::rightRev(byte spd)
{
digitalWrite(R_CTRL_1, LOW);
digitalWrite(R_CTRL_2, HIGH);
analogWrite(PWM_R, spd);
// If we have an encoder in the system, we want to make sure that it counts
// in the right direction when ticks occur.
if (encoderObject != 0)
{
encoderObject->rDir = -1;
}
}