Debian Codegraph @

Aircraft/Instruments-3d/glider/vario/S3/S3.nas

raw
1 # LX Vario S3 by Benedikt Wolf (D-ECHO) based on
2
3 # A3XX Lower ECAM Canvas
4 # Joshua Davidson (it0uchpods)
5
6 # THANKS TO Colin Geniet (SoundPitchController), original developers of the ilec-sc7 (WooT)
7
8 # Information based on manual http://www.lx-avionik.de/wp/download/manuals/LXS3ManualGermanVer0100.pdf
9 #######################################
10
11 ## REQUIRES:
12 ## * second altimeter module enabled from <sim><instrumentation>
13 ## * power supply from systems/electrical/outputs/S3
14 ## (opt) * vario sound set up in the aircraft's sound file (see S3-sound.txt as an example)
15
16 #####################
17 ## Version 05/2020 ##
18 #####################
19 ## Features of this version:
20 ## * use maketimer, only update visible pages
21 ## * use props.nas, store in local variables where sensible
22 ## * store instrument directory in variable
23 ## * clarity through indentation
24 ## * use functions for common functionality
25 ## * clean up listeners
26
27 var S3_start = nil;
28 var S3_main = nil;
29 var S3_display = nil;
30
31 var s3 = props.globals.initNode("/instrumentation/s3");
32
33 var start_prop = s3.initNode("start", 0.0, "DOUBLE");
34 var pushknob_prop = s3.initNode("knob-pushed", 0, "BOOL");
35
36 var te_rdg = s3.initNode("te-reading-mps", 0.0, "DOUBLE");
37 var te_avg = s3.initNode("te-average-mps", 0.0, "DOUBLE");
38 var volume = s3.initNode("volume", 0.5, "DOUBLE");
39
40 var alt = props.globals.initNode("instrumentation/altimeter[1]/indicated-altitude-ft", 0.0, "DOUBLE");
41 var volt_prop = props.globals.initNode("/systems/electrical/outputs/S3", 0.0, "DOUBLE");
42
43 var mc = s3.initNode("mc", 1.5, "DOUBLE");
44
45 var needle = s3.initNode( "needle-deg", 0.0, "DOUBLE" );
46
47 var instrument_dir = "Aircraft/Instruments-3d/glider/vario/S3/";
48
49 var canvas_S3_base = {
50 init: func(canvas_group, file) {
51 var font_mapper = func(family, weight) {
52 return "LiberationFonts/LiberationSans-Bold.ttf";
53 };
54
55
56 canvas.parsesvg(canvas_group, file, {'font-mapper': font_mapper});
57
58 var svg_keys = me.getKeys();
59
60 foreach(var key; svg_keys) {
61 me[key] = canvas_group.getElementById(key);
62 }
63
64 me.page = canvas_group;
65
66 return me;
67 },
68 getKeys: func() {
69 return [];
70 },
71 update: func() {
72 var start = start_prop.getDoubleValue();
73 var volts = volt_prop.getDoubleValue();
74 if ( start == 1 and volts > 9 ) {
75 S3_start.page.hide();
76 S3_main.page.show();
77 S3_main.update();
78 } else if ( start > 0 and start < 1 and volts > 9 ){
79 S3_main.page.hide();
80 S3_start.page.show();
81 } else {
82 S3_main.page.hide();
83 S3_start.page.hide();
84 }
85 },
86 };
87
88
89 var canvas_S3_main = {
90 new: func(canvas_group, file) {
91 var m = { parents: [canvas_S3_main , canvas_S3_base] };
92 m.init(canvas_group, file);
93
94 return m;
95 },
96 getKeys: func() {
97 return ["altitude","average","average.needle","mccready.needle"];
98 },
99 update: func() {
100
101 #Altimeter
102 me["altitude"].setText( sprintf( "%4d", math.round( alt.getDoubleValue() * FT2M ) ) );
103
104
105 #Average climbrate
106 var av = te_avg.getDoubleValue();
107 me["average"].setText(sprintf("%2.1f", av));
108 if(av<5 and av>-5){
109 var av2=av;
110 }else if(av<-5){
111 var av2=-5;
112 }else if(av>5){
113 var av2=5;
114 }
115 me["average.needle"].setRotation(av2*D2R*24);
116
117 #McCready
118 me["mccready.needle"].setRotation( mc.getDoubleValue()*D2R*24 );
119 }
120
121 };
122
123
124 var canvas_S3_start = {
125 new: func(canvas_group, file) {
126 var m = { parents: [canvas_S3_start , canvas_S3_base] };
127 m.init(canvas_group, file);
128
129 return m;
130 },
131 getKeys: func() {
132 return [];
133 },
134 update: func() {
135 }
136
137 };
138
139 var s3_update = maketimer(0.2, func() { canvas_S3_base.update() } );
140 s3_update.simulatedTime = 1;
141
142 var vario_needle_ctrl = func {
143 if( start_prop.getDoubleValue() == 1.0 ){
144 needle.setDoubleValue( math.clamp( te_rdg.getDoubleValue() * 24, -132, 132 ) ); # max deflection: 132 deg at +- 5.5 m/s
145 } else {
146 needle.setDoubleValue( 0.0 );
147 }
148 }
149
150 var needle_update = maketimer( 0.0, vario_needle_ctrl );
151 needle_update.simulatedTime = 1;
152
153 var ls = setlistener("sim/signals/fdm-initialized", func {
154 S3_display = canvas.new({
155 "name": "S3",
156 "size": [320, 240],
157 "view": [320, 240],
158 "mipmapping": 1
159 });
160 S3_display.addPlacement({"node": "S3.display"});
161 var groupMain = S3_display.createGroup();
162 var groupStart = S3_display.createGroup();
163
164
165 S3_main = canvas_S3_main.new(groupMain, instrument_dir~"S3_main.svg");
166 S3_start = canvas_S3_start.new(groupStart, instrument_dir~"S3_start.svg");
167
168 s3_update.start();
169 needle_update.start();
170
171 removelistener(ls);
172 });
173
174 var i=0;
175
176 var check_off = func () {
177 if( pushknob_prop.getBoolValue() and i<50){
178 i=i+1;
179 settimer(check_off, 0.1);
180 }else if( pushknob_prop.getBoolValue() and i >= 50){
181 i=0;
182 start_prop.setDoubleValue( 0.0 ); #put proper shutdown routine here later
183 }else{
184 i=0;
185 }
186 }
187
188 var check_electric_off = func () {
189 if( volt_prop.getDoubleValue() < 9.0 and start_prop.getDoubleValue() != 0.0 ){
190 start_prop.setDoubleValue( 0.0 );
191 }
192 }
193
194
195 setlistener(pushknob_prop, func {
196 if( pushknob_prop.getBoolValue() ) {
197 if( volt_prop.getDoubleValue() >= 9.0 and start_prop.getDoubleValue() == 0.0 ){
198 interpolate(start_prop, 1, 4 );
199 } else {
200 check_electric_off();
201 }
202 check_off();
203 }
204 });
205
206
207 setlistener(volt_prop, func {
208 check_electric_off();
209 });
210
211
212
213 #The following code is based on the ILEC SC7 e-vario and computes the different values shown by the display and the mechanical needle
214 io.include("Aircraft/Generic/soaring-instrumentation-sdk.nas");
215
216 ####################################
217 #### INSTRUMENT SETUP ####
218 ####################################
219
220 # Vario sound pitch controller by Colin Geniet (for ASK21), thanks!
221 #
222 # var vario_sound = SoundPitchController.new(
223 # input: Object connected to the pitch controller input, e.g. a variometer reading.
224 # max_pitch: (optional) Maximum sound frequency factor, the output will be
225 # in the range [1/max_pitch, max_pitch], default 2.
226 # max_input: Value of input for which max_pitch is reached.
227 # on_update: (optional) function to call whenever a new output is available
228
229 var SoundPitchController = {
230 parents: [InstrumentComponent],
231
232 new: func(input, max_input, max_pitch = 2, on_update = nil) {
233 return {
234 parents: [me],
235 input: input,
236 max_pitch: max_pitch,
237 max_input: max_input,
238 on_update: on_update,
239 };
240 },
241
242 update: func {
243 var input = math.clamp(me.input.output, -me.max_input, me.max_input);
244 me.output = math.pow(me.max_pitch, input / me.max_input);
245
246 if (me.on_update != nil) me.on_update(me.output);
247 },
248 };
249
250 var probe = TotalEnergyProbe.new();
251
252 var s3_needle = Dampener.new(
253 input: probe,
254 dampening: 1.5,
255 on_update: update_prop("/instrumentation/s3/te-reading-mps"));#1.5 is default dampening value according to POH
256
257 var averager = Averager.new(
258 input: probe,
259 buffer_size: 20,
260 on_update: update_prop("/instrumentation/s3/te-average-mps")); #20s is default time according to POH
261
262 var s3_sound = SoundPitchController.new(
263 input: s3_needle,
264 max_input: 5,
265 on_update: update_prop("/instrumentation/s3/sound-pitch"));
266
267 # Wrap everything together into an instrument
268 var fast_instruments = UpdateLoop.new(
269 update_period: 0,
270 components: [probe, s3_needle, s3_sound],
271 enable: 1);
272
273 var slow_instruments = UpdateLoop.new(
274 update_period: 1,
275 components: [averager],
276 enable: 1);
277