Debian Codegraph @

mlat/client/synthetic_es.py

raw symbols SCIP
1 # -*- python -*-
2
3 # Part of mlat-client - an ADS-B multilateration client.
4 # Copyright 2015, Oliver Jowett <[email protected]>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 import math
20 import _modes
21 import bisect
22
23 # It would be nice to merge this into a proper all-singing
24 # all-dancing Mode S module that combined _modes, this code,
25 # and the server side Mode S decoder. A task for another day..
26
27 __all__ = ('make_altitude_only_frame', 'make_position_frame_pair', 'make_velocity_frame', 'DF17', 'DF18')
28
29 # types of frame we can build
30 DF17 = 'DF17'
31 DF18 = 'DF18'
32 DF18ANON = 'DF18ANON'
33 DF18TRACK = 'DF18TRACK'
34
35 # lookup table for CPR_NL
36 nl_table = (
37 (10.47047130, 59),
38 (14.82817437, 58),
39 (18.18626357, 57),
40 (21.02939493, 56),
41 (23.54504487, 55),
42 (25.82924707, 54),
43 (27.93898710, 53),
44 (29.91135686, 52),
45 (31.77209708, 51),
46 (33.53993436, 50),
47 (35.22899598, 49),
48 (36.85025108, 48),
49 (38.41241892, 47),
50 (39.92256684, 46),
51 (41.38651832, 45),
52 (42.80914012, 44),
53 (44.19454951, 43),
54 (45.54626723, 42),
55 (46.86733252, 41),
56 (48.16039128, 40),
57 (49.42776439, 39),
58 (50.67150166, 38),
59 (51.89342469, 37),
60 (53.09516153, 36),
61 (54.27817472, 35),
62 (55.44378444, 34),
63 (56.59318756, 33),
64 (57.72747354, 32),
65 (58.84763776, 31),
66 (59.95459277, 30),
67 (61.04917774, 29),
68 (62.13216659, 28),
69 (63.20427479, 27),
70 (64.26616523, 26),
71 (65.31845310, 25),
72 (66.36171008, 24),
73 (67.39646774, 23),
74 (68.42322022, 22),
75 (69.44242631, 21),
76 (70.45451075, 20),
77 (71.45986473, 19),
78 (72.45884545, 18),
79 (73.45177442, 17),
80 (74.43893416, 16),
81 (75.42056257, 15),
82 (76.39684391, 14),
83 (77.36789461, 13),
84 (78.33374083, 12),
85 (79.29428225, 11),
86 (80.24923213, 10),
87
88 (81.19801349, 9),
89 (82.13956981, 8),
90 (83.07199445, 7),
91 (83.99173563, 6),
92 (84.89166191, 5),
93 (85.75541621, 4),
94 (86.53536998, 3),
95 (87.00000000, 2),
96 (90.00000000, 1)
97 )
98
99 nl_lats = [x[0] for x in nl_table]
100 nl_vals = [x[1] for x in nl_table]
101
102
103 def CPR_NL(lat):
104 """The NL function referenced in the CPR calculations: the number of longitude zones at a given latitude"""
105 if lat < 0:
106 lat = -lat
107
108 nl = nl_vals[bisect.bisect_left(nl_lats, lat)]
109 return nl
110
111
112 def CPR_N(lat, odd):
113 """The N function referenced in the CPR calculations: the number of longitude zones at a given latitude / oddness"""
114 nl = CPR_NL(lat) - (odd and 1 or 0)
115 if nl < 1:
116 nl = 1
117 return nl
118
119
120 def cpr_encode(lat, lon, odd):
121 """Encode an airborne position using a CPR encoding with the given odd flag value"""
122
123 NbPow = 2**17
124 Dlat = 360.0 / (odd and 59 or 60)
125 YZ = int(math.floor(NbPow * (lat % Dlat) / Dlat + 0.5))
126
127 Rlat = Dlat * (1.0 * YZ / NbPow + math.floor(lat / Dlat))
128 Dlon = (360.0 / CPR_N(Rlat, odd))
129 XZ = int(math.floor(NbPow * (lon % Dlon) / Dlon + 0.5))
130
131 return (YZ & 0x1FFFF), (XZ & 0x1FFFF)
132
133
134 def encode_altitude(ft):
135 """Encode an altitude in feet using the representation expected in DF17 messages"""
136 if ft is None:
137 return 0
138
139 i = int((ft + 1012.5) / 25)
140 if i < 0:
141 i = 0
142 elif i > 0x7ff:
143 i = 0x7ff
144
145 # insert Q=1 in bit 4
146 return ((i & 0x7F0) << 1) | 0x010 | (i & 0x00F)
147
148
149 def encode_velocity(kts, supersonic):
150 """Encode a groundspeed in kts using the representation expected in DF17 messages"""
151 if kts is None:
152 return 0
153
154 if kts < 0:
155 signbit = 0x400
156 kts = 0 - kts
157 else:
158 signbit = 0
159
160 if supersonic:
161 kts /= 4
162
163 kts = int(kts + 1.5)
164 if kts > 1023:
165 return 1023 | signbit
166 else:
167 return kts | signbit
168
169
170 def encode_vrate(vr):
171 """Encode a vertical rate in fpm using the representation expected in DF17 messages"""
172 if vr is None:
173 return 0
174
175 if vr < 0:
176 signbit = 0x200
177 vr = 0 - vr
178 else:
179 signbit = 0
180
181 vr = int(vr / 64 + 1.5)
182 if vr > 511:
183 return 511 | signbit
184 else:
185 return vr | signbit
186
187
188 def make_altitude_only_frame(addr, lat, lon, alt, df=DF18):
189 """Create an altitude-only DF17 frame"""
190 # ME type 0: airborne position, horizontal position unavailable
191 return make_position_frame(0, addr, 0, 0, encode_altitude(alt), False, df)
192
193
194 def make_position_frame_pair(addr, lat, lon, alt, df=DF18):
195 """Create a pair of DF17 frames - one odd, one even - for the given position"""
196 ealt = encode_altitude(alt)
197 even_lat, even_lon = cpr_encode(lat, lon, False)
198 odd_lat, odd_lon = cpr_encode(lat, lon, True)
199
200 # ME type 18: airborne position, baro alt, NUCp=0
201 eframe = make_position_frame(18, addr, even_lat, even_lon, ealt, False, df)
202 oframe = make_position_frame(18, addr, odd_lat, odd_lon, ealt, True, df)
203
204 return eframe, oframe
205
206
207 def make_position_frame(metype, addr, elat, elon, ealt, oddflag, df):
208 """Create single DF17/DF18 position frame"""
209
210 frame = bytearray(14)
211
212 if df is DF17:
213 # DF=17, CA=6 (ES, Level 2 or above transponder and ability
214 # to set CA code 7 and either airborne or on the ground)
215 frame[0] = (17 << 3) | (6)
216 imf = 0
217 elif df is DF18:
218 # DF=18, CF=2, IMF=0 (ES/NT, fine TIS-B message with 24-bit address)
219 frame[0] = (18 << 3) | (2)
220 imf = 0
221 elif df is DF18ANON:
222 # DF=18, CF=5, IMF=0 (ES/NT, fine TIS-B message with anonymous 24-bit address)
223 frame[0] = (18 << 3) | (5)
224 imf = 0
225 elif df is DF18TRACK:
226 # DF=18, CF=2, IMF=1 (ES/NT, fine TIS-B message with track file number)
227 frame[0] = (18 << 3) | (2)
228 imf = 1
229 else:
230 raise ValueError('df must be DF17 or DF18 or DF18ANON or DF18TRACK')
231
232 frame[1] = (addr >> 16) & 255 # AA
233 frame[2] = (addr >> 8) & 255 # AA
234 frame[3] = addr & 255 # AA
235 frame[4] = (metype << 3) # ME type, status 0
236 frame[4] |= imf # SAF (DF17) / IMF (DF 18)
237 frame[5] = (ealt >> 4) & 255 # Altitude (MSB)
238 frame[6] = (ealt & 15) << 4 # Altitude (LSB)
239 if oddflag:
240 frame[6] |= 4 # CPR format
241 frame[6] |= (elat >> 15) & 3 # CPR latitude (top bits)
242 frame[7] = (elat >> 7) & 255 # CPR latitude (middle bits)
243 frame[8] = (elat & 127) << 1 # CPR latitude (low bits)
244 frame[8] |= (elon >> 16) & 1 # CPR longitude (high bit)
245 frame[9] = (elon >> 8) & 255 # CPR longitude (middle bits)
246 frame[10] = elon & 255 # CPR longitude (low bits)
247
248 # CRC
249 c = _modes.crc(frame[0:11])
250 frame[11] = (c >> 16) & 255
251 frame[12] = (c >> 8) & 255
252 frame[13] = c & 255
253
254 return frame
255
256
257 def make_velocity_frame(addr, nsvel, ewvel, vrate, df=DF18):
258 """Create a DF17/DF18 airborne velocity frame"""
259
260 supersonic = (nsvel is not None and abs(nsvel) > 1000) or (ewvel is not None and abs(ewvel) > 1000)
261
262 e_ns = encode_velocity(nsvel, supersonic)
263 e_ew = encode_velocity(ewvel, supersonic)
264 e_vr = encode_vrate(vrate)
265
266 frame = bytearray(14)
267
268 if df is DF17:
269 # DF=17, CA=6 (ES, Level 2 or above transponder and ability
270 # to set CA code 7 and either airborne or on the ground)
271 frame[0] = (17 << 3) | (6)
272 imf = 0
273 elif df is DF18:
274 # DF=18, CF=2, IMF=0 (ES/NT, fine TIS-B message with 24-bit address)
275 frame[0] = (18 << 3) | (2)
276 imf = 0
277 elif df is DF18ANON:
278 # DF=18, CF=5, IMF=1 (ES/NT, fine TIS-B message with anonymous 24-bit address)
279 frame[0] = (18 << 3) | (5)
280 imf = 0
281 elif df is DF18TRACK:
282 # DF=18, CF=2, IMF=1 (ES/NT, fine TIS-B message with track file number)
283 frame[0] = (18 << 3) | (2)
284 imf = 1
285 else:
286 raise ValueError('df must be DF17 or DF18 or DF18ANON or DF18TRACK')
287
288 frame[1] = (addr >> 16) & 255 # AA
289 frame[2] = (addr >> 8) & 255 # AA
290 frame[3] = addr & 255 # AA
291 frame[4] = (19 << 3) # ES type 19, airborne velocity
292 if supersonic:
293 frame[4] |= 2 # subtype 2, ground speed, supersonic
294 else:
295 frame[4] |= 1 # subtype 1, ground speed, subsonic
296
297 frame[5] = (imf << 7) # IMF, NACp 0
298 frame[5] |= (e_ew >> 8) & 7 # E/W velocity sign and top bits
299 frame[6] = (e_ew & 255) # E/W velocity low bits
300 frame[7] = (e_ns >> 3) & 255 # N/S velocity top bits
301 frame[8] = (e_ns & 7) << 5 # N/S velocity low bits
302 frame[8] |= 16 # vertical rate source = baro
303 frame[8] |= (e_vr >> 6) & 15 # vertical rate top bits
304 frame[9] = (e_vr & 63) << 2 # vertical rate low bits
305 frame[10] = 0 # GNSS/Baro alt offset, no data
306
307 # CRC
308 c = _modes.crc(frame[0:11])
309 frame[11] = (c >> 16) & 255
310 frame[12] = (c >> 8) & 255
311 frame[13] = c & 255
312
313 return frame