Reading a GPS Receiver with Ruby

Sunday May 4 at 5 PM by

I recently started purchasing parts for a carputer. One of the most basic features expected of a carputer is of course a GPS navigation system. During the course of testing some of the more popular open source packages, the programmer inside of me began to wonder how difficult it would be to connect to the GPS receiver and read the coordinate data. It turns out it is pretty simple.

I'm running Ubuntu 8.04 with a US GlobalSat BU-353 receiver. After a bit of poking around it seems that the gpsd package is the preferred method of abstracting the various GPS receivers and providing a uniform interface for developers. I tend to prefer ruby for most of my work so that is what I used here.

First we need to grab the required packages:

sudo apt-get install ruby rubygems gpsd
sudo gem install gps

gpsd does not automatically start so we'll need to manually launch it. The -n option runs it in the background and the /dev/ttyUSB0 is my GPS receiver.

sudo gpsd -n /dev/ttyUSB0

Next I created a simple ruby script cleverly named 'test' and added the following code:

#!/usr/bin/ruby

require 'rubygems'
require 'gps'

gps = Gps::Receiver.create("gpsd")
gps.start

gps.on_position_change(0.0003) do
  puts "#{ gps.latitude }, #{ gps.longitude } @ #{ gps.speed }"
end

while true
  sleep(0.1)
end

This script will run indefinitely and print out the coordinates as it changes. The '0.0003' is a threshold value to prevent vary small changes in the position from triggering the on_position_change event. I changed the real coordinates to protect my true location!

josh@phoenix:~$ ruby test
41.948032 -87.655299 @ 0.0
41.948031 -87.655297 @ 0.0
41.948030 -87.655298 @ 0.0

0 Comments

Start the discussion:


(required)
(required, not published)