This page is about remote-controlling the Lego NXT system. For the old RCX system, please click here




Remote-controlling Lego Mindstorms NXT by using Visual C++

Introduction

After the big success of the Lego Mindstorms RCX, the Lego NXT robotics kit provides an even more powerful system for simple robotic applications. Improvements include servo-controlled motors, an ultrasound distance sensor, a more powerful computer brick and bluetooth communication.

However, again the standard way to program the NXT robot is to upload control programs to the NXT brick, after which they run autonomously without the need of a host computer. This has some drawbacks. The computational power and memory of the NXT brick is limited, which is an issue for complex control programs. Also in- and output are limited to the four sensor inputs and the three motor outputs of the NXT brick. This means that it is for example not possible for such stand-alone programs to use a signal of an overview camera, or a camera mounted on the robot for robot control.

We solved this problem previously, for the Mindstorms RCX robots, by using the infrared communication with the host computer to remote-control the robot. A host computer could then run computationally expensive applications, for example involving analysis of a camera image, read out sensor values from the RCX via infrared, and remote-control the motors of the robot, also via infrared communication. The design idea of this interface was also to provide a very simple solution which does not use proprietary libraries etc., with the source code available, so that the interface could also be modified/exdended or adapted to other compilers/languages/systems by an experienced programmer. Also the fact that the main program communicates with the interface by setting and reading variables, and the interface itself runs in a separate thread, should make it quite easy to use the interface.

The bluetooth communication capabilites of the Lego NXT robot allow for a similar way to remote-control the robot. The bluetooth interface has the advantage of higher reliability and larger range, compared to infrared communication. Also the data rate should be higher; however, polling sensor values is still very slow, because the bluetooth interface unfortunately needs approximately 50 ms to switch from receiving to sending, or vice versa.

Visual C++ remote control interface for the NXT

Fortunately, the NXT bluetooth interface can be accessed in almost the same fashion as the RCX infrared interface. For the host computer, the bluetooth connection looks just like a serial connection, and a C++ program can access it just like any other serial connection. Also fortunately, the direct control commands have not changed much from the RCX version, which made it quite simple to adapt the RCX remote control interface to the NXT. And, the NXT communication is much better documented by Lego than the RCX communication was. This interface uses the Lego Mindstorms NXT "Direct Commands".

However, there are some caveats. First, even though the bluetooth connection is more reliable and faster, polling sensors is still quite slow. Second, it is not straight-forward to access the very useful ultrasound distance sensor, for which some additional initialization commands are necessary. Third, the logic of the former RCX interface, which uses volatile variables which are simply set and read by the C++ application to remote-control the robot, is not capable of supporting the very accurate control of motors which is possible with the new servo motors of the NXT kit.

Currently supported features:

Currently NOT supported features:

Here is the code (use on your own risk!):

Instructions to use the nxt_roam example program

This interface has been written to be compiled under Windows, using Visual Studio 2005. It may also work with other compilers. Here is a list of instructions how to get the program to run under Visual Studio 2005:

To run the program, you first need to establish a link to your NXT robot by using the bluetooth interface. The robot should be set up as a TriBot with the switch and the ultrasound sensor mounted in front (see image). The left motor should be connected to output C, the right motor should be connected to output A. The ultrasound sensor should be connected to input 4, the touch sensor to input 1.

Then proceed as follows:

Now run the program. If everything is configured correctly, the robot should start to move forward. If it approaches obstacles ('seen' with the ultrasound sensor), it should slow down and veer off to the left. Hitting the touch sensor should stop the robot and the control program.

Here is a video of the NXT in remote-control action using this interface (MPEG-1, 835 kb).


This software is provided as-is. Please remember that you use this software at your own risk.
I am currently not working on Lego robotics any more, but you can contact me for questions and comments at: dberger@mit.edu
Last updated: September 7th, 2012. (Version of March 15th, 2007)