python and gps tracking - sparkfun electronics

16
8/7/13 Python and GPS Tracking - SparkFun Electronics https://www.sparkfun.com/tutorials/403 1/16 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 password Forgot your password? No account? Register one! Categories New Products Top Sellers Staff Picks Gift Certificates Classes & Events 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

Upload: gabions

Post on 30-Nov-2015

172 views

Category:

Documents


4 download

DESCRIPTION

Python and GPS Tracking

TRANSCRIPT

Page 1: Python and GPS Tracking - SparkFun Electronics

8/7/13 Python and GPS Tracking - SparkFun Electronics

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

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 password

Forgot your password? No account? Register one!

Categories

New ProductsTop SellersStaff PicksGift CertificatesClasses & Events

►Arduino

BooksBreakout Boards►Cables►Cellular►Components

►Development ToolsDings and Dents

►E-TextilesEducators

►GPSKits

►LCDs

►Programmers►Prototyping

►Retail►Retired

►RoboticsSale

►SensorsSwag

Page 2: Python and GPS Tracking - SparkFun Electronics

8/7/13 Python and GPS Tracking - SparkFun Electronics

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

►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 mycomputer and interact with embedded hardware over a serial port using the Python programming language. My goalwas 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 toUSB connection. However, in this tutorial, instead of using radios, we will use a GPS tethered to your computerover 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 myposition 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 usedas a general purpose NMEA parser, that will plot positions on a map of your choice. Just enter your NMEA datainto a text file and the program will do the rest.

Page 3: Python and GPS Tracking - SparkFun Electronics

8/7/13 Python and GPS Tracking - SparkFun Electronics

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

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 simplePython 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 areal-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 BasicEM-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, aswell as the FTDI.

What is Python?

If you are already familiar with installing and running Python, feel free to skip ahead. Python is an interpretedprogramming 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 intosomething called a Python interpreter. Once you get the Python interpreter installed, you can write scripts using anytext 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 withthe 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.

Page 4: Python and GPS Tracking - SparkFun Electronics

8/7/13 Python and GPS Tracking - SparkFun Electronics

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

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'. Youshould 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 enginealong 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 itcorrectly, 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, youcould 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.

# simple scriptdef test(): print "test"while 1: test()

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.

Page 5: Python and GPS Tracking - SparkFun Electronics

8/7/13 Python and GPS Tracking - SparkFun Electronics

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

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 linein the directory of your script and type:

python your_script_name.py

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 isa 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):

python setup.py install

You should see a bunch of action in the command window and hopefully no errors. All this process is doing ismoving 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 therelevant 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 foundhere:

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.

Page 6: Python and GPS Tracking - SparkFun Electronics

8/7/13 Python and GPS Tracking - SparkFun Electronics

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

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 availableports by typing this:

python -m serial.tools.list_ports

If you are using linux:

dmesg | grep tty

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!!):

import serial

#####Global Variables#######################################be sure to declare the variable as 'global var' in the fxnser = 0

#####FUNCTIONS##############################################initialize serial connection def init_serial(): COMNUM = 9 #set you COM port # here global ser #must be declared in each fxn used ser = serial.Serial() ser.baudrate = 9600 ser.port = COMNUM - 1 #starts at 0, so subtract 1 #ser.port = '/dev/ttyUSB0' #uncomment for linux

#you must specify a timeout (in seconds) so that the # serial port doesn't hang ser.timeout = 1 ser.open() #open the serial port

# print port open or closed if ser.isOpen(): print 'Open: ' + ser.portstr#####SETUP#################################################this is a good spot to run your initializations

Page 7: Python and GPS Tracking - SparkFun Electronics

8/7/13 Python and GPS Tracking - SparkFun Electronics

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

init_serial()

#####MAIN LOOP############################################while 1: #prints what is sent in on the serial port temp = raw_input('Type what you want to send, hit enter:\n\r') ser.write(temp) #write to the serial port bytes = ser.readline() #reads in bytes followed by a newline print 'You sent: ' + bytes #print to the console break #jump out of loop #hit ctr-c to close python window

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 arerunning linux, read the comments above for ser.port.

Now, if you want to send data over the serial port, use:

ser.write(your_data)

your_data can be one byte or multiple bytes.

If you want to receive data over the serial port, use:

your_data = ser.readline()

The readline() function will read in a series of bytes terminated with a new line character (i.e. typing something thenhitting enter on your keyboard). This works great with GPS, because each GPS NMEA sentence is terminated witha 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 port3. 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 variablesas 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):

python pythonGPS.py

Page 8: Python and GPS Tracking - SparkFun Electronics

8/7/13 Python and GPS Tracking - SparkFun Electronics

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

This script will open a port and display the port number, then wait for you to enter a string followed by the enterkey. If the loopback was successful, you should see what you sent and the program should end with a Pythonprompt >>>.

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 createcomputer 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 orderbelow.

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) matplotlibpynmea

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 isthe process I used for Ubuntu.

Page 9: Python and GPS Tracking - SparkFun Electronics

8/7/13 Python and GPS Tracking - SparkFun Electronics

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

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 mapI used was a screen shot from Google Earth. I set placemarks at each corner and noted the latitude and longitude ofeach 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 moreaccurate 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), thenconnect 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 theprogram. If the GPS doesn't have a lock when you run the program, the maps will not be generated and you will seethe raw NMEA data streaming in the console window. If you don't have a GPS connected and you try to run the

Page 10: Python and GPS Tracking - SparkFun Electronics

8/7/13 Python and GPS Tracking - SparkFun Electronics

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

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 programdirectory 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 thefile.

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 degreeunits!

#adjust these values based on your location and map, lat and long are in decimal degreesTRX = -105.1621 #top right longitudeTRY = 40.0868 #top right latitudeBLX = -105.2898 #bottom left longitudeBLY = 40.0010 #bottom left latitude

Run the program by typing:

python gpsmap.py

The program starts by getting some information from the user.

Page 11: Python and GPS Tracking - SparkFun Electronics

8/7/13 Python and GPS Tracking - SparkFun Electronics

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

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 bepresented with two mapping options: a position map...

...or an altitude map.

Page 12: Python and GPS Tracking - SparkFun Electronics

8/7/13 Python and GPS Tracking - SparkFun Electronics

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

Once you open the serial port to your GPS, the nmea.txt file will automatically be created and raw GPS NMEAdata, specifically GPGGA sentences, will be logged in a private thread. When you make a map selection, thenmea.txt file is copied into a file called temp.txt, which is parsed for latitude and longitude (or altitude). The temp.txtfile 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 getfine 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 andrun 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 onPython 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 don’t need a GPS at all. You can put your own NMEA sentences in a txt file called

Page 13: Python and GPS Tracking - SparkFun Electronics

8/7/13 Python and GPS Tracking - SparkFun Electronics

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

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 thatreceives 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 readgps 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. Inthe HAB, the GPS is connected to a transmitting radio, which is just another serial devicethat 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, theradios 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 can’t 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 GPSmodule with Breakout Board (or any NMEA capable GPS) How do I connect these? It is not clear from thediagram. Do I need a soldering kit to connect them? How about cables? What sort

Page 14: Python and GPS Tracking - SparkFun Electronics

8/7/13 Python and GPS Tracking - SparkFun Electronics

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

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 verymuch. i am not still clear how to use header in this setup. the jumper have a male/female. i amvery new to the hardware hacking and trying to get into this wonderful scene with so muchsparkfun 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 hardwarelooks like, also check out our tutorials. We have a bunch of stuff on how to solder anduse different types of hardware. After enough perusing you should get an idea how tomake the connections, it isn’t very difficult if you have the schematic picture andhardware 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 torun 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 highlightingand automatic indenting. They are much more convenient than gedit. Or if you use Eclipse for Arduino, do asetup for Python as well.

alxxx | about 8 months ago 1

Page 15: Python and GPS Tracking - SparkFun Electronics

8/7/13 Python and GPS Tracking - SparkFun Electronics

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

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 can’t program your SD cardwith a ‘python’ map/script and have your Arduino know when to do a function 30 miles away from thelaunch site. ( 30 miles being a little far off for your xbee, USB cable, or whatever to commutate with yourdesktop.)

Sharing Ingenuity

GitHubYouTubeVimeo

Google PlusFacebookFlickrTwitterRSS

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 tomake the world of electronics more accessible to the average person. In addition to products, SparkFun also offersclasses and a number of online tutorials designed to help educate individuals in the wonderful world of embeddedelectronics.

Resources

FeedsForumSparkFun EducationSparkFun IRC Channel

Privacy PolicyJobs @ SparkFunSell Your Widget on SparkFun

Page 16: Python and GPS Tracking - SparkFun Electronics

8/7/13 Python and GPS Tracking - SparkFun Electronics

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

Become a SparkFun DistributorRequest SparkFun Sponsorship

Take the SparkFun QuizTell us about a projectSparkFun Kickstarter ProjectsChat 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