Archive for September, 2010

Using Android as robot remote control

The flea market car / robot saga continues… Controlling the robot from my laptop via Bluetooth was cool
, but running after the robot with the laptop in my hands kind of diminished the James Bond – feeling of it. Therefore, I decided that my first application I would create for my brand new Android smartphone would be a remote control for the robot!

Luckily, the online developer information (available at http://developer.android.com), provides a wealth of information for getting started. What was more, I actually found a sample application there, called BluetoothChat, that almost did exactly what I wanted to do. The BluetoothChat application makes a connection to a Bluetooth device (this will be the robot) and it allows to send text to it once connected (the text will be the robot commands).

Google provides a complete development environment for developing Android applications, which can be downloaded at http://developer.android.com/sdk/index.html. Just follow the instructions over there to install the platform and create a “hello world” application. Connecting your phone over USB to the development computer allows you to debug / run the application on the phone. On my Ubuntu laptop, I have to restart the adb server as root after connecting the phone via USB:

(while in folder /android-sdk-linux_86/tools)
./adb kill-server
sudo ./adb start-server
./adb devices

Now let’s skip to the application. I wanted to change a few things to the BluetoothChat application:

  • actually get it connected to a Bluetooth device, because this wasn’t working right away.. ūüė¶
  • after connecting to the robot over Bluetooth, I wanted to automatically start my PING – PONG handshake (Android says “PING” and the robot answers with “PONG”, which indicates the connection is established¬†successfully¬†and the robot should start listening for commands)
  • Instead of typing the commands in the text field I wanted some big buttons (forward, backward, left, right, …)

Connecting the BluetoothChat example to a Bluetooth device

I first tried connecting the BluetoothChat Android application to Bluetooth on my laptop, but it didn’t work. Whatever I tried, I always got a “service discovery failed” exception on the following line of code in BluetoothChatService.java:

tmp = device.createRfcommSocketToServiceRecord(MY_UUID);

After a long time trying to solve this, I found this site, which suggested to change the line of code to this code:

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, 1);

And this worked! Also, note that you have to connect at 57600 baud to the Android Bluetooth.

Do a handshake once connected

Once connected to the BlueSMIRF on the robot, the following should happen:

  • Android sends “PING\n” to robot
  • Robot receives this and sends “PONG\r\n” to Android
  • Android receives this, and knows the connection is succesfully established.
  • Now Android can send commands like “D,70,1” to set the servo to 70¬į and the drive motor to forward motion.

I changed the run() method of the ConnectedThread to do the handshake once connected: see code.

Big buttons that send commands to the robot

And now for the fun part.. I wanted to change the layout to have nine big buttons which would send the robot in a certain direction:

I used the example at this site to create the big button layout. Once a button is pressed, it will send a command over Bluetooth to the robot which will make it ride in that direction:

        mBRButton = (Button) findViewById(R.id.button_br);

        mBRButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                // Send a message using content of the edit text widget

                String message = "D," + right + "," + backward + "\r\n";

                sendMessage(message);

            }

        });

And now a little movie to prove that it works!

The source code can be downloaded here.

edit: latest version of code is now available via https://github.com/kr3l/rommelrubot

 

September 4, 2010 at 5:18 pm 14 comments


Feeds

Articles to be written…

Twitter – kr3l

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

my del.icio.us

RSS Google Reader Shared Stuff

  • An error has occurred; the feed is probably down. Try again later.

RSS Listening to..

  • An error has occurred; the feed is probably down. Try again later.