Tuesday, November 27, 2007

Lego Mindstorms Robotics On OS X: Hello World

Here's the dead-stupid overview of how to get your Mindstorms kit running on OS X.

Getting Stuff

First, build a robot with wheels powered by motors on B and C, and with the ultrasonic sensor plugged into sensor port 4.

Then download and install NXTBrowser.

Next download and install NBC/NXC. Give the NBC compiler exec permissions:

chmod a+x nbc


Then copy the code near the middle of this nice walkthrough, where it says "NXC Sample Code." Put it in a file in the same directory as your NXC install, and call it wander.nxc. Compile it into something the Mindstorms brain can handle with this command:

nbc -O=wander.rxe wander.nxc


Turn on your Mindstorms brain; go to Bluetooth and choose On. You should see the words "Turn On?". When you do, hit the orange button. Boom.

Now go to your Bluetooth settings in System Preferences. A "wizard" will walk you through the steps of connecting to the Mindstorms brain. You may have to input a simple passkey on the Mindstorms brain. You do this just by using the arrow keys and orange button. (It may be possible to skip this step; I don't know.)

Now open NXTBrowser.

Choose "Software." There's an "Upload" button at the bottom of the UI; click it and you'll get a file browser. Choose the NBC file you created, wander.rxe, and NXTBrowser will put it on your Mindstorms brain. Go to the Mindstorms brain and choose "Software Files" - you'll see "wander", which is the program you just added. Hit Run and place robot on floor; you're done!


I'm using NXC; it's a C derivative. There's a Java derivative which looks pretty great, but for me personally the frustration of attempting to build it on OS X was too much. A few people have been successful with it, however. There's also a Ruby library, ruby-nxt; although I'm big into Ruby, and I definitely enjoy it more than C, the Ruby version is based on a remote-control model, as opposed to actually putting a program in the robot's brain. If you're building arm robots I would say go with ruby-nxt instead, but for robots that run around on the kitchen floor, NXC is my weapon of choice.