I have modified the example code in the tutorial and have written the following function to get data from the GPSd daemon and return it in a useful format, including offsetting the time to local instead of GMT:
Code: Select all
#!/usr/bin/python
import gps
from string import find
from datetime import datetime, timedelta
# ===========================================================================
def get_gps(time_offset):
session = gps.gps("localhost", "2947") #start a new session with the GPS daemon
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)
report_received = 0
while report_received == 0:
report = session.next() # wait for the next status message from the GPS receiver
if report['class'] == 'TPV': # Wait for a 'TPV' report and display the current time
if hasattr(report, 'time'):
report_received = 1
s = report.time
s = s[:find(s, "T")] + " " + s[find(s, "T")+1:]
s = s[:find(s, ".")]
dt = datetime.strptime(s, '%Y-%m-%d %H:%M:%S')
dt = dt + timedelta(hours=time_offset) # apply an offset for the local time zone
gps_time = dt.strftime('%Y-%m-%d %H:%M:%S') #convert time string into a useful format
mode = report.mode # get status from the GPS receiver. either no lock, 2d or 3d
if mode == 1:
mode_string = "NO"
if mode == 2:
mode_string = "2D"
if mode == 3:
mode_string = "3D"
lat = report.lat
longatude = report.lon
speed = 2.236936*report.speed # convert meters per second to mph
alt = report.alt
return gps_time, lat, longatude, speed, alt, mode_string; #return data back from the receiver.
gps_data = get_gps(-4)
print gps_data
gps_data = get_gps(-4)
print gps_data
gps_data = get_gps(-4)
print gps_data
when I run this, I get the following output
Code: Select all
('2013-03-18 16:48:03', 33.BANNED, -84.BANNED, 0.219219728, 295.6, '3D')
('2013-03-18 16:48:04', 33.BANNED, -84.BANNED, 0.14987471200000002, 295.6, '3D')
('2013-03-18 16:48:05', 33.BANNED, -84.BANNED, 0.091714376, 295.6, '3D')
1. Currently the GPS module and daemon seem to default to a 1hz operation (which is why there is 1 second between each time sample). The way the function is written, I am waiting for an updated packet to be sent after I enter the function. Shouldn't the daemon be handling this function? Why cant I just ask the daemon what the last packet was instead of waiting for a whole new message from the GPS receiver?
2. I found some documentation online that shows that the GPSd daemon can accept input from the client connecting to it. Since the GPS receiver is capable to operating at 10Hz, cant I send a packet to the Daemon so it can send the configuration string to the GPS receiver to configure the receiver to operate at 10Hz instead of 1Hz?
3. I have noticed that sometimes the GPS receiver deosnt send an update packet for some reason. Unfortunately, the session.next() function call will wait for the next session, no matter what. There apparently is no timeout and no way to set one as far as I can tell. Does anyone know how to configure this so that it will drop out of this function in case the GPS receiver gets hung for some reason?
I need this function to be sped up because it is limiting the overall speed of my application.
I appreciate any insight anyone can give as to how this is actually working.
Thanks