Advertisement
silver2row

Python2 changes...

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