Serial Terminal:

Using Matlab, RealTerm and Bluetooth to control Lego Mindstorm NXT

Minstorms.m is in the Examples folder after installing Realterm

 

Contents

Gregory Gutt sent the following article....


Introduction

Below are two files describing how to control Lego Mindstorm NXT with Matlab and RealTerm. RealTerm was a great tool. Therefore I am sending this to return the favor, hope you could post it in an apporopriate place! I think some users would find this helpful. Regards. Greg. (greg@mymailbox.org)

Using Matlab, RealTerm and Bluetooth to control Lego Mindstorm NXT

Developed by Gregory Gutt, for the George Mason University Neural Engineering Laboratory

Background:

My wife (Nathalia Peixoto) is a professor at George Mason University in the ECE Department. She wants to control a Lego Mindstorm NXT Robot using increasingly sophisticated biosensors and voice recognition. Matlab was the ideal platform to create the tools and interfaces because of its relatively easy development environment and sophisticated digital signal processing tools. I developed this code for her Laboratory.

Important references:

Start Up Procedure:

I. Make sure your Bluetooth hardware and software support Virtual Serial Port Mapping. Basically this allows standard serial port software to send commands through Bluetooth. I had some problems using the default Windows XP drivers with a Zoom USB Bluetooth interface. However, when I installed the software that came with the device all went well. For illustration purposes I will assume the following configuration: Com Port 5, 9600 Baud, 8 bits, 1 stop pit, No Parity, No Flow Control.

II. The first step is to download and install RealTerm (http://realterm.sourceforge.net/). RealTerm is an outstanding terminal program and offered free of charge (see the site for license terms). This tool allows for ROBUST RS-232 communications within Matlab. It also provides a good way to observe communication traffic, which is critical when developing and debugging new code.

In principle you can also use the embedded Matlab RS-232 communications. However, some versions of Matlab have known serial port issues.

III. Test the interface. Turn on the Mindstorm and establish a Bluetooth connection with your PC. Assuming this is done correctly you should now have Com Port 5 (or another port) ready for communication. Run RealTerm and set the following configuration.



Display Tab: Click these options - Hex[space], Half Duplex

Port Tab: Baud-9600, Port 5 (or per your configuration),

Click Open, (Other defaults: Parity-None, 8 bits, Stop Bits - 1, Hardware Flow Control - None, etc.).

Go to the send tab and copy and paste the following byte stream into one of the "Send Numbers" fields: 0x06 0x00 0x80 0x03 0xB8 0x01 0x28 0x00

Hit the "Send Numbers" button to the right of the copied text. If all goes well you should here a brief "beep". If you here the "beep", great! You are half way there. If not, debug to this point before proceeding.

IV. Try the Matlab program. Close down real term. Open matlab and run the m-file: mindstorm.m The file is documented with useful comments. If all goes well you should hear "Ode to Joy" and see motor 0 move back and forth. I developed this m-file in Matlab 6.5 Release 13.

Enjoy!

Greg



function [] = mindstorm();
% Robot Controller for LEGO Mindstorm NXT
% G.Gutt 11/27/2005, Ver 1.0
% greg@mymailbox.org
% Uses Bluetooth serial port emulation/port 5, and RealTerm
% control for robust serial communications. RealTerm controlled via
% ActiveX commands in matlab. Get RealTerm here:http://realterm.sourceforge.net
% This program assumes that you have RealTerm installed and that you have a
% Bluetooth configured as a virtual serial com port (here I use port 5).
% (You can also use the Matlab native serial communication routines if you wish.
% However I have found to not be as robust as RealTerm.)
% See Mindstorm NXT Bluetooth Development documentation to understand the
% structure of Byte Strings. Specifically: Appendix 2-LEGO MINDSTORMS NXT Direct commands.pdf
% http://mindstorms.lego.com/Overview/NXTreme.aspx
% Other useful links and documentation
% Using RealTerm and Matlab: http://realterm.sourceforge.net/realterm_from_matlab.html
% Example of Bluetooth Remote for NXT written in C: http://www.norgesgade14.dk/legoSider/mindstorm_en.html
% Program should play "Ode to Joy" and Move motor 0 back and forth
% Developed Using Matlab 6.5 Release 13
clear; %clear variables
close all; %close open figures
fclose('all'); %close all ports/files
clc; %clear the command window
% Global variable declarations
global ppwrstring; % hex string for
default powr - positive
global npwrstring; % hex string for
default powr - negative
global hrealterm; % activex handle for
realterm RS-232 terminal program
% Open and Configure RealTerm as a Server
disp('GMU Neural Engineering Lab');
disp('Mindstorm NXT Interface Test Program Ver 1.0');
disp('G.Gutt 11/27/2006');
disp(' ');
power = 65; % default power
level (65%) for the nxt motors
ppwrstring=['0x', dec2hex(75)]; % pos pwr, convert
default power level to hex string 0x00 format
npwrstring=['0x', dec2hex(power +128)]; % negative powr string
realtermstart; % start realterm
under ActiveX for RS-232 control
song; % Plays Ode to joy
testmotor; % reset all the
motors to absolute positioning
disp('');
disp('Type "return" to finish program and close down
RealTerm');
keyboard; % Keeps RealTerm and
Matlab active for debugging
invoke(hrealterm,'close'); % Close realterm down
delete(hrealterm); % Delete realterm handle
% Main Program Ends Here
******************************************************
% Sub Functions start here
%******************************************************************************
function [] = twait(t);
% twait(t)
% simple routine to wait is in units of 100ms.
t=t/10;
tic
while(toc<t)
end; % end of twait function
%******************************************************************************
%******************************************************************************
function [] = realtermstart();
% setup real term for active x control of serial port, com5
global hrealterm; % activex handle for realterm RS-232
terminal program
hrealterm=actxserver('realterm.realtermintf'); % start
Realterm as a server
hrealterm.baud=9600;
hrealterm.caption='Matlab Realterm Server';
hrealterm.windowstate=0; %minimized
hrealterm.Port='5'; %select port 5
hrealterm.PortOpen=1; %open the comm port
hrealterm.HalfDuplex=1;
hrealterm.FlowControl=0;
hrealterm.LinefeedIsNewline=0;
hrealterm.DisplayAs=2;
%******************************************************************************
%******************************************************************************
function []= testmotor();
global ppwrstring; % motor power string in 0x00 Hex format
global npwrstring; % neg power string
% resets the tachometer of motor to absolute positioning,
motor 0
%(5th byte in string sets motor to reset: 0,1,2)
% sends command to turn motor 0 forward 25 degrees, pause,
then reverse
sendbytes(['0x0D 0x00 0x80 0x04 0x00 ', ppwrstring, ' 0x07
0x01 0x50 0x10 0x19 0x00 0x00 0x00 0x00']);
twait(20); % wait 200msec)
sendbytes(['0x0D 0x00 0x80 0x04 0x00 ', npwrstring, ' 0x07
0x01 0x50 0x10 0x19 0x00 0x00 0x00 0x00']);
twait(20);
sendbytes(['0x0D 0x00 0x80 0x04 0x00 0x00 0x00 0x00 0x50
0x00 0x00 0x00 0x00 0x00 0x00']); % turn off motor

% Breif byte structure for SetOutputState, NXT command to
move the motor
% Byte 1, number of bytes in the message, message starts
on 3rd byte
% Byte 2, 0x00 format byte, never changes
% Byte 3, Begining of message, 0x80 format byte, never changes
% Byte 4, 0x04, the actual command, never changes
% Byte 5, Motor number 0x00, 0x01 or 0x02
% Byte 6, Power level, -100 to 100
% Byte 7, Mode bit field, see Mindstorm Bluetooth documetation
% Byte 8, Regulation Mode, see Mindstorm Bluetooth
documetation
% Byte 9, Turn Ratio, -100 to 100
% Byte 10, Run State, see Mindstorm Bluetooth documetation
% Byte 11-15, Tacholimit, number of degrees to turn (all
zeros means run forever)


%******************************************************************************
%******************************************************************************
function [] = tone(freq,duration);
% plays a tone. freq is in units of Hz, duration is in
units of 100ms
fstring=dec2hex(freq); % convert to hex
padzeros=4-length(fstring); % number of zeros needed to
pad hex output
for i=1:padzeros
fstring=['0',fstring];
end;
fstring=[' 0x',fstring(3:4),' 0x',fstring(1:2)]; %formats
frequency in 2 byte string 0x00 0x00
%note
bytes are reversed to form Uword
tstring=dec2hex(duration*10); % convert to hex
padzeros=4-length(tstring); % number of zeros needed
to pad hex output
for i=1:padzeros
tstring=['0',tstring];
end;
tstring=[' 0x',tstring(3:4),' 0x',tstring(1:2)]; %formats
tune in 2 byte string 0x00 0x00
%note
bytes are reversed to form Uword
sendbytes(['0x06 0x00 0x80 0x03',fstring, tstring]);

% Breif byte structure for PlayTone, NXT command to play a
tone
% Byte 1, number of bytes in the message, message starts
on 3rd byte
% Byte 2, 0x00 format byte, never changes
% Byte 3, Begining of message, 0x80 format byte, never changes
% Byte 4, 0x03, the actual command, never changes
% Byte 5-6, Frequency of the tone in Hz, Uword
% Byte 7-8, Duration of the tone in ms, Uword
%******************************************************************************
%******************************************************************************

function [] = sendbytes(sendstring);
% Converts String of HEX Numbers in 0x00 form to
Characters and sends
% them out via Realterm
global hrealterm; % activex handle for realterm RS-232
terminal program
byteparse=findstr(sendstring,'0x'); %finds the hex
notation start characters for the string
outstring='';
for i=1:length(byteparse);
bytestring=sendstring(byteparse(i)+2:byteparse(i)+3);
outchar=char(hex2dec(bytestring));
outstring=[outstring, outchar];
invoke(hrealterm,'PutChar',outchar); % send the
characters byte by byte
end;
disp(['Bytes Sent: ', sendstring]);
%******************************************************************************
%******************************************************************************
function [] = song();
% format (freq - Hz, Duration units of 100 ms)
% delay in units of 100ms
% Song: "Ode to Joy"
tone(440,4); twait(4);
tone(440,4); twait(4);
tone(494,4); twait(4);
tone(523,4); twait(4);
tone(523,4); twait(4);
tone(494,4); twait(4);
tone(440,4); twait(4);
tone(392,4); twait(4);
tone(349,4); twait(4);
tone(349,4); twait(4);
tone(392,4); twait(4);
tone(440,4); twait(4);
tone(440,6); twait(6);
tone(392,3); twait(3);
tone(392,3); twait(6);
tone(440,4); twait(4);
tone(440,4); twait(4);
tone(494,4); twait(4);
tone(523,4); twait(4);
tone(523,4); twait(4);
tone(494,4); twait(4);
tone(440,4); twait(4);
tone(392,4); twait(4);
tone(349,4); twait(4);
tone(349,4); twait(4);
tone(392,4); twait(4);
tone(440,4); twait(4);
tone(392,6); twait(6);
tone(349,3); twait(3);
tone(349,3);
%******************************************************************************

back to contents


Books

This is what I used to write the example...


Contact Us

crun@users.sourceforge.net

back to contents