The intensity of the term has lulled, marginally, so last night I completed my migration from PC to Mac. I installed LeJOS NXJ, a Java programming environment for the LEGO Mindstorms NXT. It lets us program LEGO robots in Java.
LeJOS is an open source project hosted in the sourceforge repository. It was forked from the TinyVM project which implemented a Java VM for the LEGO Mindstorms RCX system.
By installing I mean adding these to my Macbook Pro and successfully installing the first item on the list on my LEGO Mindstorms NXT brick:
- Replacement firmware for the NXT that includes a Java Virtual Machine
- A library of Java classes that implement the leJOS NXJ API
- A linker for linking user Java classes with classes.jar to form a binary file that can be uploaded and run on the NXT
- PC tools for flashing the firmware, uploading programs, debugging, etc
- A PC API for writing PC programs that communicate with leJOS NXJ programs using Java streams over Bluetooth, USB, or the LEGO Communications Protocol (LCP)
I’ve blogged about my introduction to LeJOS and Mindstorms and have successfully used LeJOS on Windows Vista and Windows 7 as well as Ubuntu. Configuring the environment on my Mac was, as usual, easiest of the three.
Recall that I am using a mid-2010 MacBook Pro running Mac OS X 10.6. LeJOS NXJ requires some of the drivers from the original LEGO brand software that ships with the robot. I tried using the CD that came with my Mindstorms (purchased in May 2008) to install the original LEGO system, but it didn’t install. There were no error messages but the app never appeared in my apps folder. This is how I solved the problem:
1. A little probing on the LEGO website and I found a Snow Leopard fix which I used to complete the installation. I also found and installed the latest LEGO firmware update (version 1.29).
2. Because of the difficulty I’ve had with drivers on both Windows and Ubuntu, I also installed the Fantom Driver on top of the MindStorms app.
All of these are at: http://mindstorms.lego.com/en-us/support/files/Driver.aspx#Driver
3. Download and unzip LeJOS NXJ: http://sourceforge.net/projects/lejos/files/lejos-NXJ/
4. Set the permissions in the downloaded folder:chmod -R 755 /pathto/lejos_nxj/
5. Edit etc/launchd.conf to include (I already had this from a former install):setenv JAVA_HOME /Library/Java/Home
6. Edit ~/.bash_profile to includeLEJOS_HOME=/pathto/lejos_nxj
NXJ_HOME="$LEJOS_HOME"
DYLD_LIBRARY_PATH=/pathto/lejos_nxj/bin
export LEJOS_HOME NXJ_HOME DYLD_LIBRARY_PATH
PATH="$LEJOS_HOME/bin:$PATH"
export PATH
This is where I got stuck. No matter how I adjusted these environment variables, every time I connected the NXT brick by USB and tried to flash the LeJOS firmware onto it, I received some permutation of
Bad news: An error has occurred lejos.pc.comm.NXTCommException: Cannot load a comm driver!
It’s always something with LeJOS and it’s always something I’ve overlooked. In this case, my Java defaults to 64 bit and LeJOS needs to use 32 bit. I recently upgraded my RAM to 8GB and in order for the Win 7 virtual drive in my Virtual Box to use 4 GB of memory I have to boot Snow Leopard in 64 bit kernel mode (by holding the 6 and the 4 down during startup). It is necessary to:
7. Manually add the -d32 flag to the LEJOS scripts. Setting the Java version in system preferences won’t do the trick. Open all the non-binary files (the ones that don’t end with .bin) in the /pathto/lejos_nxj/bin/ folder and edit all calls to “java” to include -d32 to make them run in 32-bit mode.
Worked like a charm.
I was motivated by more than the opportune free evening. I am considering enrolling in September in a directed studies course at UBC in which students make a proposal to a faculty member to investigate a topic that goes beyond that which is covered in existing courses. I’ve enjoyed working (and playing!) with Mindstorms (what red-blooded Canadian wouldn’t!) and last term I enjoyed learning Haskell and Prolog. This term’s course in internet computing is beginning to stand out as a favourite. I’m also itching to learn Erlang. Somewhere in this there is some interesting work…