zkv1000 / Nasal / lib.nas /
Newer Older
173 lines | 5.323kb
commit initial
Sébastien MARQUE authored on 2017-03-07
1
###
2
# Not Yet Implemented: just draw a warnig box during 3 seconds
3
var nyi = func (x) { gui.popupTip(x ~ ': not yet implemented', 3) }
4

            
5
###
6
# print message in terminal
7
var msg = func (str) { print('ZKV1000 >> ', str) }
8

            
9
###
10
# print message in popup
11
var msg_popup = func (str...) { gui.popupTip("ZKV1000\n" ~ str, 3) }
12

            
13
###
14
# just do nothing
15
var void = func { }
16

            
17
###
18
# returns decimal part arg
19
var frac = func (x) { return (x - int(x)) }
20

            
21
###
22
# rounds regarding proximity
23
# e.g.:
24
#   round(1540, 300) gives 1500
25
#   round(2370, 400) gives 2400
26
var round = func (x, m = 1) {
27
    var v = x / m;
28
    if (frac(v) >= 0.5) v += 1;
29
    return (int(v) * m);
30
}
31

            
32
###
33
# rounds regarless proximity
34
# e.g.:
35
#   round_bis(1540, 300) gives 1500
36
#   round_bis(2370, 400) gives 2000
37
var round_bis = func (x, m) {
38
    var r = round(x, m);
39
    if (r > x) r -= m;
40
    return r;
41
}
42

            
43
var alt = vs = vs_abs = ias = tas = pitch = roll = agl = stall = rpm = 0;
44
var getData = func {
45
    alt = math.abs(getprop('/instrumentation/altimeter/indicated-altitude-ft'));
46
    vs = getprop('/velocities/vertical-speed-fps') * 60; 
47
    ias = getprop('/instrumentation/airspeed-indicator/indicated-speed-kt');
48
    pitch = getprop('/orientation/pitch-deg');
49
    roll = getprop('/orientation/pitch-deg');
50
    agl = getprop('/position/altitude-agl-ft');
51
    stall = getprop('/sim/alarms/stall-warning');
52
    gear = getprop('/controls/gears/gear');
53
}
54

            
55
###
56
# returns DMS coordinates
57
# d is decimal longitude or latitude
58
# c should be one of E, W, S or N
59
# inspired from FG source: $FGSRC/src/Main/fg_props.cxx 
60
var DMS = func (d, c) {
61
    var deg = min = sec = 0.0;
62
    d = abs(d);
63
    deg = d + 0.05 / 3600;
64
    min = (deg - int(deg)) * 60;
65
    sec = (min - int(min)) * 60 - 0.049;
66
    return sprintf('%d %02d %04.1f %s', int(deg), int(min), abs(sec), c);
67
}
68

            
69
var timeUTC = func {
70
    setprop('/instrumentation/zkv1000/infos/time', sprintf('UTC %02i%02i%02i',
71
                getprop('/sim/time/utc/hour'),
72
                getprop('/sim/time/utc/minute'),
73
                getprop('/sim/time/utc/second')));
74
}
75

            
76
var timeLCL = func {
77
    var utc_hour = getprop('/sim/time/utc/hour') + (getprop('/sim/time/local-offset') / 3600);
78
    if (utc_hour > 23) utc_hour -= 24;
79
    if (utc_hour < 0)  utc_hour += 24;
80
    setprop('/instrumentation/zkv1000/infos/time', sprintf('LCL  %02i%02i%02i',
81
                utc_hour, 
82
                getprop('/sim/time/utc/minute'),
83
                getprop('/sim/time/utc/second')));
84
}
85

            
86
var timeRL = func {
87
    setprop('/instrumentation/zkv1000/infos/time', sprintf('RL   %02i%02i%02i',
88
                getprop('/sim/time/real/hour'),
89
                getprop('/sim/time/real/minute'),
90
                getprop('/sim/time/real/second')));
91
}
92

            
93
###
94
# Loads a fligtplan (route) from a XML file
95
# replaces existing route by the new one
96
var loadFPLfromFile = func (file) {
97
    afcs.getNode('route').removeChildren();
98
    fgcommand('loadxml', props.Node.new({
99
              'filename': file,
100
              'targetnode': afcs.getNode('route').getPath()})
101
            );
102
    FPLtoRouteManager();
103
    msg('route loaded from ' ~ file);
104
}
105

            
106
var wipeRoute = func {
107
    var command = props.globals.getNode('/instrumentation/gps/command');
108
    var v = getprop('/autopilot/route-manager/route/num');
109
    setprop('/autopilot/route-manager/active', 0);
110
    setprop('/instrumentation/gps/scratch/index', 0);
111
    while (v) {
112
        v -= 1;
113
        command.setValue('route-delete');
114
    }
115
}
116

            
117
var FPLtoRouteManager = func {
118
    wipeRoute();
119
    lockSearches();
120
    var scratch = props.globals.getNode('/instrumentation/gps/scratch');
121
    var command = scratch.getNode('../command');
122
    foreach (var waypoint; afcs.getNode('route').getChildren()) {
123
        scratch.getNode('index').setIntValue(-1);
124
        searchNearestNavaid(waypoint.getNode('type').getValue(), 1, waypoint.getPath());
125
        command.setValue('route-insert-after');
126
    }
127
    unlockSearches();
128
}
129

            
130
####
131
# Search nav facilities
132
var searchNearestNavaid = func (type, quantity = 1, path = nil) {
133
    getprop('/instrumentation/zkv1000/searches-locked') or return;
134
    scratch = props.globals.getNode('/instrumentation/gps/scratch/');
135
    scratch.getNode('max-results', 1).setIntValue(quantity);
136
    var lat = lon = -9999;
137
    if (path != nil) {
138
        if (props.globals.getNode(path) != nil) {
139
            lon = getprop(path ~ '/longitude-deg');
140
            lat = getprop(path ~ '/latitude-deg');
141
        }
142
    }
143
    scratch.getNode('longitude-deg', 1).setDoubleValue(lon);
144
    scratch.getNode('latitude-deg', 1).setDoubleValue(lat);
145
    scratch.getNode('type').setValue(type);
146
    setprop('/instrumentation/gps/command', 'nearest');
147
}
148

            
149
var lockSearches = func (v = 1) {
150
    props.globals.getNode('/instrumentation/zkv1000/searches-locked', 1).setBoolValue(v);
151
}
152

            
153
var unlockSearches = func {
154
    lockSearches(0);
155
}
156

            
157
#var dialog = func {
158
#    var d = gui.Widget.new();
159
#    d.set({'name': 'ZKV1000 text entry interface', 
160
#           'layout': 'vbox',
161
#           'default-padding' : 0});
162
#    d.addChild('text').
163
#}
164

            
165
var computeAltitudeDiff = void;
166
var computeAirspeedDiff = void;
167
var FLCcomputation = void;
168
var checkRollAcquisition = void;
169
var checkPitchAcquisition = void;
170
var checkTrafficProximity = void;
171
var manageLandingGears = void;
172
var checkAbnormalAttitude = void;
173
var applyFilter = void;