-
Notifications
You must be signed in to change notification settings - Fork 1
/
landing_rate.lua
259 lines (201 loc) · 7.8 KB
/
landing_rate.lua
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
-- MIT License
-- Copyright (c) 2020 Holger Teutsch
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- landing_rate.lua
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ start of customizations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local show_time = 40 -- time in seconds to show result window
local textual_rating = true
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ end of customizations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
require("rwdb")
local VERSION = "1.5"
local M_2_FT = 3.2808
-- Source of data (among others)
-- https://www.pprune.org/tech-log/510634-definition-hard-landing-maintenance.html
local a3xx_ratings = {
{180, "Smooth landing"},
{240, "Firm landing"},
{600, "Uncomfortable landing"},
{830, "Hard landing, requires inspection"},
{100000, "Severe hard landing, damage likely"}
}
local acf_model, r5csv_directory, script_directory
local was_airborne = false
local vs, rw, rw_dist, thr_dist, thr_crossed, thr_ra
local sim_type = ipc.readUW(0x3308) -- 12 = p3d, 13 = msfs2020
local window
local window_close_time -- ms until close or nil
local function get_pos()
local lat = ipc.readDD(0x560) * 90.0 / (10001750.0 * 2^32)
local lon = ipc.readDD(0x568) * 360.0 / 2^64
return lat, lon
end
-- m/s
local function get_vs()
local vs = ipc.readDBL(0x31A0) -- vertical GS fpm (Y axis)
return vs / M_2_FT
end
local function get_a3xx_rating(vs)
for i, r in ipairs(a3xx_ratings) do
if vs <= r[1] then return r[2] end
end
return ""
end
local function display_data()
-- the last recorded vs before touch down is the correct one
local td_vs = vs
local ias = ipc.readSD(0x02BC) / 128
local pitch = ipc.readUD(0x578) * 360 / 2^32
if pitch > 180 then pitch = 360 - pitch end -- normalize
local vs_fpm = -td_vs * M_2_FT * 60
local line = string.format("vs: %0.0f fpm, IAS: %0.0f kn, pitch: %.1f°", vs_fpm, ias, pitch)
if textual_rating and (acf_model == "A320" or acf_model == "A321" or acf_model == "A319") then
local rating = get_a3xx_rating(vs_fpm)
line = line .. "\n" .. rating
end
if rw ~= nil then
local lat, lon = get_pos()
ipc.log(string.format("td rw %s %s %f,%f", rw[rw_icao_], rw[rw_designator_], rw[rw_lat_], rw[rw_lon_]))
ipc.log(string.format("td lat,lon %f,%f", lat, lon))
local td_x, td_y = rwdb.mk_thr_vec(rw, lat, lon)
local td_dist = rwdb.vec_length(td_x, td_y) -- dist from threshold
local crl_x, crl_y = rwdb.mk_ctrl_uvec(rw) -- centerline unit vector
local d_crl = td_x * crl_y - td_y * crl_x -- ofs from centerline
local true_hdg = ipc.readUD(0x580) * 360 / 2^32
local true_rw = rw[rw_hdg_] + rw[rw_mag_var_]
local crab = true_hdg - true_rw
-- normalize, a 36 rwy might give -356°
if crab < -180 then
crab = crab + 360
elseif crab > 180 then
crab = crab - 360
end
-- make sure it never bombs
if thr_ra == nil then thr_ra = 0.0 end
line = line ..
string.format("\nThreshold %s/%s\nAbove: %.f ft / %.f m, Distance: %.f ft / %.f m\n" ..
"from CL: %.f ft / %.f m / %.1f°",
rw[rw_icao_], rw[rw_designator_], thr_ra * M_2_FT, thr_ra, td_dist * M_2_FT, td_dist,
d_crl * M_2_FT, d_crl, crab)
end
if sim_type == 13 then
-- msfs2020
if window == nil then
window = wnd.open("Landing Rate", 300, 700, 600, 120)
wnd.backcol(window, 0x000)
wnd.textcol(window, 0x6c0)
wnd.font(window, WND_ARIAL, -5)
end
wnd.show(window, WND_TOPMOST)
wnd.text(window, line)
window_close_time = show_time * 1000 + ipc.elapsedtime()
else
-- p3d
ipc.setdisplay(30, 600, 600, 200)
ipc.display(line, show_time)
end
local pline = "\n--------------------------------------------------------------------\n" ..
line ..
"\n--------------------------------------------------------------------\n"
ipc.log(pline)
end
-- emulate continue via return
local function loop()
if window_close_time ~= nil and window_close_time < ipc.elapsedtime() then
wnd.close(window)
window, window_close_time = nil, nil
end
if ipc.readUW(0x0366) == 1 then -- on ground flag
if was_airborne then
display_data()
was_airborne = false
end
ipc.sleep(5000)
return
end
local ra = ipc.readSD(0x31E4) / 65636.0 -- radar altitude m
if ra > 20 and not was_airborne then -- transition to airborne
was_airborne = true
rw = nil
rw_dist = 1.0E20
thr_dist = 1.0E20
thr_crossed = false
thr_ra = nil
end
if ra >= 500 then
ipc.sleep(5000) -- dormant
return
end
if ra > 150 then
if rw ~= nil then -- zap takeoff rwy
rw = nil
rw_dist = 1.0E20
thr_dist = 1.0E20
thr_crossed = false
thr_ra = nil
end
ipc.sleep(1000) -- start getting nervous
return
end
-- select runway
if ra >= 40 then
local lat, lon = get_pos()
local r, d = rwdb.nearest_rw(lat, lon)
if r ~= nil and d < rw_dist then
rw = r
rw_dist = d
end
ipc.sleep(250)
return
end
-- track crossing of threshold
if not thr_crossed and rw ~= nil then
local lat, lon = get_pos()
-- ipc.log(string.format("ra < 40 lat,lon %f,%f", lat, lon))
local d = rwdb.thr_distance(rw, lat, lon)
if d < thr_dist then
thr_dist = d
thr_ra = ra
else
thr_crossed = true -- distance is increasing again
end
end
if ra > 15 then
ipc.sleep(250)
return
end
-- come here in touchdown phase
vs = get_vs()
ipc.sleep(25) -- FSUIPC seems to pick up data with 18 Hz
end
ipc.log("landing_rate " .. VERSION .. " startup")
ipc.setdisplay(30, 600, 600, 180)
_, _, acf_model = string.find(ipc.readSTR(0x3500, 24), "([%a%d]+)")
ipc.log("ACF model: '" .. acf_model .. "'")
if sim_type == 12 then -- p3d
_, _, r5csv_directory = string.find(ipc.readSTR(0x3E00, 256), "([%a%d%s:\\_%-]+)")
elseif sim_type == 13 then -- msfs 2020
r5csv_directory = ".\\"
end
ipc.log("r5csv_directory: '" .. r5csv_directory .. "'")
script_directory = debug.getinfo(1, "S").source:sub(2)
script_directory = script_directory:match("(.*[/\\])")
ipc.log("script_directory: '" .. script_directory .. "'")
rwdb.build_cache({r5csv_directory .. "r5.csv", script_directory .. "r5_patch.csv"})
while true do
loop()
end