void main() { int pid; int to; int ball = 0; int count; /*used to keep track of times through while loops*/ enable_encoder(7); enable_encoder(8); enable_servos(); armUp(); rampDown(); /*calibrate turns*/ calibrateRight90(); calibrateLeft90(); calibrateRight180(); calibrateLeft180(); calibrateStraight(); /*calibrate white and black colors*/ printf("\nplace rr on white, rl on black"); while(!(start_button())) { sleep(.1); } getValues(); sleep(1.); while(!(start_button())) { printf("\norient me"); } /*START*/ start_machine2(16, 20); enable_encoder(7); enable_encoder(8); enable_servos(); rampDown(); armUp(); /*Turn and face up*/ orient(); /*go get top ball*/ to = forwardToBallTimeout(75, (long) 5000); count = 0; while (to && count < 2) { /*if times out and tried no more than 3 times*/ printf("\ntimed out!!!"); backwardCountTicksUntilTimeout(50, (long) 2000); if (side == 1) { left90(-30); /*turn away from wall*/ to = forwardToBallTimeout(75, (long) 5000); /*try again*/ } else if (side == 0) { right90(-30); /*turn away from wall*/ to = forwardToBallTimeout(75, (long) 5000); /*try again*/ } count++; } sleep(.05); ball = getBall(); /*backup from wall*/ to = backwardCountTicksUntilTimeout(50, (long) 3000); count = 0; if (to) { count = TOBackupCountTicks(50, (long) 3000); } if (count > 2) { lost(); } sleep(.25); /*turn around*/ if (side == 1) { /*black side*/ count = 0; to = right180(0); while (to && count < 2) { printf("\ntimed out!!!"); if (backRightHit() || backSideRightHit()) { forwardCountTicksUntilTimeout(25, (long) 2000, 0); sleep(.25); to = right90(-right90ticks/2); } else if (backLeftHit() || backSideLeftHit()) { forwardCountTicksUntilTimeout(25, (long) 2000, 0); sleep(.25); to = right90(-right90ticks*2/3); } else { left90(-left90ticks/2); sleep(.25); backwardCountTicksUntilTimeout(125, (long) 3000); sleep(.25); right90(-right90ticks/3); sleep(.25); forwardUntilHitTimeout((long) 2000); sleep(.25); to = right180(0); } count++; } /*while*/ if (count > 2) { lost(); } } else { /*white side*/ to = left180(0); count = 0; while (to && count < 2) { printf("\ntimed out!!!"); if (backRightHit() || backSideRightHit()) { forwardCountTicksUntilTimeout(25, (long) 2000, 0); sleep(.25); to = left90(-left90ticks*2/3); } else if (backLeftHit() || backSideLeftHit()) { forwardCountTicksUntilTimeout(25, (long) 2000, 0); sleep(.25); to = left90(-left90ticks/2); } else { right90(-right90ticks/2); sleep(.25); backwardCountTicksUntilTimeout(125, (long) 3000); sleep(.25); left90(-left90ticks/3); sleep(.25); forwardUntilHitTimeout((long) 2000); sleep(.25); to = left180(0); } /*while*/ count++; } if (count > 2) { lost(); } } to = backwardIntoWall((long) 3000); count = 0; while (to && count < 2) { printf("\ntimed out!!!"); if (backRightHit() || backSideRightHit()) { forwardCountTicksUntilTimeout(25, (long) 2000, 0); sleep(.25); left90(-left90ticks/2); to = backwardIntoWall((long) 3000); } else if (backLeftHit() || backSideLeftHit()) { forwardCountTicksUntilTimeout(25, (long) 2000, 0); sleep(.25); right90(-right90ticks/2); to = backwardIntoWall((long) 3000); } count++; } if (count > 2) { lost(); } sleep(.25); /*drive to bottom*/ to = forwardToDropBallTimeout((long) 8000); count = 0; while (to && count < 2) { printf("\ntimed out!!!"); if (frontRightHit() || frontSideRightHit()) { backwardCountTicksUntilTimeout(25, (long) 2000); sleep(.25); left90(-left90ticks/2); to = forwardToDropBallTimeout((long) 8000); } else if (frontLeftHit() || frontSideLeftHit()) { backwardCountTicksUntilTimeout(25, (long) 2000); sleep(.25); right90(-right90ticks/2); to = forwardToDropBallTimeout((long) 8000); } else if (ball) { rampDown(); } count++; } if (count > 2) { if (ball) { rampDown(); } lost(); } stop(); sleep(.25); /*backup to prepare for turn*/ to = backwardCountTicksUntilTimeout(25, (long) 2000); count = 0; if (to) { count = TOBackupCountTicks(25, (long) 2000); } if (count > 2) { lost(); } sleep(.25); /*turn toward opponent side*/ if (side == 1) { to = left90(3); } else if (side == 0) { to = right90(3); } count = 0; if (to) { count = TOTurn90(3); } if (count > 2) { lost();; } /*line up to drive to opponent's side*/ to = backwardIntoWall((long) 3000); count = 0; while (to && count < 2) { printf("\ntimed out!!!"); if (backRightHit() || backSideRightHit()) { forwardCountTicksUntilTimeout(25, (long) 2000, 0); sleep(.25); left90(-left90ticks/2); to = backwardIntoWall((long) 3000); } else if (backLeftHit() || backSideLeftHit()) { forwardCountTicksUntilTimeout(25, (long) 2000, 0); sleep(.25); right90(-right90ticks/2); to = backwardIntoWall((long) 3000); } count++; } if (count > 2) { lost(); } /*drive to opponent's side*/ to = forwardCountTicksUntilTimeout(1000, (long) 6000, 1); /*might be nice to return how many ticks we've travelled if we hit something*/ count = 0; count = TOForwardCountTicks(400, (long) 3000, 1); if (count > 2) { lost(); } /*turn towards opponents balls*/ if (side == 1) { to = left90(8); } else if (side == 0) { to = right90(8); } count = 0; if (to) { count = TOTurn90(8); } if (count > 2) { lost(); } /*gather his balls*/ /* armDown();*/ sleep(.05); to = forwardCountTicksUntilTimeout(300, (long) 5000, 0); count = 0; count = TOForwardCountTicks(300, (long) 5000, 0); if (count > 2) { lost(); } /*turn back to our side*/ if (side == 1) { to = left90(8); } else if (side == 0) { to = right90(8); } count = 0; if (to) { count = TOTurn90(8); } if (count > 2) { lost(); } /*drive back to our side*/ to = forwardCountTicksUntilTimeout(300, (long) 4000, 0); count = TOForwardCountTicks(300, (long) 3000, 0); if (count > 2) { lost(); } spinLeft(); sleep(2.0); stop(); /*armUp(); sleep(.5); armDown(); sleep(.5); armUp(); sleep(.5); armDown(); sleep(.5);*/ sleep(2.); armUp(); lost(); } void wait() { while(!(start_button())) { printf("\nwait...press start"); sleep(.5); } } void lost() { while(1) { printf("\nI'M LOST!!!!!!!!!!!!"); armUp(); forwardUntilHitTimeout((long) 4000); backwardCountTicksUntilTimeout(75, (long) 2000); armDown(); sleep(.5); armUp(); right90(-right90ticks/3); sleep(.4); } }