Lab9.doc

466 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
466
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lab9.doc

  1. 1. CSC 111 Lab 9 March 22, 2007 Robot Day 1 OBJECTIVES: 1. Learn about LEGO Mindstorms robots 2. Learn how to compile, download and run Lejos programs 3. Learn what subprograms are and how they simplify programs 4. Marvel at how simple reactive machinery deludes us into thinking that computers are intelligent. 5. Have lots of fun! PRELUDE: For the next four lab periods, we will investigate life with robots. No, not big people-like robots (sometimes called androids or humaniform robots), but simple, non-threatening little car-like ‘bots’ that move around on their own, according to our programming. The robots we will use are called LEGO Mindstorms robots, using the RCX “brick,” which is a tiny Hitachi H8 computer embedded in a large Lego part. Motors and sensors attach to this brick to enable it to respond to the commands of the computer. LEGO first made these robot kits in 1997 and in 2006, introduced their successor, called the NXT robot. Canisius owns a number of each type of robot (approximately 30 RCX robots and 23 of the newer NXT robots) for use in programming classes. You can buy your own robot, of either type, either on-line or through some retail stores (Target stores sell NXT kits.) They run somewhere in the neighborhood of $150. You do not need to buy a robot for this class. Everything you need is supplied by the college. However, we cannot put robots in the computer lab nor can we allow you take them home, even temporarily, for fear of breakage or loss. These four labs of CSC111 (9 through 12) will use the RCX robots, programming them using Java, to reinforce the programming you’ve learned already and to inject an element of pure fun. You will be given time during the Thursday labs to complete most of the exercises. If you would like a little more hands-on time with the robots, a few hours in the late afternoon or early evening will be announced when Dr. Meyer will be available in Wehle Room 301 (the official robot lab.) You will get a number of points (50% of the lab’s total) just for being in lab today. The other 50% of the points will come from completing three small programming tasks. Don’t get all uptight if you can’t finish all the tasks. You can save your program and work on it during the extra hours if you want to try to get all the points. 1
  2. 2. ACTIVITIES: Part 1: Getting Started... 1. Log on to the Wehle server using your own username. Copy the LAB9 folder to your H: drive and also to the desktop. 2. First, make a new project with your lab name, such as Meyer Lab 9 However, it is not a Java project this time! It is a Lejos project. So pull down the File menu and select New... Project. When the next dialog comes up, click on the plus next to Lejos and then click on leJOS RCX project: 3. Now import all the programs from the LAB9/CODE folder on your desktop into this project. 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. 2
  3. 3. 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:> h: C:> cd workspaceMeyer Lab 9 Part 2: Your first robot program... 1. Look at the program GoForward.java. Dr. Meyer will explain some of its parts. import josx.platform.rcx.*; public class GoForward implements SensorConstants { public static void main(String[] args) { Sensor.S2.setTypeAndMode(SENSOR_TYPE_TOUCH, SENSOR_MODE_BOOL); Motor.A.forward(); Motor.C.forward(); while( Sensor.S2.readValue() != 1 ) ; } } Notice that these robots are controlled by Java programs. The above program is 100% Java. However, it uses a package of objects specifically programmed for the RCX robots. These objects are in a package called josx.platform.rcx, which is what we import. The objects we use here are Sensor.S2 and Motor.A and Motor.C. Notice that the names of these objects follow an unconventional pattern. Sensor and Motor are the classes or types, and the individual “things” are Sensor.S2 and the two motors. One other thing that may seem odd is the implements SensorConstants which you must add to your class declaration. This is merely part of the “magical incantations” that you need to get Java to talk to your robots. Remember: Lejos is really just Java with these Robot classes. 2. If your program doesn’t show any red squiggles in Eclipse then it compiled okay. You could press CONTROL-S to save and compile, just to be sure. Now go to your command window, which we will just call cmd from here on out. Type the following command and press RETURN: C:> dir “Dir” is short for “directory,” which is the ancient MS-DOS command to list all the files in your current directory. You will also see other information, such as the last date and time the file was modified, how big it is in bytes, and so forth. Hopefully, you will see GoForward.class in the listing, which is the compiled Lejos program. If not, please raise your hand. 3
  4. 4. 3. Though Lejos programs can be compiled inside Eclipse, they have to be downloaded through cmd. So hook up your robot’s infrared link to the USB port as per Dr. Meyer’s directions now. You may need to cover your robot and the tower with a cardboard box because the infrared rays from other computers may interfere. Now type the following command in cmd and press RETURN. C:> lejos GoForward 4. You will see a percentage number every so often, which indicates how much of the program has been downloaded. It takes a little under 2 minutes to download a Lejos program into your robot. 5. Once it double beeps and goes to 100%, your program has been successfully transferred into the robot and you can run it by pressing the green RUN button. To stop the robot, press the red STOP button. 6. See Assignment #1 for a small change to GoForward. Part 3: Calibration is everything... 1. Now look at TurnAround.java, which is a much more complicated program. Dr. Meyer will walk you through the code. Also notice the use of subprograms, which are named snippets of code, kind of like an envelope with Java statements in it. You can call them as though they are primitive actions, and you can reuse them over and over. Some of them use parameters. (We have already seen subprograms in connection with classes. There we called them methods. Sometimes they are called functions. In Chapter 7 of the textbook, coming up next, we will learn all about subprograms.) 2. Download this program into your robot and run it. 3. If your robot doesn't turn exactly 90 degrees to the right, it must be because 600 milliseconds isn't the right amount of time. Adjust this number in the turnRight() subprogram, recompile and rerun it. Don't just guess what the correct value will be; use some logic! For instance, if your robot only turned 75 degrees, you need to pause more than 600 milliseconds. If it turned 150 degrees, you need to pause less, maybe 360 milliseconds. You could work up a proportion formula and solve it: 600 x ----- = ---- 150 90 4. See Assignment #2 for an exercise based on this program. 4
  5. 5. Part 4: A robot with the brains of a cockroach... 1. Investigate the program Avoid.java, which is quite a lot more complicated than TurnAround.java. It is actually almost intelligent because the robot determines which bumper was touched, the right or left, and it backs up and goes the other direction. It is quite fun to watch and will make non-programmers gasp as if it were a smart machine! First, compile, download and run the program and observe its behavior. 2. Now study the code. Figure out which statement tells which bumper was touched. Also notice how Math.random() is used to make it turn a random amount to the right or left. 3. See Assignment #3 for an exercise based on this program. 5
  6. 6. ASSIGNMENTS: Try to do these during lab today 1. Modify GoForward.java so that either bumper can be touched to make the robot stop. See if you can figure out what needs to be added. Name your new program GoForward2.java. Add your name and the course # to the comments. 2. Modify TurnAround.java, renaming it AboutFace.java. This is what the new code will do: execute the following forever go forward and pause for 0.5 seconds (500 millis) stop completely swivel around so it faces the direction it came from Notice that we say "go forward and pause for X seconds". What is really happening is that we turn on the motors, which keep going indefinitely, and then we tell the JVM to wait for a while. All this time the motors are going, so the robot is moving. But the program itself is actually pausing. It is kind of like driving your car on autopilot (a bad thing to do, but hey, we all do it from time to time!) To completely swivel around, write a new subprogram called swivel. Follow the model of forwardBoth(). However, turn motor A on forward and turn motor C on backward: Motor.A.forward(); Motor.C.backward(); pause(3000); Motor.A.stop(); Motor.C.stop(); This will cause it turn quickly. Get your program to work, put your names in a comment, and add it to the turnin folder. (Fun thing to try... In which direction does the robot swivel? Can you get it to swivel in the other direction? Maybe that could be a parameter to the swivel subprogram.) 3. Write a silly robot program that will cause the robot to move forward but in a random direction and for a random time. This is sometimes called "the drunkard's walk" because the robot appears to be moving aimless around. (We don't mean to offend anybody by this name, but it is used in the literature (http://en.wikipedia.org/wiki/Drunkard's_walk) to mean a random walk.) But let’s call it by another scientific name: Brownian, named after random Brownian motion. Write a program that goes forever, or you could have it stumble a fixed number of times with a for loop. Inside the loop, have it go forward a random amount of time. Remember, you turn on the motors and pause for a random amount of time. Use the following: forwardBoth(); pause((int)(Math.random() * 2500); stopBoth(); Then have it turn right or left a random turn. The random turn code is given in Avoid.java. The only thing we need to determine is if it turns right or left. We'll toss a computer coin to find that out. Here's how: 6
  7. 7. if ((int)(Math.random() * 2) == 1) // left else // right Let your robot run for a while and see what it crashes into! Name the program Brownian.java, put your name in comments and add it to the turnin folder. PROGRAMS USED IN TODAY’S LAB import josx.platform.rcx.*; public class GoForward implements SensorConstants { public static void main(String[] args) { Sensor.S2.setTypeAndMode(SENSOR_TYPE_TOUCH, SENSOR_MODE_BOOL); Motor.A.forward(); Motor.C.forward(); while( Sensor.S2.readValue() != 1 ) ; } } 7
  8. 8. import josx.platform.rcx.*; public class TurnAround implements SensorConstants { public static void main(String[] args) { Sensor.S2.setTypeAndMode(SENSOR_TYPE_TOUCH, SENSOR_MODE_BOOL); while (true) { forwardBoth(); while( Sensor.S2.readValue() != 1 ) ; stopBoth(); reverseBoth(); pause(1000); stopBoth(); turnRight(); } } public static void forwardBoth() { Motor.A.forward(); Motor.C.forward(); } public static void reverseBoth() { Motor.A.backward(); Motor.C.backward(); } public static void stopBoth() { Motor.A.stop(); Motor.C.stop(); } public static void turnRight() { Motor.C.stop(); Motor.A.forward(); pause(600); Motor.A.stop(); } public static void pause(int millis) { try { Thread.sleep(millis); } catch (InterruptedException e) {} } } 8
  9. 9. import josx.platform.rcx.*; import java.util.*; public class Avoid implements SensorConstants { public static void main(String[] args) { Sensor.S1.setTypeAndMode(SENSOR_TYPE_TOUCH, SENSOR_MODE_BOOL); Sensor.S2.setTypeAndMode(SENSOR_TYPE_TOUCH, SENSOR_MODE_BOOL); while (true) { int s1, s2; forwardBoth(); while ((s1 = Sensor.S1.readValue()) != 1 && (s2 = Sensor.S2.readValue()) != 1) ; stopBoth(); reverseBoth(); pause(1000); stopBoth(); int k = (int)(Math.random() * 2500); if (s1 == 1) turnRight(k); else turnLeft (k); } } public static void forwardBoth() { Motor.A.forward(); Motor.C.forward(); } public static void reverseBoth() { Motor.A.backward(); Motor.C.backward(); } public static void stopBoth() { Motor.A.stop(); Motor.C.stop(); } public static void turnLeft(int milliseconds) { Motor.A.stop(); Motor.C.forward(); pause(milliseconds); Motor.C.stop(); } public static void turnRight(int milliseconds) { Motor.C.stop(); Motor.A.forward(); pause(milliseconds); Motor.A.stop(); } public static void pause(int millis) { try { Thread.sleep(millis); } catch (InterruptedException e) {} } } 9
  10. 10. 10

×