Skip to content

Instantly share code, notes, and snippets.

@ibanezmatt13
Created June 11, 2013 16:57
Show Gist options
  • Save ibanezmatt13/5758589 to your computer and use it in GitHub Desktop.
Save ibanezmatt13/5758589 to your computer and use it in GitHub Desktop.
# import required libraries
import serial
import crcmod
import os
import os.path
import time
def disable_sentences():
GPS = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) # open serial to write to GPS
# Disabling all NMEA sentences except $GPGGA
GPS.write("$PUBX,40,GLL,0,0,0,0*5C\r\n")
GPS.write("$PUBX,40,GSA,0,0,0,0*4E\r\n")
GPS.write("$PUBX,40,RMC,0,0,0,0*47\r\n")
GPS.write("$PUBX,40,GSV,0,0,0,0*59\r\n")
GPS.write("$PUBX,40,VTG,0,0,0,0*5E\r\n")
GPS.close() # close serial ready for re-opening in loop
crc16f = crcmod.predefined.mkCrcFun('crc-ccitt-false') # function for CRC-CCITT checksum
counter = 0 # sentence ID start value
disable_sentences()
# function to send both telemetry and packets
def send(data):
NTX2 = serial.Serial('/dev/ttyAMA0', 300, serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_TWO) # opening serial at 300 baud for radio transmission with 8 character bits, no parity and two stop bits
NTX2.write(data) # write final datastring to the serial port
NTX2.close()
# function to read the gps and process the data it returns for transmission
def read_gps():
global counter # making the counter variable global to this function
gps = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) # open serial at 9600 baud
NMEA_sentence = gps.readline() # read the $GPGGA sentence from GPS
gps.close() # close the serial port
data = NMEA_sentence.split(",") # split sentence into individual fields
if data[0] != "$GPGGA": # if the sentence doesn't start with a valid sentence
print "Invalid sentence"
return "SENTENCE INVALID"
elif data[0] == "$GPGGA" and data[6] == "0": # if it does start with a valid sentence but with no fix
print "No Lock"
return "WAITING FOR LOCK :(\n"
elif data[0] == "$GPGGA" and data[6] != "0": # if it does start with a valid sentence and has a fix
lats = data[2]
northsouth = data[3]
lngs = data[4]
westeast = data[5]
altitude = data[9]
callsign = "MATT_test" # this will be the callsign used primarily for checksum
time = data[1]
time = float(time) # ensuring that python knows time is a float
string = "%06i" % time # creating a string out of time (this format ensures 0 is included at start if any)
hours = string[0:2]
minutes = string[2:4]
seconds = string[4:6]
time = str(str(hours) + ':' + str(minutes) + ':' + str(seconds)) # the final time string in form 'hh:mm:ss'
latitude = convert(lats, northsouth)
longitude = convert(lngs, westeast)
string = str(callsign + ',' + time + ',' + str(counter) + ',' + str(latitude) + ',' + str(longitude) + ',' + altitude) # the data string
csum = str(hex(crc16f(string))).upper()[2:] # running the CRC-CCITT checksum
csum = csum.zfill(4) # creating the checksum data
datastring = str("$$" + string + "*" + csum + "\n") # appending the datastring as per the UKHAS communication protocol
counter += 1 # increment the sentence ID for next transmission
print datastring # for testing only
return datastring
def convert(position_data, orientation):
decs = ""
decs2 = ""
for i in range(0, position_data.index('.') - 2):
decs = decs + position_data[i]
for i in range(position_data.index('.') - 2, len(position_data) - 1):
decs2 = decs2 + position_data[i]
position = float(decs) + float(str((float(decs2)/60))[:8])
if orientation == ("S") or orientation == ("W"):
position = 0 - position
return position
while True:
datastring = read_gps()
print datastring
send(datastring)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment