-
ROBOTC
Ultrasonic Rangefinder • 1© Carnegie Mellon Robotics Academy /
For use with VEX® Robotics Systems
Sensing
In this lesson, you will learn how an Ultrasonic Rangefinder
(a.k.a. Sonar Sensor) works, and how to use it to move to within a
specific distance of an object.
Ultrasonic Rangefinder Forward till Near
The Touch Sensors (Bumper and Limit Switches) allow your robot
to detect physical contact. They allow the robot to keep track of
the position of its arm, and can potentially detect walls or other
objects in the environment if the robot bumps into them.
The Encoders allow your robot to measure rotation of motors,
wheels, and other important parts. Measuring the rotation of these
parts can tell you how far the robot has traveled.
We still do not have a sensor that allows the robot to detect
objects without physically hitting them. If the robot ever hopes to
pick up the mines without knocking them over during the autonomous
period, “touchless” detection will be absolutely necessary.
-
ROBOTC
Ultrasonic Rangefinder • 2© Carnegie Mellon Robotics Academy /
For use with VEX® Robotics Systems
Sensing
Ultrasonic Rangefinder Forward till Near (cont.)
The Ultrasonic Rangefinder offers exactly this capability. Using
the same physical principle as a bat or submarine’s sonar, the
Ultrasonic Rangefinder measures distances using sound. It sends out
a pulse of sound, then waits to hear the sound’s echo off of a
solid object in the environment. By measuring how long it takes for
the sound to bounce back, the sensor can calculate the distance
that the sound must have traveled, and hence, how far away the
object was that reflected it back.
The Ultrasonic Rangefinder will work in a very similar way to
the Encoder program you wrote in the previous section, but instead
of measuring the distance that the wheel has turned, it will use
the Ultrasonic Rangefinder to measure the distance to the nearest
object in front of the arm.
1. Add an Ultrasonic Rangefinder to the front of the robot’s
arm. The design shown below will be used as the reference for the
remainder of the unit. If you choose to use a design that differs
significantly from this one, you may need to adjust
accordingly.
1a. Add the sensorAttach the Ultrasonic Rangefinder to the front
of the Squarebot 3.0 arm.
-
ROBOTC
Ultrasonic Rangefinder • 3© Carnegie Mellon Robotics Academy /
For use with VEX® Robotics Systems
Sensing
2. Create a new program.
3. Save the new program as “ForwardNear”.
4. Use the Motors and Sensors Setup Menu to configure the
Ultrasonic Rangefinder on Analog Digital Port 5, and Interrupt
1.
2. File > NewSelect File > New to create a new
program.
3a. File > Save As...Select File > Save As... to save your
program under a new name.
3b. Renate programGive this program the name ForwardNear.
3c. SaveClick Save.
4a. Robot > Motors and Sensors SetupSelect Robot > Motors
and Sensors Setup to open up the configuration menu.
1b. Connect the OUTPUT cableConnect the OUTPUT cable of the
Ultrasonic Rangefinder to INTERRUPT port 1.
1c. Connect the INPUT cableConnect the INPUT cable of the
Ultrasonic Rangefinder to ANALOG / DIGITAL port 5.
Ultrasonic Rangefinder Forward till Near (cont.)
-
ROBOTC
Ultrasonic Rangefinder • 4© Carnegie Mellon Robotics Academy /
For use with VEX® Robotics Systems
Sensing
4b. Select A/D Sensors 1-8
4c. Set the sensor Name to “sonarSensor”The Ultrasonic
Rangefinder is indexed by the number of the Analog/Digital Port it
is plugged into, so enter a name for the sensor under
Analog/Digital Port in5.
4d. Set the sensor Type to “SONAR”Set the type of the sensor to
“SONAR”, another name for the Ultrasonic Rangefinder (because it
uses sonar sound waves to measure distance).
4e. Configure the Second Port to “int1”When you select “SONAR”
as the sensor Type, a column for the “second port” appears for this
sensor. The second port is the other port that the sensor is
plugged into, Interrupt 1 (int1).
4f. Click OKClick OK to finish configuring the Ultrasonic
Rangefinder.
Checkpoint
The Ultrasonic Rangefinder is now set up and recognized by the
program. It will now provide sensor readings as values through
SensorValue[sonarSensor]. The values represent distances to the
nearest detectable object (the first echo that the sensor hears),
in inches. If an object is 6 inches away, in front of the sensor,
SensorValue[sonarSensor] will be 6.
Ultrasonic Rangefinder Forward till Near (cont.)
-
ROBOTC
Ultrasonic Rangefinder • 5© Carnegie Mellon Robotics Academy /
For use with VEX® Robotics Systems
Sensing
Let us first begin by reviewing the way a forward-for-distance
Encoder behavior works, and then adapt it to use the Ultrasonic
Rangefinder instead.
The forward-for-distance command below was taken from the early
parts of the Encoder lesson, and moves the robot forward until the
Encoder accumulates more than 5000 counts of rotation. It does so
by using a while loop to repeat basic movement commands as long as
the current count is still below the desired target.
while(SensorValue[leftEncoder] < 5000) { motor[port3] = 63;
motor[port2] = 63; }
In the case of the Ultrasonic Rangefinder moving until the robot
is close to an object (such as the “stem” of a mine), we want the
robot to move until the detected distance is below the target
distance. Rephrased, the robot should keep running as long as the
distance to the object is still greater than the desired
distance.
AutoAuto
12345
const tSensors sonarSensor = (tSensors) in5;
task main(){ wait1Msec(2000); bMotorReflected[port2] = 1;}
5. Add task main and base code.
5. Add this codeAdd task main, an initial delay, and the motor
reflection command before proceeding with the rest of the
program.
AutoAuto
123456789
1011
const tSensors sonarSensor = (tSensors) in5;
task main(){ wait1Msec(2000); bMotorReflected[port2] = 1;
while() { motor[port3] = 63; motor[port2] = 63; }}
6. Add the basic structure of a move-until behavior.
6. Add this codeThe basic structure of the behavior is a while
loop containing moving-forward commands. The (condition) will
determine how long this loop lasts, and is left blank for now.
Ultrasonic Rangefinder Forward till Near (cont.)
-
ROBOTC
Ultrasonic Rangefinder • 6© Carnegie Mellon Robotics Academy /
For use with VEX® Robotics Systems
Sensing
7. Choose the (condition) based on the Ultrasonic Rangefinder.
The (condition) should make the movement commands repeat as long as
the distance to the target is still above the desired value.
AutoAuto
123456789
1011
const tSensors sonarSensor = (tSensors) in5;
task main(){ wait1Msec(2000); bMotorReflected[port2] = 1;
while(SensorValue[sonarSensor] > 3) { motor[port3] = 63;
motor[port2] = 63; }}
7. Add this codeThe (condition) should be true as long as the
Rangefinder’s value is above 3 inches. This will make the robot
move forward as long as the Rangefinder does not detect any objects
within 3 inches.
8. Download and Run.
8. Go to Compile and Download Program
Checkpoint
Your robot will run forward until it is within 3 inches of a
detectable object. Not all objects are detectable, however, and the
“area” of detection is only in front of the Ultrasonic Rangefinder
itself. These limitations are inherent to the technology, but the
sensor does seem to be able to detect the mines, as we had
hoped.
Ultrasonic Rangefinder Forward till Near (cont.)
-
ROBOTC
Ultrasonic Rangefinder • 7© Carnegie Mellon Robotics Academy /
For use with VEX® Robotics Systems
Sensing
9. Upgrade this behavior to go straight while there is no
obstacle nearby, rather than just run forward.
9a. Robot > Motors and Sensors SetupSelect Robot > Motors
and Sensors Setup to open up the configuration menu.
9d. Press OKConfirm the new sensor configuration.
9c. Set the “Type” of both sensorsIdentify the sensor attached
to both in2 and in3 as “Rotation” sensors (Encoders sense
rotation).
9b. “Name” the sensorsAssign the name “leftEncoder” to the
sensor in port “in3” (A/D input 3). Name the “in2” (A/D input 2)
sensor “rightEncoder”.
9e. File > SaveSelect File > Save to save your
program.
9f. File > Open and Compile
Ultrasonic Rangefinder Forward till Near (cont.)
-
ROBOTC
Ultrasonic Rangefinder • 8© Carnegie Mellon Robotics Academy /
For use with VEX® Robotics Systems
Sensing
123456789
10111213141516171819
void moveStraight(){ if(SensorValue[leftEncoder] >
SensorValue[rightEncoder]) { motor[port3] = 50; motor[port2] = 63;
} if(SensorValue[leftEncoder] < SensorValue[rightEncoder]) {
motor[port3] = 63; motor[port2] = 50; } if(SensorValue[leftEncoder]
== SensorValue[rightEncoder]) { motor[port3] = 63; motor[port2] =
63; } }}
9h. Highlight codeFind the void moveStraight() function and
highlight it.
9g. Open MineArmStraight Instead of re-entering the
moving-straight code, we’ll take a copy of it from the Encoders
program.
9i. Copy this codeSelect Edit > Copy to put the highlighted
code on the clipboard.
9j. Select File > Open and Compile
Ultrasonic Rangefinder Forward till Near (cont.)
-
ROBOTC
Ultrasonic Rangefinder • 9© Carnegie Mellon Robotics Academy /
For use with VEX® Robotics Systems
Sensing
9k. Open ForwardNearReturn to the Forward Till Near Ultrasonic
Rangefinder program.
9l. Place cursor herePlace your cursor between task main and the
AUTO code.
9m. PasteThe moving straight function is now declared, and
available for use in this program.
Ultrasonic Rangefinder Forward till Near (cont.)
-
ROBOTC
Ultrasonic Rangefinder • 10© Carnegie Mellon Robotics Academy /
For use with VEX® Robotics Systems
Sensing
AutoAutoAutoAuto
123456789
1011121314151617181920212223242526272829303132
const tSensors rightEncoder = (tSensors) in2;const tSensors
leftEncoder = (tSensors) in3; const tSensors sonarSensor =
(tSensors) in5;
void moveStraight(){ if(SensorValue[leftEncoder] >
SensorValue[rightEncoder]) { motor[port3] = 50; motor[port2] = 63;
} if(SensorValue[leftEncoder] < SensorValue[rightEncoder]) {
motor[port3] = 63; motor[port2] = 50; } if(SensorValue[leftEncoder]
== SensorValue[rightEncoder]) { motor[port3] = 63; motor[port2] =
63; }}
task main(){ wait1Msec(2000); bMotorReflected[port2] = 1;
SensorValue[leftEncoder] = 0; SensorValue[rightEncoder] = 0;
while(SensorValue[sonarSensor] > 3) { moveStraight(); }}
9o. Modify this codeReplace the generic motor-forward commands
with a call to moveStraight();.
10. Download and Run.
10. Go to Compile and Download Program
Checkpoint
Your robot now runs straight until its Ultrasonic Sonar
Rangefinder detects an object within 3 inches, in its forward field
of view. You can change the stopping distance by changing the 3
inch “cutoff” point.
9n. Add this codeAdd these commands to reset the values of the
encoders to 0.
Ultrasonic Rangefinder Forward till Near (cont.)
-
ROBOTC
Ultrasonic Rangefinder • 11© Carnegie Mellon Robotics Academy /
For use with VEX® Robotics Systems
Sensing
This is the second sensor you have programmed to use a “cutoff”
value. The Encoder program ran a movement behavior until the
encoder count exceeded a certain value, and this new program runs a
movement behavior until the Ultrasonic Rangefinder distance goes
below a certain value.
These “cutoff” values – called thresholds – are important in
robot decision-making. Thresholds are values that set a cutoff
point in a range of values, so that even though there are many
possible values for encoder counts or distances, every one of them
will fall either below the threshold or above it. This division of
the many possible values into two distinct categories (above and
below the threshold) allows the robot to make a definite decision
about how to proceed for any value it may encounter.
Ultrasonic Rangefinder Forward till Near (cont.)
threshold
below above
-
ROBOTC
Ultrasonic Rangefinder • 12© Carnegie Mellon Robotics Academy /
For use with VEX® Robotics Systems
Sensing
Ultrasonic Rangefinder values above the threshold of 3 inches
made the program continue looping and moving forward. Values below
the threshold caused the robot to stop. The threshold sets the
point at which the robot’s behavior will change, because it marks
the point at which the (condition) in the while loop (or
if-statement) will change from true to false, or false to true, and
thus change which lines of code will run.
The robot is now set and ready to run for any object the
Ultrasonic Rangefinder might see… but what happens when it doesn’t
see anything at all? The sound waves have a limited range before
the echo is too soft for the sensor to pick up. In addition, some
materials or surfaces can actually deflect the sound waves away
from the sensor, preventing it from hearing the echo. What happens
then?
26272829303132
SensorValue[rightEncoder] = 0;
while(SensorValue[sonarSensor] > 3) { moveStraight(); }}
11. Making sure that your robot is turned on and plugged in,
open the ROBOTC Debugger and Devices windows. Run the program.
11a. Robot > DebuggerGo to Robot > Debugger to open the
Program Debug window.
11b. Robot > Debug Windows > DevicesOpen the Devices
window so that you can monitor the Ultrasonic Rangefinder
values.
Ultrasonic Rangefinder Forward till Near (cont.)
-
ROBOTC
Ultrasonic Rangefinder • 13© Carnegie Mellon Robotics Academy /
For use with VEX® Robotics Systems
Sensing
11c. Pick up the robotPick up the robot so that it doesn’t drive
away.
11d. Run the program
12. Point your robot’s sensor away from any nearby objects, or
toward a very soft object, like a sweater or cushion. Observe the
value that the sensor gives through the ROBOTC debugger.
12a. No SignalOrient your Ultrasonic Rangefinder so that it will
have difficulty getting an echo back from whatever is in front of
it. This may involve aiming it at something very far away or at
something soft (which absorbs sound).
12b. Observe sensor valueThe sonarSensor will show a value of -1
if it is unable to measure distance for any reason.
Ultrasonic Rangefinder Forward till Near (cont.)
-
ROBOTC
Ultrasonic Rangefinder • 14© Carnegie Mellon Robotics Academy /
For use with VEX® Robotics Systems
Sensing
13. Run your straight-till-near program in a place where the
robot will get a “-1” sonar reading.
13a. Position the robotPlace your robot so that it faces down a
very long hallway, or toward a sound-absorbing object.
13b. Run the robotSwitch the robot off and back on to run the
program.
13c. Observe robot behaviorThe robot stops immediately, as if it
were close to an object.
Checkpoint
Why would the robot act like it was close to an object when the
exact opposite was true? The SensorValue of an Ultrasonic
Rangefinder is returned as -1 when there is no object in range.
Consider how your robot is making its decisions:
while (SensorValue[sonarSensor] > 3) { moveStraight(); }
Ultrasonic Rangefinder Forward till Near (cont.)
-
ROBOTC
Ultrasonic Rangefinder • 15© Carnegie Mellon Robotics Academy /
For use with VEX® Robotics Systems
Sensing
The robot will move straight as long as the Ultrasonic
Rangefinder detects an object farther than 3 inches away.
Object at 6 inSensorValue[sonarSensor] is 6, therefore
SensorValue[sonarSensor] > 3 is true. The loop will continue
looping.
Object at 2 inSensorValue[sonarSensor] is 2, therefore
SensorValue[sonarSensor] > 3 is false. The loop will end. The
next portion of the program causes the robot to stop.
No objectSensorValue[sonarSensor] is -1, therefore
SensorValue[sonarSensor] > 3 is false. The loop will end. The
next portion of the program causes the robot to stop.
Ultrasonic Rangefinder Forward till Near (cont.)
-
ROBOTC
Ultrasonic Rangefinder • 16© Carnegie Mellon Robotics Academy /
For use with VEX® Robotics Systems
Sensing
14. Change the (condition) for continuing to move forward so
that the robot will run while the object is detected farther than 3
in OR too far to detect.
End of Lesson
The fine tuned code makes the robot move straight forward
whenever it is far away from an object, even if it is so far away
that it cannot detect it. You now have all of the tools you need to
score additional points during the autonomous period of the Mine
Removal Challenge. Now, use what you’ve learned to create your own
final competition program!
Ultrasonic Rangefinder Forward till Near (cont.)
AutoAutoAutoAuto
123456789
1011121314151617181920212223242526272829303132
const tSensors rightEncoder = (tSensors) in2;const tSensors
leftEncoder = (tSensors) in3; const tSensors sonarSensor =
(tSensors) in5;
void moveStraight(){ if(SensorValue[leftEncoder] >
SensorValue[rightEncoder]) { motor[port3] = 50; motor[port2] = 63;
} if(SensorValue[leftEncoder] < SensorValue[rightEncoder]) {
motor[port3] = 63; motor[port2] = 50; } if(SensorValue[leftEncoder]
== SensorValue[rightEncoder]) { motor[port3] = 63; motor[port2] =
63; }}
task main(){ wait1Msec(2000); bMotorReflected[port2] = 1;
SensorValue[leftEncoder] = 0; SensorValue[rightEncoder] = 0;
while(SensorValue[sonarSensor] > 3 ||
SensorValue[sonarSensor] < 0) { moveStraight(); }}
14a. Modify this codeAdd a second part to the condition, using
an OR (||) connector.