Download CSC 111 Lab 5 - Canisius College Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Robotics wikipedia , lookup

Ethics of artificial intelligence wikipedia , lookup

Cubix wikipedia , lookup

Transcript
CSC 111 Lab 11
April 12, 2007
OBJECTIVES:
1. Learn more about LEGO Mindstorms robots
2. Learn more about how to use the light sensor
3. Investigate clever algorithms to follow the light
Atten-HUP! Get ready, get set.... go!
PRELUDE:
Today we continue our investigation of robots and how to program them using Java.
Some extra time will be scheduled for Wehle Room 206, across the hall from the Computer Science
Department. The precise schedule when Dr. Meyer will be there will be posted on the website. You
can also visit the Advanced Lab, Wehle Room 208, where the tutors live. They have about 4 robots
that you can check out.
The points you earn for this lab are attached to the tasks you are asked to complete. There are no
free points for just being in lab today.
ACTIVITIES:
Part 1: Getting Started...
1.
Log on to the Wehle server using your own username. Copy the LAB11 folder to your H: drive and
also to the desktop.
2.
First, make a new project with your lab name, such as
Meyer Lab 11
Remember to make a Lejos RCX project.
3.
Now import all the programs from the LAB11/CODE folder on your desktop into this project.
1
4.
You will use Eclipse to edit and compile your robot code, but you will also have to use the command
window to download the programs into your robot, since that cannot be done inside Eclipse. To do
this, press the START button in the lower left, and select Run... When the dialog box pops up, type
cmd
and press RETURN.
5.
Navigate to the folder in your workspace. If you are working off the H: drive, then type the
following commands and press RETURN. Note: the C:\> is merely the prompt. Do not type it!
C:\>
C:\>
h:
cd \workspace\Meyer Lab 11
Part 2: Using Light to control the robot
1.
You may or may not have gotten this far last week, so we will repeat. We are going to play with the
light sensor, the little blue box. Compile and run GetLightValue.java. Download and run it. Point
the light sensor toward various light sources and look at the number on the LCD screen. This is a
percent value, between 0 and 100, although you won't see the % sign. Look in the code and see
where the light sensor is turned on and where Java tells the robot that it is a light-type sensor and that
the value is a percentage. Also try using the flashlight. Now experiment with the flashlight and, with
the robot on the floor, look to see what the number would be if the flashlight is on, and when it is
off.
2.
Also study GetLightAndBeep.java which is a variation of the simpler program. It causes the robot
to issue a beep sequence when it loses the light. Notice how we use a boolean variable called
seeingWhite to tell us of the transition from white to black. Otherwise, the robot would continue
beeping like crazy if it were in the dark.
3.
See Assignment #1 for a related task.
Part 3: Make the robot hunt for the light
1.
Having the robot follow the light sensor is a lot of fun. Some people use LEGO robots to follow
tracks on the floor. Others use light sensors to see various "colors." Though the robot can't
distinguish between red and green and blue, these colors are represented as shades of gray. You have
to experiment to find out what they are.
2.
We could elaborate on the Follow.java program (assignment problem 1) so that if the robot
loses the light, it will turn in search of the light, instead of sitting there dumbly, waiting. There are a
number of strategies, such as turning side to side, turning in a complete circle, etc. When the robot
gets a high light reading, it continues going forward.
3.
See Assignment #2 below.
2
Part 4: Artificial Intelligence
1.
Remembering things is a good idea. Your robot has a memory, too! It can remember which
direction it turned last when it successfully found the light. The next time it loses the light, it could
turn in this same direction again, hoping to find the light. If it can’t find the light, it could turn in the
opposite direction, and keep swaying back and forth. One strategy would be for it to sway in ever
wider circles, trying to find the light.
2.
Intelligence is defined in various ways, but some experts believe that a good measure of intelligence
is how many different ways of solving a problem the entity can utilize. Of course, it must utilize the
right solution method and judge that accurately. But just having one or two ways of dealing with a
situation is considered “brittle” and not very intelligent. Unfortunately, robots need to be
programmed, or somehow taught (which is itself a form of programming) all the different solution
methods. Right now, robots can’t come up with solution methods on their own. Can you think of all
the solution methods your light hunter might utilize to find the light? (This is a rhetorical question...
food for thought and private amusement.)
3.
See Assignment #3 for a related task.
ASSIGNMENTS: Try to finish these during lab today but you can come to the open lab for more time.
1.
Make a new program Follow.java. It is based on GetLightValue.java. Inside the while(true)
loop, when the value coming out of the light sensor is greater than a certain value, start both motors
so the robot goes forward. Otherwise, stop both motors. Compile, download and run it. The effect
should be that when you shine the flashlight into the light sensor, the robot goes forward. Otherwise
it stops.
Remember your comments! Put your name at the top, along with the course number, lab number
(11), date, and the problem number (1, 2, 3...)
2.
Make a new program FollowMe.java. It is based on your Follow.java. When the robot loses track
of the light, it turns a little and tries to find the light. If it does, it continues forward. The robot
should always turn a little in one direction, say 300 milliseconds right. In this way, the robot might
turn completely around before it sees the light.
You should still have a while(true) loop because your robot will go forever (or until you press the
ON/OFF button!) But inside the loop, check the light value. If it is below the threshold (close to 42),
then assume that the flashlight has been taken away. Start searching. Do this by going into another
while loop (which is nested inside the while(true) loop). This inner while loop keeps going while
the light level is below the threshold. You'll have to re-read the light sensor inside this loop. Also
turnRight(300) to cause it to turn to the right. Once you break out of this inner while loop, start both
motors in order to go forward again.
3
Here’s some psuedo-code that will work.
start both motors
while true
read the light sensor’s value
if the value is too low then it is dark
stop both motors
while the value is too low
turn right 300 milliseconds
read the light sensor’s value again
// now you are out of the loop, so
// you’ve found the light again!
turn on both motors again
// keep going forward now that you’ve found the light
3.
Modify the FollowMe.java program above, calling the new version Hunt.java. In this version, the
robot will remember in which direction it turned last time to find the light successfully. It then turns
in that same direction when it loses the light. If it doesn’t find the light, it turns in the other direction
but twice as far to compensate for the fact that it has to undo the first turn and then turn in the other
direction. If it finds light in that direction, fine. It starts going forward again and records the
direction. If it doesn’t find light, then it turns back in the other direction, but twice as far. It should
keep swaying back and forth, hunting for light, until it finds it.
Here’s a diagram for those of you who think more visually:
Many cleverer variants are possible, such as stopping after 5 sways and resting a while, or sway in
ever-widening arcs. There is no limit to the amount of intelligence that you could build into a robot.
Just think of how many different strategies you as a human have at your disposal when solving real
world problems. Then imagine how difficult it would be to build that kind of general intelligence
into a Java program. You don’t have to encode any of these cleverer possibilities, though if you do,
make them into separate variant programs called Hunt1.java, Hunt2.java, etc. and show them to
Dr. Meyer for possible extra credit.
4
PROGRAMS USED IN TODAY’S LAB
// GetLightValue.java
import josx.platform.rcx.*;
public class GetLightValue implements SensorConstants {
public static int BLACK = 50;
// percent, between 0 and 100
// experiment to find the setting
public static void main(String[] args) {
Sensor.S3.setTypeAndMode(SENSOR_TYPE_LIGHT, SENSOR_MODE_PCT);
Sensor.S3.activate();
while (true) {
int value = Sensor.S3.readValue();
LCD.showNumber(value);
}
}
}
//
//
//
//
//
GetLightAndBeep.java
The robot is looking at the light with its sensor and showing the percentage
value on its LCD. If it loses the light (goes from seeing white to seeing
black) it does its beep sequence “song.”
import josx.platform.rcx.*;
public class GetLightAndBeep implements SensorConstants {
public static int BLACK = 50;
// percent, between 0 and 100
// experiment to find the setting
public static void main(String[] args) {
Sensor.S3.setTypeAndMode(SENSOR_TYPE_LIGHT, SENSOR_MODE_PCT);
Sensor.S3.activate();
boolean seeingWhite = false;
while (true) {
int value = Sensor.S3.readValue();
LCD.showNumber(value);
if (value <= BLACK && seeingWhite) {
Sound.beepSequence();
seeingWhite = false;
// this prevents multiple beep sequences
// while we continue seeing black
}
if (value > BLACK)
seeingWhite = true;
}
}
}
5