Monday 15 April 2013

Use Android smartphone to control two wheels robot with Arduino via Bluetooth

3) Android programming part using Processing

For the programming for my android smart phone, I've tried using few ways to establish the bluetooth connection between Arduino and my smart phone. I've tried to use few library to do the programming part, like sweetbt and btserial library, but these two libraries don’t function well as my smart phone could not successfully connect to the device via Bluetooth. The btserial library can be download at this website but the .jar file is seem to be missing and waiting for rebuild.

So I tried to change to eclipse, although I manage to download the application into my smart phone(sony Xperia X10i), but the coding for eclipse is much more harder compared to processing as I totally don’t have the basic for android programming it will be very hard for me to use the eclipse. So I try to find another alternative way to do the android programming. But lastly, I managed to find a library named Ketai, for processing, which works perfectly to communicate with the Arduino Bluetooth shield via Bluetooth, and the coding is much easier than eclipse. 

You can download the Ketai library from this website and place it in your sketch "library" file : https://code.google.com/p/ketai/downloads/list

Before download the codes into your android smartphone via processing, pls do enable the sketch permission of BLUETOOTH and BLUETOOTH ADMIN via the "Android" tab. If you forgot to tick this 2 things, the apps downloaded into your smart phone cannot be launched.


I've edited the code base on this original codes, and it is very helpful for me:)
Below will be the final coding of the android processing code with implemented Ketai library function:

//required for BT enabling on startup
import android.content.Intent;
import android.os.Bundle;
import ketai.net.bluetooth.*;
import ketai.ui.*;
import ketai.net.*;
PFont fontMy;                                    //declaring font
boolean bReleased = true;       //no permament sending when finger is tap
KetaiBluetooth bt;                  // Create object from BtSerial class
boolean isConfiguring = true;
String info = "";
KetaiList klist;
ArrayList devicesDiscovered = new ArrayList(); //store in array the discovered device
boolean rectOver = false;
int rec = 0;

// The following code is required to enable bluetooth at startup.
void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
bt = new KetaiBluetooth(this);//create the BtSerial object that will handle the connection
}

void onActivityResult(int requestCode, int resultCode, Intent data)
 {
bt.onActivityResult(requestCode, resultCode, data);
}//to show the discovered device

void setup()
{
size(displayWidth, displayHeight); //size of my phone screen
smooth();
frameRate(10);                        //the frame rate of my screen
orientation(PORTRAIT);        //vertical
bt.start();                                 //start listening for BT connections
isConfiguring = true;               //at my phone start select device…
fontMy = createFont("SansSerif", 20); //font size
textFont(fontMy);
}

void draw()
{
//at app start select device
if (isConfiguring)

{
ArrayList names;

//create the BtSerial object that will handle the connection
//with the list of paired devices
klist = new KetaiList(this, bt.getPairedDeviceNames());

isConfiguring = false;                         //stop selecting device
}

else
{
color a = color(255,0,0);                      //the color for each button of the interface
color b = color(255,0,0);
color c = color(255,0,0);
color d = color(255,0,0);
color e = color(165,8,27);
color f = color(165,8,27);
color g = color(165,8,27);
color h = color(165,8,27);
color i = color(18,21,121);
color j = color(255,255,255);
color k = color(255,255,255);
color l = color(0,0,0);
update(mouseX, mouseY);                 //update our finger point at where of the screen
background(45,12,137);                      //background color

if((mousePressed)&&(rectOver)&&(rec==1))  //when we click the button’s color change  
                                                                              accordingly with different color code
{      c = color(10,237,26);
}
else if ((mousePressed)&&(rectOver)&&(rec==2))
{       d = color(10,237,26);
}
else if ((mousePressed)&&(rectOver)&&(rec==3))
{       a = color(10,237,26);
}
else if ((mousePressed)&&(rectOver)&&(rec==4))
{       b = color(10,237,26);
}
else if ((mousePressed)&&(rectOver)&&(rec==5))
{       j = color(74,182,252);
}
else if ((mousePressed)&&(rectOver)&&(rec==6))
{      k = color(74,182,252);
}
else if ((mousePressed)&&(rectOver)&&(rec==7))
{      e = color(10,237,26);
}
else if ((mousePressed)&&(rectOver)&&(rec==8))
{      f = color(10,237,26);
}
else if ((mousePressed)&&(rectOver)&&(rec==9))
{      g = color(10,237,26);
}
else if ((mousePressed)&&(rectOver)&&(rec==10))
{      h = color(10,237,26);
}
else if ((mousePressed)&&(rectOver)&&(rec==11))
{      l = color(255,255,255);
}


if ((rec == 2) && (rectOver)&&(mousePressed) && (bReleased == true)) { 
// If our finger is on the square,
    byte[] data = {'w'};           // send w to arduino when we click the button 2
    bt.broadcast(data);              //send with bt
    bReleased = false;               // send data for once until next time we click the button again
  }
if ((rec == 1) && (rectOver)&&(mousePressed) && (bReleased == true)) { 
    byte[] data = {'s'};           // send s to arduino when we click the button 1
    bt.broadcast(data);              //send with bt
    bReleased = false;
  }
  if ((rec == 3) && (rectOver)&&(mousePressed) && (bReleased == true)) {
    byte[] data = {'a'};           // send a to arduino when we click the button 3
    bt.broadcast(data);              //send with bt
    bReleased = false;
  }
  if ((rec == 4) && (rectOver)&&(mousePressed) && (bReleased == true)) { 
    byte[] data = {'d'};           // send d to arduino when we click the button 4
    bt.broadcast(data);              //send with bt
    bReleased = false;
  }
  if ((rec == 5) && (rectOver)&&(mousePressed) && (bReleased == true)) { 
    byte[] data = {'l'};           // send l to arduino when we click the button 5(ellipse)
    bt.broadcast(data);              //send with bt
    bReleased = false;
  }

  if ((rec == 6) && (rectOver)&&(mousePressed) && (bReleased == true)) { 
    byte[] data = {'k'};           // send k to arduino when we click the button 6(ellipse)
    bt.broadcast(data);              //send with bt
    bReleased = false;
  }
    if ((rec == 7) && (rectOver)&&(mousePressed) && (bReleased == true)) { 
    byte[] data = {'q'};           // send q to arduino when we click the button 7
    bt.broadcast(data);              //send with bt
    bReleased = false;
  }
    if ((rec == 8) && (rectOver)&&(mousePressed) && (bReleased == true)) { 
    byte[] data = {'e'};           // send e to arduino when we click the button 8
    bt.broadcast(data);              //send with bt
    bReleased = false;
  }
    if ((rec == 9) && (rectOver)&&(mousePressed) && (bReleased == true)) { 
    byte[] data = {'z'};           // send z to arduino when we click the button 9
    bt.broadcast(data);              //send with bt
    bReleased = false;
  }
    if ((rec == 10) && (rectOver)&&(mousePressed) && (bReleased == true)) { 
    byte[] data = {'c'};           // send c to arduino when we click the button 10
    bt.broadcast(data);              //send with bt
    bReleased = false;
  }


      if ((rec == 11) && (rectOver)&&(mousePressed) && (bReleased == true)) { 
    byte[] data = {'t'};                 // send t to arduino when we click the button 3
    bt.broadcast(data);              //send with bt
    bReleased = false;
  }
 
 
if((rectOver)&&(mousePressed == false)&& (bReleased == false))
  {                    //when our finger move up from the button, send stop command to arduino
    byte[] data = {' '};             
    bt.broadcast(data);
    bReleased = true;
  }

  fill(a);                                                //fill each area of button with the color declared above
  stroke(162); //the shape covered with a grey color line
  triangle(200,250,250,150,300,250);              //draw the triangle with the coordinates
   fill(b);
   triangle(200,600,250,700,300,600);
   fill(c);
   triangle(150,375,50,425,150,475);
    fill(d);
    triangle(350,375,450,425,350,475);
    fill(e);
    triangle(425,240,325,290,400,340);  //325
    fill(f);
    triangle(400,510,325,560,425,610);  //325
    fill(g);
    triangle(75,240,175,290,100,340); //75
    fill(h);
    triangle(100,510,175,560,75,610); //75
   fill(i);
   stroke(172);
rect(380,80,70,70);                  //draw 2 rectangle below the sphere(ellipse)
rect(380,700,70,70);
stroke(0);
fill(j);
ellipse(415,115,85,85);            //draw sphere with diameter 85
fill(k);
ellipse(415,735,85,85);
fill(l);
stroke(255);
ellipse(25,25,30,30);
}

//to print received data and show on screen
fill(255);
noStroke();
textAlign(LEFT);
text(info, 20, 104);
noFill();
}

void update(int x, int y) {       //to control the flag when we click a button
 if ( overRect(200, 150, 100, 100) ) {
    rectOver = true;                   //left right
    rec = 3;
  }
  else if
  ( overRect(50, 375, 100, 100) ) {
    rectOver = true;                   //up down
    rec = 1;
  }
  else if
  ( overRect(350, 375, 100, 100) ) {
    rectOver = true;
        rec = 2;                            //up down
  }
  else if
  ( overRect(200, 600, 100, 100) ) {
    rectOver = true;                   //left right
        rec = 4;
  }
    else if
  ( overRect(372,72,85,85) ) {
    rectOver = true;
        rec = 5;                            //for ellipse 1
  }
    else if
  ( overRect(372,692,85,85) ) {
    rectOver = true;
        rec = 6;                            //for ellipse 2
  }
      else if
  ( overRect(325, 240, 100, 100) ) {
    rectOver = true;
        rec = 7;                            //up left
  }
      else if
  ( overRect(325, 510, 100, 100) ) {
    rectOver = true;
        rec = 8;                            //up right
  }
      else if
  ( overRect(75, 240, 100, 100) ) {
    rectOver = true;
        rec = 9;                            //btm left
  }
      else if
  ( overRect(75, 510, 100, 100) ) {
    rectOver = true;
        rec = 10;                          //btm right
  }
        else if
  ( overRect(10, 10, 30, 30) ) {
    rectOver = true;
        rec = 11;                          //stop
  }
  else
  {
    rectOver = false;                 //nothing s touched on screen
  }
}

boolean overRect(int x, int y, int width, int height)  { // to scan we touch which area
  if (mouseX >= x && mouseX <= x+width &&
      mouseY >= y && mouseY <= y+height)             //to see if the mouse cursor inside rect
  {
    return true;
  } else {
    return false;
  }
}

void onKetaiListSelection(KetaiList klist)
{
String selection = klist.getSelection();            //select the device to connect
bt.connectToDeviceByName(selection);        //connect to the device
klist = null;                                                      //dispose of bluetooth list for now
}

//Call back method to manage data received
void onBluetoothDataEvent(String who, byte[] data)
{
if (isConfiguring)
return;
//received
info += new String(data);
if(info.length() > 150)             //clean the words on screen if string to long
info = "";
}//END of Android processing coding


Video will be uploaded soon :)

Friday 5 April 2013

Use Android smartphone to control two wheels robot with Arduino via Bluetooth

2) Arduino Programming Part

For the Arduino programming part, I'm using is Arduino Leonardo. The difference of programming coding of Leonardo with Uno is the "serial" command. If i'm not mistaken, for Leonardo to communicate via Bluetooth the command will be "Serial1.xx" as below. Meanwhile for Uno will be just "Serial.xx".

Before going into Arduino programming part, you have to configure your own Bluetooth module. For example: set the baudrate, name of the Bluetooth device, and password to let your Android device connect to your Bluetooth shield. I'm doing all this setting by using a software called hyperterminal. You can try to configure your bluetooth module first because everyone are using different shield & module so I'm not gonna emphasize on it here, you can always try to google it :) (Bluetooth module configuration). Also, make sure your computer had installed the correct driver for your own Bluetooth Dongle.

Bluetooth Shield, Module and Dongle that I'm using

Below are the coding for my Arduino Leonardo: 


#include <Servo.h> //include servo function
#define BUFFERSIZE 127
uint8_t inBuffer[BUFFERSIZE];
int inLength; // length of data in the buffer
int numLoop = 0; // number of times we looped
char val;

Servo myservo;  //for servo function
int motor_LF=2; //declaring pin used for the motor
int motor_LR=3;
int motor_RF=4;
int motor_RR=5;

int motor_LPWM = 13; //declaring pwm pin
int motor_RPWM = 11;

void setup() {
  Serial1.begin(38400); //baud rate I set for bluetooth module is 38400
  myservo.attach(9); //my servo at pin 9 as actuator
  pinMode(motor_LF, OUTPUT); //set pin as output
  pinMode(motor_LR, OUTPUT);
  pinMode(motor_RF, OUTPUT);
  pinMode(motor_RR, OUTPUT);
  pinMode(motor_LPWM, OUTPUT);
  pinMode(motor_RPWM, OUTPUT);
 
  motor_reset(); // call function to reset the motor
}
void loop() {
  // read string if available
  if (Serial1.available()) {
    inLength =  0;
    while (Serial1.available()) {
      val = Serial1.read();
    }
   
    Serial1.print("Arduino received: "); //send the text to the android device
    Serial1.write(val);
    Serial1.println();
  }
 
   if (val == 'k') //K
    { myservo.write(90); } //write the angle of servo should move
   
    if (val == 'l') //L
    { myservo.write(30); }
   
      if (val == 'm') ///M
    { myservo.write(178); }
   
    if (val == 'w') //W – Forward motion of motor
    {
      digitalWrite(motor_LF, HIGH);
      digitalWrite(motor_LR, LOW);
      digitalWrite(motor_RF, HIGH);
      digitalWrite(motor_RR, LOW);
      analogWrite(motor_LPWM, 255); //sending pwm pulses
      analogWrite(motor_RPWM, 255); //255 will be highest
    }
   
    if (val == 's') //S – Reverse motion
    {
      digitalWrite(motor_LF, LOW);
      digitalWrite(motor_LR, HIGH );
      digitalWrite(motor_RF, LOW);
      digitalWrite(motor_RR, HIGH);
      analogWrite(motor_LPWM, 255);
      analogWrite(motor_RPWM, 255);
}

    if (val == 'q') //Q - Left Forward
    {
      digitalWrite(motor_LF, HIGH);
      digitalWrite(motor_LR, LOW);
      digitalWrite(motor_RF, HIGH);
      digitalWrite(motor_RR, LOW);
      analogWrite(motor_LPWM, 128); //slower thn Right PWM but same direction to turn left
      analogWrite(motor_RPWM, 255);
    }
   
    if (val == 'e') //E - Right Forward
    {
      digitalWrite(motor_LF, HIGH);
      digitalWrite(motor_LR, LOW);
      digitalWrite(motor_RF, HIGH);
      digitalWrite(motor_RR, LOW);
      analogWrite(motor_LPWM, 255);
      analogWrite(motor_RPWM, 128); //slower thn left PWM but same direction to turn right
    }
        if (val == 'z') //Z - Left backward
    {
      digitalWrite(motor_LF, LOW);
      digitalWrite(motor_LR, HIGH);
      digitalWrite(motor_RF, LOW);
      digitalWrite(motor_RR, HIGH);
      analogWrite(motor_LPWM, 128);
      analogWrite(motor_RPWM, 255);
    }
        if (val == 'c') //C - Right backward
    {
      digitalWrite(motor_LF, LOW);
      digitalWrite(motor_LR, HIGH);
      digitalWrite(motor_RF, LOW);
      digitalWrite(motor_RR, HIGH);
      analogWrite(motor_LPWM, 255);
      analogWrite(motor_RPWM, 128);
    }
   
    if (val == 'a') //A - Spin Left
    {
      digitalWrite(motor_LF, LOW);
      digitalWrite(motor_LR, HIGH);
      digitalWrite(motor_RF, HIGH);
      digitalWrite(motor_RR, LOW);
      analogWrite(motor_LPWM, 255); //move in same speed but different direction to spin
      analogWrite(motor_RPWM, 255);
    }
   
    if (val == 'd') //D - Spin Right
    {
      digitalWrite(motor_LF, HIGH);
      digitalWrite(motor_LR, LOW);
      digitalWrite(motor_RF, LOW);
      digitalWrite(motor_RR, HIGH);
      analogWrite(motor_LPWM, 255);
      analogWrite(motor_RPWM, 255);
    }

    if (val == ' ') //Space – Brake function
    {
      digitalWrite(motor_LF, LOW); //make all the pin LOW to stop the motor
      digitalWrite(motor_LR, LOW);
      digitalWrite(motor_RF, LOW);
      digitalWrite(motor_RR, LOW);
      analogWrite(motor_LPWM, 255); //both 255 to make the motor stop immediately
      analogWrite(motor_RPWM, 255);
    }
   
    if (val == 't') //T – Reset the motor
    {
      motor_reset();
    }
 
}

void motor_reset()  //reset function
{
  digitalWrite(motor_LF, LOW);
  digitalWrite(motor_LR, LOW);
  digitalWrite(motor_RF, LOW);
  digitalWrite(motor_RR, LOW);
  analogWrite(motor_LPWM, 0);
  analogWrite(motor_RPWM, 0);
}//END of arduino code


Parts of the coding credits to
Nadrew Chong Jia Ying

Use Android smartphone to control two wheels robot with Arduino via Bluetooth

1) Mechanical parts: Make your own two wheels robot!

 If you still don't have a two wheels robot, you can try to make one with the components below:

     (This components are used in my own robots, you can change it as you like.)

  •      2 pieces of perspek
  •       4 PCB stand
  •        Servo motor
  •        2 gear motors
  •           1 H-Bridge for two motor for two way controlling
  •        2 wheels
  •      Small size breadboard
  •       Bluetooth Receiver Module
  •        Arduino Leonardo
  •     1 piece of 9V battery
  •     Battery holder
  •      Rechargeable batteries (2500mAh)



Below are the process of making of my own two wheels robot:

Stage 1


The first prototype of our robot is constructed in a way that the body of the robot is made out of red plastic cardboard due to the benefits of its material that is light enabling the robot to go at a higher pace. Then two 3.3V DC tamiya motor are being used to be built as a part of the wheels of our robot.  These wheels are being used as they could produce speed up to 18000rpm. But after testing we found out that Tamiya motor has very low torque. When there are some loads on the motor it will not move at all. At this stage we are still thinking of the Pulse wave modulation (PWM) problem. Another testing will be done on Tamiya motor later. As all the component (servo motor, 2 tamiya motor, arduino, battery and etc) are being constructed onto our robot, the plastic cardboard that was used as the body of the robot could not withstand the amount of weight and it was squashed.



Stage 2

This is the second prototype of our robot where the body is being built using perspek. As the red plastic board couldn’t withstand the weight of the components placed, another material was being used as the body of the robot. Due to the replacement of a new material as the body of the robot to sustain more weight, the material seem to withstand the amount of mass withstand on it but unfortunately it causes the robot to move at a slower pace due to the mass of perspek is slightly higher than to a plastic cardboard. Later on we found out that when we send the PWM pulse into the 3.3V tamiya motor via transistor from Arduino, it really has a very low torque even with high voltage power supply and full PWM pulses(255).The motor can’t move with any load so it wasn’t suitable to be used in our mini project anymore.



Stage 3

12V DC motors


Since 3.3V tamiya DC motors aren’t suitable to be used in the construction of our robot, it was replaced by a 12V DC motor instead. This replacement produces higher torque but still the speed is low hence sufficient voltage needed to be supplied in order for the robot to move faster. But this affects the total weight of the robot as the weight increases with the increase in voltage due to more amounts of batteries were being used. Besides that, our robot design was enhanced by making a few extra changes to it such as metal strips are being used to hold both the motor in place, In prevention of short circuit, this metal strips were insulated by covering it with heat tube.



Stage 4
Gear motor

As we found out that 12V DC motor are also aren’t suitable to be used in the construction of our robot, it was replaced by a pair of 6V gear motors. Gear motor can produce higher torque hence enabling the robot to actually move faster to be compared to previous prototype. This gear motor we bought comes with stall torque 0.3kg/cm and 440 free run rpm.Also, the weight of each gear motor only 10 gram as this will make the total weight of the robot a lot lighter so that it can run faster. The detail of the gear motor can be viewed on the link below: http://www.cytron.com.my/viewProduct.php?pcode=SPG10-30K&name=30:1%20Micro%20Metal%20Gearmotor



Upper view of the robot


Back view of  the robot
Side view of the robot
Bottom view of the robot(with a caster at lower middle part)

Moreover, we found out that short circuit may be occurred due to limited space to place these components on top of the robot, so we enhance the design of the robot by using a PCB stand to hold another layer of perspek. With this, we have enough space to place these components neatly in prevention of short circuit. Moreover, we changed the actuator design of the robot into a smaller piece of metal as it’s smaller and suitable to be used for the soccer game competition. To enable the robot to move faster, we used high amp rechargeable battery (2500mAh). With this, the gear motor can perform well as expected when all the component are placed on the robot.

Upper part components(Battery casing, servo motor)

Lower part components(Small breadboard with wirings, gear motors,  Arduino Leonardo)

There was a square hole made on the middle of the upper part perspex  for easy wiring.

XBee Shield V1.4 & BlueBee Bluetooth module

Bluetooth shield with module & Bluetooth dongle

Bluetooth shield with module & Arduino Leonardo


Below are the schematic diagram for the two wheels robot:



Prototype making, Photo and Schematic diagram credits to
Jayson Khoo Thiam Wee & Lee Kien Thiam