////////////////////////////////////////////////////////////////////// // // Define constants and variables to be used in the functions below // typedef struct genericRobotStruct { int powerLevel; int wheelDiameterMM; int gearRatio; int distanceBetweenWheelsMM; bool criteriaNotMetYet; } robotObj; robotObj robot; #define PI 3.14159
// There are 4 characteristics of the robot which are used in various move functions. // Each of the size characteristics are entered in milimeters (MM). // Be careful with tankbots. This distance is to the sprockets, not the center of the tread. // wheelDiameterMM = 43.2; // Self explanatory. Just look at the tire to see the diameter in MM. Use a default of 43.2--that's the typical tire we use
////////////////////////////////////////////////////////////////////// // // Define Primitive functions that may be referenced elsewhere // #define setPowerLevel(pPowerLevel) robot.powerLevel=pPowerLevel void stopOnLine() { while(SensorValue[colorSensor1]>20) { } stopAllMotors(); }
void driveForward() { setMultipleMotors(robot.powerLevel, B, C); } void driveBackward() { setMultipleMotors(robot.powerLevel * -1, B, C); } void driveForward(int distance) { driveForward(); untilDistance(distance); stopAllMotors(); } void driveBackward(int distance) { driveBackward(); untilDistance(distance); stopAllMotors(); }
////////////////////////////////////////////////////////////////////// // Just a simple swing-turn void turnRight2(int degrees){ resetMotorEncoder(C); long startingEncoderValue = getMotorEncoder(C); motor[motorC]=20; // Rotate the wheel forwards while(abs(getMotorEncoder(C)-startingEncoderValue) < degrees*3) { } stopAllMotors(); } void turnLeft(int degrees){ resetMotorEncoder(B); long startingEncoderValue = getMotorEncoder(B); motor[motorB]=20; // Rotate the wheel forwards while(abs(getMotorEncoder(B)-startingEncoderValue) < degrees*3) { } stopAllMotors(); }
//////////////////////////////////////////////////////////////////////// // void untilDistance(int moveIN) { resetMotorEncoder(B); resetMotorEncoder(C); float targetDegree; targetDegree=(moveIN * 25.4)/(robot.wheelDiameterMM*PI)*360; repeatUntil(abs(getMotorEncoder(B)) > targetDegree) { sleep(5); } } void untilTouch() { while(SensorValue(touchSensor) == 0) { // Loop until the button is pressed sleep(5); } }
////////////////////////////////////////////////////////////////////// // task FollowLine_Edge () { int diff; while(true) { displayCenteredBigTextLine(1, "Sensor %d", SensorValue[colorSensor1]); int k=20; int defaultPower=20; diff = SensorValue[colorSensor1] - k; displayCenteredBigTextLine(1, "Sensor %d", SensorValue[colorSensor1]); displayCenteredBigTextLine(4, "diff %d", diff); diff/=3; motor[B]=defaultPower-diff; motor[C]=defaultPower+diff; } } ////////////////// // Follow Center of the line // Light sensor on each edge task FollowLine () { int diff; while(true) { displayCenteredBigTextLine(1, "Sensor %d", SensorValue[colorSensor1]); int defaultPower=20; diff = SensorValue[colorSensor2] - SensorValue[colorSensor1]; displayCenteredBigTextLine(1, "Sensor %d", SensorValue[colorSensor1]); displayCenteredBigTextLine(4, "diff %d", diff); diff/=6; motor[B]=defaultPower-diff; motor[C]=defaultPower+diff; } }
////////////////////////////////////////////////////////////////////// // void stopOnLine() { while(SensorValue[colorSensor1]>20) { sleep(5); } stopAllMotors(); }
/************************************************************************************/ #include "SpeechEngine.c"; /************************************************************************************/
task main() { robot.wheelDiameterMM = 96; robot.powerLevel=30; driveForward(1); turnRight2(45); driveForward(8); driveForward(); stopOnLine(); driveForward(2); turnLeft(45); wait(1); startTask(FollowLine); // Start the line following untilDistance(26); stopTask(FollowLine);// Stop following the line stopAllMotors(); turnRight2(90); // Face the wall of balls robot.powerLevel=100;// Ready... Aim... driveForward(8); // Crash wait(1); robot.powerLevel=20; // Slow down for this move driveBackward(5); // Backup in case you are stuckon one of the trees wait(1); turnRight2(125); // Face a south by south east robot.powerLevel=20; // Slow down for this move driveForward(); stopOnLine(); // Drive up to the center line driveForward(13); // Cross over the center line turnLeft(30); // Turn toward the base robot.powerLevel=20; // Ok to move at about 40% power driveForward(10); // Drive forward turnRight2(20); // When closer to base, turn more towards the corner robot.powerLevel=10; // drive even slower driveForward(); untilTouch(); // Wait for touch sensor stopAllMotors(); // Stop now. }
task main() { robot.wheelDiameterMM = 96; robot.powerLevel=15; untilTouch(); motor[motorA]=10; // Reset the position of the arms motor[motorD]=10; wait(1); motor[motorA]=0; motor[motorD]=0; // Arm is steep at this point setMotorSyncEncoder(A, D2, 0, 30, -15); // Move the arm 30 degrees at 15% downward power wait(2); untilTouch(); driveForward(1); // Move away from the wall turnRight2(45); // Turn toward the line driveForward(8); // Drive out of base driveForward(); // Keep driving stopOnLine(); // Stop when you see the line driveForward(2); // Move forward a little turnLeft(45); // Face West startTask(FollowLine); // Startfollowing the line while(SensorValue[ultrasonicSensor] > 20) { sleep(5); } stopTask(FollowLine); // Close enough. Now stop. stopAllMotors(); // Really STOP. wait(2); driveForward(5); // Position the robot wait(2); setMotorSyncEncoder(A, D2, 0, 40, -15); // Move the arm 40 degrees at 15% downward power wait(1); driveBackward(6); turnRight2(180); // Do a U-Turn here driveForward(8); // Get away from the models turnRight2(45); // Try to get back on the line driveForward(); stopOnLine(); // Stop on the center line driveForward(2); // Cross over the center line turnLeft(40); // Face EAST startTask(FollowLine); // Startfollowing the line untilDistance(36); stopTask(FollowLine); // Close enough. Now stop. stopAllMotors(); turnRight2(45); // Turn toward Base driveForward(); // Drive toward base untilTouch(); // Until the touch sensor stopAllMotors(); // Now STOP moving. }