zkv1000 / zkv1000.nas /
Sébastien MARQUE add track display
d278ebe 3 years ago
1 contributor
301 lines | 11.639kb
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# Nasal files to be loaded at start
# the order could be important as some files need other one to be loaded first
files_to_load = [
    'lib.nas',     # some useful functions, should stay first loaded
    'radios.nas',  # all about radios COMM, NAV, XPDR
    'knobs.nas',   # handles knobs
    'buttons.nas', # handles knobs and buttons
    'softkeys.nas',# handles softkeys and menus items
    'maps/route.nas',
    'maps/navaids.nas',
    'maps/tiles.nas',
    'maps/tcas.nas',
    'maps/topo.nas',
    'maps/track.nas',
    'map.nas',     # moves the maps
    'display.nas',
    'menu.nas',    # manage windows
    'core.nas',    # the scheduler and switch on button
    'afcs.nas',    # Automatic Flight Control System
    'annunciations.nas',  # in flight tests
];
#    'routes.nas',  # manages flightplans and routes
#    'display.nas', # display messages and popups
#    'infos.nas',   # get informations from environment
#    'mud.nas',     # displays simple embedded GUI (Multi-Use Display)

var flightdeck = {};

var autopilot = {};

var annunciations = {};

var units = {
    speed : {
        from_kt  : 1,
        from_kmh : MPS2KT * 3.6,
    },
    altitude : {
        from_ft : 1,
        from_m  : M2FT,
    },
    vspeed : {
        from_fpm : 1,
        from_mpm : M2FT,
    },
    distance : {
        from_m : M2NM / 1000,
        from_nm : 1,
    },
    temperature : {
        from_C : func (c) return c,
        from_F : func (f) return (f -32) / 1.8,
    },
    volume: {
        from_l: 1,
        from_gal: 3.78541178,
        from_m3: 1000,
    },
};

var data = { # set of data common to all devices
    roll : 0,
    pitch : 0,
    vsi : 0,
    ias : 0,
    alt : 0,
    hdg : 0,
    trk : 0,
    wow : 1,
    lat : 0,
    lon : 0,
    aoa : 0,
    time : '23:59:59',
    fpSize : 0,
    tcas: [],
    tcas_level: 0,
    Vspeeds : {},
    settings: {
        units: {
            pressure: 'inhg',
            altitude: 'ft',
            speed: 'knots',
            distance: 'nm',
            vertical: 'fpm',
        },
        time: {
            label: 'GMT',
            actual: '  GMT >',
        },
    },
    timers : {
        '20Hz': maketimer (
            0.05,
            func {
                data.roll = getprop('/orientation/roll-deg');
                data.pitch = getprop('orientation/pitch-deg');
                data.vsi = getprop('/instrumentation/vertical-speed-indicator/indicated-speed-fpm') * units.vspeed.from_fpm;
                data.ias = getprop('/velocities/airspeed-kt') * units.speed.from_kt;
                data.alt = getprop('/instrumentation/altimeter/indicated-altitude-ft') * units.altitude.from_ft;
                data.hdg = getprop('/orientation/heading-deg');
                data.trk = data.wow ? data.hdg : getprop('/orientation/track-deg');
                data.aoa = getprop('/orientation/alpha-deg');
            }
        ),
        '1Hz': maketimer (
            1,
            func {
                data.wow = getprop('/gear/gear/wow');
                data.lat = getprop('/position/latitude-deg');
                data.lon = getprop('/position/longitude-deg');
            }
        ),
    },
    listeners : {},
};

var zkv = cdi = radios = alerts = infos = cursors = afcs = eis = misc = nil;

var init_props = func {
    zkv = props.globals.getNode('/instrumentation/zkv1000',1);
    foreach (var d; zkv.getChildren())
        if (d.getNode('status') != nil)
            flightdeck[d.getName()] = nil;
    zkv.getNode('emission',1).setDoubleValue(0.5);
    zkv.getNode('body-emission',1).setDoubleValue(0.0);
    zkv.getNode('body-texture',1).setValue('');
    zkv.getNode('display-brightness-norm',1).setDoubleValue(0.5);
    zkv.getNode('lightmap',1).setIntValue(0);
    if (zkv.getNode('size-factor').getValue() == nil)
        zkv.getNode('size-factor',1).setDoubleValue(1.0);
    if (zkv.getValue('flightplans') != nil and io.stat(zkv.getValue('flightplans')) == "dir")
        data.flightplans = zkv.getValue('flightplans');
    else
        data.flightplans = getprop('/sim/fg-home') ~ '/Export';

    radios = zkv.getNode('radios', 1);
    radios.getNode('nav1-selected',1).setIntValue(0);
    radios.getNode('nav1-standby',1).setIntValue(0);
    radios.getNode('nav2-selected',1).setIntValue(0);
    radios.getNode('nav2-standby',1).setIntValue(0);
    radios.getNode('nav-tune',1).setIntValue(0);
    radios.getNode('nav-freq-mhz',1).alias('/instrumentation/nav/frequencies/standby-mhz');
    radios.getNode('comm1-selected',1).setIntValue(1);
    radios.getNode('comm2-selected',1).setIntValue(0);
    radios.getNode('comm-tune',1).setIntValue(0);
    radios.getNode('comm-freq-mhz',1).alias('/instrumentation/comm/frequencies/standby-mhz');
    radios.getNode('xpdr-mode',1).setValue('GND');
    radios.getNode('brg1-source',1).setValue('OFF');
    radios.getNode('brg2-source',1).setValue('OFF');

    cdi = zkv.getNode('cdi', 1);
    cdi.getNode('source', 1).setValue('OFF');
    cdi.getNode('no-flag', 1).setBoolValue(0);
    cdi.getNode('FROM-flag', 1).alias('no-flag');
    cdi.getNode('TO-flag', 1).alias('no-flag');
    cdi.getNode('course', 1);
    cdi.getNode('course-deflection', 1);
    cdi.getNode('radial', 1);
    cdi.getNode('in-range', 1);

    alerts = zkv.getNode('alerts',1);
    alerts.getNode('traffic-proximity',1).setIntValue(0);
    alerts.getNode('marker-beacon', 1).setIntValue(0);
    alerts.getNode('warning', 1).setBoolValue(0);
    alerts.getNode('alert', 1).setBoolValue(0);
    alerts.getNode('message', 1).setValue('');
    foreach (var v; alerts.getChildren())
        if (string.match(v.getName(), 'V[a-z0-9]*'))
            data.Vspeeds[v.getName()] = v.getValue();

    var aoa = alerts.getValue('stall-aoa');
    data['stall-aoa'] = (aoa == nil or aoa == 0) ? 9999 : aoa;
    aoa = alerts.getValue('approach-aoa');
    if (aoa != nil)
        data['approach-aoa'] = aoa;

    afcs = zkv.getNode('afcs',1);
    afcs.getNode('fd-bars-visible',1).setBoolValue(0);
    afcs.getNode('alt-bug-visible',1).setBoolValue(0);
    afcs.getNode('heading-bug-deg',1).setDoubleValue(int(getprop('/orientation/heading-magnetic-deg')));
    afcs.getNode('target-pitch-deg',1).setDoubleValue(0.0);
    afcs.getNode('selected-alt-ft',1).setIntValue(2000);
    afcs.getNode('selected-alt-ft-diff',1).setDoubleValue(0.0);
    afcs.getNode('selected-ias-kt-diff',1).setDoubleValue(0.0);
    afcs.getNode('vertical-speed-fpm',1).setDoubleValue(0.0);
    afcs.getNode('roll-armed', 1).setBoolValue(0);
    afcs.getNode('pitch-armed', 1).setBoolValue(0);
    afcs.getNode('roll-armed-mode-text',1).setValue('');
    afcs.getNode('roll-active-mode-text',1).setValue('');
    afcs.getNode('roll-armed-mode',1).setIntValue(0);
    afcs.getNode('roll-active-mode',1).setIntValue(0);
    afcs.getNode('roll-active-mode-blink',1).setBoolValue(0);
    afcs.getNode('pit-armed-mode-text',1).setValue('');
    afcs.getNode('pit-active-mode-text',1).setValue('');
    afcs.getNode('pit-armed-mode',1).setIntValue(0);
    afcs.getNode('pit-active-mode',1).setIntValue(0);
    afcs.getNode('pit-active-mode-blink',1).setBoolValue(0);
    afcs.getNode('route',1);

    data.flightplans = getprop('/sim/fg-home') ~ '/Export';

    misc = zkv.getNode('misc',1);
    misc.getNode('alt-setting-inhg',1).setDoubleValue(getprop('/instrumentation/altimeter/setting-inhg'));

    eis = zkv.getNode('eis',1);
    eis.getNode('fuel-qty-at-start', 1).setValue(
        getprop('/consumables/fuel/tank/level-gal_us')
      + getprop('/consumables/fuel/tank/level-gal_us'));

    var tiles_defaults = {
        # see https://www.wikimedia.org/wiki/Maps
        server: 'maps.wikimedia.org',
        type:   'osm-intl',
        apikey: '',
        format: 'png',
        template: 'https://{server}/{type}/{z}/{x}/{y}.{format}{apikey}',
    };
    foreach (var v; keys(tiles_defaults)) {
        var val = getprop('/sim/online-tiles-' ~ v);
        data['tiles-' ~ v] = val != nil ? val : tiles_defaults[v];
    }

    props.globals.getNode('/instrumentation/transponder/id-code',1).setIntValue(1200);
    props.globals.getNode('/instrumentation/transponder/serviceable',1).setBoolValue(1);
    props.globals.getNode('/autopilot/settings/heading-bug-deg', 1).alias(afcs.getNode('heading-bug-deg').getPath());
    props.globals.getNode('/autopilot/settings/target-alt-ft',1).alias(afcs.getNode('selected-alt-ft').getPath());
    props.globals.getNode('/autopilot/settings/target-speed-kt',1).setDoubleValue(0.0);
    props.globals.getNode('/autopilot/settings/vertical-speed-fpm',1).setDoubleValue(0.0);
    props.globals.getNode('/autopilot/internal/target-pitch-deg',1).setDoubleValue(0.0);
    props.globals.getNode('/autopilot/internal/flc-altitude-pitch-deg',1).setDoubleValue(0.0);
    props.globals.getNode('/autopilot/internal/flc-airspeed-pitch-deg',1).setDoubleValue(0.0);
    props.globals.getNode('/autopilot/internal/target-roll-deg',1).setDoubleValue(0.0);
    props.globals.getNode('/autopilot/locks/pitch',1).setValue('');
    props.globals.getNode('/autopilot/locks/roll',1).setValue('');
    props.globals.getNode('/autopilot/locks/passive-mode', 1).setIntValue(1);

    data.zkv1000_reldir = io.dirname(getprop('/nasal/zkv1000/file'));
    data.zkv1000_dir = string.normpath(
            io.dirname(getprop('/sim/aircraft-dir'))
            ~ '/'
            ~ string.replace(data.zkv1000_reldir, split('/', data.zkv1000_reldir)[0], '')
        ) ~ '/';
}

var load_nasal = func {
    var nasal_dir = data.zkv1000_dir ~ 'Nasal/';
    for (var i = 0; i < size(files_to_load); i += 1)
        io.load_nasal(nasal_dir ~ files_to_load[i], 'zkv1000');
    files_to_load = nil;
}

var load_multikey = func {
    fgcommand('loadxml', props.Node.new({
        'filename': data.zkv1000_dir ~ 'Systems/multikey.xml',
        'targetnode': '/input/keyboard/'
    }));
    multikey.init();
}

var load_settings = func {
    var settings_file = getprop('/sim/fg-home') ~ '/aircraft-data/zkv1000.xml';
    if (io.stat(settings_file) != nil) {
        fgcommand('loadxml', props.Node.new({ filename: settings_file, targetnode: zkv.getNode('save', 1).getPath() }));
        var xmlsettings = zkv.getNode('save/' ~ getprop('/sim/aircraft'));
        if (xmlsettings != nil) {
            foreach (var domain; keys(units))
                foreach (var from; keys(units[domain])) {
                    if (xmlsettings.getNode(domain) != nil) {
                        var unit_value = xmlsettings.getNode(domain).getValue(from);
                        if (unit_value != nil) {
                            if (typeof(units[domain][from]) == 'scalar')
                                units[domain][from] = unit_value;
                            if (typeof(units[domain][from]) == 'func') {
                                units[domain][from] = compile(unit_value);
                                units[domain][from](0);
                            }
                        }
                    }
                }
            data.settings.units.pressure = xmlsettings.getNode('pressure').getValue();
        }
        zkv.getNode('save').remove();
    }
    foreach (var V; keys(data.Vspeeds))
        data.Vspeeds[V] *= units.speed.from_kt;
}

var zkv1000_init = func {
    removelistener(init);
    init_props();
    load_settings();
    load_multikey();
    load_nasal();
    msg('loaded');
    if (zkv.getValue('auto-power')) {
        var prop = zkv.getNode('serviceable',1).getPath();
        data.listeners[prop] = setlistener(prop, zkv1000.powerOn, 0, 0);
    }
}

var init = setlistener('/sim/signals/fdm-initialized', zkv1000_init, 0, 0);