When life gives you lemons... build a turret from Portal 2

A little over 2 years ago a certain game was released by the name of Portal 2. We all know it. We all also know that, being that it was made by Valve, it will be of a standard which would inspire people for years to come; and this recently published video on YouTube shows just how true this is:


Seeing this reminded me of how, back in May 2011, another project I came across on the internet inspired me to attempt my own Portal 2 tribute. The source of my inspiration was this, a stunningly well made replica turret. Well I'm no model maker, but I do know a bit about embedded software and electronic design - specifically based around Atmel's AVR and ATMega series of 8-bit microprocessors. Undaunted by my lack of any ability to make something that *looked* good (we'll come back to that later) I set about making something that ought to sound good and react to external stimuli in a semi-entertaining way.
     The first thing I needed was a way to play audio stored on an SD card or similar which, thankfully, had already been solved for me. This page provided exactly what I needed. I won't go into detail as you can read it for yourself (it does a much better job of explaining how it works than I ever would), instead I shall point out that I can in no way take credit for that part of my final design. The important parts to note are how it reads an 8 bit value on PINA4 to PINB7 which corresponds to a wav file numbered accordingly and that PORTB2 changes state while a file is playing. The latter is important because, although in the elm-chan design it merely lights an LED, in my design I was able to use it to signal back to my controller chip when a sound was playing and that all other operations should temporarily be suspended. Which brings me to the second part of my design: automation.
    To effectively 'bring the turret to life' I needed to be able to measure the surroundings in some way similar to the turrets in the game, and use those measurements to decide which sounds to play. The sounds themselves were fairly straight forward; using a GCF explorer I was able to extract the audio from Portal 2 itself and, after a while spent carefully perusing the sounds, decide on a sub-set which I grouped sequentially and renamed based on their perceived reaction. For example sounds 1 - 4 were waking sounds; Hello!, I See You! and so on, followed by idle sounds 5 - 8 etc. In the end I settled on a few groups: waking, being picked up, idle, going to sleep and attack. The actual order differs slighty - it's been nearly 2 years and I don't remember all the specifics. To measure the surroundings I use a PIR (an infra-red sensor similar to those found in burglar alarms, only running at 5v rather than the standard 12v) to emulate the turret's laser, and a small tilt switch. For added effect I decided to add a ring of LEDs around the turret's 'eye' which animate while audio is playing. The sensors and the LEDs are then connected to an ATMega8 8-bit microprocessor, which is where all the fun stuff actually happens. I chose an AT8 because it has a decent selection of pins to connect the sensors and LEDs to and also to interface with the ATTiny chip which plays the audio. The AT8 also has 3 timers which are extremely useful in controlling the logic behind the sensors.
    The basic premise is this: check the sensors, if we sense something output the corresponding number on an 8-bit port to the ATTiny to play the audio, and wait for the ATTiny to signal it's no longer busy by measuring the output from PORTB2. Then the sensors are checked again until the current timer expires, at which point the AT8 reverts to 'idle' mode whereby it randomly plays one of a selection of sounds until a new measurement is made.

The program flow goes something like this:

Start idle timer which plays random idle sounds

Check tilt pin


Check PIR pin


If tilt or
PIR pin changes stop poll and idle timer and start tilt or PIR timer


While tilt or
PIR timer runs play tilt or PIR sounds until timeout, then play shutdown sound and (re)start idle timer


Start poll again and play idle sounds

I wrote the software for the AT8 in C using, at the time, programmer's notepad and compiled it with AVRGCC - although these days I prefer AVRStudio 5. The polling of the pins all happens in the main loop of the program, with the timer interrupt requests controlling the switching of modes. You can download the source code in a zip file here which also includes an AVRStudio project file and a pdf of the board schematic. A note on the schematic: I have included an ISP header to allow programming of the AT8 in situ although the pins are NOT in the correct order for a standard programmer such as the Atmel STK-500. I used a home built programmer at the time so the pins are designed to match that. Unfortunately I no longer have the track layout for etching or the Eagle project file so you'll have to lay it out yourself if you want to try building your own. For reference here are a few shots of the board complete with a shoddy attempt by me to build a model turret using a Pepakura cut-out:




The power source is from a USB port, which provides a decent 5v supply - it does nothing else. For good measure here are a couple of videos of it in action (I've tried to keep them short as I find after a while I start to talk to it...):



Comments

Popular Posts