I just love GPS. It's about as close as you could get to living in the world of James Bond in terms of gadget coolness. When developing Firewing, we just had to have a GPS shield! The Firewing GPS shield is based around the Fastrax UP501 GPS module. This is a fantastic unit which has an embedded antenna and tiny footprint. It also has a built in UART, making communicating with this device a breeze. The GPS shield also has a built in Secure digital (SD) card slot, which allows you to transform your shield into a GPS data logging solution! Probably the best way to see what the shield can do is actually do something with it! This is a quick step guide to get a car GPS logger up and running. We'll talk about how it all works later...
- Insert a formatted microSD card into the GPS shield SD card slot.
- Plug the GPS shield into your Firewing board
- Download, compile and then program the following sample program into your Firewing board.
- Plug a battery pack into the Firewing main board. You can use a 9v PP3 if you like, but be warned the GPS module will soon drain one of these!
- Place the Firewing units on your dashboard and wait until the "FIX" LED starts blinking
- Go for a drive!
Displaying the Results
When you get back, plug the SD card into your computer. You will see a file called "GPS.csv". The *.csv extension means "comma delimited text" and is basically a text file of data separated by commas. For example,
Each line of the above shows date, time, latitude, longitude and speed. However, it's not very human friendly. What we want is to display the data in an application such as Google Earth. To do this, we need to convert the *.csv data into a *.kml file that Google Earth understands. I wrote a little utility which allows you to do just that - download this CSV to KML converter program and press the "Convert CSV..." button. Point the program to the *.csv file and then give a name for the *.kml file to generate. That's it! When the program has finished, just double click on the generated *.kml file to see you route rendered in Google Earth. Clicking on a point will display a popup window showing the date, time and speed you were traveling.
The converter program has a "config.ini" file which you can use to change certain conversion options. For example, the image filename and also the size of the image used for the GPS location icon. You can also change the unit of speed used. By default, the GPS unit uses knotts. However, you can get the program to convert speed into MPH or KMH by specifying unit=mph or unit=kmh from within the "config.ini" file.
How the Program Works
The GPS unit outputs its data via the built in UART as National Marine Electronics Association (NMEA) sentences. Each sentence has different information associated with it. For example, date and time information or locational data, such as latitude and longitude. The UP501 outputs a number of different NMEA sentences every one second by default (1Hz). The two most useful of these are called $GPGGA and $GPRMC and look something like this:
The first field is called the sentence identifier (in the above example, either $GPGGA or $GPRMC) followed by the actual data fields. Each field is separated by a comma and each sentence is terminated with a checksum. We are only really interested in a few of these fields, but if you want more detail about each data field, then check out this site here.
The GPS shield outputs its data on pin D2 - this can be changed, but by default it is pin D2. The GPS $GPGGA and $GPRMC sentence data is buffered by the Firewing GPS module. This allows your program to do other processing tasks until it is ready to process the GPS data. We could of course just output the raw GPS sentence information directly to the SD card. This is a particularly good strategy if you want to guarantee data integrity or have all sentence information available for later processing. However, for this program we are going to pull out chunks of data from each sentence and save these fields only. This is a good strategy if you want your program to perform any processing on incoming data and also reduce the size of the *.csv file generated.
The sample program we are using performs additional processing on the incoming data. It formats the date and time fields but more importantly, it converts latitude and longitude output by the GPS module into WGS84 format, which is used by many different types of applications, such as Google Earth, using this simple piece of code:
if Gps.GetField(rmc, 2, data) and Gps.GetField(rmc, 3, bearing) then lat = Gps.ConvertToWGS84(data, bearing(0)) haveLat = true end if
In the above example, the second data field (the sentence identifier is indexed at zero) and also the third data field from a $GPRMC sentence are extracted as string values and then converted into a WGS84 formatted string value, which is then saved to the SD card.