Debian Codegraph @

Aircraft/Generic/autochecklist.nas

raw
1 ################################################################################
2 #
3 # Automated Checklists
4 #
5 # Copyright (c) 2015, Richard Senior
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 # MA 02110-1301, USA.
21 #
22 ################################################################################
23 #
24 # This script runs the bindings in a sequence of checklists. Typically used
25 # to implement autostart or shutdown but can run any checklist sequence at any
26 # time. Checklist execution runs each binding where the condition is not yet
27 # satisfied and waits until the condition becomes true before moving on. If
28 # the condition does not become true within a timeout period, the checklist
29 # execution fails.
30 #
31 # Can also run in no wait mode where items are run in order without waiting
32 # for the previous condition to become true. Checklists never timeout or fail
33 # in no wait mode but some conditions may remain false.
34 #
35 # Typical usage:
36 #
37 # 1. Add this script to <Aircraft>-set.xml:
38 #
39 # <nasal>
40 # ... other scripts here
41 # <autochecklist>
42 # <file>Aircraft/Generic/autochecklist.nas</file>
43 # </autochecklist>
44 # </nasal>
45 #
46 # 2. In the <Aircraft>-set.xml, define a list of checklist indexes to run
47 # as a named sequence.
48 #
49 # For example, to run checklists with indexes 0 and 1 for startup and
50 # checklist 9 for shutdown:
51 #
52 # <checklists>
53 # <checklist include="Checklists/before-starting-engines.xml"/>
54 # <checklist include="Checklists/start-engines.xml"/>
55 # ... more checklists here
56 # <checklist include="Checklists/parking.xml"/>
57 # <startup>
58 # <index n="0">0</index> <!-- Before starting engines -->
59 # <index n="1">1</index> <!-- Start engines -->
60 # </startup>
61 # <shutdown>
62 # <index n="0">9</index> <!-- Parking -->
63 # </startup>
64 # </checklists>
65 #
66 # If you are using <group>-tags in your checklist definition, you need
67 # to add one <group-index> per sequence.
68 #
69 # 3. Define a menu item that calls the complete_checklists function with
70 # the name of the checklist sequence you would like to run.
71 #
72 # For example:
73 #
74 # <item>
75 # <label>Autostart</label>
76 # <binding>
77 # <command>nasal</command>
78 # <script>autochecklist.complete_checklists("startup");</script>
79 # </binding>
80 # </item>
81 #
82 # For a no wait checklist execution (which will never fail), pass zero (0)
83 # as the second argument:
84 #
85 # autochecklist.complete_checklists(sequence:"startup", wait:0);
86 #
87 # 4. Optionally, configure automated execution using the following properties.
88 # See comments within the autochecklist_init function for a description.
89 #
90 # <checklists>
91 # ...
92 # <auto>
93 # <completed-message>Checklists complete</completed-message>
94 # <startup-message>Running checklists, please wait ...</startup-message>
95 # <timeout-message>Some checks failed.</timeout-message>
96 # <timeout-sec>10</timeout-sec>
97 # <wait-sec>3</wait-sec>
98 # <auto>
99 # </checklists>
100 #
101 # Note that messages are not displayed for no wait execution.
102 #
103 ################################################################################
104
105 var checklists = nil;
106 var auto = nil;
107 var active = nil;
108 var expedited = nil;
109 var timeout_sec = nil;
110 var timeout_start = nil;
111 var wait_sec = nil;
112 var completed_message = nil;
113 var startup_message = nil;
114 var timeout_message = nil;
115
116 var autochecklist_init = func()
117 {
118 # Root property tree path for checklists
119 #
120 checklists = props.globals.getNode("sim/checklists");
121
122 # Root property tree path for auto checklists
123 #
124 auto = checklists.initNode("auto");
125
126 # Flag to indicate that checklists are being completed automatically.
127 # This can be used in Nasal bindings in the checklists to suppress a
128 # binding when automated execution is in progress, e.g. for checklist
129 # items that display dialogs.
130 #
131 active = auto.initNode("active", 0, "BOOL");
132
133 # Flag to indicate that checklist execution is expedited, i.e. there
134 # is no wait time between items. Typically indicates an in-air start. Note
135 # that the expedited flag does not imply automated checklists are active.
136 #
137 expedited = auto.initNode("expedited", 0, "BOOL");
138
139 # Timeout for completion of a checklist item. If the previous condition
140 # is still not satisifed after this timeout, the checklist fails.
141 #
142 timeout_sec = auto.initNode("timeout-sec", 10, "INT");
143 timeout_start = auto.initNode("timeout-start", 0.0, "DOUBLE");
144
145 # If the previous checklist item is not complete, the process waits
146 # this number of seconds before trying again.
147 #
148 wait_sec = auto.initNode("wait-sec", 3, "INT");
149
150 # Messages
151 #
152 completed_message = auto.initNode("completed-message",
153 "Checklists complete."
154 );
155
156 startup_message = auto.initNode("startup-message",
157 "Running checklists, please wait ..."
158 );
159
160 timeout_message = auto.initNode("timeout-message",
161 "Some checks failed. Try completing checklist manually."
162 );
163 }
164
165 ################################################################################
166
167 # Announces a message to the pilot
168 #
169 # @param message: the message to display
170 #
171 var announce = func(message)
172 {
173 setprop("sim/messages/copilot", message);
174 logprint(3, message);
175 }
176
177 # Resets the timestamp used for checking timeouts
178 #
179 var reset_timeout = func()
180 {
181 timeout_start.setValue(0.0);
182 }
183
184 # Return true if the timeout period has been exceeded
185 #
186 var timed_out = func()
187 {
188 var elapsed = getprop("sim/time/elapsed-sec") - timeout_start.getValue();
189 return timeout_start.getValue() and elapsed > timeout_sec.getValue();
190 }
191
192 # Waits for the completion of an item, setting a timestamp for timeouts
193 #
194 # @param node: the node containing the list of checklist indexes to run
195 # @param from: the checklist item node from which to start
196 #
197 var wait_for_completion = func(node, from)
198 {
199 var t = maketimer(wait_sec.getValue(), func {
200 complete(node, 1, from);
201 });
202 t.singleShot = 1;
203 t.start();
204
205 if (!timeout_start.getValue()) {
206 timeout_start.setValue(getprop("sim/time/elapsed-sec"));
207 }
208 }
209
210 # Automatically complete a set of checklists defined by a series of indexes
211 # listed under the node argument. Not intended to be called from outside this
212 # script.
213 #
214 # @param node: the node containing the list of checklist indexes to run
215 # @param wait: whether to wait for the preceding binding to complete
216 # @param from: the checklist item node from which to start, default nil
217 #
218 var complete = func(node, wait, from = nil)
219 {
220 var previous_condition = nil;
221 var skipping = from != nil;
222
223 if( node.getChild("group-index") != nil ){
224 var checklists_node = checklists.getChild("group", node.getChild("group-index").getIntValue());
225 } else {
226 var checklists_node = checklists;
227 }
228
229 foreach (var index; node.getChildren("index")) {
230 var checklist = checklists_node.getChild("checklist", index.getValue());
231 if( size( checklist.getChildren("page") ) > 0 ){
232 foreach( var page; checklist.getChildren("page") ){
233 foreach (var item; page.getChildren("item")) {
234 var condition = item.getNode("condition");
235 if (skipping) {
236 if (!item.equals(from)) {
237 previous_condition = condition;
238 continue;
239 }
240 skipping = 0;
241 }
242 if (wait) {
243 if (props.condition(previous_condition)) {
244 reset_timeout();
245 } else {
246 if (timed_out()) {
247 var title = checklist.getNode("title").getValue();
248 announce(title~": "~timeout_message.getValue());
249 } else {
250 wait_for_completion(node: node, from: item);
251 }
252 return;
253 }
254 }
255 if (!props.condition(condition)) {
256 foreach (var binding; item.getChildren("binding")) {
257 active.setValue(1);
258 props.runBinding(binding);
259 active.setValue(0);
260 }
261 }
262 previous_condition = condition;
263 }
264
265 }
266
267 } else {
268 foreach (var item; checklist.getChildren("item")) {
269 var condition = item.getNode("condition");
270 if (skipping) {
271 if (!item.equals(from)) {
272 previous_condition = condition;
273 continue;
274 }
275 skipping = 0;
276 }
277 if (wait) {
278 if (props.condition(previous_condition)) {
279 reset_timeout();
280 } else {
281 if (timed_out()) {
282 var title = checklist.getNode("title").getValue();
283 announce(title~": "~timeout_message.getValue());
284 } else {
285 wait_for_completion(node: node, from: item);
286 }
287 return;
288 }
289 }
290 if (!props.condition(condition)) {
291 foreach (var binding; item.getChildren("binding")) {
292 active.setValue(1);
293 props.runBinding(binding);
294 active.setValue(0);
295 }
296 }
297 previous_condition = condition;
298 }
299 }
300
301 }
302 if (wait) {
303 announce(completed_message.getValue());
304 }
305 }
306
307 ################################################################################
308
309 # Complete a checklist sequence, typically called from a Nasal binding in a
310 # menu, e.g. Autostart, but could be assigned to a controller button or even
311 # called from a listener.
312 #
313 # @param sequence: the name of the checklist sequence to run
314 # @param wait: whether to wait for the preceding binding, default 1 (true)
315 #
316 var complete_checklists = func(sequence, wait = 1)
317 {
318 var node = checklists.getNode(sequence);
319 if (node != nil) {
320 if (wait) {
321 announce(startup_message.getValue());
322 }
323 expedited.setValue(!wait);
324 complete(node, wait);
325 } else {
326 announce("Could not find checklist sequence called '"~sequence~"'");
327 }
328 }
329
330 setlistener("/sim/signals/fdm-initialized", func() {
331 autochecklist_init();
332 });
333