Robotics: Introduction to LEGO Programming


The Art of Downloading to your RCX using the USB IR Tower in Windows XP

The infrared communication system is used to download firmware and programs to the RCX brick from the USB IR tower. The advantages of wireless communication are obvious, but the sometimes baffling (mis)behaviors - failed downloads yielding a plethora of errors - can be frustrating. The following tips and tricks have been found to give the highest rate of successful downloads with our setup.

Plug an AC adapter into your RCX brick. You'll find a few of them scattered around the lab.

Find a computer nearby, and log in. Plug your USB tower into one of the USB ports on the front of the computer. You can find the USB ports by flipping up the front panel. Of the two ports that are there, you'll find that it's easier to get the plug into the port that's in back of the other. The USB plug should be oriented so that you see the word LEGO facing you, and the plug goes in at something of an angle.

The RCX will successfully program when 2 to 6 inches away from the IR tower. If the RCX is close to the IR tower (2-4 inches away), the RCX must be directly aimed at the tower, or else communication may fail. Five inches is the optimum distance to have between the RCX and the tower. The end of the brick with the dark window should be pointed directly at the front (dark side) of the USB IR tower. Elevate the bottom of the brick approximately 1 inch off the table relative to the bottom of the IR tower for best chance of success. (Put in on a book.) Do not obstruct the area between the RCX and the tower. Do not move either the tower or the RCX brick while downloading. Take a piece of paper, cardboard, or a thin book and place it over the tower and the brick, which helps block out the flourescent lights which interfere with the infrared transmissions. It helps if you can place the tent such that you can still see the LCD display on the brick.

We're going to download the leJOS firmware from the computer to the brick. This is something that you only need to do once for your brick, unless it sits without power (no batteries or anything) for a few minutes. But as long as your brick has batteries in it with some amount of power, or you change your batteries reasonably quickly, you won't need to download firmware more than once. That's good, because it takes a while.

Go to the "Start" menu and select "Run..." In the "Open:" text field enter "cmd" (sans quotation marks) and click OK. A command prompt will open. Make sure the brick is positioned and powered up (use the red On-Off button). At the command prompt type "firmdl" and press enter. Hopefully, the brick will beep and the firmware will begin downloading. Note: Do not multitask while downloading - we've found that using opening programs or browsing the web can cause the download to fail. If you do get an error message, reposition and power-cycle the RCX brick, and attempt "firmdl" once again. If you continue having trouble, try different distances and heights within the constraints suggested above. Ask for help: it can take a little practice to get this to work. You will be notified onscreen and the brick will beep when the download is complete. Notice that when the leJOS firmware is on the brick, the LCD displays the battery voltage. When the official MindStorm firmware is on the brick (i.e. just after you replace the batteries), just the little person and the number 1 is displayed.

Building Tippy

Next we'll build Tippy, a simple robot whose design is in Bagnall's book. Grab a fresh set of rechargeable batteries from CMC 307 (the student lounge), put them in your brick (just pop off the gray bottom), and then follow the instructions from pages 20-25 to build Tippy. Remember to keep an AC adapter plugged into your brick whenever you can to preserve battery power. The AC adapter will not charge your batteries, unfortunately.

Loading Your First Program

We have installed Crimson Editor on all Windows machines that you will be using. Crimson Editor is a free text editor with tabs and other features that is available for download at http://www.crimsoneditor.com. Feel free to install Notepad or Wordpad instead if you wish, but you will probably find that Crimson Editor has better features.

Open up Crimson Editor, enter the following program, and save it somewhere to your home folder. Call it HelloWorld.java

import josx.platform.rcx.*;
    
public class HelloWorld
{
    public static void main(String[] args) throws Exception
    {
        LCD.clear();
        TextLCD.print ("hello");
        Thread.sleep(2000);
        TextLCD.print ("world");
        Thread.sleep(2000);
    } 
}

You now need to navigate from within the DOS prompt that you opened up to the directory where you saved your HelloWorld.java file. If you are unfamiliar with DOS, click on and work through this quick help file.

To compile your code, you use the 'lejosc' command. So compile your program by typing

lejosc -source 1.2 HelloWorld.java

The -source 1.2 is necessary in order to convince leJOS to play nicely with Java 1.5. Once your code has successfully compiled, download your program to the RCX brick: make sure that your brick is positioned properly and on, and your tent is set up. Then type

lejos HelloWorld

This will download your compiled code to the brick. When the code has completed downloading, push the Run button, and you should see "hello world" display on the LCD screen.

More Programs to Try

  1. Enter the program ("class Tippy") that straddles pages 70-71 of Bagnall. Read the text following the program, and read through the program itself. Compile and run the code. See if you can make sense of what each line does.
  2. Read the section titled "Uploading More Than One Program" on pages 71-72. Type in the program in that section, and follow the instructions for installing both programs on the robot. Make sure you can swap back and forth between programs.
  3. Read the section titled "Reading Exceptions" on pages 117-118. Take one of the leJOS programs that you already have and modify it so that it will throw an exception at some point while running. Then see if you can use the tricks described in this section to successfully understand what exception has been thrown.
  4. Read the section titled "Threads" on pages 127-128. Enter in the program on page 127, then compile it and run it. Does it make sense what the code is doing? Ask questions.
  5. Do something creative based on the programs that you have seen so far. Can you choreograph a dancing and singing routine for Tippy?
  6. Read the section titled "Button" on pages 137-138. Enter in the program on page 138, then compile it and run it. Does it make sense what the code is doing? Ask questions.
  7. Modify one of your earlier Tippy programs to allow you to use the buttons on the brick to modify its behavior in some way. For example, you can modify one of your programs so that pressing Run reverses the robot's direction. Alternatively, you can modify your program so that while you hold down the View button, the robot reverses direction, but resumes its original direction again when you release the View button.

If you have time remaining... start on the next assignment!


Authored by Dave Musicant.