Debian Codegraph @

Aircraft/Generic/soaring-instrumentation-sdk.nas

raw
1 # Glider Instrumentation Toolkit
2 #
3 # Copyright (C) 2013-2014 Anton Gomez Alvedro
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation; either version 2 of the
8 # License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #
19 # Features:
20 # + Total energy compensated variometer
21 # + Netto variometer
22 # + Relative (Super Netto) variometer
23 # + Configurable dampener for simulating needle response time
24 # + Configurable averager
25 # + Speed to fly computer
26 #
27 # TODO:
28 # - add wind correction to speed-to-fly
29 # - final glide computer
30
31 io.include("updateloop.nas");
32
33 var MPS2KPH = 3.6;
34 var sqr = func(x) {x * x}
35
36
37 var InstrumentComponent = {
38 parents: [Updatable],
39 output: 0,
40 reset: func { me.output = 0 },
41 };
42
43 # This alias is for keeping backwards compatibility.
44 # TODO: Refactor aircrafts that use it and remove this.
45 var Instrument = UpdateLoop;
46
47 ##
48 # Helper generator for updating a property on every element update
49 #
50 # Example:
51 #
52 # var needle = Dampener.new(
53 # input: probe,
54 # dampening: 2.8,
55 # on_update: update_prop("/instrumentation/variometer/te-reading-mps"));
56 #
57 # See Aircraft/Instruments-3d/glider/vario/ilec-sc7.nas and
58 # Aircraft/Generic/soaring-instrumentation-sdk.nas for usage examples.
59 # You can also refer to the soaring sdk wiki page.
60
61 var update_prop = func(property) {
62 func(value) { setprop(property, value) }
63 };
64
65 # InputSwitcher
66 # Selects output from one of multiple components given as inputs
67 #
68 # var lcd_controller = InputSwitcher.new(
69 # inputs: Vector of objects connected to the input
70 # active_input: (optional) Input number that is active at start
71 # on_update: (optional) function to call whenever a new output is available
72
73 var InputSwitcher = {
74
75 parents: [InstrumentComponent],
76
77 new: func(inputs, active_input = 0, on_update = nil) {
78 return {
79 parents: [me],
80 inputs: inputs,
81 active_input: active_input,
82 on_update: on_update
83 };
84 },
85
86 select_input: func(input_number) {
87 me.active_input = input_number;
88 me.update();
89 },
90
91 update: func {
92 me.output = me.inputs[me.active_input].output;
93 if (me.on_update != nil) me.on_update(me.output);
94 }
95 };
96
97 # PropertyReader
98 # Makes a property available at its output. Its purpose is to adapt properties
99 # to the component model used by the library.
100 #
101 # var temperature = PropertyReader.new(
102 # property: Property to read from
103 # scale: Scale factor applied to the property value (output = scale * prop)
104
105 var PropertyReader = {
106
107 parents: [InstrumentComponent],
108
109 new: func(property, scale = 1) {
110 return {
111 parents: [me],
112 property: property,
113 scale: scale
114 };
115 },
116
117 update: func {
118 me.output = me.scale * getprop(me.property);
119 }
120 };
121
122 # YawString
123 # The most important instrument in a glider. Simple, cheap and effective!
124 #
125 # var string = YawString.new(
126 # on_update: update_prop("/instrumentation/yaw-string/deflection-deg");
127
128 var YawString = {
129
130 parents: [InstrumentComponent],
131
132 new: func (on_update = nil) {
133 return {
134 parents: [me],
135 on_update: on_update
136 };
137 },
138
139 update: func {
140 var airspeed = getprop("velocities/airspeed-kt");
141 var noise = (airspeed < 54) ?
142 math.sin(math.pi * airspeed / 54) * rand() : 0;
143
144 me.output = noise + getprop("orientation/side-slip-deg");
145
146 if (me.on_update != nil) me.on_update(me.output);
147 }
148 };
149
150 # TotalEnergyProbe
151 # Computes total energy variation by reading current airspeed and altitude
152 #
153 # var probe = TotalEnergyProbe.new(
154 # on_update: (optional) function to call whenever a new output is available
155
156 var TotalEnergyProbe = {
157
158 parents: [InstrumentComponent],
159 altitude: 0, # meters
160 airspeed: 0, # m/s
161
162 new: func(on_update = nil) {
163 return {
164 parents: [me],
165 on_update: on_update
166 };
167 },
168
169 reset: func {
170 me.airspeed = getprop("/velocities/airspeed-kt") * KT2MPS;
171 me.altitude = getprop("/position/altitude-ft") * FT2M;
172 me.output = 0;
173 },
174
175 update: func(dt) {
176 var altitude_now = getprop("/position/altitude-ft") * FT2M;
177 var airspeed_now = getprop("/velocities/airspeed-kt") * KT2MPS;
178
179 me.output = (altitude_now - me.altitude) / dt;
180 me.output += (sqr(airspeed_now) - sqr(me.airspeed)) / (19.62 * dt);
181
182 me.altitude = altitude_now;
183 me.airspeed = airspeed_now;
184
185 if (me.on_update != nil) me.on_update(me.output);
186 }
187 };
188
189 # Dampener
190 # Simple IIR exponential filter. Appropriate and efficient for simulating
191 # mechanical needle dampening.
192 #
193 # var needle = Dampener.new(
194 # input: Object connected to the dampeners input.
195 # dampening: (optional) Time constant for the filter in seconds
196 # scale: (optional) Scale factor applied to the input signal before filtering
197 # on_update: (optional) function to call whenever a new output is available
198
199 var Dampener = {
200
201 parents: [InstrumentComponent],
202 dampening: 0, # time constant of the exponential filter (sec)
203 scale: 1,
204
205 new: func(input, dampening = 3, scale = 1, on_update = nil) {
206 return {
207 parents: [me],
208 input: input,
209 dampening: dampening,
210 scale: scale,
211 on_update: on_update,
212 };
213 },
214
215 update: func(dt) {
216 var alfa = math.exp(-dt / me.dampening);
217 me.output = me.output * alfa + me.input.output * me.scale * (1 - alfa);
218 if (me.on_update != nil) me.on_update(me.output);
219 }
220 };
221
222 # Averager
223 # Provides a windowed moving average of its input signal. Window size is
224 # set on construction, and is given in samples (i.e. not seconds).
225 #
226 # var averager = Averager.new(
227 # input: Object connected to the averagers input.
228 # size: (optional) window size in samples
229 # on_update: (optional) function to call whenever a new output is available
230
231 var Averager = {
232
233 parents: [InstrumentComponent],
234
235 new: func(input, buffer_size = 25, on_update = nil) {
236 var m = { parents: [me] };
237 m.input = input;
238 m.on_update = on_update;
239 m.size = buffer_size;
240 m.sum = m.wp = 0;
241
242 m.buffer = setsize([], buffer_size);
243 m.reset();
244 return m;
245 },
246
247 reset: func {
248 me.sum = me.wp = me.output = 0;
249 forindex (var i; me.buffer)
250 me.buffer[i] = 0;
251 },
252
253 update: func {
254 var new_value = me.input.output;
255
256 me.sum = me.sum + new_value - me.buffer[me.wp];
257 me.output = me.sum / me.size;
258
259 me.buffer[me.wp] = new_value;
260 if ((me.wp += 1) == me.size)
261 me.wp = 0;
262
263 if (me.on_update != nil) me.on_update(me.output);
264 }
265 };
266
267 # PolarSolver
268 # Helper object required for advanced soaring instrumentation.
269 # Provides McCready speed-to-fly computations assuming a parabolic glider polar
270 # (this approximation is frequently used in real instruments as well).
271 #
272 # Polar coeficients provided on construction correspond to the equation:
273 # sink = coefs[0] * airspeed^2 + coefs[1] * airspeed + coefs[2]
274 #
275 # Note that sink is considered positive. Negative sink means.. lift!
276 #
277 # var solver = PolarSolver.new(
278 # polar_coefs: [0.000364277, -0.0479199, 2.31644]
279 # mass: Reference mass in Kg used while obtaining the polar above
280
281 var PolarSolver = {
282
283 min_sink: 0, # minimum sink m/s, according to glider polar
284
285 new: func(polar_coefs, mass) {
286 var m = { parents: [me] };
287 m.reference_coefs = polar_coefs;
288 m.coefs = polar_coefs;
289 m.reference_mass = mass;
290 m.total_mass = mass;
291 m.min_sink = m.coefs[2] - (sqr(m.coefs[1]) / (4 * m.coefs[0]));
292 return m;
293 },
294
295 set_total_mass: func(mass) {
296 me.total_mass = mass;
297 var load_factor = math.sqrt(mass / me.reference_mass);
298
299 # Update active polar
300 me.coefs[0] = me.reference_coefs[0] / load_factor;
301 me.coefs[2] = me.reference_coefs[2] * load_factor;
302
303 me.min_sink = me.coefs[2] - (sqr(me.coefs[1]) / (4 * me.coefs[0]));
304 },
305
306 speed_to_fly: func(mc, airmass_sink) {
307 var speed = (mc + me.coefs[2] + airmass_sink) / me.coefs[0];
308 return (speed > 0) ? math.sqrt(speed) : 0;
309 },
310
311 ld: func(airspeed) {
312 return aispeed / me.sink(airspeed);
313 },
314
315 sink: func(airspeed) {
316 return me.coefs[0] * sqr(airspeed)
317 + me.coefs[1] * airspeed + me.coefs[2];
318 }
319 };
320
321 # NettoVario
322 # The Netto variometer substract glider's sink rate for current airpseed from a
323 # total energy reading. The resulting value is airmass' lift/sink in m/s.
324 #
325 # var netto = NettoVario.new(
326 # te_probe: Object providing a total energy reading
327 # polar_solver: Object providing a McCready implementation
328 # on_update: (optional) function to call whenever a new output is available
329
330 var NettoVario = {
331
332 parents: [InstrumentComponent],
333
334 new: func(te_probe, polar_solver, on_update=nil) {
335 return {
336 parents: [me],
337 probe: te_probe,
338 polar: polar_solver,
339 on_update: on_update
340 };
341 },
342
343 update: func {
344 me.output = probe.output
345 + me.polar.sink(probe.airspeed);
346
347 if (me.on_update != nil) me.on_update(me.output);
348 }
349 };
350
351 # RelativeVario
352 # The Relative (aka Super Netto) variometer tell you what climb rate would you
353 # get if you slowed down to optimal thermaling speed.
354 #
355 # var snetto = RelativeVario.new(
356 # te_probe: Object providing a total energy reading
357 # polar_solver: Object providing a McCready implementation
358 # on_update: (optional) function to call whenever a new output is available
359
360 var RelativeVario = {
361
362 new: func(te_probe, polar_solver, on_update=nil) {
363 return {
364 parents: [me, NettoVario.new(te_probe, polar_solver, on_update)]
365 };
366 },
367
368 update: func {
369 me.output = probe.output
370 + me.polar.sink(probe.airspeed)
371 - me.polar.min_sink;
372
373 if (me.on_update != nil) me.on_update(me.output);
374 }
375 };
376
377 # SpeedCmdVario
378 # The speed command variometer tells you how fast or slow your airspeed is with
379 # respect to the optimal speed-to-fly (computed according to McCready theory).
380 #
381 # var speedcmd = SpeedCmdVario.new(
382 # te_probe: Object providing a total energy reading
383 # polar_solver: Object providing a McCready implementation
384 # netto: (optional) Object providing a Netto reading
385 # on_update: (optional) function to call whenever a new output is available
386
387 var SpeedCmdVario = {
388
389 parents: [InstrumentComponent],
390 mc: 0, # mccready setting
391
392 new: func(te_probe, polar_solver, netto = nil, on_update = nil) {
393 return {
394 parents: [me],
395 polar: polar_solver,
396 probe: te_probe,
397 netto: netto or NettoVario.new(te_probe, polar_solver),
398 update_netto: (netto == nil),
399 on_update: on_update
400 };
401 },
402
403 update: func {
404 if (me.update_netto) me.netto.update();
405
406 var target_speed = me.polar.speed_to_fly(me.mc, -me.netto.output);
407 me.output = me.probe.airspeed * MPS2KPH - target_speed;
408
409 if (me.on_update != nil) me.on_update(me.output);
410 }
411 };
412
413 # Vario sound pitch controller
414 # Computes the frequency factor for a variometer sound.
415 #
416 # var vario_sound = SoundPitchController.new(
417 # input: Object connected to the pitch controller input, e.g. a variometer reading.
418 # max_pitch: (optional) Maximum sound frequency factor, the output will be
419 # in the range [1/max_pitch, max_pitch], default 2.
420 # max_input: Value of input for which max_pitch is reached.
421 # on_update: (optional) function to call whenever a new output is available
422
423 var SoundPitchController = {
424 parents: [InstrumentComponent],
425
426 new: func(input, max_input, max_pitch = 2, on_update = nil) {
427 return {
428 parents: [me],
429 input: input,
430 max_pitch: max_pitch,
431 max_input: max_input,
432 on_update: on_update,
433 };
434 },
435
436 update: func {
437 var input = math.clamp(me.input.output, -me.max_input, me.max_input);
438 me.output = math.pow(me.max_pitch, input / me.max_input);
439
440 if (me.on_update != nil) me.on_update(me.output);
441 },
442 };