Thanks for the reply Mike! I have managed to sort this issue out now and realised it was related to the '\r\n' commands!
I am now getting update rate of 5Hz (not the 10Hz stated on the spec); could you advise on how i would update this to 10Hz. Seending the PMTK command for 10 Hz and using the gpsctl command for 10 Hz sees the rate default to 1 Hz. I have read in some places that 10Hz positional fix is not supported by the GPS, that is fine as I am mainly looking for 10Hz GPS Speed and time values (
).
Could someone please advise on how I would increase the Speed update rate to 10Hz?
Code: Select all
import os
import serial
from gps import *
import datetime
import time
import numpy as np
import threading
import subprocess
## Links:
## https://forums.adafruit.com/viewtopic.php?f=31&t=83827
## https://forums.adafruit.com/viewtopic.php?f=22&t=57838 # Limit of update rate of Adafruit HAT
## https://forums.adafruit.com/viewtopic.php?f=31&t=110508 - Rishi Look At Again
## Initial Initialisation of Adafruit GPS Board:
subprocess.call(["sudo","killall","gpsd"]) # kills any gpsd processes previously running
subprocess.call(["stty","-F","/dev/ttyS0","raw","9600","cs8","clocal","-cstopb"])
subprocess.call(["sudo","systemctl","stop","gpsd.socket"])
subprocess.call(["sudo","systemctl","disable","gpsd.socket"])
ser=serial.Serial('/dev/ttyS0',9600) #Initialize Serial Port
## PMTK Commands:
# Positional Update Rate
UPDATE_10_sec= "$PMTK220,10000*2F/r/n" #Update Every 10 Seconds (0.1Hz)
UPDATE_5_sec= "$PMTK220,5000*1B/r/n" #Update Every 5 Seconds (0.2Hz)
UPDATE_1_sec= "$PMTK220,1000*1F/r/n" #Update Every One Second (1Hz)
UPDATE_200_msec= "$PMTK220,200*2C/r/n" #Update Every 200 Milliseconds (5Hz)
UPDATE_100_msec= "$PMTK220,100*2F/r/n" #Update Every 100 Milliseconds (10Hz)
#This set is used to set the rate the GPS takes measurements - Sets positional accuracy:
MEAS_10_sec = "$PMTK300,10000,0,0,0,0*2C/r/n" #Measure once every 10 seconds
MEAS_5_sec = "$PMTK300,5000,0,0,0,0*18/r/n" #Measure once every 5 seconds
MEAS_1_sec = "$PMTK300,1000,0,0,0,0*1C/r/n" #Measure once every 1 second
MEAS_200_msec= "$PMTK300,200,0,0,0,0*2F/r/n" #Meaure once every 0.2 seconds
MEAS_100_msec= "$PMTK300,100,0,0,0,0*2C/r/n" #Meaure once every 0.1 seconds #THIS DOES NOT APPEAR ON DATASHEET
#Set the Baud Rate of Adafruit GPS Hat:
BAUD_115200 = "$PMTK251,115200*1F/r/n" #Set Baud Rate at 115200 (Maximum setting for Adafruit GPS)
BAUD_57600 = "$PMTK251,57600*2C/r/n" #Set Baud Rate at 57600
BAUD_9600 ="$PMTK251,9600*17/r/n" #Set Baud Rate at 9600 (Default setting for Adafruit GPS)
#Commands for which NMEA Sentences are sent:
GPRMC_ONLY= "$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29\r\n" #Send only the GPRMC Sentence #Set to output only RMC - Sending only RMC speeds up GPS as less data packets being aquired
GPRMC_GPGGA="$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n"#Send GPRMC AND GPGGA Sentences
SEND_ALL ="$PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n" #Send All NMEA Sentences
SEND_NOTHING="$PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n" #Send Nothing
#Send Commands to Serial Port of Raspberry PI:
print "PMTK Commands Initialisation Started"
ser.write(BAUD_115200) #Set GPS Baudrate
time.sleep(1)
ser.baudrate=115200 #Change Serial Baudrate to match GPS Baudrate
ser.write(UPDATE_200_msec) #Set GPS update rate
time.sleep(1)
ser.write(MEAS_200_msec) #Set GPS measurement rate
time.sleep(1)
ser.write(GPRMC_ONLY) #Choose what NMEA Sentance to output
time.sleep(1)
print "PMTK Commands Initialisation Complete"
#Set-up gpsd client
subprocess.call(["sudo","gpsd","/dev/ttyS0","-F","/var/run/gpsd.sock"])
time.sleep(1)
print "Started gpsd"
subprocess.call(['gpsctl','-s','115200']) #gpsd command to change GPS baudrate to 115200
time.sleep(1)
print "Set gpsd baudrate"
subprocess.call(['gpsctl','-c','0.2']) #gpsd command to change GPS update rate to 5 Hz
time.sleep(1)
print "Set gpsd update rate"
#os.system('clear') #clear the terminal (optional)
#GPS Polling Intialisation
gpsd = None #setting the global variable
class GpsPoller(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
global gpsd #bring it in scope
gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
self.current_value = None
self.running = True #setting the thread running to true
def run(self):
global gpsd
while gpsp.running:
gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer
gpsp = GpsPoller() # create the thread
gpsp.start() # start it up
print "Startedgpsp poller"
os.system('clear')
out = [] # creates empty list
temp = np.zeros([1, 6]) # Creates empty array
logtime = 15
print "Wait 5 Seconds"
time.sleep(5) # allows time for GPS to boot up
print "Wait finished"
#Log Time Reference values
start = time.time() #denotes general time (using RPi time clock)
startgps = time.mktime(time.strptime(gpsd.utc, '%Y-%m-%dT%H:%M:%S.%fZ')) #added in .%fZ so not assuming .000 seconds
print "Logging started at", startgps
t0 = 0 # System Clock Time
t1 = 0 # GPS Time
#Logging GPS data
while t0 < logtime:
t0 = time.time() - start
t1 = int(time.mktime(time.strptime(gpsd.utc, '%Y-%m-%dT%H:%M:%S.%fZ'))) - startgps
temp[0,0] = t0 #System time
temp[0,1] = t1 # GPS Time
temp[0,2] = gpsd.fix.speed # GPS Speed Data
out.append(temp.copy())
print "RPi Time = " , t0
print "GPS UTC Time =" , t1
print "gpsd.fix.speed = " , gpsd.fix.speed
print "gpsd.fix.longitude = " , gpsd.fix.longitude
time.sleep(0.05)
print "Logging finished at", startgps