Saturday 31 March 2012

BlueTooth Controlled TriBot Base

I used my TriBot-based robot base to play with Android and Bluetooth. Goal: controlling the robot base with an Android phone.

The robot now look like that:

And the result, where you can see that the robot can actually turn and slide in all directions (left pad for sliding, right pad for turning):



How to connect Android to an Arduino?

I bought this very cheap serial/BlueTooth board (CSR Bluetooth chip/CSR BlueTooth chip) from Iteadstudio:


To connect the Android phone to the Arduino using this BlueTooth module, I used the Amarino library.

Setting up the BT module

The HC-06 BT Module is setup at 9600 bps by default. Amarino required it to run at 57200 Bps.

To change the serial connection rate, upload this code to your Arduino, disconnect it from your PC, connect the BlueTooth module to the serail port of your Arduino and restart it.


void setup() {
  Serial1.begin(9600);
  Serial1.print("AT-BAUD7");  
}


Using the Amarino Arduino library

The code is strongly inspired by the MultiColorLamp example code.
The two interesting additional points are:
  • Each message is constituted with  values (one for each motor, from -255 to 255). Using several values in a message is undocumented, but straightforward
  • I included a timer to stop motors after half a second without any incoming message. Usefull to prevent  accidents when the Bluetooth get disconnected. Of course it implies that the Android application send messages constantly during to move the robot.



#include 

MeetAndroid meetAndroid;

int MOTOR_ENABLE[] = {5,6,7};
int MOTOR_DIR[] = {4,3,2};
int STOP_DELAY=500;

unsigned long lastSignalTime;

int LED = 13;

void setup() {                
  Serial.begin(57600); 
  meetAndroid.registerFunction(setMotor, 'm');

  for (int i=0;i<3;i++) {
    pinMode(MOTOR_ENABLE[i], OUTPUT);
    pinMode(MOTOR_DIR[i], OUTPUT);
  }
  pinMode(LED, OUTPUT);
}

void loop() {
  if (millis()-lastSignalTime>STOP_DELAY)
    stopMotor();  
  meetAndroid.receive(); 
}

void setMotor(byte flag, byte numOfValues)
{
  lastSignalTime=millis();  
  digitalWrite(LED,HIGH);
  int values[3];
  meetAndroid.getIntValues(values); 
  for (int i=0;i<3;i++) {
    int v=values[i];
    if (v>0) {
      digitalWrite(MOTOR_DIR[i],1);
      analogWrite(MOTOR_ENABLE[i], v);
    } else {
      digitalWrite(MOTOR_DIR[i],0);
      analogWrite(MOTOR_ENABLE[i], -v);
    }
  }  
  digitalWrite(LED,LOW);
}

void stopMotor() {
  for (int i=0;i<3;i++)   
      analogWrite(MOTOR_ENABLE[i],0);  

}


Using the Amarino Android Libary

I will not go into the details of the Android application, strongly inspired by MultiColorLamp as well. It displays a joypad, convert thumb positions into motor values (the tricky part), and send them to the Arduino by BlueTooth, using:


private void callAndroid() {
  int[] message = getMotorValues(leftJoyX,leftJoyY,rightJoyX,rightJoyY);
  Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'm', message);
}

Where getMotorValues returns an array of 3 integers.