RPi - Adafruit GPS HAT: UPDATE RATE NOT INCREASING

Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Locked
User avatar
k_m
 
Posts: 6
Joined: Wed Feb 21, 2018 7:42 am

RPi - Adafruit GPS HAT: UPDATE RATE NOT INCREASING

Post by k_m »

I am currently trying to write a python (2.7) script based around the GPSd library to provide GPS speed data at an update rate of 10Hz using the adafruit Ultimate GPS raspberry Pi Hat (Product ID - 2324 - https://www.adafruit.com/product/2324) with time stamps also reported at 0.1s.

The module itself is capable of up to 10Hz update rates however defaults to 1 Hz when set up and currently I cannot successfully increase it. I've have tried issuing PMTK commands (https://cdn-shop.adafruit.com/datasheets/PMTK_A11.pdf) to increase the update rate however cannot get this to work (PMTK220) and have included setting the baudrate to the maximum value of 115200.

I've searched all over for a method of getting the update rate output to increase however cannot see where the issue lies. The code below and prints out responses at a rate faster than 10 Hz however the values are only being updated every 1s.

Please Help!! My project deadline is in a few weeks and I am really struggling!!

Code: Select all

import os
import serial
from gps import *
import datetime
import time
import threading
import subprocess

#### CURRENTLY TRYING TO INCREASE GPS POLLING UPDATE RATE FROM DEFAULT 1Hz to 10Hz

subprocess.call(["stty","-F","/dev/serial0","raw","115200","cs8","clocal","-cstopb"])
subprocess.call(["sudo","systemctl","stop","gpsd.socket"])
subprocess.call(["sudo","systemctl","disable","gpsd.socket"])
subprocess.call(["sudo","gpsd","/dev/ttyS0","-F","/var/run/gpsd.sock"])

subprocess.call(["echo","-e","$PMTK251,115200*27\r\n","/dev/ttyS0"]) # command to set baudrate of serial port
subprocess.call(["echo","-e","$PMTK220,100*2F\r\n","/dev/ttyS0"]) #command to set GPS Update Rate

gpsd = None #seting the global variable

os.system('clear') #clear the terminal (optional)

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
os.system('clear')

x = 0
while x < 20: # infinite loop- use ctrl + c to end
    print gpsd.utc # print timestamp
    print gpsd.fix.speed # print gps speed
    print '-----------------'
    time.sleep(0.025) # Set update rate much higher than maximum possible of 10 Hz update rate
Image showing time being updated every second.
Image showing time being updated every second.
PIkDq.png (40.36 KiB) Viewed 622 times

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: RPi - Adafruit GPS HAT: UPDATE RATE NOT INCREASING

Post by adafruit_support_mike »

Check to see if the GPS module is emitting response messages when you send the commands to set the baudrate and update rate. Serial ports can be wierd about translating "\r\n" strings into what they think you should have sent.

User avatar
k_m
 
Posts: 6
Joined: Wed Feb 21, 2018 7:42 am

Re: RPi - Adafruit GPS HAT: UPDATE RATE NOT INCREASING

Post by k_m »

adafruit_support_mike wrote:Check to see if the GPS module is emitting response messages when you send the commands to set the baudrate and update rate. Serial ports can be wierd about translating "\r\n" strings into what they think you should have sent.

Thanks for the reply, I have tried this but it is still not working!

I am really struggling here and need to do well in this to pass my degree.

If someone could help that would be great!

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: RPi - Adafruit GPS HAT: UPDATE RATE NOT INCREASING

Post by adafruit_support_mike »

Are you getting the response packets or not?

User avatar
k_m
 
Posts: 6
Joined: Wed Feb 21, 2018 7:42 am

Re: RPi - Adafruit GPS HAT: UPDATE RATE NOT INCREASING

Post by k_m »

adafruit_support_mike wrote:Are you getting the response packets or not?
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 ( viewtopic.php?f=22&t=57838).

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


User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: RPi - Adafruit GPS HAT: UPDATE RATE NOT INCREASING

Post by adafruit_support_mike »

Make sure the baudrate is high enough to transmit all the characters in the NMEA sentences ten times per second.. 9600 baud would only allow 96 characters per update, for instance.

The exact speed you need depends on the set of NMEA sentences you want the module to emit, and how often they occur.

User avatar
k_m
 
Posts: 6
Joined: Wed Feb 21, 2018 7:42 am

Re: RPi - Adafruit GPS HAT: UPDATE RATE NOT INCREASING

Post by k_m »

Hi Mike. Even when setting the baud rate to the maximum of 115200 I cannot get the HAT to update at 10 Hz - only 5 Hz maximum (trying 10 Hz sees it default to 1 Hz).

EDIT: This also included setting the module to transmit GPRMC packets only as well

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: RPi - Adafruit GPS HAT: UPDATE RATE NOT INCREASING

Post by adafruit_support_mike »

I just double-checked the code above, and it's telling the GPS module to use a 200ms interval:

Code: Select all

ser.write(UPDATE_200_msec)  #Set GPS update rate
time.sleep(1)
ser.write(MEAS_200_msec)    #Set GPS measurement rate
time.sleep(1)
That will give you 5Hz.

User avatar
k_m
 
Posts: 6
Joined: Wed Feb 21, 2018 7:42 am

Re: RPi - Adafruit GPS HAT: UPDATE RATE NOT INCREASING

Post by k_m »

adafruit_support_mike wrote:I just double-checked the code above, and it's telling the GPS module to use a 200ms interval:

Code: Select all

ser.write(UPDATE_200_msec)  #Set GPS update rate
time.sleep(1)
ser.write(MEAS_200_msec)    #Set GPS measurement rate
time.sleep(1)
That will give you 5Hz.
Sorry I should have specified, the code above works and gives an update rate of 5Hz however if I change it to set the update rate to 10 Hz by using the commands UPDATE_100_msec and also using the gpsd command (subprocess.call(['gpsctl','-c','0.1']) the output update rate I get is only 1 Hz. I am wondering if this module is capable of actually delivering the 10 Hz specified even at the maximum baudrate as I cannot see a reason why this command does not work as it I can succesfully change it to 5 Hz to 0.1 Hz less using the same methodology.

I included the measurement command MEAS_XXX_msec (PMTK300) however I have a feeling that this is not doing anything as it is not actually specified in the PMTK command datasheet for this module and when I remove it completely it seems to have no effect, so I dont think the issue lies with this either.

User avatar
matthews_93
 
Posts: 1
Joined: Sun Mar 11, 2018 2:04 pm

Re: RPi - Adafruit GPS HAT: UPDATE RATE NOT INCREASING

Post by matthews_93 »

I'm also having this issue in that I cannot see a way to get the advertised 10 Hz for obtaining speed measurements - as 5 Hz seems to be the maximum achievable rate for speed. Mike would you be able to clarify if this is the case?

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: RPi - Adafruit GPS HAT: UPDATE RATE NOT INCREASING

Post by adafruit_support_mike »

The one I have works at 10Hz if I bump up the baudrate up to 38400 and trim the NMEA sentences down so they don't overload the connection.

I'll have someone pull one of the latest boards from inventory and see if they can duplicate that.

User avatar
adafruit2
 
Posts: 22148
Joined: Fri Mar 11, 2005 7:36 pm

Re: RPi - Adafruit GPS HAT: UPDATE RATE NOT INCREASING

Post by adafruit2 »

hey folks, there's really no reason it wouldnt work and we havent updated the firmware much - can you turn on ONLY $GPRMC and 10Hz and then print out the NMEA sentences you get?

User avatar
k_m
 
Posts: 6
Joined: Wed Feb 21, 2018 7:42 am

Re: RPi - Adafruit GPS HAT: UPDATE RATE NOT INCREASING

Post by k_m »

matthews_93 wrote:I'm also having this issue in that I cannot see a way to get the advertised 10 Hz for obtaining speed measurements - as 5 Hz seems to be the maximum achievable rate for speed. Mike would you be able to clarify if this is the case?
Same!... what are you using it for?

Locked
Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/

Return to “Adafruit Raspberry Pi® accessories”