Debian Codegraph @

Nasal/towing/hitch.nas

raw
1 #
2 # TOWING NASAL CODE
3 #
4 # original version by D-NXKT up to version 30.12.2014
5 # updates by Bea Wolf and D-NXKT, version 11/2025
6 #
7 #
8 # Purpose of this routine:
9 # ------------------------
10 #
11 # - Create visible winch- and towropes for gliders and towplanes
12 # - Support of aerotowing and winch for JSBSim-aircraft (glider and towplanes)
13 #
14 # This routine is very similar to /FDM/YASim/Hitch.cpp
15 # Aerotowing is fully compatible to the YASim functionality.
16 # This means that YASim-gliders could be towed by JSBSim-aircraft and vice versa.
17 # Setup-instructions with copy and paste examples are given below:
18 #
19 #
20 # Setup of visible winch/towropes for Yasim-aircraft:
21 # ----------------------------------------------------
22 #
23 # YASim-aircraft with winch/aerotowing functionality should work out of the box.
24 # Optional you can customize the rope-diameter by adding the following to "your_aircraft-set.xml":
25 # </sim>
26 # <hitches>
27 # <aerotow>
28 # <rope>
29 # <rope-diameter-mm type ="float">10</rope-diameter-mm>
30 # </rope>
31 # </aerotow>
32 # <winch>
33 # <rope>
34 # <rope-diameter-mm type ="float">20</rope-diameter-mm>
35 # </rope>
36 # </winch>
37 # </hitches>
38 # </sim>
39 #
40 # That's all!
41 #
42 #
43 #
44 # Support of aerotowing and winch for JSBSim-aircraft (glider and towplanes):
45 # ----------------------------------------------------------------------------
46 #
47 # 1. Define a hitch in the JSBSim-File. Coordinates according to JSBSims structural frame of reference
48 # (x points to the tail, y points to the right wing, z points upwards).
49 # Unit must be "LBS", frame must be "BODY". The force name is arbitrary.
50 #
51 # <external_reactions>
52 # <force name="hitch" frame="BODY" unit="LBS" >
53 # <location unit="M">
54 # <x>3.65</x>
55 # <y> 0.0</y>
56 # <z>-0.12</z>
57 # </location>
58 # <direction>
59 # <x>0.0</x>
60 # <y>0.0</y>
61 # <z>0.0</z>
62 # </direction>
63 # </force>
64 # </external_reactions>
65
66
67 # 2. Define controls for aerotowing and winch.
68 # Add the following key bindings in "yourAircraft-set.xml":
69 # <input>
70 # <keyboard>
71 #
72 # <key n="15">
73 # <name>Ctrl-o</name>
74 # <desc>Find aircraft for aerotow</desc>
75 # <binding>
76 # <command>nasal</command>
77 # <script>towing.findBestAIObject()</script>
78 # </binding>
79 # </key>
80 #
81 # <key n="111">
82 # <name>o</name>
83 # <desc>Lock aerotow-hook</desc>
84 # <binding>
85 # <command>nasal</command>
86 # <script>towing.closeHitch()</script>
87 # </binding>
88 # </key>
89 #
90 # <key n="79">
91 # <name>O</name>
92 # <desc>Open aerotow-hook</desc>
93 # <binding>
94 # <command>nasal</command>
95 # <script>towing.releaseHitch("aerotow")</script>
96 # </binding>
97 # </key>
98 #
99 # <key n="23">
100 # <name>Ctrl-w</name>
101 # <desc>Place Winch and hook in</desc>
102 # <binding>
103 # <command>nasal</command>
104 # <script>towing.setWinchPositionAuto()</script>
105 # </binding>
106 # </key>
107 #
108 # <key n="119">
109 # <name>w</name>
110 # <desc>Start winch</desc>
111 # <binding>
112 # <command>nasal</command>
113 # <script>towing.runWinch()</script>
114 # </binding>
115 # </key>
116 #
117 # <key n="87">
118 # <name>W</name>
119 # <desc>Open winch-hook</desc>
120 # <binding>
121 # <command>nasal</command>
122 # <script>towing.releaseHitch("winch")</script>
123 # </binding>
124 # </key>
125 #
126 # </keyboard>
127 # </input>
128 #
129 # For towplanes only "key n=79" (Open aerotow-hook) is required!
130
131
132 # 3. Set mandatory properties:
133 #<sim>
134 # <hitches>
135 # <aerotow>
136 # <force_name_jsbsim type="string">hitch</force_name_jsbsim>
137 # <force-is-calculated-by-other type="bool">false</force-is-calculated-by-other>
138 # <mp-auto-connect-period type="float">0.0</mp-auto-connect-period>
139 # <!-- OPTIONAL
140 # <decoupled-force-and-rope-locations type="bool">true</decoupled-force-and-rope-locations>
141 # <local-pos-x type="float">1.5</local-pos-x>
142 # <local-pos-y type="float"> 0.00</local-pos-y>
143 # <local-pos-z type="float">-0.3</local-pos-z>
144 # -->
145 # </aerotow>
146 # <winch>
147 # <force_name_jsbsim type="string">hitch</force_name_jsbsim>
148 # <!-- OPTIONAL
149 # <decoupled-force-and-rope-locations type="bool">true</decoupled-force-and-rope-locations>
150 # <local-pos-x type="float">0.0</local-pos-x>
151 # <local-pos-y type="float">0.0</local-pos-y>
152 # <local-pos-z type="float">0.0</local-pos-z>
153 # -->
154 # </winch>
155 # </hitches>
156 #</sim>
157 #
158 # "force_name_jsbsim" must be the external force name in JSBSim.
159 # "force-is-calculated-by-other" should be "false" for gliders and "true" for tow planes.
160 # "mp-auto-connect-period" is only needed for tow planes and should be "1".
161 #
162 # IMPORTANT:
163 # The hitch location is stored twice in the property tree (for tow force and for rope animation).
164 # This is necessary to keep the towrope animation compatible to YASim-aircraft.
165 # The hitch location for the tow force is stored in "fdm/jsbsim/external_reactions/hitch/location-x(yz)-in" and for the
166 # animated towrope in "sim/hitches/aerotow(winch)/local-pos-x(yz)".
167 # By default only values for the tow force location have to be defined. The values for the towrope location are set
168 # automatically (decoupled-force-and-rope-locations is "false" by default).
169 # It is feasible to use different locations for the force and rope. In order to do this, you have to set
170 # "decoupled-force-and-rope-locations" to "true" and provide values for "sim/hitches/aerotow(winch)/local-pos-x(yz)".
171 # Note that the frame of reference is different. Here the coordinates for the "YASim-System" are needed
172 # (x points to the nose, y points to the left wing, z points upwards).
173
174
175 # 4. Set optional properties:
176 #
177 # Only aircraft-specific properties should be set by the aircraft. All other options can be adjusted by the
178 # winch GUI dialog and are stored by this script itself
179 #
180 # Aircraft-specific properties are
181 # * weak link (aerotow/winch)
182 # sim/hitches/aerotow/tow/break-force [N]
183 # sim/hitches/winch/tow/break-force [N]
184 # * typical tow speed (winch)
185 # sim/hitches/winch/typical-tow-speed-kph [kph]
186 # * automatic release angle (winch)
187 # sim/hitches/winch/automatic-release-angle-deg [deg]
188 #
189 #<sim>
190 # <hitches>
191 # <aerotow>
192 # <tow>
193 # <break-force type="float">6000</break-force>
194 # </tow>
195 # </aerotow>
196 # <winch>
197 # <automatic-release-angle-deg type="float">70.</automatic-release-angle-deg>
198 # <typical-tow-speed-kph type="float">100</typical-tow-speed-kph>
199 # <typical-tow-force-N type="float">5000</typical-tow-force-N>
200 # <tow>
201 # <break-force type="float">10000</break-force>
202 # </tow>
203 # </winch>
204 # </hitches>
205 #<sim>
206 #
207 # That's it!
208 #
209 #
210 #
211 # Setup of hitch position for AI-aircraft:
212 # ----------------------------------------------------
213 #
214 # For the visualization of the tow rope, it is advantageous if a hitch position is defined for the AI tow plane.
215 # To set the hitch position for AI aircraft, the following must be added to the AI-Aircraft.xml file at the beginning:
216 #
217 #<?xml version="1.0"?>
218 #<PropertyList>
219 # <nasal>
220 # <load>
221 # var rplayer = cmdarg();
222 # rplayer.getNode("sim/hitches/aerotow/local-pos-x", 1).setValue("x-location");
223 # rplayer.getNode("sim/hitches/aerotow/local-pos-y", 1).setValue("y-location");
224 # rplayer.getNode("sim/hitches/aerotow/local-pos-z", 1).setValue("z-location");
225 # </load>
226 # <unload>
227 # rplayer.getNode("sim/hitches").remove();
228 # </unload>
229 # </nasal>
230 # Select suitable values for the location of your hitch position (x/y/z-location).
231 # End of modification. From here on, the file continues unchanged.
232 # <path>your_AI_Aircraft.ac</path>
233 # ...
234 #
235 # That's it! The tow towrope ends at the defined position.
236
237
238 ################################################## general info ############################################
239 #
240 # 3 different types of towplanes could exist: AI-plane, MP-plane without interaction, MP-plane with interaction.
241 # AI-planes are identified by the node "ai/models/aircraft/" or "ai/models/dragger".
242 # MP-planes (interactice/non-interactive) are identified by the existence of node "ai/models/multiplayer".
243 # Interactive MP-plane: variables in node "ai/models/multiplayer/sim/hitches/" are updated.
244 # Non-interactive MP-plane: variables are not updated (values are either not defined or have "wrong" values
245 # from a former owner of this node.
246 #
247 # The following properties are transmitted in multiplayer:
248 # "sim/hitches/aerotow/tow/elastic-constant"
249 # "sim/hitches/aerotow/tow/weight-per-m-kg-m"
250 # "sim/hitches/aerotow/tow/dist"
251 # "sim/hitches/aerotow/tow/connected-to-property-node"
252 # "sim/hitches/aerotow/tow/connected-to-ai-or-mp-callsign"
253 # "sim/hitches/aerotow/tow/break-force"
254 # "sim/hitches/aerotow/tow/end-force-x"
255 # "sim/hitches/aerotow/tow/end-force-y"
256 # "sim/hitches/aerotow/tow/end-force-z"
257 # "sim/hitches/aerotow/is-slave"
258 # "sim/hitches/aerotow/speed-in-tow-direction"
259 # "sim/hitches/aerotow/open", open);
260 # "sim/hitches/aerotow/local-pos-x"
261 # "sim/hitches/aerotow/local-pos-y"
262 # "sim/hitches/aerotow/local-pos-z"
263 #
264 ##############################################################################################################
265
266
267
268
269 # ######################################################################################################################
270 # check, if towing support makes sense
271 # ######################################################################################################################
272
273 # Check if node "sim/hitches" is defined. If not, return!
274 if (props.globals.getNode("sim/hitches") == nil ) return;
275 print("towing is active!");
276
277
278 # ######################################################################################################################
279 # set defaults / initialize at startup
280 # ######################################################################################################################
281
282 # set defaults for properties that are NOT already defined
283
284 var fdm = getprop("/sim/flight-model") or "";
285
286 if ( !(fdm == "jsb" or fdm == "yasim") ){
287 print("Unsupported FDM for towing");
288 return;
289 }
290
291 var check_or_create = func ( name, value, type ) {
292 if ( getprop( name ) == nil ){
293 return props.globals.initNode(name, value, type);
294 } else {
295 return props.globals.getNode(name);
296 }
297 }
298
299 # Load stored settings
300 var config_file = getprop("/sim/fg-home") ~ "/Export/hitch-config.xml";
301 if( io.stat( config_file ) != nil ){
302 var config_properties = io.read_properties( config_file );
303 var config_file_valid = 1;
304 } else {
305 var config_file_valid = 0;
306 }
307
308 var new_config_property = func( path, value, type, cfg_p ){
309 var p = getprop( path );
310 if( p != nil ){ # the property has been set by either aircraft-set.xml or stored aircraft-data
311 # print( "Load from aircraft-set.xml or stored aircraft-data "~ path);
312 return props.globals.getNode( path );
313 } else {
314 # print( "initialize property "~ path );
315 p = props.globals.initNode( path, value, type ); # initialize property with default values
316 if( config_file_valid ){
317 var cnfg_node = config_properties.getNode( cfg_p );
318 if( cnfg_node != nil ){
319 # If there is a valid saved configuration (Export/hitch-config.xml) for this property, load it
320 # print( "Load from global configuration "~ path );
321 p.setValue( cnfg_node.getValue() );
322 }
323 }
324 return p;
325 }
326 }
327
328 var config = {
329 aerotow: {
330 elastic_constant: new_config_property("sim/hitches/aerotow/tow/elastic-constant", 9111.0, "DOUBLE", "aerotow/elastic-constant"),
331 weight_per_m_kg_m: new_config_property("sim/hitches/aerotow/tow/weight-per-m-kg-m", 0.35, "DOUBLE", "aerotow/weight-per-m-kg-m"),
332 length: new_config_property("sim/hitches/aerotow/tow/length", 60.0, "DOUBLE", "aerotow/length"),
333 path_to_model: new_config_property("sim/hitches/aerotow/rope/path_to_model", "Models/Aircraft/towropes.xml", "STRING", "aerotow/path_to_model"),
334 rope_diameter_mm: new_config_property("sim/hitches/aerotow/rope/rope-diameter-mm", 20.0, "DOUBLE", "aerotow/rope-diameter-mm"),
335 },
336 winch: {
337 elastic_constant: new_config_property("sim/hitches/winch/tow/elastic-constant", 40001.0, "DOUBLE", "winch/elastic-constant"),
338 weight_per_m_kg_m: new_config_property("sim/hitches/winch/tow/weight-per-m-kg-m", 0.1, "DOUBLE", "winch/weight-per-m-kg-m"),
339 initial_tow_length: new_config_property("sim/hitches/winch/winch/initial-tow-length-m", 1000.0, "DOUBLE", "winch/initial-tow-length-m"),
340 path_to_model: new_config_property("sim/hitches/winch/rope/path_to_model", "Models/Aircraft/towropes.xml", "STRING", "winch/path_to_model"),
341 rope_diameter_mm: new_config_property("sim/hitches/winch/rope/rope-diameter-mm", 20.0, "DOUBLE", "winch/rope-diameter-mm"),
342
343 rope_breakage: {
344 height: new_config_property("sim/hitches/winch/breakage/height", 100.0, "DOUBLE", "winch/breakage/height"),
345 random: new_config_property("sim/hitches/winch/breakage/random", 0, "BOOL", "winch/breakage/random"),
346 },
347 loss_of_power: {
348 height: new_config_property("sim/hitches/winch/loss-of-power/height", 100.0, "DOUBLE", "winch/loss-of-power/height"),
349 random: new_config_property("sim/hitches/winch/loss-of-power/random", 0, "BOOL", "winch/loss-of-power/random"),
350 },
351 max_tow_length_m: new_config_property("sim/hitches/winch/winch/max-tow-length-m", 1500.0, "DOUBLE", "winch/max-tow-length-m"),
352 max_spool_speed: new_config_property("sim/hitches/winch/winch/max-spool-speed-m-s", 40.0, "DOUBLE", "winch/max-spool-speed-m-s"),
353
354 force_acceleration: new_config_property("sim/hitches/winch/winch/force-acceleration-N-s", 1000.0, "DOUBLE", "winch/force-acceleration-N-s"),
355 spool_acceleration: new_config_property("sim/hitches/winch/winch/spool-acceleration-m-s-s", 8.0, "DOUBLE", "winch/spool-acceleration-m-s-s"),
356
357 max_unspool_speed: new_config_property("sim/hitches/winch/winch/max-unspool-speed-m-s", 40.0, "DOUBLE", "winch/max-unspool-speed-m-s"),
358 max_force: new_config_property("sim/hitches/winch/winch/max-force-N", 10000.0, "DOUBLE", "winch/max-force-N"),
359 max_power: new_config_property("sim/hitches/winch/winch/max-power-kW", 123.0, "DOUBLE", "winch/max-power-kW"),
360 magic_constant: new_config_property("sim/hitches/winch/winch/magic-constant", 500.0, "DOUBLE", "winch/magic-constant"),
361
362 messages: {
363 launch_signaller: new_config_property("/sim/hitches/winch/messages/launch-signaller", 1, "BOOL", "winch/messages/launch-signaller"),
364 winch_driver: new_config_property("/sim/hitches/winch/messages/winch-driver", 1, "BOOL", "winch/messages/winch-driver"),
365 pilot: new_config_property("/sim/hitches/winch/messages/pilot", 1, "BOOL", "winch/messages/pilot"),
366 remote_ac: new_config_property("/sim/hitches/winch/messages/remote-ac", 1, "BOOL", "winch/messages/remote-ac"),
367 },
368 },
369 };
370
371 var write_config = func {
372 var c = props.Node.new();
373 var a = c.addChild("aerotow");
374 var w = c.addChild("winch");
375
376 a.initNode("elastic-constant").setDoubleValue( config.aerotow.elastic_constant.getDoubleValue() );
377 a.initNode("weight-per-m-kg-m").setDoubleValue( config.aerotow.weight_per_m_kg_m.getDoubleValue() );
378 a.initNode("length").setDoubleValue( config.aerotow.length.getDoubleValue() );
379 a.initNode("path_to_model","","STRING").setValue( config.aerotow.path_to_model.getValue() );
380 a.initNode("rope-diameter-mm").setDoubleValue( config.aerotow.rope_diameter_mm.getDoubleValue() );
381
382 w.initNode("elastic-constant").setDoubleValue( config.winch.elastic_constant.getDoubleValue() );
383 w.initNode("weight-per-m-kg-m").setDoubleValue( config.winch.weight_per_m_kg_m.getDoubleValue() );
384 w.initNode("initial-tow-length-m").setDoubleValue( config.winch.initial_tow_length.getDoubleValue() );
385 w.initNode("path_to_model","","STRING").setValue( config.winch.path_to_model.getValue() );
386 w.initNode("rope-diameter-mm").setDoubleValue( config.winch.rope_diameter_mm.getDoubleValue() );
387 w.initNode("breakage/height").setDoubleValue(config.winch.rope_breakage.height.getDoubleValue() );
388 w.initNode("breakage/random",0,"BOOL").setBoolValue(config.winch.rope_breakage.random.getBoolValue() );
389 w.initNode("loss-of-power/height").setDoubleValue(config.winch.loss_of_power.height.getDoubleValue() );
390 w.initNode("loss-of-power/random",0,"BOOL").setBoolValue(config.winch.loss_of_power.random.getBoolValue() );
391 w.initNode("max-tow-length-m").setDoubleValue( config.winch.max_tow_length_m.getDoubleValue() );
392 w.initNode("max-spool-speed-m-s").setDoubleValue( config.winch.max_spool_speed.getDoubleValue() );
393 w.initNode("force-acceleration-N-s").setDoubleValue( config.winch.force_acceleration.getDoubleValue() );
394 w.initNode("spool-acceleration-m-s-s").setDoubleValue( config.winch.spool_acceleration.getDoubleValue() );
395 w.initNode("max-unspool-speed-m-s").setDoubleValue( config.winch.max_unspool_speed.getDoubleValue() );
396 w.initNode("max-force-N").setDoubleValue( config.winch.max_force.getDoubleValue() );
397 w.initNode("max-power-kW").setDoubleValue( config.winch.max_power.getDoubleValue() );
398 w.initNode("magic-constant").setDoubleValue( config.winch.magic_constant.getDoubleValue() );
399 w.initNode("messages/launch-signaller",1,"BOOL").setBoolValue( config.winch.messages.launch_signaller.getBoolValue() );
400 w.initNode("messages/winch-driver",1,"BOOL").setBoolValue( config.winch.messages.winch_driver.getBoolValue() );
401 w.initNode("messages/pilot",1,"BOOL").setBoolValue( config.winch.messages.pilot.getBoolValue() );
402 w.initNode("messages/remote-ac",1,"BOOL").setBoolValue( config.winch.messages.remote_ac.getBoolValue() );
403
404 io.write_properties( config_file, c );
405 }
406
407 var aircraft_settings = {
408 aerotow: {
409 force_calc_by_other: check_or_create("sim/hitches/aerotow/force-is-calculated-by-other", 0, "BOOL"), # aircraft-specific
410 is_slave: check_or_create("sim/hitches/aerotow/is-slave", 0, "BOOL"), # aircraft-specific
411 break_force: check_or_create("sim/hitches/aerotow/tow/break-force", 12345.0, "DOUBLE"), # aircraft-specific
412 local_pos: [
413 check_or_create("sim/hitches/aerotow/local-pos-x", 0.0, "DOUBLE"), # aircraft-specific
414 check_or_create("sim/hitches/aerotow/local-pos-y", 0.0, "DOUBLE"), # aircraft-specific
415 check_or_create("sim/hitches/aerotow/local-pos-z", 0.0, "DOUBLE"), # aircraft-specific
416 ],
417 },
418 winch: {
419 typical_tow_speed: props.globals.getNode("sim/hitches/winch/typical-tow-speed-kph"),
420 typical_tow_force: props.globals.getNode("sim/hitches/winch/typical-tow-force-N"),
421 # Winch type: 0 = tow with constant force; 1 = tow with constant rope speed; aircraft-specific
422 type_p: check_or_create("sim/hitches/winch/type", 0, "INT"),
423 break_force: check_or_create("sim/hitches/winch/tow/break-force", 12345.0, "DOUBLE"), # aircraft-specific
424 local_pos: [
425 check_or_create("sim/hitches/winch/local-pos-x", 0.0, "DOUBLE"), # aircraft-specific
426 check_or_create("sim/hitches/winch/local-pos-y", 0.0, "DOUBLE"), # aircraft-specific
427 check_or_create("sim/hitches/winch/local-pos-z", 0.0, "DOUBLE"), # aircraft-specific
428 ],
429 },
430 };
431
432 var check_aircraft_tow_settings = func{
433 if( aircraft_settings.winch.typical_tow_speed == nil ){
434 aircraft_settings.winch.typical_tow_speed = props.globals.initNode( "sim/hitches/winch/typical-tow-speed-kph" );
435
436 # Estimate typical tow speed from aircraft weight, this will inherently be a rough guess
437 # Estimation basis:
438 # type | typical tow speed | typical weight | reference
439 # hang glider | 50 kph | 100 kg | https://www.safa.asn.au/resources/Tow%20Manual%20v5.4.1.pdf
440 # Ka6 | 90 kph | 250 kg | http://www.smbc-eferding.at/wp-content/uploads/2014/01/Flughandbuch-Ka-6.pdf
441 # LS8 | 120 kph | 500 kg | https://www.sglenzburg.ch/org/public/Dokumente/SGL/10-AFM/fhb_ls8a_rev3_tm8020.pdf
442 # DG-1000S | 130 kph | 650 kg | http://adelaidesoaring.on.net/wp-content/uploads/2014/01/DG-1000s-Flight-Manual.pdf
443 #
444 # a quadratic regression for this data leads to y = -0.0002x2 + 0.3286x + 20.839, we use (simplified): f(x) = -0.0002 * x^2 + 0.33 * x + 21
445 var weight_kg = weight_lb.getDoubleValue() * LB2KG;
446 aircraft_settings.winch.typical_tow_speed.setDoubleValue( -0.0002 * weight_kg * weight_kg + 0.33 * weight_kg + 21 );
447 }
448 if( aircraft_settings.winch.typical_tow_force == nil ){
449 aircraft_settings.winch.typical_tow_force = props.globals.initNode( "sim/hitches/winch/typical-tow-force-N" );
450
451 aircraft_settings.winch.typical_tow_force.setDoubleValue( 0.15 * math.pow( weight_lb.getDoubleValue(), 1.25 ) * LB2KG * 9.81);
452 }
453 }
454
455 # yasim properties for aerotow (should be already defined for yasim aircraft but not for JSBSim aircraft
456 var aerotow_hash = {
457 broken: props.globals.initNode("sim/hitches/aerotow/broken", 0, "BOOL"),
458 force: props.globals.initNode("sim/hitches/aerotow/force", 0.0, "DOUBLE"),
459 mp_auto_connect_period: props.globals.initNode("sim/hitches/aerotow/mp-auto-connect-period", 0.0, "DOUBLE"),
460 mp_time_lag: props.globals.initNode("sim/hitches/aerotow/mp-time-lag", 0.0, "DOUBLE"),
461 open: props.globals.initNode("sim/hitches/aerotow/open", 1, "BOOL"), #always init to true
462 speed_tow_direction: props.globals.initNode("sim/hitches/aerotow/speed-in-tow-direction", 0.0, "DOUBLE"),
463 old_open: props.globals.initNode("sim/hitches/aerotow/oldOpen", 1, "BOOL"),
464 tow: {
465 conn_ai_node: props.globals.initNode("sim/hitches/aerotow/tow/connected-to-ai-node", 0, "BOOL"),
466 conn_ai_or_mp_callsign: props.globals.initNode("sim/hitches/aerotow/tow/connected-to-ai-or-mp-callsign", "", "STRING"),
467 conn_ai_or_mp_id: props.globals.initNode("sim/hitches/aerotow/tow/connected-to-ai-or-mp-id", 0, "INT"),
468 conn_mp_node: props.globals.initNode("sim/hitches/aerotow/tow/connected-to-mp-node", 0, "BOOL"),
469 conn_prop_node: props.globals.initNode("sim/hitches/aerotow/tow/connected-to-property-node", 0, "BOOL"),
470 dist: props.globals.initNode("sim/hitches/aerotow/tow/dist", 0.0, "DOUBLE"),
471 end_force: [
472 props.globals.initNode("sim/hitches/aerotow/tow/end-force-x", 0.0, "DOUBLE"),
473 props.globals.initNode("sim/hitches/aerotow/tow/end-force-y", 0.0, "DOUBLE"),
474 props.globals.initNode("sim/hitches/aerotow/tow/end-force-z", 0.0, "DOUBLE"),
475 ],
476 node: props.globals.initNode("sim/hitches/aerotow/tow/node", "", "STRING"),
477 length: props.globals.initNode("sim/hitches/aerotow/tow/length", 60.0, "DOUBLE"),
478 },
479 rope: {
480 exist: props.globals.initNode("sim/hitches/aerotow/rope/exist", 0, "BOOL"),
481 model_id: props.globals.initNode("sim/hitches/aerotow/rope/model_id", -1, "INT"),
482
483 # Created on demand in the `createTowrope` function:
484 lat : nil,
485 lon : nil,
486 alt : nil,
487 hdg : nil,
488 pitch: nil,
489 },
490 };
491
492 # yasim properties for winch (should already be defined for yasim aircraft but not for JSBSim aircraft
493 var winch_hash = {
494 open: props.globals.initNode("sim/hitches/winch/open", 1, "BOOL"), #always init to true
495 broken: props.globals.initNode("sim/hitches/winch/broken", 0, "BOOL"),
496 type: 0,
497 rope_breakage_p: props.globals.initNode("sim/hitches/winch/breakage/enabled", 0, "BOOL"), # NOT saved in config: Must be selected explicitly each time
498 rope_breakage: 0,
499 rope_breakage_height_int: 100.0,
500 loss_of_power: {
501 enabled_p: props.globals.initNode("sim/hitches/winch/loss-of-power/enabled", 0, "BOOL"), # NOT saved in config: Must be selected explicitly each time
502 enabled: 0,
503 height_int: 100.0,
504 },
505 global_pos: [
506 props.globals.initNode("sim/hitches/winch/winch/global-pos-x", 0.0, "DOUBLE"),
507 props.globals.initNode("sim/hitches/winch/winch/global-pos-y", 0.0, "DOUBLE"),
508 props.globals.initNode("sim/hitches/winch/winch/global-pos-z", 0.0, "DOUBLE"),
509 ],
510 tow: {
511 length: props.globals.initNode("sim/hitches/winch/tow/length", 0.0, "DOUBLE"),
512 dist: props.globals.initNode("sim/hitches/winch/tow/dist", 0.0, "DOUBLE"),
513 },
514 old_open: props.globals.initNode("sim/hitches/winch/oldOpen", 1, "BOOL"),
515 rope: {
516 exist: props.globals.initNode("sim/hitches/winch/rope/exist", 0, "BOOL"),
517 model_id: props.globals.initNode("sim/hitches/winch/rope/model_id", -1, "INT"),
518
519 # Created on demand in the `createTowrope` function:
520 lat : nil,
521 lon : nil,
522 alt : nil,
523 hdg : nil,
524 pitch: nil,
525 },
526 };
527
528 var force_setting = 0.0; # used with constant force type winch
529 var speed_setting = 0.0; # used with constant speed type winch
530
531
532 if ( fdm == "jsb" ) {
533 # new properties for JSBSim aerotow
534 aircraft_settings.aerotow.force_name_jsbsim = check_or_create("sim/hitches/aerotow/force_name_jsbsim", "hitch", "STRING"); # aircraft-specific
535 aircraft_settings.aerotow.decoupled_locations = check_or_create("sim/hitches/aerotow/decoupled-force-and-rope-locations", 0, "BOOL"); # aircraft-specific
536 aircraft_settings.winch.force_name_jsbsim = check_or_create("sim/hitches/winch/force_name_jsbsim", "hitch", "STRING"); # aircraft-specific
537 aircraft_settings.winch.automatic_release_angle = check_or_create("sim/hitches/winch/automatic-release-angle-deg", 361.0, "DOUBLE"); # aircraft-specific
538 aircraft_settings.winch.decoupled_locations = check_or_create("sim/hitches/winch/decoupled-force-and-rope-locations", 0, "BOOL"); # aircraft-specific
539
540 aerotow_hash.mp_old_open = props.globals.initNode("sim/hitches/aerotow/mp_oldOpen", 1, "BOOL");
541 aerotow_hash.tow.mp_last_reported_dist = props.globals.initNode("sim/hitches/aerotow/tow/mp_last_reported_dist", 0.0, "DOUBLE");
542
543 # new properties for JSBSim winch
544 winch_hash.clutched = props.globals.initNode("sim/hitches/winch/winch/clutched", 0, "BOOL");
545 winch_hash.actual_spool_speed = props.globals.initNode("sim/hitches/winch/winch/actual-spool-speed-m-s", 0.0, "DOUBLE");
546
547 winch_hash.actual_force = props.globals.initNode("sim/hitches/winch/winch/actual-force-N", 0.0, "DOUBLE");
548
549 var hitchname = aircraft_settings.winch.force_name_jsbsim.getValue();
550 winch_hash.apply_force = {
551 magnitude: props.globals.initNode("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/magnitude", 0.0, "DOUBLE"),
552 coord: [
553 check_or_create("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/x", 0.0, "DOUBLE"), # aircraft-specific
554 check_or_create("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/y", 0.0, "DOUBLE"), # aircraft-specific
555 check_or_create("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/z", 0.0, "DOUBLE"), # aircraft-specific
556 ],
557 };
558
559 var hitchname = aircraft_settings.aerotow.force_name_jsbsim.getValue();
560 aerotow_hash.apply_force = {
561 magnitude: props.globals.initNode("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/magnitude", 0.0, "DOUBLE"),
562 coord: [
563 check_or_create("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/x", 0.0, "DOUBLE"), # aircraft-specific
564 check_or_create("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/y", 0.0, "DOUBLE"), # aircraft-specific
565 check_or_create("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/z", 0.0, "DOUBLE"), # aircraft-specific
566 ],
567 };
568
569 # consider older JSBSim-versions which do NOT provide the locations of external_reactions in the property tree
570 if( getprop("fdm/jsbsim/external_reactions/" ~ aircraft_settings.aerotow.force_name_jsbsim.getValue() ~ "/location-x-in") == nil ) {
571 aircraft_settings.aerotow.decoupled_locations.setBoolValue( 1 );
572 }
573 if( getprop("fdm/jsbsim/external_reactions/" ~ aircraft_settings.winch.force_name_jsbsim.getValue() ~ "/location-x-in") == nil ) {
574 aircraft_settings.winch.decoupled_locations.setBoolValue( 1 );
575 }
576
577
578 setlistener(aircraft_settings.winch.force_name_jsbsim , func() {
579 var hitchname = aircraft_settings.winch.force_name_jsbsim.getValue();
580 set_force_apply( winch_hash, hitchname );
581 });
582 setlistener(aircraft_settings.aerotow.force_name_jsbsim , func() {
583 var hitchname = aircraft_settings.aerotow.force_name_jsbsim.getValue();
584 set_force_apply( aerotow_hash, hitchname );
585 });
586 }
587
588
589 var callsign = check_or_create("sim/multiplay/callsign", "", "STRING"); # session-specific
590
591 var message_pilot = props.globals.getNode("sim/messages/pilot", 1);
592 var message_ai = props.globals.getNode("sim/messages/ai-plane", 1);
593 var message_atc = props.globals.getNode("sim/messages/atc", 1);
594 var write_message = func( type, message ){
595 if( type == "launch-signaller" ){
596 if( config.winch.messages.launch_signaller.getBoolValue() ) message_atc.setValue( message );
597 } elsif( type == "winch-driver" ){
598 if( config.winch.messages.winch_driver.getBoolValue() ) message_ai.setValue( message );
599 } elsif( type == "pilot" ){
600 if( config.winch.messages.pilot.getBoolValue() ) message_pilot.setValue( message );
601 } elsif( type == "remote-ac" ){
602 if( config.winch.messages.remote_ac.getBoolValue() ) message_ai.setValue( message );
603 } elsif( type == "system" ){
604 message_atc.setValue( message );
605 } else {
606 screen.log.write( message ~ " [type: unknown]");
607 }
608 }
609
610 var delta_time = props.globals.getNode("sim/time/delta-sec", 1);
611
612 var orientation = [
613 props.globals.getNode("orientation/heading-deg", 1),
614 props.globals.getNode("orientation/roll-deg", 1),
615 props.globals.getNode("orientation/pitch-deg", 1),
616 ];
617
618 var wind_from = check_or_create("environment/wind-from-heading-deg", 0.0, "DOUBLE");
619 var wind_speed_kt = check_or_create("environment/wind-speed-kt", 0.0, "DOUBLE");
620
621 var set_force_apply = func ( hash, hitchname ){
622 hash.apply_force = {
623 magnitude: check_or_create("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/magnitude", 0.0, "DOUBLE"),
624 coord: [
625 check_or_create("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/x", 0.0, "DOUBLE"),
626 check_or_create("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/y", 0.0, "DOUBLE"),
627 check_or_create("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/z", 0.0, "DOUBLE"),
628 ],
629 };
630 }
631
632
633 var loss_of_power = func () {
634 # The maximum power is lost to 1/10 of it over a time span of 3 +- 1 seconds
635 interpolate( config.winch.max_power, config.winch.max_power.getDoubleValue() / 10, 2 + 2 * rand() );
636 # Afterwards it is completely lost after another 2 +- 1 seconds
637 settimer( func() {
638 interpolate( config.winch.max_power, 0, 1 + 2 * rand() );
639 }, 3);
640 }
641
642
643 # ######################################################################################################################
644 # main function
645 # ######################################################################################################################
646
647 var towing = func {
648
649
650 var dt = 0;
651
652 # ------------------------------- aerotow part -------------------------------
653
654 var open = aerotow_hash.open.getBoolValue();
655 var oldOpen = aerotow_hash.old_open.getBoolValue();
656
657 if ( open != oldOpen ) { # check if my hitch state has changed, if yes: message
658
659 if ( !open ) { # my hitch was open and is closed now
660 if ( fdm == "jsb" ) {
661 var distance = aerotow_hash.tow.dist.getDoubleValue();
662 var towlength_m = aerotow_hash.tow.length.getDoubleValue();
663 if ( distance > towlength_m * 1.0001 ) {
664 write_message( "system", sprintf("Could not lock hitch (tow length is insufficient) on hitch %i!", aerotow_hash.tow.conn_mp_node.getIntValue()) );
665 aerotow_hash.open.setBoolValue( 1 ); # open my hitch again
666 } else { # mp aircraft to far away
667 # my hitch is closed
668 write_message( "system", sprintf("Locked hitch aerotow %i!", aerotow_hash.tow.conn_mp_node.getIntValue()) );
669 }
670 aerotow_hash.broken.setBoolValue(0);
671 } # end: JSBSim
672 if ( !aerotow_hash.open.getBoolValue() ) {
673 # setup ai-towrope
674 createTowrope("aerotow");
675
676 # set default hitch coordinates (needed for Ai- and non-interactive MP aircraft)
677 setAIObjectDefaults() ;
678 }
679 } # end hitch is closed
680
681 if ( open ) { # my hitch is now open
682 if ( fdm == "jsb" ) {
683 if ( aerotow_hash.broken.getBoolValue() ) {
684 #getprop("sim/hitches/aerotow/broken")
685 write_message( "pilot", "Oh no, the tow is broken" );
686 } else {
687 write_message( "pilot", sprintf("Opened hitch aerotow %i!", aerotow_hash.tow.conn_mp_node.getIntValue() ) );
688 }
689 releaseHitch("aerotow"); # open=1 / forces=0
690 } # end: JSBSim
691 removeTowrope("aerotow"); # remove towrope model
692 } # end hitch is open
693
694 aerotow_hash.old_open.setBoolValue( open );
695 } # end hitch state has changed
696
697 if (!open ) {
698 aerotow(open);
699 # end hitch is closed (open == 0)
700 } else {
701 # my hitch is open
702 var mp_auto_connect_period = aerotow_hash.mp_auto_connect_period.getDoubleValue();
703 if ( mp_auto_connect_period != 0 ) { # if auto-connect
704 if ( fdm == "jsb" ) { # only for JSBSim aircraft
705 findBestAIObject();
706 } # end JSBSim aircraft
707 dt = mp_auto_connect_period;
708 } else { # my hitch is open and not auto-connect
709 dt = 0;
710 }
711 }
712
713 # winch part
714 if (!winch_hash.open.getBoolValue() ) {
715 winch(winch_hash.open.getBoolValue());
716 }
717
718 settimer( towing, dt );
719
720 } # end towing
721
722 setlistener( winch_hash.open, func {
723 var winchopen = winch_hash.open.getBoolValue();
724 var wincholdOpen = winch_hash.old_open.getBoolValue();
725
726 if ( winchopen != wincholdOpen ) { # check if my hitch state has changed, if yes: message
727 if ( !winchopen ) { # my hitch was open and is closed now
728 if ( fdm == "jsb" ) {
729 var distance = winch_hash.tow.dist.getDoubleValue();
730 var towlength_m = winch_hash.tow.length.getDoubleValue();
731 if ( distance > towlength_m ) {
732 write_message( "system", sprintf("Could not lock hitch (tow length is insufficient) on hitch %i!", aerotow_hash.tow.conn_mp_node.getIntValue() ) );
733 winch_hash.open.setBoolValue( 1 ); # open my hitch again
734 } else { # mp aircraft to far away
735 # my hitch is closed
736 write_message( "system", sprintf("Locked hitch winch %i!", aerotow_hash.tow.conn_mp_node.getIntValue() ) );
737 winch_hash.clutched.setBoolValue( 0 );;
738 }
739 winch_hash.broken.setBoolValue( 0 );
740 winch_hash.actual_spool_speed.setDoubleValue( 0.0 );
741 } # end: JSBSim
742 if ( !winch_hash.open.getBoolValue() ) {
743 # setup ai-towrope
744 createTowrope("winch");
745
746 # set default hitch coordinates (needed for Ai- and non-interactive MP aircraft)
747 setAIObjectDefaults() ;
748 }
749 } # end hitch is closed
750
751 if ( winchopen ) { # my hitch is now open
752 if ( fdm == "jsb" ) {
753 if ( winch_hash.broken.getBoolValue() ) {
754 write_message( "pilot", "Oh no, the tow is broken" );
755 }
756 releaseHitch("winch");
757 } # end: JSBSim
758 pull_in_rope();
759 } # end hitch is open
760
761 winch_hash.old_open.setBoolValue( winchopen );
762 } # end hitch state has changed
763
764 });
765
766 setlistener( aircraft_settings.winch.type_p, func {
767 winch_hash.type = aircraft_settings.winch.type_p.getIntValue();
768 });
769
770
771 # ######################################################################################################################
772 # find best AI object
773 # ######################################################################################################################
774
775 var findBestAIObject = func (){
776
777 # the nearest found plane, that is close enough will be used
778 # set some default variables, needed later to identify if the found object is
779 # an AI-Object, a "non-interactiv MP-Object or an interactive MP-Object
780
781 # local variables
782 var aiobjects = []; # keeps the ai-planes from the property tree
783 var aiPosition = geo.Coord.new(); # current processed ai-plane
784 var myPosition = geo.Coord.new(); # coordinates of glider
785 var distance_m = 0; # distance to ai-plane
786
787 var nodeIsAiAircraft = 0;
788 var nodeIsMpAircraft = 0;
789 var running_as_autoconnect = 0;
790 var mp_open_last_state = 0;
791 var isSlave = 0;
792
793 if ( fdm == "yasim" ) return; # bypass this routine for Yasim-aircraft
794
795
796 if (aerotow_hash.mp_auto_connect_period.getDoubleValue() != 0 ) {
797 var running_as_autoconnect = 1;
798 }
799
800 var towlength_m = aerotow_hash.tow.length.getDoubleValue();
801
802 var bestdist_m = towlength_m; # initial value
803
804 myPosition = geo.aircraft_position();
805 # todo: calculate exact hitch position
806
807 if( running_as_autoconnect ) {
808 var mycallsign = callsign.getValue();
809 }
810
811 var found = 0;
812 aiobjects = props.globals.getNode("ai/models").getChildren();
813 foreach (var aimember; aiobjects) {
814 if ( (var node = aimember.getName() ) != nil ) {
815 nodeIsAiAircraft = 0;
816 nodeIsMpAircraft = 0;
817 if ( sprintf("%8s",node) == "aircraft" or sprintf("%7s",node) == "dragger" ) nodeIsAiAircraft = 1;
818 if ( sprintf("%11s",node) == "multiplayer" ) nodeIsMpAircraft = 1;
819 if ( aimember.getNode("valid") == nil ) continue;
820 if ( !nodeIsAiAircraft and !nodeIsMpAircraft) continue;
821 if ( !aimember.getNode("valid").getValue() ) continue; # node is invalid
822
823 if( running_as_autoconnect ) {
824 if ( !nodeIsMpAircraft ) continue;
825 #if ( aimember.getValue("sim/hitches/aerotow/open") == nil ) continue; # this node MUST exist for mp-aircraft which want to be towed
826 #if ( aimember.getValue("sim/hitches/aerotow/open") == 1 ) continue; # if mp hook open, auto-connect is NOT possible
827 if ( aimember.getValue("sim/hitches/aerotow/open") != 0 ) continue;
828 if (mycallsign != aimember.getValue("sim/hitches/aerotow/tow/connected-to-ai-or-mp-callsign") ) continue ; # I am the wrong one
829 if ( !aerotow_hash.mp_old_open.getBoolValue() ) continue; # this prevents an unwanted immediate auto-connect after the dragger
830 # released its hitch. Firstly wait for a reported "open" hitch from glider
831 }
832
833 var lat_deg = aimember.getNode("position/latitude-deg").getValue();
834 var lon_deg = aimember.getNode("position/longitude-deg").getValue();
835 var alt_m = aimember.getNode("position/altitude-ft").getValue() * FT2M;
836
837 var aiPosition = geo.Coord.set_latlon( lat_deg, lon_deg, alt_m );
838 distance_m = (myPosition.distance_to(aiPosition));
839 if ( distance_m < bestdist_m ) {
840 bestdist_m = distance_m;
841
842 var towEndNode = node;
843 var nodeID = aimember.getNode("id").getValue();
844 var aicallsign = aimember.getNode("callsign").getValue();
845
846 #set properties
847 aerotow_hash.open.setBoolValue( 0 );
848 aerotow_hash.tow.conn_ai_node.setBoolValue( nodeIsAiAircraft );
849 aerotow_hash.tow.conn_mp_node.setBoolValue( nodeIsMpAircraft );
850 aerotow_hash.tow.conn_ai_or_mp_callsign.setValue( aicallsign );
851 aerotow_hash.tow.conn_ai_or_mp_id.setIntValue( nodeID );
852 aerotow_hash.tow.conn_prop_node.setBoolValue( 1 );
853 aerotow_hash.tow.node.setValue( towEndNode );
854 aerotow_hash.tow.dist.setDoubleValue( bestdist_m );
855 aerotow_hash.tow.mp_last_reported_dist.setDoubleValue( 0.0 );
856
857 # Set some dummy values. In case of an "interactive"-MP plane
858 # the correct values will be transmitted in the following loop.
859 # Only set default values if the node does not yet exist.
860 # If the AI aircraft already has a defined hitch location, this prevents
861 # it from being overwritten with default values.
862 if (!aimember.getNode("sim/hitches/aerotow", 0)) {
863 aimember.getNode("sim/hitches/aerotow/local-pos-x",1).setValue(-5.);
864 aimember.getNode("sim/hitches/aerotow/local-pos-y",1).setValue(0.);
865 aimember.getNode("sim/hitches/aerotow/local-pos-z",1).setValue(0.);
866 aimember.getNode("sim/hitches/aerotow/tow/dist",1).setValue(-1.);
867 }
868 found = 1;
869 } # end distance_m < bestdist_m
870 } # end node != nil
871 } # end loop aiobjects
872
873 if (found) {
874 if ( !running_as_autoconnect) {
875 write_message( "pilot", sprintf("%s, I am on your hook, distance %4.3f meter.",aicallsign,bestdist_m) );
876 } else {
877 write_message( "remote-ac", sprintf("%s: I am on your hook, distance %4.3f meter.",aicallsign,bestdist_m ) );
878 }
879 if ( running_as_autoconnect ) {
880 isSlave = 1;
881 aircraft_settings.aerotow.is_slave.setBoolValue( isSlave );
882 }
883
884 aerotow_hash.mp_old_open.setBoolValue( 1 );
885
886 } # end: if found
887 else {
888 if (!running_as_autoconnect) {
889 write_message( "system", "Sorry, no aircraft for aerotow!" );
890 } else{
891 aerotow_hash.old_open.setBoolValue( 1 );
892 }
893 }
894
895 } # End function findBestAIObject
896
897
898 # ######################################################################################################################
899
900
901 # Start the towing animation ASAP
902 towing();
903
904
905
906 # ######################################################################################################################
907 # aerotow function
908 # ######################################################################################################################
909
910 var aerotow = func (open){
911
912 #print("function aerotow is running");
913
914 # if (!open ) {
915
916 ########################################### my hitch position ############################################
917
918 myPosition = geo.aircraft_position();
919 var my_head_deg = orientation[0].getDoubleValue();
920 var my_roll_deg = orientation[1].getDoubleValue();
921 var my_pitch_deg = orientation[2].getDoubleValue();
922
923 # hook coordinates in Yasim-system (x-> nose / y -> left wing / z -> up)
924 assignHitchLocations("aerotow");
925 var x = aircraft_settings.aerotow.local_pos[0].getDoubleValue();
926 var y = aircraft_settings.aerotow.local_pos[1].getDoubleValue();
927 var z = aircraft_settings.aerotow.local_pos[2].getDoubleValue();
928
929 var alpha_deg = my_roll_deg * (1.); # roll clockwise (looking in x-direction) := +
930 var beta_deg = my_pitch_deg * (-1.); # pitch clockwise (looking in y-direction) := -
931
932 # transform hook coordinates
933 var Xn = PointRotate3D(x:x,y:y,z:z,xr:0.,yr:0.,zr:0.,alpha_deg:alpha_deg,beta_deg:beta_deg,gamma_deg:0.);
934
935 var install_distance_m = Xn[0]; # in front of ref-point of glider
936 var install_side_m = Xn[1];
937 var install_alt_m = Xn[2];
938
939 var myHitch_pos = myPosition.apply_course_distance( my_head_deg , install_distance_m );
940 var myHitch_pos = myPosition.apply_course_distance( my_head_deg - 90. , install_side_m );
941 myHitch_pos.set_alt(myPosition.alt() + install_alt_m);
942
943 ########################################### ai hitch position ############################################
944
945 #var aiNodeID = getprop("sim/hitches/aerotow/tow/connected-to-ai-or-mp-id"); # id of former found ai/mp aircraft
946 #print("aiNodeID=",aiNodeID);
947 var aiCallsign = aerotow_hash.tow.conn_ai_or_mp_callsign.getValue(); # callsign of former found ai/mp aircraft
948
949 var found = 0;
950
951 aiobjects = props.globals.getNode("ai/models").getChildren();
952 foreach (var aimember; aiobjects) {
953 if ( (var c = aimember.getNode("id") ) != nil ) {
954 if ( !aimember.getNode("valid").getValue() ) continue; # node is invalid
955
956 # Identifying the MP-aircraft by its node-id works fine with JSBSim-aircraft but NOT with YASim.
957 # In YASim the node-id is not updated which could lead to complications (e.g. node-id changes after "Pause" or "Exit").
958 #var testprop = c.getValue();
959 #if ( testprop == aiNodeID) {
960
961 # Identifying the MP-aircraft by its callsign works fine with JSBSim AND YASim-aircraft
962 var testprop = aimember.getNode("callsign").getValue();
963 if ( testprop == aiCallsign ) {
964
965 found = found + 1;
966
967 ###################### check status of ai hitch ######################
968 if ( fdm == "jsb" ) {
969 # check if the multiplayer hitch state has changed
970 # this trick avoids immediate opening after locking because MP-aircraft has not yet reported a locked hitch
971 if ( (var d = aimember.getNode("sim/hitches/aerotow/open") ) != nil ) {
972 var mpOpen = aimember.getNode("sim/hitches/aerotow/open").getValue();
973 var mp_oldOpen = aerotow_hash.mp_old_open.getBoolValue();
974 #print('mpOpen=',mpOpen,' mp_oldOpen=',mp_oldOpen);
975 if ( mpOpen != mp_oldOpen ) { # state has changed: was open and is now locked OR was locked and is now open
976 if ( mpOpen ) {
977 write_message( "remote-ac", sprintf("%s: I have released the tow!", aerotow_hash.tow.conn_ai_or_mp_callsign.getValue() ) );
978 releaseHitch("aerotow"); # my open=1 / forces=0 / remove towrope
979 } # end: open
980 aerotow_hash.mp_old_open.setBoolValue(mpOpen);
981 } # end: state has changed
982 } # end: node is available
983 } #end : JSBSim
984 ########################################################################
985
986 # get coordinates
987 var ai_lat = aimember.getNode("position/latitude-deg").getValue();
988 var ai_lon = aimember.getNode("position/longitude-deg").getValue();
989 var ai_alt = (aimember.getNode("position/altitude-ft").getValue()) * FT2M;
990 #print("ai_lat,lon,alt",ai_lat,ai_lon,ai_alt);
991
992 var ai_pitch_deg = aimember.getNode("orientation/pitch-deg").getValue();
993 var ai_roll_deg = aimember.getNode("orientation/roll-deg").getValue();
994 var ai_head_deg = aimember.getNode("orientation/true-heading-deg").getValue();
995
996 var aiHitchX = aimember.getNode("sim/hitches/aerotow/local-pos-x").getValue();
997 var aiHitchY = aimember.getNode("sim/hitches/aerotow/local-pos-y").getValue();
998 var aiHitchZ = aimember.getNode("sim/hitches/aerotow/local-pos-z").getValue();
999
1000 var aiPosition = geo.Coord.set_latlon( ai_lat, ai_lon, ai_alt );
1001
1002 var alpha_deg = ai_roll_deg * (1.);
1003 var beta_deg = ai_pitch_deg * (-1.);
1004
1005 # transform hook coordinates
1006 var Xn = PointRotate3D(x:aiHitchX,y:aiHitchY,z:aiHitchZ,xr:0.,yr:0.,zr:0.,alpha_deg:alpha_deg,beta_deg:beta_deg,gamma_deg:0.);
1007
1008 var install_distance_m = Xn[0]; # in front of ref-point of glider
1009 var install_side_m = Xn[1];
1010 var install_alt_m = Xn[2];
1011
1012 var aiHitch_pos = aiPosition.apply_course_distance( ai_head_deg , install_distance_m );
1013 var aiHitch_pos = aiPosition.apply_course_distance( ai_head_deg - 90. , install_side_m );
1014 aiHitch_pos.set_alt(aiPosition.alt() + install_alt_m);
1015
1016 ########################################### distance between hitches #####################################
1017
1018 var distance = (myHitch_pos.direct_distance_to(aiHitch_pos)); # distance to plane in meter
1019 var aiHitchheadto = (myHitch_pos.course_to(aiHitch_pos));
1020 var height = myHitch_pos.alt() - aiHitch_pos.alt();
1021
1022 var aiHitchpitchto = -math.asin((myHitch_pos.alt()-aiHitch_pos.alt())/distance) / 0.01745;
1023 #print(" pitch: ", aiHitchpitchto);
1024
1025 # update position of rope
1026 aerotow_hash.rope.lat.setDoubleValue( myHitch_pos.lat() );
1027 aerotow_hash.rope.lon.setDoubleValue( myHitch_pos.lon() );
1028 aerotow_hash.rope.alt.setDoubleValue( myHitch_pos.alt() * M2FT );
1029 # update orientation of rope
1030 aerotow_hash.rope.hdg.setDoubleValue( aiHitchheadto );
1031 aerotow_hash.rope.pitch.setDoubleValue( aiHitchpitchto );
1032
1033 # update length of rope
1034 aerotow_hash.tow.dist.setDoubleValue( distance );
1035
1036
1037 ############################################# calc forces ##################################################
1038
1039 # calc forces only for JSBSim-aircraft
1040
1041 # tow-end-forces must be reported in N to be consiststent to Yasim-aircraft
1042 # hitch-forces must be LBS to be consistent to the JSBSim "external_forces/.../magnitude" definition
1043
1044 if ( fdm == "jsb" ) {
1045
1046 # check if the MP-aircraft properties have been updated. If not (maybe due to time-lag) bypass force calculation (use previous forces instead)
1047 var mp_reported_dist = aimember.getNode("sim/hitches/aerotow/tow/dist").getValue();
1048 var mp_last_reported_dist = aerotow_hash.tow.mp_last_reported_dist.getDoubleValue();
1049 var mp_delta_reported_dist = mp_reported_dist - mp_last_reported_dist ;
1050 aerotow_hash.tow.mp_last_reported_dist.setDoubleValue( mp_reported_dist );
1051 var mp_delta_reported_dist2 = mp_delta_reported_dist * mp_delta_reported_dist ; # we need the absolute value
1052 if ( (mp_delta_reported_dist2 > 0.0000001) or (mp_reported_dist < 0. )){ # we have the updated MP coordinates (no time lag)
1053 # or the MP-aircraft is a non-interactive mp plane (mp_reported_dist = -1)
1054 # => update forces else use the old forces!
1055
1056 var breakforce_N = getprop("sim/hitches/aerotow/tow/break-force"); # could be different in both aircraft
1057
1058 var isSlave = aircraft_settings.aerotow.is_slave.getBoolValue();
1059 if ( !isSlave ){ # if we are master, we have to calculate the forces
1060 var elastic_constant = getprop("sim/hitches/aerotow/tow/elastic-constant");
1061 var towlength_m = getprop("sim/hitches/aerotow/tow/length");
1062
1063 var delta_towlength_m = distance - towlength_m;
1064
1065 if ( delta_towlength_m < 0. ) {
1066 var forcetow_N = 0.;
1067 }
1068 else{
1069 var forcetow_N = elastic_constant * delta_towlength_m / towlength_m;
1070 }
1071 } else { # we are slave and get the forces from master
1072 var forcetowX_N = aimember.getNode("sim/hitches/aerotow/tow/end-force-x").getValue() * 1;
1073 var forcetowY_N = aimember.getNode("sim/hitches/aerotow/tow/end-force-y").getValue() * 1;
1074 var forcetowZ_N = aimember.getNode("sim/hitches/aerotow/tow/end-force-z").getValue() * 1;
1075 var forcetow_N = math.sqrt( forcetowX_N * forcetowX_N + forcetowY_N * forcetowY_N + forcetowZ_N * forcetowZ_N );
1076 }
1077
1078 var forcetow_LBS = forcetow_N * 0.224809; # N -> LBF
1079
1080 if ( forcetow_N < breakforce_N ) {
1081
1082 var distancepr = (myHitch_pos.distance_to(aiHitch_pos));
1083
1084 # correct a failure, if the projected length is larger than direct length
1085 if (distancepr > distance) { distancepr = distance;}
1086
1087 var alpha = math.acos( (distancepr / distance) );
1088 if ( aiHitch_pos.alt() > myHitch_pos.alt()) alpha = - alpha;
1089
1090 var beta = ( aiHitchheadto - my_head_deg ) * 0.01745;
1091 var gamma = my_pitch_deg * 0.01745;
1092 var delta = my_roll_deg * 0.01745;
1093
1094 var sina = math.sin(alpha);
1095 var cosa = math.cos(alpha);
1096 var sinb = math.sin(beta);
1097 var cosb = math.cos(beta);
1098 var sing = math.sin(gamma);
1099 var cosg = math.cos(gamma);
1100 var sind = math.sin(delta);
1101 var cosd = math.cos(delta);
1102
1103 var forcetow = forcetow_LBS; # we deliver LBS to JSBSim
1104
1105 # calculate unit vector of force direction in JSBSim-system
1106 var force = 1;
1107
1108 # global forces: alpha beta
1109 var fglobalx = force * cosa * cosb;
1110 var fglobaly = force * cosa * sinb;
1111 var fglobalz = force * sina;
1112
1113 # local forces by pitch: gamma
1114 var flpitchx = fglobalx * cosg - fglobalz * sing;
1115 var flpitchy = fglobaly;
1116 var flpitchz = fglobalx * sing + fglobalz * cosg;
1117
1118 # local forces by roll: delta
1119 var flrollx = flpitchx;
1120 var flrolly = flpitchy * cosd + flpitchz * sind;
1121 var flrollz = - flpitchy * sind + flpitchz * cosd;
1122
1123 # asigning to LOCAL coord of plane
1124 var forcex = flrollx;
1125 var forcey = flrolly;
1126 var forcez = flrollz;
1127
1128 # JSBSim-body-frame: x-> nose / y -> right wing / z -> down
1129 # apply forces to hook (forces are in LBS or N see above)
1130 var hitchname = getprop("sim/hitches/aerotow/force_name_jsbsim");
1131 setprop("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/magnitude", forcetow);
1132 setprop("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/x", forcex);
1133 setprop("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/y", forcey);
1134 setprop("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/z", forcez);
1135
1136 } else { # rope is broken
1137 aerotow_hash.broken.setBoolValue( 1 );
1138 #setprop("sim/messages/atc", sprintf("Oh no, the tow is broken"));
1139 releaseHitch("aerotow"); # open=1 / forces=0 / remove towrope
1140 }
1141
1142 ############################################# report forces ##############################################
1143
1144 # if we are connected to a MP-aircraft and master
1145 var nodeIsMpAircraft = getprop("sim/hitches/aerotow/tow/connected-to-mp-node");
1146 if ( nodeIsMpAircraft and !isSlave ){
1147
1148 # transform my hitch coordinates to cartesian earth coordinates
1149 var myHitchCartEarth = geodtocart(myHitch_pos.lat(),myHitch_pos.lon(),myHitch_pos.alt() );
1150 var myHitchXearth_m = myHitchCartEarth[0];
1151 var myHitchYearth_m = myHitchCartEarth[1];
1152 var myHitchZearth_m = myHitchCartEarth[2];
1153
1154 # transform MP hitch coordinates to cartesian earth coordinates
1155 var aiHitchCartEarth = geodtocart(aiHitch_pos.lat(),aiHitch_pos.lon(),aiHitch_pos.alt() );
1156 var aiHitchXearth_m = aiHitchCartEarth[0];
1157 var aiHitchYearth_m = aiHitchCartEarth[1];
1158 var aiHitchZearth_m = aiHitchCartEarth[2];
1159
1160 # calculate normal vector in tow direction in cartesian earth coordinates
1161 var dx = aiHitchXearth_m - myHitchXearth_m;
1162 var dy = aiHitchYearth_m - myHitchYearth_m;
1163 var dz = aiHitchZearth_m - myHitchZearth_m;
1164 var dl = math.sqrt( dx * dx + dy * dy + dz * dz );
1165
1166 var forcetowX_N = forcetow_N * dx / dl;
1167 var forcetowY_N = forcetow_N * dy / dl;
1168 var forcetowZ_N = forcetow_N * dz / dl;
1169
1170 setprop("sim/hitches/aerotow/tow/dist", distance);
1171 setprop("sim/hitches/aerotow/tow/end-force-x", -forcetowX_N); # force acts in
1172 setprop("sim/hitches/aerotow/tow/end-force-y", -forcetowY_N); # opposite direction
1173 setprop("sim/hitches/aerotow/tow/end-force-z", -forcetowZ_N); # at tow end
1174
1175 } # end report forces
1176
1177 } # end: timelag
1178 } # end forces/JSBSim
1179
1180
1181 } # end: aiNodeID
1182 } # end: check id != nil
1183 } # end: loop over aiobjects
1184
1185 if ( found == 0 ) {
1186 if ( fdm == "jsb" ) {
1187 write_message( "system", "MP-aircraft disappeared!" );
1188 aerotow_hash.open.setBoolValue( 1 ); # open my hitch
1189 aerotow_hash.tow.conn_ai_or_mp_id.setIntValue( 0 );
1190 aerotow_hash.tow.conn_ai_or_mp_callsign.setValue( "" );
1191 aerotow_hash.tow.conn_ai_node.setBoolValue( 0 );
1192 aerotow_hash.tow.conn_mp_node.setBoolValue( 0 );
1193 aerotow_hash.tow.conn_prop_node.setBoolValue( 0 );
1194 }
1195 #if ( fdm == "yasim" ) removeTowrope("aerotow"); # remove towrope model
1196 } # end found=0
1197
1198 } # end function aerotow
1199
1200
1201
1202 # ######################################################################################################################
1203 # winch function
1204 # ######################################################################################################################
1205
1206 var winch = func (open){
1207
1208 if (!open ) {
1209
1210
1211 ########################################### my hitch position ############################################
1212
1213 myPosition = geo.aircraft_position();
1214 var my_head_deg = orientation[0].getDoubleValue();
1215 var my_roll_deg = orientation[1].getDoubleValue();
1216 var my_pitch_deg = orientation[2].getDoubleValue();
1217
1218 # hook coordinates in Yasim-system (x-> nose / y -> left wing / z -> up)
1219 assignHitchLocations("winch");
1220 var x = aircraft_settings.winch.local_pos[0].getDoubleValue();
1221 var y = aircraft_settings.winch.local_pos[1].getDoubleValue();
1222 var z = aircraft_settings.winch.local_pos[2].getDoubleValue();
1223
1224 var alpha_deg = my_roll_deg * (1.); # roll clockwise (looking in x-direction) := +
1225 var beta_deg = my_pitch_deg * (-1.); # pitch clockwise (looking in y-direction) := -
1226
1227 # transform hook coordinates
1228 var Xn = PointRotate3D(x:x,y:y,z:z,xr:0.,yr:0.,zr:0.,alpha_deg:alpha_deg,beta_deg:beta_deg,gamma_deg:0.);
1229
1230 var install_distance_m = Xn[0]; # in front of ref-point of glider
1231 var install_side_m = Xn[1];
1232 var install_alt_m = Xn[2];
1233
1234 var myHitch_pos = myPosition.apply_course_distance( my_head_deg , install_distance_m );
1235 var myHitch_pos = myPosition.apply_course_distance( my_head_deg - 90. , install_side_m );
1236 myHitch_pos.set_alt(myPosition.alt() + install_alt_m);
1237
1238
1239 ############################ Check for rope breakage ###############################
1240 var my_height = myHitch_pos.alt() - geo.elevation( myHitch_pos.lat(), myHitch_pos.lon() );
1241
1242 if( winch_hash.rope_breakage and my_height > winch_hash.rope_breakage_height_int ) {
1243 releaseHitch("winch");
1244 winch_hash.rope_breakage = 0;
1245 # The pilot shouldn't immediately be notified this was a rope breakage (to train recognizing a rope breakage)
1246 # That's why we report the breakage and its height 5 seconds later
1247 var temp = winch_hash.rope_breakage_height_int;
1248 settimer( func() {
1249 write_message( "winch-driver", "Rope broke at " ~ sprintf("%4d", math.round( temp ) ) ~ "m height" );
1250 }, 5);
1251 return;
1252 }
1253
1254 ############################ Check for winch loss of power ##########################
1255 if( winch_hash.loss_of_power.enabled and my_height > winch_hash.loss_of_power.height_int ) {
1256 loss_of_power();
1257 winch_hash.loss_of_power.enabled = 0;
1258 # The pilot shouldn't immediately be notified this was a loss of power (to train recognizing a winch loss of power)
1259 # That's why we report the loss of power and its height 15 seconds later
1260 var temp = winch_hash.loss_of_power.height_int;
1261 settimer( func() {
1262 write_message( "winch-driver", "Winch lost power at " ~ sprintf("%4d", math.round( temp ) ) ~ "m height" );
1263 }, 15);
1264 return;
1265 }
1266
1267 ########################################### winch hitch position ############################################
1268
1269 # get coordinates
1270 var winch_global_pos_x = winch_hash.global_pos[0].getDoubleValue();
1271 var winch_global_pos_y = winch_hash.global_pos[1].getDoubleValue();
1272 var winch_global_pos_z = winch_hash.global_pos[2].getDoubleValue();
1273
1274 var winch_geod = carttogeod(winch_global_pos_x, winch_global_pos_y, winch_global_pos_z);
1275
1276 var ai_lat = winch_geod[0];
1277 var ai_lon = winch_geod[1];
1278 #var ai_alt = winch_geod[2] * FT2M;
1279 var ai_alt = winch_geod[2];
1280 #print("ai_lat,lon,alt",ai_lat,ai_lon,ai_alt);
1281
1282 var aiHitch_pos = geo.Coord.set_latlon( ai_lat, ai_lon, ai_alt );
1283
1284
1285 ########################################### distance between hitches #####################################
1286
1287 var distance = (myHitch_pos.direct_distance_to(aiHitch_pos)); # distance to winch in meter
1288 var aiHitchheadto = (myHitch_pos.course_to(aiHitch_pos));
1289 var height = myHitch_pos.alt() - aiHitch_pos.alt();
1290
1291 var aiHitchpitchto = -math.asin((myHitch_pos.alt()-aiHitch_pos.alt())/distance) * R2D;
1292 #print(" pitch: ", aiHitchpitchto);
1293
1294 #print("Update Winch Rope");
1295 #print("Winch Rope Lat is "~ myHitch_pos.lat() );
1296 # update position of rope
1297 winch_hash.rope.lat.setDoubleValue( myHitch_pos.lat() );
1298 winch_hash.rope.lon.setDoubleValue( myHitch_pos.lon() );
1299 winch_hash.rope.alt.setDoubleValue( myHitch_pos.alt() * M2FT );
1300 # update orientation of rope
1301 winch_hash.rope.hdg.setDoubleValue( aiHitchheadto );
1302 winch_hash.rope.pitch.setDoubleValue( aiHitchpitchto );
1303
1304 # update length of rope
1305 winch_hash.tow.dist.setDoubleValue( distance );
1306 #print("distance=",distance);
1307
1308
1309 ############################################# calc forces ##################################################
1310
1311 # calc forces only for JSBSim-aircraft
1312
1313 # tow-end-forces must be reported in N to be consiststent to Yasim-aircraft
1314 # hitch-forces must be LBS to be consistent to the JSBSim "external_forces/.../magnitude" definition
1315
1316 if ( fdm == "jsb" ) {
1317
1318 var spool_max = config.winch.max_spool_speed.getDoubleValue();
1319 var unspool_max = config.winch.max_unspool_speed.getDoubleValue();
1320 var max_force_N = config.winch.max_force.getDoubleValue();
1321 var max_power_W = config.winch.max_power.getDoubleValue() * 1000.;
1322 var breakforce_N = aircraft_settings.winch.break_force.getDoubleValue();
1323 var elastic_constant = config.winch.elastic_constant.getDoubleValue();
1324 var towlength_m = winch_hash.tow.length.getDoubleValue();
1325 var max_tow_length_m = config.winch.max_tow_length_m.getDoubleValue();
1326 var spoolspeed = winch_hash.actual_spool_speed.getDoubleValue();
1327 var spool_acceleration = config.winch.spool_acceleration.getDoubleValue();
1328 var delta_t = delta_time.getDoubleValue();
1329
1330 #print("towlength_m= ", towlength_m , " elastic_constant= ", elastic_constant," delta_towlength_m= ", delta_towlength_m);
1331
1332 var forcetow_N = winch_hash.actual_force.getDoubleValue();
1333 var towlength_new_m = nil;
1334 var delta_towlength_m = nil;
1335
1336 if ( winch_hash.clutched.getBoolValue() ) {
1337
1338 if( winch_hash.type == 0 ){
1339 # Concept: force is conrolled
1340
1341 towlength_new_m = towlength_m - spoolspeed * delta_t;
1342 delta_towlength_m = distance - towlength_new_m;
1343 var delta_spoolspeed = spool_acceleration * delta_t;
1344 spoolspeed = spoolspeed + delta_spoolspeed ;
1345 if ( spoolspeed > spool_max ) spoolspeed = spool_max;
1346
1347 if ( delta_towlength_m < 0. ) {
1348 forcetow_N = 0.;
1349 }
1350 else{
1351 forcetow_N = elastic_constant * delta_towlength_m / towlength_new_m;
1352 }
1353
1354 } else if ( winch_hash.type == 1 ){
1355 # Concept: regulate force to reach target speed
1356 var speed_trgt = speed_setting;
1357 if( math.abs ( spoolspeed - speed_trgt ) > 0.5 ) {
1358 if( speed_trgt > spoolspeed ) {
1359 forcetow_N += acceleration_N_s * delta_t;
1360 } else if ( speed_trgt < spoolspeed ){
1361 forcetow_N -= acceleration_N_s * delta_t;
1362 }
1363 }
1364 # New towlength is distance from hook to winch; previous towlength is stored in winch_hash.towlength_m
1365 towlength_new_m = distance;
1366 delta_towlength_m = -1 * ( distance - towlength_m );
1367 spoolspeed = delta_towlength_m / delta_t;
1368 }
1369
1370 } else { # un-clutched
1371 # --- experimental --- #
1372 towlength_new_m = towlength_m - spoolspeed * delta_t;
1373 delta_towlength_m = distance - towlength_new_m;
1374
1375 # we assume that the the winch-operator avoids tow sagging ( => rigid rope; negativ forces allowed)
1376 forcetow_N = elastic_constant * delta_towlength_m / towlength_new_m;
1377
1378 # drag of tow-rope ( magic! )
1379 var magic_constant = config.winch.magic_constant.getDoubleValue();
1380 tow_drag_N = spoolspeed * spoolspeed * math.sqrt( math.sqrt( height * height ) * max_tow_length_m ) / magic_constant ;
1381
1382 # mass = tow-mass only (drum-mass ignored)
1383 var mass_kg = max_tow_length_m * config.winch.weight_per_m_kg_m.getDoubleValue();
1384
1385 var acceleration = ( forcetow_N - tow_drag_N ) / mass_kg;
1386 var delta_spoolspeed = acceleration * delta_t;
1387 spoolspeed = spoolspeed - delta_spoolspeed;
1388 if ( spoolspeed < - unspool_max ) spoolspeed = - unspool_max;
1389
1390 if ( delta_towlength_m < 0. ) {
1391 forcetow_N = 0.;
1392 }
1393 else{
1394 forcetow_N = elastic_constant * delta_towlength_m / towlength_new_m;
1395 }
1396 }
1397
1398 if ( forcetow_N > max_force_N ) {
1399 forcetow_N = max_force_N;
1400 var towlength_new_m = distance / ( forcetow_N / elastic_constant + 1. );
1401 spoolspeed = (towlength_m - towlength_new_m ) / delta_t;
1402 }
1403
1404 var power = forcetow_N * spoolspeed;
1405 if ( power > max_power_W) {
1406 power = max_power_W;
1407 forcetow_N = power / spoolspeed;
1408 towlength_new_m = towlength_m - spoolspeed * delta_t;
1409 }
1410
1411 winch_hash.tow.length.setDoubleValue( towlength_new_m );
1412 winch_hash.actual_spool_speed.setDoubleValue( spoolspeed );
1413 winch_hash.actual_force.setDoubleValue( forcetow_N );
1414
1415 # force due to tow-weight (acts in tow direction at the heigher hitch)
1416 var force_due_to_weight_N = config.winch.weight_per_m_kg_m.getDoubleValue() * 9.81 * height;
1417 if (height < 0. ) force_due_to_weight_N = 0.;
1418
1419 forcetow_N = forcetow_N + force_due_to_weight_N;
1420 var forcetow_LBS = forcetow_N * 0.224809; # N -> LBF
1421
1422 if ( forcetow_N < breakforce_N ) {
1423
1424 var distancepr = (myHitch_pos.distance_to(aiHitch_pos));
1425
1426 # correct a failure, if the projected length is larger than direct length
1427 if (distancepr > distance) { distancepr = distance;}
1428
1429 var alpha = math.acos( (distancepr / distance) );
1430 if ( aiHitch_pos.alt() > myHitch_pos.alt()) alpha = - alpha;
1431 var beta = ( aiHitchheadto - my_head_deg ) * D2R;
1432 var gamma = my_pitch_deg * D2R;
1433 var delta = my_roll_deg * D2R;
1434
1435 var sina = math.sin(alpha);
1436 var cosa = math.cos(alpha);
1437 var sinb = math.sin(beta);
1438 var cosb = math.cos(beta);
1439 var sing = math.sin(gamma);
1440 var cosg = math.cos(gamma);
1441 var sind = math.sin(delta);
1442 var cosd = math.cos(delta);
1443
1444 # calculate unit vector of force direction in JSBSim-system
1445 var force = 1;
1446
1447 # global forces: alpha beta
1448 var fglobalx = force * cosa * cosb;
1449 var fglobaly = force * cosa * sinb;
1450 var fglobalz = force * sina;
1451
1452 # local forces by pitch: gamma
1453 var flpitchx = fglobalx * cosg - fglobalz * sing;
1454 var flpitchy = fglobaly;
1455 var flpitchz = fglobalx * sing + fglobalz * cosg;
1456
1457 # local forces by roll: delta
1458 var flrollx = flpitchx;
1459 var flrolly = flpitchy * cosd + flpitchz * sind;
1460 var flrollz = - flpitchy * sind + flpitchz * cosd;
1461
1462 # asigning to LOCAL coord of plane
1463 var forcex = flrollx;
1464 var forcey = flrolly;
1465 var forcez = flrollz;
1466
1467 # JSBSim-body-frame: x-> nose / y -> right wing / z -> down
1468 # apply forces to hook (forces are in LBS or N see above)
1469 winch_hash.apply_force.magnitude.setDoubleValue( forcetow_LBS );
1470 winch_hash.apply_force.coord[0].setDoubleValue( forcex );
1471 winch_hash.apply_force.coord[1].setDoubleValue( forcey );
1472 winch_hash.apply_force.coord[2].setDoubleValue( forcez );
1473
1474 # check, if auto-release condition is reached
1475 var rope_angle_deg = math.atan2(forcez , forcex ) * R2D;
1476 #print("rope_angle_deg=",rope_angle_deg);
1477 if (rope_angle_deg > aircraft_settings.winch.automatic_release_angle.getDoubleValue() ) releaseWinch();
1478
1479 } # end force < break force
1480 else { # rope is broken
1481 winch_hash.broken.setBoolValue( 1 );
1482 releaseWinch();
1483 }
1484
1485 if ( towlength_new_m > max_tow_length_m ) {
1486 write_message( "system", "tow length exceeded!" );
1487 releaseWinch();
1488 }
1489
1490 } # end forces/JSBSim
1491
1492 } # end hitch is closed (open == 0)
1493
1494 } # end function winch
1495
1496
1497 # ######################################################################################################################
1498 # create towrope
1499 # ######################################################################################################################
1500
1501 var createTowrope = func (device){
1502
1503 # create the towrope in the model property tree
1504 #print("createTowrope for ",device);
1505
1506 if ( getprop("sim/hitches/" ~ device ~ "/rope/exist") == 0 ) { # does the towrope exist?
1507
1508 # get the next free model id
1509 var freeModelid = getFreeModelID();
1510
1511 props.globals.getNode("sim/hitches/" ~ device ~ "/rope/model_id").setIntValue(freeModelid);
1512 props.globals.getNode("sim/hitches/" ~ device ~ "/rope/exist").setBoolValue(1);
1513
1514 var towrope_ai = props.globals.getNode("ai/models/" ~ device ~ "rope", 1);
1515 var towrope_mod = props.globals.getNode("models", 1);
1516
1517 towrope_ai.getNode("id", 1).setIntValue(4711);
1518 towrope_ai.getNode("callsign", 1).setValue("towrope");
1519 towrope_ai.getNode("valid", 1).setBoolValue(1);
1520 towrope_ai.getNode("position/latitude-deg", 1).setValue(0.);
1521 towrope_ai.getNode("position/longitude-deg", 1).setValue(0.);
1522 towrope_ai.getNode("position/altitude-ft", 1).setValue(0.);
1523 towrope_ai.getNode("orientation/true-heading-deg", 1).setValue(0.);
1524 towrope_ai.getNode("orientation/pitch-deg", 1).setValue(0.);
1525 towrope_ai.getNode("orientation/roll-deg", 1).setValue(0.);
1526
1527 towrope_mod.model = towrope_mod.getChild("model", freeModelid, 1);
1528 towrope_mod.model.getNode("path", 1).setValue(getprop("sim/hitches/" ~ device ~ "/rope/path_to_model") );
1529 towrope_mod.model.getNode("longitude-deg-prop", 1).setValue("ai/models/" ~ device ~ "rope/position/longitude-deg");
1530 towrope_mod.model.getNode("latitude-deg-prop", 1).setValue("ai/models/" ~ device ~ "rope/position/latitude-deg");
1531 towrope_mod.model.getNode("elevation-ft-prop", 1).setValue("ai/models/" ~ device ~ "rope/position/altitude-ft");
1532 towrope_mod.model.getNode("heading-deg-prop", 1).setValue("ai/models/" ~ device ~ "rope/orientation/true-heading-deg");
1533 towrope_mod.model.getNode("roll-deg-prop", 1).setValue("ai/models/" ~ device ~ "rope/orientation/roll-deg");
1534 towrope_mod.model.getNode("pitch-deg-prop", 1).setValue("ai/models/" ~ device ~ "rope/orientation/pitch-deg");
1535 towrope_mod.model.getNode("load", 1).remove();
1536
1537 if (device == "aerotow") {
1538 recreateNodes(aerotow_hash, device);
1539 } elsif (device == "winch") {
1540 recreateNodes(winch_hash, device);
1541 }
1542 } # end towrope exist
1543 }
1544
1545 var recreateNodes = func(hash, device) {
1546 hash.rope.lat = check_or_create("ai/models/" ~ device ~ "rope/position/latitude-deg", 0.0, "DOUBLE");
1547 hash.rope.lon = check_or_create("ai/models/" ~ device ~ "rope/position/longitude-deg", 0.0, "DOUBLE");
1548 hash.rope.alt = check_or_create("ai/models/" ~ device ~ "rope/position/altitude-ft", 0.0, "DOUBLE");
1549 hash.rope.hdg = check_or_create("ai/models/" ~ device ~ "rope/orientation/true-heading-deg", 0.0, "DOUBLE");
1550 hash.rope.pitch = check_or_create("ai/models/" ~ device ~ "rope/orientation/pitch-deg", 0.0, "DOUBLE");
1551 };
1552
1553
1554 # ######################################################################################################################
1555 # get the next free id of "models/model" members
1556 # ######################################################################################################################
1557
1558 var getFreeModelID = func {
1559 #print("getFreeModelID");
1560 var modelid = 0; # next unused id
1561 modelobjects = props.globals.getNode("models", 1).getChildren();
1562 foreach ( var member; modelobjects ) {
1563 if ( (var c = member.getIndex()) != nil) {
1564 modelid = c + 1;
1565 }
1566 }
1567 #print("modelid=",modelid);
1568 return(modelid);
1569 }
1570
1571
1572 # ######################################################################################################################
1573 # close aerotow hitch
1574 # ######################################################################################################################
1575
1576 var closeHitch = func {
1577
1578 #print("closeHitch");
1579
1580 # close only, if
1581 # - not yet closed
1582 # - connected to property-node
1583 # - distance < towrope length
1584
1585 if ( !aerotow_hash.open.getBoolValue() ) return;
1586
1587 var aiNodeID = getprop("sim/hitches/aerotow/tow/connected-to-ai-or-mp-id"); # id of former found ai/mp aircraft
1588 if ( aiNodeID < 1 ) {
1589 setprop("sim/messages/atc", sprintf("No aircraft selected!"));
1590 return;
1591 }
1592
1593 ##################################### calc distance between hitches ######################
1594
1595 ###################### my hitch position #######################
1596
1597 myPosition = geo.aircraft_position();
1598 var my_head_deg = getprop("orientation/heading-deg");
1599 var my_roll_deg = getprop("orientation/roll-deg");
1600 var my_pitch_deg = getprop("orientation/pitch-deg");
1601
1602 # hook coordinates in Yasim-system (x-> nose / y -> left wing / z -> up)
1603 assignHitchLocations("aerotow");
1604 var x = aircraft_settings.aerotow.local_pos[0].getDoubleValue();
1605 var y = aircraft_settings.aerotow.local_pos[1].getDoubleValue();
1606 var z = aircraft_settings.aerotow.local_pos[2].getDoubleValue();
1607 #var x = getprop("sim/hitches/aerotow/local-pos-x");
1608 #var y = getprop("sim/hitches/aerotow/local-pos-y");
1609 #var z = getprop("sim/hitches/aerotow/local-pos-z");
1610
1611 var alpha_deg = my_roll_deg * (1.); # roll clockwise (looking in x-direction) := +
1612 var beta_deg = my_pitch_deg * (-1.); # pitch clockwise (looking in y-direction) := -
1613
1614 # transform hook coordinates
1615 var Xn = PointRotate3D(x:x,y:y,z:z,xr:0.,yr:0.,zr:0.,alpha_deg:alpha_deg,beta_deg:beta_deg,gamma_deg:0.);
1616
1617 var install_distance_m = Xn[0]; # in front of ref-point of glider
1618 var install_side_m = Xn[1];
1619 var install_alt_m = Xn[2];
1620
1621 var myHitch_pos = myPosition.apply_course_distance( my_head_deg , install_distance_m );
1622 var myHitch_pos = myPosition.apply_course_distance( my_head_deg - 90. , install_side_m );
1623 myHitch_pos.set_alt(myPosition.alt() + install_alt_m);
1624
1625 ###################### ai hitch position #######################
1626
1627 var found = 0;
1628
1629 aiobjects = props.globals.getNode("ai/models").getChildren();
1630 foreach (var aimember; aiobjects) {
1631 if ( (var c = aimember.getNode("id") ) != nil ) {
1632 var testprop = c.getValue();
1633 if ( testprop == aiNodeID) {
1634 found = found + 1;
1635
1636 # get coordinates
1637 var ai_lat = aimember.getNode("position/latitude-deg").getValue();
1638 var ai_lon = aimember.getNode("position/longitude-deg").getValue();
1639 var ai_alt = (aimember.getNode("position/altitude-ft").getValue()) * FT2M;
1640
1641 var ai_pitch_deg = aimember.getNode("orientation/pitch-deg").getValue();
1642 var ai_roll_deg = aimember.getNode("orientation/roll-deg").getValue();
1643 var ai_head_deg = aimember.getNode("orientation/true-heading-deg").getValue();
1644
1645 var aiHitchX = aimember.getNode("sim/hitches/aerotow/local-pos-x").getValue();
1646 var aiHitchY = aimember.getNode("sim/hitches/aerotow/local-pos-y").getValue();
1647 var aiHitchZ = aimember.getNode("sim/hitches/aerotow/local-pos-z").getValue();
1648
1649 var aiPosition = geo.Coord.set_latlon( ai_lat, ai_lon, ai_alt );
1650
1651 var alpha_deg = ai_roll_deg * (1.);
1652 var beta_deg = ai_pitch_deg * (-1.);
1653
1654 # transform hook coordinates
1655 var Xn = PointRotate3D(x:aiHitchX,y:aiHitchY,z:aiHitchZ,xr:0.,yr:0.,zr:0.,alpha_deg:alpha_deg,beta_deg:beta_deg,gamma_deg:0.);
1656
1657 var install_distance_m = Xn[0]; # in front of ref-point of glider
1658 var install_side_m = Xn[1];
1659 var install_alt_m = Xn[2];
1660
1661 var aiHitch_pos = aiPosition.apply_course_distance( ai_head_deg , install_distance_m );
1662 var aiHitch_pos = aiPosition.apply_course_distance( ai_head_deg - 90. , install_side_m );
1663 aiHitch_pos.set_alt(aiPosition.alt() + install_alt_m);
1664
1665 var distance = (myHitch_pos.direct_distance_to(aiHitch_pos));
1666
1667 var towlength_m = props.globals.getNode("sim/hitches/aerotow/tow/length").getValue();
1668 if ( distance > towlength_m ) {
1669 var aicallsign = getprop("sim/hitches/aerotow/tow/connected-to-ai-or-mp-callsign");
1670 #setprop("sim/messages/atc", sprintf("Aircraft with callsign %s is too far away (distance is %4.0f meter).",aicallsign, distance));
1671 setprop("sim/messages/atc", sprintf("Selected aircraft is too far away (distance to %s is %4.0f meter).",aicallsign, distance));
1672 return;
1673 }
1674
1675 setprop("sim/hitches/aerotow/tow/dist", distance);
1676
1677 }
1678 }
1679 }
1680
1681 aerotow_hash.open.setBoolValue( 0 );
1682 setprop("sim/hitches/aerotow/mp_oldOpen", "true");
1683
1684 } # End function closeHitch
1685
1686
1687 # ######################################################################################################################
1688 # release hitch
1689 # ######################################################################################################################
1690
1691 var releaseHitch = func (device){
1692
1693 #print("releaseHitch");
1694
1695 if ( fdm == "yasim" ) return; # bypass this routine for Yasim-aircraft
1696
1697 setprop("sim/hitches/" ~ device ~ "/open", "true");
1698
1699 var hitchname = getprop("sim/hitches/" ~ device ~ "/force_name_jsbsim");
1700 setprop("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/magnitude", 0.);
1701 setprop("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/x", 0.);
1702 setprop("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/y", 0.);
1703 setprop("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/z", 0.);
1704
1705 winch_hash.actual_force.setDoubleValue( 0.0 );
1706
1707 if ( device == "aerotow" ) {
1708 setprop("sim/hitches/aerotow/tow/end-force-x", 0.); # MP tow-end forces
1709 setprop("sim/hitches/aerotow/tow/end-force-y", 0.); #
1710 setprop("sim/hitches/aerotow/tow/end-force-z", 0.); #
1711 } else {
1712 # Put automatic winch driver in after-tow phase
1713 phase = 5;
1714 }
1715
1716 } # End function releaseHitch
1717
1718
1719 # ######################################################################################################################
1720 # remove/delete towrope
1721 # ######################################################################################################################
1722
1723 var removeTowrope = func (device){
1724
1725 # remove the towrope from the property tree ai/models
1726 # remove the towrope from the property tree models/
1727
1728 if ( getprop("sim/hitches/" ~ device ~ "/rope/exist") == 1 ) { # does the towrope exist?
1729
1730 # remove 3d model from scenery
1731 # identification is /models/model[x] with x=id_model
1732 var id_model = getprop("sim/hitches/" ~ device ~ "/rope/model_id");
1733 var modelsNode = "models/model[" ~ id_model ~ "]";
1734 props.globals.getNode(modelsNode).remove();
1735 props.globals.getNode("ai/models/" ~ device ~ "rope").remove();
1736 #print("towrope removed");
1737 setprop("sim/hitches/" ~ device ~ "/rope/exist", 0);
1738 }
1739
1740 }
1741
1742
1743 # ######################################################################################################################
1744 # pull in towrope after hitch has been opened
1745 # ######################################################################################################################
1746
1747 var sink_rate_mps = 0.0;
1748
1749 var pull_in_rope = func {
1750
1751
1752 if ( winch_hash.open.getBoolValue() ) {
1753
1754 var current_tow_length = winch_hash.tow.length.getDoubleValue();
1755
1756 if ( current_tow_length > 15.0 ) {
1757
1758 var dt = delta_time.getDoubleValue();
1759
1760 # get position of rope end (former myHitch_pos)
1761 var tow_lat = winch_hash.rope.lat.getDoubleValue();
1762 var tow_lon = winch_hash.rope.lon.getDoubleValue();
1763 var tow_alt_m = winch_hash.rope.alt.getDoubleValue() * FT2M;
1764 # get pitch and heading of rope
1765 var tow_heading_deg = winch_hash.rope.hdg.getDoubleValue();
1766 var tow_pitch_rad = winch_hash.rope.pitch.getDoubleValue() * D2R;
1767
1768 var aiTow_pos = geo.Coord.set_latlon( tow_lat, tow_lon, tow_alt_m );
1769
1770 var delta_length_m = config.winch.max_spool_speed.getDoubleValue() * dt;
1771 var delta_distance_m = delta_length_m * math.cos(tow_pitch_rad);
1772
1773 aiTow_pos = aiTow_pos.apply_course_distance( tow_heading_deg , delta_distance_m );
1774
1775 # Wind influence
1776 aiTow_pos = aiTow_pos.apply_course_distance( wind_from.getDoubleValue() - 180, wind_speed_kt.getDoubleValue() * KT2MPS * dt );
1777
1778 var elevation = geo.elevation( aiTow_pos.lat(), aiTow_pos.lon() );
1779 if( elevation == nil ) elevation = tow_alt_m;
1780 if( tow_alt_m > elevation ){
1781 var delta_alt_m = delta_length_m * math.sin(tow_pitch_rad);
1782 # Calculate vertical sink rate
1783 # assume: drag is exclusively due to the chute, chute size is about 3.14m2 (circle radius 1m), rho is static at 1.29kg/m3
1784 var mass_kg = current_tow_length * config.winch.weight_per_m_kg_m.getDoubleValue();
1785 sink_rate_mps = sink_rate_mps + dt * ( 9.81 - 1.29 * math.pow( sink_rate_mps, 2 ) * 3.14 / mass_kg );
1786
1787 delta_alt_m = delta_alt_m - sink_rate_mps * dt;
1788
1789 aiTow_pos.set_alt( tow_alt_m + delta_alt_m );
1790 } else {
1791 aiTow_pos.set_alt( elevation );
1792 }
1793 #print("aiTow_pos.alt()= ",aiTow_pos.alt()," ",tow_alt_m + delta_alt_m);
1794
1795 # Winch position
1796 var winch_global_pos_x = winch_hash.global_pos[0].getDoubleValue();
1797 var winch_global_pos_y = winch_hash.global_pos[1].getDoubleValue();
1798 var winch_global_pos_z = winch_hash.global_pos[2].getDoubleValue();
1799
1800 var winch_geod = carttogeod(winch_global_pos_x, winch_global_pos_y, winch_global_pos_z);
1801 var aiWinch_pos = geo.Coord.new();
1802 aiWinch_pos.set_latlon( winch_geod[0], winch_geod[1], winch_geod[2] );
1803
1804 var new_tow_length_m = aiTow_pos.direct_distance_to(aiWinch_pos);
1805 var aiHitchpitchto = -math.asin( (aiTow_pos.alt()-aiWinch_pos.alt()) / new_tow_length_m ) * R2D;
1806
1807 # update position of rope
1808 #print("pull in active");
1809 winch_hash.rope.lat.setDoubleValue( aiTow_pos.lat() );
1810 winch_hash.rope.lon.setDoubleValue( aiTow_pos.lon() );
1811 winch_hash.rope.alt.setDoubleValue( aiTow_pos.alt() * M2FT );
1812 # update orientation of rope
1813 winch_hash.rope.hdg.setDoubleValue( aiTow_pos.course_to( aiWinch_pos ) );
1814 winch_hash.rope.pitch.setDoubleValue( aiHitchpitchto );
1815
1816 # update length of rope
1817 winch_hash.tow.length.setDoubleValue( new_tow_length_m );
1818 winch_hash.tow.dist.setDoubleValue( new_tow_length_m );
1819
1820 settimer( pull_in_rope , 0 );
1821 } # end towlength > min
1822 else {
1823 #print("pull in finished!");
1824 winch_hash.actual_spool_speed.setDoubleValue( 0 );
1825 removeTowrope("winch"); # remove towrope model
1826 }
1827
1828 } # end if open
1829
1830 }
1831
1832
1833 # ######################################################################################################################
1834 # set some AI-object default values
1835 # ######################################################################################################################
1836
1837 var setAIObjectDefaults = func (){
1838
1839 # set some default variables, needed to identify, if the found object is an AI-object, a "non-interactiv MP-object or
1840 # an interactive MP-object
1841
1842 var aiNodeID = aerotow_hash.tow.conn_ai_or_mp_id.getIntValue(); # id of former found ai/mp aircraft
1843
1844 aiobjects = props.globals.getNode("ai/models").getChildren();
1845 foreach (var aimember; aiobjects) {
1846 if ( (var c = aimember.getNode("id") ) != nil ) {
1847 var testprop = c.getValue();
1848 if ( testprop == aiNodeID) {
1849 # Set some dummy values. In case of an "interactive"-MP plane
1850 # the correct values will be transmitted in the following loop.
1851 # Create this variables if not present.
1852 # Only set default values if the node does not yet exist.
1853 # If the AI aircraft already has a defined hitch location, this prevents
1854 # it from being overwritten with default values.
1855 if (!aimember.getNode("sim/hitches/aerotow", 0)) {
1856 aimember.getNode("sim/hitches/aerotow/local-pos-x", 1).setValue(-5.);
1857 aimember.getNode("sim/hitches/aerotow/local-pos-y", 1).setValue(0.);
1858 aimember.getNode("sim/hitches/aerotow/local-pos-z", 1).setValue(0.);
1859 }
1860 aimember.getNode("sim/hitches/aerotow/tow/dist", 1).setValue(-1.);
1861 }
1862 }
1863 }
1864
1865 }
1866
1867
1868 # ######################################################################################################################
1869 # place winch model
1870 # ######################################################################################################################
1871
1872 var setWinchPositionAuto = func {
1873
1874 # remove already existing winch model
1875 if ( getprop("/sim/hitches/winch/winch/winch-model-index") != nil ) {
1876 var id_model = getprop("/sim/hitches/winch/winch/winch-model-index");
1877 var modelsNode = "models/model[" ~ id_model ~ "]";
1878 props.globals.getNode(modelsNode).remove();
1879 #print("winch model removed");
1880 }
1881
1882 var initial_length_m = config.winch.initial_tow_length.getDoubleValue();
1883 var ac_pos = geo.aircraft_position(); # get position of aircraft
1884 var ac_hd = orientation[0].getDoubleValue(); # get heading of aircraft
1885
1886 # setup winch
1887 # get initial runway position
1888 var ipos_lat_deg = getprop("sim/presets/latitude-deg");
1889 var ipos_lon_deg = getprop("sim/presets/longitude-deg");
1890 var ipos_hd_deg = getprop("sim/presets/heading-deg");
1891 var ipos_alt_m = geo.elevation(ipos_lat_deg,ipos_lon_deg);
1892 var ipos_geo = geo.Coord.new().set_latlon(ipos_lat_deg, ipos_lon_deg, ipos_alt_m);
1893 # offset to initial position
1894 var deviation = (ac_pos.distance_to(ipos_geo));
1895 # if deviation is too much, locate winch in front of glider, otherwise locate winch to end of runway
1896 if ( deviation > 200) {
1897 var w = ac_pos.apply_course_distance( ac_hd , initial_length_m -1. );
1898 }
1899 else {
1900 var w = ipos_geo.apply_course_distance( ipos_hd_deg , initial_length_m - 1. );
1901 }
1902 var wpalt = geo.elevation(w.lat(), w.lon());
1903 w.set_alt(wpalt);
1904
1905 var winchModel = geo.put_model("Models/Airport/supacat_winch.xml", w.lat(), w.lon(), (w.alt()+0.81), (w.course_to(ac_pos) ));
1906
1907 winch_hash.global_pos[0].setDoubleValue( w.x() );
1908 winch_hash.global_pos[1].setDoubleValue( w.y() );
1909 winch_hash.global_pos[2].setDoubleValue( w.z() );
1910
1911 winch_hash.tow.dist.setDoubleValue( initial_length_m - 1.0 );
1912 winch_hash.tow.length.setDoubleValue( initial_length_m );
1913
1914 #print("name=",winchModel.getName()," Index=",winchModel.getIndex()," Type=",winchModel.getType() );
1915 #print("val=",winchModel.getValue()," children=",winchModel.getChildren()," size=",size(winchModel) );
1916 setprop("/sim/hitches/winch/winch/winch-model-index",winchModel.getIndex() );
1917
1918
1919 winch_hash.rope_breakage = winch_hash.rope_breakage_p.getBoolValue();
1920 if( winch_hash.rope_breakage ){
1921 if( config.winch.rope_breakage.random.getBoolValue() ){
1922 winch_hash.rope_breakage_height_int = 1 + rand() * 300;
1923 } else {
1924 winch_hash.rope_breakage_height_int = config.winch.rope_breakage.height.getDoubleValue();
1925 }
1926 }
1927
1928 winch_hash.loss_of_power.enabled = winch_hash.loss_of_power.enabled_p.getBoolValue();
1929 if( winch_hash.loss_of_power.enabled ){
1930 if( config.winch.loss_of_power.random.getBoolValue() ){
1931 winch_hash.loss_of_power.height_int = 1 + rand() * 300;
1932 } else {
1933 winch_hash.loss_of_power.height_int = config.winch.loss_of_power.height.getDoubleValue();
1934 }
1935 }
1936
1937 write_message( "pilot", "Connected to winch!" );
1938
1939 winch_hash.open.setBoolValue( 0 );
1940
1941 } # End function setWinchPositionAuto
1942
1943
1944 # ######################################################################################################################
1945 # clutch / un-clutch winch
1946 # ######################################################################################################################
1947
1948 var runWinch = func {
1949
1950 if ( !winch_hash.clutched.getBoolValue() ) {
1951 winch_hash.clutched.setBoolValue( 1 );
1952 write_message( "pilot", "Winch clutched!" );
1953 if( alt_agl_ft.getDoubleValue() < 10.0 ){
1954 auto_winch_driver_init();
1955 auto_winch_driver_update.restart( 0.0 );
1956 }
1957 } else {
1958 winch_hash.clutched.setBoolValue( 0 );
1959 write_message( "pilot", "Winch un-clutched!" );
1960 }
1961
1962 } # End function runWinch
1963
1964
1965 # ######################################################################################################################
1966 # release winch
1967 # ######################################################################################################################
1968
1969 var releaseWinch = func {
1970
1971 winch_hash.open.setBoolValue( 1 );
1972
1973 } # End function releaseWinch
1974
1975
1976 # ######################################################################################################################
1977 # assignHitchLocations
1978 # ######################################################################################################################
1979
1980 var assignHitchLocations = func (device){
1981
1982 if ( fdm == "yasim" ) return; # bypass this routine for Yasim-aircraft
1983
1984 if ( getprop("sim/hitches/" ~ device ~ "/decoupled-force-and-rope-locations") ) return; # bypass this routine
1985
1986 #print("assignHitchLocations");
1987
1988 var hitchname = getprop("sim/hitches/" ~ device ~ "/force_name_jsbsim");
1989
1990 # location-x(yz)-in: JSBSim Structural Frame: x points to tail, y points to right wing, z points upward
1991 # local-pos-x(yz): YaSim frame: x points to nose, y points to left wing, z points upward
1992
1993 setprop("sim/hitches/" ~ device ~ "/local-pos-x", - getprop("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/location-x-in") * IN2M );
1994 setprop("sim/hitches/" ~ device ~ "/local-pos-y", - getprop("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/location-y-in") * IN2M );
1995 setprop("sim/hitches/" ~ device ~ "/local-pos-z", getprop("fdm/jsbsim/external_reactions/" ~ hitchname ~ "/location-z-in") * IN2M );
1996
1997 } # End function assignHitchLocations
1998
1999
2000 # ######################################################################################################################
2001 # point transformation
2002 # ######################################################################################################################
2003
2004 var PointRotate3D = func (x,y,z,xr,yr,zr,alpha_deg,beta_deg,gamma_deg){
2005
2006 # ---------------------------------------------------------------------------------
2007 # rotates point (x,y,z) about all 3 cartesian axis
2008 # center of rotation (xr,yr,zr)
2009 # angle of rotation about x-axis = alpha
2010 # angle of rotation about y-axis = beta
2011 # angle of rotation about z-axis = gamma
2012 # delivers new point coordinates (x_new,y_new,z_new)
2013 # ---------------------------------------------------------------------------------
2014 #
2015 #
2016 # Definitions:
2017 # ----------------
2018 #
2019 # x y z
2020 # alpha beta gamma
2021 #
2022 #
2023 # z
2024 # | y
2025 # | /
2026 # |/
2027 # ----->x
2028 #
2029 #----------------------------------------------------------------------------------
2030
2031 # Transformation in rotation-system X_rel = X-Xr = (x-xr, y-yr, z-zr)
2032 var x_rel = x-xr;
2033 var y_rel = y-yr;
2034 var z_rel = z-zr;
2035
2036 # Trigonometry
2037
2038 var alpha_rad = D2R * alpha_deg;
2039 var beta_rad = D2R * beta_deg;
2040 var gamma_rad = D2R * gamma_deg;
2041
2042 var sin_alpha = math.sin(alpha_rad);
2043 var cos_alpha = math.cos(alpha_rad);
2044
2045 var sin_beta = math.sin(beta_rad);
2046 var cos_beta = math.cos(beta_rad);
2047
2048 var sin_gamma = math.sin(gamma_rad);
2049 var cos_gamma = math.cos(gamma_rad);
2050
2051 # Matrices
2052 #
2053 # Rotate about x-axis Rx(alpha)
2054 #
2055 # Rx11 Rx12 Rx13 1 0 0
2056 # Rx(alpha)= Rx21 Rx22 Rx23 = 0 cos(alpha) -sin(alpha)
2057 # Rx31 Rx32 Rx33 0 sin(alpha) cos(alpha)
2058 #
2059 var Rx11 = 1.;
2060 var Rx12 = 0.;
2061 var Rx13 = 0.;
2062 var Rx21 = 0.;
2063 var Rx22 = cos_alpha;
2064 var Rx23 = - sin_alpha;
2065 var Rx31 = 0.;
2066 var Rx32 = sin_alpha;
2067 var Rx33 = cos_alpha;
2068 #
2069 # Rotate about y-axis Ry(beta)
2070 #
2071 # Ry11 Ry12 Ry13 cos(beta) 0 sin(beta)
2072 # Ry(beta)= Ry21 Ry22 Ry23 = 0 1 0
2073 # Ry31 Ry32 Ry33 -sin(beta) 0 cos(beta)
2074 #
2075 var Ry11 = cos_beta;
2076 var Ry12 = 0.;
2077 var Ry13 = sin_beta;
2078 var Ry21 = 0.;
2079 var Ry22 = 1.;
2080 var Ry23 = 0.;
2081 var Ry31 = - sin_beta;
2082 var Ry32 = 0.;
2083 var Ry33 = cos_beta;
2084 #
2085 # Rotate about z-axis Rz(gamma)
2086 #
2087 # Rz11 Rz12 Rz13 cos(gamma) -sin(gamma) 0
2088 # Rz(gamma)= Rz21 Rz22 Rz23 = sin(gamma) cos(gamma) 0
2089 # Rz31 Rz32 Rz33 0 0 1
2090 #
2091 var Rz11 = cos_gamma;
2092 var Rz12 = - sin_gamma;
2093 var Rz13 = 0.;
2094 var Rz21 = sin_gamma;
2095 var Rz22 = cos_gamma;
2096 var Rz23 = 0.;
2097 var Rz31 = 0.;
2098 var Rz32 = 0.;
2099 var Rz33 = 1.;
2100 #
2101 # First rotation about x-axis
2102 # X_x = Rx*X_rel
2103 var x_x = Rx11 * x_rel + Rx12 * y_rel + Rx13 * z_rel;
2104 var y_x = Rx21 * x_rel + Rx22 * y_rel + Rx23 * z_rel;
2105 var z_x = Rx31 * x_rel + Rx32 * y_rel + Rx33 * z_rel;
2106 #
2107 # subsequent rotation about y-axis
2108 # X_xy = Ry*X_x
2109 var x_xy = Ry11 * x_x + Ry12 * y_x + Ry13 * z_x;
2110 var y_xy = Ry21 * x_x + Ry22 * y_x + Ry23 * z_x;
2111 var z_xy = Ry31 * x_x + Ry32 * y_x + Ry33 * z_x;
2112 #
2113 # subsequent rotation about z-axis:
2114 # X_xyz = Rz*X_xy
2115 var x_xyz = Rz11 * x_xy + Rz12 * y_xy + Rz13 * z_xy;
2116 var y_xyz = Rz21 * x_xy + Rz22 * y_xy + Rz23 * z_xy;
2117 var z_xyz = Rz31 * x_xy + Rz32 * y_xy + Rz33 * z_xy;
2118
2119 # Back transformation X_rel = X-Xr = (x-xr, y-yr, z-zr)
2120 var xn = xr + x_xyz;
2121 var yn = yr + y_xyz;
2122 var zn = zr + z_xyz;
2123
2124 var Xn = [xn,yn,zn];
2125
2126 return Xn;
2127
2128 }
2129
2130 var groundspeed_kt = props.globals.getNode("velocities/groundspeed-kt");
2131 var alt_agl_ft = props.globals.getNode("position/altitude-agl-ft");
2132
2133 var weight_lb = nil;
2134 var empty_weight_lb = nil;
2135
2136 var ls = setlistener("/sim/signals/fdm-initialized", func() {
2137 if( fdm == "jsb" ) {
2138 weight_lb = props.globals.getNode("fdm/jsbsim/inertia/weight-lbs");
2139 empty_weight_lb = props.globals.getNode("fdm/jsbsim/inertia/empty-weight-lbs");
2140 } else if ( fdm == "yasim" ) {
2141 weight_lb = props.globals.getNode("yasim/gross-weight-lbs");
2142 }
2143 check_aircraft_tow_settings();
2144 removelistener(ls);
2145 });
2146
2147 var winch_target = 0.8;
2148 var winch_throttle = 0.0;
2149
2150 var winch_idle_force = 1000; # N
2151 var winch_idle_speed = 4; # mps
2152
2153 var acceleration_N_s = nil;
2154 var acceleration_m_s_s = nil;
2155
2156 var phase = 0;
2157 var alpha = 0.0;
2158 var requested_change = 0.0; # positive: request to speed up, negative: request to slow down, 0: auto speed
2159
2160 ###########
2161 ## AUTOMATIC WINCH DRIVER
2162 ###########
2163
2164 # Phase Action description Triggered by
2165 # 0 slowly pull rope until tight (currently omitted) runWinch()
2166 # 1 winch running on idle until aircraft is moving "rope tight" command
2167 # 2 winch accelerating to pre-defined RPM "ready" command
2168 # 3 glider in air, winch adjusts RPM based on angular velocity and faster/slower commands by pilot "clear of ground" command
2169 # 4 winch slowly decelerates to idle aircraft 10-20 deg from automatic release angle
2170 # 5 winch pulls in remaining rope "rope released" command
2171
2172 ## Calculation of predefined targets:
2173 # -weight factor, accounts for additional weight (second pilot, water ballast)
2174 # -wind factors, account for different tow speed due to headwind/tailwind and crosswind
2175
2176 var target_force = 0.0; # N
2177 var target_speed = 0.0; # m/s
2178
2179 var auto_winch_driver_init = func () {
2180 target_force = aircraft_settings.winch.typical_tow_force.getDoubleValue();
2181 target_speed = aircraft_settings.winch.typical_tow_speed.getDoubleValue() / 3.6; # convert kph to mps
2182
2183 # Adjust target speed and force for current wind (increase for tail- and crosswind, reduce for headwind)
2184
2185 # define wind frame: expect from 15 kts tailwind to 15 kts headwind -> translates to a change of +- 0.1
2186 var launch_heading_deg = winch_hash.rope.hdg.getDoubleValue();
2187 var wind_from_heading = wind_from.getDoubleValue();
2188 var wind_speed = wind_speed_kt.getDoubleValue();
2189
2190 # calculate linear component
2191 var rel_wind_heading = launch_heading_deg - wind_from_heading;
2192 var wind_linear = wind_speed * math.cos( rel_wind_heading * D2R ); # negative values mean tailwind
2193 var wind_normal = wind_speed * math.cos( -( 90 - rel_wind_heading ) * D2R ); # negative values mean wind from the left
2194 # compensate for tail/headwind
2195 var wind_linear_add = math.clamp( -wind_linear / 150, -0.1, 0.1 );
2196 # compensate for crosswind
2197 var wind_normal_add = math.min( math.abs( wind_normal ) / 150, 0.1 );
2198
2199 # apply compensation to winch driver variables
2200 target_force += ( wind_linear_add + wind_normal_add ) * config.winch.max_force.getDoubleValue();
2201 target_speed += ( wind_linear_add + wind_normal_add ) * config.winch.max_spool_speed.getDoubleValue();
2202
2203 # set acceleration/deceleration variables
2204 acceleration_N_s = config.winch.force_acceleration.getDoubleValue();
2205 acceleration_m_s_s = config.winch.spool_acceleration.getDoubleValue();
2206
2207 # put winch driver into initial state
2208 phase = 0;
2209 }
2210
2211 var my_pos = nil;
2212 var winch_pos = geo.Coord.new();
2213
2214 var auto_winch_driver = func () {
2215 if( !winch_hash.clutched.getBoolValue() ) return;
2216
2217 var dt = delta_time.getDoubleValue();
2218 if( winch_hash.type != 0 and winch_hash.type != 1 ) {
2219 die("(towing system): FATAL: unsupported winch type!");
2220 }
2221
2222 if( phase == 3 or phase == 4 ){
2223 my_pos = geo.aircraft_position();
2224 # Winch position
2225 var winch_global_pos_x = winch_hash.global_pos[0].getDoubleValue();
2226 var winch_global_pos_y = winch_hash.global_pos[1].getDoubleValue();
2227 var winch_global_pos_z = winch_hash.global_pos[2].getDoubleValue();
2228
2229 var winch_geod = carttogeod(winch_global_pos_x, winch_global_pos_y, winch_global_pos_z);
2230 winch_pos.set_latlon( winch_geod[0], winch_geod[1], winch_geod[2] );
2231
2232 alpha = math.asin( ( my_pos.alt() - winch_pos.alt() ) / winch_pos.direct_distance_to( my_pos ) ) * R2D;
2233 }
2234
2235 if( phase == 0 ) {
2236 phase = 1;
2237 }
2238 if( phase == 1 ) {
2239 if( winch_hash.type == 0 ){
2240 force_setting = winch_idle_force;
2241 } else {
2242 speed_setting = winch_idle_speed;
2243 }
2244
2245 # Condition for going into the next phase
2246 if( groundspeed_kt.getDoubleValue() > 1.0 ) {
2247 phase = 2;
2248 write_message( "launch-signaller", "Ready!" );
2249 write_message( "winch-driver", "Ready");
2250 }
2251 } else if ( phase == 2 or phase == 2.5 ){
2252 # Accelerate to target value
2253 if( winch_hash.type == 0 ){
2254 if( force_setting < target_force ){
2255 force_setting += acceleration_N_s * dt;
2256 } else {
2257 force_setting = target_force;
2258 if( phase == 2.5 ){
2259 phase = 3;
2260 }
2261 }
2262 } else {
2263 if( speed_setting < target_speed ){
2264 speed_setting += acceleration_m_s_s * dt;
2265 } else {
2266 speed_setting = target_speed;
2267 if( phase == 2.5 ){
2268 phase = 3;
2269 }
2270 }
2271 }
2272
2273 # Condition for going into the next phase
2274 if( alt_agl_ft.getDoubleValue() > 10 and phase < 2.5 ){
2275 phase = 2.5; # stay here until we have reached the target value
2276 write_message( "launch-signaller", "Clear of ground!" );
2277 write_message( "winch-driver", "Clear");
2278 }
2279 } else if ( phase == 3 ){
2280
2281 if( requested_change != 0 ){
2282 if( winch_hash.type == 0 ){
2283 force_setting += requested_change * config.winch.max_force.getDoubleValue() * dt;
2284 } else {
2285 speed_setting += requested_change * config.winch.max_spool_speed.getDoubleValue() * dt;
2286 }
2287 requested_change -= requested_change * dt;
2288 if( math.abs( requested_change ) < dt ){
2289 requested_change = 0.0;
2290 }
2291 }
2292
2293 var thr_speed = 0.8;
2294 var thr_angle = 0.5;
2295
2296 if( winch_hash.type == 0 ){
2297
2298 var frac_speed = math.clamp( 0.2 + 1 - ( 1 / ( 1 - thr_speed ) ) * ( ( winch_hash.actual_spool_speed.getDoubleValue() / config.winch.max_spool_speed.getDoubleValue() ) - thr_speed ) * 0.8, 0.0, 1.0 );
2299 var frac_angle = math.clamp( 0.2 + 1 - ( 1 / ( 1 - thr_angle ) ) * ( ( alpha / ( aircraft_settings.winch.automatic_release_angle.getDoubleValue() - 30 ) ) - thr_angle ) * 0.8, 0.0, 1.0 );
2300 force_setting = target_force * frac_speed * frac_angle;
2301
2302 } elsif( winch_hash.type == 1 ){
2303
2304 var frac_speed = math.clamp( 0.2 + 1 - ( 1 / ( 1 - thr_speed ) ) * ( ( winch_hash.actual_spool_speed.getDoubleValue() / config.winch.max_spool_speed.getDoubleValue() ) - thr_speed ) * 0.8, 0.0, 1.0 );
2305 var frac_angle = math.clamp( 0.2 + 1 - ( 1 / ( 1 - thr_angle ) ) * ( ( alpha / ( aircraft_settings.winch.automatic_release_angle.getDoubleValue() - 30 ) ) - thr_angle ) * 0.8, 0.0, 1.0 );
2306 speed_setting = target_speed * frac_speed * frac_angle;
2307
2308 }
2309
2310 if( ( alpha >= ( aircraft_settings.winch.automatic_release_angle.getDoubleValue() - 10 ) ) ) {
2311 phase = 4;
2312 }
2313 } else if ( phase == 4 ){
2314 # Reduce values back to idle
2315 if( speed_setting > winch_idle_speed ){
2316 var deceleration = 3 * math.clamp( ( speed_setting - winch_idle_speed ) / ( aircraft_settings.winch.automatic_release_angle.getDoubleValue() - 2 - alpha ), 5, acceleration_m_s_s );
2317 speed_setting -= deceleration * dt;
2318 } else {
2319 speed_setting = winch_idle_speed;
2320 }
2321
2322 # Condition for going into the next phase is set by the releaseHitch() command
2323 } else if ( phase == 5 ){
2324 if( winch_hash.type == 0 ){
2325 force_setting = winch_idle_force;
2326 } else {
2327 speed_setting = winch_idle_speed;
2328 }
2329 auto_winch_driver_update.stop();
2330 }
2331 }
2332
2333 var auto_winch_driver_update = maketimer( 0.0, auto_winch_driver );
2334 auto_winch_driver_update.simulatedTime = 1;
2335
2336
2337 var winch_faster = func ( d = 0.1 ) {
2338 write_message( "pilot", "Faster!");
2339
2340 # Skip acceleration to pre-set target force/speed in case of pilot command
2341 if( phase == 2.5 ){
2342 phase = 3;
2343 }
2344
2345 if( phase != 3 ){
2346 write_message( "winch-driver", "Unable!");
2347 return;
2348 }
2349
2350 if( ( winch_hash.type == 0 and ( force_setting + d * config.winch.max_force.getDoubleValue() ) <= config.winch.max_force.getDoubleValue() ) or
2351 ( winch_hash.type == 1 and ( speed_setting + d * config.winch.max_spool_speed.getDoubleValue() ) <= config.winch.max_spool_speed.getDoubleValue() ) ){
2352
2353 requested_change = d;
2354 write_message( "winch-driver", "Faster");
2355 } else {
2356 if( winch_hash.type == 0 ){
2357 force_setting = config.winch.max_force.getDoubleValue();
2358 } else {
2359 speed_setting = config.winch.max_spool_speed.getDoubleValue();
2360 }
2361 write_message( "winch-driver", "At maximum!");
2362 }
2363 }
2364 var winch_slower = func ( d = 0.1 ) {
2365 write_message( "pilot", "Slower!");
2366
2367 # Skip acceleration to pre-set target force/speed in case of pilot command
2368 if( phase == 2.5 ){
2369 phase = 3;
2370 }
2371
2372 if( phase != 3 ){
2373 write_message( "winch-driver", "Unable!");
2374 return;
2375 }
2376
2377 if( ( winch_hash.type == 0 and force_setting - d * config.winch.max_force.getDoubleValue() >= winch_idle_force ) or
2378 ( winch_hash.type == 1 and speed_setting - d * config.winch.max_spool_speed.getDoubleValue() >= winch_idle_speed ) ){
2379
2380 requested_change = -d;
2381 write_message( "winch-driver", "Slower");
2382 } else {
2383 if( winch_hash.type == 0 ){
2384 force_setting = winch_idle_force;
2385 } else {
2386 speed_setting = winch_idle_speed;
2387 }
2388 write_message( "winch-driver", "At minimum!");
2389 }
2390 }
2391
2392
2393 ##################################################################################################################################
2394
2395
2396 # todo:
2397 # ------
2398 #
2399 # - animate rope slack
2400 # - dynamic ID for ai-rope-model
2401 #
2402 # Please contact D_NXKT at yahoo.de for bug-reports, suggestions, ...
2403 #