1 // JSBsim.cxx -- interface to the JSBsim flight model
3 // Written by Curtis Olson, started February 1999.
5 // Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
28 #include <simgear/compiler.h>
30 #include <stdio.h> // size_t
31 #ifdef SG_MATH_EXCEPTION_CLASH
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>
43 #include <FDM/flight.hxx>
45 #include <Aircraft/aircraft.hxx>
46 #include <Aircraft/controls.hxx>
47 #include <Main/globals.hxx>
48 #include <Main/fg_props.hxx>
50 #include <FDM/JSBSim/FGFDMExec.h>
51 #include <FDM/JSBSim/FGAircraft.h>
52 #include <FDM/JSBSim/FGFCS.h>
53 #include <FDM/JSBSim/FGPropagate.h>
54 #include <FDM/JSBSim/FGState.h>
55 #include <FDM/JSBSim/FGAuxiliary.h>
56 #include <FDM/JSBSim/FGInitialCondition.h>
57 #include <FDM/JSBSim/FGTrim.h>
58 #include <FDM/JSBSim/FGAtmosphere.h>
59 #include <FDM/JSBSim/FGMassBalance.h>
60 #include <FDM/JSBSim/FGAerodynamics.h>
61 #include <FDM/JSBSim/FGLGear.h>
62 #include <FDM/JSBSim/FGPropertyManager.h>
63 #include <FDM/JSBSim/FGEngine.h>
64 #include <FDM/JSBSim/FGPiston.h>
65 #include <FDM/JSBSim/FGGroundCallback.h>
66 #include <FDM/JSBSim/FGTurbine.h>
67 #include <FDM/JSBSim/FGRocket.h>
68 #include <FDM/JSBSim/FGElectric.h>
69 #include <FDM/JSBSim/FGNozzle.h>
70 #include <FDM/JSBSim/FGPropeller.h>
71 #include <FDM/JSBSim/FGRotor.h>
72 #include <FDM/JSBSim/FGTank.h>
76 FMAX (double a, double b)
81 class FGFSGroundCallback : public FGGroundCallback {
83 FGFSGroundCallback(FGInterface* ifc) : mInterface(ifc) {}
84 virtual ~FGFSGroundCallback() {}
86 /** Get the altitude above sea level depenent on the location. */
87 virtual double GetAltitude(const FGLocation& l) const {
88 double pt[3] = { SG_FEET_TO_METER*l(eX),
89 SG_FEET_TO_METER*l(eY),
90 SG_FEET_TO_METER*l(eZ) };
92 sgCartToGeod( pt, &lat, &lon, &alt);
93 return alt * SG_METER_TO_FEET;
96 /** Compute the altitude above ground. */
97 virtual double GetAGLevel(double t, const FGLocation& l,
99 FGColumnVector3& n, FGColumnVector3& v) const {
100 double loc_cart[3] = { l(eX), l(eY), l(eZ) };
101 double contact[3], normal[3], vel[3], lc, ff, agl;
103 mInterface->get_agl_ft(t, loc_cart, contact, normal, vel,
104 &groundtype, &lc, &ff, &agl);
105 n = l.GetTec2l()*FGColumnVector3( normal[0], normal[1], normal[2] );
106 v = l.GetTec2l()*FGColumnVector3( vel[0], vel[1], vel[2] );
107 cont = FGColumnVector3( contact[0], contact[1], contact[2] );
111 FGInterface* mInterface;
114 /******************************************************************************/
116 FGJSBsim::FGJSBsim( double dt )
120 // Set up the debugging level
121 // FIXME: this will not respond to
124 // if flight is excluded, don't bother
125 if ((logbuf::get_log_classes() & SG_FLIGHT) != 0) {
127 // do a rough-and-ready mapping to
128 // the levels documented in FGFDMExec.h
129 switch (logbuf::get_log_priority()) {
131 FGJSBBase::debug_lvl = 0x1f;
134 FGJSBBase::debug_lvl = 0x0f;
136 FGJSBBase::debug_lvl = 0x01;
140 FGJSBBase::debug_lvl = 0x00;
145 reset_on_crash = fgGetBool("/sim/reset-on-crash", false);
147 fgSetBool("/sim/crashed", false);
149 fdmex = new FGFDMExec( (FGPropertyManager*)globals->get_props() );
151 // Register ground callback.
152 fdmex->SetGroundCallback( new FGFSGroundCallback(this) );
154 State = fdmex->GetState();
155 Atmosphere = fdmex->GetAtmosphere();
156 FCS = fdmex->GetFCS();
157 MassBalance = fdmex->GetMassBalance();
158 Propulsion = fdmex->GetPropulsion();
159 Aircraft = fdmex->GetAircraft();
160 Propagate = fdmex->GetPropagate();
161 Auxiliary = fdmex->GetAuxiliary();
162 Aerodynamics = fdmex->GetAerodynamics();
163 GroundReactions = fdmex->GetGroundReactions();
168 SGPath aircraft_path( fgGetString("/sim/aircraft-dir") );
170 SGPath engine_path( fgGetString("/sim/aircraft-dir") );
171 engine_path.append( "Engine" );
174 result = fdmex->LoadModel( aircraft_path.str(),
176 fgGetString("/sim/aero"), false );
179 SG_LOG( SG_FLIGHT, SG_INFO, " loaded aero.");
181 SG_LOG( SG_FLIGHT, SG_INFO,
182 " aero does not exist (you may have mis-typed the name).");
186 SG_LOG( SG_FLIGHT, SG_INFO, "" );
187 SG_LOG( SG_FLIGHT, SG_INFO, "" );
188 SG_LOG( SG_FLIGHT, SG_INFO, "After loading aero definition file ..." );
190 int Neng = Propulsion->GetNumEngines();
191 SG_LOG( SG_FLIGHT, SG_INFO, "num engines = " << Neng );
193 if ( GroundReactions->GetNumGearUnits() <= 0 ) {
194 SG_LOG( SG_FLIGHT, SG_ALERT, "num gear units = "
195 << GroundReactions->GetNumGearUnits() );
196 SG_LOG( SG_FLIGHT, SG_ALERT, "This is a very bad thing because with 0 gear units, the ground trimming");
197 SG_LOG( SG_FLIGHT, SG_ALERT, "routine (coming up later in the code) will core dump.");
198 SG_LOG( SG_FLIGHT, SG_ALERT, "Halting the sim now, and hoping a solution will present itself soon!");
204 // Set initial fuel levels if provided.
205 for (unsigned int i = 0; i < Propulsion->GetNumTanks(); i++) {
206 SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
207 if (node->getChild("level-gal_us", 0, false) != 0) {
208 Propulsion->GetTank(i)->SetContents(node->getDoubleValue("level-gal_us") * 6.6);
210 node->setDoubleValue("level-lb", Propulsion->GetTank(i)->GetContents());
211 node->setDoubleValue("level-gal_us", Propulsion->GetTank(i)->GetContents() / 6.6);
214 Propulsion->SetFuelFreeze((fgGetNode("/sim/freeze/fuel",true))->getBoolValue());
216 fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
217 fgSetDouble("/fdm/trim/throttle", FCS->GetThrottleCmd(0));
218 fgSetDouble("/fdm/trim/aileron", FCS->GetDaCmd());
219 fgSetDouble("/fdm/trim/rudder", FCS->GetDrCmd());
221 startup_trim = fgGetNode("/sim/presets/trim", true);
223 trimmed = fgGetNode("/fdm/trim/trimmed", true);
224 trimmed->setBoolValue(false);
226 pitch_trim = fgGetNode("/fdm/trim/pitch-trim", true );
227 throttle_trim = fgGetNode("/fdm/trim/throttle", true );
228 aileron_trim = fgGetNode("/fdm/trim/aileron", true );
229 rudder_trim = fgGetNode("/fdm/trim/rudder", true );
231 stall_warning = fgGetNode("/sim/alarms/stall-warning",true);
232 stall_warning->setDoubleValue(0);
235 flap_pos_pct=fgGetNode("/surface-positions/flap-pos-norm",true);
236 elevator_pos_pct=fgGetNode("/surface-positions/elevator-pos-norm",true);
238 =fgGetNode("/surface-positions/left-aileron-pos-norm",true);
239 right_aileron_pos_pct
240 =fgGetNode("/surface-positions/right-aileron-pos-norm",true);
241 rudder_pos_pct=fgGetNode("/surface-positions/rudder-pos-norm",true);
243 =fgGetNode("/surface-positions/speedbrake-pos-norm",true);
244 spoilers_pos_pct=fgGetNode("/surface-positions/spoilers-pos-norm",true);
246 elevator_pos_pct->setDoubleValue(0);
247 left_aileron_pos_pct->setDoubleValue(0);
248 right_aileron_pos_pct->setDoubleValue(0);
249 rudder_pos_pct->setDoubleValue(0);
250 flap_pos_pct->setDoubleValue(0);
251 speedbrake_pos_pct->setDoubleValue(0);
252 spoilers_pos_pct->setDoubleValue(0);
254 temperature = fgGetNode("/environment/temperature-degc",true);
255 pressure = fgGetNode("/environment/pressure-inhg",true);
256 density = fgGetNode("/environment/density-slugft3",true);
257 turbulence_gain = fgGetNode("/environment/turbulence/magnitude-norm",true);
258 turbulence_rate = fgGetNode("/environment/turbulence/rate-hz",true);
260 wind_from_north= fgGetNode("/environment/wind-from-north-fps",true);
261 wind_from_east = fgGetNode("/environment/wind-from-east-fps" ,true);
262 wind_from_down = fgGetNode("/environment/wind-from-down-fps" ,true);
264 for (unsigned int i = 0; i < Propulsion->GetNumEngines(); i++) {
265 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
266 Propulsion->GetEngine(i)->GetThruster()->SetRPM(node->getDoubleValue("rpm") /
267 Propulsion->GetEngine(i)->GetThruster()->GetGearRatio());
271 /******************************************************************************/
272 FGJSBsim::~FGJSBsim(void)
277 /******************************************************************************/
279 // Initialize the JSBsim flight model, dt is the time increment for
280 // each subsequent iteration through the EOM
282 void FGJSBsim::init()
286 SG_LOG( SG_FLIGHT, SG_INFO, "Starting and initializing JSBsim" );
288 // Explicitly call the superclass's
289 // init method first.
292 Atmosphere->UseInternal();
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());
301 tmp = turbulence_gain->getDoubleValue();
302 Atmosphere->SetTurbGain(tmp * tmp * 100.0);
304 tmp = turbulence_rate->getDoubleValue();
305 Atmosphere->SetTurbRate(tmp);
308 Atmosphere->UseInternal();
312 fgic->SetVnorthFpsIC( wind_from_north->getDoubleValue() );
313 fgic->SetVeastFpsIC( wind_from_east->getDoubleValue() );
314 fgic->SetVdownFpsIC( wind_from_down->getDoubleValue() );
316 //Atmosphere->SetExTemperature(get_Static_temperature());
317 //Atmosphere->SetExPressure(get_Static_pressure());
318 //Atmosphere->SetExDensity(get_Density());
319 SG_LOG(SG_FLIGHT,SG_INFO,"T,p,rho: " << fdmex->GetAtmosphere()->GetTemperature()
320 << ", " << fdmex->GetAtmosphere()->GetPressure()
321 << ", " << fdmex->GetAtmosphere()->GetDensity() );
323 if (fgGetBool("/sim/presets/running")) {
324 for (int i=0; i < Propulsion->GetNumEngines(); i++) {
325 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
326 node->setBoolValue("running", true);
327 Propulsion->GetEngine(i)->SetRunning(true);
334 fdmex->RunIC(); //loop JSBSim once w/o integrating
335 copy_from_JSBsim(); //update the bus
337 SG_LOG( SG_FLIGHT, SG_INFO, " Initialized JSBSim with:" );
339 switch(fgic->GetSpeedSet()) {
341 SG_LOG(SG_FLIGHT,SG_INFO, " Vn,Ve,Vd= "
342 << Propagate->GetVel(eNorth) << ", "
343 << Propagate->GetVel(eEast) << ", "
344 << Propagate->GetVel(eDown) << " ft/s");
347 SG_LOG(SG_FLIGHT,SG_INFO, " U,V,W= "
348 << Propagate->GetUVW(1) << ", "
349 << Propagate->GetUVW(2) << ", "
350 << Propagate->GetUVW(3) << " ft/s");
353 SG_LOG(SG_FLIGHT,SG_INFO, " Mach: "
354 << Auxiliary->GetMach() );
358 SG_LOG(SG_FLIGHT,SG_INFO, " Indicated Airspeed: "
359 << Auxiliary->GetVcalibratedKTS() << " knots" );
363 stall_warning->setDoubleValue(0);
365 SG_LOG( SG_FLIGHT, SG_INFO, " Bank Angle: "
366 << Propagate->GetEuler(ePhi)*RADTODEG << " deg" );
367 SG_LOG( SG_FLIGHT, SG_INFO, " Pitch Angle: "
368 << Propagate->GetEuler(eTht)*RADTODEG << " deg" );
369 SG_LOG( SG_FLIGHT, SG_INFO, " True Heading: "
370 << Propagate->GetEuler(ePsi)*RADTODEG << " deg" );
371 SG_LOG( SG_FLIGHT, SG_INFO, " Latitude: "
372 << Propagate->GetLocation().GetLatitudeDeg() << " deg" );
373 SG_LOG( SG_FLIGHT, SG_INFO, " Longitude: "
374 << Propagate->GetLocation().GetLongitudeDeg() << " deg" );
375 SG_LOG( SG_FLIGHT, SG_INFO, " Altitude: "
376 << Propagate->Geth() << " feet" );
377 SG_LOG( SG_FLIGHT, SG_INFO, " loaded initial conditions" );
379 SG_LOG( SG_FLIGHT, SG_INFO, " set dt" );
381 SG_LOG( SG_FLIGHT, SG_INFO, "Finished initializing JSBSim" );
383 SG_LOG( SG_FLIGHT, SG_INFO, "FGControls::get_gear_down()= " <<
384 globals->get_controls()->get_gear_down() );
387 /******************************************************************************/
389 // Run an iteration of the EOM (equations of motion)
391 void FGJSBsim::update( double dt )
396 int multiloop = _calc_multiloop(dt);
400 // Compute the radius of the aircraft. That is the radius of a ball
401 // where all gear units are in. At the moment it is at least 10ft ...
403 int n_gears = GroundReactions->GetNumGearUnits();
404 for (i=0; i<n_gears; ++i) {
405 FGColumnVector3 bl = GroundReactions->GetGearUnit(i)->GetBodyLocation();
406 double r = bl.Magnitude();
411 // Compute the potential movement of this aircraft and query for the
412 // ground in this area.
413 double groundCacheRadius = acrad + 2*dt*Propagate->GetUVW().Magnitude();
414 double alt, slr, lat, lon;
415 FGColumnVector3 cart = Auxiliary->GetLocationVRP();
416 if ( needTrim && startup_trim->getBoolValue() ) {
417 alt = fgic->GetAltitudeFtIC();
418 slr = fgic->GetSeaLevelRadiusFtIC();
419 lat = fgic->GetLatitudeDegIC() * SGD_DEGREES_TO_RADIANS;
420 lon = fgic->GetLongitudeDegIC() * SGD_DEGREES_TO_RADIANS;
421 cart = FGLocation(lon, lat, alt+slr);
423 double cart_pos[3] = { cart(1), cart(2), cart(3) };
424 bool cache_ok = prepare_ground_cache_ft( State->Getsim_time(), cart_pos,
427 SG_LOG(SG_FLIGHT, SG_WARN,
428 "FGInterface is being called without scenery below the aircraft!");
429 cout << "altitude = " << alt << endl;
430 cout << "sea level radius = " << slr << endl;
431 cout << "latitude = " << lat << endl;
432 cout << "longitude = " << lon << endl;
438 trimmed->setBoolValue(false);
441 if ( startup_trim->getBoolValue() ) {
442 double contact[3], dummy[3], lc, ff, agl;
444 get_agl_ft(State->Getsim_time(), cart_pos, contact,
445 dummy, dummy, &groundtype, &lc, &ff, &agl);
446 double terrain_alt = sqrt(contact[0]*contact[0] + contact[1]*contact[1]
447 + contact[2]*contact[2]) - fgic->GetSeaLevelRadiusFtIC();
449 SG_LOG(SG_FLIGHT, SG_INFO,
450 "Ready to trim, terrain altitude is: "
451 << terrain_alt * SG_METER_TO_FEET );
453 fgic->SetTerrainAltitudeFtIC( terrain_alt );
456 fdmex->RunIC(); //apply any changes made through the set_ functions
461 for ( i=0; i < multiloop; i++ ) {
465 FGJSBBase::Message* msg;
466 while (fdmex->ReadMessage()) {
467 msg = fdmex->ProcessMessage();
469 case FGJSBBase::Message::eText:
470 SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text );
472 case FGJSBBase::Message::eBool:
473 SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->bVal );
475 case FGJSBBase::Message::eInteger:
476 SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->iVal );
478 case FGJSBBase::Message::eDouble:
479 SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->dVal );
482 SG_LOG( SG_FLIGHT, SG_INFO, "Unrecognized message type." );
487 // translate JSBsim back to FG structure so that the
488 // autopilot (and the rest of the sim can use the updated values
491 // crashed (altitude AGL < 0)
492 if (get_Altitude_AGL() < 0.0) {
493 crash_message = "Attempted to fly under ground.";
498 /******************************************************************************/
500 // Convert from the FGInterface struct to the JSBsim generic_ struct
502 bool FGJSBsim::copy_to_JSBsim()
507 // copy control positions into the JSBsim structure
509 FCS->SetDaCmd( globals->get_controls()->get_aileron());
510 FCS->SetRollTrimCmd( globals->get_controls()->get_aileron_trim() );
511 FCS->SetDeCmd( globals->get_controls()->get_elevator());
512 FCS->SetPitchTrimCmd( globals->get_controls()->get_elevator_trim() );
513 FCS->SetDrCmd( -globals->get_controls()->get_rudder() );
514 FCS->SetYawTrimCmd( -globals->get_controls()->get_rudder_trim() );
515 // FIXME: make that get_steering work
516 // FCS->SetDsCmd( globals->get_controls()->get_steering()/80.0 );
517 FCS->SetDsCmd( globals->get_controls()->get_rudder() );
518 FCS->SetDfCmd( globals->get_controls()->get_flaps() );
519 FCS->SetDsbCmd( globals->get_controls()->get_speedbrake() );
520 FCS->SetDspCmd( globals->get_controls()->get_spoilers() );
522 // Parking brake sets minimum braking
524 double parking_brake = globals->get_controls()->get_brake_parking();
525 FCS->SetLBrake(FMAX(globals->get_controls()->get_brake_left(), parking_brake));
526 FCS->SetRBrake(FMAX(globals->get_controls()->get_brake_right(), parking_brake));
527 FCS->SetCBrake( 0.0 );
528 // FCS->SetCBrake( globals->get_controls()->get_brake(2) );
530 FCS->SetGearCmd( globals->get_controls()->get_gear_down());
531 for (i = 0; i < Propulsion->GetNumEngines(); i++) {
532 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
534 FCS->SetThrottleCmd(i, globals->get_controls()->get_throttle(i));
535 FCS->SetMixtureCmd(i, globals->get_controls()->get_mixture(i));
536 FCS->SetPropAdvanceCmd(i, globals->get_controls()->get_prop_advance(i));
538 switch (Propulsion->GetEngine(i)->GetType()) {
539 case FGEngine::etPiston:
540 { // FGPiston code block
541 FGPiston* eng = (FGPiston*)Propulsion->GetEngine(i);
542 eng->SetMagnetos( globals->get_controls()->get_magnetos(i) );
544 } // end FGPiston code block
545 case FGEngine::etTurbine:
546 { // FGTurbine code block
547 FGTurbine* eng = (FGTurbine*)Propulsion->GetEngine(i);
548 eng->SetAugmentation( globals->get_controls()->get_augmentation(i) );
549 eng->SetReverse( globals->get_controls()->get_reverser(i) );
550 eng->SetInjection( globals->get_controls()->get_water_injection(i) );
551 eng->SetCutoff( globals->get_controls()->get_cutoff(i) );
552 eng->SetIgnition( globals->get_controls()->get_ignition(i) );
554 } // end FGTurbine code block
555 case FGEngine::etRocket:
556 { // FGRocket code block
557 FGRocket* eng = (FGRocket*)Propulsion->GetEngine(i);
559 } // end FGRocket code block
562 { // FGEngine code block
563 FGEngine* eng = Propulsion->GetEngine(i);
565 eng->SetStarter( globals->get_controls()->get_starter(i) );
566 eng->SetRunning( node->getBoolValue("running") );
567 } // end FGEngine code block
571 Propagate->SetSeaLevelRadius( get_Sea_level_radius() );
573 Atmosphere->SetExTemperature(
574 9.0/5.0*(temperature->getDoubleValue()+273.15) );
575 Atmosphere->SetExPressure(pressure->getDoubleValue()*70.726566);
576 Atmosphere->SetExDensity(density->getDoubleValue());
578 tmp = turbulence_gain->getDoubleValue();
579 Atmosphere->SetTurbGain(tmp * tmp * 100.0);
581 tmp = turbulence_rate->getDoubleValue();
582 Atmosphere->SetTurbRate(tmp);
584 Atmosphere->SetWindNED( wind_from_north->getDoubleValue(),
585 wind_from_east->getDoubleValue(),
586 wind_from_down->getDoubleValue() );
587 // SG_LOG(SG_FLIGHT,SG_INFO, "Wind NED: "
588 // << get_V_north_airmass() << ", "
589 // << get_V_east_airmass() << ", "
590 // << get_V_down_airmass() );
592 for (i = 0; i < Propulsion->GetNumTanks(); i++) {
593 SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
594 FGTank * tank = Propulsion->GetTank(i);
595 tank->SetContents(node->getDoubleValue("level-gal_us") * 6.6);
596 // tank->SetContents(node->getDoubleValue("level-lb"));
598 SGPropertyNode* node = fgGetNode("/systems/refuel", true);
599 Propulsion->SetRefuel(node->getDoubleValue("contact"));
600 Propulsion->SetFuelFreeze((fgGetNode("/sim/freeze/fuel",true))->getBoolValue());
605 /******************************************************************************/
607 // Convert from the JSBsim generic_ struct to the FGInterface struct
609 bool FGJSBsim::copy_from_JSBsim()
613 _set_Inertias( MassBalance->GetMass(),
614 MassBalance->GetIxx(),
615 MassBalance->GetIyy(),
616 MassBalance->GetIzz(),
617 MassBalance->GetIxz() );
619 _set_CG_Position( MassBalance->GetXYZcg(1),
620 MassBalance->GetXYZcg(2),
621 MassBalance->GetXYZcg(3) );
623 _set_Accels_Body( Aircraft->GetBodyAccel(1),
624 Aircraft->GetBodyAccel(2),
625 Aircraft->GetBodyAccel(3) );
627 _set_Accels_CG_Body_N ( Aircraft->GetNcg(1),
629 Aircraft->GetNcg(3) );
631 _set_Accels_Pilot_Body( Auxiliary->GetPilotAccel(1),
632 Auxiliary->GetPilotAccel(2),
633 Auxiliary->GetPilotAccel(3) );
635 _set_Nlf( Aircraft->GetNlf() );
639 _set_Velocities_Local( Propagate->GetVel(eNorth),
640 Propagate->GetVel(eEast),
641 Propagate->GetVel(eDown) );
643 _set_Velocities_Wind_Body( Propagate->GetUVW(1),
644 Propagate->GetUVW(2),
645 Propagate->GetUVW(3) );
647 // Make the HUD work ...
648 _set_Velocities_Ground( Propagate->GetVel(eNorth),
649 Propagate->GetVel(eEast),
650 -Propagate->GetVel(eDown) );
652 _set_V_rel_wind( Auxiliary->GetVt() );
654 _set_V_equiv_kts( Auxiliary->GetVequivalentKTS() );
656 _set_V_calibrated_kts( Auxiliary->GetVcalibratedKTS() );
658 _set_V_ground_speed( Auxiliary->GetVground() );
660 _set_Omega_Body( Propagate->GetPQR(eP),
661 Propagate->GetPQR(eQ),
662 Propagate->GetPQR(eR) );
664 _set_Euler_Rates( Auxiliary->GetEulerRates(ePhi),
665 Auxiliary->GetEulerRates(eTht),
666 Auxiliary->GetEulerRates(ePsi) );
668 _set_Mach_number( Auxiliary->GetMach() );
670 // Positions of Visual Reference Point
671 FGLocation l = Auxiliary->GetLocationVRP();
672 _updateGeocentricPosition( l.GetLatitude(), l.GetLongitude(),
673 l.GetRadius() - get_Sea_level_radius() );
675 _set_Altitude_AGL( Propagate->GetDistanceAGL() );
677 double loc_cart[3] = { l(eX), l(eY), l(eZ) };
678 double contact[3], d[3], sd, t;
680 is_valid_m(&t, d, &sd);
681 get_agl_ft(t, loc_cart, contact, d, d, &id, &sd, &sd, &sd);
683 = FGColumnVector3( contact[0], contact[1], contact[2] ).Magnitude();
684 _set_Runway_altitude( rwrad - get_Sea_level_radius() );
687 _set_Euler_Angles( Propagate->GetEuler(ePhi),
688 Propagate->GetEuler(eTht),
689 Propagate->GetEuler(ePsi) );
691 _set_Alpha( Auxiliary->Getalpha() );
692 _set_Beta( Auxiliary->Getbeta() );
695 _set_Gamma_vert_rad( Auxiliary->GetGamma() );
697 _set_Earth_position_angle( Auxiliary->GetEarthPositionAngle() );
699 _set_Climb_Rate( Propagate->Gethdot() );
701 const FGMatrix33& Tl2b = Propagate->GetTl2b();
702 for ( i = 1; i <= 3; i++ ) {
703 for ( j = 1; j <= 3; j++ ) {
704 _set_T_Local_to_Body( i, j, Tl2b(i,j) );
708 // Copy the engine values from JSBSim.
709 for ( i=0; i < Propulsion->GetNumEngines(); i++ ) {
710 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
712 sprintf(buf, "engines/engine[%d]/thruster", i);
713 SGPropertyNode * tnode = fgGetNode(buf, true);
714 FGThruster * thruster = Propulsion->GetEngine(i)->GetThruster();
716 switch (Propulsion->GetEngine(i)->GetType()) {
717 case FGEngine::etPiston:
718 { // FGPiston code block
719 FGPiston* eng = (FGPiston*)Propulsion->GetEngine(i);
720 node->setDoubleValue("egt-degf", eng->getExhaustGasTemp_degF());
721 node->setDoubleValue("oil-temperature-degf", eng->getOilTemp_degF());
722 node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi());
723 node->setDoubleValue("mp-osi", eng->getManifoldPressure_inHg());
724 node->setDoubleValue("cht-degf", eng->getCylinderHeadTemp_degF());
725 node->setDoubleValue("rpm", eng->getRPM());
726 } // end FGPiston code block
728 case FGEngine::etRocket:
729 { // FGRocket code block
730 FGRocket* eng = (FGRocket*)Propulsion->GetEngine(i);
731 } // end FGRocket code block
733 case FGEngine::etTurbine:
734 { // FGTurbine code block
735 FGTurbine* eng = (FGTurbine*)Propulsion->GetEngine(i);
736 node->setDoubleValue("n1", eng->GetN1());
737 node->setDoubleValue("n2", eng->GetN2());
738 node->setDoubleValue("egt_degf", 32 + eng->GetEGT()*9/5);
739 node->setBoolValue("augmentation", eng->GetAugmentation());
740 node->setBoolValue("water-injection", eng->GetInjection());
741 node->setBoolValue("ignition", eng->GetIgnition());
742 node->setDoubleValue("nozzle-pos-norm", eng->GetNozzle());
743 node->setDoubleValue("inlet-pos-norm", eng->GetInlet());
744 node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi());
745 node->setBoolValue("reversed", eng->GetReversed());
746 node->setBoolValue("cutoff", eng->GetCutoff());
747 node->setDoubleValue("epr", eng->GetEPR());
748 globals->get_controls()->set_reverser(i, eng->GetReversed() );
749 globals->get_controls()->set_cutoff(i, eng->GetCutoff() );
750 globals->get_controls()->set_water_injection(i, eng->GetInjection() );
751 globals->get_controls()->set_augmentation(i, eng->GetAugmentation() );
752 } // end FGTurbine code block
754 case FGEngine::etElectric:
755 { // FGElectric code block
756 FGElectric* eng = (FGElectric*)Propulsion->GetEngine(i);
757 node->setDoubleValue("rpm", eng->getRPM());
758 } // end FGElectric code block
762 { // FGEngine code block
763 FGEngine* eng = Propulsion->GetEngine(i);
764 node->setDoubleValue("fuel-flow-gph", eng->getFuelFlow_gph());
765 node->setDoubleValue("thrust_lb", thruster->GetThrust());
766 node->setDoubleValue("fuel-flow_pph", eng->getFuelFlow_pph());
767 node->setBoolValue("running", eng->GetRunning());
768 node->setBoolValue("starter", eng->GetStarter());
769 node->setBoolValue("cranking", eng->GetCranking());
770 globals->get_controls()->set_starter(i, eng->GetStarter() );
771 } // end FGEngine code block
773 switch (thruster->GetType()) {
774 case FGThruster::ttNozzle:
775 { // FGNozzle code block
776 FGNozzle* noz = (FGNozzle*)thruster;
777 } // end FGNozzle code block
779 case FGThruster::ttPropeller:
780 { // FGPropeller code block
781 FGPropeller* prop = (FGPropeller*)thruster;
782 tnode->setDoubleValue("rpm", thruster->GetRPM());
783 tnode->setDoubleValue("pitch", prop->GetPitch());
784 tnode->setDoubleValue("torque", prop->GetTorque());
785 } // end FGPropeller code block
787 case FGThruster::ttRotor:
788 { // FGRotor code block
789 FGRotor* rotor = (FGRotor*)thruster;
790 } // end FGRotor code block
792 case FGThruster::ttDirect:
793 { // Direct code block
794 } // end Direct code block
800 // Copy the fuel levels from JSBSim if fuel
801 // freeze not enabled.
802 if ( ! Propulsion->GetFuelFreeze() ) {
803 for (i = 0; i < Propulsion->GetNumTanks(); i++) {
804 SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
805 FGTank* tank = Propulsion->GetTank(i);
806 double contents = tank->GetContents();
807 double temp = tank->GetTemperature_degC();
808 node->setDoubleValue("level-gal_us", contents/6.6);
809 node->setDoubleValue("level-lb", contents);
810 if (temp != -9999.0) node->setDoubleValue("temperature_degC", temp);
816 stall_warning->setDoubleValue( Aerodynamics->GetStallWarn() );
818 elevator_pos_pct->setDoubleValue( FCS->GetDePos(ofNorm) );
819 left_aileron_pos_pct->setDoubleValue( FCS->GetDaLPos(ofNorm) );
820 right_aileron_pos_pct->setDoubleValue( FCS->GetDaRPos(ofNorm) );
821 rudder_pos_pct->setDoubleValue( -1*FCS->GetDrPos(ofNorm) );
822 flap_pos_pct->setDoubleValue( FCS->GetDfPos(ofNorm) );
823 speedbrake_pos_pct->setDoubleValue( FCS->GetDsbPos(ofNorm) );
824 spoilers_pos_pct->setDoubleValue( FCS->GetDspPos(ofNorm) );
830 bool FGJSBsim::ToggleDataLogging(void)
832 return fdmex->GetOutput()->Toggle();
836 bool FGJSBsim::ToggleDataLogging(bool state)
839 fdmex->GetOutput()->Enable();
842 fdmex->GetOutput()->Disable();
849 void FGJSBsim::set_Latitude(double lat)
851 static const SGPropertyNode *altitude = fgGetNode("/position/altitude-ft");
853 double sea_level_radius_meters, lat_geoc;
855 // In case we're not trimming
856 FGInterface::set_Latitude(lat);
858 if ( altitude->getDoubleValue() > -9990 ) {
859 alt = altitude->getDoubleValue();
865 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat );
866 SG_LOG(SG_FLIGHT,SG_INFO," cur alt (ft) = " << alt );
868 sgGeodToGeoc( lat, alt * SG_FEET_TO_METER,
869 &sea_level_radius_meters, &lat_geoc );
870 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
871 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
872 fgic->SetLatitudeRadIC( lat_geoc );
877 void FGJSBsim::set_Longitude(double lon)
879 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon );
881 // In case we're not trimming
882 FGInterface::set_Longitude(lon);
885 fgic->SetLongitudeRadIC( lon );
889 void FGJSBsim::set_Altitude(double alt)
891 static const SGPropertyNode *latitude = fgGetNode("/position/latitude-deg");
893 double sea_level_radius_meters,lat_geoc;
895 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt );
896 SG_LOG(SG_FLIGHT,SG_INFO, " lat (deg) = " << latitude->getDoubleValue() );
898 // In case we're not trimming
899 FGInterface::set_Altitude(alt);
902 sgGeodToGeoc( latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS, alt,
903 &sea_level_radius_meters, &lat_geoc);
904 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
905 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
906 SG_LOG(SG_FLIGHT, SG_INFO,
907 "Terrain altitude: " << cur_fdm_state->get_Runway_altitude() * SG_METER_TO_FEET );
908 fgic->SetLatitudeRadIC( lat_geoc );
909 fgic->SetAltitudeFtIC(alt);
913 void FGJSBsim::set_V_calibrated_kts(double vc)
915 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " << vc );
917 // In case we're not trimming
918 FGInterface::set_V_calibrated_kts(vc);
921 fgic->SetVcalibratedKtsIC(vc);
925 void FGJSBsim::set_Mach_number(double mach)
927 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " << mach );
929 // In case we're not trimming
930 FGInterface::set_Mach_number(mach);
933 fgic->SetMachIC(mach);
937 void FGJSBsim::set_Velocities_Local( double north, double east, double down )
939 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: "
940 << north << ", " << east << ", " << down );
942 // In case we're not trimming
943 FGInterface::set_Velocities_Local(north, east, down);
946 fgic->SetVnorthFpsIC(north);
947 fgic->SetVeastFpsIC(east);
948 fgic->SetVdownFpsIC(down);
952 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w)
954 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
955 << u << ", " << v << ", " << w );
957 // In case we're not trimming
958 FGInterface::set_Velocities_Wind_Body(u, v, w);
961 fgic->SetUBodyFpsIC(u);
962 fgic->SetVBodyFpsIC(v);
963 fgic->SetWBodyFpsIC(w);
968 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi )
970 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: "
971 << phi << ", " << theta << ", " << psi );
973 // In case we're not trimming
974 FGInterface::set_Euler_Angles(phi, theta, psi);
977 fgic->SetPitchAngleRadIC(theta);
978 fgic->SetRollAngleRadIC(phi);
979 fgic->SetTrueHeadingRadIC(psi);
984 void FGJSBsim::set_Climb_Rate( double roc)
986 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
988 // In case we're not trimming
989 FGInterface::set_Climb_Rate(roc);
992 //since both climb rate and flight path angle are set in the FG
993 //startup sequence, something is needed to keep one from cancelling
995 if( !(fabs(roc) > 1 && fabs(fgic->GetFlightPathAngleRadIC()) < 0.01) ) {
996 fgic->SetClimbRateFpsIC(roc);
1001 void FGJSBsim::set_Gamma_vert_rad( double gamma)
1003 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
1006 if( !(fabs(gamma) < 0.01 && fabs(fgic->GetClimbRateFpsIC()) > 1) ) {
1007 fgic->SetFlightPathAngleRadIC(gamma);
1012 void FGJSBsim::init_gear(void )
1014 FGGroundReactions* gr=fdmex->GetGroundReactions();
1015 int Ngear=GroundReactions->GetNumGearUnits();
1016 for (int i=0;i<Ngear;i++) {
1017 FGLGear *gear = gr->GetGearUnit(i);
1018 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
1019 node->setDoubleValue("xoffset-in", gear->GetBodyLocation()(1));
1020 node->setDoubleValue("yoffset-in", gear->GetBodyLocation()(2));
1021 node->setDoubleValue("zoffset-in", gear->GetBodyLocation()(3));
1022 node->setBoolValue("wow", gear->GetWOW());
1023 node->setBoolValue("has-brake", gear->GetBrakeGroup() > 0);
1024 node->setDoubleValue("position-norm", FCS->GetGearPos());
1025 node->setDoubleValue("tire-pressure-norm", gear->GetTirePressure());
1026 if ( gear->GetSteerable() )
1027 node->setDoubleValue("steering-norm", gear->GetSteerNorm());
1031 void FGJSBsim::update_gear(void)
1033 FGGroundReactions* gr=fdmex->GetGroundReactions();
1034 int Ngear=GroundReactions->GetNumGearUnits();
1035 for (int i=0;i<Ngear;i++) {
1036 FGLGear *gear = gr->GetGearUnit(i);
1037 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
1038 node->getChild("wow", 0, true)->setBoolValue( gear->GetWOW());
1039 node->getChild("position-norm", 0, true)->setDoubleValue(FCS->GetGearPos());
1040 gear->SetTirePressure(node->getDoubleValue("tire-pressure-norm"));
1041 if ( gear->GetSteerable() )
1042 node->setDoubleValue("steering-norm", gear->GetSteerNorm());
1046 void FGJSBsim::do_trim(void)
1050 if ( fgGetBool("/sim/presets/onground") )
1052 fgic->SetVcalibratedKtsIC(0.0);
1053 fgtrim = new FGTrim(fdmex,tGround);
1055 fgtrim = new FGTrim(fdmex,tLongitudinal);
1058 if ( !fgtrim->DoTrim() ) {
1060 fgtrim->TrimStats();
1062 trimmed->setBoolValue(true);
1064 if (FGJSBBase::debug_lvl > 0)
1065 State->ReportState();
1069 pitch_trim->setDoubleValue( FCS->GetPitchTrimCmd() );
1070 throttle_trim->setDoubleValue( FCS->GetThrottleCmd(0) );
1071 aileron_trim->setDoubleValue( FCS->GetDaCmd() );
1072 rudder_trim->setDoubleValue( FCS->GetDrCmd() );
1074 globals->get_controls()->set_elevator_trim(FCS->GetPitchTrimCmd());
1075 globals->get_controls()->set_elevator(FCS->GetDeCmd());
1076 globals->get_controls()->set_throttle(FGControls::ALL_ENGINES,
1077 FCS->GetThrottleCmd(0));
1079 globals->get_controls()->set_aileron(FCS->GetDaCmd());
1080 globals->get_controls()->set_rudder( FCS->GetDrCmd());
1082 SG_LOG( SG_FLIGHT, SG_INFO, " Trim complete" );
1085 void FGJSBsim::update_ic(void)
1088 fgic->SetLatitudeRadIC(get_Lat_geocentric() );
1089 fgic->SetLongitudeRadIC( get_Longitude() );
1090 fgic->SetAltitudeFtIC( get_Altitude() );
1091 fgic->SetVcalibratedKtsIC( get_V_calibrated_kts() );
1092 fgic->SetPitchAngleRadIC( get_Theta() );
1093 fgic->SetRollAngleRadIC( get_Phi() );
1094 fgic->SetTrueHeadingRadIC( get_Psi() );
1095 fgic->SetClimbRateFpsIC( get_Climb_Rate() );
1099 void FGJSBsim::crash_handler(void)
1101 if (crashed) return; // we already crashed
1103 fgSetBool("/sim/crashed", true);
1104 SG_LOG( SG_FLIGHT, SG_WARN, " Crash: " << crash_message );
1105 if (reset_on_crash) {
1106 SGPropertyNode* node = fgGetNode("/sim/presets", true);
1107 globals->get_commands()->execute("old-reinit-dialog", node);
1109 fgSetBool("/sim/freeze/master", true);
1110 fgSetBool("/sim/freeze/clock", true);