Raspberry Pi Tracked Robot – streaming video and text to speech

with 2 comments

For phase two of my Raspberry Pi based robot (now christened ‘Roger’) I decided to add a camera for remote video based control and a speaker so that it can talk. The camera is a Logitech C200 plugged into one of the Pi’s usb ports and the speaker is the excellent  X-Mi X Mini 2 plugged into the Pi’s audio output.

Robot with camera and speaker

Text to Speech

Easily handled by the Pi. I came across this article with a few methods of achieving text to speech on the Pi and went with the Festival option. At the moment I’m happy with it sounding very robot like! I’m calling it from Python with something like the following: os.system(‘echo “‘ + text + ‘” | festival –tts’) where text is a variable containing the words to speak.

I also added the ability to play a wav file as a way to add solme fun sound effects (like a lorry start up noise when the robot starts up  and a siren for waking people up). This is done with aplay – full instructions here. Again called from python with something like: os.system(‘aplay /home/pi/robot/diesel_lorry.wav &’).

Streaming Video

For streaming video I’m using Motion – here are some quick setup instructions for the Pi. The Logitech C200 works well out of the box with the Pi. I did try a C310 first but there was significantly more delay on the video stream (presumably because of the higher source resolution). If choosing a webcam (or any other peripheral) to get for your Pi check out this list of verified peripherals first. Obviously there is tons more I can do with Motion like detecting and responding to actual motion but for a first cut of streaming video to a browser for remote control robot operation this setup works well.

HTTP API

To control the robot via web browser I’m using Webiopi. I have a Python script defining my Webiopi ‘macros’ and a custom HTML page using their Javascript API to create a page with buttons and a text box for entering speech. Complete code listings for these at the end of the post.

Demo of the robots capabilities so far

Code Listings


#webrobot.py
 
import webiopi
import time
import sys
import os
import urllib
from raspirobotboard import *

webiopi.setDebug()
robot = RaspiRobot()

def setup():
    webiopi.debug("Setup")
    flash_lights()
    flash_lights()
    os.system('amixer cset numid=3 1')
    os.system('aplay /home/pi/robot/diesel_lorry.wav &')

def destroy():
    webiopi.debug("Destroy")
    flash_lights()
    flash_lights()
    flash_lights()
    flash_lights()

@webiopi.macro
def speak(text):
    # decode url encoded parameters
    text = urllib.parse.unquote(text)
    webiopi.debug("Speaking: %s" % (text))
    os.system('echo "' + text + '" | festival --tts')

@webiopi.macro
def forward():
    robot.reverse()

@webiopi.macro
def reverse():
    robot.forward()

@webiopi.macro
def stop():
    robot.stop()

@webiopi.macro
def left():
	# turning in small increments, helps with slightly delayed response on a mobile
    robot.left(0.15) 

@webiopi.macro
def right():
    robot.right(0.15)

@webiopi.macro
def lights_on():
    robot.set_oc1(1)

@webiopi.macro
def lights_off():
    robot.set_oc1(0)

def flash_lights():
    lights_on()
    webiopi.sleep(0.25)
    lights_off()
    webiopi.sleep(0.25)



<!-- robot.html -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" content = "height = device-height, width = 420, user-scalable = no" />
        
        <script type="text/javascript" src="/webiopi.js"></script>
        <script type="text/javascript">
        webiopi().ready(function() {
                var content, button;
                content = $("#content");

                button = webiopi().createMacroButton("lights", "Lights On", "lights_on");
                content.append(button);

                button = webiopi().createMacroButton("lights", "Lights Off", "lights_off");
                content.append(button);

                button = webiopi().createMacroButton("forward", "Forward", "forward");
                content.append(button);

                button = webiopi().createMacroButton("reverse", "Reverse", "reverse");
                content.append(button);

                button = webiopi().createMacroButton("stop", "Stop", "stop");
                content.append(button);

                button = webiopi().createMacroButton("left", "Left", "left");
                content.append(button);

                button = webiopi().createMacroButton("right", "Right", "right");
                content.append(button);

                button = webiopi().createButton("speak", "Speak", speakText);
                content.append(button);
                });

        function speakText() {
                var text = $('textarea#talk').val();
                webiopi().callMacro("speak", text);
        }

        </script>
</head>
<body>
    <div id="content" align="center">
        <textarea rows="5" cols="50" id="talk">Hello. My name is Roger. Nice to meet you.</textarea>
    </div>

</body>
</html>


Written by Tim Corrigan

May 20th, 2013 at 12:41 pm

Posted in Technical

Tagged with ,

2 Responses to 'Raspberry Pi Tracked Robot – streaming video and text to speech'

Subscribe to comments with RSS or TrackBack to 'Raspberry Pi Tracked Robot – streaming video and text to speech'.

  1. Hello. At first, i am sorry for my bad english. I am from Czechia and i am fifteen years old.

    Your robot is amazing! I want to build it too. But how are you powering raspberry pi? Which cabel you are using for powering? MicroUSB? And which version of raspberry do you have? Model A or B?

    Jan

    28 Apr 14 at 11:24 am

  2. Hi Jan,

    Glad you like the Robot! I’ve actually done quite a bit to it since this post and will try and post an update this week.

    But in the mean time to quickly answer your questions:
    It’s a Model B
    It’s powered by a 6 AA rechargeable battery pack which sits in the bottom of the chassis. This feeds 7.2V to the Raspirobot board which has a regulator that steps it down to the 5V needed by the Pi. More about the board in my first post here: http://timcorrigan.com/raspberry-pi-tracked-robot-phase-1/

    If you have any other questions feel free to ask and I wish you all the best with your own Robot project!

    Tim Corrigan

    28 Apr 14 at 12:09 pm

Leave a Reply