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