]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/JSBSim.cxx
53be9685a91eeab7c89c6af6a38aa5629f5e4071
[flightgear.git] / src / FDM / JSBSim / JSBSim.cxx
1 // JSBsim.cxx -- interface to the JSBsim flight model
2 //
3 // Written by Curtis Olson, started February 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson  - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/compiler.h>
29
30 #include <stdio.h>      //      size_t
31 #ifdef SG_MATH_EXCEPTION_CLASH
32 #  include <math.h>
33 #endif
34
35 #include STL_STRING
36
37 #include <simgear/constants.h>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/math/sg_geodesy.hxx>
40 #include <simgear/misc/sg_path.hxx>
41 #include <simgear/structure/commands.hxx>
42
43 #include <FDM/flight.hxx>
44
45 #include <Aircraft/aircraft.hxx>
46 #include <Controls/controls.hxx>
47 #include <Main/globals.hxx>
48 #include <Main/fg_props.hxx>
49
50 #include <FDM/JSBSim/FGFDMExec.h>
51 #include <FDM/JSBSim/FGAircraft.h>
52 #include <FDM/JSBSim/FGFCS.h>
53 #include <FDM/JSBSim/FGPropagate.h>
54 #include <FDM/JSBSim/FGState.h>
55 #include <FDM/JSBSim/FGAuxiliary.h>
56 #include <FDM/JSBSim/FGInitialCondition.h>
57 #include <FDM/JSBSim/FGTrim.h>
58 #include <FDM/JSBSim/FGAtmosphere.h>
59 #include <FDM/JSBSim/FGMassBalance.h>
60 #include <FDM/JSBSim/FGAerodynamics.h>
61 #include <FDM/JSBSim/FGLGear.h>
62 #include <FDM/JSBSim/FGPropertyManager.h>
63 #include <FDM/JSBSim/FGEngine.h>
64 #include <FDM/JSBSim/FGPiston.h>
65 #include <FDM/JSBSim/FGGroundCallback.h>
66 #include <FDM/JSBSim/FGTurbine.h>
67 #include <FDM/JSBSim/FGRocket.h>
68 #include <FDM/JSBSim/FGElectric.h>
69 #include <FDM/JSBSim/FGNozzle.h>
70 #include <FDM/JSBSim/FGPropeller.h>
71 #include <FDM/JSBSim/FGRotor.h>
72 #include <FDM/JSBSim/FGTank.h>
73 #include "JSBSim.hxx"
74
75 static inline double
76 FMAX (double a, double b)
77 {
78   return a > b ? a : b;
79 }
80
81 class FGFSGroundCallback : public FGGroundCallback {
82 public:
83   FGFSGroundCallback(FGInterface* ifc) : mInterface(ifc) {}
84   virtual ~FGFSGroundCallback() {}
85
86   /** Get the altitude above sea level depenent on the location. */
87   virtual double GetAltitude(const FGLocation& l) const {
88     double pt[3] = { SG_FEET_TO_METER*l(eX),
89                      SG_FEET_TO_METER*l(eY),
90                      SG_FEET_TO_METER*l(eZ) };
91     double lat, lon, alt;
92     sgCartToGeod( pt, &lat, &lon, &alt);
93     return alt * SG_METER_TO_FEET;
94   }
95
96   /** Compute the altitude above ground. */
97   virtual double GetAGLevel(double t, const FGLocation& l,
98                             FGLocation& cont,
99                             FGColumnVector3& n, FGColumnVector3& v) const {
100     double loc_cart[3] = { l(eX), l(eY), l(eZ) };
101     double contact[3], normal[3], vel[3], lc, ff, agl;
102     int groundtype;
103     mInterface->get_agl_ft(t, loc_cart, contact, normal, vel,
104                            &groundtype, &lc, &ff, &agl);
105     n = l.GetTec2l()*FGColumnVector3( normal[0], normal[1], normal[2] );
106     v = l.GetTec2l()*FGColumnVector3( vel[0], vel[1], vel[2] );
107     cont = FGColumnVector3( contact[0], contact[1], contact[2] );
108     return agl;
109   }
110 private:
111   FGInterface* mInterface;
112 };
113
114 /******************************************************************************/
115
116 FGJSBsim::FGJSBsim( double dt )
117   : FGInterface(dt)
118 {
119     bool result;
120                                 // Set up the debugging level
121                                 // FIXME: this will not respond to
122                                 // runtime changes
123
124                                 // if flight is excluded, don't bother
125     if ((logbuf::get_log_classes() & SG_FLIGHT) != 0) {
126
127                                 // do a rough-and-ready mapping to
128                                 // the levels documented in FGFDMExec.h
129         switch (logbuf::get_log_priority()) {
130         case SG_BULK:
131             FGJSBBase::debug_lvl = 0x1f;
132             break;
133         case SG_DEBUG:
134             FGJSBBase::debug_lvl = 0x0f;
135         case SG_INFO:
136             FGJSBBase::debug_lvl = 0x01;
137             break;
138         case SG_WARN:
139         case SG_ALERT:
140             FGJSBBase::debug_lvl = 0x00;
141             break;
142         }
143     }
144
145     reset_on_crash = fgGetBool("/sim/reset-on-crash", false);
146     crashed = false;
147     fgSetBool("/sim/crashed", false);
148
149     fdmex = new FGFDMExec( (FGPropertyManager*)globals->get_props() );
150
151     // Register ground callback.
152     fdmex->SetGroundCallback( new FGFSGroundCallback(this) );
153
154     State           = fdmex->GetState();
155     Atmosphere      = fdmex->GetAtmosphere();
156     FCS             = fdmex->GetFCS();
157     MassBalance     = fdmex->GetMassBalance();
158     Propulsion      = fdmex->GetPropulsion();
159     Aircraft        = fdmex->GetAircraft();
160     Propagate        = fdmex->GetPropagate();
161     Auxiliary       = fdmex->GetAuxiliary();
162     Aerodynamics    = fdmex->GetAerodynamics();
163     GroundReactions = fdmex->GetGroundReactions();
164
165     fgic=fdmex->GetIC();
166     needTrim=true;
167
168     SGPath aircraft_path( fgGetString("/sim/aircraft-dir") );
169
170     SGPath engine_path( fgGetString("/sim/aircraft-dir") );
171     engine_path.append( "Engine" );
172     State->Setdt( dt );
173
174     result = fdmex->LoadModel( aircraft_path.str(),
175                                engine_path.str(),
176                                fgGetString("/sim/aero"), false );
177
178     if (result) {
179       SG_LOG( SG_FLIGHT, SG_INFO, "  loaded aero.");
180     } else {
181       SG_LOG( SG_FLIGHT, SG_INFO,
182               "  aero does not exist (you may have mis-typed the name).");
183       throw(-1);
184     }
185
186     SG_LOG( SG_FLIGHT, SG_INFO, "" );
187     SG_LOG( SG_FLIGHT, SG_INFO, "" );
188     SG_LOG( SG_FLIGHT, SG_INFO, "After loading aero definition file ..." );
189
190     int Neng = Propulsion->GetNumEngines();
191     SG_LOG( SG_FLIGHT, SG_INFO, "num engines = " << Neng );
192
193     if ( GroundReactions->GetNumGearUnits() <= 0 ) {
194         SG_LOG( SG_FLIGHT, SG_ALERT, "num gear units = "
195                 << GroundReactions->GetNumGearUnits() );
196         SG_LOG( SG_FLIGHT, SG_ALERT, "This is a very bad thing because with 0 gear units, the ground trimming");
197         SG_LOG( SG_FLIGHT, SG_ALERT, "routine (coming up later in the code) will core dump.");
198         SG_LOG( SG_FLIGHT, SG_ALERT, "Halting the sim now, and hoping a solution will present itself soon!");
199         exit(-1);
200     }
201
202     init_gear();
203
204     // Set initial fuel levels if provided.
205     for (unsigned int i = 0; i < Propulsion->GetNumTanks(); i++) {
206       SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
207       if (node->getChild("level-gal_us", 0, false) != 0) {
208         Propulsion->GetTank(i)->SetContents(node->getDoubleValue("level-gal_us") * 6.6);
209       } else {
210         node->setDoubleValue("level-lb", Propulsion->GetTank(i)->GetContents());
211         node->setDoubleValue("level-gal_us", Propulsion->GetTank(i)->GetContents() / 6.6);
212       }
213     }
214     Propulsion->SetFuelFreeze((fgGetNode("/sim/freeze/fuel",true))->getBoolValue());
215
216     fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
217     fgSetDouble("/fdm/trim/throttle",   FCS->GetThrottleCmd(0));
218     fgSetDouble("/fdm/trim/aileron",    FCS->GetDaCmd());
219     fgSetDouble("/fdm/trim/rudder",     FCS->GetDrCmd());
220
221     startup_trim = fgGetNode("/sim/presets/trim", true);
222
223     trimmed = fgGetNode("/fdm/trim/trimmed", true);
224     trimmed->setBoolValue(false);
225
226     pitch_trim = fgGetNode("/fdm/trim/pitch-trim", true );
227     throttle_trim = fgGetNode("/fdm/trim/throttle", true );
228     aileron_trim = fgGetNode("/fdm/trim/aileron", true );
229     rudder_trim = fgGetNode("/fdm/trim/rudder", true );
230
231     stall_warning = fgGetNode("/sim/alarms/stall-warning",true);
232     stall_warning->setDoubleValue(0);
233
234
235     flap_pos_pct=fgGetNode("/surface-positions/flap-pos-norm",true);
236     elevator_pos_pct=fgGetNode("/surface-positions/elevator-pos-norm",true);
237     left_aileron_pos_pct
238         =fgGetNode("/surface-positions/left-aileron-pos-norm",true);
239     right_aileron_pos_pct
240         =fgGetNode("/surface-positions/right-aileron-pos-norm",true);
241     rudder_pos_pct=fgGetNode("/surface-positions/rudder-pos-norm",true);
242     speedbrake_pos_pct
243         =fgGetNode("/surface-positions/speedbrake-pos-norm",true);
244     spoilers_pos_pct=fgGetNode("/surface-positions/spoilers-pos-norm",true);
245
246     elevator_pos_pct->setDoubleValue(0);
247     left_aileron_pos_pct->setDoubleValue(0);
248     right_aileron_pos_pct->setDoubleValue(0);
249     rudder_pos_pct->setDoubleValue(0);
250     flap_pos_pct->setDoubleValue(0);
251     speedbrake_pos_pct->setDoubleValue(0);
252     spoilers_pos_pct->setDoubleValue(0);
253
254     temperature = fgGetNode("/environment/temperature-degc",true);
255     pressure = fgGetNode("/environment/pressure-inhg",true);
256     density = fgGetNode("/environment/density-slugft3",true);
257     turbulence_gain = fgGetNode("/environment/turbulence/magnitude-norm",true);
258     turbulence_rate = fgGetNode("/environment/turbulence/rate-hz",true);
259
260     wind_from_north= fgGetNode("/environment/wind-from-north-fps",true);
261     wind_from_east = fgGetNode("/environment/wind-from-east-fps" ,true);
262     wind_from_down = fgGetNode("/environment/wind-from-down-fps" ,true);
263
264     for (unsigned int i = 0; i < Propulsion->GetNumEngines(); i++) {
265       SGPropertyNode * node = fgGetNode("engines/engine", i, true);
266       Propulsion->GetEngine(i)->GetThruster()->SetRPM(node->getDoubleValue("rpm") /
267                      Propulsion->GetEngine(i)->GetThruster()->GetGearRatio());
268     }
269 }
270
271 /******************************************************************************/
272 FGJSBsim::~FGJSBsim(void)
273 {
274   delete fdmex;
275 }
276
277 /******************************************************************************/
278
279 // Initialize the JSBsim flight model, dt is the time increment for
280 // each subsequent iteration through the EOM
281
282 void FGJSBsim::init()
283 {
284     double tmp;
285
286     SG_LOG( SG_FLIGHT, SG_INFO, "Starting and initializing JSBsim" );
287
288     // Explicitly call the superclass's
289     // init method first.
290
291 #ifdef FG_WEATHERCM
292     Atmosphere->UseInternal();
293 #else
294     if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
295       Atmosphere->UseExternal();
296       Atmosphere->SetExTemperature(
297                   9.0/5.0*(temperature->getDoubleValue()+273.15) );
298       Atmosphere->SetExPressure(pressure->getDoubleValue()*70.726566);
299       Atmosphere->SetExDensity(density->getDoubleValue());
300
301       tmp = turbulence_gain->getDoubleValue();
302       Atmosphere->SetTurbGain(tmp * tmp * 100.0);
303
304       tmp = turbulence_rate->getDoubleValue();
305       Atmosphere->SetTurbRate(tmp);
306
307     } else {
308       Atmosphere->UseInternal();
309     }
310 #endif
311
312     fgic->SetVnorthFpsIC( wind_from_north->getDoubleValue() );
313     fgic->SetVeastFpsIC( wind_from_east->getDoubleValue() );
314     fgic->SetVdownFpsIC( wind_from_down->getDoubleValue() );
315
316     //Atmosphere->SetExTemperature(get_Static_temperature());
317     //Atmosphere->SetExPressure(get_Static_pressure());
318     //Atmosphere->SetExDensity(get_Density());
319     SG_LOG(SG_FLIGHT,SG_INFO,"T,p,rho: " << fdmex->GetAtmosphere()->GetTemperature()
320      << ", " << fdmex->GetAtmosphere()->GetPressure()
321      << ", " << fdmex->GetAtmosphere()->GetDensity() );
322
323     if (fgGetBool("/sim/presets/running")) {
324           for (int i=0; i < Propulsion->GetNumEngines(); i++) {
325             SGPropertyNode * node = fgGetNode("engines/engine", i, true);
326             node->setBoolValue("running", true);
327             Propulsion->GetEngine(i)->SetRunning(true);
328           }
329     }
330
331     common_init();
332
333     copy_to_JSBsim();
334     fdmex->RunIC();     //loop JSBSim once w/o integrating
335     copy_from_JSBsim(); //update the bus
336
337     SG_LOG( SG_FLIGHT, SG_INFO, "  Initialized JSBSim with:" );
338
339     switch(fgic->GetSpeedSet()) {
340     case setned:
341         SG_LOG(SG_FLIGHT,SG_INFO, "  Vn,Ve,Vd= "
342                << Propagate->GetVel(eNorth) << ", "
343                << Propagate->GetVel(eEast) << ", "
344                << Propagate->GetVel(eDown) << " ft/s");
345     break;
346     case setuvw:
347         SG_LOG(SG_FLIGHT,SG_INFO, "  U,V,W= "
348                << Propagate->GetUVW(1) << ", "
349                << Propagate->GetUVW(2) << ", "
350                << Propagate->GetUVW(3) << " ft/s");
351     break;
352     case setmach:
353         SG_LOG(SG_FLIGHT,SG_INFO, "  Mach: "
354                << Auxiliary->GetMach() );
355     break;
356     case setvc:
357     default:
358         SG_LOG(SG_FLIGHT,SG_INFO, "  Indicated Airspeed: "
359                << Auxiliary->GetVcalibratedKTS() << " knots" );
360     break;
361     }
362
363     stall_warning->setDoubleValue(0);
364
365     SG_LOG( SG_FLIGHT, SG_INFO, "  Bank Angle: "
366             << Propagate->GetEuler(ePhi)*RADTODEG << " deg" );
367     SG_LOG( SG_FLIGHT, SG_INFO, "  Pitch Angle: "
368             << Propagate->GetEuler(eTht)*RADTODEG << " deg" );
369     SG_LOG( SG_FLIGHT, SG_INFO, "  True Heading: "
370             << Propagate->GetEuler(ePsi)*RADTODEG << " deg" );
371     SG_LOG( SG_FLIGHT, SG_INFO, "  Latitude: "
372             << Propagate->GetLocation().GetLatitudeDeg() << " deg" );
373     SG_LOG( SG_FLIGHT, SG_INFO, "  Longitude: "
374             << Propagate->GetLocation().GetLongitudeDeg() << " deg" );
375     SG_LOG( SG_FLIGHT, SG_INFO, "  Altitude: "
376             << Propagate->Geth() << " feet" );
377     SG_LOG( SG_FLIGHT, SG_INFO, "  loaded initial conditions" );
378
379     SG_LOG( SG_FLIGHT, SG_INFO, "  set dt" );
380
381     SG_LOG( SG_FLIGHT, SG_INFO, "Finished initializing JSBSim" );
382
383     SG_LOG( SG_FLIGHT, SG_INFO, "FGControls::get_gear_down()= " <<
384                                   globals->get_controls()->get_gear_down() );
385 }
386
387 /******************************************************************************/
388
389 // Run an iteration of the EOM (equations of motion)
390
391 void FGJSBsim::update( double dt )
392 {
393     if (is_suspended())
394       return;
395
396     int multiloop = _calc_multiloop(dt);
397
398     int i;
399
400     // Compute the radius of the aircraft. That is the radius of a ball
401     // where all gear units are in. At the moment it is at least 10ft ...
402     double acrad = 10.0;
403     int n_gears = GroundReactions->GetNumGearUnits();
404     for (i=0; i<n_gears; ++i) {
405       FGColumnVector3 bl = GroundReactions->GetGearUnit(i)->GetBodyLocation();
406       double r = bl.Magnitude();
407       if (acrad < r)
408         acrad = r;
409     }
410
411     // Compute the potential movement of this aircraft and query for the
412     // ground in this area.
413     double groundCacheRadius = acrad + 2*dt*Propagate->GetUVW().Magnitude();
414     double alt, slr, lat, lon;
415     FGColumnVector3 cart = Auxiliary->GetLocationVRP();
416     if ( needTrim && startup_trim->getBoolValue() ) {
417       alt = fgic->GetAltitudeFtIC();
418       slr = fgic->GetSeaLevelRadiusFtIC();
419       lat = fgic->GetLatitudeDegIC() * SGD_DEGREES_TO_RADIANS;
420       lon = fgic->GetLongitudeDegIC() * SGD_DEGREES_TO_RADIANS;
421       cart = FGLocation(lon, lat, alt+slr);
422     }
423     double cart_pos[3] = { cart(1), cart(2), cart(3) };
424     bool cache_ok = prepare_ground_cache_ft( State->Getsim_time(), cart_pos,
425                                              groundCacheRadius );
426     if (!cache_ok) {
427       SG_LOG(SG_FLIGHT, SG_WARN,
428              "FGInterface is being called without scenery below the aircraft!");
429       cout << "altitude         = " << alt << endl;
430       cout << "sea level radius = " << slr << endl;
431       cout << "latitude         = " << lat << endl;
432       cout << "longitude        = " << lon << endl;
433       //return;
434     }
435
436     copy_to_JSBsim();
437
438     trimmed->setBoolValue(false);
439
440     if ( needTrim ) {
441       if ( startup_trim->getBoolValue() ) {
442         double contact[3], dummy[3], lc, ff, agl;
443         int groundtype;
444         get_agl_ft(State->Getsim_time(), cart_pos, contact,
445                    dummy, dummy, &groundtype, &lc, &ff, &agl);
446         double terrain_alt = sqrt(contact[0]*contact[0] + contact[1]*contact[1]
447              + contact[2]*contact[2]) - fgic->GetSeaLevelRadiusFtIC();
448
449         SG_LOG(SG_FLIGHT, SG_INFO,
450           "Ready to trim, terrain altitude is: "
451             << terrain_alt * SG_METER_TO_FEET );
452
453         fgic->SetTerrainAltitudeFtIC( terrain_alt );
454         do_trim();
455       } else {
456         fdmex->RunIC();  //apply any changes made through the set_ functions
457       }
458       needTrim = false;
459     }
460
461     for ( i=0; i < multiloop; i++ ) {
462       fdmex->Run();
463     }
464
465     FGJSBBase::Message* msg;
466     while (fdmex->ReadMessage()) {
467       msg = fdmex->ProcessMessage();
468       switch (msg->type) {
469       case FGJSBBase::Message::eText:
470         SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text );
471         break;
472       case FGJSBBase::Message::eBool:
473         SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->bVal );
474         break;
475       case FGJSBBase::Message::eInteger:
476         SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->iVal );
477         break;
478       case FGJSBBase::Message::eDouble:
479         SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->dVal );
480         break;
481       default:
482         SG_LOG( SG_FLIGHT, SG_INFO, "Unrecognized message type." );
483         break;
484       }
485     }
486
487     // translate JSBsim back to FG structure so that the
488     // autopilot (and the rest of the sim can use the updated values
489     copy_from_JSBsim();
490 }
491
492 /******************************************************************************/
493
494 // Convert from the FGInterface struct to the JSBsim generic_ struct
495
496 bool FGJSBsim::copy_to_JSBsim()
497 {
498     double tmp;
499     unsigned int i;
500
501     // copy control positions into the JSBsim structure
502
503     FCS->SetDaCmd( globals->get_controls()->get_aileron());
504     FCS->SetRollTrimCmd( globals->get_controls()->get_aileron_trim() );
505     FCS->SetDeCmd( globals->get_controls()->get_elevator());
506     FCS->SetPitchTrimCmd( globals->get_controls()->get_elevator_trim() );
507     FCS->SetDrCmd( -globals->get_controls()->get_rudder() );
508     FCS->SetYawTrimCmd( -globals->get_controls()->get_rudder_trim() );
509     // FIXME: make that get_steering work
510 //     FCS->SetDsCmd( globals->get_controls()->get_steering()/80.0 );
511     FCS->SetDsCmd( globals->get_controls()->get_rudder() );
512     FCS->SetDfCmd( globals->get_controls()->get_flaps() );
513     FCS->SetDsbCmd( globals->get_controls()->get_speedbrake() );
514     FCS->SetDspCmd( globals->get_controls()->get_spoilers() );
515
516         // Parking brake sets minimum braking
517         // level for mains.
518     double parking_brake = globals->get_controls()->get_brake_parking();
519     FCS->SetLBrake(FMAX(globals->get_controls()->get_brake_left(), parking_brake));
520     FCS->SetRBrake(FMAX(globals->get_controls()->get_brake_right(), parking_brake));
521     FCS->SetCBrake( 0.0 );
522     // FCS->SetCBrake( globals->get_controls()->get_brake(2) );
523
524     FCS->SetGearCmd( globals->get_controls()->get_gear_down());
525     for (i = 0; i < Propulsion->GetNumEngines(); i++) {
526       SGPropertyNode * node = fgGetNode("engines/engine", i, true);
527
528       FCS->SetThrottleCmd(i, globals->get_controls()->get_throttle(i));
529       FCS->SetMixtureCmd(i, globals->get_controls()->get_mixture(i));
530       FCS->SetPropAdvanceCmd(i, globals->get_controls()->get_prop_advance(i));
531
532       switch (Propulsion->GetEngine(i)->GetType()) {
533       case FGEngine::etPiston:
534         { // FGPiston code block
535         FGPiston* eng = (FGPiston*)Propulsion->GetEngine(i);
536         eng->SetMagnetos( globals->get_controls()->get_magnetos(i) );
537         break;
538         } // end FGPiston code block
539       case FGEngine::etTurbine:
540         { // FGTurbine code block
541         FGTurbine* eng = (FGTurbine*)Propulsion->GetEngine(i);
542         eng->SetAugmentation( globals->get_controls()->get_augmentation(i) );
543         eng->SetReverse( globals->get_controls()->get_reverser(i) );
544         eng->SetInjection( globals->get_controls()->get_water_injection(i) );
545         eng->SetCutoff( globals->get_controls()->get_cutoff(i) );
546         eng->SetIgnition( globals->get_controls()->get_ignition(i) );
547         break;
548         } // end FGTurbine code block
549       case FGEngine::etRocket:
550         { // FGRocket code block
551         FGRocket* eng = (FGRocket*)Propulsion->GetEngine(i);
552         break;
553         } // end FGRocket code block
554       }
555
556       { // FGEngine code block
557       FGEngine* eng = Propulsion->GetEngine(i);
558
559       eng->SetStarter( globals->get_controls()->get_starter(i) );
560       eng->SetRunning( node->getBoolValue("running") );
561       } // end FGEngine code block
562     }
563
564
565     Propagate->SetSeaLevelRadius( get_Sea_level_radius() );
566
567     Atmosphere->SetExTemperature(
568                   9.0/5.0*(temperature->getDoubleValue()+273.15) );
569     Atmosphere->SetExPressure(pressure->getDoubleValue()*70.726566);
570     Atmosphere->SetExDensity(density->getDoubleValue());
571
572     tmp = turbulence_gain->getDoubleValue();
573     Atmosphere->SetTurbGain(tmp * tmp * 100.0);
574
575     tmp = turbulence_rate->getDoubleValue();
576     Atmosphere->SetTurbRate(tmp);
577
578     Atmosphere->SetWindNED( wind_from_north->getDoubleValue(),
579                             wind_from_east->getDoubleValue(),
580                             wind_from_down->getDoubleValue() );
581 //    SG_LOG(SG_FLIGHT,SG_INFO, "Wind NED: "
582 //                  << get_V_north_airmass() << ", "
583 //                  << get_V_east_airmass()  << ", "
584 //                  << get_V_down_airmass() );
585
586     for (i = 0; i < Propulsion->GetNumTanks(); i++) {
587       SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
588       FGTank * tank = Propulsion->GetTank(i);
589       tank->SetContents(node->getDoubleValue("level-gal_us") * 6.6);
590 //       tank->SetContents(node->getDoubleValue("level-lb"));
591     }
592     SGPropertyNode* node = fgGetNode("/systems/refuel", true);
593     Propulsion->SetRefuel(node->getDoubleValue("contact"));
594     Propulsion->SetFuelFreeze((fgGetNode("/sim/freeze/fuel",true))->getBoolValue());
595
596     return true;
597 }
598
599 /******************************************************************************/
600
601 // Convert from the JSBsim generic_ struct to the FGInterface struct
602
603 bool FGJSBsim::copy_from_JSBsim()
604 {
605     unsigned int i, j;
606 /*
607     _set_Inertias( MassBalance->GetMass(),
608                    MassBalance->GetIxx(),
609                    MassBalance->GetIyy(),
610                    MassBalance->GetIzz(),
611                    MassBalance->GetIxz() );
612 */
613     _set_CG_Position( MassBalance->GetXYZcg(1),
614                       MassBalance->GetXYZcg(2),
615                       MassBalance->GetXYZcg(3) );
616
617     _set_Accels_Body( Aircraft->GetBodyAccel(1),
618                       Aircraft->GetBodyAccel(2),
619                       Aircraft->GetBodyAccel(3) );
620
621     _set_Accels_CG_Body_N ( Aircraft->GetNcg(1),
622                             Aircraft->GetNcg(2),
623                             Aircraft->GetNcg(3) );
624
625     _set_Accels_Pilot_Body( Auxiliary->GetPilotAccel(1),
626                             Auxiliary->GetPilotAccel(2),
627                             Auxiliary->GetPilotAccel(3) );
628
629     _set_Nlf( Aircraft->GetNlf() );
630
631     // Velocities
632
633     _set_Velocities_Local( Propagate->GetVel(eNorth),
634                            Propagate->GetVel(eEast),
635                            Propagate->GetVel(eDown) );
636
637     _set_Velocities_Wind_Body( Propagate->GetUVW(1),
638                                Propagate->GetUVW(2),
639                                Propagate->GetUVW(3) );
640
641     // Make the HUD work ...
642     _set_Velocities_Ground( Propagate->GetVel(eNorth),
643                             Propagate->GetVel(eEast),
644                             -Propagate->GetVel(eDown) );
645
646     _set_V_rel_wind( Auxiliary->GetVt() );
647
648     _set_V_equiv_kts( Auxiliary->GetVequivalentKTS() );
649
650     _set_V_calibrated_kts( Auxiliary->GetVcalibratedKTS() );
651
652     _set_V_ground_speed( Auxiliary->GetVground() );
653
654     _set_Omega_Body( Propagate->GetPQR(eP),
655                      Propagate->GetPQR(eQ),
656                      Propagate->GetPQR(eR) );
657
658     _set_Euler_Rates( Auxiliary->GetEulerRates(ePhi),
659                       Auxiliary->GetEulerRates(eTht),
660                       Auxiliary->GetEulerRates(ePsi) );
661
662     _set_Mach_number( Auxiliary->GetMach() );
663
664     // Positions of Visual Reference Point
665     FGLocation l = Auxiliary->GetLocationVRP();
666     _updateGeocentricPosition( l.GetLatitude(), l.GetLongitude(),
667                                l.GetRadius() - get_Sea_level_radius() );
668
669     _set_Altitude_AGL( Propagate->GetDistanceAGL() );
670     {
671       double loc_cart[3] = { l(eX), l(eY), l(eZ) };
672       double contact[3], d[3], sd, t;
673       int id;
674       is_valid_m(&t, d, &sd);
675       get_agl_ft(t, loc_cart, contact, d, d, &id, &sd, &sd, &sd);
676       double rwrad
677         = FGColumnVector3( contact[0], contact[1], contact[2] ).Magnitude();
678       _set_Runway_altitude( rwrad - get_Sea_level_radius() );
679     }
680
681     _set_Euler_Angles( Propagate->GetEuler(ePhi),
682                        Propagate->GetEuler(eTht),
683                        Propagate->GetEuler(ePsi) );
684
685     _set_Alpha( Auxiliary->Getalpha() );
686     _set_Beta( Auxiliary->Getbeta() );
687
688
689     _set_Gamma_vert_rad( Auxiliary->GetGamma() );
690
691     _set_Earth_position_angle( Auxiliary->GetEarthPositionAngle() );
692
693     _set_Climb_Rate( Propagate->Gethdot() );
694
695     const FGMatrix33& Tl2b = Propagate->GetTl2b();
696     for ( i = 1; i <= 3; i++ ) {
697         for ( j = 1; j <= 3; j++ ) {
698             _set_T_Local_to_Body( i, j, Tl2b(i,j) );
699         }
700     }
701
702     // Copy the engine values from JSBSim.
703     for ( i=0; i < Propulsion->GetNumEngines(); i++ ) {
704       SGPropertyNode * node = fgGetNode("engines/engine", i, true);
705       char buf[30];
706       sprintf(buf, "engines/engine[%d]/thruster", i);
707       SGPropertyNode * tnode = fgGetNode(buf, true);
708       FGThruster * thruster = Propulsion->GetEngine(i)->GetThruster();
709
710       switch (Propulsion->GetEngine(i)->GetType()) {
711       case FGEngine::etPiston:
712         { // FGPiston code block
713         FGPiston* eng = (FGPiston*)Propulsion->GetEngine(i);
714         node->setDoubleValue("egt-degf", eng->getExhaustGasTemp_degF());
715         node->setDoubleValue("oil-temperature-degf", eng->getOilTemp_degF());
716         node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi());
717         node->setDoubleValue("mp-osi", eng->getManifoldPressure_inHg());
718         node->setDoubleValue("cht-degf", eng->getCylinderHeadTemp_degF());
719         node->setDoubleValue("rpm", eng->getRPM());
720         } // end FGPiston code block
721         break;
722       case FGEngine::etRocket:
723         { // FGRocket code block
724         FGRocket* eng = (FGRocket*)Propulsion->GetEngine(i);
725         } // end FGRocket code block
726         break;
727       case FGEngine::etTurbine:
728         { // FGTurbine code block
729         FGTurbine* eng = (FGTurbine*)Propulsion->GetEngine(i);
730         node->setDoubleValue("n1", eng->GetN1());
731         node->setDoubleValue("n2", eng->GetN2());
732         node->setDoubleValue("egt_degf", 32 + eng->GetEGT()*9/5);
733         node->setBoolValue("augmentation", eng->GetAugmentation());
734         node->setBoolValue("water-injection", eng->GetInjection());
735         node->setBoolValue("ignition", eng->GetIgnition());
736         node->setDoubleValue("nozzle-pos-norm", eng->GetNozzle());
737         node->setDoubleValue("inlet-pos-norm", eng->GetInlet());
738         node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi());
739         node->setBoolValue("reversed", eng->GetReversed());
740         node->setBoolValue("cutoff", eng->GetCutoff());
741         node->setDoubleValue("epr", eng->GetEPR());
742         globals->get_controls()->set_reverser(i, eng->GetReversed() );
743         globals->get_controls()->set_cutoff(i, eng->GetCutoff() );
744         globals->get_controls()->set_water_injection(i, eng->GetInjection() );
745         globals->get_controls()->set_augmentation(i, eng->GetAugmentation() );
746         } // end FGTurbine code block
747         break;
748       case FGEngine::etElectric:
749         { // FGElectric code block
750         FGElectric* eng = (FGElectric*)Propulsion->GetEngine(i);
751         node->setDoubleValue("rpm", eng->getRPM());
752         } // end FGElectric code block
753         break;
754       }
755
756       { // FGEngine code block
757       FGEngine* eng = Propulsion->GetEngine(i);
758       node->setDoubleValue("fuel-flow-gph", eng->getFuelFlow_gph());
759       node->setDoubleValue("thrust_lb", thruster->GetThrust());
760       node->setDoubleValue("fuel-flow_pph", eng->getFuelFlow_pph());
761       node->setBoolValue("running", eng->GetRunning());
762       node->setBoolValue("starter", eng->GetStarter());
763       node->setBoolValue("cranking", eng->GetCranking());
764       globals->get_controls()->set_starter(i, eng->GetStarter() );
765       } // end FGEngine code block
766
767       switch (thruster->GetType()) {
768       case FGThruster::ttNozzle:
769         { // FGNozzle code block
770         FGNozzle* noz = (FGNozzle*)thruster;
771         } // end FGNozzle code block
772         break;
773       case FGThruster::ttPropeller:
774         { // FGPropeller code block
775         FGPropeller* prop = (FGPropeller*)thruster;
776         tnode->setDoubleValue("rpm", thruster->GetRPM());
777         tnode->setDoubleValue("pitch", prop->GetPitch());
778         tnode->setDoubleValue("torque", prop->GetTorque());
779         } // end FGPropeller code block
780         break;
781       case FGThruster::ttRotor:
782         { // FGRotor code block
783         FGRotor* rotor = (FGRotor*)thruster;
784         } // end FGRotor code block
785         break;
786       case FGThruster::ttDirect:
787         { // Direct code block
788         } // end Direct code block
789         break;
790       }
791
792     }
793
794     // Copy the fuel levels from JSBSim if fuel
795     // freeze not enabled.
796     if ( ! Propulsion->GetFuelFreeze() ) {
797       for (i = 0; i < Propulsion->GetNumTanks(); i++) {
798         SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
799         FGTank* tank = Propulsion->GetTank(i);
800         double contents = tank->GetContents();
801         double temp = tank->GetTemperature_degC();
802         node->setDoubleValue("level-gal_us", contents/6.6);
803         node->setDoubleValue("level-lb", contents);
804         if (temp != -9999.0) node->setDoubleValue("temperature_degC", temp);
805       }
806     }
807
808     update_gear();
809
810     stall_warning->setDoubleValue( Aerodynamics->GetStallWarn() );
811
812     elevator_pos_pct->setDoubleValue( FCS->GetDePos(ofNorm) );
813     left_aileron_pos_pct->setDoubleValue( FCS->GetDaLPos(ofNorm) );
814     right_aileron_pos_pct->setDoubleValue( FCS->GetDaRPos(ofNorm) );
815     rudder_pos_pct->setDoubleValue( -1*FCS->GetDrPos(ofNorm) );
816     flap_pos_pct->setDoubleValue( FCS->GetDfPos(ofNorm) );
817     speedbrake_pos_pct->setDoubleValue( FCS->GetDsbPos(ofNorm) );
818     spoilers_pos_pct->setDoubleValue( FCS->GetDspPos(ofNorm) );
819
820     // crashed (altitude AGL < 0)
821     if (get_Altitude_AGL() < 0.0) {
822       crash_message = "Attempted to fly under ground.";
823       crash_handler();
824     }
825
826     return true;
827 }
828
829
830 bool FGJSBsim::ToggleDataLogging(void)
831 {
832     return fdmex->GetOutput()->Toggle();
833 }
834
835
836 bool FGJSBsim::ToggleDataLogging(bool state)
837 {
838     if (state) {
839       fdmex->GetOutput()->Enable();
840       return true;
841     } else {
842       fdmex->GetOutput()->Disable();
843       return false;
844     }
845 }
846
847
848 //Positions
849 void FGJSBsim::set_Latitude(double lat)
850 {
851     static const SGPropertyNode *altitude = fgGetNode("/position/altitude-ft");
852     double alt;
853     double sea_level_radius_meters, lat_geoc;
854
855     // In case we're not trimming
856     FGInterface::set_Latitude(lat);
857
858     if ( altitude->getDoubleValue() > -9990 ) {
859       alt = altitude->getDoubleValue();
860     } else {
861       alt = 0.0;
862     }
863
864     update_ic();
865     SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat );
866     SG_LOG(SG_FLIGHT,SG_INFO," cur alt (ft) =  " << alt );
867
868     sgGeodToGeoc( lat, alt * SG_FEET_TO_METER,
869                       &sea_level_radius_meters, &lat_geoc );
870     _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET  );
871     fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET  );
872     fgic->SetLatitudeRadIC( lat_geoc );
873     needTrim=true;
874 }
875
876
877 void FGJSBsim::set_Longitude(double lon)
878 {
879     SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon );
880
881     // In case we're not trimming
882     FGInterface::set_Longitude(lon);
883
884     update_ic();
885     fgic->SetLongitudeRadIC( lon );
886     needTrim=true;
887 }
888
889 void FGJSBsim::set_Altitude(double alt)
890 {
891     static const SGPropertyNode *latitude = fgGetNode("/position/latitude-deg");
892
893     double sea_level_radius_meters,lat_geoc;
894
895     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt );
896     SG_LOG(SG_FLIGHT,SG_INFO, "  lat (deg) = " << latitude->getDoubleValue() );
897
898     // In case we're not trimming
899     FGInterface::set_Altitude(alt);
900
901     update_ic();
902     sgGeodToGeoc( latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS, alt,
903                   &sea_level_radius_meters, &lat_geoc);
904     _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET  );
905     fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
906     SG_LOG(SG_FLIGHT, SG_INFO,
907           "Terrain altitude: " << cur_fdm_state->get_Runway_altitude() * SG_METER_TO_FEET );
908     fgic->SetLatitudeRadIC( lat_geoc );
909     fgic->SetAltitudeFtIC(alt);
910     needTrim=true;
911 }
912
913 void FGJSBsim::set_V_calibrated_kts(double vc)
914 {
915     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " <<  vc );
916
917     // In case we're not trimming
918     FGInterface::set_V_calibrated_kts(vc);
919
920     update_ic();
921     fgic->SetVcalibratedKtsIC(vc);
922     needTrim=true;
923 }
924
925 void FGJSBsim::set_Mach_number(double mach)
926 {
927     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " <<  mach );
928
929     // In case we're not trimming
930     FGInterface::set_Mach_number(mach);
931
932     update_ic();
933     fgic->SetMachIC(mach);
934     needTrim=true;
935 }
936
937 void FGJSBsim::set_Velocities_Local( double north, double east, double down )
938 {
939     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: "
940        << north << ", " <<  east << ", " << down );
941
942     // In case we're not trimming
943     FGInterface::set_Velocities_Local(north, east, down);
944
945     update_ic();
946     fgic->SetVnorthFpsIC(north);
947     fgic->SetVeastFpsIC(east);
948     fgic->SetVdownFpsIC(down);
949     needTrim=true;
950 }
951
952 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w)
953 {
954     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
955        << u << ", " <<  v << ", " <<  w );
956
957     // In case we're not trimming
958     FGInterface::set_Velocities_Wind_Body(u, v, w);
959
960     update_ic();
961     fgic->SetUBodyFpsIC(u);
962     fgic->SetVBodyFpsIC(v);
963     fgic->SetWBodyFpsIC(w);
964     needTrim=true;
965 }
966
967 //Euler angles
968 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi )
969 {
970     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: "
971        << phi << ", " << theta << ", " << psi );
972
973     // In case we're not trimming
974     FGInterface::set_Euler_Angles(phi, theta, psi);
975
976     update_ic();
977     fgic->SetPitchAngleRadIC(theta);
978     fgic->SetRollAngleRadIC(phi);
979     fgic->SetTrueHeadingRadIC(psi);
980     needTrim=true;
981 }
982
983 //Flight Path
984 void FGJSBsim::set_Climb_Rate( double roc)
985 {
986     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
987
988     // In case we're not trimming
989     FGInterface::set_Climb_Rate(roc);
990
991     update_ic();
992     //since both climb rate and flight path angle are set in the FG
993     //startup sequence, something is needed to keep one from cancelling
994     //out the other.
995     if( !(fabs(roc) > 1 && fabs(fgic->GetFlightPathAngleRadIC()) < 0.01) ) {
996       fgic->SetClimbRateFpsIC(roc);
997     }
998     needTrim=true;
999 }
1000
1001 void FGJSBsim::set_Gamma_vert_rad( double gamma)
1002 {
1003     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
1004
1005     update_ic();
1006     if( !(fabs(gamma) < 0.01 && fabs(fgic->GetClimbRateFpsIC()) > 1) ) {
1007       fgic->SetFlightPathAngleRadIC(gamma);
1008     }
1009     needTrim=true;
1010 }
1011
1012 void FGJSBsim::init_gear(void )
1013 {
1014     FGGroundReactions* gr=fdmex->GetGroundReactions();
1015     int Ngear=GroundReactions->GetNumGearUnits();
1016     for (int i=0;i<Ngear;i++) {
1017       FGLGear *gear = gr->GetGearUnit(i);
1018       SGPropertyNode * node = fgGetNode("gear/gear", i, true);
1019       node->setDoubleValue("xoffset-in", gear->GetBodyLocation()(1));
1020       node->setDoubleValue("yoffset-in", gear->GetBodyLocation()(2));
1021       node->setDoubleValue("zoffset-in", gear->GetBodyLocation()(3));
1022       node->setBoolValue("wow", gear->GetWOW());
1023       node->setBoolValue("has-brake", gear->GetBrakeGroup() > 0);
1024       node->setDoubleValue("position-norm", FCS->GetGearPos());
1025       node->setDoubleValue("tire-pressure-norm", gear->GetTirePressure());
1026       if ( gear->GetSteerable() )
1027         node->setDoubleValue("steering-norm", gear->GetSteerNorm());
1028     }
1029 }
1030
1031 void FGJSBsim::update_gear(void)
1032 {
1033     FGGroundReactions* gr=fdmex->GetGroundReactions();
1034     int Ngear=GroundReactions->GetNumGearUnits();
1035     for (int i=0;i<Ngear;i++) {
1036       FGLGear *gear = gr->GetGearUnit(i);
1037       SGPropertyNode * node = fgGetNode("gear/gear", i, true);
1038       node->getChild("wow", 0, true)->setBoolValue( gear->GetWOW());
1039       node->getChild("position-norm", 0, true)->setDoubleValue(FCS->GetGearPos());
1040       gear->SetTirePressure(node->getDoubleValue("tire-pressure-norm"));
1041       if ( gear->GetSteerable() )
1042         node->setDoubleValue("steering-norm", gear->GetSteerNorm());
1043     }
1044 }
1045
1046 void FGJSBsim::do_trim(void)
1047 {
1048   FGTrim *fgtrim;
1049
1050   if ( fgGetBool("/sim/presets/onground") )
1051   {
1052     fgic->SetVcalibratedKtsIC(0.0);
1053     fgtrim = new FGTrim(fdmex,tGround);
1054   } else {
1055     fgtrim = new FGTrim(fdmex,tLongitudinal);
1056   }
1057
1058   if ( !fgtrim->DoTrim() ) {
1059     fgtrim->Report();
1060     fgtrim->TrimStats();
1061   } else {
1062     trimmed->setBoolValue(true);
1063   }
1064   if (FGJSBBase::debug_lvl > 0)
1065       State->ReportState();
1066
1067   delete fgtrim;
1068
1069   pitch_trim->setDoubleValue( FCS->GetPitchTrimCmd() );
1070   throttle_trim->setDoubleValue( FCS->GetThrottleCmd(0) );
1071   aileron_trim->setDoubleValue( FCS->GetDaCmd() );
1072   rudder_trim->setDoubleValue( FCS->GetDrCmd() );
1073
1074   globals->get_controls()->set_elevator_trim(FCS->GetPitchTrimCmd());
1075   globals->get_controls()->set_elevator(FCS->GetDeCmd());
1076   globals->get_controls()->set_throttle(FGControls::ALL_ENGINES,
1077   FCS->GetThrottleCmd(0));
1078
1079   globals->get_controls()->set_aileron(FCS->GetDaCmd());
1080   globals->get_controls()->set_rudder( FCS->GetDrCmd());
1081
1082   SG_LOG( SG_FLIGHT, SG_INFO, "  Trim complete" );
1083 }
1084
1085 void FGJSBsim::update_ic(void)
1086 {
1087    if ( !needTrim ) {
1088      fgic->SetLatitudeRadIC(get_Lat_geocentric() );
1089      fgic->SetLongitudeRadIC( get_Longitude() );
1090      fgic->SetAltitudeFtIC( get_Altitude() );
1091      fgic->SetVcalibratedKtsIC( get_V_calibrated_kts() );
1092      fgic->SetPitchAngleRadIC( get_Theta() );
1093      fgic->SetRollAngleRadIC( get_Phi() );
1094      fgic->SetTrueHeadingRadIC( get_Psi() );
1095      fgic->SetClimbRateFpsIC( get_Climb_Rate() );
1096    }
1097 }
1098
1099 void FGJSBsim::crash_handler(void)
1100 {
1101    if (crashed) return;  // we already crashed
1102    crashed = true;
1103    fgSetBool("/sim/crashed", true);
1104    SG_LOG( SG_FLIGHT, SG_WARN, "  Crash: " << crash_message );
1105    if (reset_on_crash) {
1106      SGPropertyNode* node = fgGetNode("/sim/presets", true);
1107      globals->get_commands()->execute("old-reinit-dialog", node);   
1108    } else {
1109      fgSetBool("/sim/freeze/master", true);
1110      fgSetBool("/sim/freeze/clock", true);
1111    }
1112 }