Ultrasonic Rangefinder Forward till Near

Transcription

Ultrasonic Rangefinder Forward till Near
ROBOTC
Sensing
Ultrasonic Rangefinder
Forward till Near
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.
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.
© Carnegie Mellon Robotics Academy / For use with VEX® Robotics Systems
Ultrasonic Rangefinder • 1
ROBOTC
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 sensor
Attach the Ultrasonic
Rangefinder to the front of
the Squarebot 3.0 arm.
© Carnegie Mellon Robotics Academy / For use with VEX® Robotics Systems
Ultrasonic Rangefinder • 2
ROBOTC
Sensing
Ultrasonic Rangefinder Forward till Near
(cont.)
1b. Connect the OUTPUT cable
Connect the OUTPUT cable of
the Ultrasonic Rangefinder to
INTERRUPT port 1.
1c. Connect the INPUT cable
Connect the INPUT cable of
the Ultrasonic Rangefinder to
ANALOG / DIGITAL port 5.
2. Create a new program.
2. File > New
Select File > New to
create a new program.
3. Save the new program as “ForwardNear”.
3a. File > Save As...
Select File > Save As...
to save your program
under a new name.
3b. Renate program
Give this program the name ForwardNear.
3c. Save
Click Save.
4. Use the Motors and Sensors Setup Menu to configure the Ultrasonic Rangefinder on
Analog Digital Port 5, and Interrupt 1.
4a. Robot > Motors and Sensors Setup
Select Robot > Motors and Sensors Setup to
open up the configuration menu.
© Carnegie Mellon Robotics Academy / For use with VEX® Robotics Systems
Ultrasonic Rangefinder • 3
ROBOTC
Sensing
Ultrasonic Rangefinder Forward till Near
(cont.)
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 OK
Click 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.
© Carnegie Mellon Robotics Academy / For use with VEX® Robotics Systems
Ultrasonic Rangefinder • 4
ROBOTC
Sensing
Ultrasonic Rangefinder Forward till Near
(cont.)
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.
5. Add task main and base code.
Auto
Auto
1
2
3
4
5
const tSensors sonarSensor = (tSensors) in5;
task main()
{
wait1Msec(2000);
bMotorReflected[port2] = 1;
}
5. Add this code
Add task main, an initial delay, and
the motor reflection command before
proceeding with the rest of the program.
6. Add the basic structure of a move-until behavior.
Auto
Auto
1
2
3
4
5
6
7
8
9
10
11
const tSensors sonarSensor = (tSensors) in5;
task main()
{
wait1Msec(2000);
bMotorReflected[port2] = 1;
while()
{
motor[port3] = 63;
motor[port2] = 63;
}
}
© Carnegie Mellon Robotics Academy / For use with VEX® Robotics Systems
6. Add this code
The basic structure of the behavior
is a while loop containing movingforward commands. The (condition)
will determine how long this loop
lasts, and is left blank for now.
Ultrasonic Rangefinder • 5
ROBOTC
Sensing
Ultrasonic Rangefinder Forward till Near
(cont.)
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.
Auto
Auto
1
2
3
4
5
6
7
8
9
10
11
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 code
The (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.
© Carnegie Mellon Robotics Academy / For use with VEX® Robotics Systems
Ultrasonic Rangefinder • 6
ROBOTC
Sensing
Ultrasonic Rangefinder Forward till Near
(cont.)
9. Upgrade this behavior to go straight while there is no obstacle nearby,
rather than just run forward.
9a. Robot > Motors and Sensors Setup
Select Robot > Motors and Sensors Setup
to open up the configuration menu.
9b. “Name” the sensors
Assign the name “leftEncoder” to
the sensor in port “in3” (A/D input
3). Name the “in2” (A/D input 2)
sensor “rightEncoder”.
9c. Set the “Type” of both sensors
Identify the sensor attached to both
in2 and in3 as “Rotation” sensors
(Encoders sense rotation).
9d. Press OK
Confirm the new
sensor configuration.
9e. File > Save
Select File > Save to
save your program.
9f. File > Open and Compile
© Carnegie Mellon Robotics Academy / For use with VEX® Robotics Systems
Ultrasonic Rangefinder • 7
ROBOTC
Sensing
Ultrasonic Rangefinder Forward till Near
(cont.)
9g. Open MineArmStraight
Instead of re-entering the movingstraight code, we’ll take a copy of
it from the Encoders program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 code
Find the void
moveStraight()
function and highlight it.
9i. Copy this code
Select Edit > Copy to put the
highlighted code on the clipboard.
9j. Select File > Open and Compile
© Carnegie Mellon Robotics Academy / For use with VEX® Robotics Systems
Ultrasonic Rangefinder • 8
ROBOTC
Sensing
Ultrasonic Rangefinder Forward till Near
(cont.)
9k. Open ForwardNear
Return to the Forward
Till Near Ultrasonic
Rangefinder program.
9l. Place cursor here
Place your cursor between task
main and the AUTO code.
9m. Paste
The moving straight function is
now declared, and available
for use in this program.
© Carnegie Mellon Robotics Academy / For use with VEX® Robotics Systems
Ultrasonic Rangefinder • 9
ROBOTC
Sensing
Ultrasonic Rangefinder Forward till Near
Auto
Auto
Auto
Auto
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
(cont.)
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();
}
}
9n. Add this code
Add these commands to
reset the values of the
encoders to 0.
9o. Modify this code
Replace the generic motorforward 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.
© Carnegie Mellon Robotics Academy / For use with VEX® Robotics Systems
Ultrasonic Rangefinder • 10
ROBOTC
Sensing
Ultrasonic Rangefinder Forward till Near
(cont.)
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.
threshold
below
© Carnegie Mellon Robotics Academy / For use with VEX® Robotics Systems
above
Ultrasonic Rangefinder • 11
ROBOTC
Sensing
Ultrasonic Rangefinder Forward till Near
(cont.)
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.
26
27
28
29
30
31
32
SensorValue[rightEncoder] = 0;
while(SensorValue[sonarSensor] > 3)
{
moveStraight();
}
}
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?
11. Making sure that your robot is turned on and plugged in, open the ROBOTC
Debugger and Devices windows. Run the program.
11a. Robot > Debugger
Go to Robot > Debugger to open
the Program Debug window.
11b. Robot > Debug Windows > Devices
Open the Devices window so that you can
monitor the Ultrasonic Rangefinder values.
© Carnegie Mellon Robotics Academy / For use with VEX® Robotics Systems
Ultrasonic Rangefinder • 12
ROBOTC
Sensing
Ultrasonic Rangefinder Forward till Near
(cont.)
11c. Pick up the robot
Pick 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 Signal
Orient 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 value
The sonarSensor will show
a value of -1 if it is unable
to measure distance for
any reason.
© Carnegie Mellon Robotics Academy / For use with VEX® Robotics Systems
Ultrasonic Rangefinder • 13
ROBOTC
Sensing
Ultrasonic Rangefinder Forward till Near
(cont.)
13. Run your straight-till-near program in a place where the robot will get a “-1” sonar reading.
13a. Position the robot
Place your robot so that it faces
down a very long hallway, or
toward a sound-absorbing object.
13b. Run the robot
Switch the robot off and back
on to run the program.
13c. Observe robot behavior
The 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();
}
© Carnegie Mellon Robotics Academy / For use with VEX® Robotics Systems
Ultrasonic Rangefinder • 14
ROBOTC
Sensing
Ultrasonic Rangefinder Forward till Near
(cont.)
The robot will move straight as long as the Ultrasonic Rangefinder detects an
object farther than 3 inches away.
Object at 6 in
SensorValue[sonarSensor] is 6, therefore
SensorValue[sonarSensor] > 3 is true.
The loop will continue looping.
Object at 2 in
SensorValue[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 object
SensorValue[sonarSensor] is -1, therefore
SensorValue[sonarSensor] > 3 is false.
The loop will end. The next portion of the program
causes the robot to stop.
© Carnegie Mellon Robotics Academy / For use with VEX® Robotics Systems
Ultrasonic Rangefinder • 15
ROBOTC
Sensing
Ultrasonic Rangefinder Forward till Near
(cont.)
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.
Auto
Auto
Auto
Auto
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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 code
Add a second part to the condition,
using an OR (||) connector.
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!
© Carnegie Mellon Robotics Academy / For use with VEX® Robotics Systems
Ultrasonic Rangefinder • 16