-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutonomous.h
executable file
·562 lines (529 loc) · 13 KB
/
Autonomous.h
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/* $Id$ */
#include "JoystickDriver.c" //Include file to "handle" the Bluetooth messages.
#ifdef GYRO
#include "HTGYRO-driver.h"
task getHeading();
#endif /* GYRO */
/* Common defines */
#define LEFT_TURN_ENCODER 205
#define RIGHT_TURN_ENCODER 217
#define TURN_SPEED 25
#define MOTOR_OFF 0
#define MOTOR_HALF 50
#define MOTOR_FULL 100
#define MOTOR_RAMP 80
#define LED_ON 100
#define LED_OFF 0
#define ARM_RELEASE_OPEN 120
#define ARM_RELEASE_CLOSED 239
#define CLAW_RELEASE_OPEN 186
#define CLAW_RELEASE_CLOSED 131
/* Movement defines */
#define MOVE_OFF_RAMP_TIME 1700
#define RIGHT_TURN_TIME 1500
#define LEFT_TURN_TIME 1800
#define MOVE_TO_BBALL_TIME 1200
#define SLIGHT_RIGHT_TIME 350
#define SLIGHT_LEFT_TIME 500
#define MOVE_TO_CORNER_TIME 5000
#define MOVE_TO_WALL_TIME 1000
/* Back parking defines */
#define MOVE_TO_BACK_TIME 7000
#define MOVE_TO_BACK_JUKE_TIME 1500
#define MOVE_FROM_BACK_TIME 7000
#define MOVE_TO_BEACON_TIME 1000
/* Outside modes */
#define LEFT_OUTSIDE_TURN_TIME 1600
#define RIGHT_OUTSIDE_TURN_TIME 1700
#define MOVE_OUTSIDE_TO_BBALL_TIME 2800
#define SLIGHT_OUTSIDE_RIGHT_TIME 500
#define SLIGHT_OUTSIDE_LEFT_TIME 750
#define TURN_WAIT_MS 200
/* user input globals */
bool useGyro = false;
#ifdef ALL_USER_INPUT
#define START_RED "Red"
#define START_BLUE "Blue"
#define START_INSIDE "Inside"
#define START_OUTSIDE "Outside"
#define PARK_BACK "Back"
#define PARK_FRONT "Front"
string startColor;
string startPosition;
bool startPause = false;
string robotPark;
string bBallPark;
#endif /* ALL_USER_INPUT */
//bool useIRSeeker = false;
void getUserInput()
{
disableDiagnosticsDisplay();
#ifdef ALL_USER_INPUT
nxtDisplayCenteredTextLine(0, "Red or Blue?");
nxtDisplayCenteredTextLine(7, "Red Blue");
while(true)
{
if(nNxtButtonPressed == 2)
{
startColor = START_RED;
nxtDisplayCenteredTextLine(0, "Red");
break;
}
else if(nNxtButtonPressed == 1)
{
startColor = START_BLUE;
nxtDisplayCenteredTextLine(0, "Blue");
break;
}
}
PlaySound(soundBlip);
wait1Msec(1000);
nxtDisplayCenteredTextLine(1, "Inside or Outside?");
nxtDisplayCenteredTextLine(7, "Inside Outside");
while(true)
{
if(nNxtButtonPressed == 2)
{
startPosition = START_INSIDE;
nxtDisplayCenteredTextLine(1, "Inside");
break;
}
else if(nNxtButtonPressed == 1)
{
startPosition = START_OUTSIDE;
nxtDisplayCenteredTextLine(1, "Outside");
break;
}
}
PlaySound(soundBlip);
wait1Msec(1000);
nxtDisplayCenteredTextLine(2, "Wait 5 Seconds?");
nxtDisplayCenteredTextLine(7, "Yes No");
while(true)
{
if(nNxtButtonPressed == 2)
{
startPause = true;
nxtDisplayCenteredTextLine(2, "5s Wait: Yes");
break;
}
else if(nNxtButtonPressed == 1)
{
startPause = false;
nxtDisplayCenteredTextLine(2, "5s Wait: No");
break;
}
}
nxtDisplayTextLine(7, "");
PlaySound(soundBlip);
#endif /* ALL_USER_INPUT */
#ifdef GYRO
/*
wait1Msec(1000);
nxtDisplayCenteredTextLine(4, "Gyro?");
nxtDisplayCenteredTextLine(7, "Yes No");
while(true)
{
if(nNxtButtonPressed == 2)
{
useGyro = true;
nxtDisplayCenteredTextLine(4, "Gyro: Yes");
break;
}
else if(nNxtButtonPressed == 1)
{
useGyro = false;
nxtDisplayCenteredTextLine(4, "Gyro: No");
break;
}
}
nxtDisplayTextLine(7, "");
PlaySound(soundBlip);
*/
useGyro = true;
#endif /* GYRO */
#ifdef PARK_MODES
wait1Msec(1000);
nxtDisplayCenteredTextLine(5, "Ball Park?");
nxtDisplayCenteredTextLine(7, "Front Back");
while(true)
{
if(nNxtButtonPressed == 2)
{
bBallPark = PARK_FRONT;
nxtDisplayCenteredTextLine(5, "Ball Park: Front");
break;
}
else if(nNxtButtonPressed == 1)
{
bBallPark = PARK_BACK;
nxtDisplayCenteredTextLine(5, "Ball Park: Back");
break;
}
}
nxtDisplayTextLine(7, "");
PlaySound(soundBlip);
wait1Msec(1000);
if(bBallPark == PARK_BACK)
{
nxtDisplayCenteredTextLine(6, "Robot Park?");
nxtDisplayCenteredTextLine(7, "Front Back");
while(true)
{
if(nNxtButtonPressed == 2)
{
robotPark = PARK_FRONT;
nxtDisplayCenteredTextLine(6, "Bot Park: Front");
break;
}
else if(nNxtButtonPressed == 1)
{
robotPark = PARK_BACK;
nxtDisplayCenteredTextLine(6, "Bot Park: Back");
break;
}
}
nxtDisplayTextLine(7, "");
PlaySound(soundBlip);
}
#endif /* PARK_MODES */
string sayings[] = {"You Look Ravishing",
"* Geronimo! *",
"* Bansai! *",
"* Kamikazee! *",
"* Charge! *",
"* Ballin! *",
"* For Demacia! *",
"* Banana Pancakes *",
"* You Broke Me :(",
"You Look Ravishing",
"Radishes & Shining",
"Dont cross streams!",
"Break them",
"Well Then",
"Kamehameha!",
"Allurbaserblong2us!"};
nxtDisplayCenteredTextLine(7, "* Tally ho! *");
//nxtDisplayCenteredTextLine(7, sayings[rand() % 14]);
}
/**
* Initialize robot.
*/
void initializeRobot()
{
getUserInput();
#ifdef GYRO
if(useGyro)
{
wait1Msec(2000);
StartTask(getHeading);
}
#endif /* GYRO */
motor[green] = LED_ON;
motor[yellow] = LED_ON;
motor[red] = LED_ON;
motor[right] = MOTOR_OFF;
motor[clawArm] = MOTOR_OFF;
motor[left] = MOTOR_OFF;
motor[ballArm] = MOTOR_OFF;
motor[spool] = MOTOR_OFF;
motor[intake] = MOTOR_OFF;
servoChangeRate[armRelease] = 50;
servoTarget[armRelease] = ARM_RELEASE_CLOSED;
servoChangeRate[armRelease] = 10;
servoTarget[clawRelease] = CLAW_RELEASE_CLOSED;
}
#ifdef RIGHT_TURN
/**
* Make a right turn.
*/
void rightTurn()
{
nMotorEncoder[right] = 0;
nMotorEncoder[left] = 0;
while (abs(nMotorEncoder[right]) < RIGHT_TURN_ENCODER ||
abs(nMotorEncoder[left]) < RIGHT_TURN_ENCODER)
{
motor[right] = TURN_SPEED;
motor[left] = TURN_SPEED * -1;
}
motor[right] = 0;
motor[left] = 0;
nMotorEncoder[right] = 0;
nMotorEncoder[left] = 0;
}
#endif /* RIGHT_TURN */
#ifdef LEFT_TURN
/**
* Make a left turn.
*/
void leftTurn()
{
nMotorEncoder[right] = 0;
nMotorEncoder[left] = 0;
while (abs(nMotorEncoder[left]) < LEFT_TURN_ENCODER ||
abs(nMotorEncoder[right]) < LEFT_TURN_ENCODER)
{
motor[right] = TURN_SPEED * -1;
motor[left] = TURN_SPEED;
}
motor[right] = 0;
motor[left] = 0;
nMotorEncoder[right] = 0;
nMotorEncoder[left] = 0;
}
#endif /* LEFT_TURN */
#ifdef MOVE
/**
* Move the provided distance at the provided motor speed.
*
* distance -- the distance in encoder counts
* speed -- the motor speed (positive == forwards, negative == reverse
*/
void move(int distance, int speed)
{
nMotorEncoder[right] = 0;
nMotorEncoder[left] = 0;
while (abs(nMotorEncoder[left]) < distance ||
abs(nMotorEncoder[right]) < distance)
{
motor[right] = speed;
motor[left] = speed;
}
motor[right] = MOTOR_OFF;
motor[left] = MOTOR_OFF;
nMotorEncoder[right] = 0;
nMotorEncoder[left] = 0;
}
#endif /* MOVE */
#ifdef MOVE_TIMED
/**
* Move at the provided motor speed for the provided time.
*
* speed -- the motor speed (positive == forwards, negative == reverse
* time -- the time in ms
*/
void moveTimed(int speed, int time)
{
motor[right] = speed;
motor[left] = speed;
wait1Msec(time);
motor[right] = MOTOR_OFF;
motor[left] = MOTOR_OFF;
}
#endif /* MOVE_TIMED */
#ifdef TURN_RIGHT_TIMED
/**
* Turn right at the provided motor speed for the provided time.
*
* speed -- the motor speed (positive == forwards, negative == reverse
* time -- the time in ms
*/
void turnRightTimed(int speed, int time)
{
motor[right] = -speed;
motor[left] = speed;
wait1Msec(time);
motor[right] = MOTOR_OFF;
motor[left] = MOTOR_OFF;
}
#endif /* TURN_RIGHT_TIMED */
#ifdef TURN_LEFT_TIMED
/**
* Turn left at the provided motor speed for the provided time.
*
* speed -- the motor speed (positive == forwards, negative == reverse
* time -- the time in ms
*/
void turnLeftTimed(int speed, int time)
{
motor[right] = speed;
motor[left] = -speed;
wait1Msec(time);
motor[right] = MOTOR_OFF;
motor[left] = MOTOR_OFF;
}
#endif /* TURN_LEFT_TIMED */
task prettyLights()
{
while(true)
{
motor[green] = LED_ON;
wait1Msec(200);
motor[green] = LED_OFF;
motor[yellow] = LED_ON;
wait1Msec(200);
motor[yellow] = LED_OFF;
motor[red] = LED_ON;
wait1Msec(200);
motor[red] = LED_OFF;
motor[yellow] = LED_ON;
wait1Msec(200);
motor[yellow] = LED_OFF;
}
}
#ifdef GYRO
#define RATE_THRESH 3
#define MOVE_THRESH 10
#define RIGHT_TURN_THRESH 0
#define LEFT_TURN_THRESH 0
#define GYRO_TURN_SPEED 80
#define GYRO_LIMITER_ANGLE 35
float currHeading = 0;
// Task to keep track of the current heading using the HT Gyro
task getHeading ()
{
float delTime = 0;
float prevHeading = 0;
float curRate = 0;
HTGYROstartCal(HTGYRO);
PlaySound(soundBeepBeep);
while(true)
{
time1[T1] = 0;
curRate = HTGYROreadRot(HTGYRO);
if (abs(curRate) > RATE_THRESH)
{
//hogCPU();
prevHeading = currHeading;
currHeading = prevHeading + curRate * delTime;
//if (currHeading >= 360) currHeading -= 360;
//else if (currHeading < 0) currHeading += 360;
//releaseCPU();
}
/*
nxtDisplayTextLine(1, "curr: %f", currHeading);
nxtDisplayTextLine(2, "prev: %f", prevHeading);
nxtDisplayTextLine(3, "delTime: %f", delTime);
nxtDisplayTextLine(4, "curRate: %f", curRate);
*/
wait1Msec(5);
delTime = ((float)time1[T1]) / 1000;
}
}
#ifdef LEFT_GYRO_TURN
/**
* Make a left turn to a heading.
*/
void leftGyroTurn(float heading, int speed)
{
bool speedReduced = false;
while(currHeading > heading + LEFT_TURN_THRESH)
{
if(!speedReduced &&
currHeading - (heading + LEFT_TURN_THRESH) < GYRO_LIMITER_ANGLE)
{
speed = GYRO_TURN_SPEED;
speedReduced = true;
}
motor[right] = speed;
motor[left] = -speed;
}
motor[right] = MOTOR_OFF;
motor[left] = MOTOR_OFF;
}
#endif /* LEFT_GYRO_TURN */
#ifdef RIGHT_GYRO_TURN
/**
* Make a right turn to a heading.
*/
void rightGyroTurn(float heading, int speed)
{
bool speedReduced = false;
while(currHeading < heading - RIGHT_TURN_THRESH)
{
if(!speedReduced &&
(heading - RIGHT_TURN_THRESH) - currHeading < GYRO_LIMITER_ANGLE)
{
speed = GYRO_TURN_SPEED;
speedReduced = true;
}
motor[right] = -speed;
motor[left] = speed;
}
motor[right] = MOTOR_OFF;
motor[left] = MOTOR_OFF;
}
#endif /* RIGHT_GYRO_TURN */
#ifdef MOVE_GYRO
void moveGyro(int speed, int time)
{
float oHeading = currHeading;
time1[T2] = 0;
while(time1[T2] < time)
{
// drifting left
if(currHeading < oHeading)
{
motor[right] = speed - MOVE_THRESH;
motor[left] = speed;
}
// drifting right
else if(currHeading > oHeading)
{
motor[right] = speed;
motor[left] = speed - MOVE_THRESH;
}
else
{
motor[right] = speed;
motor[left] = speed;
}
wait1Msec(10);
}
motor[right] = MOTOR_OFF;
motor[left] = MOTOR_OFF;
}
#endif /* MOVE_GYRO */
#endif /* GYRO */
#ifdef IR_SEEKER
#include "HTIRS2-driver.h"
int getIRDirection()
{
int dir = HTIRS2readACDir(HTIRS2);
//nxtDisplayTextLine(1, "direction: %d", dir);
return dir;
}
void turnToIRBeacon(int speed)
{
int dir = getIRDirection();
while(true)
{
if(dir == 0)
{
// no reading
break;
}
else if(dir == 5)
{
break;
}
else if(dir > 5 && dir > 0)
{
// left of beacon
motor[right] = -speed;
motor[left] = speed;
}
else if(dir < 5 && dir > 0)
{
// right of beacon
motor[right] = speed;
motor[left] = -speed;
}
wait1Msec(100);
dir = getIRDirection();
}
motor[left] = MOTOR_OFF;
motor[right] = MOTOR_OFF;
}
#endif /* IR_SEEKER */
#ifdef BALL_GRAB
task BallGrab()
{
wait1Msec(1000 + TURN_WAIT_MS);
motor[ballArm] = -MOTOR_FULL;
wait1Msec(450);
motor[ballArm] = MOTOR_OFF;
return;
}
#endif /* BALL_GRAB */