2012-10-10 14:46:02 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright 2012 Nick Foster
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
|
|
|
|
# azimuthal projection widget to plot reception range vs. azimuth
|
|
|
|
|
|
|
|
from PyQt4 import QtCore, QtGui
|
|
|
|
import threading
|
|
|
|
import math
|
2012-10-10 23:20:07 +08:00
|
|
|
import air_modes
|
2013-06-19 02:45:58 +08:00
|
|
|
from air_modes.exceptions import *
|
2012-10-10 14:46:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
# model has max range vs. azimuth in n-degree increments
|
|
|
|
# contains separate max range for a variety of altitudes so
|
|
|
|
# you can determine your altitude dropouts by bearing
|
|
|
|
# assumes that if you can hear ac at 1000', you can hear at 5000'+.
|
|
|
|
class az_map_model(QtCore.QObject):
|
|
|
|
dataChanged = QtCore.pyqtSignal(name='dataChanged')
|
|
|
|
npoints = 360/5
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
super(az_map_model, self).__init__(parent)
|
|
|
|
self._data = []
|
|
|
|
self.lock = threading.Lock()
|
|
|
|
self._altitudes = [0, 1000, 2000, 5000, 10000, 15000, 20000, 25000, 30000]
|
|
|
|
#initialize everything to 0
|
|
|
|
for i in range(0,az_map_model.npoints):
|
|
|
|
self._data.append([0] * len(self._altitudes))
|
|
|
|
|
|
|
|
def rowCount(self):
|
|
|
|
return len(self._data)
|
|
|
|
|
|
|
|
def columnCount(self):
|
|
|
|
return len(self._altitudes)
|
|
|
|
|
|
|
|
def data(self, row, col):
|
|
|
|
return self._data[row][col]
|
|
|
|
|
|
|
|
def addRecord(self, bearing, altitude, distance):
|
|
|
|
with self.lock:
|
|
|
|
#round up to nearest altitude in altitudes list
|
|
|
|
#there's probably another way to do it
|
2012-10-11 03:51:44 +08:00
|
|
|
if altitude >= max(self._altitudes):
|
|
|
|
col = self.columnCount()-1
|
|
|
|
else:
|
|
|
|
col = self._altitudes.index(min([alt for alt in self._altitudes if alt >= altitude]))
|
|
|
|
|
2012-10-10 14:46:02 +08:00
|
|
|
#find which bearing row we sit in
|
2012-10-11 09:14:53 +08:00
|
|
|
row = int(int(bearing+(180./az_map_model.npoints)) / (360./az_map_model.npoints)) % az_map_model.npoints
|
2012-10-10 14:46:02 +08:00
|
|
|
#set max range for all alts >= the ac alt
|
|
|
|
#this expresses the assumption that higher ac can be heard further
|
2012-10-11 03:51:44 +08:00
|
|
|
update = False
|
2012-10-10 14:46:02 +08:00
|
|
|
for i in range(col, len(self._altitudes)):
|
|
|
|
if distance > self._data[row][i]:
|
|
|
|
self._data[row][i] = distance
|
2012-10-11 03:51:44 +08:00
|
|
|
update = True
|
2012-10-11 09:44:28 +08:00
|
|
|
if update:
|
|
|
|
self.dataChanged.emit()
|
2012-10-10 14:46:02 +08:00
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
with self.lock:
|
|
|
|
self._data = []
|
|
|
|
for i in range(0,az_map_model.npoints):
|
|
|
|
self._data.append([0] * len(self._altitudes))
|
2012-10-11 09:44:28 +08:00
|
|
|
self.dataChanged.emit()
|
2012-10-10 14:46:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
# the azimuth map widget
|
|
|
|
class az_map(QtGui.QWidget):
|
2012-10-10 23:20:47 +08:00
|
|
|
maxrange = 450
|
|
|
|
ringsize = 100
|
2012-10-10 14:46:02 +08:00
|
|
|
bgcolor = QtCore.Qt.black
|
|
|
|
ringpen = QtGui.QPen(QtGui.QColor(0, 96, 127, 255), 1.3)
|
2012-10-11 03:51:44 +08:00
|
|
|
#rangepen = QtGui.QPen(QtGui.QColor(255, 255, 0, 255), 1.0)
|
2012-10-10 14:46:02 +08:00
|
|
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
super(az_map, self).__init__(parent)
|
|
|
|
self._model = None
|
2012-10-11 03:51:44 +08:00
|
|
|
self._paths = []
|
|
|
|
self.maxrange = az_map.maxrange
|
|
|
|
self.ringsize = az_map.ringsize
|
2012-10-10 14:46:02 +08:00
|
|
|
|
|
|
|
def minimumSizeHint(self):
|
|
|
|
return QtCore.QSize(50, 50)
|
|
|
|
|
|
|
|
def sizeHint(self):
|
|
|
|
return QtCore.QSize(300, 300)
|
|
|
|
|
|
|
|
def setModel(self, model):
|
|
|
|
self._model = model
|
2012-10-11 09:44:28 +08:00
|
|
|
self._model.dataChanged.connect(self.repaint)
|
2012-10-10 14:46:02 +08:00
|
|
|
|
|
|
|
def paintEvent(self, event):
|
|
|
|
painter = QtGui.QPainter(self)
|
|
|
|
painter.setRenderHint(QtGui.QPainter.Antialiasing)
|
2012-10-11 03:51:44 +08:00
|
|
|
#TODO: make it not have to redraw paths EVERY repaint
|
2012-10-11 09:44:28 +08:00
|
|
|
#drawing paths is VERY SLOW
|
|
|
|
#maybe use a QTimer to limit repaints
|
2012-10-11 03:51:44 +08:00
|
|
|
self.drawPaths()
|
2012-10-10 14:46:02 +08:00
|
|
|
|
|
|
|
#set background
|
|
|
|
painter.fillRect(event.rect(), QtGui.QBrush(az_map.bgcolor))
|
|
|
|
|
|
|
|
#draw the range rings
|
|
|
|
self.drawRangeRings(painter)
|
2012-10-11 03:51:44 +08:00
|
|
|
for i in range(len(self._paths)):
|
|
|
|
alpha = 230 * (i+1) / (len(self._paths)) + 25
|
|
|
|
painter.setPen(QtGui.QPen(QtGui.QColor(alpha,alpha,0,255), 1.0))
|
|
|
|
painter.drawPath(self._paths[i])
|
|
|
|
|
|
|
|
def drawPaths(self):
|
|
|
|
self._paths = []
|
2012-10-10 14:46:02 +08:00
|
|
|
if(self._model):
|
2012-10-11 03:51:44 +08:00
|
|
|
for alt in range(0, self._model.columnCount()):
|
|
|
|
path = QtGui.QPainterPath()
|
|
|
|
for i in range(az_map_model.npoints-1,-1,-1):
|
|
|
|
#bearing is to start point of arc (clockwise)
|
|
|
|
bearing = (i+0.5) * 360./az_map_model.npoints
|
|
|
|
distance = self._model._data[i][alt]
|
|
|
|
radius = min(self.width(), self.height()) / 2.0
|
|
|
|
scale = radius * distance / self.maxrange
|
|
|
|
#convert bearing,distance to x,y
|
|
|
|
xpts = scale * math.sin(bearing * math.pi / 180)
|
|
|
|
ypts = scale * math.cos(bearing * math.pi / 180)
|
|
|
|
#get the bounding rectangle of the arc
|
|
|
|
|
|
|
|
arcrect = QtCore.QRectF(QtCore.QPointF(0-scale, 0-scale),
|
|
|
|
QtCore.QPointF(scale, scale))
|
|
|
|
|
|
|
|
if path.isEmpty():
|
|
|
|
path.moveTo(xpts, 0-ypts) #so we don't get a line from 0,0 to the first point
|
|
|
|
else:
|
|
|
|
path.lineTo(xpts, 0-ypts)
|
|
|
|
path.arcTo(arcrect, 90-bearing, 360./az_map_model.npoints)
|
|
|
|
|
|
|
|
self._paths.append(path)
|
2012-10-10 14:46:02 +08:00
|
|
|
|
|
|
|
def drawRangeRings(self, painter):
|
|
|
|
painter.translate(self.width()/2, self.height()/2)
|
|
|
|
painter.setPen(az_map.ringpen)
|
2012-10-11 03:51:44 +08:00
|
|
|
for i in range(0, self.maxrange, self.ringsize):
|
2012-10-10 14:46:02 +08:00
|
|
|
diameter = (float(i) / az_map.maxrange) * min(self.width(), self.height())
|
|
|
|
painter.drawEllipse(QtCore.QRectF(-diameter / 2.0,
|
|
|
|
-diameter / 2.0, diameter, diameter))
|
|
|
|
|
2012-10-11 03:51:44 +08:00
|
|
|
def setMaxRange(self, maxrange):
|
|
|
|
self.maxrange = maxrange
|
|
|
|
self.drawPath()
|
|
|
|
|
|
|
|
def setRingSize(self, ringsize):
|
|
|
|
self.ringsize = ringsize
|
|
|
|
self.drawPath()
|
|
|
|
|
2013-06-19 09:57:24 +08:00
|
|
|
class az_map_output:
|
|
|
|
def __init__(self, cprdec, model, pub):
|
|
|
|
self._cpr = cprdec
|
2012-10-10 23:20:07 +08:00
|
|
|
self.model = model
|
2013-06-19 09:57:24 +08:00
|
|
|
pub.subscribe("type17_dl", output)
|
2012-10-11 06:49:09 +08:00
|
|
|
|
2012-10-10 23:20:07 +08:00
|
|
|
def output(self, msg):
|
2013-06-19 02:45:58 +08:00
|
|
|
try:
|
2013-06-19 09:57:24 +08:00
|
|
|
msgtype = msg.data["df"]
|
2013-06-19 02:45:58 +08:00
|
|
|
now = time.time()
|
2012-10-10 23:20:07 +08:00
|
|
|
|
2013-06-19 02:45:58 +08:00
|
|
|
if msgtype == 17:
|
2013-06-19 09:57:24 +08:00
|
|
|
icao = msg.data["aa"]
|
|
|
|
subtype = msg.data["ftc"]
|
2013-06-19 02:45:58 +08:00
|
|
|
distance, altitude, bearing = [0,0,0]
|
|
|
|
if 5 <= subtype <= 8:
|
2013-06-19 09:57:24 +08:00
|
|
|
(ground_track, decoded_lat, decoded_lon, distance, bearing) = air_modes.parseBDS06(msg.data, self._cpr)
|
2013-06-19 02:45:58 +08:00
|
|
|
altitude = 0
|
|
|
|
elif 9 <= subtype <= 18:
|
2013-06-19 09:57:24 +08:00
|
|
|
(altitude, decoded_lat, decoded_lon, distance, bearing) = air_modes.parseBDS05(msg.data, self._cpr)
|
2013-06-19 02:45:58 +08:00
|
|
|
|
|
|
|
self.model.addRecord(bearing, altitude, distance)
|
|
|
|
except ADSBError:
|
|
|
|
pass
|
2012-10-10 23:20:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
##############################
|
|
|
|
# Test stuff
|
|
|
|
##############################
|
2012-10-10 15:22:13 +08:00
|
|
|
import random, time
|
2012-10-10 15:31:00 +08:00
|
|
|
|
|
|
|
class model_updater(threading.Thread):
|
|
|
|
def __init__(self, model):
|
|
|
|
super(model_updater, self).__init__()
|
|
|
|
self.model = model
|
|
|
|
self.setDaemon(1)
|
|
|
|
self.done = False
|
|
|
|
self.start()
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
for i in range(az_map_model.npoints):
|
2012-10-11 09:44:28 +08:00
|
|
|
time.sleep(0.005)
|
2012-10-10 23:20:07 +08:00
|
|
|
if(self.model):
|
2012-10-11 09:19:22 +08:00
|
|
|
for alt in self.model._altitudes:
|
|
|
|
self.model.addRecord(i*360./az_map_model.npoints, alt, random.randint(0,az_map.maxrange)*alt / max(self.model._altitudes))
|
|
|
|
self.done = True
|
2012-10-10 15:31:00 +08:00
|
|
|
|
2012-10-10 14:46:02 +08:00
|
|
|
class Window(QtGui.QWidget):
|
|
|
|
def __init__(self):
|
|
|
|
super(Window, self).__init__()
|
|
|
|
layout = QtGui.QGridLayout()
|
2012-10-10 15:22:13 +08:00
|
|
|
self.model = az_map_model()
|
2012-10-10 14:46:02 +08:00
|
|
|
mymap = az_map(None)
|
2012-10-10 15:22:13 +08:00
|
|
|
mymap.setModel(self.model)
|
2012-10-10 15:31:00 +08:00
|
|
|
self.updater = model_updater(self.model)
|
2012-10-10 14:46:02 +08:00
|
|
|
layout.addWidget(mymap, 0, 1)
|
|
|
|
self.setLayout(layout)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
app = QtGui.QApplication(sys.argv)
|
|
|
|
window = Window()
|
|
|
|
window.show()
|
2012-10-10 15:22:13 +08:00
|
|
|
window.update()
|
2012-10-10 14:46:02 +08:00
|
|
|
sys.exit(app.exec_())
|