Debian Codegraph @

Aircraft/Instruments-3d/RAF_Mk9_bubble_sextant/RAF_Mk9_bubble_sextant.nas

raw
1 ###############################################################################
2 ##
3 ## RAF mk9 bubble sextant.
4 ##
5 ## Copyright (C) 2007 - 2013 Anders Gidenstam (anders(at)gidenstam.org)
6 ## This file is licensed under the GPL license version 2 or later.
7 ##
8 ###############################################################################
9
10 ## NOTE: This module MUST be loaded as RAFmk9sextant;
11
12 ## You can override these parameters when loading this file.
13 # Field of view when looking through the sextant
14 var FOV = 25;
15 # Distance from the eye to the sextant when looking through the sextant.
16 var VIEW_DISTANCE = 0.15;
17 # The view in which the sextant will be used.
18 var VIEW_NAME = "Cockpit View";
19 # The storage location and orientation.
20 # The location is relative to the default center for the view.
21 var STOWED = { position : {x:-0.3, y: 0.0, z: -0.2},
22 orientation : {heading : 320.0,
23 pitch : -45.0,
24 roll : 0.0}
25 };
26
27 ## Interface functions.
28 var pick_up = func {
29 if (handling.lookthrough) return;
30 if (view.current != handling.source_view) return;
31
32 handling.toggle();
33 if (handling.enabled) {
34 handling.view_angle.setValue(-90.0);
35 handling.view_distance.setValue(2.0*VIEW_DISTANCE);
36 }
37 }
38
39 var toggle_look_through = func {
40 handling.toggle_look_through();
41 }
42
43 ###############################################################################
44 # Bindings for mouse X and Y movements. Install these to the appropriate
45 # mouse mode and axes, e.g. in the -set file.
46 var mouseXmove = func {
47 if (!_initialized or !handling.enabled) return;
48
49 var delta = 3*cmdarg().getNode("offset").getValue();
50 var view = "/sim/current-view/heading-offset-deg";
51 var val = getprop(view) - delta;
52 if(val < 0) val = 0;
53 if(val > 360) val = 360;
54 setprop(view, val);
55 }
56 var mouseXtilt = func {
57 if (!_initialized or !handling.enabled) return;
58
59 var delta = 3*cmdarg().getNode("offset").getValue();
60 # Roll adjustment
61 var orient = sextant.get_orientation();
62 var roll = orient[2] - delta;
63 if(roll < -180) roll = -180;
64 if(roll > 180) roll = 180;
65 sextant.set_orientation(orient[0], orient[1], roll);
66 }
67
68 var mouseYmove = func {
69 if (!_initialized or !handling.enabled) return;
70
71 var delta = 3*cmdarg().getNode("offset").getValue();
72 var view = "/sim/current-view/pitch-offset-deg";
73 var val = getprop(view) - delta;
74 if(val < -90) val = -90;
75 if(val > 90) val = 90;
76 setprop(view, val);
77 }
78 var mouseYaltitude = func {
79 if (!_initialized or !handling.enabled) return;
80
81 var delta = 3*cmdarg().getNode("offset").getValue();
82 # Altitude adjustment
83 sextant.adjust_altitude_fine(delta);
84 }
85
86 var RAD = math.pi/180;
87 var DEG = 180/math.pi;
88
89 ###############################################################################
90 # Class for managing one RAF mk9 bubble sextant instrument.
91 var sextant = {
92 ##################################################
93 init : func (n=0) {
94 me.UPDATE_INTERVAL = 0.0;
95 me.loopid = 0;
96 me.base =
97 props.globals.getNode("instrumentation/sextant["~ n ~"]/", 1);
98
99 ## Instrument properties
100 me.pitch_err = me.base.getNode("pitch-error-deg", 1);
101 me.pitch_err.setDoubleValue(0);
102 me.roll_err = me.base.getNode("roll-error-deg", 1);
103 me.roll_err.setDoubleValue(0);
104 me.setting_min = me.base.getNode("setting/min", 1);
105 me.setting_min.setDoubleValue(0);
106 me.setting_deg1 = me.base.getNode("setting/deg1", 1);
107 me.setting_deg1.setDoubleValue(0); # 0 - 10 deg fractional.
108 me.setting_deg10 = me.base.getNode("setting/deg10", 1);
109 me.setting_deg10.setDoubleValue(0);
110 me.bubble = me.base.getNode("bubble-norm", 1);
111 me.bubble.setDoubleValue(0);
112 me.serviceable = me.base.getNode("serviceable", 1);
113 me.serviceable.setBoolValue(1);
114
115 ## The instrument's orientation in the aircraft frame
116 ## and position offset relative its initial position.
117 me.position = [me.base.getNode("offsets/x-m", 1),
118 me.base.getNode("offsets/y-m", 1),
119 me.base.getNode("offsets/z-m", 1)];
120 me.heading = me.base.getNode("offsets/heading-deg", 1);
121 me.pitch = me.base.getNode("offsets/pitch-deg", 1);
122 me.roll = me.base.getNode("offsets/roll-deg", 1);
123
124 me.position[0].setValue(0);
125 me.position[1].setValue(0);
126 me.position[2].setValue(0);
127 me.heading.setValue(0);
128 me.pitch.setValue(0);
129 me.roll.setValue(0);
130
131 me.reset();
132
133 print("RAF Mk9 bubble sextant ... initialized");
134 },
135 ##################################################
136 set_position : func (x, y ,z) {
137 me.position[0].setValue(x);
138 me.position[1].setValue(y);
139 me.position[2].setValue(z);
140 },
141 ##################################################
142 get_position : func (x, y ,z) {
143 return [me.position[0].getValue(),
144 me.position[1].getValue(),
145 me.position[2].getValue()];
146 },
147 ##################################################
148 set_orientation : func (heading, pitch, roll) {
149 me.heading.setValue(heading);
150 me.pitch.setValue(pitch);
151 me.roll.setValue(roll);
152 },
153 ##################################################
154 get_orientation : func () {
155 return [me.heading.getValue(),
156 me.pitch.getValue(),
157 me.roll.getValue()];
158 },
159 ##################################################
160 step_10deg_knob : func (d) {
161 var val = me.setting_deg10.getValue() + (d < 0 ? -1 : 1);
162 if(val < 0) val = 0;
163 if(val > 8) val = 8;
164 me.setting_deg10.setValue(val);
165 },
166 ##################################################
167 step_5deg_knob : func (d) {
168 var val = me.setting_deg1.getValue();
169 var new = val;
170 if (d <= 0 and val >= 5.0) new -= 5.0;
171 if (d >= 0 and val < 5.0) new += 5.0;
172 if(new < 0.0) new = 0;
173 if(new > 10.0) new = 10;
174 me.setting_deg1.setValue(new);
175 },
176 ##################################################
177 step_bubble_knob : func (d) {
178 var val = me.bubble.getValue() + d;
179 if(val < 0) val = 0;
180 if(val > 1) val = 1;
181 me.bubble.setValue(val);
182 },
183 ##################################################
184 adjust_altitude_fine : func (d) {
185 var val = me.setting_deg1.getValue() - d;
186 if(val < 0) val = 0;
187 if(val > 10) val = 10;
188
189 me.setting_deg1.setValue(val);
190 me.setting_min.setValue(60.0*val - 60*int(val));
191 },
192 ##################################################
193 get_altitude : func {
194 return 10.0 * me.setting_deg10.getValue() + me.setting_deg1.getValue();
195 },
196 ##################################################
197 reset : func {
198 me.loopid += 1;
199 me._loop_(me.loopid);
200 },
201 ##################################################
202 update : func {
203 ## State data we need.
204 var heading_ac = getprop("/orientation/heading-deg") * RAD;
205 var pitch_ac = getprop("/orientation/pitch-deg") * RAD;
206 var roll_ac = getprop("/orientation/roll-deg") * RAD;
207
208 var yaw_v = me.heading.getValue() * RAD;
209 var pitch_v = me.pitch.getValue() * RAD;
210 var roll_v = me.roll.getValue() * RAD;
211
212 ## Compute local aircraft axes vectors in the local frame
213 ## Account for aircraft orientation. (x/y/z = front/left/up)
214 var T_ac = mulMM(mulMM(rotateZ(heading_ac), rotateY(pitch_ac)),
215 rotateX(roll_ac));
216 ## Account for view orientation and sextant settings.
217 ## The sextant frame is assumed to coincide with the view frame
218 ## except that it is pitched down altitude deg around its local
219 ## Y axis.
220 var T_bs = mulMM(mulMM(mulMM(mulMM(
221 T_ac,
222 rotateZ(yaw_v)),
223 rotateY(pitch_v)),
224 rotateX(roll_v)),
225 rotateY(-me.get_altitude() * RAD));
226
227 var X_bs = mulMv(T_bs, X);
228 var Y_bs = mulMv(T_bs, Y);
229 var Z_bs = mulMv(T_bs, Z);
230
231 ## Transform up in the local frame to the sextant frame.
232 var Up = mulMv([X_bs, Y_bs, Z_bs], Z);
233 var Up_xz = [Up[0], 0, Up[2]];
234 var Up_yz = [0, Up[1], Up[2]];
235
236 ## Compute interesting angles in the sextant frame.
237 var p_err = angleV(Z, Up_xz);
238 if (scalar(Up_xz, X) < 0.0) {
239 p_err *= -1;
240 }
241 me.pitch_err.setValue(p_err*DEG);
242
243 var r_err = angleV(Z, Up_yz);
244 if (scalar(Up_yz, Y) < 0.0) {
245 r_err *= -1;
246 }
247 me.roll_err.setValue(r_err*DEG);
248 },
249 ##################################################
250 _loop_ : func(id) {
251 id == me.loopid or return;
252 me.update();
253 settimer(func { me._loop_(id); }, me.UPDATE_INTERVAL, 1);
254 }
255 };
256
257 ###############################################################################
258 ## Singleton class for handling (i.e. moving / rotating) a sextant.
259 var handling = {
260 enabled : 0,
261 ##################################################
262 init : func (n=0) {
263 # Initialize the instrument.
264 sextant.init(n);
265 sextant.set_position
266 (STOWED.position.x,
267 STOWED.position.y,
268 STOWED.position.z);
269 sextant.set_orientation
270 (STOWED.orientation.heading,
271 STOWED.orientation.pitch,
272 STOWED.orientation.roll);
273
274 me.UPDATE_INTERVAL = 0.0;
275 me.loopid = 0;
276 me.lookthrough = 0;
277
278 me.base =
279 props.globals.getNode("instrumentation/sextant["~ n ~"]/", 1);
280
281 ## Instrument properties
282 me.altitude_deg = me.base.getNode("altitude-deg", 1);
283 me.altitude_deg.setDoubleValue(0);
284
285 ## 3d model position properties
286 me.source_view = view.views[view.indexof(VIEW_NAME)];
287 var src = me.source_view.getNode("config");
288 me.offset = {x: src.getNode("z-offset-m").getValue(),
289 y: src.getNode("x-offset-m").getValue(),
290 z: src.getNode("y-offset-m").getValue()};
291
292 me.view_distance = me.base.getNode("view-distance-m", 1);
293 me.view_distance.setDoubleValue(2.0*VIEW_DISTANCE);
294 me.view_angle = me.base.getNode("view-angle-deg", 1);
295 me.view_angle.setDoubleValue(0.0);
296
297 ## Instrument "display"
298 me.display = screen.display.new(20, 10);
299 me.display.format = "%2.4f";
300 me.display.add(me.altitude_deg,
301 props.globals.getNode("/sim/time/gmt"));
302
303 settimer(func { me.disable(); }, 0.0);
304
305 print("RAF Mk9 bubble sextant handling ... initialized");
306 },
307 ##################################################
308 toggle : func {
309 if (me.enabled) {
310 me.disable();
311 } else {
312 me.enable();
313 }
314 },
315 ##################################################
316 enable : func {
317 me.enabled = 1;
318 me.display.redraw();
319
320 me.loopid += 1;
321 me._loop_(me.loopid);
322 },
323 ##################################################
324 toggle_look_through : func {
325 if (!me.enabled) return;
326 if (!me.lookthrough) {
327 me.lookthrough = 1;
328
329 me.view_distance.setDoubleValue(VIEW_DISTANCE);
330 me.view_angle.setValue(0.0);
331
332 me.old_view = view.point.save();
333 setprop("/sim/current-view/field-of-view", FOV);
334 } else {
335 me.lookthrough = 0;
336 setprop("/sim/current-view/field-of-view",
337 me.old_view.getChild("field-of-view").getValue());
338 # view.point.restore();
339 me.view_angle.setValue(-90.0);
340 me.view_distance.setValue(2.0*VIEW_DISTANCE);
341 }
342 },
343 ##################################################
344 disable : func {
345 me.enabled = 0;
346 me.lookthrough = 0;
347 me.display.close();
348
349 sextant.set_position
350 (STOWED.position.x,
351 STOWED.position.y,
352 STOWED.position.z);
353 sextant.set_orientation
354 (STOWED.orientation.heading,
355 STOWED.orientation.pitch,
356 STOWED.orientation.roll);
357 me.view_distance.setDoubleValue(0.0);
358 me.view_angle.setValue(0.0);
359 },
360 ##################################################
361 update : func {
362 ## Move the 3d model.
363 if (view.current == me.source_view) {
364 var src = props.globals.getNode("/sim/current-view");
365 sextant.set_position
366 (src.getNode("z-offset-m").getValue() - me.offset.x,
367 src.getNode("x-offset-m").getValue() - me.offset.y,
368 src.getNode("y-offset-m").getValue() - me.offset.z);
369 var old = sextant.get_orientation();
370 sextant.set_orientation
371 (getprop("/sim/current-view/heading-offset-deg"),
372 getprop("/sim/current-view/pitch-offset-deg"),
373 old[2]);
374 me.altitude_deg.setValue(sextant.get_altitude());
375 }
376 },
377 ##################################################
378 _loop_ : func(id) {
379 id == me.loopid and me.enabled or return;
380 me.update();
381 settimer(func { me._loop_(id); }, me.UPDATE_INTERVAL, 1);
382 }
383 };
384
385 ###############################################################################
386 var _initialized = 0;
387 setlistener("/sim/signals/fdm-initialized", func {
388 if (!_initialized) {
389 handling.init();
390 _initialized = 1;
391 } else {
392 handling.disable();
393 }
394 });
395
396 ###############################################################################
397 ## Ugly matrix math as needed.
398 ## Probably horribly inefficient matrix representation:
399 ## M[row][col] = [[row1], [row2], [row3]]
400 ## v[row] = [x, y, z]
401
402 var X = [1, 0, 0];
403 var Y = [0, 1, 0];
404 var Z = [0, 0, 1];
405 var id = [X, Y, Z];
406
407 var mulMv = func(M, v) {
408 return [M[0][0]*v[0] + M[0][1]*v[1] + M[0][2]*v[2],
409 M[1][0]*v[0] + M[1][1]*v[1] + M[1][2]*v[2],
410 M[2][0]*v[0] + M[2][1]*v[1] + M[2][2]*v[2]];
411 }
412
413 var mulMM = func(A, B) {
414 return [[A[0][0]*B[0][0] + A[0][1]*B[1][0] + A[0][2]*B[2][0],
415 A[0][0]*B[0][1] + A[0][1]*B[1][1] + A[0][2]*B[2][1],
416 A[0][0]*B[0][2] + A[0][1]*B[1][2] + A[0][2]*B[2][2]],
417 [A[1][0]*B[0][0] + A[1][1]*B[1][0] + A[1][2]*B[2][0],
418 A[1][0]*B[0][1] + A[1][1]*B[1][1] + A[1][2]*B[2][1],
419 A[1][0]*B[0][2] + A[1][1]*B[1][2] + A[1][2]*B[2][2]],
420 [A[2][0]*B[0][0] + A[2][1]*B[1][0] + A[2][2]*B[2][0],
421 A[2][0]*B[0][1] + A[2][1]*B[1][1] + A[2][2]*B[2][1],
422 A[2][0]*B[0][2] + A[2][1]*B[1][2] + A[2][2]*B[2][2]]];
423 }
424
425 var scalar = func(a, b) {
426 return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
427 }
428
429 var absV = func(a) {
430 return math.sqrt(scalar(a, a));
431 }
432
433 var crossV = func(a, b) {
434 return [a[1]*b[2] - a[2]*b[1],
435 a[2]*b[0] - a[0]*b[2],
436 a[0]*b[1] - a[1]*b[0]];
437 }
438
439 var rotateX = func (r) {
440 return [[1, 0, 0],
441 [0, math.cos(-r), -math.sin(-r)],
442 [0, math.sin(-r), math.cos(-r)]];
443 }
444
445 var rotateY = func (r) {
446 return [[math.cos(-r), 0, math.sin(-r)],
447 [0, 1, 0],
448 [-math.sin(-r), 0, math.cos(-r)]];
449 }
450
451 var rotateZ = func (r) {
452 return [[math.cos(r), -math.sin(r), 0],
453 [math.sin(r), math.cos(r), 0],
454 [0, 0, 1]];
455 }
456
457 var printMat = func (m) {
458 foreach (var e; m) {
459 if (typeof(e) == "scalar") {
460 print(" " ~ e);
461 } else {
462 var line = "";
463 foreach (var ee; e) {
464 line = line ~ " " ~ ee;
465 }
466 print(line);
467 }
468 }
469 }
470
471 var angleV = func (a, b) {
472 return math.acos(math.abs(scalar(a,b)) / (absV(a) * absV(b)));
473 }
474
475 #print("id*id=");
476 #printMat(mulMM(id,id));
477
478 #print("X=");
479 #printMat(X);
480
481 #print("RotY(45)*X=");
482 #printMat(mulMv(rotateY(45*math.pi/180),X));
483
484 #print("Y*X= " ~scalar(Y,X));