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