COSC343

Artificial Intelligence

LEGO Resources

This page describes the NXT mindstorms robots, and how to program them.

Background

We use Mindstorms NXT robots in COSC343. These robots use the 32-bit ARM7 microprocessor, and the 8-bit Atmel AVR microcontroller. Here's a rundown of the features of the Mindstorms NXT kits.

  • They have four inputs (for sensors) and three outputs (for motors).
  • The motors are servomotors, which come with position sensors, so you can tell them to rotate by precise angles.
  • There are four sensors:
    • A light sensor. (Sensitive to intensity only - not colour.)
    • A bump sensor. (On/off.)
    • A sound sensor. (Sensitive to intensity only - not pitch.)
    • A sonar sensor, which computes distance to an object in a certain direction. (Only works well if the object presents a smooth plane, and there are no confusing sonar reflections in the environment.)

  • The NXT robots have 64K of RAM, and 256K of flash memory.
  • They have a simple file system, and a simple LCD screen.
  • They communicate with a host machine via a USB link. In fact, they can also communicate using bluetooth, though we don't have that enabled in the department.
  • The NXT robots are quite sturdy, being based on the LEGO technics paradigm: lots of struts, and not so many bricks (though regular bricks are still compatible).
  • The robots have inboard rechargeable batteries; much more--convenient.

You can find out more about the NXT system:


A hierarchy of programming languages

As with any computer system, there are several different levels on which you can interact with the NXT robots.

  • At the lowest level, the ARM7 microprocessor has its own instruction set: a set of low-level operations for accessing memory, changing register contents and so on.
  • The NXT robots also come with their own purpose built operating system, called its firmware. The firmware allows programmers to interact with the robot at a slightly higher level, using a special purpose bytecode: basically a kind of high-level assembly language.
    The firmware should already be installed on your robot. But if a robot's batteries ever run out completely, it needs to be reinstalled. Ask Matthew for instructions about how to do this.
  • Bytecode is still pretty low-level. Several compilers have been developed to allow programmers to use high-level programming languages to interact with the NXT robots. These compilers convert a high-level language into NXT bytecode. The Wikipedia page has details of some of these compilers.

NXC

The language we'll be using on this course is called NXC (which stands for 'not exactly C'). You can find out everything you need about NXC on the NXC website. In particular:

  • There's a useful NXC tutorial. (The tutorial assumes you're using a rather nifty development environment called bricxcc, which is only available for MS Windows. The compilation/uploading instructions below provide alternatives which work for Linux.)
  • There's also a comprehensive NXC programmer's guide.
Note that every NXC program begins with a call to include a file of macro and constant definitions called NXCDefs.h .
#include "NXCDefs.h"
You probably won't have to look inside this file - just remember to include it as the first line in all your programs.

You can download the NXCDefs.h file from here.

Writing and installing a program using NXC

Assuming your firmware is installed, the basic development method you will be using is as follows:

  • First write the NXC program, on a regular text editor, in the NXC language. (Obviously you should start with a simple 'hello world' program; perhaps something which drives forward for a few seconds-- then drives backwards for a few seconds, then stops.)
  • Then compile the program. To do this, you first need to place the NXCDefs.h file in a directory [dirname]. Assuming your NXC program file is called [myprogram.nxc], you need to run the following command:
    nbc -I=[dirname] [myprogram.nxc] -O=[myprogram.rxe]
    When your NXT program is compiling cleanly, the compiler creates a bytecode file called [myprogram.rxe].
  • Now you need to upload the file to your robot.
    • Turn your robot on, and plug it into a USB port on your machine.
    • Now run the following command:
      linxt -u [myprogram.rxe]
      Linxt is a wee Perl program which communicates with your USB port. You may need to set a few environment variables to run the right version of Perl:
      PATH=/usr/local/bin:/usr/bin:/bin LD_LIBRARY_PATH=/usr/local/lib:/usr/local/lib/perl5:/usr/lib:/usr/lib64 PERLLIB=/usr/local/lib/perl5/site_perl/5.10.0:/usr/lib/perl5/site_perl//5.8.8/i386-linux-thread-multi:/usr/lib64/perl5
    • You should now be able to find (and run) your program on the robot. See the next section for how to do this.

The robot's control panel

You control your robot using a simple system of hierarchical menus. There are four buttons: 'left', 'right', 'okay' (the orange button) and 'back/delete' (the dark grey one). Kind of primitive, but if you managed to get used to texting, you should be able to get used to this too.

The top-level menu includes an option 'my files', which takes you to a primitive file system. At the top level of the file system is the option 'software files'; inside here you'll find your program. If you select it, you have the option of running it.

Once your robot's program is running, you can abort by hitting the dark grey button. Easier said than done when it's zipping along the carpet. I suggest you pick it up first.