Skip to content

Instantly share code, notes, and snippets.

@ibanezmatt13
Last active December 18, 2015 00:09
Show Gist options
  • Save ibanezmatt13/5694404 to your computer and use it in GitHub Desktop.
Save ibanezmatt13/5694404 to your computer and use it in GitHub Desktop.
# import required libraries
import serial
import crcmod
import os
import os.path
import time
GPS = serial.Serial('/dev/ttyAMA0', 9600) # 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
# 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) # 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"
elif data[0] == "$GPGGA" and data[6] == "0": # if it does start with a valid sentence but with no fix
print "No Lock"
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
send(datastring) # send the new telemetry sentence to the send function
# function to convert lats and lngs to decimal latitude and longitude
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(lats) - 1):
decs2 = decs2 + position_data[i]
position = float(decs) + float(str((float(decs2)/60))[:8])
if orientation == "S" or "W":
position = 0 - position
return position
# function to search a directory and return the name of the largest file
def find_best_file():
best_file = ""
biggest_size = 0
imgdir = '/home/pi/to_transmit/'
for file in os.listdir(imgdir): # for all files in "to_transmit" folder
size = os.path.getsize(imgdir + file) # get the size of the file
if size > biggest_size: # if the size is bigger than the previous biggest size
biggest_size = size # reset biggest size
best_file = file # reset best file
if biggest_size == 0:
best_file == ""
return best_file
while True: # forever
best_file = find_best_file() # setting best_file variable by running previous function
if best_file == "": # if there are no images yet in the directory
read_gps() # only read the GPS, nothing else
else: # if there are images and thus a largest image
os.system('ssdv -e -c MATT-1 -i ' + str(counter) + ' /home/pi/to_transmit/' + best_file + ' packets') # create a packets file for the images
#os.system('rm /home/pi/to_transmit/*') # remove all images from the directory
packets_file = open("packets", "rb") # open the packets file
packets = packets_file.read(256) # read the first packet (256 bytes)
while packets != '': # while there are still packets to be sent
read_gps() # read the GPS (which also sends the GPS data - see "read_gps" function)
send(packets) # send latest packet to the send function
print packets # for testing only
packets = packets_file.read(256) # re-read the packets file for the next packet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment