Getting Ready to go with LeJos and an IDE NSF Workshop November 19-21, 2004 The University of Mississippi Pam Lawhead
Part 0 – Installing LeJos Taken from: http://lejos.sourceforge.net/tutorial/getstarted/firstbrick/win32.html
Step 1 You will need: LEGO MINDSTORMS RCX, IR Tower and the leJOS environment and The JavaTM 2 Platform, Standard Edition download it from http://java.sun.com/j2se/1.4/download.html and to consult the associated installation instructions (make sure to download the SDK, not the JRE!).
The leJOS environment may be downloaded from: Download section of http://www.lejos.org. Installation of the leJOS environment After you have downloaded the leJOS environment zip file, unzip it directly to your C: directory. Add the SDK's bin directory to your PATH environment variable. (Using Start/Control Panel/System/ [advanced]/[environment variables] In XP Start/Control Panel/Performance and Maintenance/System/[advanced]/ [environment variables] 4. Set the LEJOS_HOME environment variable to the directory you installed lejos into: set LEJOS_HOME=< your lejos directory > 5. Add leJOS's bin directory to your PATH environment variable: set PATH=%PATH%;%LEJOS_HOME%\bin 6. Add the leJOS classes to your CLASSPATH environment variable: set CLASSPATH=%CLASSPATH%;.;%LEJOS_HOME%/lib/classes.jar;%LEJOS_HOME%/lib/pcrcxcomm.jar 7. Set the RCXTTY environment variable to your 'tower' device: set RCXTTY=COM1 or set RCXTTY=USB
RCX Firmware leJOS-enable your RCX To enable your RCX for the execution of leJOS programs, you will have to replace the original LEGO® with the leJOS operating system. Place the IR sensor of the RCX in front of the IR Tower Turn on the RCX Open a command shell and change to the bin directory of your leJOS installation call firmdl.bat The progress of the download is displayed in the command shell and on the display of the RCX; the RCX will double beep when the download is complete and display the battery voltage OR…
Use the RXC Direct Interface Available from: http://lejos.sourceforge.net/utilities.html Download the file RCXTools1.5.zip Unpack "RCXTools_1_5.zip doubleclick"RCXDownload.bat" Set up preferences as in Get Started slide Download firmware using button GETTING STARTED
Setting up the IDE NSF Workshop November 19-21, 2004 The University of Mississippi Pam Lawhead
Setting up the IDE http://rcxtools.sourceforge.net/e_download.html Download RCXTools_1.5.zip Double Click "RCXDownload.bat" You will now participate in the dialog that follows
Here put the path to your JDK Here put the path to your lejos bin directory Here put the path to your JDK
Used courtesy of RCX Tools Install page Set Up Continues Used courtesy of RCX Tools Install page
Let us test it Create Program “Hello World” Save it as HelloWorld.java Run RCXDownload.bat Use the “Open” button to find the file Load the file Use the Compile button to compile
helloWorld.java import josx.platform.rcx.*; public class HelloWorld { public static void main (String[] aArg) throws Exception LCD.clear(); TextLCD.print ("hello"); Thread.sleep(2000); TextLCD.print ("world"); }
Download program to RCX Place robot in front of IR Tower Be careful to cover it if you are not alone Turn Robot on Click IDE Download Button Watch numbers on LCD if they are not increasing try again
Run the Program HELLO WORLD When the robot beeps twice Hit the RUN button You will see HELLO WORLD
LeJos, Mindstorm Links http://lejos.sourceforge.net/tutorial/index.html http://lejos.sourceforge.net/ http://dudley.wellesley.edu/~anderson/robots/lejosdocs/ The Lejos API http://www.informatik.fh_muenchen.de/~schieder/usinglejos http://dudley.wellesley.edu/~anderson/robots/lejos/ http://lvi.sourceforge.net/ Another way to interface with LeJos http://rcxtools.sourceforge.net/ My Favorite IDE http://graphics.stanford.edu/~kekoa/rcx/ How the internals of the RCX work http://www.informatik.fh_muenchen.de/~schieder/usinglejos/ “Instant Lejos” For a quick uncomplicated approach http://mindstorms.lego.com/eng/default.asp The Official Lego Mindstorm site http://www.devdaily.com/java/lejos/GettingStartedWithLejos/ Another Introductory Site
Using LeJos NSF Workshop November 19-21, 2004 The University of Mississippi Pam Lawhead
Button and LeJos All buttons my be re-programmed except “RUN” Controlled by a “button class” Polling or Using a Listener (Discuss) “listener” or directly using waitForPressAndRelease()
Button Example (Polling) import josx.platform.rcx.*; public class RunButton { public static void main(String[] args) throws InterruptedException {// Move forward Motor.A.forward(); Motor.B.forward(); // just run until RUN button is pressed again Button.RUN.waitForPressAndRelease(); }
Button Example (listener) -from Tutorial public class MyButtonListener implements ButtonListener { public void buttonPressed(Button b) // maybe do something here } // buttonPressed() public void buttonReleased(Button b) // buttonReleased() } // class MyButtonListener //used it looks like this Button.RUN.addButtonListener(myButtonListener);
Motors Three Motors A, B, C Controlled using: public static void forward() public static void backward() public static void reverseDirection() public static void flt() public static void stop()
Motor Examples Motor.A.forward(); Motor.B.forward(); Motor.B.flt(); //like an idle Motor.A.reverseDirection(); Motor.A.setPower(3); //possible values 1-7 (deceptive – does not cause the motor to go slow it just causes to use less energy which, perhaps, might slow it down)
Sensors There are three possible Sensor.S1, Sensor.S2, Sensor.S3 You must “set type and mode” to know the type of the sensor setTypeAndMode(int aType, int aMode) Parameters include: aType _ 0 = RAW, 1 = TOUCH, 2 = TEMP, 3 = LIGHT, 4 = ROT aMode _ 0x00 = RAW, 0x20 = BOOL, 0x40 = EDGE, 0x60 = PULSE, 0x80 = PERCENT, 0xA0 = DEGC, 0xC0 = DEGF, 0xE0 = ANGLE Also, mode can be OR'd with slope (0..31).
More Sensors Sensor.S1.activate(); //turns the sensor on Sensor.S1.passivate(); //turns the sensor off Same argument exists: Do I want listeners or do I poll? (see Barnes article for full discussion)
Sensor Examples Sensor.S2.readRawValue(); (must be used if aType = 0) Sensor.S2.readBooleanValue(); (used if aMode = BOOL e.g. touch sensor) Sensor.S2.readValue(); (used for light sensor percentage aMode = 3 aType – 0x80)
Sensor Code Example import josx.platform.rcx.*; class LCDShowLight { public static void main(String[] args) Sensor.S1.setTypeAndMode(3,0x80); Sensor.S1.activate(); while(true) LCD.showNumber(Sensor.S1.readValue()); }
Sound Uses RCX speaker – useful for debugging beep //one beep beepSequence() // a series of beeps going down buzz() playTone(int aFrequency, int aDuration) aFrequency – 31-2100 Hz //human sound range aDuration – 1 – 256 Centiseconds
Sound Example import josx.platform.rcx.*; class AudibleSounds { public static void main(String[] args) throws InterruptedException { for(int f = 440; f < 10000; f = 110*f/100) { LCD.showNumber(f); Sound.playTone(f, 50); Thread.sleep(500); } for(int f = 440; f > 20; f = 90*f/100) Thread.sleep(500); } } }
Part 4 Doing an Assignment The Light Sensor NSF Workshop November 19-21, 2004 The University of Mississippi Pam Lawhead
Assignment: The Light Sensor
The Sensors Normally, there are three types of sensors used programming the RCX, and they are: Touch Sensor Light Sensor Rotation Sensor This assignment will focus on the Light sensor.
Overview The purpose of this assignment is to give the student experience in Java event programming involving light sensors. It will give students more chances to practice using the RCX-Lejos LCD screen control, the If statement, and additional loop control skills.
The Lego Mindstorm with Light and Touch Sensor The sensor illuminates whatever is in front of it with an LED (light emitting diode) and records the amount of light it get back with a photo-sensitive device. This photo-sensitive device returns a signal that is amplified and digitized (transformed into a number). The range of values for the signal is 0(dark) to 100(bright). The value measured will vary with the surface it examines, the distance separating it, and the ambient light (environment)
The Lego Mindstorm with Light Sensor The LEGO Mindstorm’s light sensor has a range of about 3 inches. In Java, the sensor can measure the percentage of light received by the photo sensitive device. The RCX, displays a lower percentage of light on black colors and a higher percentage on white colors. The robot can follow a path by reacting to light percentages.
Problem Statement Write a small Java program to demonstrate to how the light sensor works. There are three different colors used on the background surface, (black, green, and white). Through the program, the LCD should show the percentage of light corresponding to different colors on the background. When the light sensor is on white, there should be a range of numbers, on green another, and finally on black another. ***Battery power and ambient light will affect how the machine behaves.*** Using data gained from above experiment, write a Java program to make an RCX, equipped with one light sensor and at least two wheels, navigate on flat surfaces and follow the black line.
Laboratory Preparation How does this program get input data or receive data in this lab? What is the difference from the previous labs? How many variables do you plan to use? And what data types are the variables? Write a small method called LeftAndRight that makes the RCX turn left or turn right based on some value that it receives? Write the program outline for the testing light sensor assignment. Write the method headers for this assignment. Once this is done write the full code. Bring this code to the lab.
Laboratory Robot Needed: RCX brick, 2 motors, 2 or 4 wheels, and 1 light sensor. Lejos classes and method needed: Motor: setPower (int aPower), forward(), backward(), stop() Sensor: activate(), passvate(), int readPercentage() (for light sensor) LCD: showNumber(int value) Other equipment: Track IR Tower
Solution Strategy Fix the light sensor at a 90 degree angle to surface. Allow no more than 3 inches from sensors to surface. The key to functionality is using the method readPercentage() of the class Sensor, Sensor.S1.readPercentage() returns an integer value representing the percentage of light received. The shape of the trace is ellipsoid. Set one Motor to stop and allow the other Motor to move forward, causing the RCX to turn left or right. When executing, watch direction RCX is moving. If the program allows the RCX to turn left, keep the left side of the RCX inside of the black line. Otherwise, place right side on the inside.
Testing Stand in different places when checking the color percentages. Start Robot on Green, on White, on Black What happens if you reverse directions? Could you adjust if you had to change directions? How?
Submissions Answers to Pre-Lab questions Brief description of expectations before lab Discussion of how time was spent in lab How long did lab take, including Pre-Lab preparation? A list of the problems encountered writing code for lab. A list of the problems had running and using lab. Names and roles of any collaborators Email program after demonstration
Assignment Array Sorter NSF Workshop November 19-21, 2004 The University of Mississippi Pam Lawhead
Overview The purpose of this assignment is to give the student experience using Java arrays, light sensors and arrays to sort the sensor reading stored in an array. The text used for this class (Lambert and Osborne) provides code for sorting a simple on dimensional array. This code may be used in this assignment. The only sensor required is a light sensor. You need to read Chapter 7 in your textbook very carefully to understand classes. Next, you need to answer the following questions BEFORE you come to lab.
Background The array data structure is provided by the Java programming language to handle those situations where many similar items need to be read, manipulated, stored, written or dealt with by the computer in some way. . The actual assignment has an extensive background section
Problem Statement You have been chosen as the field tester for the Our Colors Don’t Fade in the Sun Paint company. The sun shines unrelentingly here in summertime Mississippi and you have decided that it is entirely too hot for you to go out each day and take the readings from each color sample to determine if there has been any change. The goal is to keep the colors stored in the company’s primary computer from lightest to darkest. To do this you must write a program that allows a robot to travel across a field of sample paint colors, take a reading of each using the light sensor, and then store that value in an array of colors. Once the colors have been collected, you need the robot to return those values to the main computer where they will be stored and sorted for future use. Your company is a start-up company so it is not making many different colors of paint at this time. In this case, “summer paint sun test” there are only 5 colors that are being tested. As profits increase, the company will add additional colors to it’s a pallette but, for now, 5 will do.
Lab Preparation 1. What is the Java syntax for declaring an array? 2. What new leJos classes will you need to complete this assignment? 3. What are constructors used for? How are they defined? 4. What is the simplest way that you can sort a list of numbers? 5. What are the classes that have you already written that can be reused here? 6. Will you need two arrays to sort the numbers? How will you handle that?
Laboratory Robot needed: RCX brick, two motors, 4 (or 2) wheels and one light sensor LeJos classes and methods needed: Motor: setPower(int aPower), forward(), backward(), stop(),flt() Sensor: activate(), passvate(), int readPercentage()(for light sensor ) LCD: showNumber(int value), RCXComm
Solution Strategy (Note: You might need to write a little test program to determine the light sensor percentage for the different colors.) You need to be sure that the paint colors are distinct enough that the different colors can be recognized by the. You might need to put a black line between the colors. You need to first identify the different parts of your program - objects, attributes and methods. Decide what variables that you will need. What methods will be required to access these variables? Using the description of a class template found in your book on p. 160 define each of the four parts of your robot class template.
Solution Strategy (continued) Write each of the methods that you have identified in step one. How will you get your robot to return values to the PC? What class will you use? What methods in that class will be required? What are the arguments required? Hint: You might have a problem with the robot running too fast to get accurate readings of each of the colors, what are some solutions to that problem?
Testing 1. What is the first thing that you need to test? 2. What should the output from this program look like? 3. What is the input? 4. What would be an example of the program not working? 5. What would you do to fix the problem identified in question 4? 6. What are the expected inputs and outputs of each module? 7. What would it mean for a value to be out of bounds for each module? 8. What are two different ways that an array could be out of bounds?