Tweeting Duck

duck

This project builds on the tweeting chicken from radiate Science and adds natural language playback from Google translate. It also adds some  audio hardware to play the tweet using the loudspeaker inside the duck and modifies the code to suit the soft toy duck that we used to make this work. The requirements for the project are a Raspberry Pi, a PiFace board (or some electronics bits & bobs) and a Duck!


Note to pedants:

Yes I know it’s a duck so it it will Quack not Tweet but the the original was a chicken that would have Clucked and not Tweeted either. Buy me an animatronic yellow canary and I’ll disembowel that instead!


About The Duck

The toy duck was originally designed to move it’s foot and body whilst singing “Singing in the Rain”. It cost a fiver from eBay and its worth searching on eBay if you want a similar duck (It’s called Ducksley). The duck incorporates a motor gearbox unit that moves the body and left foot of the duck whilst it sings.

The song & movement is started / stopped by a momentary switch in the right foot. The code for the original chicken closed the switch to start the chicken wobble and opened it to stop. For the duck we have to momentarily close the switch and then open it to start the duck and repeat the process to stop it.

lm386The loudspeaker in the duck was originally fed from an Integrated device specifically programmed for “Singing in the Rain”. Connecting the speaker directly to the Pi did not provide enough volume so a circuit board incorporating an LM386 Audio Amplifier (cost a quid from eBay) was added inside the duck

 

About the Code

The python code is almost identical to that for the chicken. The original code is here and consists of two parts, raspberry_pi_farm.py and twitter_listen.py.

raspberry_pi_farm.py is modified to simulate the momentory switch effect in these lines.


def toggle_wobble(self):
		"""Starts wobbling the chicken"""
		if VERBOSE_MODE:
			print "Toggle Wobble"
		pfio.digital_write(self.relay_pin, 1)
		time.sleep(0.1)
		pfio.digital_write(self.relay_pin, 0)

 

There is a 100mSec delay between turning the switch on then off. This is because the duck did not recognise an instant on/off

raspberry_pi_farm.py also has an additional few lines of code that call Google Translate. Google returns an mp3 that is played using mplayer. For testing we used a BBC twitter feed as this gave us a constant supply of tweets. Most of these tweets ended in a url which was difficult to understand when spoken. The code detects an ‘http’ pattern and strips everything out after that point. Additionally, the code includes a line to truncate the string before it is is sent to Google. This is because a tweet can be 140 characters long and Google will ignore any string that contains over 100 characters. The code snippet is shown below:

if VERBOSE_MODE:
			print "Chicken says: %s" % text_to_say

                if VERBOSE_MODE:
                        # using 'espeak' to make the chicken talk
                	subprocess.call(["espeak",
			"-v", "en-rp",
			"-p", str(self.voice_pitch),
			"-s", str(self.voice_speed),
			text_to_say])

                print "Full Tweet: %s" % text_to_say
                http_pos=text_to_say.find('http')# Finds "http" in string text_to_say
                print (http_pos)
                text_to_say=text_to_say[:http_pos] # truncates string
                print "Stripped http: %s" % text_to_say

                # Using Google Translate to create Natural Language mp3
		subprocess.call(["./Speech.sh", text_to_say])

 

When Verbose mode is enabled the Duck will speak the full tweet using espeak and then the truncated string using mplayer. This is for testing, e.g. checking the internet connection to Google etc.

About the Hardware

There are two primary modifications to the hardware inside the duck. The stop / start motion of the duck was originally controlled by a momentary switch in the duck’s foot. This has been left in place and a relay contact placed in parallel to the switch so imitating the switch. The PiFace board includes the relay, without a PiFace board some additional circuitry is required as shown in the diagram below.

The loudspeaker inside the duck was originally fed from an amplifier circuit in the duck. Connecting the speaker directly to the headphone output of the Pi did not give enough volume so an LM386 audio amplifier board was added.


…Work in Progress

Leave a Reply