Debian Codegraph @

Aircraft/c172p/Nasal/annunciator_panel.nas

raw
1 #
2 # C182S Annunciator panel
3 #
4 # (c) 2018 Benedikt Hallinger, public domain
5 #
6 # Described in POH 4-8; the lights will flash for approximately 10 seconds before turning steady on.
7 # This is also true for the test mode toggled by the switch.
8
9
10 #################################################################
11 # Infrastructure code to model the components
12
13
14 # Define a basic panel
15 var annunciator_panel = {
16 node: "/instrumentation/annunciator/",
17 volts: "/systems/electrical/outputs/annunciators",
18 svc: "/instrumentation/annunciator/serviceable",
19 test: "/instrumentation/annunciator/testswitch",
20 freq: 0.5, # the blinking interval of annunciators (also used as loop interval)
21 blink: 10, # blink for this many seconds
22 regobj: [], # registered annunciators for main loop
23
24 init: func() {
25 me.loop = maketimer(annunciator_panel.freq, func(){
26 # Annunciator panel loop: check annunciator states and trigger blinking/steady lights
27 foreach(annunc_obj; me.regobj) { annunc_obj.update(); };
28 });
29 me.loop.simulatedTime = 1;
30 me.loop.start();
31
32 print("Annunciator panel: initialized");
33 },
34
35 add: func(annunc_obj) {
36 append(me.regobj, annunc_obj);
37
38 #print("DBG: Annunciator panel: added new annunciator at: "~annunc_obj.tgt);
39
40 },
41
42 };
43
44
45 # Basic Abstract Annunciator class
46 # modelling the state of a single annunciator that can be activated.
47 # A display blinking value is added to a property appended with "-indicated".
48 # Expects a sublcass that implements isTriggered()!
49 var Annunciator = {
50 # Returns a new Annunciator object writing to a tgt_prop
51 new: func(tgt_prop) {
52 return {
53 parents: [Annunciator],
54 tgt: tgt_prop,
55 triggered_t: -1,
56 };
57 },
58
59 # Updates the state; called by Panel class main loop
60 update: func {
61 var l_volts = getprop(annunciator_panel.volts);
62 var l_svc = getprop(annunciator_panel.svc);
63 #print(" DBG: check annunciator: "~me.tgt~" [l_volts="~l_volts~"; l_svc="~l_svc~"; testSw:"~getprop(annunciator_panel.test)~"; ");
64 if (l_volts and l_svc and (me.isTriggered() or getprop(annunciator_panel.test) == 2)) {
65 # Annunciator test said we are triggeed (or testswitch is engaged)!
66 # initiate flashing or steady light
67 var now = getprop("/sim/time/elapsed-sec");
68
69 if (me.triggered_t == -1) {
70 # triggered the first time
71 me.triggered_t = now;
72 setprop(me.tgt, 1);
73 setprop(me.tgt~"-indicated", 1);
74 } else {
75 # already triggered: see if we should blink or light steady
76 var tgt_v = getprop(me.tgt~"-indicated");
77 if ((me.triggered_t + annunciator_panel.blink) > now) {
78 # blink!
79 setprop(me.tgt~"-indicated", !tgt_v);
80
81 } else {
82 # light steady!
83 if (tgt_v != 1) {
84 setprop(me.tgt~"-indicated", 1);
85 }
86 }
87 }
88
89 } else {
90 # reset annunciator to off (cause solved or loss of power)
91 me.triggered_t = -1;
92 setprop(me.tgt, 0);
93 setprop(me.tgt~"-indicated", 0);
94 }
95 },
96 };
97
98 # Annunciator class that triggers if a property value is lower than x
99 # Extends Annunciator base class
100 var AnnunciatorMin = {
101 new: func(tgt_prop, src_prop, v_min) {
102 var m = Annunciator.new(tgt_prop); # call parent constructor
103 append(m.parents, AnnunciatorMin);
104 m.src = src_prop;
105 m.min = v_min;
106
107 return m;
108 },
109
110 # Expected from Annunciator base class: tests if this annunciator was triggered
111 isTriggered: func() {
112 if (getprop(me.src) < me.min ) {
113 var r = 1;
114 } else {
115 var r = 0;
116 }
117 #print(" DBG: check annunciator: "~me.tgt~" ["~me.src~"] < ["~me.min~"]? => "~r);
118 return r;
119 },
120
121 };
122
123 # Annunciator class that triggers if a property value is higher than x
124 # Extends Annunciator base class
125 var AnnunciatorMax = {
126 new: func(tgt_prop, src_prop, v_max) {
127 var m = Annunciator.new(tgt_prop); # call parent constructor
128 append(m.parents, AnnunciatorMax);
129 m.src = src_prop;
130 m.max = v_max;
131
132 return m;
133 },
134
135 # Expected from Annunciator base class: tests if this annunciator was triggered
136 isTriggered: func() {
137 if (getprop(me.src) > me.max ) {
138 var r = 1;
139 } else {
140 var r = 0;
141 }
142 #print(" DBG: check annunciator: "~me.tgt~" ["~me.src~"] < ["~me.max~"]? => "~r);
143 return r;
144 },
145
146 };
147
148 # Annunciator class that triggers if one of the properties is true
149 # Extends Annunciator base class
150 var AnnunciatorOR = {
151 new: func(tgt_prop, src_prop_list) {
152 var m = Annunciator.new(tgt_prop); # call parent constructor
153 append(m.parents, AnnunciatorOR);
154 m.src = src_prop_list;
155
156 return m;
157 },
158
159 # Expected from Annunciator base class: tests if this annunciator was triggered
160 isTriggered: func() {
161 var r = 0;
162 foreach(var p; me.src) {
163 if (getprop(p) > 0) {
164 r = 1;
165 break;
166 }
167 #print(" DBG: check annunciator: "~me.tgt~" ["~p~"] > 0? => "~r);
168 }
169 return r;
170 },
171
172 };
173
174
175
176
177 ####################################################################
178 # Ok, lets go and define the annunciators and add them to the panel!
179 #
180 annunciator_panel.add( AnnunciatorMin.new(annunciator_panel.node~"volts-low", "/systems/electrical/volts", 24.5) );
181 annunciator_panel.add( AnnunciatorMin.new(annunciator_panel.node~"vac-low-l", "/systems/vacuum[0]/suction-inhg", 3.0) );
182 annunciator_panel.add( AnnunciatorMin.new(annunciator_panel.node~"vac-low-r", "/systems/vacuum[1]/suction-inhg", 3.0) );
183 annunciator_panel.add( AnnunciatorMin.new(annunciator_panel.node~"oilpress-low", "/engines/engine/indicated-oil-pressure-psi", 21.0) );
184 annunciator_panel.add( AnnunciatorMin.new(annunciator_panel.node~"fuel-low-r", "/consumables/fuel/tank[1]/indicated-level-gal_us", 8) );
185 annunciator_panel.add( AnnunciatorMin.new(annunciator_panel.node~"fuel-low-l", "/consumables/fuel/tank[0]/indicated-level-gal_us", 8) );
186 annunciator_panel.add( AnnunciatorOR.new(annunciator_panel.node~"fuel-low", ["/instrumentation/annunciator/fuel-low-l", "/instrumentation/annunciator/fuel-low-r"] ) );
187 annunciator_panel.add( AnnunciatorMax.new(annunciator_panel.node~"pitch-trim", "/instrumentation/annunciator/pitch-trim-trigger", 0) );
188
189 #TODO: for mor realism... the fuel detection should probably be modelled finer using a custom class: The POH says, that the annunciator nly fires if the low-condition is met for at least 60 seconds
190
191
192
193
194
195 ####################################################
196 # INIT: Start the panel code once FDM is ready
197
198 setlistener("/sim/signals/fdm-initialized", func {
199 annunciator_panel.init();
200
201
202 # The PITCH-TRIM annunciator needs the previuos AP state.
203 setprop("/autopilot/kap140/panel/state-previous", 0);
204 setprop("/autopilot/kap140/panel/state-previous-memory", 0);
205 setlistener("/autopilot/kap140/panel/state", func(n) {
206 var mem = getprop("/autopilot/kap140/panel/state-previous-memory");
207 if (getprop("autopilot/kap140/panel/button-ap") and n.getValue() == 5 and mem == 6) {
208 setprop("/autopilot/kap140/panel/state-previous", n.getValue());
209 } else {
210 setprop("/autopilot/kap140/panel/state-previous", mem);
211 }
212 setprop("/autopilot/kap140/panel/state-previous-memory", n.getValue());
213 }, 1, 0);
214 });