User Tools

Site Tools


nxt_pc_bluetooth

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
nxt_pc_bluetooth [2016/08/28 17:12] – [Sending Direct Commands to NXT over Bluetooth] alvaropintadonxt_pc_bluetooth [2016/10/23 19:58] (current) dwallace
Line 1: Line 1:
-====== Lego Bluetooth Tutorial ======+====== NXT Bluetooth Communication Tutorial ======
  
 **Author:** Alvaro Pintado Email: <[email protected]> \\ **Author:** Alvaro Pintado Email: <[email protected]> \\
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 "Tools" Menu   - Go to the "Tools" Menu
   - Select "Brick Joystick"    - Select "Brick Joystick" 
-  - Configure the Left and Right Motors accoring to the ports the motors on the Tribot+  - Configure the Left and Right Motors according to the ports the motors on the Tribot
   - Control your NXT Robot with the buttons on the Joystick window   - Control your NXT Robot with the buttons on the Joystick window
  
Line 46: Line 46:
 **Note:** See documentation for more commands and details about the LEGO NXT Communication Protocol {{:alvarop:appendix_2-lego_mindstorms_nxt_direct_commands.pdf| NXT Direct Commands}} **Note:** See documentation for more commands and details about the LEGO NXT Communication Protocol {{:alvarop:appendix_2-lego_mindstorms_nxt_direct_commands.pdf| 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++ 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 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.
  
-<code>+<code c++ NXTBluetooth.cpp>
 /* /*
 Alvaro Pintado Alvaro Pintado
Line 57: Line 57:
 LEGO Mindstorms NXT Communication Protocol. LEGO Mindstorms NXT Communication Protocol.
  
-Notes:  +Notes: 
-1. When writing commands to the LEGO NXT, it is advised that you do not request a response +-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  +in order to avoid the time delay of the Bluetooth chipping switching modes from receiving 
-to transmiting (30 - 60 ms).+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.
  
 */ */
Line 93: Line 98:
  NXT->WriteTimeout = writeTimeout; //sets write timeout  NXT->WriteTimeout = writeTimeout; //sets write timeout
  
- // Opens port for communication with NXT Brick over Bluetooth+   // Opens port for communication with NXT Brick over Bluetooth
  NXT->Open();  NXT->Open();
  Console::WriteLine(portNum + " opened for Bluetooth communication.");  Console::WriteLine(portNum + " opened for Bluetooth communication.");
Line 100: Line 105:
  
  // Byte array for a direct command to the NXT Brick to play a tone  // 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  // See LEGO Mindstorms NXT Communication Protocol documentation for details
- cli::array<wchar_t, 1>tone = { 0x0C, 0x00, 0x80, 0x04, 0xFF, 0x64, 0x07, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00 }; + // motorA = 0, motor b = 1, motor c = 1 
-  + wchar_t motorL = [left motor port];  
- //Writes direct command to play a tone to the NXT Brick + wchar_t motorR = [right motor port]; 
- Console::WriteLine("Play a tone");+ 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);  Sleep(1000);
- NXT->Write(tone, 0, 14);+ 
 + //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  //Closes the port and exits the program
  Console::WriteLine("Closing program...");  Console::WriteLine("Closing program...");
- Sleep(3000);+ Sleep(5000);
  NXT->Close();  NXT->Close();
  exit(0);  exit(0);
Line 120: 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 video: https://www.youtube.com/watch?v=pnJPoHl106c+The NXT should then drive as demonstrated in the video: https://www.youtube.com/watch?v=pnJPoHl106c
 ==== Final Words ==== ==== Final Words ====
  
nxt_pc_bluetooth.1472429567.txt.gz · Last modified: 2016/08/28 17:12 by alvaropintado