////////////////////////////////////////////////////////////////////// // // 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() { setMotorSync(B, C, 0, robot.powerLevel); } void driveForwardOnWall() { setMotorSync(C, B, 15, robot.powerLevel); resetMotorEncoder(B); resetMotorEncoder(C); } void driveBackward() { setMotorSync(B, C, 0, robot.powerLevel * -1); } void driveBackwardOnWall() { setMotorSync(C, B, 5, robot.powerLevel * -1); resetMotorEncoder(B); resetMotorEncoder(C); }
////////////////////////////////////////////////////////////////////// // Just a simple swing-turn void turnRight(int degrees){ resetMotorEncoder(C); long startingEncoderValue = getMotorEncoder(C); motor[motorC]=20; // Rotate the wheel forwards while(abs(getMotorEncoder(C)-startingEncoderValue) < degrees*6) { } stopAllMotors(); } void turnLeft(int degrees){ resetMotorEncoder(B); long startingEncoderValue = getMotorEncoder(B); motor[motorB]=20; // Rotate the wheel forwards while(abs(getMotorEncoder(B)-startingEncoderValue) < degrees*6) { } stopAllMotors(); } void gradualTurnClockwise(float degrees, int turnRadiusIN) { if(!OkToMove) return; degrees -= 16 / (360 / degrees); float innerTurnRadius = ((turnRadiusIN * 25.4) - (robot.distanceBetweenWheelsMM/2)) * 2 * PI * (degrees/360); float outerTurnRadius = ((turnRadiusIN * 25.4) + (robot.distanceBetweenWheelsMM/2)) * 2 * PI * (degrees/360); float turnRatio = 50 - (innerTurnRadius/outerTurnRadius * 50); setMotorSync(C, B,-1 * turnRatio,20); resetMotorEncoder(B); resetMotorEncoder(C); repeatUntil(getMotorEncoder(C)>(outerTurnRadius/(robot.wheelDiameterMM*PI))*360) { sleep(5); } setMotorSpeeds(0,0); stopAllMotors(); } void gradualTurnCounterClockwise(float degrees, int turnRadiusIN) { if(!OkToMove) return; degrees -= 16 / (360 / degrees); float innerTurnRadius = ((turnRadiusIN * 25.4) - (robot.distanceBetweenWheelsMM/2)) * 2 * PI * (degrees/360); float outerTurnRadius = ((turnRadiusIN * 25.4) + (robot.distanceBetweenWheelsMM/2)) * 2 * PI * (degrees/360); float turnRatio = 50 - (innerTurnRadius/outerTurnRadius * 50); setMotorSync(B, C, turnRatio,20); resetMotorEncoder(B); resetMotorEncoder(C); repeatUntil(getMotorEncoder(B)>(outerTurnRadius/(robot.wheelDiameterMM*PI))*360) { sleep(5); } setMotorSpeeds(0,0); stopAllMotors(); } void gradualTurnClockwise(float degrees) { gradualTurnClockwise(degrees, 20); } void gradualTurnCounterClockwise(float degrees) { gradualTurnCounterClockwise(degrees, 20); }
//////////////////////////////////////////////////////////////////////// // 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); } } void untilSonarLessThan(float distance_in_inches) { float distance_in_cm; distance_in_cm = distance_in_inches * 2.54; while(SensorValue((short) ultrasonicSensor) > distance_in_cm) { displayCenteredBigTextLine(1, "Sensor %d", SensorValue[ultrasonicSensor]); sleep(5); } } void untilArmTravelled(int degrees, int defPower) { resetMotorEncoder(A); // Reset the motor float targetDegree; targetDegree=(degrees * 5);// the small tan gears are 5 times smaller than the turntable gear if(degrees<0) { degrees *= -1; defPower *= -1; } // If degrees are negative, then reverse the power motor[A] = defPower * -1; // make the power reversed because of the gearbox being used repeatUntil(abs(getMotorEncoder(A)) > targetDegree) { sleep(5); } motor[A]=0; }
// Overloaded function void driveForward(int distance) { driveForward(); untilDistance(distance); stopAllMotors(); } void driveForwardOnWall(int distance) { driveForwardOnWall(); untilDistance(distance); stopAllMotors(); } void driveBackward(int distance) { driveBackward(); untilDistance(distance); stopAllMotors(); } void driveBackwardOnWall(int distance) { driveBackwardOnWall(); untilDistance(distance); stopAllMotors(); }
////////////////////////////////////////////////////////////////////// // task FollowLine_Edge () { int diff; while(true) { displayCenteredBigTextLine(1, "Sensor %d", SensorValue[colorSensor1]); int k=20; int defaultPower=25; 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() { wait(1); robot.wheelDiameterMM = 43.2; robot.distanceBetweenWheelsMM = 130; robot.powerLevel=40; untilTouch(); driveForward(25); turnRight(30); driveForward(10); turnLeft(30); driveForward(10); driveBackward(24); turnLeft(57); driveForward(22); driveBackward(4); turnRight(90); driveBackward(10); robot.powerLevel=60; driveForward(14); robot.powerLevel=40; driveBackward(19); turnRight(99); /* stopAllMotors(); wait(2); driveForward(2); turnLeft(90); driveForward(3); driveBackward(6); turnRight(90); startTask(FollowLine); untilSonarLessThan(3); stopTask(FollowLine); stopAllMotors(); wait(2); driveForward(2); turnLeft(90); driveForward(3); driveBackward(6); */ }
task main() { wait(1); robot.wheelDiameterMM = 43.2; robot.distanceBetweenWheelsMM = 130; robot.powerLevel=40; untilTouch(); driveForward(13); wait(1); driveForward(); stopOnLine(); turnRight(50); startTask(FollowLine); while(SensorValue((short) ultrasonicSensor) > 3) { displayCenteredBigTextLine(6, "Sensor %d", SensorValue[ultrasonicSensor]); //sleep(5); } untilSonarLessThan(3); stopTask(FollowLine); stopAllMotors(); motor[A]=10; wait(2); motor[A]=0; robot.wheelDiameterMM = 43.2; robot.distanceBetweenWheelsMM = 130; robot.powerLevel=40; //COMING BACK TO BASE driveBackward(9); turnRight(270); driveForward(); stopOnLine(); turnLeft(90); startTask(FollowLine); untilDistance(36); stopTask(FollowLine); stopAllMotors(); turnLeft(38); driveForward(36); /*while(SensorValue((short) ultrasonicSensor) > 3) { displayCenteredBigTextLine(6, "Sensor %d", SensorValue[ultrasonicSensor]); } */ /* stopAllMotors(); wait(2); driveForward(2); turnLeft(90); driveForward(3); driveBackward(6); turnRight(90); startTask(FollowLine); untilSonarLessThan(3); stopTask(FollowLine); */ }