03.03.2013 Views

Ultrasonic Rangefinder Forward till Near - ROBOTC.net

Ultrasonic Rangefinder Forward till Near - ROBOTC.net

Ultrasonic Rangefinder Forward till Near - ROBOTC.net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Sensing<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> <strong>Forward</strong> <strong>till</strong> <strong>Near</strong><br />

In this lesson, you will learn how an <strong>Ultrasonic</strong> <strong>Rangefinder</strong> (a.k.a. Sonar Sensor)<br />

works, and how to use it to move to within a specific distance of an object.<br />

The Touch Sensors (Bumper and Limit Switches) allow your robot to detect physical contact.<br />

They allow the robot to keep track of the position of its arm, and can potentially detect walls<br />

or other objects in the environment if the robot bumps into them.<br />

The Encoders allow your robot to measure rotation of motors, wheels, and other important<br />

parts. Measuring the rotation of these parts can tell you how far the robot has traveled.<br />

We s<strong>till</strong> do not have a sensor that allows the robot to detect objects without physically<br />

hitting them. If the robot ever hopes to pick up the mines without knocking them over<br />

during the autonomous period, “touchless” detection will be absolutely necessary.<br />

© Carnegie Mellon Robotics Academy / For use with VEX ® Robotics Systems<br />

<strong>ROBOTC</strong><br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> • 1


Sensing<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> <strong>Forward</strong> <strong>till</strong> <strong>Near</strong> (cont.)<br />

The <strong>Ultrasonic</strong> <strong>Rangefinder</strong> offers exactly this capability. Using the same physical principle<br />

as a bat or submarine’s sonar, the <strong>Ultrasonic</strong> <strong>Rangefinder</strong> measures distances using<br />

sound. It sends out a pulse of sound, then waits to hear the sound’s echo off of a solid<br />

object in the environment. By measuring how long it takes for the sound to bounce back,<br />

the sensor can calculate the distance that the sound must have traveled, and hence, how<br />

far away the object was that reflected it back.<br />

The <strong>Ultrasonic</strong> <strong>Rangefinder</strong> will work in a very similar way to the Encoder program you<br />

wrote in the previous section, but instead of measuring the distance that the wheel has<br />

turned, it will use the <strong>Ultrasonic</strong> <strong>Rangefinder</strong> to measure the distance to the nearest object<br />

in front of the arm.<br />

1. Add an <strong>Ultrasonic</strong> <strong>Rangefinder</strong> to the front of the robot’s arm. The design shown below<br />

will be used as the reference for the remainder of the unit. If you choose to use a<br />

design that differs significantly from this one, you may need to adjust accordingly.<br />

© Carnegie Mellon Robotics Academy / For use with VEX ® Robotics Systems<br />

1a. Add the sensor<br />

Attach the <strong>Ultrasonic</strong><br />

<strong>Rangefinder</strong> to the front of<br />

the Squarebot 3.0 arm.<br />

<strong>ROBOTC</strong><br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> • 2


Sensing<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> <strong>Forward</strong> <strong>till</strong> <strong>Near</strong> (cont.)<br />

2. Create a new program.<br />

3. Save the new program as “<strong>Forward</strong><strong>Near</strong>”.<br />

© Carnegie Mellon Robotics Academy / For use with VEX ® Robotics Systems<br />

2. File > New<br />

Select File > New to<br />

create a new program.<br />

3a. File > Save As...<br />

Select File > Save As...<br />

to save your program<br />

under a new name.<br />

1b. Connect the OUTPUT cable<br />

Connect the OUTPUT cable of<br />

the <strong>Ultrasonic</strong> <strong>Rangefinder</strong> to<br />

INTERRUPT port 1.<br />

1c. Connect the INPUT cable<br />

Connect the INPUT cable of<br />

the <strong>Ultrasonic</strong> <strong>Rangefinder</strong> to<br />

ANALOG / DIGITAL port 5.<br />

3b. Renate program<br />

Give this program the name <strong>Forward</strong><strong>Near</strong>.<br />

3c. Save<br />

Click Save.<br />

4. Use the Motors and Sensors Setup Menu to configure the <strong>Ultrasonic</strong> <strong>Rangefinder</strong> on<br />

Analog Digital Port 5, and Interrupt 1.<br />

4a. Robot > Motors and Sensors Setup<br />

Select Robot > Motors and Sensors Setup to<br />

open up the configuration menu.<br />

<strong>ROBOTC</strong><br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> • 3


Sensing<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> <strong>Forward</strong> <strong>till</strong> <strong>Near</strong> (cont.)<br />

Checkpoint<br />

© Carnegie Mellon Robotics Academy / For use with VEX ® Robotics Systems<br />

4b. Select A/D Sensors 1-8<br />

<strong>ROBOTC</strong><br />

4c. Set the sensor Name to “sonarSensor”<br />

The <strong>Ultrasonic</strong> <strong>Rangefinder</strong> is indexed by<br />

the number of the Analog/Digital Port it is<br />

plugged into, so enter a name for the sensor<br />

under Analog/Digital Port in5.<br />

4d. Set the sensor Type to “SONAR”<br />

Set the type of the sensor to “SONAR”,<br />

another name for the <strong>Ultrasonic</strong><br />

<strong>Rangefinder</strong> (because it uses sonar<br />

sound waves to measure distance).<br />

4e. Configure the Second Port to “int1”<br />

When you select “SONAR” as<br />

the sensor Type, a column for the<br />

“second port” appears for this<br />

sensor. The second port is the other<br />

port that the sensor is plugged into,<br />

Interrupt 1 (int1).<br />

4f. Click OK<br />

Click OK to finish configuring<br />

the <strong>Ultrasonic</strong> <strong>Rangefinder</strong>.<br />

The <strong>Ultrasonic</strong> <strong>Rangefinder</strong> is now set up and recognized by the program. It will now provide<br />

sensor readings as values through SensorValue[sonarSensor]. The values represent<br />

distances to the nearest detectable object (the first echo that the sensor hears), in inches. If an<br />

object is 6 inches away, in front of the sensor, SensorValue[sonarSensor] will be 6.<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> • 4


Sensing<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> <strong>Forward</strong> <strong>till</strong> <strong>Near</strong> (cont.)<br />

Let us first begin by reviewing the way a forward-for-distance Encoder behavior works,<br />

and then adapt it to use the <strong>Ultrasonic</strong> <strong>Rangefinder</strong> instead.<br />

The forward-for-distance command below was taken from the early parts of the<br />

Encoder lesson, and moves the robot forward until the Encoder accumulates more than<br />

5000 counts of rotation. It does so by using a while loop to repeat basic movement<br />

commands as long as the current count is s<strong>till</strong> below the desired target.<br />

while(SensorValue[leftEncoder] < 5000)<br />

{<br />

motor[port3] = 63;<br />

motor[port2] = 63;<br />

}<br />

In the case of the <strong>Ultrasonic</strong> <strong>Rangefinder</strong> moving until the robot is close to an object<br />

(such as the “stem” of a mine), we want the robot to move until the detected distance<br />

is below the target distance. Rephrased, the robot should keep running as long as the<br />

distance to the object is s<strong>till</strong> greater than the desired distance.<br />

5. Add task main and base code.<br />

Auto<br />

Auto<br />

1<br />

2<br />

3<br />

4<br />

5<br />

const tSensors sonarSensor = (tSensors) in5;<br />

task main()<br />

{<br />

wait1Msec(2000);<br />

bMotorReflected[port2] = 1;<br />

}<br />

6. Add the basic structure of a move-until behavior.<br />

Auto<br />

Auto<br />

1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

9<br />

10<br />

11<br />

const tSensors sonarSensor = (tSensors) in5;<br />

task main()<br />

{<br />

wait1Msec(2000);<br />

bMotorReflected[port2] = 1;<br />

while()<br />

{<br />

motor[port3] = 63;<br />

motor[port2] = 63;<br />

}<br />

}<br />

© Carnegie Mellon Robotics Academy / For use with VEX ® Robotics Systems<br />

5. Add this code<br />

Add task main, an initial delay, and<br />

the motor reflection command before<br />

proceeding with the rest of the program.<br />

6. Add this code<br />

The basic structure of the behavior<br />

is a while loop containing movingforward<br />

commands. The (condition)<br />

will determine how long this loop<br />

lasts, and is left blank for now.<br />

<strong>ROBOTC</strong><br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> • 5


Sensing<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> <strong>Forward</strong> <strong>till</strong> <strong>Near</strong> (cont.)<br />

7. Choose the (condition) based on the <strong>Ultrasonic</strong> <strong>Rangefinder</strong>. The (condition)<br />

should make the movement commands repeat as long as the distance to the<br />

target is s<strong>till</strong> above the desired value.<br />

Auto<br />

Auto<br />

1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

9<br />

10<br />

11<br />

const tSensors sonarSensor = (tSensors) in5;<br />

task main()<br />

{<br />

wait1Msec(2000);<br />

bMotorReflected[port2] = 1;<br />

while(SensorValue[sonarSensor] > 3)<br />

{<br />

motor[port3] = 63;<br />

motor[port2] = 63;<br />

}<br />

}<br />

8. Download and Run.<br />

Checkpoint<br />

© Carnegie Mellon Robotics Academy / For use with VEX ® Robotics Systems<br />

8. Go to Compile and Download Program<br />

7. Add this code<br />

The (condition) should be true as long<br />

as the <strong>Rangefinder</strong>’s value is above 3<br />

inches. This will make the robot move<br />

forward as long as the <strong>Rangefinder</strong> does<br />

not detect any objects within 3 inches.<br />

Your robot will run forward until it is within 3 inches of a detectable object. Not all objects are<br />

detectable, however, and the “area” of detection is only in front of the <strong>Ultrasonic</strong> <strong>Rangefinder</strong><br />

itself. These limitations are inherent to the technology, but the sensor does seem to be able to<br />

detect the mines, as we had hoped.<br />

<strong>ROBOTC</strong><br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> • 6


Sensing<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> <strong>Forward</strong> <strong>till</strong> <strong>Near</strong> (cont.)<br />

9. Upgrade this behavior to go straight while there is no obstacle nearby,<br />

rather than just run forward.<br />

© Carnegie Mellon Robotics Academy / For use with VEX ® Robotics Systems<br />

9a. Robot > Motors and Sensors Setup<br />

Select Robot > Motors and Sensors Setup<br />

to open up the configuration menu.<br />

9e. File > Save<br />

Select File > Save to<br />

save your program.<br />

9f. File > Open and Compile<br />

9d. Press OK<br />

Confirm the new<br />

sensor configuration.<br />

<strong>ROBOTC</strong><br />

9b. “Name” the sensors<br />

Assign the name “leftEncoder” to<br />

the sensor in port “in3” (A/D input<br />

3). Name the “in2” (A/D input 2)<br />

sensor “rightEncoder”.<br />

9c. Set the “Type” of both sensors<br />

Identify the sensor attached to both<br />

in2 and in3 as “Rotation” sensors<br />

(Encoders sense rotation).<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> • 7


Sensing<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> <strong>Forward</strong> <strong>till</strong> <strong>Near</strong> (cont.)<br />

1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

9<br />

10<br />

11<br />

12<br />

13<br />

14<br />

15<br />

16<br />

17<br />

18<br />

19<br />

void moveStraight()<br />

{<br />

if(SensorValue[leftEncoder] > SensorValue[rightEncoder])<br />

{<br />

motor[port3] = 50;<br />

motor[port2] = 63;<br />

}<br />

if(SensorValue[leftEncoder] < SensorValue[rightEncoder])<br />

{<br />

motor[port3] = 63;<br />

motor[port2] = 50;<br />

}<br />

if(SensorValue[leftEncoder] == SensorValue[rightEncoder])<br />

{<br />

motor[port3] = 63;<br />

motor[port2] = 63;<br />

}<br />

}<br />

}<br />

© Carnegie Mellon Robotics Academy / For use with VEX ® Robotics Systems<br />

9i. Copy this code<br />

Select Edit > Copy to put the<br />

highlighted code on the clipboard.<br />

9j. Select File > Open and Compile<br />

<strong>ROBOTC</strong><br />

9g. Open MineArmStraight<br />

Instead of re-entering the movingstraight<br />

code, we’ll take a copy of<br />

it from the Encoders program.<br />

9h. Highlight code<br />

Find the void<br />

moveStraight()<br />

function and highlight it.<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> • 8


Sensing<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> <strong>Forward</strong> <strong>till</strong> <strong>Near</strong> (cont.)<br />

© Carnegie Mellon Robotics Academy / For use with VEX ® Robotics Systems<br />

9k. Open <strong>Forward</strong><strong>Near</strong><br />

Return to the <strong>Forward</strong><br />

Till <strong>Near</strong> <strong>Ultrasonic</strong><br />

<strong>Rangefinder</strong> program.<br />

9l. Place cursor here<br />

Place your cursor between task<br />

main and the AUTO code.<br />

<strong>ROBOTC</strong><br />

9m. Paste<br />

The moving straight function is<br />

now declared, and available<br />

for use in this program.<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> • 9


Sensing<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> <strong>Forward</strong> <strong>till</strong> <strong>Near</strong> (cont.)<br />

Auto<br />

Auto<br />

Auto<br />

Auto<br />

1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

9<br />

10<br />

11<br />

12<br />

13<br />

14<br />

15<br />

16<br />

17<br />

18<br />

19<br />

20<br />

21<br />

22<br />

23<br />

24<br />

25<br />

26<br />

27<br />

28<br />

29<br />

30<br />

31<br />

32<br />

const tSensors rightEncoder = (tSensors) in2;<br />

const tSensors leftEncoder = (tSensors) in3;<br />

const tSensors sonarSensor = (tSensors) in5;<br />

void moveStraight()<br />

{<br />

if(SensorValue[leftEncoder] > SensorValue[rightEncoder])<br />

{<br />

motor[port3] = 50;<br />

motor[port2] = 63;<br />

}<br />

if(SensorValue[leftEncoder] < SensorValue[rightEncoder])<br />

{<br />

motor[port3] = 63;<br />

motor[port2] = 50;<br />

}<br />

if(SensorValue[leftEncoder] == SensorValue[rightEncoder])<br />

{<br />

motor[port3] = 63;<br />

motor[port2] = 63;<br />

}<br />

}<br />

task main()<br />

{<br />

wait1Msec(2000);<br />

bMotorReflected[port2] = 1;<br />

SensorValue[leftEncoder] = 0;<br />

SensorValue[rightEncoder] = 0;<br />

while(SensorValue[sonarSensor] > 3)<br />

{<br />

moveStraight();<br />

}<br />

}<br />

10. Download and Run.<br />

Checkpoint<br />

© Carnegie Mellon Robotics Academy / For use with VEX ® Robotics Systems<br />

10. Go to Compile and Download Program<br />

9n. Add this code<br />

Add these commands to<br />

reset the values of the<br />

encoders to 0.<br />

<strong>ROBOTC</strong><br />

9o. Modify this code<br />

Replace the generic motorforward<br />

commands with a<br />

call to moveStraight();.<br />

Your robot now runs straight until its <strong>Ultrasonic</strong> Sonar <strong>Rangefinder</strong> detects an object within<br />

3 inches, in its forward field of view. You can change the stopping distance by changing<br />

the 3 inch “cutoff” point.<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> • 10


Sensing<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> <strong>Forward</strong> <strong>till</strong> <strong>Near</strong> (cont.)<br />

This is the second sensor you have programmed to use a “cutoff” value. The Encoder program<br />

ran a movement behavior until the encoder count exceeded a certain value, and this new program<br />

runs a movement behavior until the <strong>Ultrasonic</strong> <strong>Rangefinder</strong> distance goes below a certain value.<br />

These “cutoff” values – called thresholds – are important in robot decision-making.<br />

Thresholds are values that set a cutoff point in a range of values, so that even though there<br />

are many possible values for encoder counts or distances, every one of them will fall either<br />

below the threshold or above it. This division of the many possible values into two distinct<br />

categories (above and below the threshold) allows the robot to make a definite decision<br />

about how to proceed for any value it may encounter.<br />

© Carnegie Mellon Robotics Academy / For use with VEX ® Robotics Systems<br />

threshold<br />

below above<br />

<strong>ROBOTC</strong><br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> • 11


Sensing<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> <strong>Forward</strong> <strong>till</strong> <strong>Near</strong> (cont.)<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> values above the threshold of 3 inches made the program continue<br />

looping and moving forward. Values below the threshold caused the robot to stop. The threshold<br />

sets the point at which the robot’s behavior will change, because it marks the point at which the<br />

(condition) in the while loop (or if-statement) will change from true to false, or false to true, and<br />

thus change which lines of code will run.<br />

26<br />

27<br />

28<br />

29<br />

30<br />

31<br />

32<br />

SensorValue[rightEncoder] = 0;<br />

while(SensorValue[sonarSensor] > 3)<br />

{<br />

moveStraight();<br />

}<br />

}<br />

The robot is now set and ready to run for any object the <strong>Ultrasonic</strong> <strong>Rangefinder</strong> might see…<br />

but what happens when it doesn’t see anything at all? The sound waves have a limited range<br />

before the echo is too soft for the sensor to pick up. In addition, some materials or surfaces can<br />

actually deflect the sound waves away from the sensor, preventing it from hearing the echo.<br />

What happens then?<br />

11. Making sure that your robot is turned on and plugged in, open the <strong>ROBOTC</strong><br />

Debugger and Devices windows. Run the program.<br />

© Carnegie Mellon Robotics Academy / For use with VEX ® Robotics Systems<br />

11a. Robot > Debugger<br />

Go to Robot > Debugger to open<br />

the Program Debug window.<br />

11b. Robot > Debug Windows > Devices<br />

Open the Devices window so that you can<br />

monitor the <strong>Ultrasonic</strong> <strong>Rangefinder</strong> values.<br />

<strong>ROBOTC</strong><br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> • 12


Sensing<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> <strong>Forward</strong> <strong>till</strong> <strong>Near</strong> (cont.)<br />

© Carnegie Mellon Robotics Academy / For use with VEX ® Robotics Systems<br />

11c. Pick up the robot<br />

Pick up the robot so that<br />

it doesn’t drive away.<br />

11d. Run the program<br />

12. Point your robot’s sensor away from any nearby objects, or toward a very soft object, like a<br />

sweater or cushion. Observe the value that the sensor gives through the <strong>ROBOTC</strong> debugger.<br />

12a. No Signal<br />

Orient your <strong>Ultrasonic</strong> <strong>Rangefinder</strong> so that it<br />

will have difficulty getting an echo back from<br />

whatever is in front of it. This may involve<br />

aiming it at something very far away or at<br />

something soft (which absorbs sound).<br />

<strong>ROBOTC</strong><br />

12b. Observe sensor value<br />

The sonarSensor will show<br />

a value of -1 if it is unable<br />

to measure distance for<br />

any reason.<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> • 13


Sensing<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> <strong>Forward</strong> <strong>till</strong> <strong>Near</strong> (cont.)<br />

13. Run your straight-<strong>till</strong>-near program in a place where the robot will get a “-1” sonar reading.<br />

Checkpoint<br />

© Carnegie Mellon Robotics Academy / For use with VEX ® Robotics Systems<br />

13a. Position the robot<br />

Place your robot so that it faces<br />

down a very long hallway, or<br />

toward a sound-absorbing object.<br />

13b. Run the robot<br />

Switch the robot off and back<br />

on to run the program.<br />

13c. Observe robot behavior<br />

The robot stops immediately, as<br />

if it were close to an object.<br />

Why would the robot act like it was close to an object when the exact opposite was true? The<br />

SensorValue of an <strong>Ultrasonic</strong> <strong>Rangefinder</strong> is returned as -1 when there is no object in range.<br />

Consider how your robot is making its decisions:<br />

while (SensorValue[sonarSensor] > 3)<br />

{<br />

moveStraight();<br />

}<br />

<strong>ROBOTC</strong><br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> • 14


Sensing<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> <strong>Forward</strong> <strong>till</strong> <strong>Near</strong> (cont.)<br />

The robot will move straight as long as the <strong>Ultrasonic</strong> <strong>Rangefinder</strong> detects an<br />

object farther than 3 inches away.<br />

© Carnegie Mellon Robotics Academy / For use with VEX ® Robotics Systems<br />

Object at 6 in<br />

SensorValue[sonarSensor] is 6, therefore<br />

SensorValue[sonarSensor] > 3 is true.<br />

The loop will continue looping.<br />

Object at 2 in<br />

SensorValue[sonarSensor] is 2, therefore<br />

SensorValue[sonarSensor] > 3 is false.<br />

The loop will end. The next portion of the<br />

program causes the robot to stop.<br />

No object<br />

SensorValue[sonarSensor] is -1, therefore<br />

SensorValue[sonarSensor] > 3 is false.<br />

The loop will end. The next portion of the program<br />

causes the robot to stop.<br />

<strong>ROBOTC</strong><br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> • 15


Sensing<br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> <strong>Forward</strong> <strong>till</strong> <strong>Near</strong> (cont.)<br />

14. Change the (condition) for continuing to move forward so that the robot will run while<br />

the object is detected farther than 3 in OR too far to detect.<br />

Auto<br />

Auto<br />

Auto<br />

Auto<br />

1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

9<br />

10<br />

11<br />

12<br />

13<br />

14<br />

15<br />

16<br />

17<br />

18<br />

19<br />

20<br />

21<br />

22<br />

23<br />

24<br />

25<br />

26<br />

27<br />

28<br />

29<br />

30<br />

31<br />

32<br />

End of Lesson<br />

const tSensors rightEncoder = (tSensors) in2;<br />

const tSensors leftEncoder = (tSensors) in3;<br />

const tSensors sonarSensor = (tSensors) in5;<br />

void moveStraight()<br />

{<br />

if(SensorValue[leftEncoder] > SensorValue[rightEncoder])<br />

{<br />

motor[port3] = 50;<br />

motor[port2] = 63;<br />

}<br />

if(SensorValue[leftEncoder] < SensorValue[rightEncoder])<br />

{<br />

motor[port3] = 63;<br />

motor[port2] = 50;<br />

}<br />

if(SensorValue[leftEncoder] == SensorValue[rightEncoder])<br />

{<br />

motor[port3] = 63;<br />

motor[port2] = 63;<br />

}<br />

}<br />

task main()<br />

{<br />

wait1Msec(2000);<br />

bMotorReflected[port2] = 1;<br />

SensorValue[leftEncoder] = 0;<br />

SensorValue[rightEncoder] = 0;<br />

while(SensorValue[sonarSensor] > 3 || SensorValue[sonarSensor] < 0)<br />

{<br />

moveStraight();<br />

}<br />

}<br />

The fine tuned code makes the robot move straight forward whenever it is far away from<br />

an object, even if it is so far away that it cannot detect it. You now have all of the tools<br />

you need to score additional points during the autonomous period of the Mine Removal<br />

Challenge. Now, use what you’ve learned to create your own final competition program!<br />

© Carnegie Mellon Robotics Academy / For use with VEX ® Robotics Systems<br />

14a. Modify this code<br />

Add a second part to the condition,<br />

using an OR (||) connector.<br />

<strong>ROBOTC</strong><br />

<strong>Ultrasonic</strong> <strong>Rangefinder</strong> • 16

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!