Archive for February, 2008

TV remote control, Ruby and Turtle Graphics

Friday, February 22nd, 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 key_pressed is used for communication between these threads. Access to key_pressed 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

Keyboard lights & Ruby

Wednesday, February 20th, 2008

I happened to see a ruby library called Blinkenlights, which helps to control the three LEDs (Caps lock, Scroll lock & Num lock) on keyboards. I have written few simple programs to play with the keyboard LEDs - basically simulation of three experiments I did in my Digital Electronics lab - Binary Counter, Ring Counter & Johnson Counter.

sudo gem install blinkenlights

Binary Counter

A counter that counts from 0 to 7 in binary, using the three LEDs as three bits. 0(000) - All LEDs will be turned off, and 7(111) all of them turned ON.
counter.rb

super user privilege is required to access the keyboard, so run it as

sudo ruby counter.rb

Ring Counter

Starting with 4 (100), we right shift to get 2 (010), again right shift to get 1(001), and then start over again with 4. This causes the keyboard LEDs to turn ON and then OFF one by one.

ringcounter.rb

Johnson Counter

This is the Johson counter sequence : 000,001,011,111,110,100,000,001,…

johnsoncounter.rb

Riding the Turtle with Ruby

Friday, February 15th, 2008

Turtle graphics is all about making a ‘turtle’ to move around on the computer screen. The turtle leaves a trail while moving around. So if the Turtle is made to move in a straight line, it draws a straight line on the screen, if the turtle moves in a circle, it results in drawing a circle on the screen. As simple as that :)

Ruby has a lightweight turtle graphics library TkTurtle, which uses Tk. It can be downloaded from here. You need to have Tk and its Ruby bindings installed.

Getting started

Once tkturtle is installed, try the following in in irb. (Use the command ‘irb’ to open the interactive Ruby console) :

require 'tkturtle'
include TurtleGraphics

t = Turtle.new

A window pops up, with a small white triangle (arrow shaped), at the center. This is the ‘turtle’ !

t.left

turtle turns to the left.

t.right

turtle turns to the right.

t.forward 10

turtle moves 10 pixes forward (in the current direction)

t.turn 45

turtle turns 45 degrees in the anticlockwise direction.

These are the only basic commands needed for doing Turtle graphics.

Looping to draw some complex shapes

def one_line
forward(300);
end

This ruby method simply draws a straight line.

def two_perpendicular_lines
2.times { one_line ; turn 90 }
end

This method draws a pair of mutually perpendicular lines.

2.times {two_perpendicular_lines }

This will give a square.

10.times {two_perpendicular_lines}

This will also give the same square because lines go on overlapping. To avoid this, I introduced a small angle in beween consecutive “two_perpendicular_lines” like this :

def draw(a,b)
a.times { two_perpendicular_lines ; turn(b) ; wait }
end

wait is just to introduce a small delay in between, so that we can watch the turtle draw the lines.

Yea, thats it. Now lets go ahead and see the magic.

I have the code put into a file, for the sake of convenience, turtle1.rb

ruby turtle1.rb 0.1 1 20

0.1 is the time delay in to be introduced while drawing, 1 is is the number of times our basic building block (two perpendicualr lines) is to be repeated, and 20 is the angle in degrees between two consecuive building blocks.

This just gives a pair of perpendicular lines.

Now try

ruby turtle1.rb 0.1 3 20

Nothig much even now - 3 pairs of perpendicular lines, with an extra angle of 20 between them.

Now lets increases the number of times they are repeated.

ruby turtle1.rb 0.1 100 20

Wow.. See what i got..!

turtle.gif

Its more interesting to see the drawing process, how a pairs of perpendicular lines join together one by one to produce this figure, than watching this static image!

Now lets us try changing the parameters

ruby turtle1.rb 0.1 50 63

turtle2.gif

Try various combinations of parameters and observe the patterns produced.

Drawing with Multiple Turtles simultaneously

Idea is to make one turtle draw a circle. Then make six other turtles draw the same circle simultaneosly, but in six different orientations. Its great to see the drawing process, six turtles starting off from one single point, then travel in 6 different directions, do their work and come back to where they started.

Here is my program turtle2.rb

ruby turtle2.rb 6

turtle3.gif

Lets employ more number of turtles now

ruby turtle2.rb 15

turtle4.gif

Fractals , Recursion ad Turtles

Fractals when translated to computer language, to me it is Recursion..! I am trying to plot Koch curve which is a classical fractal curve.

koch.rb

turtle5.gif

Functional programing with DrScheme

Friday, February 15th, 2008

First of all i am thanking my guru Mr.Pramod .C.E , for his article in Linux Gazette from where i got the idea of DrScheme.
DrScheme is an interactive, integrated, graphical programming environment for the Scheme, a functional programming language,which is similar to LISP.

Drscheme can be downloaded and Installed from http://www.plt-scheme.org/software/drscheme/ A script will be available there, running which will install DrScheme and along with it, pl- games (some games in Scheme), scheme web server etc.

Now run the command “drscheme” , to start the program. Then choose scheme as our language. Try the basic built in functions like + , -,* etc. Then try writing custom functions like factorial.

DrScheme is an excellent tool for learning programming.For a beginner it will be very easy to go deeply into recursive function executions using the built in “stepper”. For using the stepper you need to change the mode as “intermediate student” in the Language menu.Now you will have the STEPPING button on the window, with which you can trace your function execution, and will be able to understand how control is moving in your code.This can be very helpful for a beginner in functional programming.

Language menu can set high privileges like advanced student option , so that we have the flexibility to define functions like this

privileges

(define (my-cube x) (* x x x))
(define (fun f x) (f x))
(fun my-cube 3)

here fun will replace f as my-cube function with a argument of 3 and returns you 27

We can also use lambda ie to call anonymous functions and define local variables inside a function.

Ex (define (fun x y)
(let ((p (* x y))
(q (+ x y)))
(* p q)))

(fun 2 3) ; evaluates to 30
The general syntax of ‘let’ is:

(let ( (var1 exprn1) (var2 exprn2) ... (varn exprn) ) body)

The three fundamental operations on a list are car, cdr and cons. Let try it in scheme.

(define a (list 1 2 3 4)) ; create a list (1 2 3 4) and bind it to ‘a’
(define b '(1 2 3 4 5)) ; same as above; note the use of the quote
(empty? b) ; returns false
'() ; empty list
(cons 1 '(2 3 4)) ; yields '(1 2 3 4)
(car '(4 1 2 3)) ; yields 4
(first '(1 2 3)) ; yields 1
(cdr '(1 4 2 3)) ; yields '(4 2 3)
(rest '(1 2 3)) ; yields'(2 3)

Bit of graphics
We can move on to simple graphic functions by loading teaching packs such as graphing.ss , draw.ss etc .Here we can write small code fragment to draw a line .

Ex:

(start 100 100) ; opens a window
(define p (make-posn 0 0)) ; creates a ‘posn’ structure
(define q (make-posn 50 50))
(draw-solid-line p q 'blue) ; which will draw a line from p to q in color blue

Also we can simulate a game called The Chaos Game , to get code for this click here program
Here is the screen shot of it.
chaosgame

Also i wished to do some turtle graphics programming stuffs in scheme but unfortunately i couldn’t able to load the library (turtle.ss) successfully. Hope i can do it next time :)