Archive for the 'Ruby' Category

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

Print

mp3 tag editor using Ruby

Friday, February 8th, 2008

Was listening to music and thought of changing the mp3 tags. Quickly i opened the file and right clicked to change it, but unfortunately i couldn’t. So i thought of building a small application which will help to me to play with these tags.

I searched for some helpers in ruby and came to know about “RUBY-MP3INFO” which will help you to read, write, remove id3v1 and id3v2 tags.You can easily do this in few steps.
Installation
$ ruby install.rb config
$ ruby install.rb setup
# ruby install.rb install

Or simply
gem install ruby-mp3info
Documentation is available at http://ruby-mp3info.rubyforge.org/

We knows the tags now with the help of documentation . Using some simple coding we can access this tags , the following code fragments will help to do the rest.

Dispaly all tags

Show_tags

run as ./mp3tags.rb -tags <mp3filename>; , which will give u that tags of that mp3 file

Now we can expand this code to insert our own tags.

chaging_tag

run ./change_tag.rb -s <field> <fieldname> <mp3 filename>; it will help u to change the <field> of <filename> with <fieldname>

After doing these i thought of giving a directory as input and want to change the field of all the files insidfe that directory.For doing that FileTest.directory?(arg) in ruby helped to find out whether the passed argument is a file or directory.

directory

run ./directory.rb -dir <field> <fieldname> <directoryname>; which will change the field of all files inside the directory name with given fieldname

Atlast I integerated all these together which which perfectly working and giving you access to read, write, remove id3v1 and id3v2 tags

change_tags.rb

run /change_tags.rb with respective field

changing tags

Fianlly we can play with tags of mp3 files.
Also we can apply “Tk” tool of our Ruby and we can have a GUI for dispalying the tags and editing fields.If you installed Ruby yourself, possibly the Tk bindings do not work , so make sure that by requiring Tk on irb

irb>>require("tk")
true
irb>>

Or u can easily install this by

sudo apt-get install libtcltk-ruby

Gui form

gui

Hope this will help u and make a try out of this :)

Print

Some more fun with Ruby and Instant messaging

Tuesday, January 22nd, 2008

AI used to be one of my favorite subjects right from college days. I was searching for some AI algorithms and happened to find a ‘chat-bot’ called Eliza in between. ‘She’ was good enough, even though she doesn’t perfectly simulate a human user, she does a pretty good job. The next moment i wanted to use Eliza to talk to my friends on behalf of me through my Instant messenger.

Since i already had some ruby programs which could talk to IM servers, an interface to Eliza was just what i needed. I visted the Eliza’s webpage, one of the simplest webpages ever. It just had a plain html form, with a textfield and a submit button. The page source said that textfield’s name was “Entry1″. That was all i needed !

Here’s my Ruby interface for Eliza :

def eliza(str)
response = Net::HTTP.post_form(URI.parse("http://www-ai.ijs.si/eliza-cgi-bin/eliza_script"),{'Entry1'=>str})
return response.body.split("</strong>\n").last.split("\n").first
end

Hats off to Ruby…!

Now whenever a chat message arrives on my IM program, i simply call this ‘eliza’ method, get a response string from Eliza, and send it as reply in the IM program.

Here’s the full Ruby code for the Eliza chat program.

Print

VPython-Python module for 3 Dimensional scientific programming

Friday, January 18th, 2008

Battle with VPython was pretty good. In my opinion VPython is a very powerful one. VPython is very interesting to make small applications. First of all we have to install VPython which is quite easy. Also we can mix music & such multimedia along with our applications, for that we just need to install python-gamepy package. Its not only for playing music but also we can add up many more things.

apt-get install python-visual

It will be very useful for implementing physics applications, students can visualize their experiments and study easily with the help of VPython. They can easily write a small code set of their experiment with the formulas.. Coding in python won’t be a big problem even for a newbie :) . VPython documentation is very simple with proper examples which will make our task much more easy & interesting.

hello

“HELLO” as 3D

VPython will help us to plot graphs of the equations and which can sense the mouse click, key board interactions etc. So that we can easily implement anything as we wish.For all these things we have plenty of functions in the VPython library.

Mouse clicks

We studied a lot of stuffs with sine and cosine functions , VPython made the plotting of such functions very easier.Just try the following for getting a cosine curve

from visual import * # just to import your visual python
scene.title = "Cosine Curve"
scene.center = vector(0,0,0)
xaxis = box(length= 20, height=0.2, width= 0.5, color=color.orange)
cosinecurve = curve( color = color.blue, radius=0.2)
dt = 0.2
for t in arange(0,10,dt):
dydt = vector( t,cos(t), 0 );
cosinecurve.append( pos=dydt, color=(1,0,0) )
rate( 500 )

Just run your .py program to get a cosine wave in ur window .

Cosine Wave

While our code is running, VPython runs another program (“parallel thread”) which periodically gets information about the attributes of our code segment.
For example for finding out the position of a object, need to specify velocity of the ball ,then take a small interval of time dt , we knows velocity=displacement/time ,so we have current position , so adding current position with the product of velocity and time gap will give you the periodic updates of the position of that object using a parallel thread. So which making our task simple :)

so its worth trying on VPython :) Links to get the materials on VPython

Print