You are on page 1of 7

ECE1882

LEGO NXT Brick Programming Guide


Introduction
This document was written with two goals in mind. The first is to orient a new user to the
graphical programming language used in the MindSpring NXT robot control software.
The best way for this to occur is to watch the series of videos
http://cgsweb.catlin.edu/msrobotics/html/downloads.html. The link is for the NXT
Tutorial. For those who dont want to invest the time to watch all the videos, this
document is your next best source.
The second goal is to provide a list of tips, tricks, and best known methods that have been
discovered over the life span of the course.

Introduction to the Programming Tableau


The programming Tableau (ta blow) is the graph paper that is presented when the
Mindstorms-> New Program series of window choices is selected. A picture of the
Tableau is below:

The tableau has three general areas. The first is the palette selector. This is the graphical
shapes on the left side. The palette is currently in the complete mode, which allows

selection of all of the programming icons. Selecting the green dot over the Motor icon on
the bottom left changes the palette selection to a subset of the complete palette, and the
blue double lines allows access to user defined subroutine modules called My Blocks.
The graph paper in the center of the screen is where the user builds his program by
connecting various blocks. On the bottom of the screen, the programming bar allows the
user to configure a programming block. In the picture above, the motor block has been
selected, and the user has configured it to move the A motor forward at a 75% power
rating.
A very important and often overlooked feature of the programming tableau is the Help
pane, which is shown below:

The icon shown is the Display object, and when selected, the link in the bottom right with
the text More help opens a help page with all the information on this block that a user
could need. This page is very often overlooked and is perhaps the most powerful tool a
user has when trying to debug a problem.

How to control program flow.


In a text based programming language, the statement order in the program file controls
the order that statements execute in. For example, the text:
sscanf(read_device, read_file,num_bytes);
printf(display,I read %d bytes\n,num_bytes);

reads from some type of character based device into a file a certain number of bytes, and
then prints out to the screen a message containing the number of bytes that were read.
In the NXT graphical programming device, there is not a top/bottom orientation to
control the program flow. Instead, blocks are connected together via the connection bar.
The program flow is from left to right on the tableau. When two icons are connected
together, in general, after one icon has completed its action, the next icon is activated,
and performs its actions. There are some cases where an icon can pass control to the
following block before all of the first blocks actions are complete. This exception is
covered later.

In the above tableau, the Start Bar is on the far left and is connected to a motor block.
Once the motor block completes its movements, the control flow is split, and the two
legs of the program that both begin with CB move blocks are executed in parallel. In
each leg, once the movement portion is complete, the Ultrasound and display tasks would
be executed. Please note, there is no way to synchronize the actions in the parallel legs
without bringing the flow back to a single connection bar and placing some type of if
or test (i.e., test for light sensor > 30 dB) gate onto the connection bar.
To attach an icon to the connection bar, mouse over to the palette on the left side of the
screen and click on the icon that you wish to place. This will pick the icon and attach it
to the mouse pointer. Move the mouse over to when you want to place the icon, and click.

If the mouse is over the connection bar, the icon will be placed onto the bar. If the mouse
is between two icons on the connection bar, the icon that the mouse pointer is holding
will be inserted between the two existing icons, and the screen will be redrawn.
In addition to passing control via the control bar, the icons can communicate by
connecting wires or setting variables. A wire can be considered a local variable. The
following picture shows an ultrasonic sensor connected to a display block. The ultrasonic
sensor will perform its measurement, and then control will pass to the display block via
the connection bar. The connection bar does not pass any data, it passes control only.
Therefore, to export data from an icon, you must either save it to a variable or send the
data somewhere with a wire.

The wire has no global scope, the variable in the wire only exists in the places that are
directly connected to the wire. A variable read/write block can convert a wire to a static
global variable that can be accessed other places in the program without having to run
wires everywhere.
Previously, we said that an icon finished its actions before it passed control to the block
behind it. This is mostly true. However, there are some blocks such as those that play
sound, write to the display, or move the motors, that have the ability to pass control
before they have completed their actions. See the help page for each icon to determine if
this option exists.

Tips to help your programming effort go better.


The Wire Tool:
In general, most students abhor having to use the wire tool. It seems to click where the
user does not want it, and it does not connect in the proper place. The best
recommendation that can be made is to move the mouse to the connection point, and wait
for the mouse pointer to change. Once the wire tool has been presented, then click, and
wait for the indictor to change again. Then, move directly to the desired connection point,
and click again. If you try to do this too quickly, you will just become frustrated.
Sometimes, there is nothing to be done other than delete the wire and start over again.

Code Size:
We have noticed that when the programming tableau gets a large number of icons, the
screen redraw becomes odious. The suggestion is to gather linear blocks of icons into
My Blocks, so that there are fewer icons on the screen. We have not found any
performance impact to replacing sections of code with My Blocks.

Specific Tools:

The move block allows a single block to control multiple motors. This block
allows less precise movement than instantiating two motor blocks.
Bluetooth
1. Once Bluetooth is enabled on the NXT bricks, devices are able to see other
Bluetooth-enabled devices in the vicinity of 10m, including cell phones, PDAs
and other teams' bots. Thus, it is very vital for you to assign unique names to
your NXT bricks by using the LEGO Mindstorms software. Names can be
assigned through the tool in the Mindstorms software that is used for
controlling the connection between your NXT and the computer.
2. After the NXT brick is turned off, the Bluetooth connection is lost. After
turning the NXT brick back on, the connection can be reestablished between
any two bricks by using the "search" utility under the Bluetooth menu of the
NXT brick. If a Bluetooth connection has previously been established
between two NXT bricks, then passphrases do not have to be entered again
(see tip #3).
There are two vital initialization steps. One is to provide a passphrase between
any two communicating NXT bricks for security and sync purposes. Here, an
initial passphrase is exchanged between the two NXT bots. After passphrases
are set once, the bricks will remember them even if they are turned off. The
other step is done after the initial exchange of passphrases, where the NXT
needs to do a search for setting up the communication channel. A

communication channel is chosen between the two devices having the same
passphrase. (Please see the 4th tip below as well.) An NXT bot is able to see
(i.e., "search") other NXT and Bluetooth-enabled devices in its vicinity.
However, an NXT has no capability to discern other Bluetooth devices before
the passphrase setup, except by name. Thus, the need for naming as in tip #1.
3. NXTs communicate over channels. There are 4 different channels for
Bluetooth communication. Please make sure that the connection channel
chosen in your software program is the same as the one chosen when setting
up the connection between the two bots.
4. NXT bricks have a built-in functionality to send files (such as compiled code)
to other NXT bricks. However, please note that transfers are only possible
from a master to its slaves.

Debugging steps.
One of the most critical skill sets that you will need to develop during this course are
debugging skills. These are normally specific to a person, but some general guidelines
can be given.
1. Break the problem into smaller steps. If you have a large program, and are not
sure what is happening, break the problem into logical pieces, and work on one
piece at a time. When you have a block that works correctly, move it into a My
Block construct and move to the next block.
a. Make sure that you have tested the corner cases of your My Block.
Running a single test does not mean that your code works correctly in all
cases. Before claiming that the code is perfect, you need to test it fairly
rigorously.
2. Start with a flowchart of your problem, both the way you want it to work, and the
way it appears to be working. If you are working with a small enough block of
code, through trial and error, you should be able to construct the flowchart of
what is actually happening. Compare this with the desired behavior.
3. If you have branches or loops in your code, and it appears that certain gates are
not being recognized, consider using LEDs or sounds to indicate that the brick has
recognized a certain criteria.

The Help Browser


The help browser is very useful, but most students do not use it. For example, by just
pressing the Help link in the bottom right hand corner, the help browser displays the
general topics screen:

Many students suffer with wires and broken wires for many weeks before they either
discover the secret found in the Data Wires, Broken section for themselves. (Have you
read this section, yet?)

You might also like