-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtouchdownrecorder.lua
335 lines (292 loc) · 11.6 KB
/
touchdownrecorder.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
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
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- Script: touchdownrecorder.lua
-- Version: 3.0
-- Build: 2017-08-23 03:24z +08
-- Author: Wei Shuai <[email protected]>
-- website: https://github.com/cpuwolf/TouchDownRecorder
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- Usage
--
-- Just copy this file into the "Scripts" directory of FlyWithLua.
--
-- a TouchDown Graphic will be printed on your screen when airplane touch down to the ground
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
require "graphics"
local touchdown_vs_table = {}
local touchdown_g_table = {}
local touchdown_pch_table = {}
local touchdown_air_table = {}
local touchdown_elev_table = {}
local touchdown_eng_table = {}
local touchdown_agl_table = {}
local touchdown_tm_table = {}
local _TD_CHART_HEIGHT = 200
local max_table_elements = 500
show_touchdown_counter = 3
collect_touchdown_data = true
ground_counter = 10
local lastVS = 1.0
local lastG = 1.0
local lastPitch = 1.0
local lastAir = false
local lastElev = 0.0
local lastEng = 0.0
local lastAgl = 0.0
local lastTm = 0.0
local gearFRef = XPLMFindDataRef("sim/flightmodel/forces/fnrml_gear")
local gForceRef = XPLMFindDataRef("sim/flightmodel2/misc/gforce_normal")
local vertSpeedRef = XPLMFindDataRef("sim/flightmodel/position/vh_ind_fpm2")
local pitchRef = XPLMFindDataRef("sim/flightmodel/position/theta")
local elevatorRef = XPLMFindDataRef("sim/flightmodel2/controls/pitch_ratio")
local engRef = XPLMFindDataRef("sim/flightmodel2/engines/throttle_used_ratio")
local aglRef = XPLMFindDataRef("sim/flightmodel/position/y_agl")
local tmRef = XPLMFindDataRef("sim/time/total_flight_time_sec")
local landingString = ""
local IsLogWritten = true
local IsTouchDown = false
local close_x = 0
local close_y = 0
function write_log_file()
-- get airport info
navref = XPLMFindNavAid( nil, nil, LATITUDE, LONGITUDE, nil, xplm_Nav_Airport)
local logAirportId
local logAirportName
-- all output we are not intereted in can be send to variable _ (a dummy variable)
_, _, _, _, _, _, logAirportId, logAirportName = XPLMGetNavAidInfo(navref)
buf = string.format("%s [%s] %s %s %s\n", os.date(), PLANE_TAILNUMBER, logAirportId, logAirportName, landingString)
local file = io.open(SCRIPT_DIRECTORY.."TouchDownRecorderLog.txt", "a+")
file:write(buf)
file:close()
IsLogWritten = true
end
function check_ground(n)
if 0.0 ~= n then
return true
-- LAND
end
return false
-- AIR
end
function is_on_ground()
return check_ground(XPLMGetDataf(gearFRef))
end
function collect_flight_data()
lastVS = XPLMGetDataf(vertSpeedRef)
lastG = XPLMGetDataf(gForceRef)
lastPitch = XPLMGetDataf(pitchRef)
lastAir = check_ground(XPLMGetDataf(gearFRef))
lastElev = XPLMGetDataf(elevatorRef)
lastAgl = XPLMGetDataf(aglRef)
lastTm = XPLMGetDataf(tmRef)
local engtb = XPLMGetDatavf(engRef,0,4)
lastEng = engtb[0]
-- fill the table
table.insert(touchdown_vs_table, lastVS)
table.insert(touchdown_g_table, lastG)
table.insert(touchdown_pch_table, lastPitch)
table.insert(touchdown_air_table, lastAir)
table.insert(touchdown_elev_table, lastElev)
table.insert(touchdown_eng_table, lastEng)
table.insert(touchdown_agl_table, lastAgl)
table.insert(touchdown_tm_table, lastTm)
-- limit the table size to the given maximum
if table.maxn(touchdown_vs_table) > max_table_elements then
table.remove(touchdown_vs_table, 1)
table.remove(touchdown_g_table, 1)
table.remove(touchdown_pch_table, 1)
table.remove(touchdown_air_table, 1)
table.remove(touchdown_elev_table, 1)
table.remove(touchdown_eng_table, 1)
table.remove(touchdown_agl_table, 1)
table.remove(touchdown_tm_table, 1)
end
end
function get_max_val(mytable)
-- calculate max data
local mabs = math.abs
local max_data = 0.0
for k, el in pairs(mytable) do
if mabs(el) > mabs(max_data) then
max_data = el
end
end
return max_data
end
function draw_curve(mytable, cr,cg,cb, text_to_print, x_text_start, y_text_start, x_orig, y_orig, x_start, y_start, max_axis, max_data)
-- now draw the chart line orange
graphics.set_color(cr, cg, cb, 1)
graphics.set_width(1)
-- print text
local x_text = x_text_start
local y_text = y_text_start
local width_text_to_print = measure_string(text_to_print)
draw_string(x_text, y_text, text_to_print, cr, cg, cb)
x_text = x_text + width_text_to_print
-- draw line
local x_tmp = x_start
local y_tmp = y_start
local last_recorded = mytable[1]
local draw_max_counter = 0
for k, p in pairs(mytable) do
local y_height = (p / max_axis * _TD_CHART_HEIGHT)
graphics.draw_line(x_tmp, y_tmp + (last_recorded / max_axis * _TD_CHART_HEIGHT), x_tmp + 2, y_tmp + y_height)
if p == max_data then
if draw_max_counter == 0 then
graphics.draw_line(x_tmp, y_orig, x_tmp, y_orig + _TD_CHART_HEIGHT)
end
draw_max_counter = draw_max_counter + 1
end
x_tmp = x_tmp + 2
last_recorded = p
end
return x_text
end
function draw_touchdown_graph()
if collect_touchdown_data == true then
collect_flight_data()
end
-- dont draw when the function isn't wanted
if show_touchdown_counter <= 0 then return end
-- draw background first
local x = (SCREEN_WIDTH / 2) - max_table_elements
local y = SCREEN_HIGHT - 250
XPLMSetGraphicsState(0,0,0,1,1,0,0)
graphics.set_color(0, 0, 0, 0.50)
graphics.draw_rectangle(x, y, x + (max_table_elements * 2), y + _TD_CHART_HEIGHT)
-- draw center line
graphics.set_color(0, 0, 0, 1)
graphics.set_width(3)
graphics.draw_line(x, y + (_TD_CHART_HEIGHT / 2), x + (max_table_elements * 2), y + (_TD_CHART_HEIGHT / 2))
-- draw horizontal axis
x_tmp = x
local last_tm_recorded = touchdown_tm_table[1]
for k, a in pairs(touchdown_tm_table) do
-- second axis
if a - last_tm_recorded >= 1.0 then
-- 1 second
graphics.draw_line(x_tmp, y, x_tmp, y + _TD_CHART_HEIGHT)
last_tm_recorded = touchdown_tm_table[k]
end
x_tmp = x_tmp + 2
end
-- and print on the screen
graphics.set_color(1, 1, 1, 1)
graphics.set_width(3)
-- title
draw_string(x + 5, y + _TD_CHART_HEIGHT - 15, "TouchDownRecorder V3.0 by cpuwolf", "grey")
local x_text = x + 5
local y_text = y + 8
-- draw touch point vertical lines
x_tmp = x
landingString = ""
local last_air_recorded = touchdown_air_table[1]
for k, a in pairs(touchdown_air_table) do
if a ~= last_air_recorded then
if a then
IsTouchDown = true
-- draw vertical line
graphics.draw_line(x_tmp, y + (_TD_CHART_HEIGHT/4), x_tmp, y + (_TD_CHART_HEIGHT*3/4))
-- print text
landingVS = touchdown_vs_table[k]
landingG = touchdown_g_table[k]
landingPitch = touchdown_pch_table[k]
text_to_print = string.format("%.02f", landingVS).."fpm "..string.format("%.02f", landingG).."G "..string.format("%.02f", landingPitch).."Degree | "
landingString = landingString..text_to_print
width_text_to_print = measure_string(text_to_print)
draw_string(x_text, y_text, text_to_print)
x_text = x_text + width_text_to_print
end
end
x_tmp = x_tmp + 2
last_air_recorded = a
end
-- now draw the chart line green
max_vs_axis = 1000.0
max_vs_recorded = get_max_val(touchdown_vs_table)
text_to_p = "Max "..string.format("%.02f", max_vs_recorded).."fpm "
x_text = draw_curve(touchdown_vs_table, 0,1,0, text_to_p, x_text, y_text, x, y, x, y + (_TD_CHART_HEIGHT / 2), max_vs_axis, max_vs_recorded)
-- now draw the chart line red
max_g_axis = 2.0
max_g_recorded = get_max_val(touchdown_g_table)
text_to_p = "Max "..string.format("%.02f", max_g_recorded).."G "
x_text = draw_curve(touchdown_g_table, 1,0.68,0.78, text_to_p, x_text, y_text, x, y, x, y, max_g_axis, max_g_recorded)
-- now draw the chart line light blue
max_pch_axis = 14.0
max_pch_recorded = get_max_val(touchdown_pch_table)
text_to_p = "Max pitch "..string.format("%.02f", max_pch_recorded).."Degree "
x_text = draw_curve(touchdown_pch_table, 0.6,0.85,0.87, text_to_p, x_text, y_text, x, y, x, y + (_TD_CHART_HEIGHT / 2), max_pch_axis, max_pch_recorded)
-- now draw the chart line orange
max_elev_axis = 2.0
max_elev_recorded = get_max_val(touchdown_elev_table)
text_to_p = "Max elevator "..string.format("%.02f", max_elev_recorded*100.0).."% "
x_text = draw_curve(touchdown_elev_table, 1.0,0.49,0.15, text_to_p, x_text, y_text, x, y, x, y + (_TD_CHART_HEIGHT / 2), max_elev_axis, max_elev_recorded)
-- now draw the chart line yellow
max_eng_axis = 2.0
max_eng_recorded = get_max_val(touchdown_eng_table)
text_to_p = "Max eng "..string.format("%.02f", max_eng_recorded*100.0).."% "
x_text = draw_curve(touchdown_eng_table, 1.0,1.0,0.0, text_to_p, x_text, y_text, x, y, x, y + (_TD_CHART_HEIGHT / 2), max_eng_axis, max_eng_recorded)
-- now draw the chart line red
max_agl_axis = 6.0
max_agl_recorded = get_max_val(touchdown_agl_table)
text_to_p = "Max AGL "..string.format("%.02f", max_agl_recorded).."M "
x_text = draw_curve(touchdown_agl_table, 1.0,0.1,0.1, text_to_p, x_text, y_text, x, y, x, y + (_TD_CHART_HEIGHT / 2), max_agl_axis, max_agl_recorded)
-- draw close button on top-right
graphics.set_color(1, 1, 1, 1)
close_x = x + (2*max_table_elements) - 18
close_y = y + _TD_CHART_HEIGHT - 18
draw_string_Helvetica_18( close_x, close_y, "X")
end
function check_click( point_x, point_y, rect_x, rect_y, rect_w, rect_h)
if point_x < rect_x then return false end
if point_x > rect_x+rect_w then return false end
if point_y < rect_y then return false end
if point_y > rect_y+rect_h then return false end
return true
end
function mouse_clck()
-- check mouse click
if check_click(MOUSE_X, MOUSE_Y, close_x, close_y, 18, 18) then
show_touchdown_counter = 0
end
end
function calc_touchdown()
is_gnd = is_on_ground()
if is_gnd then
ground_counter = ground_counter + 1
-- ignore debounce takeoff
if ground_counter == 2 then
show_touchdown_counter = 4
-- stop data collection
elseif ground_counter == 3 then
collect_touchdown_data = false
if IsTouchDown then
IsLogWritten = false
end
elseif ground_counter == 5 then
if not IsLogWritten then
write_log_file()
end
end
else
-- in the air
ground_counter = 0
collect_touchdown_data = true
IsTouchDown = false
end
-- count down
if show_touchdown_counter > 0 then
show_touchdown_counter = show_touchdown_counter - 1
end
end
function toggle_show()
if show_touchdown_counter > 0 then
show_touchdown_counter = 0
else
show_touchdown_counter = 60
end
end
do_every_draw("draw_touchdown_graph()")
do_often("calc_touchdown()")
do_on_mouse_click( "mouse_clck()")
add_macro("Show TouchDownRecorder", "show_touchdown_counter = 60")
create_command("FlyWithLua/TouchDownRecorder/Show", "Toggle TouchDownRecorder Chart", "toggle_show()","","")