Self-Balancing Robot First Step:The Interaction

Here is the first step in making of Self Balancing Robot. In this step i worked on Bluetooth & Serial Communication.
Communication scheme:
N95 (Bluetooth)—> PC —–( Serial)—-> Arduino
I used the same motor driver shield which i already made back in Narduino Project.
If you havent heard about Narduino Shield and its simple design done on veroboard click here to see it and its circuit diagram.
After the Narduino shield properly connected to motor wires and the supply connection ,  it is time to talk about coding…
Here below , i shared all the codes used in the project.

Desktop_AccBalance.py

from bluetooth import *
import serial
server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
s=serial.Serial(2,9600)
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
advertise_service( server_sock, "SampleServer",
                   service_id = uuid,
                   service_classes = [ uuid, SERIAL_PORT_CLASS ],
                   profiles = [ SERIAL_PORT_PROFILE ],
#                   protocols = [ OBEX_UUID ]
                    )
print "Waiting for connection on RFCOMM channel %d" % port
client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info
try:
    while True:
        data = client_sock.recv(1024)
        if len(data) == 0:
		    break
        print data
        s.write(data)
except IOError:
    pass
print "disconnected"
client_sock.close()
server_sock.close()
print "all done"

N95_AccBalance.py

import socket,axyz,appuifw,e32
sock=0
w=True
tp=0
def connect():
    global sock
    sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM)
    addr,services=socket.bt_discover()
    print "Discovered: %s, %s"%(addr,services)
    if len(services)>0:
        import appuifw
        choices=services.keys()
        choices.sort()
        choice=appuifw.popup_menu([unicode(services[x])+": "+x
                                   for x in choices],u'Choose port:')
        port=services[choices[choice]]
    else:
        port=services[services.keys()[0]]
    address=(addr,port)
    print "Connecting to "+str(address)+"...",
    sock.connect(address)
    print "OK."
connect()
def quit():
 sock.close()
 global w
 w=False
appuifw.app.menu = [(u"Exit",quit)]
def acc(x,y,z):
    global sock,acc,tp,j,w
    j=z+100
    if( abs(tp-j)>1):
        sock.send(str(j))
        tp=j
    axyz.disconnect()
    e32.ao_sleep(0.05)
    if w==True:
        axyz.connect(acc)
axyz.connect(acc)

Arduino_AccBalance

int mot1ana=3;
int mot1a=2;
int mot1b=4;
//motor1
int val=100;
void setup()
{
 Serial.begin(9600);
 pinMode(mot1ana,OUTPUT);
 pinMode(mot1a,OUTPUT);
 pinMode(mot1b,OUTPUT);
}
void loop()
{
 int i, serAva;
 char inputBytes [7];
 char * inputBytesPtr = &inputBytes[0];
 if (Serial.available()>0)
 {
   delay(5);
   serAva = Serial.available();
     for (i=0; i<serAva; i++)     
 {
inputBytes[i] = Serial.read();
}
 inputBytes[i] =  '';            
 val= atoi(inputBytesPtr);  
 } 
 if(val>105)
 {
  digitalWrite(mot1a,LOW);
  digitalWrite(mot1b,HIGH);
  analogWrite(mot1ana,200);
 }
  if(val<85)
 {
  digitalWrite(mot1a,HIGH);
  digitalWrite(mot1b,LOW);
  analogWrite(mot1ana,200);
 }
 else if(val<105 && val>85)
 {
  digitalWrite(mot1a,LOW);
  digitalWrite(mot1b,LOW);
  analogWrite(mot1ana,0);
 }
}

3 thoughts on “Self-Balancing Robot First Step:The Interaction

Leave a Reply