
Date: 6.12.2007
Duration of activity: 3 hour
List of participants: Alessandro, Daniela, Samuele
Goal: trying to get NXT bricks to communicate each other via Bluetooth©.
Plan:
- get used with leJOS Bluetooth API
- activate bluetooth on NXT brick and assign a name to each robot
- get two NXT bricks to communicate via Bluetooth
- (only if the above test fail) get the NXT brick to communicate with PC
The package that provide all the NXT communication classes is lejos.nxt.comm.
Particularly we are interested in the classes Bluetooth, BTRemoteDevice, BTConnection.
These classes are poorly documented, so we expect for hard work...
It follows a brief description of each class:
- Bluetooth - it is the main class of this package and it provides all the methods to activate the own bluetooth device, to discover other devices, create connections and communicate with them;
- BTRemoteDevice - the methods in this class allows to change the parameters of the robot connected with the bluetooth: the address, the friendly name and the class which the device belongs to;
- BTConnection - it represents a connection with another device: using a BTConnection instance you can open streams to write and read data from that device (on the documentation you can read that this classes are "not yet implemented", but we used them with success!).
Then we try to assign a name to both of the robots, using the function Bluetooth.setFriendlyName: first of all the function gets as argument a byte array, so we build an function to convert the string into a byte array (all the utility functions and the constant variable are in the file BTUtils.java).
Applying the setFriendlyName using as input the simple string converted into an array we get a strange exception during the execution of the program...ok, no panic...let's go into the source code of the function:
public static void setFriendlyName(byte[] name) {
byte[] reply = new byte[32];
byte[] msg = new byte[32];
friendlyName = name;
msg[0] = MSG_SET_FRIENDLY_NAME;
for(int i=-0;i<16;i++) msg[i+1] = name[i];
...
We just figure out that there is a magic number that the argument must comply with.
After the appropriate modification to the function that convert the string, we check if the robot changed its name: yes, it works!
To trick to know the name of the robot is to search it from another device: on the screen of this one will appear the name of all the found devices.
Subplan:
- send an integer from a robot to another one
- send a real time produced stream of data
Thanks to the streams we are allowed to send any type of data: boolean, byte, char, short, int, String; all our test will be based on byte type.
Obviously we don't succeed at the first trying, but fortunetly only because of two small errors: we forgot to flush the stream after sending the byte and more stupid to refresh the screen on the receiver. After fixed these problems, it works, so we can try the second step of our subplan.
The idea is to generate a stream of data produced by a sensor of a robot, send this stream via bluetooth to the other robot and control a motor of this one using the values received. We are using the MotorPort abstraction, so at the first trying we don't perceive any movement because the scales of values are different: a normalization on the values received makes the system working.
Files:
BTTransmitterP.java
BTReceiverP.java
Conclusion:
We met the first deadline!
We succeeded to get two NXT sending data each other. This will be useful when the robots will have to communicate each other the positions of the sound sources detected. For this purpose other tests will be necessary, i.e. sending a "big" data amount and store it in one of the robot, but the goal of this session was just to prove the feasibility of the idea.
References:
- leJOS Forum - http://lejos.sourceforge.net/forum/
- leJOS API (particularly the section about the communication package) - http://lejos.sourceforge.net/p_technologies/nxt/nxj/api/lejos/nxt/comm/package-summary.html
1 comment:
I have problems :S i try to compile BTTransmitterP.java but it says it has an error in line 25: cannot find symbol
symbol: class BTRemoteDevice
location: class BTTransmitterP,
i guess its because the classes used are "not yet implemented", or i dunno i need help i have to connect two nxts but i didnt find anything about connecting two nxt only with the pc :( pls help me my mail chato_122@yahoo.com
Post a Comment