-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathADSBread.py
156 lines (126 loc) · 4.28 KB
/
ADSBread.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import subprocess
import serial
import time
import math
from datetime import datetime, timedelta
#Fletchers House
latH = 33.250
longH = -84.401
altH = 820
gpsH = [latH,longH,altH]
fletchHex = 0 #indecx of plane to track
#arduino = serial.Serial(port='/dev/ttyUSB0',baudrate=19200,timeout=3.0)
gpsP = []
numF = 0 #number of flights
flts = [] # [[hex,timeRx,timeTx,fltnum,alt,speed,heading,lat,long],[]]
hexVal = []
hexInd = 0
tri = 1; tti = 2; fi =3; ai = 4; si = 5; hi = 6; lati = 7; longi = 8; #indexes
#Delay for Motor Driving!
period = timedelta(milliseconds = 50)
next_time = datetime.now() + period
#Program:
command = ['./dump1090', '--net', '--quiet'] #writing to port 30003
subP = subprocess.Popen(command)
time.sleep(3)
reader = ['nc','127.0.0.1','30003'] # |egrep','--line','-buffered','MSG,1,|MSG,3,|MSG,4,']
p = subprocess.Popen(reader, stdout=subprocess.PIPE)
time.sleep(3)
#loop through and filter the output of dump1090 ported to 30003 and imported as subprocess p
while 1:
line = p.stdout.readline().decode('utf-8')
split = line.split(',') #make a list of each value (comma sperated)
try: #search if hex value has been found before
hexInd = hexVal.index(split[4])
flts[hexInd][0] = hexVal[hexInd] #set the hex value based on hex index
flts[hexInd][tri] = split[7]
flts[hexInd][tti] = split[9]
if split[1] == '1': #check if we get fltNum
flts[hexInd][fi] = split[10]
elif split[1] == '3': #check if we get atl,lat,long
flts[hexInd][ai] = split[11]
flts[hexInd][lati] = split[14]
flts[hexInd][longi] = split[15]
elif split[1] == '4': #check if we get speed, heading
flts[hexInd][si] = split[12]
flts[hexInd][hi] = split[13]
except ValueError:
hexInd = len(hexVal)
hexVal.append(split[4])
#add everything b/c of append calls suck
flts.append([split[4],split[7],split[9],split[10],split[11],
split[12],split[13],split[14],split[15]])
# print(flts)
if (flts[hexInd][0] == 'A7E276'):
fletchHex = hexInd
# print(flts[fletchHex])
#hex = A667F2
#GPS Calc
try:
latP = float(flts[fletchHex][lati])
longP = float(flts[fletchHex][longi])
altP = float(flts[fletchHex][ai])
gpsP = [latP,longP,altP]
dlat = (gpsP[0] - gpsH[0])
dlon = (gpsP[1] - gpsH[1])
dist = math.sqrt(((dlat*363815.46)*(dlat*363815.46)) + ((dlon*309172.01)*(dlon*309172.01)))
dalt = (gpsP[2] - gpsH[2])
pitch = round(math.degrees(math.atan(dalt/dist)))
#assuming flat earth
if (dlon>0): #if east
yaw = 180 - math.degrees(math.atan(dlat/dlon))
elif ((dlon<0) & (dlat > 0)): #if west & north
yaw = math.degrees(math.atan(dlat/dlon))
elif ((dlon<0) & (dlat < 0)):
yaw = 360 - math.degrees(math.atan(dlat/dlon))
elif ((dlon==0) & (dlat > 0)):
yaw = 90
elif ((dlon==0) & (dlat < 0)):
yaw = 270
yaw = round(yaw)
print('latP,longH,altH: ',latH,longH,altH)
print('latP,longP,altP: ',latP,longP,altP)
if next_time <= datetime.now():
cmd = "{},{}n".format(yaw, pitch)
arduino.write(cmd.encode('utf-8'))
next_time += period
print(pitch)
print(yaw)
except KeyboardInterrupt:
try:
subP.terminate()
p.terminate() #check this line!!
except OSError as e:
print("here")
print(str(e))
subP.wait()
except Exception as e:
print(str(e))
#pitch
# try:
# if int(alt) < low:
# low = int(alt)
# lowInd = hexInd
# except ValueError:
# print('not an int?')
#
# ##Some Bullshit for GPS
# if low < 300000:
# try:
# gpsPlane = [int(flts[lowInd][lati]),int(flts[lowInd][longi]),int(flts[lowInd][ai])]
# if gpsPlane[1] > 0:
# d = math.sqrt(((gpsHome[0]-gpsPlane[0])*120.628409*(gpsHome[0]-gpsPlane[0])**120.628409)
# + ((gpsHome[1]-gpsPlane[1])*58.5553*(gpsHome[1]-gpsPlane[1])*58.5553)) # bad
# d = d*5280
# alpha = 20
# math.degrees(math.atan(1.18))
# cmd = "190,40" #"{},{}\n".format(cmd_elv, cmd_az)
# arduino.write(cmd.encode('utf-8'))
# except ValueError:
# print('not an int?')
#filt = ['egrep','--line','-buffered','MSG,1,|MSG,3,|MSG,4,']
#p3 = subprocess.Popen(filt, stdout=subprocess.PIPE)
#egrep --line-buffered 'MSG,3,|MSG,4,'
##close?
##lsof -i :30002
##kill PID number