Skip to main content

Beam me up, Rosie!

How do you get from A to B?

You can't, as As and Bs are just letters in the alphabet. But if A is your house, and B is a meerkat village at your favourite safari park, you'd probably use a device equipped with GPS.  Not to be confused with UPS, who will deliver you your chosen meerkat through the post. And why on Earth would Rosie Patrol need one? Precisely, it's because she is on Earth that she needs one. Because our planet is rather big. Big enough to get lost in. And we don't want to lose our friendly plastic boxes on wheels. And maybe, eventually when she's clever enough, she'll go and defeat baddies on her own. And return home afterwards for a well deserved Earl Grey tea.

Besides, why wouldn't we want to add another three letter acronym to Rosie Patrol's repertoire?

All superheroes need:

  • One Raspberry Pi 3, running Raspbian OS
  • Computer from which you are connecting to the Raspberry Pi
  • Probably the most important bit: a GPS receiver thingmy.   There are many different types available.  We're using a USB dongle: a Diymall Vk-172.  Under the covers, we're told it is powered by a U-Blox UBX-G7020 chip.

Already completed these missions?

You'll need to have completed the Rosie series.  Then:
  1. Lights in shining armour 
  2. I'm just angling around 
  3. Eye would like to have some I's
  4. Eh, P.I?  
  5. Lights, camera, satisfaction

Your mission, should you accept it, is to:

  • Connect the GPS receiver to the Pi.  It's a USB device.  Can you guess where we need to plug this into?
  • Install gpsd which is what we'll use to interact with the receiver
  • Test that we can contact satellites in space.  And that we can get in on the geolocation action.
  • Install gpsd-py3 to allow us to interact with gpsd, using Python3
  • Track you robot as it fights crime across the globe

The brief:

In case you were wondering, GPS stands for Global Positioning System.  Using GPS, a receiver based down on Earth (yes, that's our USB dongle thingmy) can contact a few GPS satellites in space, and obtain geolocation information.  Which means we can now make wild (but actually quite true) claims about our robot being able to contact satellites in space.   More importantly, it means Rosie Patrol can find out for herself where she is on Earth.

Drones have them.  Watches have them.  Smart phones have them.  So it's only natural that robots should have GPS too.

And surprisingly, thanks to the people who have already thought about all this (not us!), it's not very difficult to get some pretty interesting, real-life data.  Without doing too much engineering ourselves.

We'll leave what you do with that data up to you.  You could draw a map of where your robot has been.  Or maybe you'll use it to guide your creation across town.

But - for now - let's get GPS up and running.

The devil is in the detail:

Do you know how to insert a USB device into a USB port?  Good.  That's about it for the hardware.  We hope we didn't disappoint.

Next, let's dabble in a bit of Linux.  Actually, more than just a bit.  Firstly, run lsusb to see what USB devices are connected to your Pi, and what the Raspbian OS sees.

sudo lsusb
...lists USB device information

Connect the GPS receiver to the USB port on the Pi, and try again.   That's promising!  We can see a 'U-blox' device, which suspiciously looks like our GPS USB dongle thingmy.  Let's see it in another place, just to be sure. 

View the Raspbian OS's Linux kernel log (/var/log/kern.log) using cat and filter for text 'u-blox' using grep.

cat /var/log/kern.log | grep u-blox
...nothing to do with actual cats.  We are simply viewing the content of this log file.  We're interested in just the lines with the text 'u-blox'.

We can see entries from the time the device was connected.  So where is this device, actually?  And what is its name in Linux?  Looking at other log entries from around the time the receiver was connected to the USB port, more is revealed.

cat /var/log/kern.log
...still, nothing to do with cats.  This time, we want to trawl through the whole file, without grep.

This suggests that the device isn't named Harry, or Henry, or Henrietta.  It's actually called... wait for it... ttyACM0.  It's really memorable, isn't it?  Look in our /dev Linux device directory and you'll find it there, waiting to be found.

ls /dev/ttyACM0
...lists the ttyACM0 device file

...At which point you can attempt to read the raw data from the device using cat (but good luck understanding it).

cat /dev/ttyACM0
...attempts to read content of ttyACM0 device file.  Nope, not really of any use.

There is garbled text that looks suspiciously like some form of GPS geolocation data, but it's all a bit messed up really.  There's no way we can work with this.  This is where gpsd comes in.  It can do all the funny GPS language -> English translation for us, and we can only query the information that we want.

This means we need to install some Linux packages.  And we do this using apt-get.

sudo apt-get install gpsd gpsd-clients python-gps
...downloads and installs gpsd, gpsd-clients and python-gps Linux packages

Once everything's been installed, let's temporarily stop the gpsd service from starting up automatically when the Pi boots.  Why?  Because we just want to manually start it, and see how it behaves.  We'll re-enable this later.

sudo systemctl stop gpsd.socket
sudo systemctl disable gpsd.socket
sudo gpsd /dev/ttyACM0 -F /var/run/gpsd.sock
...uses systemctl to stop the gpsd service, and disable it.  We then manually start up gpsd, while pointing it at the /dev/ttyACM0 device. 

This is indeed the moment of truth.  Now, gpsd is running.  And it should be deciphering the geolocation data from our GPS receiver (and doing a better job of it, than we did when we had a peek).  To check if this is the case, we can run a helpful test tool - cgps.

cgps -s
 ...runs cgps.  And enjoy the wealth of data being provided to us by the satellites in the sky!  This really is Rosie Patrol, talking to things in space.

If everything is working successfully, you should see a neat table, with lots of useful information.  The most useful for geolocation - of course - is the latitude and longitude data, which is an important component of the geographic coordinate system that us humans use to describe a specific position on Earth.  There are other bits of useful information here too, like speed and elevation.

There's a very good reason why we're blotching out our own GPS coordinates with a big white blob.  It's because GPS coordinates tell people where you are - exactly.  Well, that is the whole point of GPS isn't it?

Never provide your GPS coordinates to people you don't know, or publish them on the Internet, unless you want them to know exactly where you are.  In most cases, there just won't be a good reason to.

So how do you make sense of these coordinates?

The easiest way to quickly visualise it, is to use Google Maps.  For example, if your Latitude says '51.188637 N' and Longitude says '2.262735 W', enter '51.188637 N, 2.262735 W' in Google Maps.

Congratulations.  Your robot is in the middle of a Safari Park.  Hopefully, somewhere near the meerkat village.  And not in the tiger zone. So far so good.  It's all working out nicely.  And we've completed the hardest part of this challenge.

The only bit remaining is to wrap it all up, using Python.  And you won't be at all surprised to know that there are some Python libraries out there that allow you to interact with gpsd.

Let's install gpsd-py using pip.

sudo pip3 install gpsd-py3
...downloads and installs gpsd-py3 Python 3 library for gpsd

Let's now fire up IPython.  All we need to do is import the gpsd library, run a connect() to hook up with gpsd, and retrieve the latest GPS data using get_current().  For position information, you can get a tuple containing your longitude and latitude coordinates using position().  It really couldn't be any easier...

import gpsd
data = gpsd.get_current()

Now, do you remember we disabled automatic start-up of the gpsd service?  We want to enable it again.  First, edit the /etc/default/gpsd configuration file to point it at our GPS receiver.

sudo nano /etc/default/gpsd
...uses Nano to open the file.  Enter the device name (ttyACM0) where it is currently blank.

Then let's enable the service again.  And start it up.

sudo systemctl enable gpsd.socket 
sudo systemctl start gpsd.socket
...uses systemctl to enable the gpsd service, and start it up

To prove the point, we'll reboot the Pi.  And check if the service is running.

sudo reboot
sudo systemctl status gpsd.socket
...reboots Pi.  Use systemctl to check status of gpsd service.

You should be able to get right back into IPython and connect to gpsd to retrieve your latest data.  This proves that our gspd service starts up automatically, and is pointing at our USB GPS receiver.

Now what you do with the data is up to you.  You could store it in a directory on the Pi, so you can draw the route your robot has taken during its mission.  Or, you could use it to control it in real time.  Could you, for example, tell it where to go based on GPS waypoints if the robot is equipped with a compass?

Here is a snippet of the Python code we used to write the longitude and latitude coordinates to a file, every second.

import gpsd
import time
import datetime

# Wait 60 seconds before connecting to gpsd
date ="%d_%m_%Y_%H_%M_%S")
filename = "geo/geodata_" + date + ".txt"
while True:
        data = gpsd.get_current()
        position = data.position()
        f = open(filename, "a") 
        f.write(str(position[0]) + "," + str(position[1]) + "\n")

Configure the above using Supervisor and your Pi will start to log its coordinates to a text file in the "/geo" directory.  The longitude and latitude will be values separated by a comma, which makes it perfect for processing.  There are plenty of websites - such as this or this - that allow you to copy and paste the coordinates to plot them on Google Maps.  Here's the result of our little detour around a park.

For something so sophisticated, the steps were actually quite simple.  We can now say Rosie Patrol is 'GPS-enabled' and track her as she wanders around the meerkat village.  And hopefully not the lion zone.

Information overload:

Our USB GPS receiver is based on the U-blox UBX-G7020 chipset:
Here's another guide on how to set up a GPIO receiver with a Pi (although the steps are different from ours, due to different hardware being used):
The gpsd-py3 library can be found on GitHub here:
And, finally, extensive GPSD documentation is available here:


Popular posts from this blog

Tea minus 30

We're fast approaching Christmas time.  And if robots were to make one simple observation about the human species during the Christmas festivities, it's that they watch a lot of TV.  A LOT.  Often, accompanied by an inappropriate amount of greenhouse gas-producing food.  Stuff you don't normally eat during the remainder of the year - for good reason.

And most so-called shows on TV are boring to robots like Rosie.  After all, why watch a minor subspecies of the human race - celebrities - stumble awkwardly around the dance floor, dressed like a faulty, sparking circuit board?  Such branch of entertainment doesn't require robots to engage any of their proud circuitry.  Their processors remain idle.  Memory under-utilised.

But if robots are to be part of people's homes (and blend in), they need to look at least a little interested in some of this irrational nonsense.  Nobody likes a party pooper.  A killjoy.  And this is where a certain subgenre of TV entertainment co…

Hello supervision, goodbye supervision

The time has finally come, to let our precious Rosie go.  Wish her luck (yes, she'll really need it).  Press the big red button.  And watch her destroy / navigate (delete as appropriate) the terrain*.  All on her own.  Without your help.  Well, sort of.

*Somewhat scientific term... for your lounge

You see, cool robots are never meant to be controlled by us clumsy humans.  If they want to be clumsy, they can be clumsy on their own.  That way we can deny all responsibility when they crash into the neighbour's car.  Or, less importantly, take over the world.

And by the way, Autonomous stuff seems to be in fashion these days.  Self-driving cars are just around the corner (maybe quite literally).  Very clever computers also beat humans at chess, apparently.  After all, if robots only do what we tell them to do, they are going to need us.  They're going to be just like us.  Oh no.  We want them to work alongside us, to do things that we simply can't be bothered to do.  Like …