Bluetooth Steering WheelPosted: January 19th, 2011 | Author: Karel | Filed under: Uncategorized | 83 Comments »
For my next project I want to take the Android controlled robot one step further. In my previous post, I used the ability of the Blueterm program to send characters over Bluetooth. So, I didn’t had to do any Android programming. The Blueterm program had all the functionality I needed. But since I’d like to learn how to build an Android app, here is my next challenge. The want to hold the phone in front of me as if I’m holding a steering wheel. If I tilt the phone forward I want the robot drive forward. If I tilt the phone backward the robot needs to drive backward. If I turn left the robot turns left and so on … Therefore, I want to build an Android app that sends the data of its acceleration sensor over Bluetooth to the Dwengo bot. From this data the orientation of the phone with respect to earth can be determined, and from this orientation it must be possible to make the robot behave as I just described.
Ok guys its working. The prove can be found below.
The Source Code
The code is available for download on the bottom of this page. The file contains both the code for the android app and the code for the Dwengo board. The android project can simply be imported (File> Import> General> Existing Projects into Workspace) in eclipse if you have installed the ADT plugin.
Building an android app is not very difficult. If it’s your first time you should visit the android developer website. It contains a lot of good information on how to build an android app. Just take some of the the tutorials and you’re on your way.
How does it work?
On the highest level, the robots requests the next command by sending the character ‘r’ to the phone over the Bluetooth connection. The phone replies by either sending the character ‘s’ (stop) when driving is deactivated or the character ‘c’ followed by the x, the y and the z components of the gravity vector. The vector is normalized and rescaled so that each component fits in the range -128 to 127. In this way each component can be send as one byte. For the details please download the code and take a look at the comments. If things stay unclear you should leave a comment on the bottom of the page.
I spend most of my effort on the BluetoothSPPConnection class in the hope that it can be easily reused in one of your bluetooth experiments. You simply create an object of the class and open a connection with a device using the open(device) method. Next, you create an object that implements the BluetoothSPPConnectionListener interface. The interface contains methods that are called when the status of the connection changes (onConnecting(), onConnected(), onConnectionFailed() and onConnectionLost()). To send characters from the phone to the module you simply call the write(byte buffer) method, where buffer contains the data that should be send. When data is send by the module to the phone, the bluetoothWrite(nmbrBytes, buffer) method is called by the system, where nmbrBytes is the number of bytes that is received and buffer contains these bytes. If every thing is right, all thread and message passing stuff is handled by the BluetoothSPPConnection object.
If you discover a bug please leave a comment on this page. I will fix it as soon as possible.
That’s it for now. Hope to see you soon!
PS: If you like this post, please click one (or all ) of the social network buttons below. Thanx!
BluetoothSteeringWheel.zip (3429) (Please like before you download? Thanx!)