Loren on the Art of MATLAB

Turn ideas into MATLAB

LEGO Mindstorms NXT in Teaching

Today I’d like to introduce a guest blogger, Gautam Vallabha, a developer here at The MathWorks who works on classroom applications of MATLAB. Today he shows you one way in which MATLAB
can be used to teach programming concepts.




Before I joined The MathWorks, I was an academic doing research on neural networks and neuroscience. MATLAB's utility in research
is a commonplace, but one thing that struck me was its utility in learning: its interpreted nature and visualization allowed
me to play with ideas numerically and graphically. Today, I want to highlight one such teaching application, which is the
use of MATLAB with the LEGO Mindstorms robotics system.

What is LEGO Mindstorms?

The LEGO Mindstorms NXT is a "robotics system" composed of a few sensors, motors, and an embedded processor (the "NXT brick"). What makes the system
more than just an obscure hobbyist toy is its price (around $250, in the same ballpark as consumer electronics items) and
the decision by LEGO to create an open programming interface. This allows third-party programs to send commands to the NXT over a Bluetooth wireless link, compile programs to be executed
autonomously on the NXT, or replace the NXT firmware entirely with custom firmware and programs. In this post, I focus on
the Bluetooth wireless approach.

But what does this have to do with teaching? Simply this: it is now easy and (relatively) inexpensive to connect lines of
code on a computer screen to concrete actions in the world. A LEGO Mindstorms robot's actions - scooting around the room,
bumping off the walls, playing sounds - can be controlled on the fly by a MATLAB program. This provides a powerful way to
motivate students to explore and master programming concepts.

Step 1: Build an NXT Robot

Let's see how to control the standard NXT example robot, the tribot.

The tribot has two independently-powered wheels, a touch sensor in front to detect obstacles, and a sound sensor (so that
the tribot can be started or stopped with a clap, for example). In addition, it has a pair of claws that are powered by a
third motor. The NXT has four sensor ports (Port1Port4) and three motor ports (PortAPortC). In a typical configuration,

  • PortA is connected to the claw motor
  • PortB and PortC are connected to the left and right wheel motors
  • Port1 and Port2 are connected to the touch and sound sensors.

Step 2: Set Up the Bluetooth Connection

Next I establish a Bluetooth connection between the LEGO NXT and the host computer. To do this, I need a Bluetooth adapter
(preferably one recommended by LEGO). Once the adapter is installed and paired with the LEGO NXT, the communication channel shows up on the host computer as
a virtual serial port (e.g., 'COM40').

Step 3: Set Up a Connection Between MATLAB and the Tribot

I use the serial port I/O capability to access a serial port. The freely-downloadable MATLAB toolbox wraps this communication channel into a high-level object-oriented interface. A similar, though non-object-oriented, toolbox
has also been developed at RWTH Aachen University in Germany.

See UPDATE at the bottom of this post

Once the MATLAB toolbox is downloaded and installed, I first need to add the download directory to the MATLAB path, using

Now I can initiate a MATLAB connection to the tribot. (You can try out the toolbox even if you do not have a LEGO robot by
using 'test' for the name of the serial port).

nxt = legoNXT('test');

nxt is a MATLAB object and the input and output ports are objects also.

leftMotor = nxt.PortB;
touchSensor = nxt.Port1;
whos nxt leftMotor touchSensor
  Name             Size            Bytes  Class         Attributes

  leftMotor        1x1              1260  outputPort              
  nxt              1x1               840  legoNXT                 
  touchSensor      1x1               280  inputPort               

Issue commands by invoking the appropriate object methods. For example, let me to rotate the left wheel for 3 seconds at a
power level of 30 (out of a maximum of 100).

start(leftMotor, 30);

In order to read a sensor, I first need to tell the NXT what type of sensor is connected to the input port. From then on,
the sensor can be read with a simple method call:

% configure nxt as a touch sensor
set(nxt.Port1, 'type', 'touch');
% get a sensor reading
v = getdata(nxt.Port1);

The NXT Bluetooth interface has other capabilities in addition to accessing motors and sensors. To access these capabilities,
I need to get the "low-level" NXT interface from the nxt object.

type lowlevelNXT
 nxti = get(nxt, 'NxtInterface');
 % Play a 1000 Hz tone for 300 milliseconds 
 % execute a program already present on the NXT
 startProgram(nxti, 'Demo.rxe');
 % get a full list of low-level commands
 help nxtInterface/Contents     

Finally, close and clean up the interface.

clear nxt

Step 4: Create Tribot Programs in MATLAB!

The capabilities I just showed provide the building blocks for students to explore a variety of programming concepts. A simple
assignment might be, "wait until the sensor is pressed and released five times". This assignment requires understanding of
both looping and conditionals. Here's one solution.

type checkSensor
function checkSensor(nxt)
% sensor values less than 50 => touch sensor is depressed
count = 0;
previousValue = 100; 
while count < 5
   currentValue  = getdata(nxt.Port1); 
   if (previousValue >= 50) && (currentValue < 50)
      count = count + 1;    
   previousValue = currentValue;

Motor actions lend themselves naturally to grouping, through the use of functions. This function has the tribot back up for
duration seconds, chirping all the while.

type backupTribot
function backupTribot(nxt, duration)

nxti = get(nxt, 'NxtInterface');
playSoundFile(nxti, '! Attention.rso', 1);
start(nxt.PortB, -20); start(nxt.PortC, -20);
stop(nxt.PortB); stop(nxt.PortC);
playSoundFile(nxtInterface, '! Attention.rso', 0);

An advantage of developing these programs in MATLAB is that students can use the MATLAB environment for interactive debugging
and visualization. For more advanced assignments, students could build a MATLAB-based GUI to control a robot (these would
integrate well with teaching object-oriented programming). To give a flavor of such GUI-based control, the toolbox ships with
nxtdemo, a sample GUI.

I should note that the NXT Bluetooth interface does not provide support for fine motor control (for example, to rotate a motor
through a specified angle). Likewise, the round trip latency – sending a command from the PC to the NXT and receiving a response
is on the order of 30 ms – makes it impractical for real-time control. Nevertheless, the basic capabilities provide enough
flexibility for many interesting projects.

For those looking for real-time control, there is a free Simulink-based library that provides much more sophisticated embedded control.

What Else?

What other capabilities would make the MATLAB + LEGO NXT combination especially useful in a classroom? Let me know in here.


As of April 2010, the toolbox described above is deprecated and no longer
available. We encourage users to download the
RWTH Aachen Mindstorms NXT Toolbox
--- it has all the capabilities described above, as well as
support for precise motor control and ability to control the robot over
a USB cable. For more information, see LEGO MINDSTORMS NXT Software for
MATLAB and Simulink

Published with MATLAB® 7.10

  • print
  • send email


댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.