Categories
Assignments

Lab 5. GPS logging

In this lab, you’re going to learn how to access GPS data, store it on a microSD card, and then map it. First, you’ll work through the GPS Logger tutorial on Adafruit. Then, you’ll change your SD output to a more user-friendly form (you’ll reuse this code in Lab 6). Finally, you’ll map your GPS data.

Note: This lab will be a little easier for you if you have a microSD card reader on hand. If you do not have a microSD card reader, I’ve left one in my mailbox in the main office. Please use it while sitting in the main office and then put it back. Please do not take it out of the office. If it goes missing, we will collectively be SO SAD. If you want your own microSD card reader, there’s some for sale at the bookstore for $5-6. Or, lots of options on Amazon.

1. Review how a GPS works on Sparkfun.

2. Open the tutorial for the Ultimate GPS Logger on Adafruit. Read the Overview. Note that I had to do a bit of soldering to get these ready for you to use — learn about that by reading about stacking headers under the Shield Headers section (that’s what you’re using).

3. Carry on through the next section, Direct Connect. You’ll have to go outside for the GPS to work.

Note that the comments below about wiring do NOT apply to you. These same comments appear several times throughout the tutorial, but they are for the GPS breakout boards (e.g., this one), not the shield. You will not need to follow these instructions at any point throughout the entire tutorial, so ignore these lines always.

Direct Connect allows you to write immediately to your serial monitor without going through the main chip on the board. It’s a faster way of writing to the serial monitor and less prone to errors, since there’s no code involved. But, the output data is not in a useful form.

You will use Direct Connect to make sure all of your GPS hardware works. After, you will need to repeat the exercise in a different way (Soft Serial Connect) to go through the microchip, which will allow us to parse the raw GPS data into a more usable form.

4. After you successfully connect to the GPS via Direct Connect, move on to Soft Serial Connect.

Be sure to take your time reading through the tutorial and code. If you try to do it quickly, you’re very likely to make mistakes, which will ultimately take much longer.

Don’t forget to flip your switch back to Soft Serial.

What does the following sentence from the tutorial mean? “The soft serial connection works by setting up a secondary UART on two pins (digital 7 and 8) so that the main UART is free for debugging & uploading sketches.” Look up UART.

Download the Adafruit GPS library. Note that you’re using an Uno and, because of memory constraints on the Uno, you’ll have to use version 1.30 or earlier. Scroll down under File > Examples to find the code that comes with the library.

The directions in most of the Adafruit tutorials are geared towards the Arduino Leonardo (now discontinued). Again, you’re using an Uno. There are a few places in the Adafruit comments that say things like “uncomment this line for an Uno.” Be sure to follow these instructions.

Open up the leo_echo (it is now called GPS_SoftwareSerial_EchoTest in the Examples — the text in the tutorial is a little off) sketch and start to read it.

The leo_echo sketch prints the serial data it receives from the GPS. While the GPS data is now moving through the chip, at this point the serial monitor will look the same as the previous step.

The sketch will likely be overwhelming at first, but give it some time. Read through the sketch slowly. If you don’t understand a line, note that you don’t understand it. You don’t have to look up every last piece of the sketch, but work with your partner to make sure you understand most of it.

One thing to pay attention to: The code uses the words Serial and mySerial. Serial communicates across the normal serial port that you always use to communicate with your serial monitor. mySerial, an instance of the SoftwareSerial library, communicates through the secondary UART that you set up on pins 7 and 8. Serial talks to your computer. mySerial talks to the GPS.

Run your code and look at the serial monitor. I was getting a lot of ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮s.

Why? The computer can communicate at several different baud rates. The baud rate specified in the code needs to match the baud rate specified in the serial monitor. Change the baud rate in the drop down menu to the same baud rate that you passed to Serial.begin in the code. (Note you can turn off Autoscroll if it’s too much.)

The correct serial monitor output:

If the sketch is still not working for you, you’ll need to debug. Here’s a tip that will hopefully save you some time: be sure to check that your library is saved in the right place.

5. Once you have Soft Serial Connect working, keep going with the next section, Parsing Data.

GPS satellites communicate through NMEA sentences. These are the strings that you saw in your serial monitor in the Direct Connect and Soft Serial Connect sketches. NMEA sentences are difficult to understand at first glance. Adafruit’s GPS library breaks down these sentences and makes them more interpretable (i.e., “parses” them).

As before, read through this code slowly. Try to understand most of it. What is an interrupt?

When you open up your serial monitor, you should see parsed Time, Date, Fix, Location, Location (degrees), Speed, Angle, Altitude, Satellites. You’ll also see the raw NMEA sentences. At first, my serial monitor data was a bit garbled. There were extra symbols and characters scattered around the parsed data. I upped Serial’s baud rate (both in the code and in the IDE), and the problem disappeared.

6. Now, on to SD Logging.

The Adafruit code shows instructions on how to update an SD library. You do not need to do this, since you’re on an Uno. Instead, start this tutorial at the line “Then open up the Adafruit_GPS->shield_sdlog sketch and upload it to your Arduino.”

The serial monitor does not show what’s being printed to the SD card. Spend some time looking at the code and try to interpret what is being printed to the SD card.

If you stop the GPS and re-start it (i.e., plug in/unplug the Arduino), this sketch will not overwrite your previous file. It will save your data to a new text file.

7. If you’re not outside, go outside. Run the shield_sdlog sketch to collect data for 5-10 minutes.

While you’re doing that, browse through the rest of the Adafruit tutorial. You’re done! The other sections contain a lot of useful information, but that’s all we’ll do today.

Now, stop the Arduino sketch, eject the microSD card, and use a microSD card reader to open the file on your computer. What do you see?

8. Rather than the current output, let’s print the parsed time, date, latitude and longitude (in degrees).

Here is an alternative GPS Logger sketch that saves parsed data. Go to the website, scroll past the video, and down to the code. Copy and paste the code into a blank sketch in your Arduino IDE.

Look at the sketch closely. Read through it and understand it. It approaches GPS logging in a slightly less sophisticated way than the Adafruit sketch, but is perhaps more intuitive.

9. Update your new GPS logger code so the pin assignments work with the Uno. Not all the pins are currently assigned correctly for you. Change the pins at mySerial and chipSelect, as is appropriate (check shield_sdlog for information on the correct pins).

Once you’ve done this, upload the sketch to your board. Then, open the serial monitor to make sure it’s running.

10. Now, in your new GPS logger code, find the lines that write to GPSData.txt. This is the name of the file saved on your SD card.

Change the data fields that are being written to the file. Instead, write the following four fields. Separate each field by a comma (we’re eventually making a CSV):

  • Time. Write this exactly as it’s written in GPS_SoftwareSerial_Parsing, with the hour, minutes, and seconds (you don’t need milliseconds). Note: unfortunately, all single digit numbers are parsed as 1, 2, 3, …, instead of 01, 02, 03, …. This means that the time for 12:01 is written as 12:1. We won’t fix this right now, but it’s a flaw.
  • Day. Write this exactly as it’s written in GPS_SoftwareSerial_Parsing, with the day, month, and year separated by a slash.
  • Latitude in degrees. This function call  is available in GPS_SoftwareSerial_Parsing.
  • Longitude in degrees. This function call  is available in GPS_SoftwareSerial_Parsing.

Upload your revised sketch.

11. Test your GPS’s SD logging. Run the GPS for a short time, making sure you get a satellite fix. Unplug the GPS, take out the SD card, and pop the SD card into your computer. The file should look like this:

12. BEWARE ☠ , if you turn on and off your GPS, you’ll overwrite any saved data. Amend your code to NOT overwrite.

You can do this as you want, but here’s some quick tips on how I suggest proceeding:

  • Look at how filename is treated in shield_sdlog
  • Copy sheild_sdlog‘s strcpy and for statements into your setup
  • Declare filename as a global variable
  • Do not copy over any references to logfile
  • Comment out the setup code that refers to deleting files
  • Change the SD.open line to: “mySensorData = SD.open(filename, FILE_WRITE);”

13. Once you have your logging sorted out, go outside and MOVE for approximately 10 minutes to collect a longer run of data. Go on a walk. Or a bike ride. Get the GPS outside for a little bit. Do not just sit in one location.

14. Open your log file on your microSD card reader. Go to File > Save as. Choose “All Files” under the “Save as type” drop down menu. Then, manually type in .csv. Click save and close your file.

15. Map your data! You may use whatever program you want.

Before you get into this recall from the direct connect sketch, “Despite appearances, the geolocation data is NOT in decimal degrees. It is in degrees and minutes in the following format: Latitude: DDMM.MMMM (The first two characters are the degrees.) Longitude: DDDMM.MMMM (The first three characters are the degrees.)” You will need to convert your data into decimal degrees. Here’s an approach on stack overflow (the top answer is a good answer).

One easy way to map your data is with Google Maps. Here are instructions:

  • Go to Google Maps
  • Sign into your account
  • Click on the menu (the three little lines next to the search bar)
  • Click on Your Places > Maps > Create Map
  • Under Untitled layer, click Import
  • Upload your new CSV
  • Define your latitude and longitude fields
  • Title your markers. The time column would be best, but since single digits are not parsed correctly, time or date is fine.
  • Finish!
  • Style the map as you want.
  • Save the map as a PDF.

16. Together (as in, one assignment), you and your partner should submit your well-commented code (8 points) and your map (2 points) to Canvas.

One reply on “Lab 5. GPS logging”

Leave a Reply

Your email address will not be published. Required fields are marked *