Thursday, August 03, 2006

Getting Started with NXT Byte Code (NBC) Programming

NXT Byte Code (NBC) is an “assembly” language that allows to write software directly on the NXT brick. It works with the standard Lego firmware that comes with your NXT set. This means that you can develop some of your programs using the NXT-G graphical environment, and some with NBC. Both software be uploaded independently from each other. However, it isn’t possible (yet?) to include NBC within the NXT-G environment (or vice versa).

NBC is really simple, but documentation on it is still rather scarce. That’s why I decided to write a short tutorial on how to get started with NXT Byte Code programming.

Hello World

Let's start with looking at the first program for any programming language: putting "Hello world" on the screen. In this case, on the screen of the NXT brick. Here's the complete Hello world program:

#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
endt

So how do we execute this program? The simplest way is to use the Bricx Command Center together with the NXT Byte Code compiler.

BricxCC and NBC Installation

The Bricx Command Center supports Lego Mindstorms NXT, and NBC, and provides an easy to use editor for it. If you prefer to install only NBC, then skip to the “NBC Only Installation” section. If you are using Apple or Linux, then you also need the “NBC only” installation.

First, we need to download the BricxCC software from http://bricxcc.sourceforge.net/test_release.zip. You may want to check http://bricxcc.sourceforge.net for later versions. Download the zip by selecting the “Open” option (or save first, and open it locally). Double click on “BricxCC.exe”. Windows will provide the options “Extract all”, “Run”, or “Cancel”. Choose “Extract all”. Follow the wizard, and choose a directory to install. For this introduction, I’ll use the directory “C:\Program Files \BricxCC”. Notice that the wizard allows you to create new directories as needed. After selecting a directory, the wizard will install BricxCC. When installation is complete, you can close the archive you opened.

Next, download the NBC software from http://bricxcc.sourceforge.net/nbc/. In this introduction, I’ll be using version 0.1.4 b4, dated June 15, 2006/July 26, 2006. Select that beta version, and next select the Win32 version. You can save it locally, and then open it, or open it directly. Double-click on “nbc.exe”. Again, select “Extract all” and use the wizard. Make sure to install NBC in the same directory as BricxCC. So within the wizard, select the “C:\Program Files \BricxCC” directory to install in. At the end, the wizard shows you the content of the BricxCC directory.

You may want to create a shortcut to BricxCC by dragging the BricxCC.exe file to the start menu, and to the location where you want it. For instance, in the Start > Program Files > LEGO MINDSTORMS NXT directory. Feel free to rename the shortcut.

Start BricxCC (any starting options). Define the location of the compiler by selecting “Edit”, “Preferences”, “Compiler”, “NBC”. Set the EXE Path to “C:\Program Files\BricxCC”.

This completes the installation. To run the Hello World program, turn on the NXT and connect it to the PC. Then start the BricxCC software. This time, make sure to select “usb”, “NXT” and “Standard” as starting options. It takes a while for BricxCC to start up due to connection time-outs. Create a new document, and copy following Hello World code in it. Save it. Then press the Run, or select Run from the compilation menu. Now look at the screen of the NXT... You can terminate the program with the grey cancel key on the NXT brick.

NBC Only Installation (command line)

If you prefer not to use BricxCC, then here’s how to install NBC without installing BricxCC. To use NBC later on, you will need to be able to use the command line. If you are working with a Windows PC, and have no experience with DOS commands and the command line, then choose the BricxCC installation option. There is no BricxCC for Apple or Linux, so those users need to install NBC only. I do not own an Apple or use Linux, so I cannot provide installation instructions for those platforms, sorry.

Let’s first download the software from http://bricxcc.sourceforge.net/nbc/. In this introduction, I’ll be using version 0.1.4 b4, dated June 15, 2006/July 26, 2006. Select that beta version, and next you’ll need to select the operating system that you are currently using. I used the Win32 version for this introduction. You can save it locally, and then open it, or open it directly.

After opening the zip, you should see three files: "history.txt", "nbc.exe" and "NXTDefs.h". Double click on "nbc.exe". Then choose the Windows option "Extract all". Next, the NBC "Extraction Wizard" pops up. Click next and choose a directory. The wizard gives you the option to create directories as needed. For this introduction, I’ll use "C:\Program Files\NBC". After selecting a directory, click "Next" again.

Next, you’ll want to add the directory you have just created to the command path. Assuming you are working with Windows XP, go to the "Control Panel" (via "Start"). If you are in classic view, then choose "System". Otherwise, choose "Performance and Maintenance" and then choose "System". Go to the "Advanced" tab, and click the "Environment Variables" button. In the bottom window, scroll and select the "Path" variable. Select "Edit". At the end of the existing variable value, add the text ";C:\Program Files\NBC" (without the quotes – or the directory where you have installed NBC. Don't forget the leading semi-column). Click OK three times and close the control panel.

Your manual installation is now complete.

To run the Hello World program, save it to a file, for instance "bNXT_HelloWorld.nbc". Then, within the directory where you saved the file, start the following command at the command prompt:

“c:\program files\nbc\nbc” –S=usb –d bNXT_HelloWorld.nbc

Make sure that the include file is in the same directory, or that you specify the include directory.

3 Comments:

At November 29, 2006 10:10 AM , Anonymous Anonymous said...

The debugger for NBC is available at http://www.sorosy.com/lego/nxtdbg/index.phtml. Plenty of features, practically everything that you need to debug NBC programs. You can leave feedback at http://forums.nxtasy.org/index.php?showtopic=534

 
At March 13, 2007 2:22 PM , Anonymous Anonymous said...

NBC Debugger Beta 2 has been released. New features:
- Added motor start/stop syncronization with the program's debug run/pause/stop state
- Added context menu to breakpoint window and variable watch window
- Added ability to remove user watch variables
- Performance tuned file loading and initializing improved load time by over 70%
- Added source code syntax coloring based on coloring set in BricxCC
- Added source window status bar with file name and cursor location
- Added "Goto Line" functionality to source window
- Added "Find" functionality to source window

 
At December 22, 2007 4:54 AM , Anonymous Anonymous said...

I am stuck on the installation of BricxCC v34. A password is requested.
Can anyone help me?
Thanks in advance.

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home