1# Lockheed 1049H
2#
3# Custom 1049H routines for engine support
4#
5# Copyright (c) 2011 Gary Neely aka 'Buckaroo'
6# Copyright (c) 2015 Ludovic Brenta
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (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# These vars declared in electrical.nas:
18# MIN_VOLTS, bus_dc
19
20# Set the starter on a given engine on or off but only on if it has power.
21#
22var set_engine_starter = func(engine, on)
23{
24    var engines = props.globals.getNode("controls/engines");
25    var starter = engines.getChild("engine", engine).getNode("starter");
26    var powered = bus_dc.getNode("volts").getValue() >= MIN_VOLTS;
27
28    starter.setValue(on and powered);
29}
30
31# Set the state of the engine starter on the selected engine in response to
32# changes to the engine starter switch.
33#
34var engine_start_listener = func(engine_start)
35{
36    var selected = getprop("controls/switches/engine-start-select");
37
38    if (selected > 0) {
39        set_engine_starter(selected - 1, engine_start.getValue());
40    }
41}
42
43# Ensure starters on unselected engines are turned off when the starter select
44# dial changes. Disabled for automated checklist starts because multiple
45# starters may fire during expedited (in-air) starts.
46#
47var engine_select_listener = func(engine_start_select)
48{
49    var auto = getprop("sim/checklists/auto/active") or 0;
50    if (auto) return;
51
52    var engine_start = getprop("controls/switches/engine-start");
53
54    for (var s = 1; s <= 4; s += 1) {
55        var selected = (s == engine_start_select.getValue());
56        set_engine_starter(s - 1, selected and engine_start);
57    }
58}
59
60# Adjust the cooling factor of each engine as cowl flaps are opened or closed.
61
62var adjust_cooling_factor = func (cowl_flaps_node) {
63   var engine_number = cowl_flaps_node.getParent ().getIndex ();
64   setprop ("/fdm/jsbsim/propulsion/engine[" ~ engine_number ~ "]/cooling-factor",
65            0.4 + 0.15 * cowl_flaps_node.getValue ());
66}
67
68var cowl_flaps_listeners = [ 0, 0, 0, 0 ]; # prevents re-registering listeners on Shift+Esc
69
70setlistener ("/sim/signals/fdm-initialized", func {
71
72   # Listen for changes to cowl flap settings
73   for (var engine = 0; engine < 4; engine = engine + 1) {
74      if (cowl_flaps_listeners [engine] == 0) {
75         cowl_flaps_listeners [engine] =
76           setlistener ("/controls/engines/engine[" ~ engine ~ "]/cowl-flaps-norm",
77                        adjust_cooling_factor, 1, 0);
78      }
79      else {
80         print ("FDM reinitialized; not re-registering cowl flap listeners.");
81      }
82   }
83
84   # Listen for changes to engine start switch
85   setlistener("controls/switches/engine-start",
86       engine_start_listener, 0, 0
87   );
88
89   # Listen for changes to engine start select switch
90   setlistener("controls/switches/engine-start-select",
91       engine_select_listener, 0, 0
92   );
93
94}, 0, 0);
95