Sunday, March 11, 2012

Cloud Robotics Hackathon - Notes and thoughts

Last weekend Andra organized a very cool robotics hackathon at the Citrix Startup Accelerator as part of the Cloud Robotics Hackathon. Apart from being ground breaking, it was a heck of a lot of fun. Here's some images and videos of the event on the Google+ for RobotLaunchpad.

In this blog I wanted to capture the approach that Guy Bieber and I came down to with the supplied RobotShop Rover with ultimately a bluetooth module and Andra's HTC Android phone. This is intended to be a quick capture of approach and code to help others who use similar kits.

Photo-2

This was a fun weekend and way to explore the near future of these types of devices and using them with cloud functionality. I'm looking forward to much more from commodity robots linked in with SmartPhones and the cloud. This was a good way to make the Internet of Things more visceral.

__

The goal of the exercise was to build a useful tool with a combination of the rover and the myrobots.com site for robot connectivity. We had discussed a bunch of possible outcomes including:

  • A robot that responds to (online visible) sports outcomes by dancing happy or sad dances
  • listening for particular phrases then adding notes to evernote
  • taking pictures when names given
  • if bored, play a youtube video
  • link to mechanical turk to assist with navigation
  • use ifttt to control outcomes

Ultimately however, it took quite a while to get the robot working, and to iterate through possible combinations of connectivity, programming environment and more. Hence we didn't play with the possbilities as much as anticipated, but we did find a very workable combination of technologies that also - happily - leverages the SmartPhone, which is one of the less complex routes to the 'Internet of Things' and to affordable commericalization of a bunch of interesting robotic ideas.

What didn't work:

  • We found that the USB host connection to Android, and using the USB Host kit was simply too complex for a quick weekend hack. This would make a lot of sense to revisit at time of commercialization, etc.
  • Bluetooth connection to iPhone - we're both iPhone users, and thought that getting bluetooth connection up would make iPhone control viable. However this was not the case, as Apple has locked down bluetooth connectivity to only 'blessed' devices.
  • Easy connectivity to the myrobots.com site by device or Android - however with judicious exploration of scripts for thinkspeak we found some viable options.

What tweaks were needed along the way:

Code fragments that follow are not neat, particualrly documented, nor particualrly well written, however they are provided in hopes that they are of use to others who might get further with this combination of tech in future.

 

SL4A code

##############################################################################
#
# Drive rover with voice commands from iPhone
# startBluetooth came from uibtre.py - this also contains code for accelerometer control
# (http://www.youtube.com/watch?v=BtmRBxRsMk4, http://code.google.com/p/android-jp-kobe/source/browse/trunk/pyAndyUI/uibtre.py)
#
# Michael Harries -- March 4, 2012
##############################################################################
import sys
import time
import json
import httplib, urllib
import android

d = android.Android()

def startbluetooth():
    uuid = '00001101-0000-1000-8000-00805F9B34FB'
    d.dialogCreateAlert( "select BlueTooth operation" )
    d.dialogSetPositiveButtonText( "server" )
    d.dialogSetNeutralButtonText( "client" )
    d.dialogSetNegativeButtonText( "no-BT" )
    d.dialogShow()
    ret = d.dialogGetResponse().result[ "which" ]

    if ret == "positive":
        d.bluetoothMakeDiscoverable()
        d.bluetoothAccept( uuid )
        return True
    elif ret == "neutral":
        ret = d.bluetoothConnect( uuid ).result
        if not ret:
            d.makeToast( "bluetooth not connected" )
            sys.exit( 0 )
        return True
    print "skip bt setup"
    return False

def main():
    prevchar=' '
    nextchar=' '
    # d.startSensingTimed( 2, 300 )   ### sense start

    fBT = startbluetooth()
    d.ttsSpeak("command me baby")
    #d.ttsSpeak("forward backward left right stop cloud quit")
   
    while True:
        command = d.recognizeSpeech("Command me baby", None, None)
        print command[1]
       
        if command[1] == "stop":
            nextchar = 'x'
            fBT and d.bluetoothWrite('x')
            d.ttsSpeak("stop")
        elif command[1] == "left":
            nextchar = 'a'
            fBT and d.bluetoothWrite('a')
            d.ttsSpeak("left")
        elif command[1] == "right":
            nextchar = 'd'
            fBT and d.bluetoothWrite('d')            
            d.ttsSpeak("right")
        elif command[1] == "forward":
            nextchar = 'w'
            fBT and d.bluetoothWrite('w')   
            d.ttsSpeak("forward")
        elif command[1] == "back":
            nextchar = 's'
            fBT and d.bluetoothWrite('s')
            d.ttsSpeak("back")
        elif command[1]=="cloud":
            nextchar='c'
            # grab command by myrobot.com
            conn = httplib.HTTPConnection("bots.myrobots.com")
            conn.request("GET", "/channels/595/feed/last.json")
            response = conn.getresponse()
            print response.status, response.reason
            json_string = response.read()
            print json_string
            conn.close()
            data = json.loads(json_string)
            print data
            #say
            say=data['field2']
            if say:
                d.ttsSpeak(say)
            #move
            move=data['field1']
            if move:
                print move
                #d.bluethoothWrite('w')
                fBT and d.bluetoothWrite(move)
                time.sleep(1)
                fBT and d.bluetoothWrite('x')
            #play
            play=data['field3']
            if play:
                print play
                d.mediaPlay(play)
                fBT and d.bluetoothWrite('a')
                time.sleep(.5)
                fBT and d.bluetoothWrite('d')
                time.sleep(.5)
                fBT and d.bluetoothWrite('w')
                time.sleep(.5)
                fBT and d.bluetoothWrite('s')
                time.sleep(.5)
                fBT and d.bluetoothWrite('x')
           
        elif command[1] == "quit":
            fBT and d.bluetoothWrite('x')
            d.ttsSpeak("Did I do something wrong")
            return True
           
        #time.sleep( 0.5 )
        prevchar = nextchar
   
    d.dialogDismiss()

if __name__ == "__main__":
    main()

Arduino - Rover code

int E1 = 6; //M1 Speed Control
int E2 = 5; //M2 Speed Control
int M1 = 8; //M1 Direction Control
int M2 = 7; //M2 Direction Control
void setup(void) {
  int i;
  for(i=5;i<=8;i++) pinMode(i, OUTPUT);
  Serial.begin(9600);
}
void loop(void) {
  while (Serial.available() < 1) {
  } // Wait until a character is received
  char val = Serial.read();
  int leftspeed = 255; //255 is maximum speed
  int rightspeed = 255;
  switch(val) // Perform an action depending on the command
  {
  case 'w'://Move Forward
    forward (leftspeed,rightspeed);
    break;
  case 's'://Move Backwards
    reverse (leftspeed,rightspeed);
    break;
  case 'a'://Turn Left
    left (leftspeed,rightspeed);
    break;
  case 'd'://Turn Right
    right (leftspeed,rightspeed);
    break;
  case 'x'://stop
    stop ();
    break;
  default:
    stop();
    break;
  }
}
void stop(void) //Stop
{
  digitalWrite(E1,LOW);
  digitalWrite(E2,LOW);
}
void forward(char a,char b) {
  analogWrite (E1,a);
  digitalWrite(M1,LOW);
  analogWrite (E2,b);
  digitalWrite(M2,LOW);
}
void reverse (char a,char b) {
  analogWrite (E1,a);
  digitalWrite(M1,HIGH);
  analogWrite (E2,b);
  digitalWrite(M2,HIGH);
}
void left (char a,char b) {
  analogWrite (E1,a);
  digitalWrite(M1,HIGH);
  analogWrite (E2,b);
  digitalWrite(M2,LOW);
}
void right (char a,char b) {
  analogWrite (E1,a);
  digitalWrite(M1,LOW);
  analogWrite (E2,b);
  digitalWrite(M2,HIGH);
}

Python script for uploading commands to myrobots.com

import httplib, urllib
import time
 
def doit():
    params = urllib.urlencode({'field1': 'w', 'field2': 'not quite a square','field3': 'http://www.youtube.com/watch?v=WKxx5QC0ewc#t=57s','key':'B85D18A801134D7F'})
    headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
    conn = httplib.HTTPConnection("bots.myrobots.com")
    conn.request("POST", "/update", params, headers)
    response = conn.getresponse()
    print response.status, response.reason
    data = response.read()
    conn.close()
 
#sleep for 16 seconds (api limit of 15 secs)
if __name__ == "__main__":
        doit()

Posted via email from _technoist_

No comments: