3.12.2014

GPSd, Java, and the Raspberry Pi

The float for RoboGoby will have a Global Positioning System, or GPS, onboard for real-time tracking. GPSs are readily available and extremely easy to interface into your project. There are various different GPS module from Sparkfun, Adafruit, Mouser, and other distributors. They come with different antennas, although a custom fit ceramic antenna is the most common.

Ceramic antenna

GPSs also come with different update rates (in Hz) and different channels. Channels represent the number of satellites a receiver can receive data from at the same time. And Hz, or hertz, is the speed of your module. If you're not sure which GPS is right for your project, check out SparkFun's GPS Buying Guide

The awesome thing about GPS receivers is that soon after they receive power, they continuously output a string of ASCII characters through a serial cable/UART connection. The string contains coordinates, altitude, time, speed, directional degrees, etc.  In order to read the data from a GPS you need to download a GPS parser which can decode the GPS ASCII and then implement in your code (in our case a simple Java program). 

We are currently using a pre-constructed USB GPS (image below -- you can see the ceramic antenna inside). Other GPSs (like the Ultimate GPS from Adafruit) can be wired to an Arduino or Pi using UART (more info found here).


USB GPS Reciever




To test the GPS, we used a generic python setup from Adafruit (found here). The following quick steps make sure the GPS is working (our USB GPS was located on /dev/ttyUSB0). After making sure the GPS is working, you can add the python code, gps.txt file, and Java code we used to read the GPS data (we had to write the data to a file using python and then read it using Java. Although this means switching platforms it is ten-fold easier than using Java directly -- trust us, we tried).


If you type in the following you will see a bunch of unknown characters. This is because the Pi cannot decipher the ASCII strings. You'll need to download GPSd to do that.




1. Download GPSd and the python library needed to read GPS data:




2. Tell GPSd where to look on the Pi for the data stream (in our case /dev/ttyUSB0)




3. Make sure that your GPS is using GPSd and receiving data from your module:

GPS Data


...and using the same cat command above you now see recognizable data (somewhat recognizable...)!




The next step is parsing the data into a usable form. For this, we are going to use Java. (make sure you have Java installed ---> sudo apt-get install openjdk-7-jre). The Java code will read the data written to a text file using Python. The python code uses the TPV-Class to read the lat and lon data (check out the link for the full set of data available using TPV).

All of the code can be found in the GPS folder located on our download site. Below is a quick explanation of what the code does.

1. The python code (gpsPi.py) reads the data from the GPS and parses it into recognizable latitude and longitude values. It then writes this data to gps.txt and then immediately runs gps.jar. The Java file (gps.jar) reads the data from gps.txt and converts it into a DD°MM.M'SS.SSS" format. Pictures of the code are below:

***do not name your python file gps.py as the method it uses to call the GPS module will NOT work.


gpsPi.py

gps.jar



…and this is the data output on the Pi!




To make sure your coordinates are correct check out this sweet website.

5 comments:

  1. cant seem to get the python code working

    ReplyDelete
    Replies
    1. I assume you've followed the steps above (gpsd download, python-gps client download, etc) and the GPS is working (ie. cat /dev/ttyUSB0 spits out strings).

      Download the entire gpsPi.py file (it was not previously on our downloads page). It includes a few things that were not in the picture or in the example code provided by Adafruit. Find the GPS folder on this site: https://sites.google.com/site/projectrobogobydownloads/ and download the gpsPi.py file.

      The gps.txt file along with the gps.jar executable and the gpsPi.py script also must be located in:

      /home/pi/scripts/gps

      If you are not working in /home/pi/scripts/gps change the reference directory in the Java example code (also found on our download page). If neither of the solutions above work, just reply and we'll try to find the solution to your problem.

      I hope this helps! Thanks for commenting!

      Delete
    2. thanks that code work,im try find way for the java to call python class first and then read that data.

      Delete
    3. got it working thanks so very much
      so it call the python before the reading the gps.txt
      ProcessBuilder pb = new ProcessBuilder("sudo", "python", "gpsPi.py");
      pb.redirectErrorStream(true);
      Process proc = pb.start();
      BufferedReader br= new BufferedReader (new InputStreamReader(proc.getInputStream()));

      Delete
    4. You're welcome! Consolidating everything into one Java script makes everything a lot cleaner. Nice call. Thanks again for the comment. We're available to help anytime.

      Delete