Skip to content

Instantly share code, notes, and snippets.

@avtehnik
Last active November 21, 2015 18:12
Show Gist options
  • Save avtehnik/eff52ad89521dc21eefc to your computer and use it in GitHub Desktop.
Save avtehnik/eff52ad89521dc21eefc to your computer and use it in GitHub Desktop.
u-blox neo-6
#!/usr/local/bin/python
import serial
import sys
from math import pow, degrees, radians, acos
from scipy import cos, sin, arctan, sqrt, arctan2
#####Global Variables######################################
#be sure to declare the variable as 'global var' in the fxn
ser = 0
def ecef2geodetic(x, y, z):
"""Convert ECEF coordinates to geodetic.
J. Zhu, "Conversion of Earth-centered Earth-fixed coordinates \
to geodetic coordinates," IEEE Transactions on Aerospace and \
Electronic Systems, vol. 30, pp. 957-961, 1994."""
a = 6378.137
b = 6356.7523142
esq = 6.69437999014 * 0.001
e1sq = 6.73949674228 * 0.001
# return h in kilo
r = sqrt(x * x + y * y)
Esq = a * a - b * b
F = 54 * b * b * z * z
G = r * r + (1 - esq) * z * z - esq * Esq
C = (esq * esq * F * r * r) / (pow(G, 3))
S = sqrt(1 + C + sqrt(C * C + 2 * C))
P = F / (3 * pow((S + 1 / S + 1), 2) * G * G)
Q = sqrt(1 + 2 * esq * esq * P)
r_0 = -(P * esq * r) / (1 + Q) + sqrt(0.5 * a * a*(1 + 1.0 / Q) - \
P * (1 - esq) * z * z / (Q * (1 + Q)) - 0.5 * P * r * r)
U = sqrt(pow((r - esq * r_0), 2) + z * z)
V = sqrt(pow((r - esq * r_0), 2) + (1 - esq) * z * z)
Z_0 = b * b * z / (a * V)
h = U * (1 - b * b / (a * V))
lat = arctan((z + e1sq * Z_0) / r)
lon = arctan2(y, x)
return degrees(lat), degrees(lon), h
#####FUNCTIONS#############################################
#initialize serial connection
def init_serial():
COMNUM = 9 #set you COM port # here
global ser #must be declared in each fxn used
ser = serial.Serial()
ser.baudrate = 115200
#ser.port = COMNUM - 1 #starts at 0, so subtract 1
ser.port = '/dev/tty.usbserial' #uncomment for linux
#you must specify a timeout (in seconds) so that the
# serial port doesn't hang
ser.timeout = 1
ser.open() #open the serial port
# print port open or closed
if ser.isOpen():
print 'Open: ' + ser.portstr
#####SETUP################################################
#this is a good spot to run your initializations
init_serial()
#####MAIN LOOP############################################
while 1:
str = ser.readline()
if ord(str[0]) == 181 and ord(str[1]) == 98 :
print "Class {:02x}".format(ord(str[2]))
print "Message {:02x}".format(ord(str[3]))
print "Length " + `(ord(str[4])+ord(str[5]))`
if ord(str[2]) == 1 and ord(str[3]) == 6 :
offset = 5;
print "GPSfix Type {:02x}".format(ord(str[10+offset]))
print "Fix Status Flags {:02x}".format(ord(str[11+offset]))
print "Lat " + `(ord(str[12+offset])+ord(str[13+offset])+ord(str[14+offset])+ord(str[15+offset]))`
print "Lng " + `(ord(str[16+offset])+ord(str[17+offset])+ord(str[18+offset])+ord(str[19+offset]))`
print "Lng " + `(ord(str[20+offset])+ord(str[21+offset])+ord(str[22+offset])+ord(str[23+offset]))`
print ecef2geodetic((ord(str[12+offset])+ord(str[13+offset])+ord(str[14+offset])+ord(str[15+offset])), (ord(str[16+offset])+ord(str[17+offset])+ord(str[18+offset])+ord(str[19+offset])), (ord(str[20+offset])+ord(str[21+offset])+ord(str[22+offset])+ord(str[23+offset])))
print ":".join("{:02x}".format(ord(c)) for c in str)
# print str
sys.stdout.flush()
#!/usr/local/bin/python
import serial
#####Global Variables######################################
#be sure to declare the variable as 'global var' in the fxn
ser = 0
#####FUNCTIONS#############################################
#initialize serial connection
def init_serial():
COMNUM = 9 #set you COM port # here
global ser #must be declared in each fxn used
ser = serial.Serial()
ser.baudrate = 9600
# ser.baudrate = 115200
#ser.port = COMNUM - 1 #starts at 0, so subtract 1
ser.port = '/dev/tty.usbserial' #uncomment for linux
#you must specify a timeout (in seconds) so that the
# serial port doesn't hang
ser.timeout = 1
ser.open() #open the serial port
# print port open or closed
if ser.isOpen():
print 'Open: ' + ser.portstr
#####SETUP################################################
#this is a good spot to run your initializations
init_serial()
#####MAIN LOOP############################################
ser.write("$PGCMD,16,0,0,0,0,0*6A\r\n") #write to the serial port
ser.write("$PSRF100,0,38400,8,1,0*3C\r\n") #write to the serial port
ser.write("\265\142\006\001\003\000\001\006\001\022\117$PUBX,41,1,0003,0001,115200,0*1E\r\n") #write to the serial port
while 1:
print ser.readline()
import sys
from struct import *
str = '\x00\x80\xe4\x42\x21\x67\x86\x1f\x13\x9a\xcd\x76\x1d\x24\x9d\x02\x00\xc1\x3d\x02\x00\xda\xc6\x00\x00\x52\x55\x00'
data = unpack('=LllllLL', str)
print data
#result
#(1122271232, 528901921, 1993185811, 43852829, 37601536, 13031936, 5591552)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment