]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/JSBSim.cxx
Make spoilers and speed-brake position accessible
[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 #ifdef SG_MATH_EXCEPTION_CLASH
31 #  include <math.h>
32 #endif
33
34 #include STL_STRING
35
36 #include <simgear/constants.h>
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/math/sg_geodesy.hxx>
39 #include <simgear/misc/sg_path.hxx>
40
41 #include <FDM/flight.hxx>
42
43 #include <Aircraft/aircraft.hxx>
44 #include <Controls/controls.hxx>
45 #include <Main/globals.hxx>
46 #include <Main/fg_props.hxx>
47
48 #include <FDM/JSBSim/FGFDMExec.h>
49 #include <FDM/JSBSim/FGAircraft.h>
50 #include <FDM/JSBSim/FGFCS.h>
51 #include <FDM/JSBSim/FGPosition.h>
52 #include <FDM/JSBSim/FGRotation.h>
53 #include <FDM/JSBSim/FGState.h>
54 #include <FDM/JSBSim/FGTranslation.h>
55 #include <FDM/JSBSim/FGAuxiliary.h>
56 #include <FDM/JSBSim/FGInitialCondition.h>
57 #include <FDM/JSBSim/FGTrim.h>
58 #include <FDM/JSBSim/FGAtmosphere.h>
59 #include <FDM/JSBSim/FGMassBalance.h>
60 #include <FDM/JSBSim/FGAerodynamics.h>
61 #include <FDM/JSBSim/FGLGear.h>
62 #include <FDM/JSBSim/FGPropertyManager.h>
63 #include "JSBSim.hxx"
64
65 static inline double
66 FMAX (double a, double b)
67 {
68   return a > b ? a : b;
69 }
70
71
72 /******************************************************************************/
73
74 FGJSBsim::FGJSBsim( double dt ) 
75   : FGInterface(dt)
76 {
77     bool result;
78    
79     fdmex = new FGFDMExec( (FGPropertyManager*)globals->get_props() );
80     
81     State           = fdmex->GetState();
82     Atmosphere      = fdmex->GetAtmosphere();
83     FCS             = fdmex->GetFCS();
84     MassBalance     = fdmex->GetMassBalance();
85     Propulsion      = fdmex->GetPropulsion();
86     Aircraft        = fdmex->GetAircraft();
87     Translation     = fdmex->GetTranslation();
88     Rotation        = fdmex->GetRotation();
89     Position        = fdmex->GetPosition();
90     Auxiliary       = fdmex->GetAuxiliary();
91     Aerodynamics    = fdmex->GetAerodynamics();
92     GroundReactions = fdmex->GetGroundReactions(); 
93     
94     fgic=fdmex->GetIC();
95     needTrim=true;
96   
97     SGPath aircraft_path( globals->get_fg_root() );
98     aircraft_path.append( "Aircraft" );
99
100     SGPath engine_path( globals->get_fg_root() );
101     engine_path.append( "Engine" );
102     State->Setdt( dt );
103
104     result = fdmex->LoadModel( aircraft_path.str(),
105                                engine_path.str(),
106                                fgGetString("/sim/aero") );
107     
108     if (result) {
109       SG_LOG( SG_FLIGHT, SG_INFO, "  loaded aero.");
110     } else {
111       SG_LOG( SG_FLIGHT, SG_INFO,
112               "  aero does not exist (you may have mis-typed the name).");
113       throw(-1);
114     }
115
116     SG_LOG( SG_FLIGHT, SG_INFO, "" );
117     SG_LOG( SG_FLIGHT, SG_INFO, "" );
118     SG_LOG( SG_FLIGHT, SG_INFO, "After loading aero definition file ..." );
119
120     int Neng = Propulsion->GetNumEngines();
121     SG_LOG( SG_FLIGHT, SG_INFO, "num engines = " << Neng );
122     
123     if ( GroundReactions->GetNumGearUnits() <= 0 ) {
124         SG_LOG( SG_FLIGHT, SG_ALERT, "num gear units = "
125                 << GroundReactions->GetNumGearUnits() );
126         SG_LOG( SG_FLIGHT, SG_ALERT, "This is a very bad thing because with 0 gear units, the ground trimming");
127          SG_LOG( SG_FLIGHT, SG_ALERT, "routine (coming up later in the code) will core dump.");
128          SG_LOG( SG_FLIGHT, SG_ALERT, "Halting the sim now, and hoping a solution will present itself soon!");
129          exit(-1);
130     }
131         
132     
133     init_gear();
134
135                                 // Set initial fuel levels if provided.
136     for (unsigned int i = 0; i < Propulsion->GetNumTanks(); i++) {
137       SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
138       if (node->getChild("level-gal_us", 0, false) != 0)
139         Propulsion->GetTank(i)
140           ->SetContents(node->getDoubleValue("level-gal_us") * 6.6);
141     }
142     
143     fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
144     fgSetDouble("/fdm/trim/throttle",   FCS->GetThrottleCmd(0));
145     fgSetDouble("/fdm/trim/aileron",    FCS->GetDaCmd());
146     fgSetDouble("/fdm/trim/rudder",     FCS->GetDrCmd());
147
148     startup_trim = fgGetNode("/sim/presets/trim", true);
149
150     trimmed = fgGetNode("/fdm/trim/trimmed", true);
151     trimmed->setBoolValue(false);
152
153     pitch_trim = fgGetNode("/fdm/trim/pitch-trim", true );
154     throttle_trim = fgGetNode("/fdm/trim/throttle", true );
155     aileron_trim = fgGetNode("/fdm/trim/aileron", true );
156     rudder_trim = fgGetNode("/fdm/trim/rudder", true );
157     
158     stall_warning = fgGetNode("/sim/alarms/stall-warning",true);
159     stall_warning->setDoubleValue(0);
160     
161
162     flap_pos_pct=fgGetNode("/surface-positions/flap-pos-norm",true);
163     elevator_pos_pct=fgGetNode("/surface-positions/elevator-pos-norm",true);
164     left_aileron_pos_pct
165         =fgGetNode("/surface-positions/left-aileron-pos-norm",true);
166     right_aileron_pos_pct
167         =fgGetNode("/surface-positions/right-aileron-pos-norm",true);
168     rudder_pos_pct=fgGetNode("/surface-positions/rudder-pos-norm",true);
169     speed_brake_pos_pct
170         =fgGetNode("/surface-positions/speed-brake-pos-norm",true);
171     spoiler_pos_pct=fgGetNode("/surface-positions/spoiler-pos-norm",true);
172     
173
174     elevator_pos_pct->setDoubleValue(0);
175     left_aileron_pos_pct->setDoubleValue(0);
176     right_aileron_pos_pct->setDoubleValue(0);
177     rudder_pos_pct->setDoubleValue(0);
178     flap_pos_pct->setDoubleValue(0);
179     speed_brake_pos_pct->setDoubleValue(0);
180     spoiler_pos_pct->setDoubleValue(0);
181
182     temperature = fgGetNode("/environment/temperature-degc",true);
183     pressure = fgGetNode("/environment/pressure-inhg",true);
184     density = fgGetNode("/environment/density-slugft3",true);
185     turbulence = fgGetNode("environment/turbulence-norm",true);
186     
187     wind_from_north= fgGetNode("/environment/wind-from-north-fps",true);
188     wind_from_east = fgGetNode("/environment/wind-from-east-fps" ,true);
189     wind_from_down = fgGetNode("/environment/wind-from-down-fps" ,true);
190      
191
192 }
193 /******************************************************************************/
194 FGJSBsim::~FGJSBsim(void) {
195         delete fdmex;
196 }
197
198 /******************************************************************************/
199
200 // Initialize the JSBsim flight model, dt is the time increment for
201 // each subsequent iteration through the EOM
202
203 void FGJSBsim::init() {
204     
205     SG_LOG( SG_FLIGHT, SG_INFO, "Starting and initializing JSBsim" );
206
207     // Explicitly call the superclass's
208     // init method first.
209
210 #ifdef FG_WEATHERCM
211     Atmosphere->UseInternal();
212 #else
213     if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
214       Atmosphere->UseExternal();
215       Atmosphere->SetExTemperature(
216                   9.0/5.0*(temperature->getDoubleValue()+273.15) );
217       Atmosphere->SetExPressure(pressure->getDoubleValue()*70.726566);
218       Atmosphere->SetExDensity(density->getDoubleValue());
219       Atmosphere->SetTurbGain(turbulence->getDoubleValue() *
220                               turbulence->getDoubleValue() *
221                               100.0);
222     } else {
223       Atmosphere->UseInternal();
224     }
225 #endif
226     
227     fgic->SetVnorthFpsIC( wind_from_north->getDoubleValue() );
228     fgic->SetVeastFpsIC( wind_from_east->getDoubleValue() );
229     fgic->SetVdownFpsIC( wind_from_down->getDoubleValue() );
230
231     //Atmosphere->SetExTemperature(get_Static_temperature());
232     //Atmosphere->SetExPressure(get_Static_pressure());
233     //Atmosphere->SetExDensity(get_Density());
234     SG_LOG(SG_FLIGHT,SG_INFO,"T,p,rho: " << fdmex->GetAtmosphere()->GetTemperature()
235      << ", " << fdmex->GetAtmosphere()->GetPressure() 
236      << ", " << fdmex->GetAtmosphere()->GetDensity() );
237
238     common_init();
239     copy_to_JSBsim();
240     
241
242     fdmex->RunIC(); //loop JSBSim once w/o integrating
243     copy_from_JSBsim(); //update the bus
244
245     SG_LOG( SG_FLIGHT, SG_INFO, "  Initialized JSBSim with:" );
246
247     switch(fgic->GetSpeedSet()) {
248     case setned:
249         SG_LOG(SG_FLIGHT,SG_INFO, "  Vn,Ve,Vd= "
250                << Position->GetVn() << ", "
251                << Position->GetVe() << ", "
252                << Position->GetVd() << " ft/s");
253     break;
254     case setuvw:
255         SG_LOG(SG_FLIGHT,SG_INFO, "  U,V,W= "
256                << Translation->GetUVW(1) << ", "
257                << Translation->GetUVW(2) << ", "
258                << Translation->GetUVW(3) << " ft/s");
259     break;
260     case setmach:
261         SG_LOG(SG_FLIGHT,SG_INFO, "  Mach: "
262                << Translation->GetMach() );
263     break;
264     case setvc:
265     default:
266         SG_LOG(SG_FLIGHT,SG_INFO, "  Indicated Airspeed: "
267                << Auxiliary->GetVcalibratedKTS() << " knots" );
268     break;
269     }
270     
271     stall_warning->setDoubleValue(0);
272     
273     SG_LOG( SG_FLIGHT, SG_INFO, "  Bank Angle: "
274             <<  Rotation->Getphi()*RADTODEG << " deg" );
275     SG_LOG( SG_FLIGHT, SG_INFO, "  Pitch Angle: "
276             << Rotation->Gettht()*RADTODEG << " deg" );
277     SG_LOG( SG_FLIGHT, SG_INFO, "  True Heading: "
278             << Rotation->Getpsi()*RADTODEG << " deg" );
279     SG_LOG( SG_FLIGHT, SG_INFO, "  Latitude: "
280             << Position->GetLatitude() << " deg" );
281     SG_LOG( SG_FLIGHT, SG_INFO, "  Longitude: "
282             << Position->GetLongitude() << " deg" );
283     SG_LOG( SG_FLIGHT, SG_INFO, "  Altitude: "
284         << Position->Geth() << " feet" );
285     SG_LOG( SG_FLIGHT, SG_INFO, "  loaded initial conditions" );
286
287     SG_LOG( SG_FLIGHT, SG_INFO, "  set dt" );
288
289     SG_LOG( SG_FLIGHT, SG_INFO, "Finished initializing JSBSim" );
290     
291     SG_LOG( SG_FLIGHT, SG_INFO, "FGControls::get_gear_down()= " << 
292                                   globals->get_controls()->get_gear_down() );
293     
294
295    
296 }
297
298 /******************************************************************************/
299
300 // Run an iteration of the EOM (equations of motion)
301
302 void
303 FGJSBsim::update( double dt ) {
304
305     if (is_suspended())
306       return;
307
308     int multiloop = _calc_multiloop(dt);
309
310     int i;
311
312     // double save_alt = 0.0;
313
314     copy_to_JSBsim();
315
316     trimmed->setBoolValue(false);
317     
318     if ( needTrim ) {
319       if ( startup_trim->getBoolValue() ) {
320         SG_LOG(SG_FLIGHT, SG_INFO,
321           "Ready to trim, terrain altitude is: " 
322             << cur_fdm_state->get_Runway_altitude() * SG_METER_TO_FEET );
323         fgic->SetTerrainAltitudeFtIC( cur_fdm_state->get_ground_elev_ft() );
324         do_trim();
325       } else {
326         fdmex->RunIC();  //apply any changes made through the set_ functions
327       }
328       needTrim = false;  
329     }    
330     
331     for ( i=0; i < multiloop; i++ ) {
332         fdmex->Run();
333     }
334
335     FGJSBBase::Message* msg;
336     while (fdmex->ReadMessage()) {
337       msg = fdmex->ProcessMessage();
338       switch (msg->type) {
339       case FGJSBBase::Message::eText:
340         SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text );
341         break;
342       case FGJSBBase::Message::eBool:
343         SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->bVal );
344         break;
345       case FGJSBBase::Message::eInteger:
346         SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->iVal );
347         break;
348       case FGJSBBase::Message::eDouble:
349         SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->dVal );
350         break;
351       default:
352         SG_LOG( SG_FLIGHT, SG_INFO, "Unrecognized message type." );
353         break;
354       }
355     }
356
357     // translate JSBsim back to FG structure so that the
358     // autopilot (and the rest of the sim can use the updated values
359     copy_from_JSBsim();
360 }
361
362 /******************************************************************************/
363
364 // Convert from the FGInterface struct to the JSBsim generic_ struct
365
366 bool FGJSBsim::copy_to_JSBsim() {
367     unsigned int i;
368
369     // copy control positions into the JSBsim structure
370
371     FCS->SetDaCmd( globals->get_controls()->get_aileron());
372     FCS->SetRollTrimCmd( globals->get_controls()->get_aileron_trim() );
373     FCS->SetDeCmd( globals->get_controls()->get_elevator());
374     FCS->SetPitchTrimCmd( globals->get_controls()->get_elevator_trim() );
375     FCS->SetDrCmd( -globals->get_controls()->get_rudder() );
376     FCS->SetYawTrimCmd( -globals->get_controls()->get_rudder_trim() );
377     FCS->SetDfCmd(  globals->get_controls()->get_flaps() );
378     FCS->SetDsbCmd( globals->get_controls()->get_speed_brake() );
379     FCS->SetDspCmd( globals->get_controls()->get_spoilers() );
380
381                                 // Parking brake sets minimum braking
382                                 // level for mains.
383     double parking_brake = globals->get_controls()->get_parking_brake();
384     FCS->SetLBrake(FMAX(globals->get_controls()->get_brake(0), parking_brake));
385     FCS->SetRBrake(FMAX(globals->get_controls()->get_brake(1), parking_brake));
386     FCS->SetCBrake( globals->get_controls()->get_brake( 2 ) );
387
388     FCS->SetGearCmd( globals->get_controls()->get_gear_down());
389     for (i = 0; i < Propulsion->GetNumEngines(); i++) {
390       FGEngine * eng = Propulsion->GetEngine(i);
391       SGPropertyNode * node = fgGetNode("engines/engine", i, true);
392       FCS->SetThrottleCmd(i, globals->get_controls()->get_throttle(i));
393       FCS->SetMixtureCmd(i, globals->get_controls()->get_mixture(i));
394       FCS->SetPropAdvanceCmd(i, globals->get_controls()->get_prop_advance(i));
395       Propulsion->GetThruster(i)->SetRPM(node->getDoubleValue("rpm"));
396       eng->SetMagnetos( globals->get_controls()->get_magnetos(i) );
397       eng->SetStarter( globals->get_controls()->get_starter(i) );
398     }
399
400     _set_Runway_altitude( cur_fdm_state->get_Runway_altitude() );
401     Position->SetSeaLevelRadius( get_Sea_level_radius() );
402     Position->SetRunwayRadius( get_Runway_altitude() 
403                                + get_Sea_level_radius() );
404
405     Atmosphere->SetExTemperature(
406                   9.0/5.0*(temperature->getDoubleValue()+273.15) );
407     Atmosphere->SetExPressure(pressure->getDoubleValue()*70.726566);
408     Atmosphere->SetExDensity(density->getDoubleValue());
409     Atmosphere->SetTurbGain(turbulence->getDoubleValue() *
410                             turbulence->getDoubleValue() *
411                             100.0);
412
413     Atmosphere->SetWindNED( wind_from_north->getDoubleValue(),
414                             wind_from_east->getDoubleValue(),
415                             wind_from_down->getDoubleValue() );
416 //    SG_LOG(SG_FLIGHT,SG_INFO, "Wind NED: "
417 //                  << get_V_north_airmass() << ", "
418 //                  << get_V_east_airmass()  << ", "
419 //                  << get_V_down_airmass() );
420
421     for (i = 0; i < Propulsion->GetNumTanks(); i++) {
422       SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
423       FGTank * tank = Propulsion->GetTank(i);
424       tank->SetContents(node->getDoubleValue("level-gal_us") * 6.6);
425 //       tank->SetContents(node->getDoubleValue("level-lb"));
426     }
427
428     return true;
429 }
430
431 /******************************************************************************/
432
433 // Convert from the JSBsim generic_ struct to the FGInterface struct
434
435 bool FGJSBsim::copy_from_JSBsim() {
436     unsigned int i, j;
437
438     _set_Inertias( MassBalance->GetMass(),
439                    MassBalance->GetIxx(),
440                    MassBalance->GetIyy(),
441                    MassBalance->GetIzz(),
442                    MassBalance->GetIxz() );
443
444     _set_CG_Position( MassBalance->GetXYZcg(1),
445                       MassBalance->GetXYZcg(2),
446                       MassBalance->GetXYZcg(3) );
447
448     _set_Accels_Body( Aircraft->GetBodyAccel()(1),
449                       Aircraft->GetBodyAccel()(2),
450                       Aircraft->GetBodyAccel()(3) );
451
452     //_set_Accels_CG_Body( Aircraft->GetBodyAccel()(1),
453     //                     Aircraft->GetBodyAccel()(2),
454     //                     Aircraft->GetBodyAccel()(3) );
455     //
456     _set_Accels_CG_Body_N ( Aircraft->GetNcg()(1),
457                             Aircraft->GetNcg()(2),
458                             Aircraft->GetNcg()(3) );
459     
460     _set_Accels_Pilot_Body( Auxiliary->GetPilotAccel()(1),
461                             Auxiliary->GetPilotAccel()(2),
462                             Auxiliary->GetPilotAccel()(3) );
463
464    // _set_Accels_Pilot_Body_N( Auxiliary->GetPilotAccel()(1)/32.1739,
465    //                           Auxiliary->GetNpilot(2)/32.1739,
466    //                           Auxiliary->GetNpilot(3)/32.1739 );
467
468     _set_Nlf( Aircraft->GetNlf() );
469
470     // Velocities
471
472     _set_Velocities_Local( Position->GetVn(),
473                            Position->GetVe(),
474                            Position->GetVd() );
475
476     _set_Velocities_Wind_Body( Translation->GetUVW(1),
477                                Translation->GetUVW(2),
478                                Translation->GetUVW(3) );
479
480     _set_V_rel_wind( Translation->GetVt() );
481
482     _set_V_equiv_kts( Auxiliary->GetVequivalentKTS() );
483
484     // _set_V_calibrated( Auxiliary->GetVcalibratedFPS() );
485
486     _set_V_calibrated_kts( Auxiliary->GetVcalibratedKTS() );
487
488     _set_V_ground_speed( Position->GetVground() );
489
490     _set_Omega_Body( Rotation->GetPQR(1),
491                      Rotation->GetPQR(2),
492                      Rotation->GetPQR(3) );
493
494     _set_Euler_Rates( Rotation->GetEulerRates(1),
495                       Rotation->GetEulerRates(2),
496                       Rotation->GetEulerRates(3) );
497
498     _set_Geocentric_Rates(Position->GetLatitudeDot(),
499                           Position->GetLongitudeDot(),
500                           Position->Gethdot() );
501
502     _set_Mach_number( Translation->GetMach() );
503
504     // Positions
505     _updateGeocentricPosition( Position->GetLatitude(),
506                                Position->GetLongitude(),
507                                Position->Geth() );
508
509     _set_Altitude_AGL( Position->GetDistanceAGL() );
510
511     _set_Euler_Angles( Rotation->Getphi(),
512                        Rotation->Gettht(),
513                        Rotation->Getpsi() );
514
515     _set_Alpha( Translation->Getalpha() );
516     _set_Beta( Translation->Getbeta() );
517
518
519     _set_Gamma_vert_rad( Position->GetGamma() );
520     // set_Gamma_horiz_rad( Gamma_horiz_rad );
521
522     _set_Earth_position_angle( Auxiliary->GetEarthPositionAngle() );
523
524     _set_Climb_Rate( Position->Gethdot() );
525
526
527     for ( i = 1; i <= 3; i++ ) {
528         for ( j = 1; j <= 3; j++ ) {
529             _set_T_Local_to_Body( i, j, State->GetTl2b(i,j) );
530         }
531     }
532
533                                 // Copy the engine values from JSBSim.
534     for( i=0; i < Propulsion->GetNumEngines(); i++ ) {
535       SGPropertyNode * node = fgGetNode("engines/engine", i, true);
536       FGEngine * eng = Propulsion->GetEngine(i);
537       FGThruster * thrust = Propulsion->GetThruster(i);
538
539       node->setDoubleValue("mp-osi", eng->getManifoldPressure_inHg());
540       node->setDoubleValue("rpm", thrust->GetRPM());
541       node->setDoubleValue("egt-degf", eng->getExhaustGasTemp_degF());
542       node->setDoubleValue("fuel-flow-gph", eng->getFuelFlow_gph());
543       node->setDoubleValue("cht-degf", eng->getCylinderHeadTemp_degF());
544       node->setDoubleValue("oil-temperature-degf", eng->getOilTemp_degF());
545       node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi());
546       node->setBoolValue("running", eng->GetRunning());
547       node->setBoolValue("cranking", eng->GetCranking());
548     }
549
550     static const SGPropertyNode *fuel_freeze
551         = fgGetNode("/sim/freeze/fuel");
552
553                                 // Copy the fuel levels from JSBSim if fuel
554                                 // freeze not enabled.
555     if ( ! fuel_freeze->getBoolValue() ) {
556         for (i = 0; i < Propulsion->GetNumTanks(); i++) {
557             SGPropertyNode * node
558                 = fgGetNode("/consumables/fuel/tank", i, true);
559             double contents = Propulsion->GetTank(i)->GetContents();
560             node->setDoubleValue("level-gal_us", contents/6.6);
561             // node->setDoubleValue("level-lb", contents);
562         }
563     }
564
565     update_gear();
566     
567     stall_warning->setDoubleValue( Aerodynamics->GetStallWarn() );
568     
569     /* elevator_pos_deg->setDoubleValue( FCS->GetDePos()*SG_RADIANS_TO_DEGREES );
570     left_aileron_pos_deg->setDoubleValue( FCS->GetDaLPos()*SG_RADIANS_TO_DEGREES );
571     right_aileron_pos_deg->setDoubleValue( FCS->GetDaRPos()*SG_RADIANS_TO_DEGREES );
572     rudder_pos_deg->setDoubleValue( -1*FCS->GetDrPos()*SG_RADIANS_TO_DEGREES );
573     flap_pos_deg->setDoubleValue( FCS->GetDfPos() ); */
574
575     
576     elevator_pos_pct->setDoubleValue( FCS->GetDePos(ofNorm) );
577     left_aileron_pos_pct->setDoubleValue( FCS->GetDaLPos(ofNorm) );
578     right_aileron_pos_pct->setDoubleValue( -1*FCS->GetDaLPos(ofNorm) );
579     rudder_pos_pct->setDoubleValue( -1*FCS->GetDrPos(ofNorm) );
580     flap_pos_pct->setDoubleValue( FCS->GetDfPos(ofNorm) );
581     speed_brake_pos_pct->setDoubleValue( FCS->GetDsbPos(ofNorm) );
582     spoiler_pos_pct->setDoubleValue( FCS->GetDspPos(ofNorm) );
583
584     
585     return true;
586 }
587
588 bool FGJSBsim::ToggleDataLogging(void) {
589     return fdmex->GetOutput()->Toggle();
590 }
591
592
593 bool FGJSBsim::ToggleDataLogging(bool state) {
594     if (state) {
595       fdmex->GetOutput()->Enable();
596       return true;
597     } else {
598       fdmex->GetOutput()->Disable();
599       return false;
600     }
601 }
602
603
604 //Positions
605 void FGJSBsim::set_Latitude(double lat) {
606     static const SGPropertyNode *altitude = fgGetNode("/position/altitude-ft");
607     double alt;
608     double sea_level_radius_meters, lat_geoc;
609     
610     if ( altitude->getDoubleValue() > -9990 ) {
611       alt = altitude->getDoubleValue();
612     } else {
613       alt = 0.0;
614     }
615    
616     update_ic();
617     SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat );
618     SG_LOG(SG_FLIGHT,SG_INFO," cur alt (ft) =  " << alt );
619
620     sgGeodToGeoc( lat, alt * SG_FEET_TO_METER, 
621                       &sea_level_radius_meters, &lat_geoc );
622     _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET  );
623     fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET  );    
624     _set_Runway_altitude( cur_fdm_state->get_Runway_altitude() );
625     fgic->SetTerrainAltitudeFtIC( cur_fdm_state->get_ground_elev_ft() );
626     fgic->SetLatitudeRadIC( lat_geoc );
627     needTrim=true;
628 }
629
630 void FGJSBsim::set_Longitude(double lon) {
631
632     SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon );
633     update_ic();
634     fgic->SetLongitudeRadIC( lon );
635     _set_Runway_altitude( cur_fdm_state->get_Runway_altitude() );
636     fgic->SetTerrainAltitudeFtIC( cur_fdm_state->get_ground_elev_ft() );
637     needTrim=true;
638 }
639
640 void FGJSBsim::set_Altitude(double alt) {
641     static const SGPropertyNode *latitude = fgGetNode("/position/latitude-deg");
642
643     double sea_level_radius_meters,lat_geoc;
644
645     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt );
646     SG_LOG(SG_FLIGHT,SG_INFO, "  lat (deg) = " << latitude->getDoubleValue() );
647     
648     update_ic();
649     sgGeodToGeoc( latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS, alt,
650                   &sea_level_radius_meters, &lat_geoc);
651     _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET  );
652     fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
653     _set_Runway_altitude( cur_fdm_state->get_Runway_altitude() );
654     fgic->SetTerrainAltitudeFtIC( cur_fdm_state->get_ground_elev_ft() );
655     SG_LOG(SG_FLIGHT, SG_INFO,
656           "Terrain altitude: " << cur_fdm_state->get_Runway_altitude() * SG_METER_TO_FEET );
657     fgic->SetLatitudeRadIC( lat_geoc );
658     fgic->SetAltitudeFtIC(alt);
659     needTrim=true;
660 }
661
662 void FGJSBsim::set_V_calibrated_kts(double vc) {
663     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " <<  vc );
664     
665     update_ic();
666     fgic->SetVcalibratedKtsIC(vc);
667     needTrim=true;
668 }
669
670 void FGJSBsim::set_Mach_number(double mach) {
671     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " <<  mach );
672     
673     update_ic();
674     fgic->SetMachIC(mach);
675     needTrim=true;
676 }
677
678 void FGJSBsim::set_Velocities_Local( double north, double east, double down ){
679     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: "
680        << north << ", " <<  east << ", " << down );
681     
682     update_ic();
683     fgic->SetVnorthFpsIC(north);
684     fgic->SetVeastFpsIC(east);
685     fgic->SetVdownFpsIC(down);
686     needTrim=true;
687 }
688
689 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w){
690     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
691        << u << ", " <<  v << ", " <<  w );
692     
693     update_ic();
694     fgic->SetUBodyFpsIC(u);
695     fgic->SetVBodyFpsIC(v);
696     fgic->SetWBodyFpsIC(w);
697     needTrim=true;
698 }
699
700 //Euler angles
701 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi ) {
702     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: "
703        << phi << ", " << theta << ", " << psi );
704     
705     update_ic();
706     fgic->SetPitchAngleRadIC(theta);
707     fgic->SetRollAngleRadIC(phi);
708     fgic->SetTrueHeadingRadIC(psi);
709     needTrim=true;
710 }
711
712 //Flight Path
713 void FGJSBsim::set_Climb_Rate( double roc) {
714     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
715     
716     update_ic();
717     //since both climb rate and flight path angle are set in the FG
718     //startup sequence, something is needed to keep one from cancelling
719     //out the other.
720     if( !(fabs(roc) > 1 && fabs(fgic->GetFlightPathAngleRadIC()) < 0.01) ) {
721       fgic->SetClimbRateFpsIC(roc);
722     }  
723     needTrim=true;
724 }
725
726 void FGJSBsim::set_Gamma_vert_rad( double gamma) {
727     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
728     
729     update_ic();
730     if( !(fabs(gamma) < 0.01 && fabs(fgic->GetClimbRateFpsIC()) > 1) ) {
731       fgic->SetFlightPathAngleRadIC(gamma);
732     }  
733     needTrim=true;
734 }
735
736 void FGJSBsim::init_gear(void ) {
737     
738     FGGroundReactions* gr=fdmex->GetGroundReactions();
739     int Ngear=GroundReactions->GetNumGearUnits();
740     for (int i=0;i<Ngear;i++) {
741       SGPropertyNode * node = fgGetNode("gear/gear", i, true);
742       node->setDoubleValue("xoffset-in",
743                            gr->GetGearUnit(i)->GetBodyLocation()(1));
744       node->setDoubleValue("yoffset-in",
745                            gr->GetGearUnit(i)->GetBodyLocation()(2));
746       node->setDoubleValue("zoffset-in",
747                            gr->GetGearUnit(i)->GetBodyLocation()(3));
748       node->setBoolValue("wow", gr->GetGearUnit(i)->GetWOW());
749       node->setBoolValue("has-brake", gr->GetGearUnit(i)->GetBrakeGroup() > 0);
750       node->setDoubleValue("position-norm", FCS->GetGearPos());
751     }  
752 }
753
754 void FGJSBsim::update_gear(void) {
755     
756     FGGroundReactions* gr=fdmex->GetGroundReactions();
757     int Ngear=GroundReactions->GetNumGearUnits();
758     for (int i=0;i<Ngear;i++) {
759       SGPropertyNode * node = fgGetNode("gear/gear", i, true);
760       node->getChild("wow", 0, true)
761         ->setBoolValue(gr->GetGearUnit(i)->GetWOW());
762       node->getChild("position-norm", 0, true)
763         ->setDoubleValue(FCS->GetGearPos());
764     }  
765 }
766
767 void FGJSBsim::do_trim(void) {
768
769         FGTrim *fgtrim;
770         if( fgGetBool("/sim/presets/onground") ) {
771             fgic->SetVcalibratedKtsIC(0.0);
772             fgtrim=new FGTrim(fdmex,tGround);
773         } else {
774             fgtrim=new FGTrim(fdmex,tLongitudinal);
775         }
776         if( !fgtrim->DoTrim() ) {
777             fgtrim->Report();
778             fgtrim->TrimStats();
779         } else {
780             trimmed->setBoolValue(true);
781         }
782         State->ReportState();
783         delete fgtrim;
784         pitch_trim->setDoubleValue( FCS->GetPitchTrimCmd() );
785         throttle_trim->setDoubleValue( FCS->GetThrottleCmd(0) );
786         aileron_trim->setDoubleValue( FCS->GetDaCmd() );
787         rudder_trim->setDoubleValue( FCS->GetDrCmd() );
788
789         globals->get_controls()->set_elevator_trim(FCS->GetPitchTrimCmd());
790         globals->get_controls()->set_elevator(FCS->GetDeCmd());
791         globals->get_controls()->set_throttle(FGControls::ALL_ENGINES,
792                                               FCS->GetThrottleCmd(0));
793
794         globals->get_controls()->set_aileron(FCS->GetDaCmd());
795         globals->get_controls()->set_rudder( FCS->GetDrCmd());
796     
797         SG_LOG( SG_FLIGHT, SG_INFO, "  Trim complete" );
798 }          
799
800 void FGJSBsim::update_ic(void) {       
801    if( !needTrim ) {
802      fgic->SetLatitudeRadIC(get_Lat_geocentric() );       
803      fgic->SetLongitudeRadIC( get_Longitude() );       
804      fgic->SetAltitudeFtIC( get_Altitude() );       
805      fgic->SetVcalibratedKtsIC( get_V_calibrated_kts() );       
806      fgic->SetPitchAngleRadIC( get_Theta() );       
807      fgic->SetRollAngleRadIC( get_Phi() );       
808      fgic->SetTrueHeadingRadIC( get_Psi() );       
809      fgic->SetClimbRateFpsIC( get_Climb_Rate() );
810    }  
811 }
812