-
Notifications
You must be signed in to change notification settings - Fork 7
/
xgs.c
1158 lines (909 loc) · 32.6 KB
/
xgs.c
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <float.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <sys/param.h>
#define XPLM200
#define XPLM210
#define XPLM300
#define XPLM301
#include "XPLMPlugin.h"
#include "XPLMPlanes.h"
#include "XPLMDisplay.h"
#include "XPLMGraphics.h"
#include "XPLMDataAccess.h"
#include "XPLMUtilities.h"
#include "XPLMProcessing.h"
#include "XPLMMenus.h"
#include "XPLMNavigation.h"
#include "XPWidgets.h"
#include "XPStandardWidgets.h"
#include <acfutils/assert.h>
#include <acfutils/airportdb.h>
#define VERSION "3.50"
static float flight_loop_cb(float inElapsedSinceLastCall,
float inElapsedTimeSinceLastFlightLoop, int inCounter,
void *inRefcon);
static void force_widget_visible(int widget_width);
#define MS_2_FPM 196.850
#define M_2_FT 3.2808
#define G 9.80665
#define ACF_STATE_GROUND 1
#define ACF_STATE_AIR 2
#define N_WIN_LINE 9
#define WINDOW_HEIGHT (N_WIN_LINE * 15 + 35)
#define STD_WINDOW_WIDTH 185
#define SIDE_MARGIN 10
static int xgs_enabled;
static int init_done;
static int init_failure;
static XPWidgetID main_widget;
static XPLMDataRef gear_fnrml_dr, flight_time_dr, acf_num_dr, icao_dr,
lat_dr, lon_dr, elevation_dr, y_agl_dr, hdg_dr, vy_dr, vr_enabled_dr,
theta_dr, phi_dr, in_replay_dr;
/* acf specific datarefs and data */
static XPLMDataRef acf_ias_dr, toliss_vls_dr, toliss_strut_compress_dr;
static float toliss_vls;
static int toliss_a340; /* 1 = is A34x */
const char *acf_ias_unit;
static float acf_ias_conv; /* conversion from knt to unit */
static char acf_tailnum[50];
static char acf_icao[40];
static int get_acf_dr_done; /* try to get acf specific vls_dr in flight_loop */
static char landMsg[N_WIN_LINE][100];
static geo_pos3_t cur_pos;
static vect3_t last_pos_v;
static int acf_last_state;
static float landing_vspeed;
static float last_vspeed;
static float landing_G;
static float landing_theta, landing_phi;
static float lastG;
static float landing_ias;
static vect2_t landing_thr_v; /* landing threshold */
static float nose_wheel_td_dist = 0; /* dist until nose wheel td */
typedef
struct show_time_ctx_ {
const char *label;
int idx;
float duration;
} show_time_ctx_t;
static show_time_ctx_t show_time_ctx[] = {
{" 5 seconds", 0, 5.0},
{"10 seconds", 0, 10.0},
{"30 seconds", 0, 30.0},
{"60 seconds", 0, 60.0},
{"Until closed", 0, FLT_MAX}
};
#define N_SHOW_TIME_CTX 5
/* these must be < 0 */
#define MENU_LOG_ENABLE -1
#define MENU_SHOW_IN_REPLAY -2
static int show_time_idx = 3; /* 60 seconds */
static float remaining_show_time;
static float remaining_update_time;
static float air_time;
static int win_pos_x;
static int win_pos_y;
static int widget_in_vr;
static XPLMMenuID xgs_menu;
static int enable_log_item, show_in_replay_item;
static int log_enabled = 0;
static int show_in_replay = 0;
typedef struct rating_ { float limit; char txt[100]; } rating_t;
static rating_t std_rating[] = {
{0.5, "excellent landing"},
{1.0, "good landing"},
{1.5, "acceptable landing"},
{2.0, "hard landing"},
{2.5, "you are fired!!!"},
{3.0, "anybody survived?"},
{FLT_MAX, "R.I.P."},
};
#define NRATING 10
static rating_t cfg_rating[NRATING];
static rating_t *rating = std_rating;
static int window_width;
static char xpdir[512];
static const char *psep;
static airportdb_t airportdb;
static list_t *near_airports;
static float arpt_last_reload;
/* will be set on transitioning into the rwy_bbox, if set then reloading is paused */
static const runway_t *landing_rwy;
static int landing_rwy_end;
static double landing_cross_height;
static double landing_dist;
static int touchdown;
static double landing_cl_delta, landing_cl_angle;
typedef struct ts_val_s {
float ts; /* timestamp */
float vy; /* vy */
double g; /* g as derivative of vy */
double g_lp; /* g after low pass filtering */
} ts_val_t;
/* length of array */
#define N_TS_VY 4
/* order of LP filter */
#define G_LP_ORDER 3
#if G_LP_ORDER > (N_TS_VY-1)
#error G_LP_ORDER too large
#endif
/* initialize so we never get a divide by 0 in compute_g */
static ts_val_t ts_vy[N_TS_VY] = { {-2.0f}, {-1.0f} };
static int ts_val_cur = 2;
static int loops_in_touchdown;
/* get (and set) the acf specific datarefs here in one place */
static void get_acf_dr()
{
/* default */
acf_ias_conv = 1.0;
acf_ias_unit = "kts";
acf_ias_dr = XPLMFindDataRef("sim/flightmodel/position/indicated_airspeed");
/* ASK21 speed is metric */
if (0 == strcmp(acf_icao, "AS21")) {
logMsg("ASK21 detected");
acf_ias_unit = "km/h";
acf_ias_conv = 1.852;
return;
}
/* try ToLiss A3xx */
toliss_vls_dr = XPLMFindDataRef("toliss_airbus/pfdoutputs/general/VLS_value");
if (toliss_vls_dr) {
logMsg("ToLiss A3xx detected");
acf_ias_dr = XPLMFindDataRef("AirbusFBW/IASCapt");
toliss_a340 = (0 == strncmp(acf_icao, "A34", 3));
/* the A340 uses the GearStrutCompressDist_m dr */
toliss_strut_compress_dr = NULL;
if (toliss_a340) {
toliss_strut_compress_dr = XPLMFindDataRef("AirbusFBW/GearStrutCompressDist_m");
if (toliss_strut_compress_dr == NULL)
logMsg(".. but toliss_strut_compress_dr is not defined");
} else /* the narrow bodies use this standard dr */
toliss_strut_compress_dr = XPLMFindDataRef("sim/flightmodel2/gear/tire_vertical_deflection_mtr");
}
}
static FILE* fopen_config(char *mode)
{
char path[512];
XPLMGetPrefsPath(path);
XPLMExtractFileAndPath(path);
strcat(path, psep);
strcat(path, "xgs.prf");
return fopen(path, mode);
}
static void save_config()
{
FILE *f;
f = fopen_config("w");
if (! f)
return;
fprintf(f, "%i %i %i %i %i", win_pos_x, win_pos_y, log_enabled, show_time_idx, show_in_replay);
fclose(f);
}
static void load_config()
{
FILE *f;
f = fopen_config("r");
if (! f)
return;
fscanf(f, "%i %i %i %i %i", &win_pos_x, &win_pos_y, &log_enabled, &show_time_idx, &show_in_replay);
fclose(f);
/* this is an index into an array, so sanitize */
if (show_time_idx < 0 || show_time_idx >= N_SHOW_TIME_CTX)
show_time_idx = 3;
}
static void force_widget_visible(int widget_width)
{
/* we use modern windows under the hut so UI coordinates are in boxels */
int xl, yl, xr, yr;
XPLMGetScreenBoundsGlobal(&xl, &yr, &xr, &yl);
win_pos_x = (win_pos_x + widget_width < xr) ? win_pos_x : xr - widget_width - 50;
win_pos_x = (win_pos_x <= xl) ? 20 : win_pos_x;
win_pos_y = (win_pos_y + WINDOW_HEIGHT < yr) ? win_pos_y : (yr - WINDOW_HEIGHT - 50);
win_pos_y = (win_pos_y >= WINDOW_HEIGHT) ? win_pos_y : (yr / 2);
logMsg("force_widget_visible: s: (%d, %d) -> (%d, %d), ww: %d, wp: (%d, %d)",
xl, yl, xr, yr, widget_width, win_pos_x, win_pos_y);
}
static void update_menu_items()
{
XPLMCheckMenuItem(xgs_menu, enable_log_item,
log_enabled ? xplm_Menu_Checked : xplm_Menu_Unchecked);
XPLMCheckMenuItem(xgs_menu, show_in_replay_item,
show_in_replay ? xplm_Menu_Checked : xplm_Menu_Unchecked);
for (int i = 0; i < N_SHOW_TIME_CTX; i++) {
XPLMCheckMenuItem(xgs_menu, show_time_ctx[i].idx,
i == show_time_idx ? xplm_Menu_Checked : xplm_Menu_Unchecked);
}
}
static void xgs_menu_cb(void *menuRef, void *param)
{
if (MENU_LOG_ENABLE == (long long)param) {
log_enabled = ! log_enabled;
} else if (MENU_SHOW_IN_REPLAY == (long long)param) {
show_in_replay = ! show_in_replay;
} else {
show_time_idx = (long long)param;
}
update_menu_items();
}
static void trim(char *str)
{
int len = strlen(str);
len--;
while (0 < len) {
if (('\r' == str[len]) || ('\n' == str[len])) {
str[len] = 0;
len--;
} else
return;
}
}
static void update_landing_log()
{
if (XPLMGetDatai(in_replay_dr)) /* do not log in replay mode */
return;
FILE *f;
char buf[512];
char airport_id[50];
sprintf(buf, "%sOutput%sxgs_landing.log", xpdir, psep);
f = fopen(buf, "a");
if (! f) return;
/* in case we didn't fix a runway... */
float lat = cur_pos.lat;
float lon = cur_pos.lon;
XPLMNavRef ref = XPLMFindNavAid(NULL, NULL, &lat, &lon, NULL, xplm_Nav_Airport);
if (XPLM_NAV_NOT_FOUND != ref) {
XPLMGetNavAidInfo(ref, NULL, &lat, &lon, NULL, NULL, NULL, airport_id,
NULL, NULL);
} else {
airport_id[0] = '\0';
}
time_t now = time(NULL);
strftime(buf, sizeof buf, "%c", localtime(&now));
fprintf(f, "%s %s %s %s %.3f m/s %.0f fpm %.1f° pitch %.3f G, ", buf, acf_icao, acf_tailnum,
airport_id, landing_vspeed,
landing_vspeed * MS_2_FPM, landing_theta, landing_G);
if (0.0 < landing_dist) {
fprintf(f, "Threshold %s, Above: %.f ft / %.f m, Distance: %.f ft / %.f m, from CL: %.f ft / %.f m / %.1f°, ",
landing_rwy->ends[landing_rwy_end].id,
landing_cross_height * M_2_FT, landing_cross_height,
landing_dist * M_2_FT, landing_dist,
landing_cl_delta * M_2_FT, landing_cl_delta,
landing_cl_angle);
} else {
fputs("Not on a runway!, ", f);
}
fprintf(f, "%s\n", landMsg[0]);
fclose(f);
}
static void close_event_window()
{
if (main_widget) {
XPGetWidgetGeometry(main_widget, &win_pos_x, &win_pos_y, NULL, NULL);
XPHideWidget(main_widget);
logMsg("widget closed at (%d,%d)", win_pos_x, win_pos_y);
}
landing_vspeed = 0.0;
landing_G = 0.0;
remaining_show_time = 0.0;
air_time = 0.0;
landing_ias = 0.0;
toliss_vls = 0.0;
landing_rwy = NULL;
nose_wheel_td_dist = -1.0; /* -1.0 meaning landing_thr_v not initialized */
touchdown = 0;
landing_dist = -1.0;
}
static int load_rating_cfg(const char *path)
{
logMsg("trying rating config file '%s'", path);
FILE *f = fopen(path, "r");
if (f) {
char line[200];
int i = 0;
int firstLine = 1;
while (fgets(line, sizeof line, f) && i < NRATING) {
if (line[0] == '#') continue;
trim(line);
if ('\0' == line[0])
continue;
if (firstLine) {
firstLine = 0;
if (0 == strcmp(line, "V30")) {
continue; /* the only version currently supported */
} else {
logMsg("Config file does not start with version number");
break;
}
}
char *s2 = NULL;
char *s1 = strchr(line, ';');
if (s1) {
*s1++ = '\0';
s2 = strchr(s1, ';');
}
if (NULL == s1 || NULL == s2) {
logMsg("ill formed line -> %s", line);
break;
}
s2++;
float v_ms = fabs(atof(line));
float v_fpm = fabs(atof(s1));
logMsg("%f, %f, <%s>", v_ms, v_fpm, s2);
s2 = strncpy(cfg_rating[i].txt, s2, sizeof(cfg_rating[i].txt));
cfg_rating[i].txt[ sizeof(cfg_rating[i].txt) -1 ] = '\0';
if (v_ms > 0) {
cfg_rating[i].limit = v_ms;
} else if (v_fpm > 0) {
cfg_rating[i].limit = v_fpm / MS_2_FPM;
} else {
cfg_rating[i].limit = FLT_MAX;
break;
}
i++;
}
fclose(f);
if (i < NRATING && FLT_MAX == cfg_rating[i].limit) {
rating = cfg_rating;
logMsg("rating config file '%s' loaded successfully", path);
return 1;
}
logMsg("Invalid config file '%s'", path);
}
return 0;
}
static int map_acf_to_cfg(const char *acf, const char *map_path, char *cfg_name)
{
logMsg("trying to map '%s' to cfg", acf);
FILE *f = fopen(map_path, "r");
if (NULL == f) {
logMsg("mapping file '%s' don't exist", map_path);
return 0;
}
char line[200];
int ret = 0;
while (fgets(line, sizeof line, f)) {
if (line[0] == '#') continue;
trim(line);
if ('\0' == line[0])
continue;
char *cptr = strchr(line, ' ');
if (NULL == cptr) {
logMsg("bad line: %s", line);
continue;
}
*cptr++ = '\0';
if (0 == strcmp(acf, line)) {
strcpy(cfg_name, cptr);
ret = 1;
break;
}
}
fclose(f);
return ret;
}
static int get_current_state()
{
/* ToLiss specific: check strut compression > 0.01 m */
if (toliss_strut_compress_dr) {
float sc[2];
XPLMGetDatavf(toliss_strut_compress_dr, sc, 1, 2); /* main gear */
return (sc[0] > 0.01 || sc[1] > 0.01) ? ACF_STATE_GROUND : ACF_STATE_AIR;
} else
return 0.0 != XPLMGetDataf(gear_fnrml_dr) ? ACF_STATE_GROUND : ACF_STATE_AIR;
}
static int print_landing_message()
{
ASSERT(NULL != landing_rwy);
int w_width = STD_WINDOW_WIDTH;
/* rating terminates with FLT_MAX */
int i = 0;
while (fabs(landing_vspeed) > rating[i].limit) i++;
strcpy(landMsg[0], rating[i].txt);
w_width = MAX(w_width, (int)(2*SIDE_MARGIN + ceil(XPLMMeasureString(xplmFont_Basic, landMsg[0], strlen(landMsg[0])))));
sprintf(landMsg[1], "Vy: %.0f fpm / %.2f m/s / p %.1f° / b %.1f°",
landing_vspeed * MS_2_FPM, landing_vspeed, landing_theta, landing_phi);
w_width = MAX(w_width, (int)(2*SIDE_MARGIN + ceil(XPLMMeasureString(xplmFont_Basic, landMsg[1], strlen(landMsg[1])))));
i = 2;
if (landing_ias > 0) {
if (toliss_vls > 0) {
sprintf(landMsg[i++], "IAS / VLS: %.0f / %.0f %s",
landing_ias * acf_ias_conv, toliss_vls, acf_ias_unit);
} else {
sprintf(landMsg[i++], "IAS: %.0f %s",
landing_ias * acf_ias_conv, acf_ias_unit);
}
}
sprintf(landMsg[i++], "G: %.2f", landing_G);
if (landing_dist > 0.0) {
sprintf(landMsg[i++], "Threshold %s/%s", landing_rwy->arpt->icao, landing_rwy->ends[landing_rwy_end].id);
sprintf(landMsg[i++], "Above: %.f ft / %.f m", landing_cross_height * M_2_FT, landing_cross_height);
if (toliss_strut_compress_dr)
sprintf(landMsg[i++], "Main wheel TD: %.f ft / %.f m", landing_dist * M_2_FT, landing_dist);
else
sprintf(landMsg[i++], "Distance: %.f ft / %.f m", landing_dist * M_2_FT, landing_dist);
if (nose_wheel_td_dist > 0.0)
sprintf(landMsg[i++], "Nose wheel TD: %.f ft / %.f m", nose_wheel_td_dist * M_2_FT, nose_wheel_td_dist);
sprintf(landMsg[i], "from CL: %.f ft / %.f m / %.1f°",
landing_cl_delta * M_2_FT, landing_cl_delta, landing_cl_angle);
w_width = MAX(w_width, (int)(2*SIDE_MARGIN + ceil(XPLMMeasureString(xplmFont_Basic, landMsg[i], strlen(landMsg[i])))));
} else {
strcpy(landMsg[i], "Not on a runway!");
}
ASSERT(i < N_WIN_LINE); /* better safe than sorry */
if (i < N_WIN_LINE - 1)
landMsg[i+1][0] = '\0';
return w_width;
}
static void update_landing_result()
{
int changed = 0;
if (landing_vspeed > last_vspeed) {
landing_vspeed = last_vspeed;
changed = 1;
}
if (landing_G < lastG) {
landing_G = lastG;
changed = 1;
}
/* ToLiss specific: record distance to nose wheel td */
if (toliss_strut_compress_dr != NULL && nose_wheel_td_dist == 0.0 && landing_dist > 0.0
&& landing_rwy != NULL) { /* this on may be redundant but better be safe */
float sc;
int nw = toliss_a340 ? 3 : 0; /* narrow body: 0, A340: 3 */
XPLMGetDatavf(toliss_strut_compress_dr, &sc, nw, 1); /* nose wheel */
if (sc > 0.01) {
vect2_t pos_v = geo2fpp(GEO3_TO_GEO2(cur_pos), &landing_rwy->arpt->fpp);
nose_wheel_td_dist = vect2_abs(vect2_sub(pos_v, landing_thr_v));
logMsg("dist to nose wheel TD: %0.1f m", nose_wheel_td_dist);
changed = 1;
}
}
if (changed || landMsg[0][0]) {
int w = print_landing_message();
if (w > window_width) {
window_width = w;
force_widget_visible (window_width);
XPSetWidgetGeometry(main_widget, win_pos_x, win_pos_y,
win_pos_x + window_width, win_pos_y - WINDOW_HEIGHT);
}
}
}
static int widget_cb(XPWidgetMessage msg, XPWidgetID widget_id, intptr_t param1, intptr_t param2)
{
if (widget_id == main_widget) {
if (msg == xpMessage_CloseButtonPushed) {
close_event_window();
return 1;
}
return 0;
}
/* for the embedded custom widget */
if (xpMsg_Draw == msg) {
int left, top;
XPGetWidgetGeometry(widget_id, &left, &top, NULL, NULL);
static float color[] = { 1.0, 1.0, 1.0 }; /* RGB White */
for (int i = 0; i < N_WIN_LINE; i++) {
if ('\0' == landMsg[i][0])
break;
XPLMDrawString(color, left, top - (i+1)*15, landMsg[i], NULL, xplmFont_Basic);
}
return 1;
}
return 0;
}
static void create_event_window()
{
remaining_show_time = show_time_ctx[show_time_idx].duration;
window_width = STD_WINDOW_WIDTH;
force_widget_visible(window_width);
int left = win_pos_x;
int top = win_pos_y;
if (NULL == main_widget) {
main_widget = XPCreateWidget(left, top, left + STD_WINDOW_WIDTH, top - WINDOW_HEIGHT,
0, "Landing Speed " VERSION, 1, NULL, xpWidgetClass_MainWindow);
XPSetWidgetProperty(main_widget, xpProperty_MainWindowType, xpMainWindowStyle_Translucent);
XPSetWidgetProperty(main_widget, xpProperty_MainWindowHasCloseBoxes, 1);
XPAddWidgetCallback(main_widget, widget_cb);
left += SIDE_MARGIN; top -= 20;
(void)XPCreateCustomWidget(left, top, left + STD_WINDOW_WIDTH, top - WINDOW_HEIGHT,
1, "", 0, main_widget, widget_cb);
} else {
/* reset to standard width */
window_width = STD_WINDOW_WIDTH;
XPSetWidgetGeometry(main_widget, win_pos_x, win_pos_y,
win_pos_x + window_width, win_pos_y - WINDOW_HEIGHT);
}
update_landing_result();
XPShowWidget(main_widget);
logMsg("widget opened at (%d,%d) width %d", win_pos_x, win_pos_y, window_width);
int in_vr = (NULL != vr_enabled_dr) && XPLMGetDatai(vr_enabled_dr);
if (in_vr) {
logMsg("VR mode detected");
XPLMWindowID window = XPGetWidgetUnderlyingWindow(main_widget);
XPLMSetWindowPositioningMode(window, xplm_WindowVR, -1);
widget_in_vr = 1;
} else {
if (widget_in_vr) {
logMsg("widget now out of VR, map at (%d,%d)", win_pos_x, win_pos_y);
XPLMWindowID window = XPGetWidgetUnderlyingWindow(main_widget);
XPLMSetWindowPositioningMode(window, xplm_WindowPositionFree, -1);
/* A resize is necessary so it shows up on the main screen again */
force_widget_visible(window_width);
XPSetWidgetGeometry(main_widget, win_pos_x, win_pos_y,
win_pos_x + window_width, win_pos_y - WINDOW_HEIGHT);
widget_in_vr = 0;
}
}
}
static void get_near_airports()
{
ASSERT(NULL == landing_rwy);
if (near_airports)
free_nearest_airport_list(near_airports);
load_nearest_airport_tiles(&airportdb, GEO3_TO_GEO2(cur_pos));
unload_distant_airport_tiles(&airportdb, GEO3_TO_GEO2(cur_pos));
near_airports = find_nearest_airports(&airportdb, GEO3_TO_GEO2(cur_pos));
}
/*
* Catch the transition into the rwy_bbox of the nearest threshold.
*
*/
static void fix_landing_rwy()
{
/* have it already */
if (landing_rwy)
return;
double thresh_dist_min = 1.0E12;
float hdg = XPLMGetDataf(hdg_dr);
int in_rwy_bb = 0;
const airport_t *min_arpt;
const runway_t *min_rwy;
int min_end;
ASSERT(NULL != near_airports);
/* loop over all runway ends */
for (const airport_t *arpt = list_head(near_airports);
arpt != NULL; arpt = list_next(near_airports, arpt)) {
ASSERT(arpt->load_complete);
vect2_t pos_v = geo2fpp(GEO3_TO_GEO2(cur_pos), &arpt->fpp);
for (const runway_t *rwy = avl_first(&arpt->rwys); rwy != NULL;
rwy = AVL_NEXT(&arpt->rwys, rwy)) {
if (point_in_poly(pos_v, rwy->rwy_bbox)) {
for (int e = 0; e <=1; e++) {
const runway_end_t *rwy_end = &rwy->ends[e];
double rhdg = fabs(rel_hdg(hdg, rwy_end->hdg));
if (rhdg > 20)
continue;
vect2_t thr_v = rwy_end->thr_v;
double dist = vect2_abs(vect2_sub(thr_v, pos_v));
if (dist < thresh_dist_min) {
thresh_dist_min = dist;
in_rwy_bb = 1;
min_arpt = arpt;
min_rwy = rwy;
min_end = e;
}
}
}
}
}
if (in_rwy_bb) {
landing_rwy = min_rwy;
landing_rwy_end = min_end;
landing_cross_height = XPLMGetDataf(y_agl_dr);
logMsg("fix runway airport: %s, runway: %s, distance: %0.0f",
min_arpt->icao, landing_rwy->ends[landing_rwy_end].id, thresh_dist_min);
/* get VLS now because it may be flushed on touch down */
if (toliss_vls_dr) {
toliss_vls = XPLMGetDataf(toliss_vls_dr);
logMsg("ToLiss A3xx vls %0.1f", toliss_vls);
}
}
}
#ifdef DEBUG_G_LP
/* put values in CSV format into log so it can be grepped out easily */
#define MAX_GREC 200
typedef struct grec_s {double t,v,g; } grec_t;
static grec_t grec[MAX_GREC];
static int n_grec;
static void dump_grec()
{
grec_t *p = &grec[0];
double tstart = p->t;
for (int i = 1; i < n_grec; i++) {
grec_t *gr = &grec[i];
logMsg("grec# %f;%f;%f", gr->t - tstart, gr->v, gr->g);
}
n_grec = 0;
}
static void record_grec(const ts_val_t *p)
{
if (n_grec < MAX_GREC) {
grec_t *gr = &grec[n_grec++];
gr->t = p->ts;
gr->v = p->vy;
gr->g = p->g_lp;
}
}
#else
#define dump_grec() do {} while(0)
#define record_grec(p) do {} while(0)
#endif
/* g as derivative of vy per second order approximation */
static void compute_g()
{
ts_val_t *p0, *p1, *p2;
p0 = &ts_vy[(ts_val_cur + (-2 + N_TS_VY)) % N_TS_VY];
p1 = &ts_vy[(ts_val_cur + (-1 + N_TS_VY)) % N_TS_VY];
p2 = &ts_vy[ts_val_cur];
double h10 = p1->ts - p0->ts;
double h20 = p2->ts - p0->ts;
double h21 = p2->ts - p1->ts;
p1 -> g = 1.0 + (-p0->vy * h21 / (h10 * h20) + p1->vy / h10 - p1->vy / h21 + p2->vy * h10 / (h21 * h20)) / G;
}
/* low pass filter for g */
static void compute_g_lp()
{
ts_val_t *p[G_LP_ORDER+1];
for (int i = 0; i < G_LP_ORDER + 1; i++) {
/* */
p[i] = &ts_vy[(ts_val_cur - G_LP_ORDER + i + N_TS_VY) % N_TS_VY];
}
/* low pass as integral over g considered as step function. With loop delay >= 0.25 and ~ 30 frames/sec
this filters below ~ 0.1 Hz */
double sum = 0.0;
for (int i = 0; i < G_LP_ORDER; i++)
sum += p[i]->g * (p[i+1]->ts - p[i]->ts);
p[G_LP_ORDER - 2]->g_lp = sum / (p[G_LP_ORDER]->ts - p[0]->ts);
}
/* to be called on initial ground contact */
static void record_touchdown()
{
if (NULL != landing_rwy) {
vect2_t pos_v = geo2fpp(GEO3_TO_GEO2(cur_pos), &landing_rwy->arpt->fpp);
/* check whether we are really on a runway */
if (point_in_poly(pos_v, landing_rwy->rwy_bbox)) {
const runway_end_t *near_end = &landing_rwy->ends[landing_rwy_end];
const runway_end_t *far_end = &landing_rwy->ends[(0 == landing_rwy_end ? 1 : 0)];
landing_thr_v = near_end->thr_v; /* save for later */
nose_wheel_td_dist = 0.0; /* 0.0 : landing_thr_v is initialized */
vect2_t center_line_v = vect2_sub(far_end->thr_v, near_end->thr_v);
vect2_t my_v = vect2_sub(pos_v, near_end->thr_v);
landing_dist = vect2_abs(my_v);
double cl_len = vect2_abs(center_line_v);
if (cl_len > 0.0) {
vect2_t cl_unit_v = vect2_scmul(center_line_v, 1/cl_len);
double dprod = vect2_dotprod(cl_unit_v, my_v);
vect2_t p_v = vect2_scmul(cl_unit_v, dprod);
vect2_t dev_v = vect2_sub(my_v, p_v);
/* get signed deviation, + -> right, - -> left */
landing_cl_delta = vect2_abs(dev_v);
double xprod_z = my_v.x * cl_unit_v.y - my_v.y * cl_unit_v.x;
/* by sign of cross product */
landing_cl_delta = xprod_z > 0 ? landing_cl_delta : -landing_cl_delta;
/* angle between cl and my heading */
landing_cl_angle = rel_hdg(near_end->hdg, XPLMGetDataf(hdg_dr));
}
} else {
landing_dist = -1.0; /* did not land on runway */
}
}
if (acf_ias_dr)
landing_ias = XPLMGetDataf(acf_ias_dr);
landing_theta = XPLMGetDataf(theta_dr);
landing_phi = XPLMGetDataf(phi_dr);
}
static float flight_loop_cb(float inElapsedSinceLastCall,
float inElapsedTimeSinceLastFlightLoop, int inCounter,
void *inRefcon)
{
if (! xgs_enabled)
return 2.0;
float timeFromStart = XPLMGetDataf(flight_time_dr);
float loop_delay = 0.025f;
cur_pos = GEO_POS3(XPLMGetDataf(lat_dr), XPLMGetDataf(lon_dr), XPLMGetDataf(elevation_dr));
float height = XPLMGetDataf(y_agl_dr);
vect3_t cur_pos_v = sph2ecef(cur_pos);
/* if we go 3 * supersonic it's a teleportation */
int teleportation = (vect3_dist(cur_pos_v, last_pos_v) / inElapsedSinceLastCall > 3 * 340.0);
if (teleportation)
logMsg("Teleportation detected");
/* independent of state */
if (0.0 < remaining_show_time) {
remaining_show_time -= inElapsedSinceLastCall;
if (teleportation || (0.0 >= remaining_show_time))
close_event_window();
}
/* nothing to do in replay mode */
if (! show_in_replay && XPLMGetDatai(in_replay_dr))
return 1.0;
int acf_state = get_current_state();
if (ACF_STATE_AIR == acf_state) {
if (height > 10.0) {
air_time += inElapsedSinceLastCall;
/* delayed per flight one time init */
if (!get_acf_dr_done) {
get_acf_dr_done = 1;
get_acf_dr();
}
}
/* low, alert mode */
if (height < 150) {
if (NULL == landing_rwy) {
if (arpt_last_reload + 10.0 < timeFromStart) {
arpt_last_reload = timeFromStart;
get_near_airports();
}
if (NULL != near_airports)
fix_landing_rwy();
}
} else if (height > 200) {
landing_rwy = NULL; /* may be a go around */
touchdown = 0;
landing_dist = -1.0;
}
if (height > 500)
loop_delay = 1.0f; /* we can be lazy */
}
/* ensure we have a real flight (and not teleportation or a bumpy takeoff) */
if (15.0 < air_time && height < 20) {
ts_val_cur = (ts_val_cur + 1) % N_TS_VY;
ts_val_t *tsv = &ts_vy[ts_val_cur];
tsv->ts = timeFromStart;
tsv->vy = XPLMGetDataf(vy_dr) * cos(XPLMGetDataf(theta_dr) * 0.0174533);
compute_g();
compute_g_lp();
if (0.0 < remaining_update_time) {
remaining_update_time -= inElapsedSinceLastCall;
/* we start with the last value prior to ground contact.
This is 2 back from current at touchdown */
if (1 <= loops_in_touchdown) {
const ts_val_t *tsv = &ts_vy[(ts_val_cur - 2 + N_TS_VY) % N_TS_VY];
last_vspeed = tsv->vy;
lastG = tsv->g_lp;
record_grec(tsv);
}
update_landing_result();
if (20 == loops_in_touchdown)
create_event_window();
if (0.0 > remaining_update_time) {
dump_grec();
if (log_enabled)
update_landing_log();
remaining_update_time = 0.0;
}
loops_in_touchdown++;
loop_delay = -1.0; /* highest resolution */
}
/* catch only first TD, i.e. no bouncing,
landing_rwy can be NULL here after a teleportation or when not landing on a rwy */
if (!touchdown && ACF_STATE_AIR == acf_last_state && ACF_STATE_GROUND == acf_state) {
touchdown = 1;
record_touchdown();
remaining_update_time = 10.0f;