Python and GPS Tracking - SparkFun Electronics

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

8/7/13

Python and GPS Tracking - SparkFun Electronics

0 items in cart $ USD US Dollar Canadian Dollar Australian Dollar British Pound Euro More Currencies... Wish Lists You are not logged in. log in email Forgot your password? No account? Register one! SparkFun ElectronicsCategories New Products Top Sellers Staff Picks Gift Certificates Classes & Events

password

Arduino Books Breakout Boards Cables Cellular Components Development Tools Dings and Dents E-Textiles Educators GPS Kits LCDs Programmers Prototyping Retail Retired Robotics Sale Sensors Swag
https://www.sparkfun.com/tutorials/403 1/16

8/7/13

Python and GPS Tracking - SparkFun Electronics

Tools Widgets Wireless Home | Tutorials | Python and GPS Tracking

Python and GPS Tracking


Skill Level: Advanced

by a1ronzo | December 17, 2012 | 17 comments

Introduction
In my quest to design a radio tracking system for my next HAB, I found it very easy to create applications on my computer and interact with embedded hardware over a serial port using the Python programming language. My goal was to have my HAB transmit GPS data (as well as other sensor data) over RF, to a base station, and graphically display position and altitude on a map. My base station is a radio receiver connected to my laptop over a serial to USB connection. However, in this tutorial, instead of using radios, we will use a GPS tethered to your computer over USB, as a proof of concept. Of course, with an internet connection, I could easily load my waypoints into many different online tools to view my position on a map, but I didn't want to rely on internet coverage. I wanted the position of the balloon plotted on my own map, so that I could actively track, without the need for internet or paper maps. The program can also be used as a general purpose NMEA parser, that will plot positions on a map of your choice. Just enter your NMEA data into a text file and the program will do the rest.

https://www.sparkfun.com/tutorials/403

2/16

8/7/13

Python and GPS Tracking - SparkFun Electronics

Showing a trip from SparkFun to Boulder, CO. This tutorial will start with a general introduction to Python and Python programming. Once you can run a simple Python script, we move to an example that shows you how to perform a serial loop back test, by creating a stripped down serial terminal program. The loopback test demonstrates how to send and receive serial data through Python, which is the first step to interacting with all kinds of embedded hardware over the serial port. We will finish with a real-world example that takes GPS data over the serial port and plots position overlaid on a scaled map of your choice. If you want to follow along with everything in this tutorial, there are a few pieces of hardware you will need. 5V FTDI Basic EM-406 GPS module with Breakout Board (or any NMEA capable GPS) For the loopback test, all you need is the FTDI Basic. For the GPS tracking example, you will need a GPS unit, as well as the FTDI.

What is Python?
If you are already familiar with installing and running Python, feel free to skip ahead. Python is an interpreted programming language, which is slightly different than something like Arduino or programming in C. The program you write isn't compiled as a whole, into machine code, rather each line of the program is sequentially fed into something called a Python interpreter. Once you get the Python interpreter installed, you can write scripts using any text editor. Your program is run by simply calling your Python script and, line by line, your code is fed into the interpreter. If your code has a mistake, the interpreter will stop at that line and give you an error code, along with the line number of the error. The holy grail for Python 2.7 reference can be found here: python.org

Installing Python
At the time of this tutorial, Python 2.7 is the most widely used version of Python and has the most compatible libraries (aka modules). Python 3 is available, but I suggest sticking with 2.7, if you want the greatest compatibility.
https://www.sparkfun.com/tutorials/403 3/16

8/7/13

Python and GPS Tracking - SparkFun Electronics

Download and Install Python 2.7 After you install Python, you should be able to open a command prompt within any directory and type 'python'. You should see the interpreter fire up.

If you don't see this, it is time to start some detective work. Copy your error code, enter it into your search engine along with the name 'python' and your OS name, and then you should see a wealth of solutions to issues similar, if not exact, to yours. Very likely, if the command 'python' is not found, you will need to edit your PATH variables. More information on this can be found here. FYI, be VERY careful editing PATH variables. If you don't do it correctly, you can really mess up your computer, so follow the instructions exactly. You have been warned. If you don't want to edit PATH variables, you can always run Python.exe directly out of your Python installation folder.

Running a Python Script


Once you can invoke the Python interpreter, you can now run a simple test script. Now is a good time to choose a text editor, preferably one that knows you are writing Python code. In Windows, I suggest Programmers Notepad, and in Mac/Linux I use gedit. One of the main rules you need to follow when writing Python code is that code chunks are not enclosed by brackets {}, like they are in C programming. Instead, Python uses tabs to separate code blocks, specifically 4 space tabs. If you don't use 4 space tabs or don't use an editor that tabs correctly, you could get errant formatting, the interpreter will throw errors, and you will no doubt have a bad time. For example, here is a simple script that will print 'test' continuously.
#s i m p l es c r i p t d e ft e s t ( ) : p r i n t" t e s t " w h i l e1 : t e s t ( )

Now save this code in a text editor with the extention your_script_name.py. The first line is a comment (text that isn't executed) and can be created by using a # . The second line is a function definition named test(). The third line is spaced 4 times and is the function body, which just prints "test" to the command window.
https://www.sparkfun.com/tutorials/403 4/16

8/7/13

Python and GPS Tracking - SparkFun Electronics

The third line is where the code starts in a while loop, running the test() function. To run this script, copy and paste the code into a file and save it with the extention .py. Now open a command line in the directory of your script and type:
p y t h o ny o u r _ s c r i p t _ n a m e . p y

The window should see the word 'test' screaming by.

To stop the program, hit Ctrl+c or close the window.

Installing a Python Module


At some point in your development, you will want to use a library or module that someone else has written. There is a simple process of installing Python modules. The first one we want to install is pyserial. pyserial Download the tar.gz file and un-compress it to an accessible location. Open a command prompt in the location of the pyserial directory and send the command (use sudo if using linux):
p y t h o ns e t u p . p yi n s t a l l

You should see a bunch of action in the command window and hopefully no errors. All this process is doing is moving some files into your main Python installation location, so that when you call the module in your script, Python knows where to find it. You can actually delete the module folder and tar.gz file when you are done, since the relevant source code was just copied to a location in your main Python directory. More information on how this works can be found here: Installing Python Modules FYI, many Python modules can be found in Windows .exe installation packages that allow you to forgo the above steps for a 'one-click' installation. A good resource for Windows binary files for 32-bit and 64-bit OS can be found here: http://www.lfd.uci.edu/~gohlke/pythonlibs/

Python Serial Loopback Test


This example requires using an FTDI Basic or any other serial COM port device.

https://www.sparkfun.com/tutorials/403

5/16

8/7/13

Python and GPS Tracking - SparkFun Electronics

Simply, connect the TX pin to the RX pin with a wire to form a loopback. Anything that gets sent out of the serial port transmit pin gets bounced back to the receive pin. This test proves your serial device works and that you can send and receive data. Now, plug your FTDI Basic into your computer and find your COM port number. We can see a list of available ports by typing this:
p y t h o nms e r i a l . t o o l s . l i s t _ p o r t s

If you are using linux:


d m e s g|g r e pt t y

Note your COM port number. Now download the piece of code below and open it in a text editor (make sure everything is tabbed in 4 space intervals!!):
i m p o r ts e r i a l # # # # # G l o b a lV a r i a b l e s # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # b es u r et od e c l a r et h ev a r i a b l ea s' g l o b a lv a r 'i nt h ef x n s e r=0 # # # # # F U N C T I O N S # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # i n i t i a l i z es e r i a lc o n n e c t i o n d e fi n i t _ s e r i a l ( ) : C O M N U M=9# s e ty o uC O Mp o r t#h e r e g l o b a ls e r# m u s tb ed e c l a r e di ne a c hf x nu s e d s e r=s e r i a l . S e r i a l ( ) s e r . b a u d r a t e=9 6 0 0 s e r . p o r t=C O M N U M-1# s t a r t sa t0 ,s os u b t r a c t1 # s e r . p o r t=' / d e v / t t y U S B 0 '# u n c o m m e n tf o rl i n u x # y o um u s ts p e c i f yat i m e o u t( i ns e c o n d s )s ot h a tt h e #s e r i a lp o r td o e s n ' th a n g s e r . t i m e o u t=1 s e r . o p e n ( )# o p e nt h es e r i a lp o r t #p r i n tp o r to p e no rc l o s e d i fs e r . i s O p e n ( ) : p r i n t' O p e n :'+s e r . p o r t s t r # # # # # S E T U P # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # t h i si sag o o ds p o tt or u ny o u ri n i t i a l i z a t i o n s
https://www.sparkfun.com/tutorials/403 6/16

8/7/13

Python and GPS Tracking - SparkFun Electronics

i n i t _ s e r i a l ( ) # # # # # M A I NL O O P # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # w h i l e1 : # p r i n t sw h a ti ss e n ti no nt h es e r i a lp o r t t e m p=r a w _ i n p u t ( ' T y p ew h a ty o uw a n tt os e n d ,h i te n t e r : \ n \ r ' ) s e r . w r i t e ( t e m p )# w r i t et ot h es e r i a lp o r t b y t e s=s e r . r e a d l i n e ( )# r e a d si nb y t e sf o l l o w e db yan e w l i n e p r i n t' Y o us e n t :'+b y t e s# p r i n tt ot h ec o n s o l e b r e a k# j u m po u to fl o o p # h i tc t r ct oc l o s ep y t h o nw i n d o w

First thing you need to do before running this code is to change the COM port number to the one that is attached to your FTDI. The COMNUM variable in the first few lines is where you enter your COM port number. If you are running linux, read the comments above for ser.port. Now, if you want to send data over the serial port, use:
s e r . w r i t e ( y o u r _ d a t a )

your_data can be one byte or multiple bytes. If you want to receive data over the serial port, use:
y o u r _ d a t a=s e r . r e a d l i n e ( )

The readline() function will read in a series of bytes terminated with a new line character (i.e. typing something then hitting enter on your keyboard). This works great with GPS, because each GPS NMEA sentence is terminated with a newline. For more information on how to use pyserial, look here. You might realize that there are three communication channels being used: 1. ser.write - writes or transmitts data out of the serial port 2. ser.read - reads or receives data from the serial port 3. print - prints to the console window Just be aware that 'print' does not mean print out to the serial port, it prints to the console window. Notice, we don't define the type of variables (i.e. int i = 0). This is because Python treats all variables like strings, which makes parsing text/data very easy. If you need to make calculations, you will need to type cast your variables as floats. An example of this is in the GPS tracking section below. Now try to run the script by typing (remember you need to be working out of the directory of the pythonGPS.py file):
p y t h o np y t h o n G P S . p y

https://www.sparkfun.com/tutorials/403

7/16

8/7/13

Python and GPS Tracking - SparkFun Electronics

This script will open a port and display the port number, then wait for you to enter a string followed by the enter key. If the loopback was successful, you should see what you sent and the program should end with a Python prompt >>>. To close the window after successfully running, hit Ctrl + c. Congratulations! You have just made yourself a very simple serial terminal program that can transmit and receive data!

Read a GPS and plot position with Python


Now that we know how to run a python script and open a serial port, there are many things you can do to create computer applications that communicate with embedded hardware. In this example, I am going to show you a program that reads GPS data over a serial port, saves the data to a txt file; then the data is read from the txt file, parsed, and plotted on a map. There are a few steps that need to be followed in order for this program to work.Install the modules in the order below.

Install modules
Use the same module installation process as above or find an executable package. pyserial (should already be installed from above) numpy (use: this link for Windows and this link for Mac/Linux) matplotlib pynmea The above process worked for me on my W7 machine, but I had to do some extra steps to get it to work on Ubuntu. Same might be said about Macs. With Ubuntu, you will need to completely clean your system of numpy, then build the source for numpy and matplotlib separately, so that you don't mess up all of the dependencies. Here is the process I used for Ubuntu.

https://www.sparkfun.com/tutorials/403

8/16

8/7/13

Python and GPS Tracking - SparkFun Electronics

Once you have all of these modules installed without errors, you can download my project from github and run the program with a pre-loaded map and GPS NMEA data to see how it works: github-gps_tracker Or you can proceed and create your own map and GPS NMEA data.

Select a map
Any map image will work, all you need to know are the bottom left and top right coordinates of the image. The map I used was a screen shot from Google Earth. I set placemarks at each corner and noted the latitude and longitude of each corner. Be sure to use decimal degrees coordinates.

Then I cropped the image around the two points using gimp. The more accurate you crop the image the more accurate your tracking will be. Save the image as 'map.png' and keep it to the side for now.

Hardware Setup
The hardware for this example includes a FTDI Basic and any NMEA capable GPS unit.

EM-406 GPS connected to a FTDI Basic For the connections, all you need to do is power the GPS with the FTDI basic (3.3V or 5V and GND), then connect the TX pin of the GPS to the RX pin on the FTDI Basic. It is probably best to allow the GPS to get a lock by leaving it powered for a few minutes before running the program. If the GPS doesn't have a lock when you run the program, the maps will not be generated and you will see the raw NMEA data streaming in the console window. If you don't have a GPS connected and you try to run the
https://www.sparkfun.com/tutorials/403 9/16

8/7/13

Python and GPS Tracking - SparkFun Electronics

program, you will get out-of-bound errors from the parsing. You can verify your GPS is working correctly by opening a serial terminal program.

Run the program


Here is the main GPS tracking program file: gpsmap.py Save the python script into a folder and drop your map.png file along side maps.py. Here is what your program directory should look like if you have a GPS connected:

The nmea.txt file will automatically be created if you have your GPS connected. If you don't have a GPS connected and you already have NMEA sentences to be displayed, create a file called 'nmea.txt' and drop the data into the file. Now open maps.py, we will need to edit some variables, so that your map image will scale correctly. Edit these variables specific to the top right and bottom left corners of your map. Don't forget to use decimal degree units!
# a d j u s tt h e s ev a l u e sb a s e do ny o u rl o c a t i o na n dm a p ,l a ta n dl o n ga r ei nd e c i m a ld e g r e e s T R X=1 0 5 . 1 6 2 1 # t o pr i g h tl o n g i t u d e T R Y=4 0 . 0 8 6 8 # t o pr i g h tl a t i t u d e B L X=1 0 5 . 2 8 9 8 # b o t t o ml e f tl o n g i t u d e B L Y=4 0 . 0 0 1 0 # b o t t o ml e f tl a t i t u d e

Run the program by typing:


p y t h o ng p s m a p . p y

The program starts by getting some information from the user.

https://www.sparkfun.com/tutorials/403

10/16

8/7/13

Python and GPS Tracking - SparkFun Electronics

You will select to either run the program with a GPS device connected or you can load your own GPS NMEA sentences into a file called nmea.txt. Since you have your GPS connected, you will select your COM port and be presented with two mapping options: a position map...

...or an altitude map.

https://www.sparkfun.com/tutorials/403

11/16

8/7/13

Python and GPS Tracking - SparkFun Electronics

Once you open the serial port to your GPS, the nmea.txt file will automatically be created and raw GPS NMEA data, specifically GPGGA sentences, will be logged in a private thread. When you make a map selection, the nmea.txt file is copied into a file called temp.txt, which is parsed for latitude and longitude (or altitude). The temp.txt file is created to parse the data so that we don't corrupt or happen to change the main nmea.txt log file. The maps are generated in their own windows with options to save, zoom, and hover your mouse over points to get fine grain x,y coordinates. Also, the maps don't refresh automatically, so as your GPS logs data, you will need to close the map window and run the map generation commands to get new data. If you close the entire Python program, the logging to nmea.txt halts. This program isn't finished by any means. I found myself constantly wanting to add features and fix bugs. I binged on Python for a weekend, simply because there are so many modules to work with: GUI tools, interfacing to the web, etc. It is seriously addicting. If you have any modifications or suggestions, please feel free to leave them in the comments below. Thanks for reading!

Comments 17 comments
Login or register to post comments. Thetinkerer | about 8 months ago 1 All this is with it plugged in right? How would you use this in a HAB? a1ronzo | about 8 months ago 1 All this is with it plugged in right? Actually, you dont need a GPS at all. You can put your own NMEA sentences in a txt file called
https://www.sparkfun.com/tutorials/403 12/16

8/7/13

Python and GPS Tracking - SparkFun Electronics

nmea.txt and the program will parse that file, then plot the positions. Make sure you are using NMEA GGA sentences since that is what the parser looks for. How would you use this in a HAB? HAB receives GPS data then transmits the GPS data over a long range radio to a ground station that receives the GPS data, parses it, and plots it in real time. Thetinkerer | about 8 months ago 1 What I meant was that this example has it plugged in via usb, how would you rig a HAB to read gps coordinates and altitude and transmit it to, say , a laptop at a distance? a1ronzo | about 8 months ago * 1 In the example, the GPS is directly connected to your computer over serial to USB. In the HAB, the GPS is connected to a transmitting radio, which is just another serial device that takes the serial data and sends it over the air. The receiving radio receives the data and sends it over, once again, serial to USB and back to your computer. Basically, the radios are serial devices and are creating a wireless serial link. Member #409816 | about 4 months ago 1 check out UKHAS fell | about 8 months ago 1 Good tutorial, thanks!!! TheRegnirps | about 8 months ago 1 One thing about Python is if you publish your code you usually get a blast of comments suggesting more Pythonic ways of doing things. A great thing about the Python community is that they are at least 90% helpful and fame-retardant. jago lee | about 8 months ago 1 I cant wait for your next HAB! Member #290410 | about 8 months ago 1 I am really interested to try this project, I am not familiar with these parts, 5V FTDI Basic EM-406 GPS module with Breakout Board (or any NMEA capable GPS) How do I connect these? It is not clear from the diagram. Do I need a soldering kit to connect them? How about cables? What sort
https://www.sparkfun.com/tutorials/403 13/16

8/7/13

Python and GPS Tracking - SparkFun Electronics

Thanks a1ronzo | about 8 months ago * 1 You will need these three parts: 5V FTDI Basic EM-406 GPS module EM-406 Breakout Board Along with these parts you might already have: soldering iron headers jumper wire Then connect it according to this schematic. Remember, this example is emulating a long range radio tracking system by just having the GPS connected to your computer (which might not really serve a purpose). Member #290410 | about 8 months ago 1 Thank you , getting clear. my idea is to just try as a Proof of concept and get into python also. Could you please post a photo of the actual setup or better yet a video tutorial will help very much. i am not still clear how to use header in this setup. the jumper have a male/female. i am very new to the hardware hacking and trying to get into this wonderful scene with so much sparkfun has to offer. a1ronzo | about 8 months ago 1 The schematic I linked to is pretty much a photo. I would suggest looking through the product page images (for the items I linked to above) to get an idea of what the hardware looks like, also check out our tutorials. We have a bunch of stuff on how to solder and use different types of hardware. After enough perusing you should get an idea how to make the connections, it isnt very difficult if you have the schematic picture and hardware in front of you. Member #290410 | about 8 months ago 1 Thanks! I already got all the software and modules installed and has been able to run your python files and use existing data files to generate altitude and position maps. Now onto some research, order parts and get this project going! TheRegnirps | about 8 months ago 1 When you install Python you can install eric and idle and get a simple IDE with Python with syntax highlighting and automatic indenting. They are much more convenient than gedit. Or if you use Eclipse for Arduino, do a setup for Python as well. alxxx | about 8 months ago 1
https://www.sparkfun.com/tutorials/403 14/16

8/7/13

Python and GPS Tracking - SparkFun Electronics

Great tutorial! Few python libraries for mapping basemap from matplotlib http://matplotlib.org/basemap/users/examples.html http://code.google.com/p/geopy/ which can be used with google maps , yahoo etc http://code.google.com/p/pymaps/ Also http://pymag.sourceforge.net/ which uses openstreetmap (commandline tool) for gps maps. Blacklab1 | about 7 months ago 1 So let me get this right. You are tethered to a PC right- one way or another? Reason I am asking is because a friend is trying to convince me that you can use an Arduino with this. You just cant program your SD card with a python map/script and have your Arduino know when to do a function 30 miles away from the launch site. ( 30 miles being a little far off for your xbee, USB cable, or whatever to commutate with your desktop.) Sharing Ingenuity GitHub YouTube Vimeo Google Plus Facebook Flickr Twitter RSS

SparkFun?
SparkFun is an online retail store that sells the bits and pieces to make your electronics projects possible. Whether it's a robot that can cook your breakfast or a GPS cat tracking device, our products and resources are designed to make the world of electronics more accessible to the average person. In addition to products, SparkFun also offers classes and a number of online tutorials designed to help educate individuals in the wonderful world of embedded electronics.

Resources
Feeds Forum SparkFun Education SparkFun IRC Channel Privacy Policy Jobs @ SparkFun Sell Your Widget on SparkFun
https://www.sparkfun.com/tutorials/403 15/16

8/7/13

Python and GPS Tracking - SparkFun Electronics

Become a SparkFun Distributor Request SparkFun Sponsorship Take the SparkFun Quiz Tell us about a project SparkFun Kickstarter Projects Chat With Us

Feedback
What's on your mind?

For which department?


General

Please include your email address if you'd like us to respond to a specific question.
email address Submit

SparkFun Electronics | Boulder, Colorado | Customer Service | Site Map

https://www.sparkfun.com/tutorials/403

16/16

You might also like