Lego Mindstorms NXT and Mac 49633
Wednesday September 27th I received my Lego Mindstorms NXT kit, which I ordered from the Lego Shop. I got it pretty quick (3 days)! As far as I understood this is the first Mindstorms kit which supports the Mac. The bundled programming software runs on the Mac and the NXT brick (the micro-controller) supports the Mac perfectly. It speaks bluetooth as well as USB.

So far I’ve built the TriBot, Spike and this fellow.
Being a programmer though, you outgrow the bundled software quickly. So what are the alternatives? Currently only one: NBC. NBC stands for Nxt Byte Codes and is so far the only text-based programming language for the Mac. Using it is pretty simple (if you read the docs carefully).
First I put the code from this tutorial in TextMate and save it as helloworld.nbc.
Then you run nbc helloworld.nbc -O=helloworld.rxe and it generates a helloworld.rxe file. This you dowload to your NXT brick using the bundled NXT software and off you go. Pretty brilliant!
NBC: How to read Mindstorms touch sensor 999
Here a small program to read the touch sensor value. Be sure to connect the touch sensor to port 1.
#include "NXTDefs.h"
dseg segment
dtArgs TDrawText
dseg ends
thread main
dseg segment
sensorValue byte
sensorPort byte
dseg ends
setin IN_TYPE_SWITCH, IN_1, Type
setin IN_MODE_RAW, IN_1, InputMode
Endless:
getin sensorValue, IN_1, RawValue
set dtArgs.Location.X, 1
set dtArgs.Location.Y, 1
// Clear entire screen
set dtArgs.Options, 1
// Convert from number to string
numtostr dtArgs.Text, sensorValue
syscall DrawText, dtArgs
jmp Endless
endtGetting started with NBC on Apple OSX 39040
NBC means NeXT Byte Code and is an assembler like programming language for the LEGO Mindstorms NXT programmable brick. It uses the default LEGO firmware. What follows is a tutorial on how to start programming using your Mac and NBC.
Writing and compiling the code
First download NBC (I’ve used version 1.0.1 b8) from the NBC homepage. This will usually download onto your desktop. The editor I use is TextMate, it’s not expensive and is very usable. Later I will publish a TextMate Bundle for NBC, so that it’ll become easier to start programming. Now type or cut-and-paste the following code (which I stole from here) in TextMate and save the file as helloworld.nbc:#include "NXTDefs.h"
// Always include the above
// Segments declare variables or types
dseg segment
// Variables are always global.
// Define variable dtArgs of type TDrawText
dtArgs TDrawText /* see Syscall DrawText docs */
dseg ends
// Code is organized in threads
thread main
// Set is to store scalar constants
// For instance, set X location to 1
// X is a member of the TDrawText struct
set dtArgs.Location.X, 1
set dtArgs.Location.Y, 1
set dtArgs.Options, 1 /* erase previous text */
// Mov is to copy more complex data
mov dtArgs.Text, 'Hello World'
// Write to the screen
// by calling the Lego firmware
syscall DrawText, dtArgs
// Wait forever so the user sees the screen
Endless:
jmp Endless
endtOkay now you need to start Terminal.app and run nbc:

Make sure the extension of your sourcecode is .nbc, the extension of the output file is .rxe and the command you type is: ./nbc helloworld.nbc -O=helloworld.rxe. For more command-line options type ./nbc -help.
Putting it on the brick
Currently the Mac version of the NBC program is not capable of downloading your program to the NXT brick, like the Windows version does, so we’ll need to use the Mindstorms NXT software which came with your kit:
First start the software and start you’re brick (press the orange button). Now we need to download to you’re brick, the simplest way of achieving this I think is by pressing the New program GO button:
Now click the NXT Window button and open the Memory tab:
Click the Download button and find your helloworld.rxe application:
Click the Ok button and … that’s it! Now start helloworld from your brick! Quit the Mindstorms NXT application and don’t save the program (as there isn’t any programming done in the LEGO Mindstorms application). Do save your helloworld.nbc program of course!
Afterthoughts and future
Some afterthoughts and something on the future on NXT and Mac:- In the near future NBC on Mac OSX will very likely support auto-downloading to your brick, which simplifies things drastically.
- TextMate supports Bundles, which are helpers to simplify programming for you. In the near future I will create such a TextMate Bundle and publish it on this site
- This site is built using Typo and uses NBC code syntax highlighting using the default plugin of Typo and a NBC specific class to do the highlighting. I will publish this later.
Older posts: 1 2


