21 #include "path_planning.h"
35 #define BEAM_IGNORE_COUNT 6
36 #define CORRAL_COUNT 3
37 #define LIFT_DONE_COUNT 8
51 static bool liftDown =
false;
52 static uint8_t nextBallNodeNum = 0;
57 static void moveServosToStart(
void);
70 static inline bool positionBot(
void);
72 static bool standardBallSearch(
void );
73 static inline bool nodeCode0(
void );
74 static inline bool nodeCode22(
void );
75 static inline bool diagNodeCode(
void);
76 static inline bool nodeCode37(
void );
77 static inline void initBallSeeking(
void);
81 bool justTurned =
false;
101 lcdWriteStr(
" ", 0, 0);
102 lcdWriteStr(
" ", 1, 0);
103 lcdPrintDecU08(botNode, 1, 0);
104 lcdPrintDecS08(botHeading, 1, 3);
109 while (pathList[pathListIndex + 1] !=
STOP_NODE)
113 justTurned = positionBot();
118 setServo(
LIFT, LIFT_OPEN);
127 lcdPrintDecS08(botHeading, 1, 3);
136 lcdPrintDecU08(botNode, 1, 0);
141 if (pathList[pathListIndex + 1] ==
STOP_NODE)
148 inline void initBallSeeking(
void)
151 for (i = 0; i < ELEMENTSOF(checkedList); ++i)
153 checkedList[i] =
false;
157 void moveServosToStart(
void)
160 lcdWriteStr(
"Servos ", 0, 6);
162 setServo(
PAN, PAN_CENTER + panOffset);
163 setServo(
TILT, TILT_FORWARD);
164 setServo(
BOOM, BOOM_UP);
165 setServo(
DOOR, DOOR_CLOSED);
167 setServo(
LIFT, LIFT_UP);
174 inline bool positionBot(
void)
176 bool justTurned =
true;
178 int8_t nextHeading = getNextHeading();
179 int8_t bradsToTurn = nextHeading - botHeading;
194 else if (bradsToTurn != 0)
197 switch ((int8_t) bradsToTurn)
203 tickWheels(20, 20, 255);
205 moveStraight(-20, 255);
208 tickWheels(-29, 29, 250);
212 tickWheels(28, 28, 250);
213 tractorTurn(255, -tempTweak4);
217 tractorTurn(255, 23);
220 tractorTurn(255, -28);
223 tickWheels(17, 17, 250);
232 ticksToTurn = bradsToTurn + turnSubtract;
235 ticksToTurn = bradsToTurn - turnSubtract;
237 tractorTurn(255, ticksToTurn);
252 botHeading = nextHeading;
255 nextBallNodeNum = getNextBallNodeNum();
256 if (nextBallNodeNum != 0)
258 setServo(
LIFT, LIFT_OPEN);
274 switch ((int8_t) (bbHeading - botHeading))
278 tickWheels(28, 0, 255);
282 tickWheels(0, 28, 255);
286 tickWheels(10, 10, 255);
287 tickWheels(0, 32, 255);
290 fatalError(
"ERR: bonusBallPi",
"ckUpManeuver: 1");
296 setServo(
TILT, TILT_BACK);
301 forward(BOTH_MOTORS, 255);
306 moveStraight(-10, 255);
307 forward(BOTH_MOTORS, 255);
314 setServo(
TILT, TILT_BB_GRAB);
316 setServo(
BOOM, BOOM_UP);
320 setServo(
LIFT, LIFT_UP);
323 moveStraight(-18, 255);
324 setServo(
TILT, TILT_FORWARD);
331 switch ((int8_t) (nextHeading - bbHeading))
344 fatalError(
"ERR: bonusBallPi",
"ckUpManeuver: 2");
355 bool juncApproaching =
false;
356 uint8_t juncCount = 0;
358 uint8_t ignoreJuncCount;
367 uint8_t pickingUp =
false;
368 uint8_t pickingUpCount = 0;
370 uint8_t ignoreBreakBeamCount = BEAM_IGNORE_COUNT;
378 while (lineStatsProcessed) ;
384 if (ignoreJuncCount > 0 && junctionY == 0)
390 if (ignoreJuncCount == 0 && junctionY != 0)
392 if (junctionY < turnPoint)
394 juncApproaching =
true;
395 }
else if (juncApproaching)
397 juncApproaching =
false;
404 botNode = pathList[pathListIndex];
405 }
while (!isJunction(botNode));
408 if (juncCount >= numJunctions)
416 if (liftDown && ignoreBreakBeamCount != 0)
418 ignoreBreakBeamCount--;
422 if (liftDown && ignoreBreakBeamCount == 0 && BREAK_BEAM_TRIGGERED)
439 if (pickingUpCount == CORRAL_COUNT)
442 setServo(
LIFT, LIFT_UP);
446 if (pickingUpCount == LIFT_DONE_COUNT)
451 botNode = nextBallNodeNum;
452 removeFromGoalList(nextBallNodeNum);
454 if (nextBallNodeNum == 1)
456 adjustNumKnownGoals(1);
460 while (botNode != pathList[pathListIndex])
468 ignoreBreakBeamCount = BEAM_IGNORE_COUNT;
482 lcdWriteStr(
"No ball ", 0, 0);
484 setServo(
LIFT, LIFT_UP);
490 removeFromGoalList(nextBallNodeNum);
492 adjustNumKnownGoals(-1);
503 if (!lineStatsProcessed)
514 setServo(
LIFT, LIFT_UP);
518 setServo(
DOOR, DOOR_OPEN);
519 moveStraight(-1, 255);
523 setServo(
DOOR, DOOR_CLOSED);
538 void junctionCode(
void)
540 bool foundBall =
false;
545 foundBall = nodeCode0();
550 foundBall = standardBallSearch();
551 foundBall |= nodeCode22();
555 foundBall = diagNodeCode();
558 if (!allGoalsFound() && (!checkedList[13] || !checkedList[17]))
561 moveStraight(10, 255);
562 foundBall = diagNodeCode();
563 moveStraight(-10, 255);
571 foundBall = standardBallSearch();
581 for (i = 0; i < NUM_BALL_NODES + 1; i++)
583 checkedList[i] =
true;
595 bool standardBallSearch(
void )
601 int8_t hallHeading = 0;
602 uint8_t ballDist = 0;
603 uint8_t uncheckedBalls[3][2];
604 uint8_t numUncheckedBalls = 0;
605 bool foundBall =
false;
608 bool stopped =
false;
609 inSeekPosition =
false;
612 for (i = 0; i < 2; i++)
615 hallHeading = botHeading + lookDir * 64;
616 nextNodeNum = botNode;
617 numUncheckedBalls = 0;
619 while (nextNodeNum > 0)
621 getNode(nextNodeNum, &curNode);
622 nextNodeNum = getNodeAtHeading(&curNode, hallHeading);
625 getNode(nextNodeNum, &nextNode);
627 ballDist += getCostToNode(&curNode, nextNodeNum);
628 if (isBallNode(nextNodeNum) && !checkedList[nextNodeNum])
630 uncheckedBalls[numUncheckedBalls][BALL_DIST] = ballDist;
631 uncheckedBalls[numUncheckedBalls][BALL_NODE_NUM] = nextNodeNum;
632 checkedList[nextNodeNum] =
true;
639 if (numUncheckedBalls > 0)
642 trackColorInit(lookDir);
646 foundBall |= cameraSeekLeft(uncheckedBalls, numUncheckedBalls);
647 }
else if (lookDir == 1)
649 foundBall |= cameraSeekRight(uncheckedBalls, numUncheckedBalls);
658 moveStraight(0xb, 255);
659 setServo(
PAN, PAN_CENTER + panOffset);
660 setServo(
TILT, TILT_FORWARD);
669 inline bool nodeCode0(
void)
671 bool foundBall =
false;
679 inline bool nodeCode22()
681 bool foundBall =
false;
682 uint8_t scanHeight = 4;
684 uint8_t scanLimit = 1;
685 uint8_t foundBallNum = 0;
690 trackColorInit(LOOK_UP);
693 while ( y - scanHeight > scanLimit )
709 addToGoalList( foundBallNum );
719 setServo(
PAN, PAN_CENTER+panOffset);
720 setServo(
TILT, TILT_FORWARD);
727 inline bool diagNodeCode(
void)
729 bool foundBall =
false;
731 if( botHeading ==
N_WEST && (!checkedList[13] || !checkedList[17]) )
733 tankTurn(255,tempTweak3);
735 foundBall = standardBallSearch();
737 tankTurn(255, -1*tempTweak3);
739 else if( botHeading !=
S_EAST && (!checkedList[14] || !checkedList[15]) )
741 tankTurn(255, -1*tempTweak3);
743 foundBall = standardBallSearch();
745 tankTurn(255,tempTweak3);
751 inline bool nodeCode37(
void )
753 bool foundBall =
false;