TV remote control, Ruby and Turtle Graphics

22 Feb 2008

In my previous post I had written about doing Turtle graphics with Ruby. Now i want to control the movement of the turtle on the screen, using a TV remote control, wirelessly from a distance. Let me breakdown the whole process into simple steps :

  1. Transmitting Infrared (IR) signals from a distance.
  2. Receiving these IR signals.
  3. Interface to the computer.
  4. Software Interface (API) for reading the data from the I/O port.
  5. Application level program for controlling the movement of Turtle based on this data.
Transmitting IR signals

An circuit comprising of IR LED, power supply and a few switches would have been required, but we can simply use a TV remote control, instead :)

Receiving IR signals

A simple IR receiver circuit and instructions for building it can be found here. Components costs less than Rs.20 and can be bought from any electronics shop.

Here is the one which i made :

ir_receiver.jpg

Interface to the computer

Our IR receiver circuit has a serial interface. So we can connect it directly to the serial port of the computer.

Software Interface for reading and interpreting signals on the serial port

The Linux Infrared Remote Control project (LIRC) provides all necessary support for us. LIRC is a package that allows us to decode infra-red signals of the most commonly used remote controls. Even if we have a remote control that is not directly supported by LIRC, we can train LIRC to use our remote comtrol.

Instructions for installing and configuring LIRC is available here.

Alas, the remote controller which I had with me - it was the one which came with my TV tuner card, and was faulty. So I couldn't configure it properly with LIRC. I was able to receive signals from my remote, and see it using 'xmode2' program that comes with LIRC, but LIRC wasn't able to configure the keys of my remote control. I dint have the time to go and get a new remote control, so i devised a temporary work around - Make the turtle move in any one direction by default at a predefined speed, and whenever a key pressed on the remote (irrespective of which key is pressed), turn the turtle by 45 degrees.

Program for controlling Turtle movements upon keypress on remote control

Even though LIRC couldn't help me directly with my remote, i found a device file /dev/lirc0 (that appears once the circuit is connected, and 'lircd' daemon started). I tried reading from this file, and on each keypress on the remote i got a series of bytes. So i decided to use this file, for detecting keypress on the remote.

Now the only thing left was modifying my previous turtle program, to make the turtle change direction upon every keypress on the remote control. I used two threads :

  • first one for moving the turtle forward in a direction
  • second one for detecting keypress (by reading /dev/lirc0) and changing the direction whenever key is pressed
A shared global variable keypressed is used for communication between these threads. Access to keypressed is synchronized with a mutex.

Here is my complete program for controlling Turtle on screen with a TV remote.

turtle_remote.rb

Root previleges are required for accessing /dev/lirc0, so run it as

sudo ruby turtle_remote.rb

Turtles movement on screen can be seen here