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
use std::f64::consts::PI;
pub const ADSB_SIZE_BYTES: usize = 14;
#[derive(Debug, Copy, Clone)]
pub enum DecodeError {
CrossedLatitudeZones,
}
pub fn get_adsb_icao_address(icao: &[u8; 3]) -> u32 {
let mut bytes = [0; 4];
bytes[1..4].copy_from_slice(icao);
u32::from_be_bytes(bytes)
}
pub fn get_adsb_message_type(bytes: &[u8; ADSB_SIZE_BYTES]) -> i64 {
((bytes[4] >> 3) & 0x1F) as i64
}
pub fn decode_altitude(altitude: u16) -> f32 {
let altitude: u32 = altitude as u32;
let coef_ft: u32 = if (0x010 & altitude) > 0 { 25 } else { 100 };
let n: u32 = ((0xFE0 & altitude) >> 1) | (0xF & altitude);
let alt_ft = n * coef_ft - 1000;
0.3048 * alt_ft as f32
}
fn modulus(x: f64, y: f64) -> f64 {
x - y * ((x / y).floor())
}
fn nl(lat: f64) -> f64 {
const NZ: f64 = 30.; let numerator: f64 = 2. * PI;
let a = 1. - (PI / NZ).cos();
let b = (1. + (2. * (PI * lat / 180.)).cos()) / 2.;
let x = a / b;
let mut denominator = 1. - x;
if denominator < -1. {
denominator = -1.;
} else if denominator > 1. {
denominator = 1.;
}
denominator = denominator.acos();
let result = numerator / denominator;
result.floor()
}
pub fn decode_cpr(
lat_cpr_even: u32,
lon_cpr_even: u32,
lat_cpr_odd: u32,
lon_cpr_odd: u32,
) -> Result<(f64, f64), DecodeError> {
let lat_cpr_even: f64 = lat_cpr_even as f64 / 131072.;
let lon_cpr_even: f64 = lon_cpr_even as f64 / 131072.;
let lat_cpr_odd: f64 = lat_cpr_odd as f64 / 131072.;
let lon_cpr_odd: f64 = lon_cpr_odd as f64 / 131072.;
let lat_index: f64 = (59. * lat_cpr_even - 60. * lat_cpr_odd + 0.5).floor();
let dlat_even = 6.0; let dlat_odd = 6.101694915254237; let mut lat_even: f64 = dlat_even * (lat_cpr_even + modulus(lat_index, 60.));
let mut lat_odd: f64 = dlat_odd * (lat_cpr_odd + modulus(lat_index, 59.));
if lat_even >= 270. {
lat_even -= 360.;
}
if lat_odd >= 270. {
lat_odd -= 360.;
}
let latitude: f64 = lat_even; let nl_le: f64 = nl(lat_even);
let nl_lo: f64 = nl(lat_odd);
if nl_le != nl_lo {
return Err(DecodeError::CrossedLatitudeZones);
}
let ni = if nl_le < 1. { 1. } else { nl_le };
let dlon: f64 = 360. / ni;
let m: f64 = (lon_cpr_even * (nl_le - 1.) - lon_cpr_odd * nl_le + 0.5).floor();
let mut longitude: f64 = dlon * (modulus(m, ni) + lon_cpr_even);
if longitude >= 180. {
longitude -= 360.;
}
Ok((latitude, longitude))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ut_number_of_longitude_zones() {
assert_eq!(nl(0.), 59.);
assert_eq!(nl(87.), 2.);
assert_eq!(nl(-87.), 2.);
}
#[test]
fn ut_decode_cpr() {
let lat_even = 0b10110101101001000;
let lon_even = 0b01100100010101100;
let lat_odd = 0b10010000110101110;
let lon_odd = 0b01100010000010010;
let (latitude, longitude) = decode_cpr(lat_even, lon_even, lat_odd, lon_odd).unwrap();
println!("(ut_decode_cpr) lat: {}, lon: {}", latitude, longitude);
assert!((latitude - 52.25720214843750).abs() < 0.0000001);
assert!((longitude - 3.91937).abs() < 0.0001);
}
#[test]
fn ut_decode_altitude() {
let alt = 0b110000111000;
let expected_ft: f32 = 38000.;
let expected_meters = expected_ft * 0.3048;
let altitude = decode_altitude(alt);
assert!((altitude - expected_meters).abs() < 0.001);
}
}