2 Synthesis‎ > ‎Comptronics‎ > ‎

Lego NXT


Setting Up

Open a terminal from the Ubuntu menu;
  • Applications->Accessories->Terminal

With the NXT plugged in via the USB and switched on type;

lsusb|grep Lego

You should get the response;

    Bus 007 Device 002: ID 0694:0002 Lego Group Mindstorms NXT

Indicating the NXT is connected.

Now we need the nbc executable. To find the latest version start at;
Scroll down the page to the heading "News" where there is a list of releases like this;

TBD, 2011 - NBC 1.2.1 r5 is (not yet) available.
    See history.txt for a list of all the changes in this release.

March 13, 2011 - NBC 1.2.1 r4 is available.
    See history.txt for a list of all the fixes in this release.

June 30, 2010 - NBC 1.2.1 r3 is available.
    See history.txt for a list of all the fixes in this release.

June 6, 2010 - NBC 1.2.1 r2 is available.
    See history.txt for a list of all the fixes in this release.

Choose the latest available and click on the link. There are links to the source and Executables click on;
  • Linux (ia32)
There is a menu "Summary Files Reviews Support Develop MailingLists Code" select "Files".
Then select from the list of folders NBC_NXC.
Then select from the list of folders"NBC release 1.2.1.r4" or a later version and another list appears.
Then select from the list of files "nbc-1.2.1.r4.tgz" or a later version don't select the "nbc-1.2.1.r4.src.tgz"
DON'T click again. In a few seconds a dialog box opens. Take the option to "Open with Archive Manager".

You have downloaded three directories "doc", "NXT" and "tests" drag these to your Home folder.

Then my preference is to drag "doc" and "tests" into the "NXT" folder.

Using the Terminal you opened earlier change to the NXT directory;
  • cd NXT
Typing ls should show that it contains "doc nbc tests". Make sure that nbc is executable with;
  • chmod +x nbc

Then type;

  • ./nbc

The system responds;

Next Byte Codes Compiler version 1.2 (1.2.1.r4, built Tue Mar 15 16:10:49 CDT 2011) Copyright (c) 2006-2010, John Hansen
Use "nbc -help" for more information.

Running the Test

So the compiler is there. Now lets compile the test code "struct.nxc" in the "tests" folder. Type;

  • sudo ./nbc -d -EF tests/struct.nxc

Enter your password at the prompt. Using supervisor mode allows the code to upload to the Lego NXT. Then this should spit out a load of text. If it ends "Status: Firmware version check failed." then you will need to update the firmware on the Lego NXT as follows;

Downloading NBC/NXC firmware

On Sourceforge - NBC NXC SPC it says "To take advantage of multi-dimensional array support and native shift operations be sure to download the enhanced NBC/NXC firmware and install it on your NXT." so click on that last link and get the two files offered. In the NXT folder create a folder "firmware" and put them in it.

Download libnxt

Clicking on  takes you to the site and under downloads at the bottom left I clicked on libnxt-0.3.tar.gz and from there click on the download link to the right of the word "File" Open the downloaded file and it should contain a folder called "libnxt-0.3" copy this into your NXT folder.

Enter the folder by typing;

      • cd libnxt-0.3/

patch in the bug fix,

At the above link you will find this code;

--- libnxt-0.3/lowlevel.c    2007-04-22 05:24:17.000000000 +0200
+++ libnxt-0.3.1/lowlevel.c    2010-11-15 00:46:00.000000000 +0100
@@ -93,9 +93,18 @@
   char buf[2];
   int ret;
+  char bound_driver_name[20];
   nxt->hdl = usb_open(nxt->dev);
+//detach possible kernel driver bound to interface
+ if (usb_get_driver_np(nxt->hdl,1,bound_driver_name,sizeof(bound_driver_name))==0)
+   {
+  if (usb_detach_kernel_driver_np(nxt->hdl, 1)<0)
+    fprintf(stderr,"Failed to detach the driver %s (or cdc-acm) bound to the USB interface",bound_driver_name);
+    //Let usb_set_configuration() below return an error and handle it
+   }   
   ret = usb_set_configuration(nxt->hdl, 1);
   if (ret < 0)

From the menu choose Applications->Accessories->Text Editor and copy the above code into the open window making sure you don't  TODO are additional blank lines important?

Save As bugfix.patch in the NXT folder.

Now apply the patch with;

      • patch < ../bugfix.patch

The system responds "patching file lowlevel.c"

and build it.

      • scons

The system responds with "scons: Reading SConscript files ..." and a lot more ending with "scons: done building targets."

Check the system is still connected with;

      • lsusb|grep Lego

The system responds "Bus 007 Device 003: ID 0694:0002 Lego Group Mindstorms NXT"

Upload the firmware

POKE PAPER CLIP as shown on this site How to Flash a Lego NXT Brick on Ubuntu hold it for 5 seconds until you hear a quiet click click click every second. Now the NXT is ready to upload the new firmware.

      • sudo ./fwflash ../firmware/lms_arm_nbcnxc_132.rfw

The system responds "Checking firmware... OK." and ends with "New firmware started!".

The new firmware is uploaded. Type;

  • cd ..

to go back to the NXT directory.

go back to the start of this section headed "Running the Test" and try again.

If it ends "Status: Finished" then your ready.

Look at your NXT brick screen go into;

My Files->Software Files->struct->struct Run

and the program should run.


How to Flash a Lego NXT Brick on Ubuntu

sudo apt-get install build-essential libusb-dev scons

sudo apt-get install libusb-dev


HOWTO: Flash Lego Mindstorms NXT firmware in Linux 

Great but still nobody actually tells you how to get the bloody thing working!

Useful Links;

Java on NXT

The Manuals

Some Programs

NXC Sample Programs
NXT Ballbot balancing robot Remote Control
NXC: OnFwd
Starting MindStorm NXT 2.0 Development On Linux | Karol Krizka
NBC - NeXT Byte Codes, Not eXactly C, and SuperPro C
Ubuntu Daily: Using Lego Mindstorms NXT with Ubuntu Linux


HiTechnic Products

© Tom de Havas 2011. The information under this section is my own work it may be reproduced without modification but must include this notice.

 tail -f /var/log/