Advertisement
silver2row

toptechboy.com gps script...

Oct 5th, 2020
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.24 KB | None | 0 0
  1. # by McWhorter on toptechboy.com...
  2.  
  3. import serial
  4. import Adafruit_BBIO.UART as UART
  5. from time import sleep
  6.  
  7. UART.setup("UART2")
  8. ser=serial.Serial("/dev/ttyS2", 9600)
  9.  
  10. class GPS:
  11.  
  12.     def __init__(self):
  13.         #This sets up variables for useful commands.
  14.         #This set is used to set the rate the GPS reports
  15.         UPDATE_10_sec = b"$PMTK220,10000*2F\n" #Update Every 10 Seconds
  16.         UPDATE_5_sec = b"$PMTK220,5000*1B\n"   #Update Every 5 Seconds
  17.         UPDATE_1_sec = b"$PMTK220,1000*1F\n"   #Update Every One Second
  18.         UPDATE_200_msec = b"$PMTK220,200*2C\n" #Update Every 200 Milliseconds
  19.         #This set is used to set the rate the GPS takes measurements
  20.         MEAS_10_sec = b"$PMTK300,10000,0,0,0,0*2C\n" #Measure every 10 seconds
  21.         MEAS_5_sec = b"$PMTK300,5000,0,0,0,0*18\n"   #Measure every 5 seconds
  22.         MEAS_1_sec = b"$PMTK300,1000,0,0,0,0*1C\n"   #Measure once a second
  23.         MEAS_200_msec = b"$PMTK300,200,0,0,0,0*2F\n"  #Meaure 5 times a seconds
  24.         #Set the Baud Rate of GPS
  25.         BAUD_57600 = b"$PMTK251,57600*2C\n"       #Set Baud Rate at 57600
  26.         BAUD_9600 = b"$PMTK251,9600*17\n"         #Set 9600 Baud Rate
  27.         #Commands for which NMEA Sentences are sent
  28.         ser.write(BAUD_57600.encode('utf-8')
  29.         sleep(1)
  30.         ser.baudrate=57600
  31.         GPRMC_ONLY = b"$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29\n" #Send only the GPRMC Sentence
  32.         GPRMC_GPGGA = b"$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\n"#Send GPRMC AND GPGGA Sentences
  33.         SEND_ALL = b"$PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0*28\n" #Send All Sentences
  34.         SEND_NOTHING = b"$PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\n" #Send Nothing
  35.         ser.write(UPDATE_1_sec()
  36.         sleep(1)
  37.         ser.write(MEAS_1_sec()
  38.         sleep(1)
  39.         ser.write(GPRMC_GPGGA()
  40.         sleep(1)
  41.         ser.flushInput()
  42.         ser.flushInput()
  43.  
  44.     def read(self):
  45.         ser.flushInput()
  46.         ser.flushInput()
  47.         while ser.inWaiting() == 0:
  48.             pass
  49.         self.NMEA1=ser.readline()
  50.         while ser.inWaiting() == 0:
  51.             pass
  52.         self.NMEA2=ser.readline()
  53.         NMEA1_array = self.NMEA1.split(',')
  54.         NMEA2_array = self.NMEA2.split(',')
  55.         if NMEA1_array[0] == b'$GPRMC':
  56.             self.timeUTC = NMEA1_array[1][:-8] + ':' + NMEA1_array[1][-8:-6] + ':' + NMEA1_array[1][-6:-4]
  57.             self.latDeg = NMEA1_array[3][:-7]
  58.             self.latMin = NMEA1_array[3][-7:]
  59.             self.latHem = NMEA1_array[4]
  60.             self.lonDeg = NMEA1_array[5][:-7]
  61.             self.lonMin = NMEA1_array[5][-7:]
  62.             self.lonHem = NMEA1_array[6]
  63.             self.knots = NMEA1_array[7]
  64.         if NMEA1_array[0] == b'$GPGGA':
  65.             self.fix = NMEA1_array[6]
  66.             self.altitude = NMEA1_array[9]
  67.             self.sats = NMEA1_array[7]
  68.         if NMEA2_array[0] == b'$GPRMC':
  69.             self.timeUTC = NMEA2_array[1][:-8] + ':' + NMEA1_array[1][-8:-6] + ':' + NMEA1_array[1][-6:-4]
  70.             self.latDeg = NMEA2_array[3][:-7]
  71.             self.latMin = NMEA2_array[3][-7:]
  72.             self.latHem = NMEA2_array[4]
  73.             self.lonDeg = NMEA2_array[5][:-7]
  74.             self.lonMin = NMEA2_array[5][-7:]
  75.             self.lonHem = NMEA2_array[6]
  76.             self.knots = NMEA2_array[7]
  77.  
  78.         if NMEA2_array[0] == b'$GPGGA':
  79.             self.fix = NMEA2_array[6]
  80.             self.altitude = NMEA2_array[9]
  81.             self.sats = NMEA2_array[7]
  82.  
  83. myGPS = GPS()
  84. GPSdata = open('/home/debian/GPS/GPSTwo.csv', 'w')
  85. GPSdata.close()
  86. while(1):
  87.     myGPS.read()
  88.     if myGPS.fix != 0:
  89.         try:
  90.             latDec = float(myGPS.latDeg) + float(myGPS.latMin)/60.
  91.             lonDec = float(myGPS.lonDeg) + float(myGPS.lonMin)/60.
  92.             if myGPS.lonHem == 'W':
  93.                 lonDec=(-1)*lonDec
  94.             if myGPS.latHem=='S':
  95.                 latDec=(-1)*latDec
  96.             alt = myGPS.altitude
  97.             GPSdata = open('/home/debian/GPS/GPS.txt', 'a')
  98.             myString = str(lonDec) + ',' + str(latDec) + ',' + alt +' '
  99.             GPSdata.write(myString)
  100.             GPSdata.close()
  101.         except:
  102.             pass
  103.  
  104.  
  105. Error:
  106.  
  107.   File "GPSone.py", line 27
  108.     sleep(1)
  109.         ^
  110. SyntaxError: invalid syntax
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement