User Tools

Site Tools


nxt_pc_bluetooth

NXT Bluetooth Communication Tutorial

Author: Alvaro Pintado Email: pintado@unlv.nevada.edu
Date: Last modified on 08/21/2016
Keywords: NXT, Bluetooth, BrixCC

Tutorial Prerequisite

  • Windows 10
  • Visual Studio 15
  • Intermediate experience with Cpp programming (Classes, objects, pointers)
  • Basic LEGO NXT Tribot

To begin this tutorial, we will start by demonstrating how to connect your Lego NXT Brick to a computer over Bluetooth. A computer running Windows 10 is required because the code supplied was written using the updated Serial Port class for Windows 10, and therefore is different and incompatible from the Serial Port class that is used in earlier versions of Windows.

Pairing the LEGO NXT Brick with a PC

Note: If your computer does not already have Bluetooth capabilities, you will need to purchase a Bluetooth dongle. Any Bluetooth dongle should work. This tutorial uses the Abe Bluetooth dongle that was made for the Lego NXT; however, these are discontinued and most Bluetooth dongles will work. The Lego NXT already has built in Bluetooth capabilities.

  1. Turn on your LEGO NXT and navigate Main Menu > Bluetooth >Visibility > Visible and Enter on the brick (orange button).(Note: Make sure your brick remains on for the whole process.)
  2. With the Bluetooth dongle plugged into a USB port on your computer, use the search menu to look for “Bluetooth Settings”. Your computer will automatically search for discoverable Bluetooth devices and you should see a device labeled “NXT” in the search results list. Select it and click “Pair”.
  3. Set a pairing key (default “1234” is fine) on your NXT Brick and then enter it on your PC when Windows prompts you for the pairing key

Connecting the NXT Brick with BricxCC

Connecting to Bluetooth via BricxCC may be a pain sometimes. At times it connects easily, other times it won't connect at all.

  1. Launch BrixCC
  2. In the “Find Brick” window, under the “Port” menu you should find an option that starts with “BTH::NXT::” and then is followed by some numbers. Select that choice.
  3. Under the “Brick Type” menu, select “NXT”.
  4. Leave “Firmware” as is (standard) and click “OK”. (Note: At this point, the NXT Brick should display a diamond shape next to the Bluetooth sign in the upper left hand corner of the display to signal that is connected with another Bluetooth device)

Testing the Bluetooth Connection in BrixCC

The Bluetooth connection can be tested in BrixCC once the LEGO NXT has been connected to BrixCC as instructed in the previous section. To do so:

  1. Go to the “Tools” Menu
  2. Select “Brick Joystick”
  3. Configure the Left and Right Motors according to the ports the motors on the Tribot
  4. Control your NXT Robot with the buttons on the Joystick window

Here's a video on what it should look like: https://youtu.be/DZpp9yBOnfc

Sending Direct Commands to NXT over Bluetooth

LEGO includes the LEGO Mindstorms NXT Communication Protocol programmed into all NXT Bricks through the firmware. This communication protocol lies above the Bluetooth communication protocol. It allows for direct commands to the NXT Brick. These commands are structured as byte arrays with hexadecimal encoding.
Note: See documentation for more commands and details about the LEGO NXT Communication Protocol NXT Direct Commands

There are several methods you can you go about sending direct commands. There are programs that allow you to send data to certain COM ports, but most of these are only good for testing and not writing programs that utilize the COM ports. This tutorial will demonstrate the use of the C-plus-plus Serial Port class in Windows that can be used to write programs that send data to COM ports, thus allowing the user to send direct commands to the NXT Brick with a C-plus-plus program.

NXTBluetooth.cpp
/*
Alvaro Pintado
[email protected]
 
Objective: This program opens up a COM port for Bluetooth communication with a LEGO NXT Brick.
The program sends direct commands in the form of byte arrays to be interpreted through the
LEGO Mindstorms NXT Communication Protocol.
 
Notes:
-When writing commands to the LEGO NXT, it is advised that you do not request a response
in order to avoid the time delay of the Bluetooth chipping switching modes from receiving
to transmitting (30 - 60 ms). Unfortunately, I was unable to get that command byte working
as the hexadecimal encoding used in the Serial Port class only encodes integers up to 127.
For my application, the latency was not a significant issue (and won't be in most
applications). If the user would like to experiment with sending commands that are over
127, they should look into changing the encoding for the Serial Port class to a different
encoding scheme such as UTF-8.
 
*/
 
// Necessary header files and namespaces in order to use the Serial Port Class
 
#using <System.dll>
#include <Windows.h>
#include <string>
 
using namespace System;
using namespace System::IO::Ports;
using namespace System::Threading;
using namespace std;
 
int main() {
 
	//Settings for COM Port
	Int32 baudRate = 9600;
	Int32 readTimeout = 500; // time limit for attempting to write to port before exiting
	Int32 writeTimeout = 500; // time limit for attempting to read from port before exiting
 
	//Requests Port Number from user
	Console::Write("Enter NXT Port and press Enter: ");
	String^ portNum = Console::ReadLine();
	Console::WriteLine("Opening COM port ...");
	Console::WriteLine();
 
	//Creates Serial Port object named "NXT" for communication over Bluetooth
	SerialPort^ NXT = gcnew SerialPort(portNum, baudRate, Parity::None, 8, StopBits::One); //creates Serial Port object named "NXT"
	NXT->ReadTimeout = readTimeout; // sets read timeout
	NXT->WriteTimeout = writeTimeout; //sets write timeout
 
									  // Opens port for communication with NXT Brick over Bluetooth
	NXT->Open();
	Console::WriteLine(portNum + " opened for Bluetooth communication.");
	Console::WriteLine();
	Sleep(3000);
 
	// Byte array for a direct command to the NXT Brick to play a tone
	// Byte 00: Least Significant Byte:				12th byte
	// Byte 01: Most Significant Bit:				0th Byte
	// Byte 02: Command response requested:			        0x80 for response, 0x00 for no response
	// Byte 03: Mode byte:				                0x04 for controlling a motor
	// Byte 04: Power set in percentages:	                        0x64 is 100 (hex)
	// Byte 05: Output mode byte:					0x07 for all ON
	// Byte 06: Regulation mode byte:				0x00 for idle
	// Byte 07: Turn ratio:						0x00 
	// Byte 08: Motor run state:					0x20 for running
	// Byte 09 - 13: Tacho limit:					0x00 0x00 0x00 0x00 for running forever
	// See LEGO Mindstorms NXT Communication Protocol documentation for details
	// motorA = 0, motor b = 1, motor c = 1
	wchar_t motorL = [left motor port]; 
	wchar_t motorR = [right motor port];
	cli::array<wchar_t, 1>^ driveL = { 0x0C, 0x00, 0x00, 0x04, motorL, 0x64, 0x07, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00 };
	cli::array<wchar_t, 1>^ driveR = { 0x0C, 0x00, 0x00, 0x04, motorR, 0x64, 0x07, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00 };
	cli::array<wchar_t, 1>^ stopL = { 0x0C, 0x00, 0x00, 0x04, motorL, 0x00, 0x07, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00 };
	cli::array<wchar_t, 1>^ stopR = { 0x0C, 0x00, 0x00, 0x04, motorR, 0x00, 0x07, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00 };
 
 
	//Writes direct command to the NXT Brick to drive two motors forward
	Console::WriteLine("Drive forward for 1 second");
	Sleep(5000);
	NXT->Write(driveL, 0, 14);
	NXT->Write(driveR, 0, 14);
	Sleep(1000);
 
	//Writes direct command to the NXT Brick to stop two motors
	Console::WriteLine("Stop motors");
	Sleep(1000);
	NXT->Write(stopL, 0, 14);
	NXT->Write(stopR, 0, 14);
 
	//Closes the port and exits the program
	Console::WriteLine("Closing program...");
	Sleep(5000);
	NXT->Close();
	exit(0);
}      
  1. Download the Visual Studio 15 project: pc-bluetooth-nxt.7z
  2. In Windows, in Bluetooth Settings, click “More Bluetooth options” and a window should appear
  3. Under the “COM Ports” menu are the current COM Ports assigned to different devices. Find the Outgoing Port for the NXT and take note of it. It will be used later in the tutorial.
  4. Open the sample code and read through the comments to get a general idea of what the program does
  5. Change the values for the motor variables MotorL and MotorR to your NXT Bot's corresponding ports
  6. Build and run the the solution, when prompted, enter the COM port that the NXT Brick is currently utilizing

The NXT should then drive as demonstrated in the video: https://www.youtube.com/watch?v=pnJPoHl106c

Final Words

This tutorial covers Bluetooth communication with the NXT Brick, and gives a sample program written in C++ with Visual Studio 15 to show how to create programs that communicate with the NXT Brick over Bluetooth.

Based off the sample code, the user should be able to implement the Serial Port Class in their own program to control the NXT Brick over Bluetooth for other applications.

nxt_pc_bluetooth.txt · Last modified: 2016/10/23 19:58 by dwallace