Robot controlled by Android Phone

We decided a few weeks ago to use the Android as a remote controller instead of building a remote control from scratch. We had started developing our android controller with Google Labs App Inventor http://www.googlelabs.com/show_details?app_key=agtnbGFiczIwLXd3d3IUCxIMTGFic0FwcE1vZGVsGPOaIgw but one of the guys over at LMR did the same thing and put it on Android Market for $2. So we decided to use Chris the Carpenter’s http://letsmakerobots.com/node/28202 RocketBot Controller.

After installing the app,  It was pretty easy to pair with the Android phone but getting it connected and working took a little thought, but not too difficult.

Check the video out on youtube

(for some reason, wordpress wouldn’t embed it) 

http://www.youtube.com/watch?v=AeP8vU1Uoww

I’ll post more of the detail in a follow-up post as you’ll need to change the baud rate on the BlueSmirf Silver as the Motorola Droid choked at 115,200k baud. You can see in the code below we set the BlueSmirf Silver baud at 38,400k baud.

We choose the Sparkfun BlueSmirf Silverhttp://www.sparkfun.com/products/10269 as our bluetooth adapter for Homer. It’s about 1/2 the cost of the gold version and was fine for our indoor application. (be aware that if you google bluesmirf silver, you will be directed to bluesmirf gold which is about double the cost ..)

We decided to strip homer down to essentials to configure bluetooth. We anticipated a

lot of testing and retesting with a lot of battery exhaustion and wanted to focus only on gettting bluetooth to work.

Here’s the Arduino code:

/* Android Controlled Robotshop Rover
Author - Jim Winburn > Appiphania.com
uses Chris Carpenter's RocketBrand Android Controller
Note : Directional Function only in this example.
D1 - Forward
D2 - Left
D3 - Right
D4 - Reverse
D5 - Stop
*/
#include <NewSoftSerial.h>
#define BlueSmirf_RxPin 2
#define BlueSmirf_TxPin 3

NewSoftSerial nss(BlueSmirf_RxPin,BlueSmirf_TxPin); 

int E1 = 6; //M1 Speed Control
int E2 = 5; //M2 Speed Control
int M1 = 8; //M1 Direction Control
int M2 = 7; //M2 Direction Control

int leftspeed;
int rightspeed;

int i;
int qualifier;
int dataByte;

void setup()
{
//Serial.begin(9600);// on for debug
nss.begin(38400); // best baud for Motorola Droid tested

// set up motors

for(i=5;i<=8;i++)
pinMode(i, OUTPUT);

// Motor tuning - since this is a track vehicle
// and motors are never exact, they must be tuned ...
  //255 is maximum speed

leftspeed = 255; //adjust to balance motors
rightspeed = 195;//adjust to balance motors

}
void loop()
{

if(nss.available()>1)
{
qualifier=nss.read();
dataByte=nss.read();
}

/* uncomment below to debug with serial monitor
Serial.print(byte(qualifier)); //will display "D" instead of "68"
Serial.print(" : ");
Serial.print(dataByte);
Serial.println("");
delay(1000);
*/

switch (qualifier)
{

case 'D':

      switch (dataByte)
      {

        case 1:  //forward

        forward (leftspeed,rightspeed);
        break;

        case 2: //left

        left (leftspeed,rightspeed);
        break;

        case 3: //right

        right (leftspeed,rightspeed);
        break;

        case 4: //reverse

        reverse (leftspeed,rightspeed);
        break;

        case 5: //stop
        stop ();
        break;

      }
}

qualifier=0;
dataByte=0;

}
// functions

void stop(void) //Stop
{
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
}
void forward(char a,char b) //Forward
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void reverse (char a,char b) //Reverse
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
void left (char a,char b) //Left
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void right (char a,char b) //Right
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}