Debian Codegraph @

Aircraft/c172p/Nasal/c172p.nas

raw
1 ##########################################
2 # Autostart
3 ##########################################
4
5 var autostart = func (msg=1) {
6 if (getprop("/engines/active-engine/running")) {
7 if (msg)
8 gui.popupTip("Engine already running", 5);
9 return;
10 }
11
12 # Reset battery charge and circuit breakers
13 electrical.reset_battery_and_circuit_breakers();
14
15 #Guarantee repairing flag is reset
16 setprop("/fdm/jsbsim/damage/repairing", 0);
17
18 # Setting levers and switches for startup
19 setprop("/controls/switches/magnetos", 3);
20 setprop("/controls/engines/engine[0]/throttle", 0.2);
21 setprop("/controls/engines/engine[1]/throttle", 0.2);
22
23 var auto_mixture = getprop("/fdm/jsbsim/engine/auto-mixture");
24 setprop("/controls/engines/current-engine/mixture", auto_mixture);
25
26 setprop("/controls/flight/elevator-trim", 0.0);
27 setprop("/controls/flight/rudder-trim", 0.02);
28 setprop("/controls/switches/master-bat", 1);
29 setprop("/controls/switches/master-alt", 1);
30 setprop("/controls/switches/master-avionics", 1);
31 if (getprop("controls/panel/glass")) {
32 setprop("/controls/switches/master-avionics2", 1);
33 }
34
35 # Setting lights
36 setprop("/controls/lighting/nav-lights", 1);
37 setprop("/controls/lighting/strobe", 1);
38 setprop("/controls/lighting/beacon", 1);
39
40 # Setting instrument lights if needed
41 var light_level = 1-getprop("/rendering/scene/diffuse/red");
42 if (light_level > .6) {
43 if (!getprop("/controls/panel/glass")){
44 if (getprop("/controls/lighting/instruments-norm") == 0) {
45 if (light_level > .8) light_level = .8;
46 setprop("/controls/lighting/instruments-norm", light_level);
47 }
48 setprop("/controls/switches/dome-red", 1);
49 }
50 if (getprop("/controls/panel/glass")) {
51 if (getprop("/controls/lighting/swcb-norm") == 0) {
52 setprop("/controls/lighting/swcb-norm", .55);
53 }
54 if (getprop("/controls/lighting/avionics-norm") == 0) {
55 setprop("/controls/lighting/avionics-norm", .6);
56 }
57 if (getprop("/controls/lighting/stby-norm") == 0) {
58 setprop("/controls/lighting/stby-norm", .8);
59 }
60 }
61 } else {
62 if (getprop("/controls/panel/glass")) {
63 if (getprop("/controls/lighting/swcb-norm") == 0) {
64 setprop("/controls/lighting/swcb-norm", .35);
65 }
66 if (getprop("/controls/lighting/avionics-norm") == 0) {
67 setprop("/controls/lighting/avionics-norm", .35);
68 }
69 if (getprop("/controls/lighting/stby-norm") == 0) {
70 setprop("/controls/lighting/stby-norm", .75);
71 }
72 }
73 }
74 if (getprop("/controls/panel/glass")) {
75 setprop("/controls/switches/stby-batt", 2);
76 }
77 # Setting amphibious landing gear if needed
78 if (getprop("/fdm/jsbsim/bushkit")==4){
79 if (getprop("/fdm/jsbsim/hydro/active-norm")) {
80 setprop("controls/gear/gear-down-command", 0);
81 setprop("/fdm/jsbsim/gear/gear-pos-norm", 0);
82 } else {
83 setprop("controls/gear/gear-down-command", 1);
84 setprop("/fdm/jsbsim/gear/gear-pos-norm", 1);
85 }
86 }
87
88 # Setting flaps to 0
89 setprop("/controls/flight/flaps", 0.0);
90
91 # Set the altimeter
92 var pressure_sea_level = getprop("/environment/pressure-sea-level-inhg");
93 setprop("/instrumentation/altimeter/setting-inhg", pressure_sea_level);
94
95 # Insta-Spin up gyros
96 setprop("/instrumentation/heading-indicator/spin", 1.0);
97 setprop("/instrumentation/turn-indicator/spin", 1.0);
98 setprop("/instrumentation/attitude-indicator/spin", 1.0);
99
100 # Set heading offset
101 # Note: this needs a correctly spun up gyro to work.
102 var magnetic_variation = getprop("/environment/magnetic-variation-deg");
103 magnetic_variation = sprintf("%.2f", magnetic_variation);
104 setprop("/instrumentation/heading-indicator/align-deg", -magnetic_variation);
105 setprop("/instrumentation/heading-indicator/error-deg", 0);
106 setprop("/instrumentation/heading-indicator/offset-deg", 0);
107 print("Heading Indicator calibrated to: " ~ magnetic_variation ~ " magVar");
108
109 # Pre-flight inspection
110 setprop("/sim/model/c172p/cockpit/control-lock-placed", 0);
111 setprop("/controls/gear/brake-parking", 0);
112 setprop("/sim/model/c172p/securing/chock", 0);
113 setprop("/sim/model/c172p/securing/cowl-plugs-visible", 0);
114 setprop("/sim/model/c172p/securing/pitot-cover-visible", 0);
115 setprop("/sim/model/c172p/securing/tiedownL-visible", 0);
116 setprop("/sim/model/c172p/securing/tiedownR-visible", 0);
117 setprop("/sim/model/c172p/securing/tiedownT-visible", 0);
118
119 # Setting max oil level
120 var oil_enabled = getprop("/engines/active-engine/oil_consumption_allowed");
121 var oil_level = getprop("/engines/active-engine/oil-level");
122
123 if (oil_enabled and oil_level < 5.0) {
124 if (getprop("/controls/engines/active-engine") == 0) {
125 setprop("/engines/active-engine/oil-level", 7.0);
126 }
127 else {
128 setprop("/engines/active-engine/oil-level", 8.0);
129 };
130 };
131
132 # removing any ice from the carburetor
133 setprop("/engines/active-engine/carb_ice", 0.0);
134 setprop("/engines/active-engine/carb_icing_rate", 0.0);
135 setprop("/engines/active-engine/volumetric-efficiency-factor", 0.85);
136
137 # Removing any contamination from water
138 setprop("/consumables/fuel/tank[0]/water-contamination", 0.0);
139 setprop("/consumables/fuel/tank[1]/water-contamination", 0.0);
140 setprop("/consumables/fuel/tank[0]/sample-water-contamination", 0.0);
141 setprop("/consumables/fuel/tank[1]/sample-water-contamination", 0.0);
142
143 # set fuel configuration
144 set_fuel();
145
146 setprop("/controls/engines/engine[0]/primer-lever", 0);
147 setprop("/controls/engines/engine/primer", 3);
148
149 # All set, starting engine
150 setprop("/controls/switches/starter", 1);
151 setprop("/engines/active-engine/auto-start", 1);
152
153 var engine_running_check_delay = 5.0;
154 settimer(func {
155 if (!getprop("/engines/active-engine/running")) {
156 gui.popupTip("The autostart failed to start the engine. You must lean the mixture and start the engine manually.", 5);
157 }
158 setprop("/controls/switches/starter", 0);
159 setprop("/engines/active-engine/auto-start", 0);
160 }, engine_running_check_delay);
161
162 };
163
164 ##########################################
165 # Brakes
166 ##########################################
167
168 controls.applyBrakes = func (v, which = 0) {
169 if (which <= 0 and !getprop("/fdm/jsbsim/gear/unit[1]/broken")) {
170 interpolate("/controls/gear/brake-left", v, controls.fullBrakeTime);
171 }
172 if (which >= 0 and !getprop("/fdm/jsbsim/gear/unit[2]/broken")) {
173 interpolate("/controls/gear/brake-right", v, controls.fullBrakeTime);
174 }
175 };
176
177 ##########################################
178 # Set Fuel Configuration
179 ##########################################
180 var set_fuel = func {
181 # Checking for minimal fuel level
182 var fuel_level_left_default = getprop("/consumables/fuel/tank[0]/level-norm");
183 var fuel_level_right_default = getprop("/consumables/fuel/tank[1]/level-norm");
184 var fuel_level_left_integral = getprop("/consumables/fuel/tank[2]/level-norm");
185 var fuel_level_right_integral = getprop("/consumables/fuel/tank[3]/level-norm");
186 # Check which tanks are being used
187 var integral_tanks = getprop("/fdm/jsbsim/fuel/tank");
188 if (integral_tanks) {
189 if (fuel_level_left_integral < 0.25)
190 setprop("/consumables/fuel/tank[2]/level-norm", 0.25);
191 if (fuel_level_right_integral < 0.25)
192 setprop("/consumables/fuel/tank[3]/level-norm", 0.25);
193 setprop("/consumables/fuel/tank[2]/selected", 1);
194 setprop("/consumables/fuel/tank[3]/selected", 1);
195 setprop("/consumables/fuel/tank[0]/selected", 0);
196 setprop("/consumables/fuel/tank[1]/selected", 0);
197 } else {
198 if (fuel_level_left_default < 0.25)
199 setprop("/consumables/fuel/tank[0]/level-norm", 0.25);
200 if (fuel_level_right_default < 0.25)
201 setprop("/consumables/fuel/tank[1]/level-norm", 0.25);
202 setprop("/consumables/fuel/tank[0]/selected", 1);
203 setprop("/consumables/fuel/tank[1]/selected", 1);
204 setprop("/consumables/fuel/tank[2]/selected", 0);
205 setprop("/consumables/fuel/tank[3]/selected", 0);
206 }
207 setprop("sim/model/open-pfuel-cap", 0);
208 setprop("sim/model/open-sfuel-cap", 0);
209 setprop("sim/model/open-pfuel-sump", 0);
210 setprop("sim/model/open-sfuel-sump", 0);
211 fgcommand("dialog-close", props.Node.new({"dialog-name": "c172p-left-fuel-dialog"}));
212 fgcommand("dialog-close", props.Node.new({"dialog-name": "c172p-right-fuel-dialog"}));
213 fgcommand("dialog-close", props.Node.new({"dialog-name": "c172p-fuel-both-tanks-dialog"}));
214 fgcommand("dialog-close", props.Node.new({"dialog-name": "c172p-left-fuel-sample-dialog"}));
215 fgcommand("dialog-close", props.Node.new({"dialog-name": "c172p-right-fuel-sample-dialog"}));
216 };
217
218 ##########################################
219 # Fuel Contamination
220 ##########################################
221 var fuel_contamination = func {
222 var chance = rand();
223
224 # Chance of contamination is 1 %
225 if (getprop("/consumables/fuel/contamination_allowed") and chance < 0.01) {
226 # Quantity of water is much more likely to be small than large, since
227 # it's given by x^6 (76 % of the time it will be lower than 0.2)
228 var water = math.pow(rand(), 6);
229
230 setprop("/consumables/fuel/tank[0]/water-contamination", water);
231
232 # level of water in the right tank will be the same as in the left tank +- 0.1
233 water = water + 0.2 * (rand() - 0.5);
234 water = std.max(0.0, std.min(water, 1.0));
235 setprop("/consumables/fuel/tank[1]/water-contamination", water);
236 }
237 else {
238 setprop("/consumables/fuel/tank[0]/water-contamination", 0.0);
239 setprop("/consumables/fuel/tank[1]/water-contamination", 0.0);
240 };
241 };
242
243 ##########################################
244 # Take Fuel Sample
245 ##########################################
246 var take_fuel_sample = func(index) {
247 var fuel = getprop("/consumables/fuel/tank", index, "level-gal_us");
248 var water = getprop("/consumables/fuel/tank", index, "water-contamination");
249
250 # Remove 50 ml of fuel
251 setprop("/consumables/fuel/tank", index, "level-gal_us", fuel - 0.0132086);
252
253 # Remove a bit of water if contaminated
254 if (water > 0.0) {
255 var sample_water = std.min(0.2, water);
256 water = water - sample_water;
257 setprop("/consumables/fuel/tank", index, "water-contamination", water);
258 setprop("/consumables/fuel/tank", index, "sample-water-contamination", sample_water);
259 };
260 };
261
262 ##########################################
263 # Return Fuel Sample
264 ##########################################
265 var return_fuel_sample = func(index) {
266 var fuel = getprop("/consumables/fuel/tank", index, "level-gal_us");
267 var water = getprop("/consumables/fuel/tank", index, "water-contamination");
268 var sample_water = getprop("/consumables/fuel/tank", index, "sample-water-contamination");
269
270 # Add back the 50 ml of fuel
271 setprop("/consumables/fuel/tank", index, "level-gal_us", fuel + 0.0132086);
272
273 # Add back the (contaminated) water
274 if (sample_water > 0.0) {
275 water = water + sample_water;
276 setprop("/consumables/fuel/tank", index, "water-contamination", water);
277 setprop("/consumables/fuel/tank", index, "sample-water-contamination", 0.0);
278 };
279 };
280
281 ##########################################
282 # Preflight control surface check: left aileron
283 ##########################################
284 var control_surface_check_left_aileron = func {
285 var auto_coordination = getprop("/controls/flight/auto-coordination");
286 setprop("/controls/flight/auto-coordination", 0);
287 interpolate("/controls/flight/aileron", 1.0, 0.5, -1.0, 1.0, 0.0, 0.5);
288 settimer(func(){
289 setprop("/controls/flight/auto-coordination", auto_coordination);
290 }, 2.0);
291 };
292
293 ##########################################
294 # Preflight control surface check: right aileron
295 ##########################################
296 var control_surface_check_right_aileron = func {
297 var auto_coordination = getprop("/controls/flight/auto-coordination");
298 setprop("/controls/flight/auto-coordination", 0);
299 interpolate("/controls/flight/aileron", -1.0, 0.5, 1.0, 1.0, 0.0, 0.5);
300 settimer(func(){
301 setprop("/controls/flight/auto-coordination", auto_coordination);
302 }, 2.0);
303 };
304
305 ##########################################
306 # Preflight control surface check: elevator
307 ##########################################
308 var control_surface_check_elevator = func {
309 interpolate("/controls/flight/elevator", 1.0, 0.8, -1.0, 1.6, 0.0, 0.8);
310 };
311
312 ##########################################
313 # Preflight control surface check: rudder
314 ##########################################
315 var control_surface_check_rudder = func {
316 interpolate("/controls/flight/rudder", -1.0, 0.8, 1.0, 1.6, 0.0, 0.8);
317 };
318
319 ##########################################
320 # Switches Save State
321 ##########################################
322 var switches_save_state = func {
323 if (!getprop("/instrumentation/save-switches-state")) {
324 setprop("/controls/engines/engine[0]/primer", 0);
325 setprop("/controls/engines/engine[0]/primer-lever", 0);
326 setprop("/controls/engines/engine[0]/use-primer", 0);
327 setprop("/controls/engines/engine[0]/throttle", 0.0);
328 setprop("/controls/engines/engine[1]/throttle", 0.0);
329 setprop("/controls/engines/current-engine/mixture", 0.0);
330 #setprop("/controls/circuit-breakers/aircond", 1);
331 setprop("/controls/circuit-breakers/autopilot", 1);
332 setprop("/controls/circuit-breakers/bcnlt", 1);
333 setprop("/controls/circuit-breakers/flaps", 1);
334 setprop("/controls/circuit-breakers/instr", 1);
335 setprop("/controls/circuit-breakers/intlt", 1);
336 setprop("/controls/circuit-breakers/landing", 1);
337 setprop("/controls/circuit-breakers/master", 1);
338 setprop("/controls/circuit-breakers/navlt", 1);
339 setprop("/controls/circuit-breakers/pitot-heat", 1);
340 setprop("/controls/circuit-breakers/radio1", 1);
341 setprop("/controls/circuit-breakers/radio2", 1);
342 setprop("/controls/circuit-breakers/radio3", 1);
343 setprop("/controls/circuit-breakers/radio4", 1);
344 setprop("/controls/circuit-breakers/radio5", 1);
345 setprop("/controls/circuit-breakers/strobe", 1);
346 if (getprop("sim/model/variant") == 4) {
347 setprop("controls/circuit-breakers/gear-advisory", 1);
348 setprop("controls/circuit-breakers/hydraulic-pump", 1);
349 setprop("controls/circuit-breakers/gear-select", 1);
350 }
351 setprop("/controls/circuit-breakers/turn-coordinator", 1);
352 setprop("/controls/switches/master-avionics", 0);
353 setprop("/controls/switches/starter", 0);
354 setprop("/controls/switches/master-alt", 0);
355 setprop("/controls/switches/master-bat", 0);
356 setprop("/controls/switches/magnetos", 0);
357 setprop("/controls/switches/dome-white", 0);
358 setprop("/controls/switches/dome-red", 0);
359 setprop("/controls/lighting/nav-lights", 0);
360 setprop("/controls/lighting/beacon", 0);
361 setprop("/controls/lighting/strobe", 0);
362 setprop("/controls/lighting/taxi-light", 0);
363 setprop("/controls/lighting/landing-lights", 0);
364 setprop("/controls/lighting/instruments-norm", 0.0);
365 setprop("/controls/lighting/radio-norm", 0.0);
366 setprop("/controls/lighting/dome-white-norm", 1.0);
367 setprop("/controls/lighting/gps-norm", 0.0);
368 setprop("/controls/lighting/gearled", 0);
369 setprop("/controls/gear/water-rudder", 0);
370 setprop("/controls/gear/water-rudder-down", 0);
371 setprop("/controls/gear/brake-parking", 1);
372 setprop("/controls/flight/flaps", 0.0);
373 setprop("/surface-positions/flap-pos-norm", 0.0);
374 setprop("/controls/flight/elevator-trim", 0.0);
375 setprop("/controls/anti-ice/engine[0]/carb-heat", 0);
376 setprop("/controls/anti-ice/engine[1]/carb-heat", 0);
377 setprop("/controls/anti-ice/pitot-heat", 0);
378 setprop("/environment/aircraft-effects/cabin-heat-set", 0.0);
379 setprop("/environment/aircraft-effects/cabin-air-set", 0.0);
380 setprop("/consumables/fuel/tank[0]/level-norm", 0.0);
381 setprop("/consumables/fuel/tank[1]/level-norm", 0.0);
382 setprop("/consumables/fuel/tank[2]/level-norm", 0.0);
383 setprop("/consumables/fuel/tank[3]/level-norm", 0.0);
384
385 if (getprop("/sim/model/c172p/ruddertrim-visible"))
386 setprop("/controls/flight/rudder-trim", 0);
387
388 if (getprop("controls/panel/glass")) {
389 electrical.reset_battery_and_circuit_breakers();
390 setprop("/controls/switches/master-avionics", 0);
391 setprop("/controls/switches/master-avionics2", 0);
392 }
393
394 };
395 };
396
397 ##########################################
398 # Click Sounds
399 ##########################################
400
401 var click = func (name, timeout=0.1, delay=0) {
402 var sound_prop = "/sim/model/c172p/sound/click-" ~ name;
403
404 settimer(func {
405 # Play the sound
406 setprop(sound_prop, 1);
407
408 # Reset the property after 0.2 seconds so that the sound can be
409 # played again.
410 settimer(func {
411 setprop(sound_prop, 0);
412 }, timeout);
413 }, delay);
414 };
415
416 ##########################################
417 # Thunder Sound
418 ##########################################
419
420 var speed_of_sound = func (t, re) {
421 # Compute speed of sound in m/s
422 #
423 # t = temperature in Celsius
424 # re = amount of water vapor in the air
425
426 # Compute virtual temperature using mixing ratio (amount of water vapor)
427 # Ratio of gas constants of dry air and water vapor: 287.058 / 461.5 = 0.622
428 var T = 273.15 + t;
429 var v_T = T * (1 + re/0.622)/(1 + re);
430
431 # Compute speed of sound using adiabatic index, gas constant of air,
432 # and virtual temperature in Kelvin.
433 return math.sqrt(1.4 * 287.058 * v_T);
434 };
435
436 var thunder = func (name) {
437 var thunderCalls = 0;
438
439 var lightning_pos_x = getprop("/environment/lightning/lightning-pos-x");
440 var lightning_pos_y = getprop("/environment/lightning/lightning-pos-y");
441 var lightning_distance = math.sqrt(math.pow(lightning_pos_x, 2) + math.pow(lightning_pos_y, 2));
442
443 # On the ground, thunder can be heard up to 16 km. Increase this value
444 # a bit because the aircraft is usually in the air.
445 if (lightning_distance > 20000)
446 return;
447
448 var t = getprop("/environment/temperature-degc");
449 var re = getprop("/environment/relative-humidity") / 100;
450 var delay_seconds = lightning_distance / speed_of_sound(t, re);
451
452 # Maximum volume at 5000 meter
453 var lightning_distance_norm = std.min(1.0, 1 / math.pow(lightning_distance / 5000.0, 2));
454
455 settimer(func {
456 var thunder1 = getprop("/sim/model/c172p/sound/click-thunder1");
457 var thunder2 = getprop("/sim/model/c172p/sound/click-thunder2");
458 var thunder3 = getprop("/sim/model/c172p/sound/click-thunder3");
459
460 if (!thunder1) {
461 thunderCalls = 1;
462 setprop("/sim/model/c172p/sound/lightning/dist1", lightning_distance_norm);
463 }
464 else if (!thunder2) {
465 thunderCalls = 2;
466 setprop("/sim/model/c172p/sound/lightning/dist2", lightning_distance_norm);
467 }
468 else if (!thunder3) {
469 thunderCalls = 3;
470 setprop("/sim/model/c172p/sound/lightning/dist3", lightning_distance_norm);
471 }
472 else
473 return;
474
475 # Play the sound (sound files are about 9 seconds)
476 click("thunder" ~ thunderCalls, 9.0, 0);
477 }, delay_seconds);
478 };
479
480 var reset_system = func {
481 if (getprop("/fdm/jsbsim/running")) {
482 c172p.autostart(0);
483 }
484
485 # These properties are aliased to MP properties in /sim/multiplay/generic/.
486 # This aliasing seems to work in both ways, because the two properties below
487 # appear to receive the random values from the MP properties during initialization.
488 # Therefore, override these random values with the proper values we want.
489 props.globals.getNode("/fdm/jsbsim/crash", 0).setBoolValue(0);
490 props.globals.getNode("/fdm/jsbsim/gear/unit[0]/broken", 0).setBoolValue(0);
491 props.globals.getNode("/fdm/jsbsim/gear/unit[1]/broken", 0).setBoolValue(0);
492 props.globals.getNode("/fdm/jsbsim/gear/unit[2]/broken", 0).setBoolValue(0);
493 props.globals.getNode("/fdm/jsbsim/pontoon-damage/left-pontoon", 0).setIntValue(0);
494 props.globals.getNode("/fdm/jsbsim/pontoon-damage/right-pontoon", 0).setIntValue(0);
495
496 setprop("/engines/active-engine/kill-engine", 0);
497
498 setprop("/fdm/jsbsim/damage/repairing", 0);
499
500 set_fuel();
501 }
502
503 ############################################
504 # Engine coughing sound
505 ############################################
506
507 setlistener("/engines/active-engine/killed", func (node) {
508 if (node.getValue() and getprop("/engines/active-engine/running")) {
509 click("coughing-engine-sound", 0.7, 0);
510 };
511 });
512
513 ############################################
514 # Static objects: right safety cone
515 ############################################
516
517 var StaticModel = {
518 new: func (name, file) {
519 var m = {
520 parents: [StaticModel],
521 model: nil,
522 model_file: file,
523 object_name: name
524 };
525
526 setlistener("/sim/" ~ name ~ "/enable", func (node) {
527 if (node.getBoolValue()) {
528 m.add();
529 }
530 else {
531 m.remove();
532 }
533 });
534
535 return m;
536 },
537
538 add: func {
539 var manager = props.globals.getNode("/models", 1);
540 var i = 0;
541 for (; 1; i += 1) {
542 if (manager.getChild("model", i, 0) == nil) {
543 break;
544 }
545 }
546 var position = geo.aircraft_position().set_alt(getprop("/position/ground-elev-m"));
547 if (me.object_name == "anchorbuoy") {
548 me.model = geo.put_model(me.model_file, getprop("/fdm/jsbsim/mooring/anchor-lat"), getprop("/fdm/jsbsim/mooring/anchor-lon"), getprop("/position/ground-elev-m"), getprop("/orientation/heading-deg"));
549 } else {
550 me.model = geo.put_model(me.model_file, position, getprop("/orientation/heading-deg"));
551 }
552 },
553
554 remove: func {
555 if (me.model != nil) {
556 me.model.remove();
557 me.model = nil;
558 }
559 }
560 };
561
562 StaticModel.new("coneR", "Aircraft/c172p/Models/Exterior/safety-cone/coneR.ac");
563 StaticModel.new("coneL", "Aircraft/c172p/Models/Exterior/safety-cone/coneL.ac");
564 StaticModel.new("gpu", "Aircraft/c172p/Models/Exterior/external-power/external-power.xml");
565 StaticModel.new("ladderR", "Aircraft/c172p/Models/Exterior/ladder/ladderR.ac");
566 StaticModel.new("ladderL", "Aircraft/c172p/Models/Exterior/ladder/ladderL.ac");
567 StaticModel.new("fueltanktrailer", "Aircraft/c172p/Models/Exterior/fueltanktrailer/fueltanktrailer.ac");
568 StaticModel.new("externalheater", "Aircraft/c172p/Models/Exterior/external-heater/heater.xml");
569
570 # Mooring anchor and rope
571 StaticModel.new("anchorbuoy", "Aircraft/c172p/Models/Effects/pontoon/mooring.xml");
572
573
574 #
575 # Realism settings
576 #
577 var latitude_nut_update = func() {
578 var p = "/instrumentation/heading-indicator/latitude-nut-setting";
579 var lat = getprop("position/latitude-deg");
580 var tgt = sprintf("%2.0f", lat);
581 var cur = sprintf("%2.0f", getprop(p));
582 if (tgt != cur) {
583 setprop(p, tgt);
584 print("C172 HI/DG latitude nut adjusted from "~cur~"° to "~tgt~"°");
585 }
586 }
587 var latitude_nut_setter_timer = maketimer(30.0, latitude_nut_update);
588
589 # Define realism adjustments. Default values here are the "unrealistic" settings.
590 var prev_realism_state = [
591 {
592 id:"attitude-indicator",
593 node: props.globals.getNode("/instrumentation/attitude-indicator"),
594 props:[
595 {name:"gyro/spin-up-sec", value:4.0}
596 ]
597 },
598 {
599 id:"heading-indicator",
600 node: props.globals.getNode("/instrumentation/heading-indicator"),
601 props:[
602 {name:"gyro/spin-up-sec", value:4.0},
603 {name:"limits/g-error-factor", value:0.0},
604 {name:"limits/yaw-error-factor", value:0.0},
605 {name:"limits/g-limit-lower", value:-99.0},
606 {name:"limits/g-limit-upper", value:99.0}
607 ]
608 },
609 {
610 id:"turn-indicator",
611 node: props.globals.getNode("/instrumentation/turn-indicator"),
612 props:[
613 {name:"gyro/spin-up-sec", value:4.0}
614 ]
615 },
616 {
617 id:"magnetic-compass",
618 node: props.globals.getNode("/instrumentation/magnetic-compass"),
619 props:[
620 {name:"roll-limit-left", value:-999.0},
621 {name:"roll-limit-right", value:999.0},
622 {name:"pitch-limit-up", value:999.0},
623 {name:"pitch-limit-down", value:-999.0}
624 ]
625 },
626 ];
627 var setRealismInstruments_setting = 1;
628 var setRealismInstruments = func() {
629 var activate = getprop("/sim/realism/instruments/realistic-instruments");
630 if (activate == setRealismInstruments_setting) return;
631
632 setRealismInstruments_setting = activate;
633 var setting_txt = (activate)? "enabled": "disabled";
634 print("C172 realistic instruments: "~setting_txt);
635
636 # Swap the current value with the old one for each defined prop
637 foreach (item; prev_realism_state) {
638 print(" "~item.id);
639 foreach (p; item.props) {
640 var cur_val = item.node.getValue(p.name);
641 #print(" "~p.name~ "\t\t(old="~cur_val~"; new="~p.value~")");
642 item.node.setValue(p.name, p.value);
643 p.value = cur_val;
644 }
645 }
646
647 # Do some specific actions
648 if (activate) {
649 # Activate realistic behaviour
650 latitude_nut_setter_timer.stop();
651 print(" HI/DG latitude nut autoset stopped");
652
653 } else {
654 # Make unrealistic
655 latitude_nut_update();
656 latitude_nut_setter_timer.start();
657 print(" HI/DG latitude nut autoset activated");
658 }
659
660 }
661
662 ############################################
663 # Global loop function
664 # If you need to run nasal as loop, add it in this function
665 ############################################
666 var global_system_loop = func {
667 c172p.physics_loop();
668 }
669
670 ##########################################
671 # SetListerner must be at the end of this file
672 ##########################################
673 var set_limits = func (node) {
674 if (node.getValue() == 1) {
675 var limits = props.globals.getNode("/limits/mass-and-balance-180hp");
676 }
677 else {
678 var limits = props.globals.getNode("/limits/mass-and-balance-160hp");
679 }
680 var ac_limits = props.globals.getNode("/limits/mass-and-balance");
681
682 # Get the mass limits of the current engine
683 var ramp_mass = limits.getNode("maximum-ramp-mass-lbs");
684 var takeoff_mass = limits.getNode("maximum-takeoff-mass-lbs");
685 var landing_mass = limits.getNode("maximum-landing-mass-lbs");
686
687 # Get the actual mass limit nodes of the aircraft
688 var ac_ramp_mass = ac_limits.getNode("maximum-ramp-mass-lbs");
689 var ac_takeoff_mass = ac_limits.getNode("maximum-takeoff-mass-lbs");
690 var ac_landing_mass = ac_limits.getNode("maximum-landing-mass-lbs");
691
692 # Set the mass limits of the aircraft
693 ac_ramp_mass.unalias();
694 ac_takeoff_mass.unalias();
695 ac_landing_mass.unalias();
696
697 ac_ramp_mass.alias(ramp_mass);
698 ac_takeoff_mass.alias(takeoff_mass);
699 ac_landing_mass.alias(landing_mass);
700 };
701
702 setlistener("/controls/engines/active-engine", func (node) {
703 # Set new mass limits for Fuel and Payload Settings dialog
704 set_limits(node);
705
706 # Emit a sound because the engine has been replaced
707 click("engine-repair", 6.0);
708 }, 0, 0);
709
710 var update_pax = func {
711 var state = 0;
712 state = bits.switch(state, 0, getprop("pax/co-pilot/present"));
713 state = bits.switch(state, 1, getprop("pax/left-passenger/present"));
714 state = bits.switch(state, 2, getprop("pax/right-passenger/present"));
715 state = bits.switch(state, 3, getprop("pax/pilot/present"));
716 setprop("/payload/pax-state", state);
717 };
718
719 setlistener("/pax/co-pilot/present", update_pax, 0, 0);
720 setlistener("/pax/left-passenger/present", update_pax, 0, 0);
721 setlistener("/pax/right-passenger/present", update_pax, 0, 0);
722 setlistener("/pax/pilot/present", update_pax, 0, 0);
723 update_pax();
724
725 var update_securing = func {
726 var state = 0;
727 state = bits.switch(state, 0, getprop("/sim/model/c172p/securing/pitot-cover-visible"));
728 state = bits.switch(state, 1, getprop("/sim/model/c172p/securing/chock-visible"));
729 state = bits.switch(state, 2, getprop("/sim/model/c172p/securing/tiedownL-visible"));
730 state = bits.switch(state, 3, getprop("/sim/model/c172p/securing/tiedownR-visible"));
731 state = bits.switch(state, 4, getprop("/sim/model/c172p/securing/tiedownT-visible"));
732 setprop("/payload/securing-state", state);
733 };
734
735 setlistener("/sim/model/c172p/securing/pitot-cover-visible", update_securing, 0, 0);
736 setlistener("/sim/model/c172p/securing/chock-visible", update_securing, 0, 0);
737 setlistener("/sim/model/c172p/securing/tiedownL-visible", update_securing, 0, 0);
738 setlistener("/sim/model/c172p/securing/tiedownR-visible", update_securing, 0, 0);
739 setlistener("/sim/model/c172p/securing/tiedownT-visible", update_securing, 0, 0);
740 update_securing();
741
742 var log_cabin_temp = func {
743 if (getprop("/sim/model/c172p/enable-fog-frost")) {
744 var temp_degc = getprop("/fdm/jsbsim/heat/cabin-air-temp-degc");
745 if (temp_degc >= 32)
746 logger.screen.red("Cabin temperature exceeding 90F/32C!");
747 elsif (temp_degc <= 0)
748 logger.screen.red("Cabin temperature falling below 32F/0C!");
749 }
750 };
751 var cabin_temp_timer = maketimer(30.0, log_cabin_temp);
752
753 var log_fog_frost = func {
754 if (getprop("/sim/model/c172p/enable-fog-frost")) {
755 logger.screen.white("Wait until fog/frost clears up or decrease cabin air temperature");
756 }
757 };
758 var fog_frost_timer = maketimer(30.0, log_fog_frost);
759
760 var dialog_battery_reload = func {
761 electrical.reset_battery_and_circuit_breakers();
762 gui.popupTip("The battery is now fully charged!");
763 }
764
765 var c172_timer = maketimer(0.25, func{global_system_loop()});
766
767 setlistener("/sim/signals/fdm-initialized", func {
768 # Randomize callsign of new users to avoid them blocking
769 # other new users on multiplayer
770 if (getprop("/sim/multiplay/callsign") == "callsign") {
771 var digit = func {
772 return math.round(rand()*9);
773 };
774 var new_callsign = "FG-" ~ digit() ~ digit() ~ digit() ~ digit();
775 setprop("/sim/multiplay/callsign", new_callsign);
776 };
777
778 # Use Nasal to make some properties persistent. <aircraft-data> does
779 # not work reliably.
780 aircraft.data.add("/sim/model/c172p/immat-on-panel");
781 aircraft.data.load();
782
783 # Initialize mass limits
784 set_limits(props.globals.getNode("/controls/engines/active-engine"));
785
786 # Close all caps and doors
787 setlistener("/engines/active-engine/cranking", func (node) {
788 setprop("sim/model/show-dip-stick", 0);
789 setprop("sim/model/open-pfuel-cap", 0);
790 setprop("sim/model/open-sfuel-cap", 0);
791 setprop("sim/model/open-pfuel-sump", 0);
792 setprop("sim/model/open-sfuel-sump", 0);
793 setprop("sim/model/door-positions/oilDoor/position-norm", 0);
794 setprop("sim/model/c172p/securing/cowl-plugs-visible", 0);
795 fgcommand("dialog-close", props.Node.new({"dialog-name": "c172p-oil-dialog-160"}));
796 fgcommand("dialog-close", props.Node.new({"dialog-name": "c172p-oil-dialog-180"}));
797 fgcommand("dialog-close", props.Node.new({"dialog-name": "c172p-left-fuel-dialog"}));
798 fgcommand("dialog-close", props.Node.new({"dialog-name": "c172p-right-fuel-dialog"}));
799 fgcommand("dialog-close", props.Node.new({"dialog-name": "c172p-left-fuel-sample-dialog"}));
800 fgcommand("dialog-close", props.Node.new({"dialog-name": "c172p-right-fuel-sample-dialog"}));
801 }, 0, 0);
802
803 setlistener("/engines/active-engine/running", func (node) {
804 var autostart = getprop("/engines/active-engine/auto-start");
805 var cranking = getprop("/engines/active-engine/cranking");
806 if (autostart and cranking and node.getBoolValue()) {
807 setprop("/controls/switches/starter", 0);
808 setprop("/engines/active-engine/auto-start", 0);
809 }
810 }, 0, 0);
811
812 setlistener("/engines/active-engine/cranking", func (node) {
813 setprop("/engines/active-engine/external-heat/enabled", 0);
814 setprop("/sim/externalheater/enable", 0);
815 setprop("/fdm/jsbsim/external_reactions/towbar/attached", 0);
816 }, 0, 0);
817
818 # Checking if switches should be moved back to default position (in case save state is off)
819 switches_save_state();
820
821 # Checking if fuel contamination is allowed, and if so generating a random situation
822 fuel_contamination();
823
824 # Listening for lightning strikes
825 setlistener("/environment/lightning/lightning-pos-y", thunder);
826
827 reset_system();
828
829 var onground = getprop("/sim/presets/onground") or "";
830 if (!onground) {
831 state_manager();
832 }
833
834 # set user defined pilot view or initialize it
835 if (getprop("sim/current-view/view-number") == 0){
836 settimer(func {
837 if (getprop("sim/current-view/user/x-offset-m") != nil){
838 setprop("sim/current-view/x-offset-m", getprop("sim/current-view/user/x-offset-m"));
839 } else {
840 setprop("sim/current-view/user/x-offset-m", getprop("sim/view/config/x-offset-m"));
841 }
842 if (getprop("sim/current-view/user/y-offset-m") != nil){
843 setprop("sim/current-view/y-offset-m", getprop("sim/current-view/user/y-offset-m"));
844 } else {
845 setprop("sim/current-view/user/y-offset-m", getprop("sim/view/config/y-offset-m"));
846 }
847 if (getprop("sim/current-view/user/z-offset-m") != nil){
848 setprop("sim/current-view/z-offset-m", getprop("sim/current-view/user/z-offset-m"));
849 } else {
850 setprop("sim/current-view/user/z-offset-m", getprop("sim/view/config/z-offset-m"));
851 }
852 if (getprop("sim/current-view/user/default-field-of-view-deg") != nil){
853 setprop("sim/current-view/field-of-view", getprop("sim/current-view/user/default-field-of-view-deg"));
854 } else {
855 setprop("sim/current-view/user/default-field-of-view-deg", getprop("sim/view/config/default-field-of-view-deg"));
856 }
857 if (getprop("sim/current-view/user/pitch-offset-deg") != nil){
858 setprop("sim/current-view/pitch-offset-deg", getprop("sim/current-view/user/pitch-offset-deg"));
859 } else {
860 setprop("sim/current-view/user/pitch-offset-deg", getprop("sim/view/config/pitch-offset-deg"));
861 }
862 }, 1);
863 }
864
865 if (getprop("sim/model/variant") == 4) {
866 settimer(func {
867 # check for ground type (land or water, only if not in flight)
868 var on_water = getprop("fdm/jsbsim/hydro/active-norm");
869 print("ON-WATER ="~ on_water);
870 if (on_water) {
871 setprop("controls/gear/gear-down-command", 0);
872 setprop("controls/gear/gear-down", 0);
873 setprop("fdm/jsbsim/gear/gear-pos-norm", 0);
874 } else {
875 setprop("controls/gear/gear-down-command", 1);
876 setprop("controls/gear/gear-down", 1);
877 setprop("fdm/jsbsim/gear/gear-pos-norm", 1);
878 }
879 }, 3);
880 }
881
882 settimer(func {
883 if (getprop("/sim/model/c172p/securing/tiedownT"))
884 setprop("/sim/model/c172p/securing/tiedownT-visible", 1);
885 if (getprop("/sim/model/c172p/securing/tiedownR"))
886 setprop("/sim/model/c172p/securing/tiedownR-visible", 1);
887 if (getprop("/sim/model/c172p/securing/tiedownL"))
888 setprop("/sim/model/c172p/securing/tiedownL-visible", 1);
889 }, 10);
890
891
892 # HI/DG: Reset offset to stored values
893 # The DG always initializes to indicated_heading=/orientation/heading-deg, so we need to apply the
894 # difference of the last known orientation to offset the HI, so it shows the last stored value.
895 # The result is, that the HI is only correctly aligned when the planes location and orientation did
896 # not change between sessions AND it was calibrated correctly.
897 # (This all would be alot easier if the c++ instrument would initialize to saved values)
898 var dg_offset_storemode = 0;
899 var dg_hdg_cur = props.globals.getNode("/orientation/heading-deg");
900 var dg_hdg_saved = props.globals.getNode("/orientation/heading-deg-saved", 1);
901 var dg_offset_cur = props.globals.getNode("/instrumentation/heading-indicator/offset-deg");
902 var dg_offset_saved = props.globals.getNode("/instrumentation/heading-indicator/offset-deg-saved", 1);
903 var dg_error_cur = props.globals.getNode("/instrumentation/heading-indicator/error-deg");
904 var dg_error_saved = props.globals.getNode("/instrumentation/heading-indicator/error-deg-saved", 1);
905
906 # Restore values from last session
907 if (dg_hdg_saved.getValue() != nil and dg_offset_saved.getValue() != nil and dg_error_saved.getValue() != nil) {
908 print("C172 restore HI/DG settings...");
909 print(" HI/DG startup error: "~sprintf("%.2f",dg_error_saved.getValue()));
910 dg_error_cur.setDoubleValue(dg_error_saved.getValue());
911
912 print(" HI/DG startup offset: "~sprintf("%.2f",dg_offset_saved.getValue()));
913 dg_offset_cur.setDoubleValue(dg_offset_saved.getValue());
914
915 # Apply orientation difference from last session
916 var hdg_diff = dg_hdg_saved.getValue() - dg_hdg_cur.getValue();
917 var dg_offset_startup = dg_offset_cur.getValue() + hdg_diff;
918 print(" HI/DG startup heading: from "~sprintf("%.2f",dg_offset_cur.getValue())~" to "~sprintf("%.2f",dg_offset_startup)~" (stored orientation diff="~sprintf("%.2f", hdg_diff)~")");
919 dg_offset_cur.setDoubleValue(dg_offset_startup);
920 }
921
922 # Store values for next session (as we can't store the instruments values itself, they get overwritten)
923 var dg_offset_startup_timer = maketimer(1.0, func(){
924 dg_hdg_saved.setDoubleValue(dg_hdg_cur.getValue());
925 #print("C172 updating stored HI/DG heading to "~dg_hdg_saved.getValue());
926 dg_offset_saved.setDoubleValue(dg_offset_cur.getValue());
927 #print("C172 updating stored HI/DG offset to "~dg_offset_saved.getValue());
928 dg_error_saved.setDoubleValue(dg_error_cur.getValue());
929 #print("C172 updating stored HI/DG error to "~dg_error_saved.getValue());
930 });
931 dg_offset_startup_timer.start();
932
933 # Handle realism settings
934 setlistener("/sim/realism/instruments/realistic-instruments", setRealismInstruments, 1, 0);
935
936 c172_timer.start();
937 });
938
939 setlistener("/sim/model/c172p/cabin-air-temp-in-range", func (node) {
940 if (node.getValue()) {
941 cabin_temp_timer.stop();
942 logger.screen.green("Cabin temperature between 32F/0C and 90F/32C");
943 }
944 else {
945 log_cabin_temp();
946 cabin_temp_timer.start();
947 }
948 }, 1, 0);
949
950 setlistener("/sim/model/c172p/fog-or-frost-increasing", func (node) {
951 if (node.getValue()) {
952 log_fog_frost();
953 fog_frost_timer.start();
954 }
955 else {
956 fog_frost_timer.stop();
957 }
958 }, 1, 0);
959
960 # season-winter is a conversion value, see c172p-ground-effects.xml
961 setprop("/sim/startup/season-winter", getprop("/sim/startup/season") == "winter");
962 setlistener("/sim/startup/season", func (node) {
963 setprop("/sim/startup/season-winter", node.getValue() == "winter");
964 }, 0, 0);
965
966 # rudder trim setting changes, manual or automatic
967 setlistener("/sim/model/c172p/ruddertrim-visible", func (node) {
968 if (node.getValue()) {
969 setprop("/controls/flight/rudder-trim", 0);
970 } else
971 setprop("/controls/flight/rudder-trim", 0.02);
972 }, 0, 0);
973
974 #fuel tank configuration switch
975 setlistener("/fdm/jsbsim/fuel/tank", func (node) {
976 # Set fuel configuration
977 set_fuel();
978 }, 0, 0);
979
980 #amphibious gear control
981 setlistener("sim/model/variant", func (node) {
982 if (getprop("sim/model/variant") == 4) {
983 setprop("controls/circuit-breakers/gear-advisory", 1);
984 setprop("controls/circuit-breakers/hydraulic-pump", 1);
985 setprop("controls/circuit-breakers/gear-select", 1);
986 # check for ground type
987 var on_water = getprop("fdm/jsbsim/hydro/active-norm");
988 if (on_water) {
989 setprop("controls/gear/gear-down-command", 0);
990 setprop("controls/gear/gear-down", 0);
991 setprop("fdm/jsbsim/gear/gear-pos-norm", 0);
992 } else {
993 setprop("controls/gear/gear-down-command", 1);
994 setprop("controls/gear/gear-down", 1);
995 setprop("fdm/jsbsim/gear/gear-pos-norm", 1);
996 }
997 }
998 }, 1, 0);
999
1000
1001 ##########
1002 # Handle Yoke transparency
1003 ##########
1004 var updateYokeTransparency = func() {
1005 var hide = getprop("/sim/model/hide-yoke") or 0;
1006 if (hide == 1) {
1007 alpha = getprop("sim/model/hide-yoke-alpha-cmd");
1008 } else {
1009 alpha = 1;
1010 }
1011 setprop("/sim/model/hide-yoke-alpha", alpha);
1012 }
1013 setlistener("/sim/model/hide-yoke", updateYokeTransparency, 1, 0);
1014 setlistener("/sim/model/hide-yoke-alpha-cmd", updateYokeTransparency, 1, 0);