2012-06-16 10:34:03 +08:00
|
|
|
#!/usr/bin/env python
|
2010-10-19 00:59:08 +08:00
|
|
|
#
|
2012-06-16 10:34:03 +08:00
|
|
|
# Copyright 2010, 2012 Nick Foster
|
2010-10-19 00:59:08 +08:00
|
|
|
#
|
|
|
|
# This file is part of gr-air-modes
|
|
|
|
#
|
|
|
|
# gr-air-modes is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
# any later version.
|
|
|
|
#
|
|
|
|
# gr-air-modes is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with gr-air-modes; see the file COPYING. If not, write to
|
|
|
|
# the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
# Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
|
2010-09-15 13:01:56 +08:00
|
|
|
import math, time
|
2012-07-17 10:27:09 +08:00
|
|
|
from air_modes.exceptions import *
|
2012-06-13 22:49:22 +08:00
|
|
|
#this implements CPR position decoding and encoding.
|
|
|
|
#the decoder is implemented as a class, cpr_decoder, which keeps state for local decoding.
|
|
|
|
#the encoder is cpr_encode([lat, lon], type (even=0, odd=1), and surface (0 for surface, 1 for airborne))
|
2010-09-15 13:01:56 +08:00
|
|
|
|
2012-10-18 00:13:10 +08:00
|
|
|
#TODO: remove range/bearing calc from CPR decoder class. you can do this outside of the decoder.
|
|
|
|
|
2010-09-15 13:01:56 +08:00
|
|
|
latz = 15
|
|
|
|
|
|
|
|
def nz(ctype):
|
|
|
|
return 4 * latz - ctype
|
|
|
|
|
|
|
|
def dlat(ctype, surface):
|
|
|
|
if surface == 1:
|
|
|
|
tmp = 90.0
|
|
|
|
else:
|
|
|
|
tmp = 360.0
|
|
|
|
|
|
|
|
nzcalc = nz(ctype)
|
|
|
|
if nzcalc == 0:
|
|
|
|
return tmp
|
|
|
|
else:
|
|
|
|
return tmp / nzcalc
|
|
|
|
|
|
|
|
def nl(declat_in):
|
2012-06-16 13:27:20 +08:00
|
|
|
if abs(declat_in) >= 87.0:
|
|
|
|
return 1.0
|
2012-10-18 00:13:10 +08:00
|
|
|
return math.floor( (2.0*math.pi) * math.acos(1.0- (1.0-math.cos(math.pi/(2.0*latz))) / math.cos( (math.pi/180.0)*abs(declat_in) )**2 )**-1)
|
2010-09-15 13:01:56 +08:00
|
|
|
|
|
|
|
def dlon(declat_in, ctype, surface):
|
2012-10-18 00:13:10 +08:00
|
|
|
if surface:
|
2010-09-15 13:01:56 +08:00
|
|
|
tmp = 90.0
|
|
|
|
else:
|
|
|
|
tmp = 360.0
|
2012-10-18 00:13:10 +08:00
|
|
|
nlcalc = max(nl(declat_in)-ctype, 1)
|
|
|
|
return tmp / nlcalc
|
2010-09-15 13:01:56 +08:00
|
|
|
|
|
|
|
def decode_lat(enclat, ctype, my_lat, surface):
|
|
|
|
tmp1 = dlat(ctype, surface)
|
2012-10-15 13:34:50 +08:00
|
|
|
tmp2 = float(enclat) / (2**17)
|
2012-06-19 09:18:47 +08:00
|
|
|
j = math.floor(my_lat/tmp1) + math.floor(0.5 + ((my_lat % tmp1) / tmp1) - tmp2)
|
2010-09-15 13:01:56 +08:00
|
|
|
|
|
|
|
return tmp1 * (j + tmp2)
|
|
|
|
|
|
|
|
def decode_lon(declat, enclon, ctype, my_lon, surface):
|
|
|
|
tmp1 = dlon(declat, ctype, surface)
|
2012-10-15 13:34:50 +08:00
|
|
|
tmp2 = float(enclon) / (2**17)
|
2012-06-19 09:18:47 +08:00
|
|
|
m = math.floor(my_lon / tmp1) + math.floor(0.5 + ((my_lon % tmp1) / tmp1) - tmp2)
|
2010-09-15 13:01:56 +08:00
|
|
|
|
|
|
|
return tmp1 * (m + tmp2)
|
|
|
|
|
|
|
|
def cpr_resolve_local(my_location, encoded_location, ctype, surface):
|
|
|
|
[my_lat, my_lon] = my_location
|
|
|
|
[enclat, enclon] = encoded_location
|
|
|
|
|
|
|
|
decoded_lat = decode_lat(enclat, ctype, my_lat, surface)
|
|
|
|
decoded_lon = decode_lon(decoded_lat, enclon, ctype, my_lon, surface)
|
|
|
|
|
|
|
|
return [decoded_lat, decoded_lon]
|
|
|
|
|
2012-10-15 13:34:50 +08:00
|
|
|
def cpr_resolve_global(evenpos, oddpos, mypos, mostrecent, surface):
|
|
|
|
#cannot resolve surface positions unambiguously without knowing receiver position
|
|
|
|
if surface and mypos is None:
|
|
|
|
raise CPRNoPositionError
|
|
|
|
|
2012-06-15 09:14:53 +08:00
|
|
|
dlateven = dlat(0, surface)
|
|
|
|
dlatodd = dlat(1, surface)
|
2010-09-15 13:01:56 +08:00
|
|
|
|
|
|
|
evenpos = [float(evenpos[0]), float(evenpos[1])]
|
|
|
|
oddpos = [float(oddpos[0]), float(oddpos[1])]
|
2012-06-16 13:27:20 +08:00
|
|
|
|
2012-06-27 14:27:58 +08:00
|
|
|
j = math.floor(((nz(1)*evenpos[0] - nz(0)*oddpos[0])/2**17) + 0.5) #latitude index
|
2010-09-15 13:01:56 +08:00
|
|
|
|
2012-06-27 14:27:58 +08:00
|
|
|
rlateven = dlateven * ((j % nz(0))+evenpos[0]/2**17)
|
|
|
|
rlatodd = dlatodd * ((j % nz(1))+ oddpos[0]/2**17)
|
2010-09-15 13:01:56 +08:00
|
|
|
|
2012-06-19 09:18:47 +08:00
|
|
|
#limit to -90, 90
|
|
|
|
if rlateven > 270.0:
|
|
|
|
rlateven -= 360.0
|
|
|
|
if rlatodd > 270.0:
|
|
|
|
rlatodd -= 360.0
|
2010-09-15 13:01:56 +08:00
|
|
|
|
2012-06-16 09:50:37 +08:00
|
|
|
#This checks to see if the latitudes of the reports straddle a transition boundary
|
|
|
|
#If so, you can't get a globally-resolvable location.
|
2010-09-15 13:01:56 +08:00
|
|
|
if nl(rlateven) != nl(rlatodd):
|
2012-06-20 06:43:43 +08:00
|
|
|
raise CPRBoundaryStraddleError
|
2010-09-15 13:01:56 +08:00
|
|
|
|
|
|
|
if mostrecent == 0:
|
|
|
|
rlat = rlateven
|
|
|
|
else:
|
|
|
|
rlat = rlatodd
|
|
|
|
|
2012-10-15 13:34:50 +08:00
|
|
|
#disambiguate latitude
|
|
|
|
if surface:
|
|
|
|
if mypos[0] < 0:
|
|
|
|
rlat -= 90
|
|
|
|
|
2010-09-15 13:01:56 +08:00
|
|
|
dl = dlon(rlat, mostrecent, surface)
|
2012-10-18 00:13:10 +08:00
|
|
|
nl_rlat = nl(rlat)
|
2012-10-15 13:34:50 +08:00
|
|
|
|
2012-10-18 00:13:10 +08:00
|
|
|
m = math.floor(((evenpos[1]*(nl_rlat-1)-oddpos[1]*nl_rlat)/2**17)+0.5) #longitude index
|
|
|
|
|
|
|
|
#when surface positions straddle a disambiguation boundary (90 degrees),
|
|
|
|
#surface decoding will fail. this might never be a problem in real life, but it'll fail in the
|
|
|
|
#test case. the documentation doesn't mention it.
|
2010-09-15 13:01:56 +08:00
|
|
|
|
|
|
|
if mostrecent == 0:
|
|
|
|
enclon = evenpos[1]
|
|
|
|
else:
|
|
|
|
enclon = oddpos[1]
|
|
|
|
|
2012-10-18 00:13:10 +08:00
|
|
|
rlon = dl * ((m % max(nl_rlat-mostrecent,1)) + enclon/2.**17)
|
|
|
|
|
|
|
|
#print "DL: %f nl: %f m: %f rlon: %f" % (dl, nl_rlat, m, rlon)
|
|
|
|
#print "evenpos: %x, oddpos: %x, mostrecent: %i" % (evenpos[1], oddpos[1], mostrecent)
|
2012-10-15 13:34:50 +08:00
|
|
|
|
|
|
|
if surface:
|
|
|
|
#longitudes need to be resolved to the nearest 90 degree segment to the receiver.
|
|
|
|
wat = mypos[1]
|
|
|
|
if wat < 0:
|
|
|
|
wat += 360
|
|
|
|
zone = lambda lon: 90 * (int(lon) / 90)
|
|
|
|
rlon += (zone(wat) - zone(rlon))
|
2010-09-15 13:01:56 +08:00
|
|
|
|
2012-10-15 13:34:50 +08:00
|
|
|
#limit to (-180, 180)
|
2010-09-15 13:01:56 +08:00
|
|
|
if rlon > 180:
|
2012-10-15 13:34:50 +08:00
|
|
|
rlon -= 360.0
|
2010-09-15 13:01:56 +08:00
|
|
|
|
|
|
|
return [rlat, rlon]
|
|
|
|
|
|
|
|
|
2011-06-02 11:37:51 +08:00
|
|
|
#calculate range and bearing between two lat/lon points
|
|
|
|
#should probably throw this in the mlat py somewhere or make another lib
|
2010-09-15 13:01:56 +08:00
|
|
|
def range_bearing(loc_a, loc_b):
|
|
|
|
[a_lat, a_lon] = loc_a
|
|
|
|
[b_lat, b_lon] = loc_b
|
|
|
|
|
|
|
|
esquared = (1/298.257223563)*(2-(1/298.257223563))
|
|
|
|
earth_radius_mi = 3963.19059 * (math.pi / 180)
|
|
|
|
|
|
|
|
delta_lat = b_lat - a_lat
|
|
|
|
delta_lon = b_lon - a_lon
|
|
|
|
|
2012-07-17 05:35:43 +08:00
|
|
|
avg_lat = ((a_lat + b_lat) / 2.0) * math.pi / 180
|
2010-09-15 13:01:56 +08:00
|
|
|
|
2010-09-21 02:09:59 +08:00
|
|
|
R1 = earth_radius_mi*(1.0-esquared)/pow((1.0-esquared*pow(math.sin(avg_lat),2)),1.5)
|
2012-07-17 05:35:43 +08:00
|
|
|
|
2010-09-15 13:01:56 +08:00
|
|
|
R2 = earth_radius_mi/math.sqrt(1.0-esquared*pow(math.sin(avg_lat),2))
|
|
|
|
|
|
|
|
distance_North = R1*delta_lat
|
|
|
|
distance_East = R2*math.cos(avg_lat)*delta_lon
|
|
|
|
|
|
|
|
bearing = math.atan2(distance_East,distance_North) * (180.0 / math.pi)
|
|
|
|
if bearing < 0.0:
|
|
|
|
bearing += 360.0
|
|
|
|
|
|
|
|
rnge = math.hypot(distance_East,distance_North)
|
|
|
|
return [rnge, bearing]
|
2012-02-02 01:14:02 +08:00
|
|
|
|
|
|
|
class cpr_decoder:
|
|
|
|
def __init__(self, my_location):
|
|
|
|
self.my_location = my_location
|
2012-02-26 08:50:15 +08:00
|
|
|
self.evenlist = {}
|
|
|
|
self.oddlist = {}
|
2012-10-18 00:24:15 +08:00
|
|
|
self.evenlist_sfc = {}
|
|
|
|
self.oddlist_sfc = {}
|
2012-02-02 01:14:02 +08:00
|
|
|
|
2013-01-23 00:07:46 +08:00
|
|
|
def set_location(self, new_location):
|
2012-02-02 01:14:02 +08:00
|
|
|
self.my_location = new_location
|
|
|
|
|
|
|
|
def weed_poslists(self):
|
2012-10-15 08:56:01 +08:00
|
|
|
for poslist in [self.evenlist, self.oddlist]:
|
2012-02-02 01:14:02 +08:00
|
|
|
for key, item in poslist.items():
|
2012-10-15 08:56:01 +08:00
|
|
|
if time.time() - item[2] > 10:
|
2012-02-02 01:14:02 +08:00
|
|
|
del poslist[key]
|
2012-10-18 00:24:15 +08:00
|
|
|
for poslist in [self.evenlist_sfc, self.oddlist_sfc]:
|
|
|
|
for key, item in poslist.items():
|
|
|
|
if time.time() - item[2] > 25:
|
|
|
|
del poslist[key]
|
2012-02-02 01:14:02 +08:00
|
|
|
|
2012-02-26 08:50:15 +08:00
|
|
|
def decode(self, icao24, encoded_lat, encoded_lon, cpr_format, surface):
|
2012-10-18 00:24:15 +08:00
|
|
|
if surface:
|
|
|
|
oddlist = self.oddlist_sfc
|
|
|
|
evenlist = self.evenlist_sfc
|
|
|
|
else:
|
|
|
|
oddlist = self.oddlist
|
|
|
|
evenlist = self.evenlist
|
|
|
|
|
2012-02-02 01:14:02 +08:00
|
|
|
#add the info to the position reports list for global decoding
|
|
|
|
if cpr_format==1:
|
2012-10-18 00:24:15 +08:00
|
|
|
oddlist[icao24] = [encoded_lat, encoded_lon, time.time()]
|
2012-02-02 01:14:02 +08:00
|
|
|
else:
|
2012-10-18 00:24:15 +08:00
|
|
|
evenlist[icao24] = [encoded_lat, encoded_lon, time.time()]
|
2012-02-02 01:14:02 +08:00
|
|
|
|
|
|
|
[decoded_lat, decoded_lon] = [None, None]
|
|
|
|
|
2012-10-15 08:56:01 +08:00
|
|
|
#okay, let's traverse the lists and weed out those entries that are older than 10 seconds
|
2012-02-02 01:14:02 +08:00
|
|
|
self.weed_poslists()
|
|
|
|
|
2012-10-18 00:24:15 +08:00
|
|
|
if (icao24 in evenlist) \
|
|
|
|
and (icao24 in oddlist):
|
|
|
|
newer = (oddlist[icao24][2] - evenlist[icao24][2]) > 0 #figure out which report is newer
|
|
|
|
[decoded_lat, decoded_lon] = cpr_resolve_global(evenlist[icao24][0:2], oddlist[icao24][0:2], self.my_location, newer, surface) #do a global decode
|
2012-06-27 14:27:58 +08:00
|
|
|
else:
|
|
|
|
raise CPRNoPositionError
|
2012-02-02 01:14:02 +08:00
|
|
|
|
2012-06-20 06:43:43 +08:00
|
|
|
if self.my_location is not None:
|
2012-02-02 01:14:02 +08:00
|
|
|
[rnge, bearing] = range_bearing(self.my_location, [decoded_lat, decoded_lon])
|
|
|
|
else:
|
|
|
|
rnge = None
|
|
|
|
bearing = None
|
|
|
|
|
|
|
|
return [decoded_lat, decoded_lon, rnge, bearing]
|
|
|
|
|
2012-06-13 22:49:22 +08:00
|
|
|
#encode CPR position
|
|
|
|
def cpr_encode(lat, lon, ctype, surface):
|
|
|
|
if surface is True:
|
2012-10-18 00:13:10 +08:00
|
|
|
scalar = 2.**19
|
2012-06-13 22:49:22 +08:00
|
|
|
else:
|
2012-10-18 00:13:10 +08:00
|
|
|
scalar = 2.**17
|
2012-06-13 22:49:22 +08:00
|
|
|
|
2012-10-18 00:13:10 +08:00
|
|
|
#encode using 360 constant for segment size.
|
|
|
|
dlati = dlat(ctype, False)
|
2012-06-19 09:18:47 +08:00
|
|
|
yz = math.floor(scalar * ((lat % dlati)/dlati) + 0.5)
|
2012-06-13 22:49:22 +08:00
|
|
|
rlat = dlati * ((yz / scalar) + math.floor(lat / dlati))
|
2012-06-16 13:27:20 +08:00
|
|
|
|
2012-10-18 00:13:10 +08:00
|
|
|
#encode using 360 constant for segment size.
|
|
|
|
dloni = dlon(lat, ctype, False)
|
2012-06-19 09:18:47 +08:00
|
|
|
xz = math.floor(scalar * ((lon % dloni)/dloni) + 0.5)
|
2012-06-13 22:49:22 +08:00
|
|
|
|
2012-10-18 00:13:10 +08:00
|
|
|
yz = int(yz) & (2**17-1)
|
|
|
|
xz = int(xz) & (2**17-1)
|
2012-06-13 22:49:22 +08:00
|
|
|
|
|
|
|
return (yz, xz) #lat, lon
|
2012-06-16 09:50:37 +08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2012-06-16 10:34:03 +08:00
|
|
|
import sys, random
|
2012-10-18 00:13:10 +08:00
|
|
|
|
|
|
|
rounds = 10001
|
2012-06-16 10:34:03 +08:00
|
|
|
threshold = 1e-3 #0.001 deg lat/lon
|
|
|
|
#this accuracy is highly dependent on latitude, since at high
|
|
|
|
#latitudes the corresponding error in longitude is greater
|
2012-06-16 13:27:20 +08:00
|
|
|
|
|
|
|
bs = 0
|
2012-10-18 00:13:10 +08:00
|
|
|
surface = False
|
|
|
|
|
|
|
|
lats = [i/(rounds/170.)-85 for i in range(0,rounds)]
|
|
|
|
lons = [i/(rounds/360.)-180 for i in range(0,rounds)]
|
2012-06-16 10:34:03 +08:00
|
|
|
|
|
|
|
for i in range(0, rounds):
|
2012-10-18 00:13:10 +08:00
|
|
|
even_lat = lats[i]
|
|
|
|
#even_lat = random.uniform(-85, 85)
|
|
|
|
even_lon = lons[i]
|
|
|
|
#even_lon = random.uniform(-180, 180)
|
|
|
|
odd_lat = even_lat + 1e-3
|
|
|
|
odd_lon = min(even_lon + 1e-3, 180)
|
2012-10-15 13:34:50 +08:00
|
|
|
decoder = cpr_decoder([odd_lat, odd_lon])
|
2012-06-16 10:34:03 +08:00
|
|
|
|
|
|
|
#encode that position
|
2012-10-15 13:34:50 +08:00
|
|
|
(evenenclat, evenenclon) = cpr_encode(even_lat, even_lon, False, surface)
|
|
|
|
(oddenclat, oddenclon) = cpr_encode(odd_lat, odd_lon, True, surface)
|
2012-06-16 10:34:03 +08:00
|
|
|
|
2012-10-18 00:13:10 +08:00
|
|
|
#try to perform a global decode -- this should fail since the decoder
|
|
|
|
#only has heard one position. need two for global decoding.
|
2012-06-16 10:34:03 +08:00
|
|
|
icao = random.randint(0, 0xffffff)
|
2012-06-20 06:43:43 +08:00
|
|
|
try:
|
2012-10-15 13:34:50 +08:00
|
|
|
evenpos = decoder.decode(icao, evenenclat, evenenclon, False, surface)
|
2012-06-16 10:34:03 +08:00
|
|
|
raise Exception("CPR test failure: global decode with only one report")
|
2012-06-20 06:43:43 +08:00
|
|
|
except CPRNoPositionError:
|
|
|
|
pass
|
2012-06-16 10:34:03 +08:00
|
|
|
|
2012-10-18 00:13:10 +08:00
|
|
|
#now try to do a real decode with the last packet's odd complement
|
|
|
|
#watch for a boundary straddle -- this isn't fatal, it just indicates
|
|
|
|
#that the even and odd reports lie on either side of a longitudinal boundary
|
|
|
|
#and so you can't get a position
|
2012-06-20 06:43:43 +08:00
|
|
|
try:
|
2012-10-15 13:34:50 +08:00
|
|
|
(odddeclat, odddeclon, rng, brg) = decoder.decode(icao, oddenclat, oddenclon, True, surface)
|
2012-06-20 06:43:43 +08:00
|
|
|
except CPRBoundaryStraddleError:
|
2012-06-16 13:27:20 +08:00
|
|
|
bs += 1
|
2012-06-16 10:34:03 +08:00
|
|
|
continue
|
2012-06-20 06:43:43 +08:00
|
|
|
except CPRNoPositionError:
|
|
|
|
raise Exception("CPR test failure: no decode after even/odd inputs")
|
|
|
|
|
2012-06-21 09:57:01 +08:00
|
|
|
if abs(odddeclat - odd_lat) > threshold or abs(odddeclon - odd_lon) > threshold:
|
2012-10-15 13:34:50 +08:00
|
|
|
print "F odddeclat: %f odd_lat: %f" % (odddeclat, odd_lat)
|
|
|
|
print "F odddeclon: %f odd_lon: %f" % (odddeclon, odd_lon)
|
2012-06-16 13:27:20 +08:00
|
|
|
raise Exception("CPR test failure: global decode error greater than threshold")
|
2012-10-18 00:13:10 +08:00
|
|
|
# else:
|
|
|
|
# print "S odddeclat: %f odd_lat: %f" % (odddeclat, odd_lat)
|
|
|
|
# print "S odddeclon: %f odd_lon: %f" % (odddeclon, odd_lon)
|
2012-06-16 10:34:03 +08:00
|
|
|
|
2012-10-18 00:13:10 +08:00
|
|
|
nexteven_lat = odd_lat + 1e-3
|
|
|
|
nexteven_lon = min(odd_lon + 1e-3, 180)
|
2012-06-21 09:57:01 +08:00
|
|
|
|
2012-10-15 13:34:50 +08:00
|
|
|
(nexteven_enclat, nexteven_enclon) = cpr_encode(nexteven_lat, nexteven_lon, False, surface)
|
2012-06-21 09:57:01 +08:00
|
|
|
|
2012-10-18 00:13:10 +08:00
|
|
|
#try a locally-referenced decode
|
2012-06-20 06:43:43 +08:00
|
|
|
try:
|
2012-10-15 13:34:50 +08:00
|
|
|
(evendeclat, evendeclon) = cpr_resolve_local([even_lat, even_lon], [nexteven_enclat, nexteven_enclon], False, surface)
|
2012-06-20 06:43:43 +08:00
|
|
|
except CPRNoPositionError:
|
|
|
|
raise Exception("CPR test failure: local decode failure to resolve")
|
2012-10-18 00:13:10 +08:00
|
|
|
|
|
|
|
#check to see if the positions were valid
|
2012-06-21 09:57:01 +08:00
|
|
|
if abs(evendeclat - nexteven_lat) > threshold or abs(evendeclon - nexteven_lon) > threshold:
|
2012-10-15 13:34:50 +08:00
|
|
|
print "F evendeclat: %f nexteven_lat: %f evenlat: %f" % (evendeclat, nexteven_lat, even_lat)
|
|
|
|
print "F evendeclon: %f nexteven_lon: %f evenlon: %f" % (evendeclon, nexteven_lon, even_lon)
|
2012-06-16 13:27:20 +08:00
|
|
|
raise Exception("CPR test failure: local decode error greater than threshold")
|
2012-06-16 09:50:37 +08:00
|
|
|
|
2012-06-16 13:27:20 +08:00
|
|
|
print "CPR test successful. There were %i boundary straddles over %i rounds." % (bs, rounds)
|