Three different programming environments are available from LEGO for the NXT– NXT-G, ROBOLAB and ROBOTC, and they all make it quite easy to program Mindstorms robots. In addition, the open source community has developed other alternative programming solutions for the NXT. This article provides an introduction to the rich selection of programming approaches available today. This online expanded version of the Winter 2007 article features an extended comparison table and a lot more technical details behind this article.
NXT-G
NXT-G is a graphical programming environment developed by National Instruments for LEGO. Writing an NXT-G program is very much like creating a flowchart. You “write” a program by dragging icons (“code blocks”) that describe different behaviors, e.g., turn motor A on at 75 percent of full power, and connect them with lines to describe the program behavior. Using a variety of code blocks, you can control motors, introduce delays, play sounds and direct the flow of your code according to the state of sensors and timers, etc.
The diagram shows an NXT-G implementation for a “classical” line-following robot. The program looks at the value of a light sensor positioned above the line, and depending on which side of the line edge it is on, i.e., light or dark, turns one rear wheel on and the other rear wheel off. This is implemented as a loop that is repeated forever. Inside the loop, the light-sensor value is checked and the program branches to “true” or “false” code based on the value. NXT-G is targeted at children and adults with no programming experience, and for this reason, it is very easy to use.
NXT-G graphical programming screen for a line-following robot.
ROBOLAB
ROBOLAB was originally developed by Tufts University for LEGO for the first generation LEGO Mindstorms RCX microprocessor “brick.” It was extensively enhanced and revised to support both the RCX and the second-generation NXT.
ROBOLAB is another graphical environment, although it is not as intuitive as the NXT-G language. If you already know
ROBOLAB or you need to program on both the RCX and NXT, ROBOLAB is a good choice for you. If you’re just starting and want a graphical programming language for the NXT, the
NXT-G is the better choice.
ROBOLAB program screen for line following.
ROBOLAB was written using the LabVIEW system from National Instruments. LabVIEW is also the underlying core technology used to write NXT-G. Other useful ROBOLAB features include support of both integer and floating point calculations, and ROBOLAB has a comprehensive data-logging solution. Graphical interfaces such as NXT-G and ROBOLAB are very intuitive but can become tedious as you become more experienced with programming.
ROBOTC
The ROBOTC solution allows the NXT to be programmed using the industry-standard C language. It was developed by the Robotics Academy at Carnegie Mellon University and can be obtained from the LEGO Education Group or directly from the Robotics Academy at www.robotc.net. Both of the graphical programming solutions had drag-and-drop capabilities for the “code blocks.” ROBOTC has a similar capability, but with it, you drag and drop text. The left window in the picture contains the “dictionary” of ROBOTC’s built-in robotics control capabilities. ROBOTC is targeted at novices and advanced users. ROBOTC has “basic” and “expert” modes; in the “basic” mode, a lot of the advanced functionality and menus are hidden.
ROBOTC has a powerful interactive real-time debugger that significantly reduces the time it takes to debug programs. So far, over 2,000 students have been taught ROBOTC in the classroom; at the end of the first class, they were programming and running their first ROBOTC programs for the NXT. Carnegie Mellon University’s Robotics Academy has developed step-by-step instructional videos that enable new users to become competent programmers quickly. ROBOTC supports both the NXT and RCX as well as products from Innovation FIRST (VEX and the FIRST Robotics Competition).
NXC ... NOT EXACTLY C
NXC (Not eXactly C) is a C-like language for the NXT. NXC programs are developed using the Bricx development environment. Bricx was originally developed for LEGO’s earlier RCX robotics product and has been enhanced to support the NXT.
NXC uses the same firmware as NXT-G. This is very convenient for users who want to program in both a graphical and a text environment because they don’t have to reload and change the firmware every time they switch the environment type. You can store both NXT-G and NXC programs simultaneously in the same brick. NXC has the same limitations as the NXT-G solution. It has integer but not floating-point variables. It doesn’t have more powerful LCD text formatting.
NXC line-drawing program screen.
The NXC programming language syntax and semantics are similar to C’s, but they are also different. For example, NXC character strings start with a double quote and end with a single quote; C uses double quotes for both the start and the end. NXC and ROBOTC are the only solutions that have run-time debuggers.
PBLUA
Lua is a relatively new text-based language that has gained a lot of attention and interest in the academic arena. pbLua is a full-featured version of Lua for the NXT. The pbLua implementation of the line-
following program is very similar to the ROBOTC and NXC
implementation.
pBLua implementation of line following.
NXJ
NXJ is a JAVA implementation for the NXT. It is standard JAVA but with a much smaller Class library. The standard Class library is far too large for the total 256K bytes of memory on the NXT. NXJ programs are written and compiled on the PC. The compiled programs are then
transferred to the NXT where they can be executed.
NXJ line-following programming.
SUMMARY OF PROGRAMMING LANGUAGES AND IDEs
FEATURE
NXT-G
ROBOLAB
ROBOTC
NXC
pbLua
NXJ
LANGUAGE
Graphical
Graphical
C
C-like
Lua
JAVA
PRICE
$0 (retail)
$50(education)
$50
$30 web
$50 – CD
Open Source
Open Source
Open Source
PLATFORMS
Windows, MAC OSX
Windows, MAC OSX
Windows
Windows, MAC OSX,
LINUX
Windows, MAC OSX
Windows
END USER FIT
Novice to Intermediate
Novice to Intermediate
Novice to Advanced
Intermediate to Advanced
Intermediate or higher
Intermediate or higher
ONE BUTTON INSTALLATION
Y
Y
Y
Y
No
Partial
EASE OF USE (1 to 10. 10 is best)
10
7
8
6
4
4
RELATIVE EXECUTION SPEED
1X
70X
130X (fastest)
25X
35X
n/a
INTEGRATED DEVELOPMENT ENVIRONMENT
Y
Y
Y
Y
Soon
(via Eclipse)
Partial
(via Eclipse)
REAL TIME DEVELOPER
N
N
Full Featured
Partial
N
N
SOUND PLAYBACK CAPABILITIES
Tones + WAV
Tones + WAV
Tones + WAV
Tones + WAV
Tones
Tones + WAV
LCD DRAWING CAPABILITIES
Text
Basic
Enhanced++
Enhanced++
Basic
Enhanced
Enhanced
Graphics
Basic
Enhanced
Enhanced
Basic
N
Enhanced
Icons
Y
Y
Y
Y
N
N
LANGUAGE FEATURES
Variables
Hard
Hard
Y
Y
Y
Y
Floating Point
N
Y
Y
N
Y
Y
Trigonometry Functions
N
Y
Y
N
Y?
Y?
‘switch’ construct
Y
N
Y
?
N
Y
Arrays
N
N
Y
Y
Y
Y
BLUETOOTH CONNECTIVITY
Connectivity to other devices
PC, NXT
None
PC, NXT, GPS, others
PC, NXT
PC, NXT, GPS, others
NXT, Others
Fantom Protocol Supported
Y
Y
Y
N
N
Duplex (speed)
Half
Full
Half
Full
Full
ON-BRICK FILE SYSTEM
Y
Y
Y
Y
N
N
ON-BRICK PROGRAMMING SYSTEM
Y
Y
Y
Y
N
N
INTEGRATED DATA LOGGING
N
Y
Y
N
N
N
LEGO RCX COMPATIBLE
N
Y
Y
Partial
N
Partial
The above table was created September 2007. Many of the solutions continue to evolve and, over time, may improve their entries on an individual item. A large amount of data was condensed and simplified to create the above table. The following provides additional expansion on the table items.
Sound Playback Capabilities
FUNCTIONALITY
FEATURE DESCRIPTION
TONES
Play a single tone on the NXT speed at a specified frequency and for a specified duration.
TONES + WAV
Play “WAV” (i.e. sound) files stored on the NXT. The sound files contain a list of sound “samples” that are played back at a fast rate.
LCD Drawing Capabilities
ATTRIBUTE
FUNCTIONALITY
FEATURE DESCRIPTION
TEXT
Basic
Draw a text string at specified coordinates on the LCD screen.
Enhanced
“Print” format controls on text string and numbers. For example, “%6d” to format a number with exactly six characters padded with blanks on left.
Enhanced++
Multiple display fonts. ROBOTC and ROBOLAB have both the 6x8 size used in other systems and a 12x16 double size font.
Optionally erase individual text lines before drawing to eliminate artifacts. For example, if text line currently contains “012345” and you draw “999” you’ll get “999345” displayed without the optional erase.
Drawing routines optimized for minimal real time.
GRAPHICS
Basic
Draw lines, rectangles, circles and single points on LCD
Enhanced
Erase and fill functions for rectangles and circles for expanded drawing capability.
Draw ovals as well as circles.
ICONS
Ability to draw ICON files to the screen.
Language Features
ATTRIBUTE
CAPABILITIES
VARIABLES
All solutions support user defined variables.
The mechanism for defining variables in the NXT-G language is cumbersome.
Variables in graphical languages have global scope. You cannot define variables with local scope.
Variables in text languages are letter-case sensitive. ROBOTC has a nice feature that will generate a warning message rather than a fatal error message if you mistype the letter case; e.g. if variable is defined as “Value” and is subsequently referred to as “value” other compilers will generate a fatal “undefined variable” error whereas ROBOTC will (optionally) generate a warning only.
FLOATING POINT
All solutions support “integer” or whole number calculations. Expression results are always rounded to integer value; e.g. “4 / 5 results in “0” and not “0.8”.
Many text languages all support fractions or “floating point” numbers.
TRIG FUNCTIONS
Support for trigonometry functions is useful for programs performing “dead reckoning” based on wheel encoder results.
"SWITCH" CONSTRUCT
A “switch” statement allows testing of multiple conditions at once. For example
switch (v)
{
case ‘a’: print("aah"); break;
case ‘b’: print("bee"); break;
case ‘c’: print("see"); break;
case ‘d’: print("dee"); break;
}
You can use a series of nested “if” clauses to emulate a “switch” but it is cumbersome
if v == "a" then print("aah");
else if v == "b" then print("bee");
else if v == "c" then print("see");
else if v == "d" then print("dee");
The code generation from many compilers for a ‘switch’ statement is the same as the series of nested “if” statements – i.e. generates lots of instructions involving many individual comparisons are performed for matching the target item. ROBOTC has a special single opcode for the switch statement.
ARRAYS
Arrays are a useful programming concept. For example, to keep the last 10 sensor readings. Or to store a list of waypoints that robot should move to.
Bluetooth Connectivity
ATTRIBUTE
CAPABILITIES
OTHER DEVICES
Standard NXT-G firmware supports Bluetooth (BT) communications between PC and NXT and between NXTs. This includes use of the LEGO defined “Fantom” message protocol for this communication.
There are requirements to connect a BT device that does not support the “Fantom” protocol. A good example is connecting to BT enabled GPS receivers which utilize a different messaging protocol.
DUPLEX (SPEED)
The Fantom protocol uses the BT link in a half-duplex fashion.
The master device of BT link can send messages to a slave device at any time.
The slave device only sends messages to master in response to a ‘poll’ request from the master.
This results in inefficient use of the BT link and a long latency in sending messages from the slave to the master. When a slave wants to send a message, it is first buffered in an internal queue waiting for a polling request from the master to “get top message from the queue and send it back to the master”.
Some solutions can utilize the BT link in a full-duplex fashion. The slave can immediately transfer messages to the BT transmission hardware without waiting for a poll request from the master. This significantly reduces transmission latency.