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 Lesser 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 // Lesser General Public License for more details.
17 // You should have received a copy of the GNU Lesser 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.
21 // $Id: JSBSim.cxx,v 1.64 2010/10/31 04:49:25 jberndt Exp $
28 #include <simgear/compiler.h>
30 #include <stdio.h> // size_t
33 #include <simgear/constants.h>
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/math/sg_geodesy.hxx>
36 #include <simgear/misc/sg_path.hxx>
37 #include <simgear/structure/commands.hxx>
39 #include <FDM/flight.hxx>
41 #include <Aircraft/controls.hxx>
42 #include <Main/globals.hxx>
43 #include <Main/fg_props.hxx>
46 #include <FDM/JSBSim/FGFDMExec.h>
47 #include <FDM/JSBSim/FGJSBBase.h>
48 #include <FDM/JSBSim/initialization/FGInitialCondition.h>
49 #include <FDM/JSBSim/initialization/FGTrim.h>
50 #include <FDM/JSBSim/models/FGModel.h>
51 #include <FDM/JSBSim/models/FGAircraft.h>
52 #include <FDM/JSBSim/models/FGFCS.h>
53 #include <FDM/JSBSim/models/FGPropagate.h>
54 #include <FDM/JSBSim/models/FGAuxiliary.h>
55 #include <FDM/JSBSim/models/FGInertial.h>
56 #include <FDM/JSBSim/models/FGAtmosphere.h>
57 #include <FDM/JSBSim/models/FGMassBalance.h>
58 #include <FDM/JSBSim/models/FGAerodynamics.h>
59 #include <FDM/JSBSim/models/FGLGear.h>
60 #include <FDM/JSBSim/models/FGGroundReactions.h>
61 #include <FDM/JSBSim/models/FGPropulsion.h>
62 #include <FDM/JSBSim/models/propulsion/FGEngine.h>
63 #include <FDM/JSBSim/models/propulsion/FGPiston.h>
64 #include <FDM/JSBSim/models/propulsion/FGTurbine.h>
65 #include <FDM/JSBSim/models/propulsion/FGTurboProp.h>
66 #include <FDM/JSBSim/models/propulsion/FGRocket.h>
67 #include <FDM/JSBSim/models/propulsion/FGElectric.h>
68 #include <FDM/JSBSim/models/propulsion/FGNozzle.h>
69 #include <FDM/JSBSim/models/propulsion/FGPropeller.h>
70 #include <FDM/JSBSim/models/propulsion/FGRotor.h>
71 #include <FDM/JSBSim/models/propulsion/FGTank.h>
72 #include <FDM/JSBSim/input_output/FGPropertyManager.h>
73 #include <FDM/JSBSim/input_output/FGGroundCallback.h>
75 using namespace JSBSim;
78 FMAX (double a, double b)
83 class FGFSGroundCallback : public FGGroundCallback {
85 FGFSGroundCallback(FGJSBsim* ifc) : mInterface(ifc) {}
86 virtual ~FGFSGroundCallback() {}
88 /** Get the altitude above sea level dependent on the location. */
89 virtual double GetAltitude(const FGLocation& l) const {
90 double pt[3] = { SG_FEET_TO_METER*l(eX),
91 SG_FEET_TO_METER*l(eY),
92 SG_FEET_TO_METER*l(eZ) };
94 sgCartToGeod( pt, &lat, &lon, &alt);
95 return alt * SG_METER_TO_FEET;
98 /** Compute the altitude above ground. */
99 virtual double GetAGLevel(double t, const FGLocation& l,
100 FGLocation& cont, FGColumnVector3& n,
101 FGColumnVector3& v, FGColumnVector3& w) const {
102 double loc_cart[3] = { l(eX), l(eY), l(eZ) };
103 double contact[3], normal[3], vel[3], angularVel[3], agl = 0;
104 mInterface->get_agl_ft(t, loc_cart, SG_METER_TO_FEET*2, contact, normal,
105 vel, angularVel, &agl);
106 n = FGColumnVector3( normal[0], normal[1], normal[2] );
107 v = FGColumnVector3( vel[0], vel[1], vel[2] );
108 w = FGColumnVector3( angularVel[0], angularVel[1], angularVel[2] );
109 cont = FGColumnVector3( contact[0], contact[1], contact[2] );
113 FGJSBsim* mInterface;
116 /******************************************************************************/
118 FGJSBsim::FGJSBsim( double dt )
119 : FGInterface(dt), got_wire(false)
122 // Set up the debugging level
123 // FIXME: this will not respond to
126 // if flight is excluded, don't bother
127 if ((logbuf::get_log_classes() & SG_FLIGHT) != 0) {
129 // do a rough-and-ready mapping to
130 // the levels documented in FGFDMExec.h
131 switch (logbuf::get_log_priority()) {
133 FGJSBBase::debug_lvl = 0x1f;
136 FGJSBBase::debug_lvl = 0x0f;
138 FGJSBBase::debug_lvl = 0x01;
142 FGJSBBase::debug_lvl = 0x00;
147 resetPropertyState();
149 fdmex = new FGFDMExec( (FGPropertyManager*)globals->get_props() );
151 // Register ground callback.
152 fdmex->SetGroundCallback( new FGFSGroundCallback(this) );
154 Atmosphere = fdmex->GetAtmosphere();
155 FCS = fdmex->GetFCS();
156 MassBalance = fdmex->GetMassBalance();
157 Propulsion = fdmex->GetPropulsion();
158 Aircraft = fdmex->GetAircraft();
159 Propagate = fdmex->GetPropagate();
160 Auxiliary = fdmex->GetAuxiliary();
161 Inertial = fdmex->GetInertial();
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" );
173 SGPath systems_path( fgGetString("/sim/aircraft-dir") );
174 systems_path.append( "Systems" );
176 // deprecate sim-time-sec for simulation/sim-time-sec
177 // remove alias with increased configuration file version number (2.1 or later)
178 SGPropertyNode * node = fgGetNode("/fdm/jsbsim/simulation/sim-time-sec");
179 fgGetNode("/fdm/jsbsim/sim-time-sec", true)->alias( node );
180 // end of sim-time-sec deprecation patch
184 result = fdmex->LoadModel( aircraft_path.str(),
187 fgGetString("/sim/aero"), false );
190 SG_LOG( SG_FLIGHT, SG_INFO, " loaded aero.");
192 SG_LOG( SG_FLIGHT, SG_INFO,
193 " aero does not exist (you may have mis-typed the name).");
197 SG_LOG( SG_FLIGHT, SG_INFO, "" );
198 SG_LOG( SG_FLIGHT, SG_INFO, "" );
199 SG_LOG( SG_FLIGHT, SG_INFO, "After loading aero definition file ..." );
201 int Neng = Propulsion->GetNumEngines();
202 SG_LOG( SG_FLIGHT, SG_INFO, "num engines = " << Neng );
204 if ( GroundReactions->GetNumGearUnits() <= 0 ) {
205 SG_LOG( SG_FLIGHT, SG_ALERT, "num gear units = "
206 << GroundReactions->GetNumGearUnits() );
207 SG_LOG( SG_FLIGHT, SG_ALERT, "This is a very bad thing because with 0 gear units, the ground trimming");
208 SG_LOG( SG_FLIGHT, SG_ALERT, "routine (coming up later in the code) will core dump.");
209 SG_LOG( SG_FLIGHT, SG_ALERT, "Halting the sim now, and hoping a solution will present itself soon!");
215 // Set initial fuel levels if provided.
216 for (unsigned int i = 0; i < Propulsion->GetNumTanks(); i++) {
217 SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
218 if (node->getChild("level-gal_us", 0, false) != 0) {
219 Propulsion->GetTank(i)->SetContents(node->getDoubleValue("level-gal_us") * 6.6);
221 node->setDoubleValue("level-lbs", Propulsion->GetTank(i)->GetContents());
222 node->setDoubleValue("level-gal_us", Propulsion->GetTank(i)->GetContents() / 6.6);
224 node->setDoubleValue("capacity-gal_us",
225 Propulsion->GetTank(i)->GetCapacity() / 6.6);
227 Propulsion->SetFuelFreeze((fgGetNode("/sim/freeze/fuel",true))->getBoolValue());
229 fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
230 fgSetDouble("/fdm/trim/throttle", FCS->GetThrottleCmd(0));
231 fgSetDouble("/fdm/trim/aileron", FCS->GetDaCmd());
232 fgSetDouble("/fdm/trim/rudder", FCS->GetDrCmd());
234 startup_trim = fgGetNode("/sim/presets/trim", true);
236 trimmed = fgGetNode("/fdm/trim/trimmed", true);
237 trimmed->setBoolValue(false);
239 pitch_trim = fgGetNode("/fdm/trim/pitch-trim", true );
240 throttle_trim = fgGetNode("/fdm/trim/throttle", true );
241 aileron_trim = fgGetNode("/fdm/trim/aileron", true );
242 rudder_trim = fgGetNode("/fdm/trim/rudder", true );
244 stall_warning = fgGetNode("/sim/alarms/stall-warning",true);
245 stall_warning->setDoubleValue(0);
248 flap_pos_pct=fgGetNode("/surface-positions/flap-pos-norm",true);
249 elevator_pos_pct=fgGetNode("/surface-positions/elevator-pos-norm",true);
251 =fgGetNode("/surface-positions/left-aileron-pos-norm",true);
252 right_aileron_pos_pct
253 =fgGetNode("/surface-positions/right-aileron-pos-norm",true);
254 rudder_pos_pct=fgGetNode("/surface-positions/rudder-pos-norm",true);
256 =fgGetNode("/surface-positions/speedbrake-pos-norm",true);
257 spoilers_pos_pct=fgGetNode("/surface-positions/spoilers-pos-norm",true);
258 tailhook_pos_pct=fgGetNode("/gear/tailhook/position-norm",true);
259 wing_fold_pos_pct=fgGetNode("surface-positions/wing-fold-pos-norm",true);
261 elevator_pos_pct->setDoubleValue(0);
262 left_aileron_pos_pct->setDoubleValue(0);
263 right_aileron_pos_pct->setDoubleValue(0);
264 rudder_pos_pct->setDoubleValue(0);
265 flap_pos_pct->setDoubleValue(0);
266 speedbrake_pos_pct->setDoubleValue(0);
267 spoilers_pos_pct->setDoubleValue(0);
269 ab_brake_engaged = fgGetNode("/autopilot/autobrake/engaged", true);
270 ab_brake_left_pct = fgGetNode("/autopilot/autobrake/brake-left-output", true);
271 ab_brake_right_pct = fgGetNode("/autopilot/autobrake/brake-right-output", true);
273 temperature = fgGetNode("/environment/temperature-degc",true);
274 pressure = fgGetNode("/environment/pressure-inhg",true);
275 density = fgGetNode("/environment/density-slugft3",true);
276 turbulence_gain = fgGetNode("/environment/turbulence/magnitude-norm",true);
277 turbulence_rate = fgGetNode("/environment/turbulence/rate-hz",true);
279 wind_from_north= fgGetNode("/environment/wind-from-north-fps",true);
280 wind_from_east = fgGetNode("/environment/wind-from-east-fps" ,true);
281 wind_from_down = fgGetNode("/environment/wind-from-down-fps" ,true);
283 slaved = fgGetNode("/sim/slaved/enabled", true);
285 for (unsigned int i = 0; i < Propulsion->GetNumEngines(); i++) {
286 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
287 Propulsion->GetEngine(i)->GetThruster()->SetRPM(node->getDoubleValue("rpm") /
288 Propulsion->GetEngine(i)->GetThruster()->GetGearRatio());
291 hook_root_struct = FGColumnVector3(
292 fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-offset-x-in", 196),
293 fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-offset-y-in", 0),
294 fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-offset-z-in", -16));
299 /******************************************************************************/
300 FGJSBsim::~FGJSBsim(void)
305 /******************************************************************************/
307 // Initialize the JSBsim flight model, dt is the time increment for
308 // each subsequent iteration through the EOM
310 void FGJSBsim::init()
312 SG_LOG( SG_FLIGHT, SG_INFO, "Starting and initializing JSBsim" );
314 // Explicitly call the superclass's
315 // init method first.
317 if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
318 Atmosphere->UseExternal();
319 Atmosphere->SetExTemperature(
320 9.0/5.0*(temperature->getDoubleValue()+273.15) );
321 Atmosphere->SetExPressure(pressure->getDoubleValue()*70.726566);
322 Atmosphere->SetExDensity(density->getDoubleValue());
323 Atmosphere->SetTurbType(FGAtmosphere::ttCulp);
324 Atmosphere->SetTurbGain(turbulence_gain->getDoubleValue());
325 Atmosphere->SetTurbRate(turbulence_rate->getDoubleValue());
328 Atmosphere->UseInternal();
331 fgic->SetVNorthFpsIC( -wind_from_north->getDoubleValue() );
332 fgic->SetVEastFpsIC( -wind_from_east->getDoubleValue() );
333 fgic->SetVDownFpsIC( -wind_from_down->getDoubleValue() );
335 //Atmosphere->SetExTemperature(get_Static_temperature());
336 //Atmosphere->SetExPressure(get_Static_pressure());
337 //Atmosphere->SetExDensity(get_Density());
338 SG_LOG(SG_FLIGHT,SG_INFO,"T,p,rho: " << fdmex->GetAtmosphere()->GetTemperature()
339 << ", " << fdmex->GetAtmosphere()->GetPressure()
340 << ", " << fdmex->GetAtmosphere()->GetDensity() );
342 // deprecate egt_degf for egt-degf to have consistent naming
343 // TODO: raise log-level to ALERT in summer 2010,
344 // remove alias in fall 2010,
345 // remove this code in winter 2010
346 for (unsigned int i=0; i < Propulsion->GetNumEngines(); i++) {
347 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
348 SGPropertyNode * egtn = node->getNode( "egt_degf" );
350 SG_LOG(SG_FLIGHT,SG_WARN,
351 "Aircraft uses deprecated node egt_degf. Please upgrade to egt-degf");
352 node->getNode("egt-degf", true)->alias( egtn );
355 // end of egt_degf deprecation patch
357 if (fgGetBool("/sim/presets/running")) {
358 for (unsigned int i=0; i < Propulsion->GetNumEngines(); i++) {
359 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
360 node->setBoolValue("running", true);
361 Propulsion->GetEngine(i)->SetRunning(true);
365 FCS->SetDfPos( ofNorm, globals->get_controls()->get_flaps() );
370 fdmex->RunIC(); //loop JSBSim once w/o integrating
371 copy_from_JSBsim(); //update the bus
373 SG_LOG( SG_FLIGHT, SG_INFO, " Initialized JSBSim with:" );
375 switch(fgic->GetSpeedSet()) {
377 SG_LOG(SG_FLIGHT,SG_INFO, " Vn,Ve,Vd= "
378 << Propagate->GetVel(FGJSBBase::eNorth) << ", "
379 << Propagate->GetVel(FGJSBBase::eEast) << ", "
380 << Propagate->GetVel(FGJSBBase::eDown) << " ft/s");
383 SG_LOG(SG_FLIGHT,SG_INFO, " U,V,W= "
384 << Propagate->GetUVW(1) << ", "
385 << Propagate->GetUVW(2) << ", "
386 << Propagate->GetUVW(3) << " ft/s");
389 SG_LOG(SG_FLIGHT,SG_INFO, " Mach: "
390 << Auxiliary->GetMach() );
394 SG_LOG(SG_FLIGHT,SG_INFO, " Indicated Airspeed: "
395 << Auxiliary->GetVcalibratedKTS() << " knots" );
399 stall_warning->setDoubleValue(0);
401 SG_LOG( SG_FLIGHT, SG_INFO, " Bank Angle: "
402 << Propagate->GetEuler(FGJSBBase::ePhi)*RADTODEG << " deg" );
403 SG_LOG( SG_FLIGHT, SG_INFO, " Pitch Angle: "
404 << Propagate->GetEuler(FGJSBBase::eTht)*RADTODEG << " deg" );
405 SG_LOG( SG_FLIGHT, SG_INFO, " True Heading: "
406 << Propagate->GetEuler(FGJSBBase::ePsi)*RADTODEG << " deg" );
407 SG_LOG( SG_FLIGHT, SG_INFO, " Latitude: "
408 << Propagate->GetLocation().GetLatitudeDeg() << " deg" );
409 SG_LOG( SG_FLIGHT, SG_INFO, " Longitude: "
410 << Propagate->GetLocation().GetLongitudeDeg() << " deg" );
411 SG_LOG( SG_FLIGHT, SG_INFO, " Altitude: "
412 << Propagate->GetAltitudeASL() << " feet" );
413 SG_LOG( SG_FLIGHT, SG_INFO, " loaded initial conditions" );
415 SG_LOG( SG_FLIGHT, SG_INFO, " set dt" );
417 SG_LOG( SG_FLIGHT, SG_INFO, "Finished initializing JSBSim" );
419 SG_LOG( SG_FLIGHT, SG_INFO, "FGControls::get_gear_down()= " <<
420 globals->get_controls()->get_gear_down() );
423 /******************************************************************************/
425 // Run an iteration of the EOM (equations of motion)
427 void FGJSBsim::update( double dt )
430 if(!fgGetBool("/sim/crashed"))
431 fgSetBool("/sim/crashed", true);
438 int multiloop = _calc_multiloop(dt);
442 // Compute the radius of the aircraft. That is the radius of a ball
443 // where all gear units are in. At the moment it is at least 10ft ...
445 int n_gears = GroundReactions->GetNumGearUnits();
446 for (i=0; i<n_gears; ++i) {
447 FGColumnVector3 bl = GroundReactions->GetGearUnit(i)->GetBodyLocation();
448 double r = bl.Magnitude();
453 // Compute the potential movement of this aircraft and query for the
454 // ground in this area.
455 double groundCacheRadius = acrad + 2*dt*Propagate->GetUVW().Magnitude();
456 double alt, slr, lat, lon;
457 FGLocation cart = Auxiliary->GetLocationVRP();
458 if ( needTrim && startup_trim->getBoolValue() ) {
459 alt = fgic->GetAltitudeASLFtIC();
460 slr = fgic->GetSeaLevelRadiusFtIC();
461 lat = fgic->GetLatitudeDegIC() * SGD_DEGREES_TO_RADIANS;
462 lon = fgic->GetLongitudeDegIC() * SGD_DEGREES_TO_RADIANS;
463 cart = FGLocation(lon, lat, alt+slr);
465 double cart_pos[3] = { cart(1), cart(2), cart(3) };
466 double t0 = fdmex->GetSimTime();
467 bool cache_ok = prepare_ground_cache_ft( t0, t0 + dt, cart_pos,
470 SG_LOG(SG_FLIGHT, SG_WARN,
471 "FGInterface is being called without scenery below the aircraft!");
473 alt = fgic->GetAltitudeASLFtIC();
474 SG_LOG(SG_FLIGHT, SG_WARN, "altitude = " << alt);
476 slr = fgic->GetSeaLevelRadiusFtIC();
477 SG_LOG(SG_FLIGHT, SG_WARN, "sea level radius = " << slr);
479 lat = fgic->GetLatitudeDegIC() * SGD_DEGREES_TO_RADIANS;
480 SG_LOG(SG_FLIGHT, SG_WARN, "latitude = " << lat);
482 lon = fgic->GetLongitudeDegIC() * SGD_DEGREES_TO_RADIANS;
483 SG_LOG(SG_FLIGHT, SG_WARN, "longitude = " << lon);
489 trimmed->setBoolValue(false);
492 if ( startup_trim->getBoolValue() ) {
493 double contact[3], d[3], vel[3], agl;
494 get_agl_ft(fdmex->GetSimTime(), cart_pos, SG_METER_TO_FEET*2, contact,
496 double terrain_alt = sqrt(contact[0]*contact[0] + contact[1]*contact[1]
497 + contact[2]*contact[2]) - fgic->GetSeaLevelRadiusFtIC();
499 SG_LOG(SG_FLIGHT, SG_INFO,
500 "Ready to trim, terrain elevation is: "
501 << terrain_alt * SG_METER_TO_FEET );
503 if (fgGetBool("/sim/presets/onground")) {
504 FGColumnVector3 gndVelNED = cart.GetTec2l()
505 * FGColumnVector3(vel[0], vel[1], vel[2]);
506 fgic->SetVNorthFpsIC(gndVelNED(1));
507 fgic->SetVEastFpsIC(gndVelNED(2));
508 fgic->SetVDownFpsIC(gndVelNED(3));
510 fgic->SetTerrainElevationFtIC( terrain_alt );
513 fdmex->RunIC(); //apply any changes made through the set_ functions
518 for ( i=0; i < multiloop; i++ ) {
520 update_external_forces(fdmex->GetSimTime() + i * fdmex->GetDeltaT());
523 FGJSBBase::Message* msg;
524 while (msg = fdmex->ProcessNextMessage()) {
525 // msg = fdmex->ProcessNextMessage();
527 case FGJSBBase::Message::eText:
528 if (msg->text == "Crash Detected: Simulation FREEZE.")
530 SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text );
532 case FGJSBBase::Message::eBool:
533 SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->bVal );
535 case FGJSBBase::Message::eInteger:
536 SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->iVal );
538 case FGJSBBase::Message::eDouble:
539 SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->dVal );
542 SG_LOG( SG_FLIGHT, SG_INFO, "Unrecognized message type." );
547 // translate JSBsim back to FG structure so that the
548 // autopilot (and the rest of the sim can use the updated values
552 /******************************************************************************/
554 // Convert from the FGInterface struct to the JSBsim generic_ struct
556 bool FGJSBsim::copy_to_JSBsim()
561 // copy control positions into the JSBsim structure
563 FCS->SetDaCmd( globals->get_controls()->get_aileron());
564 FCS->SetRollTrimCmd( globals->get_controls()->get_aileron_trim() );
565 FCS->SetDeCmd( globals->get_controls()->get_elevator());
566 FCS->SetPitchTrimCmd( globals->get_controls()->get_elevator_trim() );
567 FCS->SetDrCmd( -globals->get_controls()->get_rudder() );
568 FCS->SetYawTrimCmd( -globals->get_controls()->get_rudder_trim() );
569 FCS->SetDsCmd( globals->get_controls()->get_rudder() );
570 FCS->SetDfCmd( globals->get_controls()->get_flaps() );
571 FCS->SetDsbCmd( globals->get_controls()->get_speedbrake() );
572 FCS->SetDspCmd( globals->get_controls()->get_spoilers() );
574 // Parking brake sets minimum braking
576 double parking_brake = globals->get_controls()->get_brake_parking();
577 double left_brake = globals->get_controls()->get_brake_left();
578 double right_brake = globals->get_controls()->get_brake_right();
580 if (ab_brake_engaged->getBoolValue()) {
581 left_brake = ab_brake_left_pct->getDoubleValue();
582 right_brake = ab_brake_right_pct->getDoubleValue();
585 FCS->SetLBrake(FMAX(left_brake, parking_brake));
586 FCS->SetRBrake(FMAX(right_brake, parking_brake));
589 FCS->SetCBrake( 0.0 );
590 // FCS->SetCBrake( globals->get_controls()->get_brake(2) );
592 FCS->SetGearCmd( globals->get_controls()->get_gear_down());
593 for (i = 0; i < Propulsion->GetNumEngines(); i++) {
594 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
596 FCS->SetThrottleCmd(i, globals->get_controls()->get_throttle(i));
597 FCS->SetMixtureCmd(i, globals->get_controls()->get_mixture(i));
598 FCS->SetPropAdvanceCmd(i, globals->get_controls()->get_prop_advance(i));
599 FCS->SetFeatherCmd(i, globals->get_controls()->get_feather(i));
601 switch (Propulsion->GetEngine(i)->GetType()) {
602 case FGEngine::etPiston:
603 { // FGPiston code block
604 FGPiston* eng = (FGPiston*)Propulsion->GetEngine(i);
605 eng->SetMagnetos( globals->get_controls()->get_magnetos(i) );
607 } // end FGPiston code block
608 case FGEngine::etTurbine:
609 { // FGTurbine code block
610 FGTurbine* eng = (FGTurbine*)Propulsion->GetEngine(i);
611 eng->SetAugmentation( globals->get_controls()->get_augmentation(i) );
612 eng->SetReverse( globals->get_controls()->get_reverser(i) );
613 //eng->SetInjection( globals->get_controls()->get_water_injection(i) );
614 eng->SetCutoff( globals->get_controls()->get_cutoff(i) );
615 eng->SetIgnition( globals->get_controls()->get_ignition(i) );
617 } // end FGTurbine code block
618 case FGEngine::etRocket:
619 { // FGRocket code block
620 FGRocket* eng = (FGRocket*)Propulsion->GetEngine(i);
622 } // end FGRocket code block
623 case FGEngine::etTurboprop:
624 { // FGTurboProp code block
625 FGTurboProp* eng = (FGTurboProp*)Propulsion->GetEngine(i);
626 eng->SetReverse( globals->get_controls()->get_reverser(i) );
627 eng->SetCutoff( globals->get_controls()->get_cutoff(i) );
628 eng->SetIgnition( globals->get_controls()->get_ignition(i) );
630 eng->SetGeneratorPower( globals->get_controls()->get_generator_breaker(i) );
631 eng->SetCondition( globals->get_controls()->get_condition(i) );
633 } // end FGTurboProp code block
638 { // FGEngine code block
639 FGEngine* eng = Propulsion->GetEngine(i);
641 eng->SetStarter( globals->get_controls()->get_starter(i) );
642 eng->SetRunning( node->getBoolValue("running") );
643 } // end FGEngine code block
647 Propagate->SetSeaLevelRadius( get_Sea_level_radius() );
649 Atmosphere->SetExTemperature(
650 9.0/5.0*(temperature->getDoubleValue()+273.15) );
651 Atmosphere->SetExPressure(pressure->getDoubleValue()*70.726566);
652 Atmosphere->SetExDensity(density->getDoubleValue());
654 tmp = turbulence_gain->getDoubleValue();
655 //Atmosphere->SetTurbGain(tmp * tmp * 100.0);
657 tmp = turbulence_rate->getDoubleValue();
658 //Atmosphere->SetTurbRate(tmp);
660 Atmosphere->SetWindNED( -wind_from_north->getDoubleValue(),
661 -wind_from_east->getDoubleValue(),
662 -wind_from_down->getDoubleValue() );
663 // SG_LOG(SG_FLIGHT,SG_INFO, "Wind NED: "
664 // << get_V_north_airmass() << ", "
665 // << get_V_east_airmass() << ", "
666 // << get_V_down_airmass() );
668 for (i = 0; i < Propulsion->GetNumTanks(); i++) {
669 SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
670 FGTank * tank = Propulsion->GetTank(i);
671 tank->SetContents(node->getDoubleValue("level-gal_us") * 6.6);
672 // tank->SetContents(node->getDoubleValue("level-lbs"));
675 Propulsion->SetFuelFreeze((fgGetNode("/sim/freeze/fuel",true))->getBoolValue());
676 fdmex->SetChild(slaved->getBoolValue());
681 /******************************************************************************/
683 // Convert from the JSBsim generic_ struct to the FGInterface struct
685 bool FGJSBsim::copy_from_JSBsim()
689 _set_Inertias( MassBalance->GetMass(),
690 MassBalance->GetIxx(),
691 MassBalance->GetIyy(),
692 MassBalance->GetIzz(),
693 MassBalance->GetIxz() );
695 _set_CG_Position( MassBalance->GetXYZcg(1),
696 MassBalance->GetXYZcg(2),
697 MassBalance->GetXYZcg(3) );
699 _set_Accels_Body( Aircraft->GetBodyAccel(1),
700 Aircraft->GetBodyAccel(2),
701 Aircraft->GetBodyAccel(3) );
703 _set_Accels_CG_Body_N ( Aircraft->GetNcg(1),
705 Aircraft->GetNcg(3) );
707 _set_Accels_Pilot_Body( Auxiliary->GetPilotAccel(1),
708 Auxiliary->GetPilotAccel(2),
709 Auxiliary->GetPilotAccel(3) );
711 _set_Nlf( Aircraft->GetNlf() );
715 _set_Velocities_Local( Propagate->GetVel(FGJSBBase::eNorth),
716 Propagate->GetVel(FGJSBBase::eEast),
717 Propagate->GetVel(FGJSBBase::eDown) );
719 _set_Velocities_Wind_Body( Propagate->GetUVW(1),
720 Propagate->GetUVW(2),
721 Propagate->GetUVW(3) );
723 // Make the HUD work ...
724 _set_Velocities_Ground( Propagate->GetVel(FGJSBBase::eNorth),
725 Propagate->GetVel(FGJSBBase::eEast),
726 -Propagate->GetVel(FGJSBBase::eDown) );
728 _set_V_rel_wind( Auxiliary->GetVt() );
730 _set_V_equiv_kts( Auxiliary->GetVequivalentKTS() );
732 _set_V_calibrated_kts( Auxiliary->GetVcalibratedKTS() );
734 _set_V_ground_speed( Auxiliary->GetVground() );
736 _set_Omega_Body( Propagate->GetPQR(FGJSBBase::eP),
737 Propagate->GetPQR(FGJSBBase::eQ),
738 Propagate->GetPQR(FGJSBBase::eR) );
740 _set_Euler_Rates( Auxiliary->GetEulerRates(FGJSBBase::ePhi),
741 Auxiliary->GetEulerRates(FGJSBBase::eTht),
742 Auxiliary->GetEulerRates(FGJSBBase::ePsi) );
744 _set_Mach_number( Auxiliary->GetMach() );
746 // Positions of Visual Reference Point
747 FGLocation l = Auxiliary->GetLocationVRP();
748 _updateGeocentricPosition( l.GetLatitude(), l.GetLongitude(),
749 l.GetRadius() - get_Sea_level_radius() );
751 _set_Altitude_AGL( Propagate->GetDistanceAGL() );
753 double loc_cart[3] = { l(FGJSBBase::eX), l(FGJSBBase::eY), l(FGJSBBase::eZ) };
754 double contact[3], d[3], sd, t;
755 is_valid_m(&t, d, &sd);
756 get_agl_ft(t, loc_cart, SG_METER_TO_FEET*2, contact, d, d, d, &sd);
758 = FGColumnVector3( contact[0], contact[1], contact[2] ).Magnitude();
759 _set_Runway_altitude( rwrad - get_Sea_level_radius() );
762 _set_Euler_Angles( Propagate->GetEuler(FGJSBBase::ePhi),
763 Propagate->GetEuler(FGJSBBase::eTht),
764 Propagate->GetEuler(FGJSBBase::ePsi) );
766 _set_Alpha( Auxiliary->Getalpha() );
767 _set_Beta( Auxiliary->Getbeta() );
770 _set_Gamma_vert_rad( Auxiliary->GetGamma() );
772 _set_Earth_position_angle( Inertial->GetEarthPositionAngle() );
774 _set_Climb_Rate( Propagate->Gethdot() );
776 const FGMatrix33& Tl2b = Propagate->GetTl2b();
777 for ( i = 1; i <= 3; i++ ) {
778 for ( j = 1; j <= 3; j++ ) {
779 _set_T_Local_to_Body( i, j, Tl2b(i,j) );
783 // Copy the engine values from JSBSim.
784 for ( i=0; i < Propulsion->GetNumEngines(); i++ ) {
785 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
786 SGPropertyNode * tnode = node->getChild("thruster", 0, true);
787 FGThruster * thruster = Propulsion->GetEngine(i)->GetThruster();
789 switch (Propulsion->GetEngine(i)->GetType()) {
790 case FGEngine::etPiston:
791 { // FGPiston code block
792 FGPiston* eng = (FGPiston*)Propulsion->GetEngine(i);
793 node->setDoubleValue("egt-degf", eng->getExhaustGasTemp_degF());
794 node->setDoubleValue("oil-temperature-degf", eng->getOilTemp_degF());
795 node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi());
796 node->setDoubleValue("mp-osi", eng->getManifoldPressure_inHg());
797 // NOTE: mp-osi is not in ounces per square inch.
798 // This error is left for reasons of backwards compatibility with
799 // existing FlightGear sound and instrument configurations.
800 node->setDoubleValue("mp-inhg", eng->getManifoldPressure_inHg());
801 node->setDoubleValue("cht-degf", eng->getCylinderHeadTemp_degF());
802 node->setDoubleValue("rpm", eng->getRPM());
803 } // end FGPiston code block
805 case FGEngine::etRocket:
806 { // FGRocket code block
807 FGRocket* eng = (FGRocket*)Propulsion->GetEngine(i);
808 } // end FGRocket code block
810 case FGEngine::etTurbine:
811 { // FGTurbine code block
812 FGTurbine* eng = (FGTurbine*)Propulsion->GetEngine(i);
813 node->setDoubleValue("n1", eng->GetN1());
814 node->setDoubleValue("n2", eng->GetN2());
815 node->setDoubleValue("egt-degf", 32 + eng->GetEGT()*9/5);
816 node->setBoolValue("augmentation", eng->GetAugmentation());
817 node->setBoolValue("water-injection", eng->GetInjection());
818 node->setBoolValue("ignition", eng->GetIgnition());
819 node->setDoubleValue("nozzle-pos-norm", eng->GetNozzle());
820 node->setDoubleValue("inlet-pos-norm", eng->GetInlet());
821 node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi());
822 node->setBoolValue("reversed", eng->GetReversed());
823 node->setBoolValue("cutoff", eng->GetCutoff());
824 node->setDoubleValue("epr", eng->GetEPR());
825 globals->get_controls()->set_reverser(i, eng->GetReversed() );
826 globals->get_controls()->set_cutoff(i, eng->GetCutoff() );
827 globals->get_controls()->set_water_injection(i, eng->GetInjection() );
828 globals->get_controls()->set_augmentation(i, eng->GetAugmentation() );
829 } // end FGTurbine code block
831 case FGEngine::etTurboprop:
832 { // FGTurboProp code block
833 FGTurboProp* eng = (FGTurboProp*)Propulsion->GetEngine(i);
834 node->setDoubleValue("n1", eng->GetN1());
835 //node->setDoubleValue("n2", eng->GetN2());
836 node->setDoubleValue("itt_degf", 32 + eng->GetITT()*9/5);
837 node->setBoolValue("ignition", eng->GetIgnition());
838 node->setDoubleValue("nozzle-pos-norm", eng->GetNozzle());
839 node->setDoubleValue("inlet-pos-norm", eng->GetInlet());
840 node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi());
841 node->setBoolValue("reversed", eng->GetReversed());
842 node->setBoolValue("cutoff", eng->GetCutoff());
843 node->setBoolValue("starting", eng->GetEngStarting());
844 node->setBoolValue("generator-power", eng->GetGeneratorPower());
845 node->setBoolValue("damaged", eng->GetCondition());
846 node->setBoolValue("ielu-intervent", eng->GetIeluIntervent());
847 node->setDoubleValue("oil-temperature-degf", eng->getOilTemp_degF());
848 // node->setBoolValue("onfire", eng->GetFire());
849 globals->get_controls()->set_reverser(i, eng->GetReversed() );
850 globals->get_controls()->set_cutoff(i, eng->GetCutoff() );
851 } // end FGTurboProp code block
853 case FGEngine::etElectric:
854 { // FGElectric code block
855 FGElectric* eng = (FGElectric*)Propulsion->GetEngine(i);
856 node->setDoubleValue("rpm", eng->getRPM());
857 } // end FGElectric code block
859 case FGEngine::etUnknown:
863 { // FGEngine code block
864 FGEngine* eng = Propulsion->GetEngine(i);
865 node->setDoubleValue("fuel-flow-gph", eng->getFuelFlow_gph());
866 node->setDoubleValue("thrust_lb", thruster->GetThrust());
867 node->setDoubleValue("fuel-flow_pph", eng->getFuelFlow_pph());
868 node->setBoolValue("running", eng->GetRunning());
869 node->setBoolValue("starter", eng->GetStarter());
870 node->setBoolValue("cranking", eng->GetCranking());
871 globals->get_controls()->set_starter(i, eng->GetStarter() );
872 } // end FGEngine code block
874 switch (thruster->GetType()) {
875 case FGThruster::ttNozzle:
876 { // FGNozzle code block
877 FGNozzle* noz = (FGNozzle*)thruster;
878 } // end FGNozzle code block
880 case FGThruster::ttPropeller:
881 { // FGPropeller code block
882 FGPropeller* prop = (FGPropeller*)thruster;
883 tnode->setDoubleValue("rpm", thruster->GetRPM());
884 tnode->setDoubleValue("pitch", prop->GetPitch());
885 tnode->setDoubleValue("torque", prop->GetTorque());
886 tnode->setBoolValue("feathered", prop->GetFeather());
887 } // end FGPropeller code block
889 case FGThruster::ttRotor:
890 { // FGRotor code block
891 FGRotor* rotor = (FGRotor*)thruster;
892 } // end FGRotor code block
894 case FGThruster::ttDirect:
895 { // Direct code block
896 } // end Direct code block
902 // Copy the fuel levels from JSBSim if fuel
903 // freeze not enabled.
904 if ( ! Propulsion->GetFuelFreeze() ) {
905 for (i = 0; i < Propulsion->GetNumTanks(); i++) {
906 SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
907 FGTank* tank = Propulsion->GetTank(i);
908 double contents = tank->GetContents();
909 double temp = tank->GetTemperature_degC();
910 node->setDoubleValue("level-gal_us", contents/6.6);
911 node->setDoubleValue("level-lbs", contents);
912 if (temp != -9999.0) node->setDoubleValue("temperature_degC", temp);
918 stall_warning->setDoubleValue( Aerodynamics->GetStallWarn() );
920 elevator_pos_pct->setDoubleValue( FCS->GetDePos(ofNorm) );
921 left_aileron_pos_pct->setDoubleValue( FCS->GetDaLPos(ofNorm) );
922 right_aileron_pos_pct->setDoubleValue( FCS->GetDaRPos(ofNorm) );
923 rudder_pos_pct->setDoubleValue( -1*FCS->GetDrPos(ofNorm) );
924 flap_pos_pct->setDoubleValue( FCS->GetDfPos(ofNorm) );
925 speedbrake_pos_pct->setDoubleValue( FCS->GetDsbPos(ofNorm) );
926 spoilers_pos_pct->setDoubleValue( FCS->GetDspPos(ofNorm) );
927 tailhook_pos_pct->setDoubleValue( FCS->GetTailhookPos() );
928 wing_fold_pos_pct->setDoubleValue( FCS->GetWingFoldPos() );
930 // force a sim crashed if crashed (altitude AGL < 0)
931 if (get_Altitude_AGL() < -100.0) {
932 fdmex->SuspendIntegration();
940 bool FGJSBsim::ToggleDataLogging(void)
942 // ToDo: handle this properly
943 fdmex->DisableOutput();
948 bool FGJSBsim::ToggleDataLogging(bool state)
951 fdmex->EnableOutput();
954 fdmex->DisableOutput();
961 void FGJSBsim::set_Latitude(double lat)
963 static SGConstPropertyNode_ptr altitude = fgGetNode("/position/altitude-ft");
965 double sea_level_radius_meters, lat_geoc;
967 // In case we're not trimming
968 FGInterface::set_Latitude(lat);
970 if ( altitude->getDoubleValue() > -9990 ) {
971 alt = altitude->getDoubleValue();
977 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat );
978 SG_LOG(SG_FLIGHT,SG_INFO," cur alt (ft) = " << alt );
980 sgGeodToGeoc( lat, alt * SG_FEET_TO_METER,
981 &sea_level_radius_meters, &lat_geoc );
982 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
983 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
984 fgic->SetLatitudeRadIC( lat_geoc );
989 void FGJSBsim::set_Longitude(double lon)
991 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon );
993 // In case we're not trimming
994 FGInterface::set_Longitude(lon);
997 fgic->SetLongitudeRadIC( lon );
1001 // Sets the altitude above sea level.
1002 void FGJSBsim::set_Altitude(double alt)
1004 static SGConstPropertyNode_ptr latitude = fgGetNode("/position/latitude-deg");
1006 double sea_level_radius_meters,lat_geoc;
1008 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt );
1009 SG_LOG(SG_FLIGHT,SG_INFO, " lat (deg) = " << latitude->getDoubleValue() );
1011 // In case we're not trimming
1012 FGInterface::set_Altitude(alt);
1015 sgGeodToGeoc( latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS, alt,
1016 &sea_level_radius_meters, &lat_geoc);
1017 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
1018 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
1019 SG_LOG(SG_FLIGHT, SG_INFO,
1020 "Terrain elevation: " << FGInterface::get_Runway_altitude() * SG_METER_TO_FEET );
1021 fgic->SetLatitudeRadIC( lat_geoc );
1022 fgic->SetAltitudeASLFtIC(alt);
1026 void FGJSBsim::set_V_calibrated_kts(double vc)
1028 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " << vc );
1030 // In case we're not trimming
1031 FGInterface::set_V_calibrated_kts(vc);
1034 fgic->SetVcalibratedKtsIC(vc);
1038 void FGJSBsim::set_Mach_number(double mach)
1040 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " << mach );
1042 // In case we're not trimming
1043 FGInterface::set_Mach_number(mach);
1046 fgic->SetMachIC(mach);
1050 void FGJSBsim::set_Velocities_Local( double north, double east, double down )
1052 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: "
1053 << north << ", " << east << ", " << down );
1055 // In case we're not trimming
1056 FGInterface::set_Velocities_Local(north, east, down);
1059 fgic->SetVNorthFpsIC(north);
1060 fgic->SetVEastFpsIC(east);
1061 fgic->SetVDownFpsIC(down);
1065 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w)
1067 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
1068 << u << ", " << v << ", " << w );
1070 // In case we're not trimming
1071 FGInterface::set_Velocities_Wind_Body(u, v, w);
1074 fgic->SetUBodyFpsIC(u);
1075 fgic->SetVBodyFpsIC(v);
1076 fgic->SetWBodyFpsIC(w);
1081 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi )
1083 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: "
1084 << phi << ", " << theta << ", " << psi );
1086 // In case we're not trimming
1087 FGInterface::set_Euler_Angles(phi, theta, psi);
1090 fgic->SetThetaRadIC(theta);
1091 fgic->SetPhiRadIC(phi);
1092 fgic->SetPsiRadIC(psi);
1097 void FGJSBsim::set_Climb_Rate( double roc)
1099 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
1101 // In case we're not trimming
1102 FGInterface::set_Climb_Rate(roc);
1105 //since both climb rate and flight path angle are set in the FG
1106 //startup sequence, something is needed to keep one from cancelling
1108 if( !(fabs(roc) > 1 && fabs(fgic->GetFlightPathAngleRadIC()) < 0.01) ) {
1109 fgic->SetClimbRateFpsIC(roc);
1114 void FGJSBsim::set_Gamma_vert_rad( double gamma)
1116 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
1119 if( !(fabs(gamma) < 0.01 && fabs(fgic->GetClimbRateFpsIC()) > 1) ) {
1120 fgic->SetFlightPathAngleRadIC(gamma);
1125 void FGJSBsim::init_gear(void )
1127 FGGroundReactions* gr=fdmex->GetGroundReactions();
1128 int Ngear=GroundReactions->GetNumGearUnits();
1129 for (int i=0;i<Ngear;i++) {
1130 FGLGear *gear = gr->GetGearUnit(i);
1131 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
1132 node->setDoubleValue("xoffset-in", gear->GetBodyLocation()(1));
1133 node->setDoubleValue("yoffset-in", gear->GetBodyLocation()(2));
1134 node->setDoubleValue("zoffset-in", gear->GetBodyLocation()(3));
1135 node->setBoolValue("wow", gear->GetWOW());
1136 node->setDoubleValue("rollspeed-ms", gear->GetWheelRollVel()*0.3043);
1137 node->setBoolValue("has-brake", gear->GetBrakeGroup() > 0);
1138 node->setDoubleValue("position-norm", gear->GetGearUnitPos());
1139 node->setDoubleValue("tire-pressure-norm", gear->GetTirePressure());
1140 node->setDoubleValue("compression-norm", gear->GetCompLen());
1141 node->setDoubleValue("compression-ft", gear->GetCompLen());
1142 if ( gear->GetSteerable() )
1143 node->setDoubleValue("steering-norm", gear->GetSteerNorm());
1147 void FGJSBsim::update_gear(void)
1149 FGGroundReactions* gr=fdmex->GetGroundReactions();
1150 int Ngear=GroundReactions->GetNumGearUnits();
1151 for (int i=0;i<Ngear;i++) {
1152 FGLGear *gear = gr->GetGearUnit(i);
1153 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
1154 node->getChild("wow", 0, true)->setBoolValue( gear->GetWOW());
1155 node->getChild("rollspeed-ms", 0, true)->setDoubleValue(gear->GetWheelRollVel()*0.3043);
1156 node->getChild("position-norm", 0, true)->setDoubleValue(gear->GetGearUnitPos());
1157 gear->SetTirePressure(node->getDoubleValue("tire-pressure-norm"));
1158 node->setDoubleValue("compression-norm", gear->GetCompLen());
1159 node->setDoubleValue("compression-ft", gear->GetCompLen());
1160 if ( gear->GetSteerable() )
1161 node->setDoubleValue("steering-norm", gear->GetSteerNorm());
1165 void FGJSBsim::do_trim(void)
1169 if ( fgGetBool("/sim/presets/onground") )
1171 fgtrim = new FGTrim(fdmex,tGround);
1173 fgtrim = new FGTrim(fdmex,tLongitudinal);
1176 if ( !fgtrim->DoTrim() ) {
1178 fgtrim->TrimStats();
1180 trimmed->setBoolValue(true);
1184 pitch_trim->setDoubleValue( FCS->GetPitchTrimCmd() );
1185 throttle_trim->setDoubleValue( FCS->GetThrottleCmd(0) );
1186 aileron_trim->setDoubleValue( FCS->GetDaCmd() );
1187 rudder_trim->setDoubleValue( FCS->GetDrCmd() );
1189 globals->get_controls()->set_elevator_trim(FCS->GetPitchTrimCmd());
1190 globals->get_controls()->set_elevator(FCS->GetDeCmd());
1191 globals->get_controls()->set_throttle(FGControls::ALL_ENGINES,
1192 FCS->GetThrottleCmd(0));
1194 globals->get_controls()->set_aileron(FCS->GetDaCmd());
1195 globals->get_controls()->set_rudder( FCS->GetDrCmd());
1197 SG_LOG( SG_FLIGHT, SG_INFO, " Trim complete" );
1200 void FGJSBsim::update_ic(void)
1203 fgic->SetLatitudeRadIC(get_Lat_geocentric() );
1204 fgic->SetLongitudeRadIC( get_Longitude() );
1205 fgic->SetAltitudeASLFtIC( get_Altitude() );
1206 fgic->SetVcalibratedKtsIC( get_V_calibrated_kts() );
1207 fgic->SetThetaRadIC( get_Theta() );
1208 fgic->SetPhiRadIC( get_Phi() );
1209 fgic->SetPsiRadIC( get_Psi() );
1210 fgic->SetClimbRateFpsIC( get_Climb_Rate() );
1215 FGJSBsim::get_agl_ft(double t, const double pt[3], double alt_off,
1216 double contact[3], double normal[3], double vel[3],
1217 double angularVel[3], double *agl)
1219 const SGMaterial* material;
1220 simgear::BVHNode::Id id;
1221 if (!FGInterface::get_agl_ft(t, pt, alt_off, contact, normal, vel,
1222 angularVel, material, id))
1224 SGGeod geodPt = SGGeod::fromCart(SG_FEET_TO_METER*SGVec3d(pt));
1225 SGQuatd hlToEc = SGQuatd::fromLonLat(geodPt);
1226 *agl = dot(hlToEc.rotate(SGVec3d(0, 0, 1)), SGVec3d(contact) - SGVec3d(pt));
1230 inline static double dot3(const FGColumnVector3& a, const FGColumnVector3& b)
1232 return a(1) * b(1) + a(2) * b(2) + a(3) * b(3);
1235 inline static double sqr(double x)
1240 static double angle_diff(double a, double b)
1242 double diff = fabs(a - b);
1243 if (diff > 180) diff = 360 - diff;
1248 static void check_hook_solution(const FGColumnVector3& ground_normal_body, double E, double hook_length, double sin_fi_guess, double cos_fi_guess, double* sin_fis, double* cos_fis, double* fis, int* points)
1250 FGColumnVector3 tip(-hook_length * cos_fi_guess, 0, hook_length * sin_fi_guess);
1251 double dist = dot3(tip, ground_normal_body);
1252 if (fabs(dist + E) < 0.0001) {
1253 sin_fis[*points] = sin_fi_guess;
1254 cos_fis[*points] = cos_fi_guess;
1255 fis[*points] = atan2(sin_fi_guess, cos_fi_guess) * SG_RADIANS_TO_DEGREES;
1261 static void check_hook_solution(const FGColumnVector3& ground_normal_body, double E, double hook_length, double sin_fi_guess, double* sin_fis, double* cos_fis, double* fis, int* points)
1263 if (sin_fi_guess >= -1 && sin_fi_guess <= 1) {
1264 double cos_fi_guess = sqrt(1 - sqr(sin_fi_guess));
1265 check_hook_solution(ground_normal_body, E, hook_length, sin_fi_guess, cos_fi_guess, sin_fis, cos_fis, fis, points);
1266 if (fabs(cos_fi_guess) > SG_EPSILON) {
1267 check_hook_solution(ground_normal_body, E, hook_length, sin_fi_guess, -cos_fi_guess, sin_fis, cos_fis, fis, points);
1272 void FGJSBsim::update_external_forces(double t_off)
1274 const FGMatrix33& Tb2l = Propagate->GetTb2l();
1275 const FGMatrix33& Tl2b = Propagate->GetTl2b();
1276 const FGLocation& Location = Propagate->GetLocation();
1277 const FGMatrix33& Tec2l = Location.GetTec2l();
1279 double hook_area[4][3];
1281 FGColumnVector3 hook_root_body = MassBalance->StructuralToBody(hook_root_struct);
1282 FGColumnVector3 hook_root = Location.LocalToLocation(Tb2l * hook_root_body);
1283 hook_area[1][0] = hook_root(1);
1284 hook_area[1][1] = hook_root(2);
1285 hook_area[1][2] = hook_root(3);
1287 hook_length = fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-length-ft", 6.75);
1288 double fi_min = fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-pos-min-deg", -18);
1289 double fi_max = fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-pos-max-deg", 30);
1290 double fi = fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-pos-norm") * (fi_max - fi_min) + fi_min;
1291 double cos_fi = cos(fi * SG_DEGREES_TO_RADIANS);
1292 double sin_fi = sin(fi * SG_DEGREES_TO_RADIANS);
1294 FGColumnVector3 hook_tip_body = hook_root_body;
1295 hook_tip_body(1) -= hook_length * cos_fi;
1296 hook_tip_body(3) += hook_length * sin_fi;
1299 double ground_normal[3];
1300 double ground_vel[3];
1301 double ground_angular_vel[3];
1305 bool got = get_agl_ft(t_off, hook_area[1], 0, contact, ground_normal,
1306 ground_vel, ground_angular_vel, &root_agl_ft);
1307 if (got && root_agl_ft > 0 && root_agl_ft < hook_length) {
1308 FGColumnVector3 ground_normal_body = Tl2b * (Tec2l * FGColumnVector3(ground_normal[0], ground_normal[1], ground_normal[2]));
1309 FGColumnVector3 contact_body = Tl2b * Location.LocationToLocal(FGColumnVector3(contact[0], contact[1], contact[2]));
1310 double D = -dot3(contact_body, ground_normal_body);
1312 // check hook tip agl against same ground plane
1313 double hook_tip_agl_ft = dot3(hook_tip_body, ground_normal_body) + D;
1314 if (hook_tip_agl_ft < 0) {
1316 // hook tip: hx - l cos, hy, hz + l sin
1317 // on ground: - n0 l cos + n2 l sin + E = 0
1319 double E = D + dot3(hook_root_body, ground_normal_body);
1321 // substitue x = sin fi, cos fi = sqrt(1 - x * x)
1322 // and rearrange to get a quadratic with coeffs:
1323 double a = sqr(hook_length) * (sqr(ground_normal_body(1)) + sqr(ground_normal_body(3)));
1324 double b = 2 * E * ground_normal_body(3) * hook_length;
1325 double c = sqr(E) - sqr(ground_normal_body(1) * hook_length);
1327 double disc = sqr(b) - 4 * a * c;
1329 double delta = sqrt(disc) / (2 * a);
1331 // allow 4 solutions for safety, should never happen
1337 double sin_fi_guess = -b / (2 * a) - delta;
1338 check_hook_solution(ground_normal_body, E, hook_length, sin_fi_guess, sin_fis, cos_fis, fis, &points);
1339 check_hook_solution(ground_normal_body, E, hook_length, sin_fi_guess + 2 * delta, sin_fis, cos_fis, fis, &points);
1342 double diff1 = angle_diff(fi, fis[0]);
1343 double diff2 = angle_diff(fi, fis[1]);
1344 int point = diff1 < diff2 ? 0 : 1;
1346 sin_fi = sin_fis[point];
1347 cos_fi = cos_fis[point];
1348 hook_tip_body(1) = hook_root_body(1) - hook_length * cos_fi;
1349 hook_tip_body(3) = hook_root_body(3) + hook_length * sin_fi;
1355 FGColumnVector3 hook_root_vel = Propagate->GetVel() + (Tb2l * (Propagate->GetPQR() * hook_root_body));
1356 double wire_ends_ec[2][3];
1357 double wire_vel_ec[2][3];
1358 get_wire_ends_ft(t_off, wire_ends_ec, wire_vel_ec);
1359 FGColumnVector3 wire_vel_1 = Tec2l * FGColumnVector3(wire_vel_ec[0][0], wire_vel_ec[0][1], wire_vel_ec[0][2]);
1360 FGColumnVector3 wire_vel_2 = Tec2l * FGColumnVector3(wire_vel_ec[1][0], wire_vel_ec[1][1], wire_vel_ec[1][2]);
1361 FGColumnVector3 rel_vel = hook_root_vel - (wire_vel_1 + wire_vel_2) / 2;
1362 if (rel_vel.Magnitude() < 3) {
1365 fgSetDouble("/fdm/jsbsim/external_reactions/hook/magnitude", 0.0);
1367 FGColumnVector3 wire_end1_body = Tl2b * Location.LocationToLocal(FGColumnVector3(wire_ends_ec[0][0], wire_ends_ec[0][1], wire_ends_ec[0][2])) - hook_root_body;
1368 FGColumnVector3 wire_end2_body = Tl2b * Location.LocationToLocal(FGColumnVector3(wire_ends_ec[1][0], wire_ends_ec[1][1], wire_ends_ec[1][2])) - hook_root_body;
1369 FGColumnVector3 force_plane_normal = wire_end1_body * wire_end2_body;
1370 force_plane_normal.Normalize();
1371 cos_fi = dot3(force_plane_normal, FGColumnVector3(0, 0, 1));
1372 if (cos_fi < 0) cos_fi = -cos_fi;
1373 sin_fi = sqrt(1 - sqr(cos_fi));
1374 fi = atan2(sin_fi, cos_fi) * SG_RADIANS_TO_DEGREES;
1376 fgSetDouble("/fdm/jsbsim/external_reactions/hook/x", -cos_fi);
1377 fgSetDouble("/fdm/jsbsim/external_reactions/hook/y", 0);
1378 fgSetDouble("/fdm/jsbsim/external_reactions/hook/z", sin_fi);
1379 fgSetDouble("/fdm/jsbsim/external_reactions/hook/magnitude", fgGetDouble("/fdm/jsbsim/systems/hook/force"));
1383 FGColumnVector3 hook_tip = Location.LocalToLocation(Tb2l * hook_tip_body);
1385 hook_area[0][0] = hook_tip(1);
1386 hook_area[0][1] = hook_tip(2);
1387 hook_area[0][2] = hook_tip(3);
1390 // The previous positions.
1391 hook_area[2][0] = last_hook_root[0];
1392 hook_area[2][1] = last_hook_root[1];
1393 hook_area[2][2] = last_hook_root[2];
1394 hook_area[3][0] = last_hook_tip[0];
1395 hook_area[3][1] = last_hook_tip[1];
1396 hook_area[3][2] = last_hook_tip[2];
1398 // Check if we caught a wire.
1399 // Returns true if we caught one.
1400 if (caught_wire_ft(t_off, hook_area)) {
1405 // save actual position as old position ...
1406 last_hook_tip[0] = hook_area[0][0];
1407 last_hook_tip[1] = hook_area[0][1];
1408 last_hook_tip[2] = hook_area[0][2];
1409 last_hook_root[0] = hook_area[1][0];
1410 last_hook_root[1] = hook_area[1][1];
1411 last_hook_root[2] = hook_area[1][2];
1413 fgSetDouble("/fdm/jsbsim/systems/hook/tailhook-pos-deg", fi);
1417 void FGJSBsim::resetPropertyState()
1419 // this code works-around bug #222:
1420 // http://code.google.com/p/flightgear-bugs/issues/detail?id=222
1421 // for whatever reason, having an existing value for the WOW
1422 // property causes the NaNs. Should that be fixed, this code can die
1423 SGPropertyNode* gear = fgGetNode("/fdm/jsbsim/gear", false);
1429 SGPropertyNode* unitNode = NULL;
1430 for (; (unitNode = gear->getChild("unit", index)) != NULL; ++index) {
1431 unitNode->removeChild("WOW", 0, false);