happening. If you want proof of this assertion, watch
children learn to program with a visual language. They
generally can create programs quickly by simply duplicating
pictures, but ask them to modify the program to create an
alternative behavior and watch their reaction.
It is important to stress that we are not saying that
visual languages are without value. For many situations,
they provide an ideal solution and visual learners may learn
better when using them.
Procedural languages, though, make the flow of the
program much easier to grasp for many conventional users.
Regrettably, though, procedural languages generally force
programmers to immerse themselves in many other details
in order to accomplish even simple tasks. In order to
remedy this problem, we created a library of routines that
hide the details of controlling LEGO’s motors and reading
The library routines are generic enough that they can
be used with a variety of configurations of the NXT robot.
Figure 1 shows the robot we used to develop and test the
routines. The LEGO computer has built-in Bluetooth
hardware and a communication protocol that our routines
use to control the robot directly without downloading any
programs to the robot itself. This feature alone makes it far
easier for many beginners to program the LEGO robot. (See
this month’s The NXT Big Thing column.)
Let’s look at a simple program that demonstrates how
the library can make programming an NXT easy, but more
importantly, it demonstrates how programs written with the
library are easily understood by beginners. The program in
Figure 2 is written in RobotBASIC — a free language
available from www.RobotBASIC.com.
The first line in the program includes the LegoLibrary,
so that our routines become a part of the program. The
second line initializes a variable to the number of the
Bluetooth port used by our machine. The third line initializes
the library and brings us to the main body of the program.
A call to the library routine LegoDriveMotors turns
both motors on at a FAST speed. The first parameter
controls the left wheel and the second controls the right.
The next line calls another library routine to produce a
3,000 ms pause before the motors are
turned off. Nearly anyone can quickly
understand how this program works, and
once told they can use a speed of SLOW
as well as FAST, they can easily make
the robot move at different speeds for
different periods of time. They can also
make the robot turn by stopping or
slowing down one wheel while the other
moves at a FAST pace. This open-loop
control allows novice hobbyists or even
children to program a robot to move in
various patterns. We have successfully
taught fifth graders the fundamentals of
programming using this approach.
After a little practice with open-loop
control, it is easy to use LEGO’s sensors
to control the robot’s behavior. The
program in Figure 3, for example,
BluetoothPort = 34
causes the robot to move forward until the ultrasonic
ranging sensor detects an object less than five inches away.
When this happens, the robot turns a random amount and
continues the behavior.
Our LegoLibrary contains routines for interrogating
LEGO’s line sensor, sound sensor, bumper sensor, as well as
the ranging sensor. The routines must be past the port
number where the sensor is connected. This allows the
utilization of multiple sensors if you choose. For example,
you could create a robot with four ranging sensors.
The source code for the library can be downloaded
from www.RobotBASIC.com and anyone with
programming experience can easily identify all the available
routines by looking through the listing. Those new to
programming might benefit from the book RobotBASIC
Projects for the LEGO NXT. It uses the library routines to
develop robot behaviors for avoiding objects, following
lines, hugging walls, and more.
A LegoSimulationLibrary is also available from the web
page. When it is included instead of the standard library,
the same programs control a simulated robot allowing
experimentation even when hardware is not available. This
can be especially valuable for schools because every student
can work with their own simulated robot. When their
program is working, the teacher can plug in a Bluetooth
adapter to allow control of the real robot.
If you are looking for a more conventional language to
control your LEGO NXT or if you just want to share your
enthusiasm with someone totally new to robotics, give our
LegoLibrary a try. SV
BluetoothPort = 34
RangePort = 3
// Places the distance measured into the variable r
if r < 5
call LegoDriveMotors(SLOW, -SLOW) // rotate robot
call Wait(1500 + random(800)) // for a random time
call LegoDriveMotors(SLOW, SLOW)
SERVO 06.2011 61