Showing posts with label communication protocols. Show all posts
Showing posts with label communication protocols. Show all posts

Saturday, July 31, 2010

NXT Bluetooth Tutorial

This blog is the first of many as a tutorial on using the NXT with Bluetooth.  First, I’ll speak to the issue of programming the robot because it’s the easiest to understand.  The following program is intended to be run on the NXT.  This code will not run on your PC.

The steps for your NXT are quite simple

1.    Declare all of the class’s instance variables.
2.    Display a message on the NXT’s LCD display that the NXT is waiting for a connection.  I also like the NXT to sound two beeps in case the NXT is not near enough to read the LCD.
3.    Make a blocking call to wait for a Bluetooth connection from your PC.  I'll explain in future blogs how to make the PC initiate the Bluetooth connection.
4.    Once the connection is made, set up the input and output streams.  I also like the NXT to sound a tune so that I know that the connection is ready.
5.    Next, send the NXT into a while loop controlled by a Boolean flag.  The stuff inside the while loop is what you want the NXT to do. 
6.    When your PC signals the NXT that it is through with the Bluetooth connection, the NXT should set the Boolean flag mentioned in step 5 to false.  Consequently, the while loop will cease.
7.    Close the input and output buffers to close the Bluetooth connection.

Step 1:  Declare all of the class’s instance variables

import lejos.nxt.*;
import lejos.nxt.comm.*;
import lejos.robotics.navigation.*;

import java.io.*;


public class Robot {   
   
    public static void main(String [] args) throws Exception
    {
      int[] command = new int[3];
      int[] reply = new int[8];
      boolean keepItRunning = true;
      String connected = "Connected";
      String waiting = "Waiting...";
      String closing = "Closing...";
      DataInputStream dis = null;
      DataOutputStream dos = null;
      BTConnection btc = null;
      UltrasonicSensor sonic = new UltrasonicSensor(SensorPort.S1);
      Pilot piloting = new TachoPilot(56.0f, 121.0f, Motor.C, Motor.A);
       

The two integer arrays are used to hold the commands sent by the PC to the NXT, and the reply is used to hold the NXT's reply to the command.

The Boolean flag is used to indicate if the while loop is to keep looping.

The strings are used to display status messages on the LCD screen.

The streams are used to facilitate communication between the PC and the NXT.

The ultrasonic sensor and pilot objects are used within the while loop to control the NXT.

Step 2:  Display a Message and Sound two Beeps

        LCD.drawString(waiting,0,0);
        LCD.refresh();
       
        Sound.twoBeeps();

These simple commands put the words “Waiting…” on the NXT’s LCD and sounds two beeps.

Step 3:  Wait for a Bluetooth Connection

        btc = Bluetooth.waitForConnection();
       
Remember that this is a blocking method call.  The NXT will not go beyond this step until it makes a Bluetooth connection.  I’ll explain in future blog posts how the PC initiates the Bluetooth connection.  If you try to run this program on your NXT, it will stop at this point and go no further.

Step 4:  Set up the Streams

        dis = btc.openDataInputStream();
        dos = btc.openDataOutputStream();
       
        Sound.beepSequenceUp();

With the Bluetooth connection established the streams can be opened.  I make the NXT play a tune as an audible reminder that a Bluetooth connection has been successfully made.

Steps 5 and 6:  Go into a while loop

        while (keepItRunning){
   
        // put your stuff here including code to change the Boolean
        // flag to false once the PC signals to end the connection
               
        }

Step 7:  Close the Streams and the Bluetooth Connection

        dis.close();
        dos.close();
        Thread.sleep(100); // wait for data to drain
        LCD.clear();
        LCD.drawString(closing,0,0);
        LCD.refresh();
        btc.close();
        LCD.clear();
  } // end of main

} // end of class Robot

Closing the streams and the Bluetooth connection is very simple.  I also have status messages displayed on the NXT’s LCD.

In my next blog entry I’ll elaborate on Steps 5 and 6; that is, the “stuff” you want your NXT to do.

Go questions?  Post a comment.  I’ll reply.

Sunday, January 4, 2009

The Communication Protocol

In communicating with the NXT robot, we want to keep our communication protocol as simple as possible. Through simplicity, the PC can direct more of its attention to the problem of Monte Carlo Localization (MCL), and less of its time on Bluetooth connection. The PC sends its command in the form of three integers. The first integer is the command itself, an integer from 0 to 4. The two integers that follow the command are optional parameters.

The robot’s reply is always eight integers. We need eight integers to accommodate the eight echoes that we obtain from the ultrasonic sensor.

There are five cases to consider:

Description: The user requests the battery voltage
Command 0, 0, 0
Reply millivoltage, 0, 0, 0, 0, 0, 0, 0


Description: The user asks for the ultrasonic sensor to ping and report back
Command 1,0,0
Reply A series of 8 integers being the echo values


Description: The user requests that the robot move forward/backward
Command 2,distance,speed
Reply A series of 8 integers being the echo values


Description The user requests that the robot turn left/right
Command 3,angle,speed
Reply A series of 8 integers being the echo values


Description The user requests that the Bluetooth connection be terminated
Command 4,0,0
Reply 255,255,255,255,255,255,255,255


The responsibility for sending the command to the robot and receiving a reply from it is shared between the Event Handler and BTCommPC classes. Upon detecting that an action event has been fired on the GUI, it is the Event Handler’s responsibility to gather the necessary information, compose the three-integer command and pass a reference to the integer array to the BTCommPC class. BTCommPC is responsible for sending the command one integer at a time to the robot and await a reply. Once a reply starts being sent, BTCommPC reads each integer, one at a time, placing them in an eight-integer array. BTCommPC then passes a reference to the array to the Event Handler.

Once the Event Handler receives a reference to the reply array, it is handler’s responsibility to disseminate it. Usually, this involves passing values back to the GUI panels and passing the ultrasonic sensor readings to the MCL class so that the MCL class can update its statistics. This command/reply process continues until the user requests that the Bluetooth connection be terminated either explicitly by pressing the disconnect button, or implicitly by terminating the program.

The robot’s program is complementary to BTCommPC’s algorithm. At first, the robot waits for a Bluetooth connection. Once a connection is made, it waits to receive its three-integer command. Once the command is received, the robot moves or turns, if necessary, and then sends back its eight-integer reply one integer at a time. The robot then waits for its next command. If the robot is commanded to terminate its Bluetooth connection, the robot sends back its acknowledgement, disconnects, and its program terminates on the brick.

More implementation to come.