nxt_pc_bluetooth
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
nxt_pc_bluetooth [2016/08/28 16:35] – [Sending Direct Commands to NXT over Bluetooth] alvaropintado | nxt_pc_bluetooth [2016/10/23 19:58] (current) – dwallace | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
**Author:** Alvaro Pintado Email: < | **Author:** Alvaro Pintado Email: < | ||
Line 9: | Line 9: | ||
* Windows 10 | * Windows 10 | ||
* Visual Studio 15 | * Visual Studio 15 | ||
- | * Intermediate experience with C++ programming (Classes, objects, pointers) | + | * Intermediate experience with Cpp programming (Classes, objects, pointers) |
* Basic LEGO NXT Tribot | * Basic LEGO NXT Tribot | ||
Line 36: | Line 36: | ||
- Go to the " | - Go to the " | ||
- Select "Brick Joystick" | - Select "Brick Joystick" | ||
- | - Configure the Left and Right Motors | + | - Configure the Left and Right Motors |
- Control your NXT Robot with the buttons on the Joystick window | - Control your NXT Robot with the buttons on the Joystick window | ||
Line 44: | Line 44: | ||
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. \\ | 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 details on other commands and details about the LEGO NXT Communication Protocol | + | **Note:** See documentation for more commands and details about the LEGO NXT Communication Protocol {{: |
- | 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++ 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++ program. | + | 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 |
+ | |||
+ | <code c++ NXTBluetooth.cpp> | ||
+ | /* | ||
+ | Alvaro Pintado | ||
+ | [email protected] | ||
+ | |||
+ | Objective: This program | ||
+ | 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, | ||
+ | as the hexadecimal encoding used in the Serial Port class only encodes integers up to 127. | ||
+ | For my application, | ||
+ | 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 < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | using namespace System; | ||
+ | using namespace System:: | ||
+ | using namespace System:: | ||
+ | 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:: | ||
+ | String^ portNum = Console:: | ||
+ | Console:: | ||
+ | Console:: | ||
+ | |||
+ | //Creates Serial Port object named " | ||
+ | SerialPort^ NXT = gcnew SerialPort(portNum, | ||
+ | NXT-> | ||
+ | NXT-> | ||
+ | |||
+ | | ||
+ | NXT-> | ||
+ | Console:: | ||
+ | Console:: | ||
+ | Sleep(3000); | ||
+ | |||
+ | // Byte array for a direct command to the NXT Brick to play a tone | ||
+ | // Byte 00: Least Significant Byte: | ||
+ | // Byte 01: Most Significant Bit: 0th Byte | ||
+ | // Byte 02: Command response requested: | ||
+ | // Byte 03: Mode byte: | ||
+ | // Byte 04: Power set in percentages: | ||
+ | // Byte 05: Output mode byte: | ||
+ | // Byte 06: Regulation mode byte: | ||
+ | // Byte 07: Turn ratio: | ||
+ | // Byte 08: Motor run state: | ||
+ | // Byte 09 - 13: Tacho limit: | ||
+ | // 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:: | ||
+ | cli:: | ||
+ | cli:: | ||
+ | cli:: | ||
+ | |||
+ | |||
+ | //Writes direct command to the NXT Brick to drive two motors forward | ||
+ | Console:: | ||
+ | Sleep(5000); | ||
+ | NXT-> | ||
+ | NXT-> | ||
+ | Sleep(1000); | ||
+ | |||
+ | //Writes direct command to the NXT Brick to stop two motors | ||
+ | Console:: | ||
+ | Sleep(1000); | ||
+ | NXT-> | ||
+ | NXT-> | ||
+ | |||
+ | //Closes the port and exits the program | ||
+ | Console:: | ||
+ | Sleep(5000); | ||
+ | NXT-> | ||
+ | exit(0); | ||
+ | } | ||
+ | </ | ||
- Download the Visual Studio 15 project: {{: | - Download the Visual Studio 15 project: {{: | ||
Line 52: | Line 150: | ||
- 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. | - 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. | ||
- Open the sample code and read through the comments to get a general idea of what the program does | - Open the sample code and read through the comments to get a general idea of what the program does | ||
+ | - Change the values for the motor variables MotorL and MotorR to your NXT Bot's corresponding ports | ||
- Build and run the the solution, when prompted, enter the COM port that the NXT Brick is currently utilizing | - Build and run the the solution, when prompted, enter the COM port that the NXT Brick is currently utilizing | ||
- | The NXT should then play a tone as demonstrated in the linked | + | The NXT should then drive as demonstrated in the video: https:// |
- | https:// | + | |
==== Final Words ==== | ==== Final Words ==== | ||
nxt_pc_bluetooth.1472427343.txt.gz · Last modified: 2016/08/28 16:35 by alvaropintado