Debian Codegraph @

Aircraft/c172p/Nasal/tiedowns.nas

raw
1 # Copyright (C) 2015 onox
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 # Position and offset of the start point of a tiedown. The position is on
17 # the surface of the fuselage or wing.
18 var point_coords = {
19 left: {
20 x: 0.1188,
21 y: -2.2998,
22 z: 0.4008,
23 offset: 90.0
24 },
25
26 right: {
27 x: 0.1188,
28 y: 2.2998,
29 z: 0.4008,
30 offset: -90.0
31 },
32
33 tail: {
34 x: 4.7116,
35 y: 0.0000,
36 z: -0.6663,
37 offset: 180.0
38 },
39 };
40
41 # Values must be the same as in Models/c172p.xml
42 var model_offsets_pitch_deg = -3.0;
43 var model_offsets_z_m = -0.065;
44
45 var TiedownPositionUpdater = {
46
47 new: func (name) {
48 var m = {
49 parents: [TiedownPositionUpdater]
50 };
51 m.loop = updateloop.UpdateLoop.new(components: [m], update_period: 0.0, enable: 0);
52 m.name = name;
53 return m;
54 },
55
56 enable: func {
57 me.loop.reset();
58 me.loop.enable();
59 },
60
61 disable: func {
62 me.loop.disable();
63 },
64
65 enable_or_disable: func (enable) {
66 if (enable)
67 me.enable();
68 else
69 me.disable();
70 },
71
72 reset: func {
73 me.end_point = geo.aircraft_position();
74 var heading = getprop("/orientation/heading-deg");
75
76 var x = getprop("/sim/model/c172p/tiedowns", me.name, "x");
77 var y = getprop("/sim/model/c172p/tiedowns", me.name, "y");
78
79 # Set position of end point
80 var course = heading + geo.normdeg(math_ext.atan(y, x));
81 var distance = math.sqrt(math.pow(x, 2) + math.pow(y, 2));
82 me.end_point.apply_course_distance(course, distance);
83
84 # Set altitude of end point
85 var elev_m = geo.elevation(me.end_point.lat(), me.end_point.lon());
86 me.end_point.set_alt(elev_m or getprop("/position/ground-elev-m"));
87
88 me.init_ref_length();
89 },
90
91 init_ref_length: func {
92 # Call update() to compute initial length
93 me.update(0);
94 var length = getprop("/sim/model/c172p/tiedowns", me.name, "length");
95 setprop("/sim/model/c172p/tiedowns", me.name, "ref-length", length);
96 },
97
98 update: func (dt) {
99 var point = point_coords[me.name];
100
101 var start_x = point.x;
102 var start_y = point.y;
103 var start_z = point.z + model_offsets_z_m;
104
105 var roll_deg = getprop("/orientation/roll-deg");
106 var pitch_deg = getprop("/orientation/pitch-deg") + model_offsets_pitch_deg;
107 var heading = getprop("/orientation/heading-deg");
108
109 # Compute the actual position of the start of the tiedown
110 var (start_point_2d, start_point) = math_ext.get_point(start_x, start_y, start_z, roll_deg, pitch_deg, heading);
111
112 var (yaw, pitch, distance) = math_ext.get_yaw_pitch_distance_inert(start_point_2d, start_point, me.end_point, heading);
113 (yaw, pitch) = math_ext.get_yaw_pitch_body(roll_deg, pitch_deg, yaw, pitch, point.offset);
114
115 setprop("/sim/model/c172p/tiedowns", me.name, "heading-deg", yaw);
116 setprop("/sim/model/c172p/tiedowns", me.name, "pitch-deg", pitch);
117 setprop("/sim/model/c172p/tiedowns", me.name, "length", distance);
118 },
119
120 };
121
122 var tiedown_left_updater = TiedownPositionUpdater.new("left");
123 var tiedown_right_updater = TiedownPositionUpdater.new("right");
124 var tiedown_tail_updater = TiedownPositionUpdater.new("tail");
125
126 setlistener("/sim/signals/fdm-initialized", func {
127 setlistener("/sim/model/c172p/securing/tiedownL-visible", func (node) {
128 tiedown_left_updater.enable_or_disable(node.getValue());
129 }, 1, 0);
130
131 setlistener("/sim/model/c172p/securing/tiedownR-visible", func (node) {
132 tiedown_right_updater.enable_or_disable(node.getValue());
133 }, 1, 0);
134
135 setlistener("/sim/model/c172p/securing/tiedownT-visible", func (node) {
136 tiedown_tail_updater.enable_or_disable(node.getValue());
137 }, 1, 0);
138
139 setlistener("/fdm/jsbsim/damage/repairing", func (node) {
140 # When the aircraft has been repaired (value is switched back
141 # to 0), compute the new initial length of the tiedowns
142 if (!node.getValue()) {
143 tiedown_left_updater.init_ref_length();
144 tiedown_right_updater.init_ref_length();
145 tiedown_tail_updater.init_ref_length();
146 }
147 }, 0, 0);
148 });