-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathadsbTest.py
More file actions
362 lines (259 loc) · 9.7 KB
/
adsbTest.py
File metadata and controls
362 lines (259 loc) · 9.7 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import numpy as np
from textwrap import wrap
import math
#Main method for program
def main():
print("\nADS-B Test program\n")
print("Press 1 to test everything")
print("Press 2 to test aircraft identification")
print("Press 3 to test aircraft location")
print("Press 4 to test aircraft crc")
#User input for mode
selection = input("Enter your choice: ")
if selection == "1":
print("\ndoing everything")
icao = input("Enter ICAO address (hex):\n")
addressMsg = aircraftID(icao)
alt = int(input("Enter altitude in feet: "))
lat = float(input("Enter lat in decimal format: "))
lon = float(input("Enter lon in decimal format: "))
evenLocMsg = position(lat, lon, alt, False)
oddLocMsg = position(lat, lon, alt, True)
evenMsg = addressMsg + evenLocMsg
oddMsg = addressMsg + oddLocMsg
#evenCRC = crc(format(int(evenMsg, 2), 'x'))
#oddCRC = crc(format(int(oddMsg, 2), 'x'))
#evenMsg = evenMsg + format(int(evenCRC, 16), 'b')
#oddMsg = oddMsg + format(int(oddCRC, 16), 'b')
evenCRC = crc(evenMsg)
oddCRC = crc(oddMsg)
evenMsg = evenMsg + evenCRC
oddMsg = oddMsg + oddCRC
print("\nEven: ")
print("Bin: " + evenMsg)
print("hex: " + bin2hex(evenMsg))
print("encoded: " + encodeMsg(evenMsg))
print("\nOdd: ")
print("Bin: " + oddMsg)
print("hex: " + bin2hex(oddMsg))
print("encoded: " + encodeMsg(oddMsg))
elif selection == "2":
print("\ndoing ID")
icao = input("Enter ICAO address (hex):\n")
adsbMsg = aircraftID(icao)
print ("ID: " + adsbMsg)
print ("hex: " + format(int(adsbMsg, 2), 'x'))
elif selection == "3":
print("\ndoing location")
alt = int(input("Enter altitude in feet: "))
lat = float(input("Enter lat in decimal format: "))
lon = float(input("Enter lon in decimal format: "))
print("Even: " + format(int(position(lat, lon, alt, False), 2), 'x'))
print(" Odd: " + format(int(position(lat, lon, alt, True), 2), 'x'))
else:
print("\n testing CRC")
adsbMsg = input("Enter message in hex withoout CRC: \n")
adsbMsg = adsbMsg + crc(adsbMsg)
print(adsbMsg)
print(format(int(adsbMsg, 2), 'x'))
#method to generate aircraft ID fields (DF, CA, ICAO)
def aircraftID(icao):
"""
generates DF, CA, and ICAO fields of ADS-B message
DF (downlink format) Field: 5 Bits
CA (Capability identifiers): 3 bits
ICAO address: 24 bits
"""
#the message that we will be returning
msg = ''
#DF 17 is ADS-B
df = format(17, 'b')
#CA is 5 for to match type code later
ca = format(5, 'b')
#The full message
msg = msg + df + ca + format(int(icao,16),'b')
while (len(msg) < 32):
msg = "0" + msg
return msg
def hex2bin(hexstr):
"""Convert a hexdecimal string to binary string, with zero fillings."""
num_of_bits = len(hexstr) * 4
binstr = bin(int(hexstr, 16))[2:].zfill(int(num_of_bits))
return binstr
def hex2int(hexstr):
"""Convert a hexdecimal string to integer."""
return int(hexstr, 16)
def int2hex(n):
"""Convert a integer to hexadecimal string."""
# strip 'L' for python 2
return hex(n)[2:].rjust(6, "0").upper().rstrip("L")
def bin2int(binstr):
"""Convert a binary string to integer."""
return int(binstr, 2)
def bin2hex(hexstr):
"""Convert a hexdecimal string to integer."""
return int2hex(bin2int(hexstr))
def bin2np(binstr):
"""Convert a binary string to numpy array."""
return np.array([int(i) for i in binstr])
def np2bin(npbin):
"""Convert a binary numpy array to string."""
return np.array2string(npbin, separator="")[1:-1]
def adsbMod(x, y):
"""ADS-B Mod method"""
return float(x - (y * math.floor(x/y)))
#CRC method
def crc(msg):
"""
Mode-S Cyclic Redundancy Check.
Detect if bit error occurs in the Mode-S message. When encode option is on,
the checksum is generated.
Args:
msg (string): 22 bytes hexadecimal message string
Returns:
int: message checksum, or partity bits (encoder)
"""
# the CRC generator
G = [int("11111111", 2), int("11111010", 2), int("00000100", 2), int("10000000", 2)]
#adding crc bytes to messgae
#msg = msg + "000000"
msg = msg + "000000000000000000000000"
#msgbin = hex2bin(msg)
#msgbin_split = wrap(msgbin, 8)
msgbin_split = wrap(msg, 8)
mbytes = list(map(bin2int, msgbin_split))
for ibyte in range(len(mbytes) - 3):
for ibit in range(8):
mask = 0x80 >> ibit
bits = mbytes[ibyte] & mask
if bits > 0:
mbytes[ibyte] = mbytes[ibyte] ^ (G[0] >> ibit)
mbytes[ibyte + 1] = mbytes[ibyte + 1] ^ (
0xFF & ((G[0] << 8 - ibit) | (G[1] >> ibit))
)
mbytes[ibyte + 2] = mbytes[ibyte + 2] ^ (
0xFF & ((G[1] << 8 - ibit) | (G[2] >> ibit))
)
mbytes[ibyte + 3] = mbytes[ibyte + 3] ^ (
0xFF & ((G[2] << 8 - ibit) | (G[3] >> ibit))
)
result = (mbytes[-3] << 16) | (mbytes[-2] << 8) | mbytes[-1]
result = str(format(result, 'b'))
while (len(result) < 24):
result = "0" + result
#print(result)
return result
#eturn format(int(result, 2), 'x')
#Position method (Lat, Lon, Alt)
def position(lat, lon, alt, odd):
"""
Mode S extended positon generator.
Takes in lat, lon, and alt as ints
Takes in odd as a boolean for even/odd frame
Type code hard coded to 0x58
returns a hex string
"""
#perform altitude and TC math
altitude = (alt + 1000)/25
#explicitly casting as int because python can be dumb
altitude = int(altitude)
hold = (altitude & 0x00F)
altitude = (altitude & 0xFF0) << 1
altitude = (altitude | hold); #concatenate to the entire message
altitude = (altitude | 0x010)
altitude = (altitude | 0x58000) #Takes on the TC (0x58) field
#formating altitude into bin string
altitude = str(format(altitude, 'b'))
#print("alt: " + altitude)
while (len(altitude) < 20):
altitude = "0" + altitude
#Calculate LatLon ------------------------
#utc bit hardcoded to zero
utcBit = "0"
#cpr bit for even odd pairing
if (odd):
cpr = 1
cprBit = "1"
else:
cpr = 0
cprBit = "0"
#NZ is hardcoded to 15 for our messages
nz = 15
#NB is hardcoded to 17 for 17 bits
nb = 17
#the latitude zone in the north south direction
dlat = float(360.0 / (4.0 * nz - cpr))
#print("dalt: " + str(dlat))
#latitude for mod operations
if lat < 0:
modLat = lat + 360.0
else:
modLat = float(lat)
#the y coordinate within the zone
#int YZi = (int) Math.round(Math.pow(2, Nb) * ((modLat % Dlat)/ Dlat));
#yZi = round(math.pow(2,nb)) * (adsbMod(modLat, dlat) / dlat)
yZi = float(adsbMod(lat, dlat))
yZi = yZi / dlat
yZi = yZi * math.pow(2, nb)
yZi = round(yZi)
#print("yZi: " + str(yZi))
#the latitude that the receiver will decode
#rLat = dlat * ((yZi / math.pow(2, nb))) + math.floor(lat / dlat)
rLat = float(yZi) / math.pow(2, nb)
rLat = rLat + math.floor(lat / dlat)
rLat = rLat * dlat
#print("RLat: " + str(rLat))
#NLat is based on latitude given
if (abs(lat) > 87):
nlLat = 1
elif (abs(lat) == 87):
nlLat = 2
else:
nlLat = math.floor((2*math.pi)/(math.acos(1 - ((1 - math.cos(math.pi / (2 * nz)))/(math.cos((math.pi / 180) * lat)**2)))))
#nlLat = math.floor( math.pow * 2 * (math.pow(math.acos(1 - ((1 - math.cos(math.pi / (2 * nz))) / (math.pow(math.cos((math.pi / 180) * abs(float(rLat))), 2)))), -1)) )
#Longitude calcs
if ((nlLat - cpr) - cpr) > 0:
dLon = 360.0 / (nlLat - cpr)
else:
dLon = 360.0
#the X coordinates within the zone
xZi = adsbMod(lon , dLon)
xZi = xZi / dLon
xZi = round(xZi * math.pow(2, nb))
#make sure the values are nb bits
yZi = adsbMod(yZi, pow(2,nb))
xZi = adsbMod(xZi, pow(2, nb))
#print("yZi: " + str(yZi))
#print(str(format(int(yZi), 'x')))
yBin = str(format(int(yZi), 'b'))
while (len(yBin) < 17):
yBin = "0" + yBin
#print("xZi: " + str(xZi))
#print(str(format(int(xZi), 'x')))
xBin = str(format(int(xZi), 'b'))
while (len(xBin) < 17):
xBin = "0" + xBin
locMsg = altitude + utcBit + cprBit + yBin + xBin
#print("bin: " + locMsg)
#print("hex: " + str(format(int(locMsg,2), 'x')))
return locMsg
#converts our string into the encoding style needed to transmit
def encodeMsg(msg):
"""
Mode S encoder that converts the given binary message into the Mode S encoding
Also adds preamble.
Returns a binary string of the preamble and encoded message - note needs to transmit at 2Mbps
"""
#the message preamble to turn on IFF reciever
preamble = "1010000101000000"
#end spce to give us room for when we transmit back to back
endSpace = "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
encodedMsg = ""
for i in range(0, len(msg)):
if (msg[i] == '1'):
encodedMsg = encodedMsg + "10"
else:
encodedMsg = encodedMsg + "01"
return preamble + encodedMsg + endSpace
if __name__ == '__main__':
main()