Accessing the USB GPS programmatically

Now that you can access the GPS device, let's work on accessing the data programmatically. Your project should now have the GPS device connected and have access to query the data via the serial port. In this section, you will create a program to use this data to discover where you are, then you can determine what to do with that information.

If you've completed the previous section, you should be able to receive the raw data from the GPS unit. Now you want to be able to do something with this data; for example, find your current location and altitude and then decide whether your target location is to the West, East, North, or South.

First, get the information from the raw data. As noted previously, the position and speed is in the $GPMRC output of the GPS device. Write a program to simply parse out a couple of pieces of information from that data. So, open a new file (you can name it location.py) and edit it, as shown in the following screenshot:

#!/usr/bin/python 
import serial 
ser = serial.Serial('/dev/ttyUSB0', 4800, timeout = 1) 
x = ser.read(1200) 
pos1 = x.find("$GPRMC") 
pos2 = x.find("
", pos1) 
loc = x[pos1:pos2] 
data = loc.split(',') 
if data(2) == 'V': 
   print 'No location found' 
else: 
   print "Latitude = " + data[3] + data[4] 
   print "Longitude = " + data[5] + data[6] 
   print "Speed = " + data[7] 
   print "Course = " + data[8] 

The code lines are explained as follows:

  • #!/usr/bin/python: As always, this line simply makes this file available for you to execute from the command line.
  • import serial: You again import the serial library. This will allow you to interface the USB GPS sensor with the GPS system.
  • if __name__=="__main__":: The main part of your program is then defined using this line.
  • ser = serial.Serial('/dev/ttyUSB0', 4800, timeout = 1): This command sets up the serial port to use the /dev/ttyUSB0 device, which is your GPS sensor using a baud rate of 4800 and a timeout value of 1.
  • x = ser.read(500): This command then reads a set of values from the USB port. In this case, you read 500 values, which includes a fairly complete set of your GPS data.
  • pos1 = x.find("$GPRMC"): This will find the first occurrence of $GPRMC and set the value pos1 to that position. In this case, you want to isolate the $GPRMC response line.
  • pos2 = x.find(" ", pos1): This will find the end of this string of text.
  • loc = x[pos1:pos2]: The loc variable will now hold the path that includes all of the information you are interested in.
  • data = loc.split(','): This will break your comma-separated line into an array of values.
  • if data[2] == 'V':: You now check to see whether or not the data is valid. If not, the next line simply prints out that you did not find a valid location.
  • else: If the data is valid, the next few lines print out the various pieces of data.

The following screenshot is an example showing the result that appeared when my device was able to find its location:

Accessing the USB GPS programmatically

Once you have the data, you can do some interesting things with it. For example, you may want to figure out the distance from and the direction to another waypoint. There is a piece of code at http://code.activestate.com/recipes/577594-GPS-distance-and-bearing-between-two-GPS-points/ that you can use to find the distances from and bearings to other waypoints, based on your current location. You can easily add this code to your location.py program to update your robot on the distances and bearings to other waypoints.

Now your quadcopter knows where it is and the direction it needs to go toward to get to other locations! There is another way to configure your GPS device that may make it a bit easier to access the data from other programs; it is using a set of functionality held in the gpsd library. To install this capability, type sudo apt-get install gpsd gpsd-clients, and this will install the gpsd software. For a tutorial on this software, go to http://wiki.ros.org/gpsd_client/Tutorials/Getting%20Started%20with%20gpsd_client. This software works by starting a background program (called a daemon) that communicates with your GPS device. You can then just query this program to get the GPS data. To start the process, type sudo gpsd /dev/ttyUSB0 -F /var/run/gpsd.sock. You can run the program by typing cgps.

The following screenshot shows a sample result:

Accessing the USB GPS programmatically

The preceding screenshot displays both the formatted and some of the raw data that is being received from the GPS sensor. If you get a timeout error when attempting to run this program, type sudo killall gpsd to kill all the running instances of the daemon and then type sudo gpsd /dev/ttyUSB0 -F /var/run/gpsd.sock again. You can also access this information from a program. To do this, edit a new file called gpstry1.py. The code will look like this:

#!/usr/bin/python 
import gps 
session = gps.gps("localhost", "2947") 
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE) 
while True: 
   report = session.next() 
   if report['class'] == 'TPV': 
      if hasattr(report, 'time): 
         print report.time 

The following are the details of your code:

  • #!/usr/bin/python: As always, this line simply makes this file available for you to execute from the command line.
  • import gps: In this case, you import the gps library. This will allow you to access the gpsd functionality.
  • session = gps.gps("localhost", "2947"): This opens a communication path between the gpsd functionality and your program. It also opens port 2947, which is assigned to the gpsd functionality, on the localhost.
  • session.stream(GPS.WATCH_ENABLE | GPS.WATCH_NEWSTYLE): This tells the system to look for new GPS data as it becomes available.
  • while True:: This simply loops and processes information until you ask the system to stop (it can be stopped by pressing Ctrl + C).
  • report = session.next(): When a report is ready, it is saved in the report variable.
  • if report['class'] == 'TPV':: This line checks to see whether the report will give you the type of data that you need.
  • if hasattr(report, 'time'):: This line makes sure that the report holds time data.
  • print report.time: This prints the time data. I use this in my example because the time data is always returned even if the GPS is not able to see enough satellites to return the position data. To see other possible attributes, visit http://www.catb.org/gpsd/gpsd_json.html for details.

Once you have created the program, you can run it by typing python gpstry1.py. The following screenshot shows how the output should look after running the program:

Accessing the USB GPS programmatically

Now you have access to your position; you can use it for a number of different capabilities. Perhaps the most useful is planning your flight path. This information and the path-planning procedures are discussed at http://code.activestate.com/recipes/577594-GPS-distance-and-bearing-between-two-GPS-points/ in more detail. As a brief summary, if you know your current position and your desired future location, you can plan a path between the two. This will include an angle and a direction.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset