Advertisement
silver2row

An older GPS module script...BBGW!

Oct 4th, 2020 (edited)
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. # probably made by Seeed Studio, BBB persons, and/or an avenger...
  2. # when I find where this came from, updates will be posted...
  3.  
  4. # I figured out where this source came from online: It is located here >>> https://github.com/beagleboard/cloud9-examples/blob/v2020.01/BeagleBone/Green/Grove/Software/Python/Grove_GPS.py
  5.  
  6. import Adafruit_BBIO.UART as UART
  7. import serial
  8. import time
  9. import sys
  10. import struct
  11. import math
  12.  
  13. UART.setup("UART2")
  14.  
  15. ser = serial.Serial(port = "/dev/ttyS2", baudrate=9600)
  16. ser.flush()
  17.  
  18. class GPS:
  19.     #The GPS module used is a Grove GPS module http://www.seeedstudio.com/depot/Grove-GPS-p-959.html
  20.     inp=[]
  21.     # Refer to SIM28 NMEA spec file http://www.seeedstudio.com/wiki/images/a/a0/SIM28_DATA_File.zip
  22.     GGA=[]
  23.  
  24.     #Read data from the GPS
  25.     def read(self):    
  26.         while True:
  27.             GPS.inp=ser.readline()
  28.             if GPS.inp[:6] == '$GPGGA': # GGA data , packet 1, has all the data we need
  29.                 break
  30.             time.sleep(0.1)
  31.         try:
  32.             ind=GPS.inp.index('$GPGGA', 5, len(GPS.inp))    #Sometimes multiple GPS data packets come into the stream. Take the data only after the last '$GPGGA' is seen
  33.             GPS.inp = GPS.inp[ind:]
  34.         except ValueError:
  35.             print(" ")
  36.         GPS.GGA = GPS.inp.split(", ")    #Split the stream into individual parts
  37.         return [GPS.GGA]
  38.        
  39.     #Split the data into individual elements
  40.     def vals(self):
  41.         time = GPS.GGA[1]
  42.         lat = GPS.GGA[2]
  43.         lat_ns = GPS.GGA[3]
  44.         long = GPS.GGA[4]
  45.         long_ew = GPS.GGA[5]
  46.         fix = GPS.GGA[6]
  47.         sats = GPS.GGA[7]
  48.         alt = GPS.GGA[9]
  49.         return [time, fix, sats, alt, lat, lat_ns, long, long_ew]
  50.  
  51. g = GPS()
  52. f = open("gps_data.csv", 'w')    #Open file to log the data
  53. f.write("name, latitude, longitude\n")    #Write the header to the top of the file
  54. ind=0
  55. while True:
  56.     try:
  57.         x = g.read()    #Read from GPS
  58.         [time, fix, sats, alt, lat, lat_ns, long, long_ew] = g.vals()    #Get the individial values
  59.         print("Time:", t, "Fix status:", fix, "Sats in view:", sats, "Altitude", alt, "Lat:", lat, lat_ns, "Long:", long, long_ew)
  60.         #s=str(t)+","+str(float(lat)/100)+","+str(float(long)/100)+"\n"    
  61.         #f.write(s)    #Save to file
  62.         time.sleep(2)
  63.     except IndexError:
  64.         print("Unable to read")
  65.     except KeyboardInterrupt:
  66.         f.close()
  67.         print("Exiting")
  68.         sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement