Skip to content

Instantly share code, notes, and snippets.

@ibanezmatt13
Last active December 21, 2015 16:29
Show Gist options
  • Save ibanezmatt13/6334136 to your computer and use it in GitHub Desktop.
Save ibanezmatt13/6334136 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import os
import serial
import crcmod
import time
import time as time_
trigger = False
setNav = bytearray.fromhex("B5 62 06 24 24 00 FF FF 06 03 00 00 00 00 10 27 00 00 05 00 FA 00 FA 00 64 00 2C 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 16 DC")
crc16f = crcmod.predefined.mkCrcFun('crc-ccitt-false') # function for CRC-CCITT checksum
counter = 0 # this counter will increment as our sentence_id
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
print data
NTX2.close()
#create function equivalent to arduino millis();
def millis():
return int(round(time_.time() * 1000))
# function to convert latitude and longitude into a different format
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
def parse_nmea(NMEA_sentence):
data = NMEA_sentence.split(",") # split sentence into individual fields
if data[6] == "0": # if it does start with a valid sentence but with no fix
print "No Lock"
pass
else:
# parsing required telemetry fields
satellites = data[7]
lats = data[2]
northsouth = data[3]
lngs = data[4]
westeast = data[5]
altitude = int(float(data[10]))
callsign = "NORB_Test"
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)
if altitude >= 142:
trigger = True
string = str(callsign + ',' + time + ',' + str(counter) + ',' + str(latitude) + ',' + str(longitude) + ',' + str(trigger) + ',' + satellites + ',' + str(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
return datastring # send the datastring to the send function to send to the NTX2
GPS = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) # open serial port
# 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()
def check_for_NMEA():
GPS = serial.Serial('/dev/ttyAMA0', 9600, timeout=1)
start_time = millis()
while millis() - start_time <= 3000:
a = GPS.read(1)
if a == "$":
NMEA_sentence = GPS.readline()
if "GPGGA" in NMEA_sentence:
GPS.close()
send(parse_nmea(NMEA_sentence))
while True:
check_for_NMEA()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment