-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMoveRobot.ino
More file actions
354 lines (290 loc) · 9.31 KB
/
MoveRobot.ino
File metadata and controls
354 lines (290 loc) · 9.31 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//Motor1 Bank A
#include <Wire.h>
#include <NXShield.h>
#include <NXTUS.h>
#include <Servo.h>
#include <SoftwareSerial.h>
//Don't make # symbols
//Bank_A: front-right:1, back-right:2
//Bank_B: front-left: 1, back-left: 2
NXShield nxshield;
NXShield sensorShield;
// 2 line LCD on arduino digital pin #9
SoftwareSerial lcd(2, 9);
//Servo elevatorServo;
//
// declare the i2c devices used on NXShield(s).
//
NXTUS sonarB; // left
NXTUS sonarA; // right
NXTUS sonarBack;
void setup(){
Serial.begin(115200);
lcd.begin(9600);
lcd.print("Setup Starting...");
//elevatorServo.attach(3);
//elevatorStop();
delay(500);
//
// some constants?
//
int updateDelay = 100; // 100ms sensor / screen update time
nxshield.init(SH_HardwareI2C);
Serial.println("Press GO!");
clearDisplay();
lcd.print("Press GO!");
nxshield.waitForButtonPress(BTN_GO);
nxshield.bank_a.motorReset();
nxshield.bank_b.motorReset();
//
// Initialize the i2c sensors.
//
sonarB.init( &nxshield, SH_BBS1 );
sonarA.init( &nxshield, SH_BAS2 );
sonarBack.init( &nxshield, SH_BBS2 );
//approachHoop();
//approachBackWall(20, 5);
scan(25, 2, 10, 10); // backWall distance, backThresh, ballThresh, sideThresh
}
void
loop(){
}
void
coloryLights(){
nxshield.ledSetRGB(8,0,0);
delay(1000);
nxshield.ledSetRGB(0,8,0);
delay(1000);
nxshield.ledSetRGB(0,0,8);
delay(1000);
nxshield.ledSetRGB(0,0,0);
delay(1000);
}
// dist - desired distance (~cm) from the back wall of the field.
// backThresh - the threshold (~cm) before the robot will attempt to correct for drift.
// ballThresh - the threshold (~cm) of sudden distance change that will trigger a ball found state.
// sideDist - the distance (~cm) from the sides of the field that the robot will maintain while scanning.
void scan(int dist, int backThresh, int ballThresh, int sideDist) {
clearDisplay();
lcd.print("BEGIN SCANNING!");
findCenter(5); // moves the bot to the center
delay(500);
//alignLeft(dist, sideDist, backThresh); // move bot to the left of the field
delay(500);
approachBackWall(dist, backThresh);
delay(500);
int curDist = abs(sonarBack.getDist() - dist);
int rightDist = sonarA.getDist() - sideDist;
while(curDist < (ballThresh)) {
curDist = abs(sonarBack.getDist() - dist);
rightDist = sonarA.getDist() - sideThresh;
delay(updateDelay); // sensor / lcd update delay
if(rightDist > 0) {
lcd.print("PANNING RIGHT!!");
strafeRight(50);
} else {
clearDisplay();
lcd.print("Right wall reached?");
delay(200 * sideThresh); // delay allowing robot to run up against wall to square itself...
approachBackWall(dist, backThresh);
alignLeft(dist, sideDist, backThresh); // move all the way back to the left when the right wall is reached.
clearDisplay();
}
if(curDist > backThresh) {
clearDisplay();
lcd.print("correcting dist. !");
delay(100);
approachBackWall(dist, backThresh);
}
}
clearDisplay();
lcd.print("found ball!?");
setLCDCursor(16);
lcd.print("curDist: ");
lcd.print(curDist);
stopMoving();
}
// broken right now...
boolean pan(int sideThresh, boolean right) {
lcd.print("panning! ");
boolean toReturn = right;
int dist;
if(toReturn) {
strafeRight(50);
dist = abs(sonarA.getDist() - sideThresh);
} else {
strafeLeft(50);
dist = abs(sonarB.getDist() - sideThresh);
}
if(dist < 0) {
toReturn = !toReturn;
stopMoving();
delay(100);
}
return toReturn;
}
// moves the bot to within dist cm, give of take thresh cm, of the back wall
void approachBackWall(int dist, int thresh) {
clearDisplay();
lcd.print("approching back wall!");
int curDist = sonarBack.getDist() - dist;
while(abs(curDist) > thresh) {
delay(updateDelay);
curDist = sonarBack.getDist() - dist;
clearDisplay();
lcd.print("approachBackWall()");
setLCDCursor(16);
lcd.print("curDist: ");
lcd.print(curDist);
if (curDist < 0) {
moveForward(50);
} else {
moveBackward(50);
}
}
stopMoving();
clearDisplay();
}
// centers the robot, and then moves forward till the bot is ~53cm from the hoop wall.
void approachHoop() {
findCenter(5);
clearDisplay();
lcd.write("approachHoop()");
delay(500);
moveForward(50);
while(sonarBack.getDist() < 53) {
}
clearDisplay();
stopMoving();
}
// a really silly method that just moves the thing left. hopefully.
void alignLeft(int backDist, int sideDist, int backThresh){
clearDisplay();
lcd.write("aligning left...");
int distB = sonarB.getDist() - sideDist;
int curDist = sonarBack.getDist() - backDist;
strafeLeft(50);
while(distB > 0) {
delay(updateDelay);
distB = sonarB.getDist() - sideDist;
curDist = abs(sonarBack.getDist() - backDist) ;
clearDisplay();
lcd.print("Aligning left...");
setLCDCursor(16);
lcd.print("left dist: ");
lcd.print(distB);
if(curDist > backThresh) {
//stopMoving();
clearDisplay();
lcd.write("correcting dist. !");
delay(100);
approachBackWall(backDist, 2);
}
}
stopMoving();
clearDisplay();
}
// centers the robot left / right on the field
void findCenter(int threshold) {
int distA = sonarA.getDist();
int distB = sonarB.getDist();
clearDisplay();
lcd.write("findCenter()..");
while(abs(distA - distB) > threshold) {
delay(updateDelay);
distA = sonarA.getDist();
distB = sonarB.getDist();
clearDisplay();
lcd.print("Finding Center...");
setLCDCursor(16);
lcd.print("distA: ");
lcd.print(distA);
lcd.print(" distB: ");
lcd.print(distB);
if(distA > distB) {
strafeRight(50);
} else {
strafeLeft(50);
}
}
clearDisplay();
stopMoving();
}
/////////////////////////// MOVEMENT COMMANDS //////////////////////////
void
moveForward(int speed1){
nxshield.bank_b.motorRunUnlimited(SH_Motor_1, SH_Direction_Forward, speed1);
nxshield.bank_b.motorRunUnlimited(SH_Motor_2, SH_Direction_Reverse, speed1);
nxshield.bank_a.motorRunUnlimited(SH_Motor_1, SH_Direction_Forward, speed1);
nxshield.bank_a.motorRunUnlimited(SH_Motor_2, SH_Direction_Reverse, speed1);
}
void
moveBackward(int speed1){
nxshield.bank_b.motorRunUnlimited(SH_Motor_1, SH_Direction_Reverse, speed1);
nxshield.bank_b.motorRunUnlimited(SH_Motor_2, SH_Direction_Forward, speed1);
nxshield.bank_a.motorRunUnlimited(SH_Motor_1, SH_Direction_Reverse, speed1);
nxshield.bank_a.motorRunUnlimited(SH_Motor_2, SH_Direction_Forward, speed1);
}
void
stopMoving(){
nxshield.bank_a.motorStop(SH_Motor_Both, SH_Next_Action_Float);
nxshield.bank_b.motorStop(SH_Motor_Both, SH_Next_Action_Float);
}
void
strafeRight(int speed1){
nxshield.bank_b.motorRunUnlimited(SH_Motor_1, SH_Direction_Forward, speed1);
nxshield.bank_b.motorRunUnlimited(SH_Motor_2, SH_Direction_Forward, speed1);
nxshield.bank_a.motorRunUnlimited(SH_Motor_1, SH_Direction_Reverse, speed1);
nxshield.bank_a.motorRunUnlimited(SH_Motor_2, SH_Direction_Reverse, speed1);
}
void
strafeLeft(int speed1){
nxshield.bank_b.motorRunUnlimited(SH_Motor_1, SH_Direction_Reverse, speed1);
nxshield.bank_b.motorRunUnlimited(SH_Motor_2, SH_Direction_Reverse, speed1);
nxshield.bank_a.motorRunUnlimited(SH_Motor_1, SH_Direction_Forward, speed1);
nxshield.bank_a.motorRunUnlimited(SH_Motor_2, SH_Direction_Forward, speed1);
}
void
turnClockwise(int speed1){
nxshield.bank_b.motorRunUnlimited(SH_Motor_1, SH_Direction_Forward, speed1);
nxshield.bank_b.motorRunUnlimited(SH_Motor_2, SH_Direction_Reverse, speed1);
nxshield.bank_a.motorRunUnlimited(SH_Motor_1, SH_Direction_Reverse, speed1);
nxshield.bank_a.motorRunUnlimited(SH_Motor_2, SH_Direction_Forward, speed1);
}
void
turnCounterClockwise(int speed1){
nxshield.bank_b.motorRunUnlimited(SH_Motor_1, SH_Direction_Reverse, speed1);
nxshield.bank_b.motorRunUnlimited(SH_Motor_2, SH_Direction_Forward, speed1);
nxshield.bank_a.motorRunUnlimited(SH_Motor_1, SH_Direction_Forward, speed1);
nxshield.bank_a.motorRunUnlimited(SH_Motor_2, SH_Direction_Reverse, speed1);
}
void
diagonal(){
nxshield.bank_b.motorRunUnlimited(SH_Motor_1, SH_Direction_Reverse, 40);
nxshield.bank_b.motorRunUnlimited(SH_Motor_2, SH_Direction_Reverse, 90);
nxshield.bank_a.motorRunUnlimited(SH_Motor_1, SH_Direction_Forward, 90);
nxshield.bank_a.motorRunUnlimited(SH_Motor_2, SH_Direction_Forward, 40);
}
///////////////////////////// SERVO COMMANDS ////////////////////////////
//void elevatorRaise() {
// elevatorServo.write(0);
//}
//
//void elevatorLower() {
// elevatorServo.write(180);
//}
//
//void elevatorStop() {
// elevatorServo.write(90);
//}
///////////////////////////// LCD COMMANDS //////////////////////////////
void clearDisplay() {
lcd.write(0xFE);
lcd.write(0x01);
}
void setLCDCursor(byte cursor_position){
lcd.write(0xFE); // ready LCD for special command
lcd.write(0x80); // ready LCD to recieve cursor potition
lcd.write(cursor_position); // send cursor position
}
#