Getting Ready to go with LeJos and an IDE NSF Workshop November 19-21, 2004 The University of...

Preview:

Citation preview

Getting Ready to go with LeJos and an IDE

NSF WorkshopNovember 19-21, 2004The University of MississippiPam Lawhead

Part 0 – Installing LeJos

Taken from:http://lejos.sourceforge.net/tutorial/getstarted/firstbrick/win32.html

You will need:LEGO MINDSTORMS RCX, IR Tower and the leJOS environmentand

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!).

Step 1

The leJOS environment may be downloaded from: Download section of http://www.lejos.org.

Installation of the leJOS environment

1. After you have downloaded the leJOS environment zip file, unzip it directly to your C: directory.

2. Add the SDK's bin directory to your PATH environment variable. (Using Start/Control Panel/System/ [advanced]/[environment variables]

3. 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

leJOS-enable your RCXTo enable your RCX for the execution of leJOS programs, you will have to replace the original LEGO® with the leJOS operating system.

1. Place the IR sensor of the RCX in front of the IR Tower 2. Turn on the RCX 3. Open a command shell and change to the bin directory of your

leJOS installation call firmdl.bat

4. The progress of the download is displayed in the command shelland on the display of the RCX; the RCX will double beep when the download is completeand display the battery voltage

OR…

RCX Firmware

Use the RXC Direct Interface

Available from: http://lejos.sourceforge.net/utilities.html

1. Download the file RCXTools1.5.zip2. Unpack "RCXTools_1_5.zip3. doubleclick"RCXDownload.bat"

4. Set up preferences as in Get Started slide5. Download firmware using button

GETTING STARTED

Setting up the IDE

NSF Workshop November 19-21, 2004The University of Mississippi Pam Lawhead

Setting up the IDE

http://rcxtools.sourceforge.net/e_download.htmlDownload RCXTools_1.5.zipDouble Click "RCXDownload.bat"

You will now participate in the dialog that follows

Here put the path to your lejos

bin directoryHere put the

path to your JDK

Set Up Continues

Used courtesy of RCX Tools Install page

Let us test it

Create Program “Hello World”Save it as HelloWorld.javaRun RCXDownload.batUse the “Open” button to find the fileLoad the fileUse the Compile button to compile

helloWorld.javaimport 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"); Thread.sleep(2000); }}

Download program to RCX

Place robot in front of IR TowerBe careful to cover it if you are not aloneTurn Robot onClick IDE Download ButtonWatch numbers on LCD if they are not increasing try again

Run the Program

When the robot beeps twiceHit the RUN buttonYou will see

HELLO

WORLD

LeJos, Mindstorm Linkshttp://lejos.sourceforge.net/tutorial/index.htmlhttp://lejos.sourceforge.net/http://dudley.wellesley.edu/~anderson/robots/lejosdocs/ The Lejos APIhttp://www.informatik.fh_muenchen.de/~schieder/usinglejoshttp://dudley.wellesley.edu/~anderson/robots/lejos/http://lvi.sourceforge.net/ Another way to interface with LeJoshttp://rcxtools.sourceforge.net/ My Favorite IDEhttp://graphics.stanford.edu/~kekoa/rcx/ How the internals of the RCX workhttp://www.informatik.fh_muenchen.de/~schieder/usinglejos/

“Instant Lejos” For a quick uncomplicated approachhttp://mindstorms.lego.com/eng/default.asp The Official Lego Mindstorm sitehttp://www.devdaily.com/java/lejos/GettingStartedWithLejos/ Another Introductory Site

Using LeJos

NSF Workshop November 19-21, 2004The 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) { // maybe do something here } // buttonReleased() } // class MyButtonListener

//used it looks like this

Button.RUN.addButtonListener(myButtonListener);

Motors

Three Motors A, B, CControlled 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 idleMotor.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.S3You 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 = ROTaMode _ 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 = 3aType – 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 rangeaDuration – 1 – 256 Centiseconds

Sound Exampleimport 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) { LCD.showNumber(f); Sound.playTone(f, 50); Thread.sleep(500); } } }

Part 4Doing an Assignment

The Light Sensor

NSF Workshop November 19-21, 2004The 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 SensorThe 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 StatementWrite 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.

LaboratoryRobot 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:TrackIR Tower

Solution StrategyFix 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 BlackWhat happens if you reverse directions?Could you adjust if you had to change directions? How?

Submissions

Answers to Pre-Lab questionsBrief description of expectations before labDiscussion of how time was spent in labHow 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 collaboratorsEmail program after demonstration

Assignment Array Sorter

NSF Workshop November 19-21, 2004The University of Mississippi Pam Lawhead

OverviewThe 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 programminglanguage 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 StatementYou have been chosen as the field tester for the Our Colors Don’t Fadein the Sun Paint company. The sun shines unrelentingly here insummertime Mississippi and you have decided that it is entirely toohot for you to go out each day and take the readings from each colorsample to determine if there has been any change. The goal is to keepthe colors stored in the company’s primary computer from lightest to darkest. To do this you must write a program that allows a robot totravel across a field of sample paint colors, take a reading of each usingthe light sensor, and then store that value in an array of colors. Oncethe colors have been collected, you need the robot to return those values to the main computer where they will be stored and sorted for futureuse. 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?

Recommended