[lib] nxj-chat - A bluetooth protocol

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

[lib] nxj-chat - A bluetooth protocol

Postby Alshain » Sun Aug 01, 2010 12:12 pm

I thought I'd share my bluetooth communication protocol with you.
You are free to participate in the project by forking it, or just download the source code.

Code: Select all
http://github.com/alshain/nxj-chat


Readme from Github:

nxj-chat

A simple and customizable bluetooth protocol for leJOS.


Features


  • Provides an object-oriented abstraction to leJOS bluetooth interface.
  • Logically group data in Requests
  • Easily customizable for custom needs
  • Emailish request header with name, subject, module, sender...
  • Answer to one individual received request
  • Send follow-ups to sent requests
  • Block thread execution until reply is received

Motivation

I was working on a project for school with the Lego Mindstorms NXT. Because the proposed language, NXC, didn't appeal to me and I always wanted to learn a little Java, I decided to go with leJOS.

As we were planning our projects, the idea arose that we wanted to have robots that communicate with each other and send data to the computer. During our research on the web we discovered, that an NXT could not have more than one outgoing Bluetooth connection at a time. To compensate for this communication problem, we wanted our Computer to act as master between the NXTs.

I decided to abstract the NXTs raw bluetooth communication and introduce an OO approach. Soon, nxj-chat was born.
Future

Because the project is finished, I have no longer access to the NXTs. Therefore, I am unable to extend this project any further because I lack the possibility to test code. However, feel free to open an issue on github and I will look into it.

That's why I have decided to publish the source on github, so that other people can use or fork it and add their own features.

Unfortunately I haven't been very busy documenting the source code. But I hope you can figure out how it works by looking at the sample application I have provided.

Bottom Line

As I have mentioned, this was my first Java Experience and please excuse the complicated class hierachies...


Old Introduction


A while ago I needed means to communicate between multiple NXTs.
I learned that an NXT can only have one incoming connection and instead of continuous reconnects, I decided to bring the computer as master into the project.

Using this library, you should be able to easily communicate between a PC and NXTs.


So how does it work?
There are three main classes, Uplink, MasterMind, and Slave
  • Uplink establishes a connection from an NXT to a PC. It waits until a PC connects.
  • Slave represents the NXT on the PC. It estlishes the connection from the PC to an NXT.
  • MasterMind manages the Slaves on the PC

You send data between the NXTs and the PC using the Request class.
A request can be "replied" to, and you can wait until a reply is received. That is why it is called nxj-"chat".

Every request has a header, an id, a subject, sender and some other fields. An then of course, there is the data.
When sent, each field is preceded by its length, so the receiver knows how many bytes to put in that field. This is important to send strings and the custom data.

I'll write a more detailed tutorial later perhaps, but I just recently started using github, and I thought I might as well put it up there.

So here you go:
Code: Select all
http://github.com/alshain/nxj-chat


I just imported my entire nxj-chat related SVN tree into github, so the structure is awful at the moment.

Along with the code comes a little sample application that you might use to discover how the code works :) (sorry, I don't have much time atm)

The code structure needs some cleaning up, that is, how the repository is organized. I intend to make this bearable in the next week or so.

The code is split in two parts: PC related, and NXT related. The PC code however needs access to the NXT related code, because they derive from the same base classes.

Looking back at the code, it's not the smarted solution, but it worked for me. Also note, that this was both my first Java Project and also my first NXT one. Since this is just from an actual project of mine, there are some classes that are not complete (Broadcast for instance) and others that are just quick and dirty. The code design is also not always the smartest :)

Let me know what you think and please don't judge me on the folder structure yet, I know it's awful :D
Last edited by Alshain on Thu Aug 05, 2010 6:02 pm, edited 2 times in total.
Alshain
New User
 
Posts: 14
Joined: Sat Jan 02, 2010 1:48 pm

Postby Alshain » Thu Aug 05, 2010 5:56 pm

Okay... I have restructured the sourcecode to make it more accessible I hope, and split the "library" part from the sample project.
Alshain
New User
 
Posts: 14
Joined: Sat Jan 02, 2010 1:48 pm

Postby Alshain » Sat Aug 14, 2010 10:27 am

So... two weeks have passed since I posted this and I'm wondering....

Has nobody replied because there is no interest? Is it, because there is too little documentation? Isn't it clear what this project's goal is?
Alshain
New User
 
Posts: 14
Joined: Sat Jan 02, 2010 1:48 pm

Postby bbagnall » Thu Aug 19, 2010 4:59 pm

Hi Alshain,

The lack of replies might be because your classes are a proprietary way to handle data rather than through a Java standard.

We've got a Bluetooth package in leJOS that allows communication, in the package javax.bluetooth. This allows you to open a connection and get a standard data stream.
User avatar
bbagnall
Site Admin
 
Posts: 392
Joined: Fri Aug 04, 2006 4:03 pm

Postby Alshain » Fri Aug 27, 2010 2:29 pm

Hmm... Basically it's just a wrapper to the datastreams. The goal is to simplify the communication so you don't have to mess that much with sending bits and bytes. Also, if you have different "modules" in your robot that need to communicate with a PC, it should be simplier this way.
Alshain
New User
 
Posts: 14
Joined: Sat Jan 02, 2010 1:48 pm

Postby jdtangney » Tue Aug 31, 2010 5:47 am

Alshain wrote:Hmm... Basically it's just a wrapper to the datastreams. The goal is to simplify the communication so you don't have to mess that much with sending bits and bytes. Also, if you have different "modules" in your robot that need to communicate with a PC, it should be simplier this way.


Sounds quite interesting. I will download and take a look.
--johnt
John Tangney
JD Tangney and Associates
skype: jdtangney
+1-510-579-2800
User avatar
jdtangney
New User
 
Posts: 15
Joined: Mon Nov 30, 2009 6:29 am
Location: Berkeley, CA, USA

Postby Alshain » Thu Sep 02, 2010 4:16 pm

Hey, thanks for trying :)

If you need any help or if there are bugs in the code (I reorganised it and don't have an NXT anymore) feel free to send me a PM so I can give you my MSN account.
Alshain
New User
 
Posts: 14
Joined: Sat Jan 02, 2010 1:48 pm


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 0 guests

more stuff