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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21 // $Id: FlightGear.cxx,v 1.15 2014/01/28 09:42:20 ehofman Exp $
28 #include <simgear/compiler.h>
29 #include <simgear/sg_inlines.h>
31 #include <cstdlib> // size_t
34 #include <simgear/constants.h>
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/math/sg_geodesy.hxx>
37 #include <simgear/misc/sg_path.hxx>
38 #include <simgear/structure/commands.hxx>
39 #include <simgear/bvh/BVHMaterial.hxx>
41 #include <FDM/flight.hxx>
43 #include <Aircraft/controls.hxx>
44 #include <Main/globals.hxx>
45 #include <Main/fg_props.hxx>
48 #include <FDM/JSBSim/FGFDMExec.h>
49 #include <FDM/JSBSim/FGJSBBase.h>
50 #include <FDM/JSBSim/initialization/FGInitialCondition.h>
51 #include <FDM/JSBSim/initialization/FGTrim.h>
52 #include <FDM/JSBSim/models/FGModel.h>
53 #include <FDM/JSBSim/models/FGAircraft.h>
54 #include <FDM/JSBSim/models/FGFCS.h>
55 #include <FDM/JSBSim/models/FGPropagate.h>
56 #include <FDM/JSBSim/models/FGAuxiliary.h>
57 #include <FDM/JSBSim/models/FGInertial.h>
58 #include <FDM/JSBSim/models/FGAtmosphere.h>
59 #include <FDM/JSBSim/models/FGMassBalance.h>
60 #include <FDM/JSBSim/models/FGAerodynamics.h>
61 #include <FDM/JSBSim/models/FGLGear.h>
62 #include <FDM/JSBSim/models/FGGroundReactions.h>
63 #include <FDM/JSBSim/models/FGPropulsion.h>
64 #include <FDM/JSBSim/models/FGAccelerations.h>
65 #include <FDM/JSBSim/models/atmosphere/FGWinds.h>
66 #include <FDM/JSBSim/models/propulsion/FGEngine.h>
67 #include <FDM/JSBSim/models/propulsion/FGPiston.h>
68 #include <FDM/JSBSim/models/propulsion/FGTurbine.h>
69 #include <FDM/JSBSim/models/propulsion/FGTurboProp.h>
70 #include <FDM/JSBSim/models/propulsion/FGRocket.h>
71 #include <FDM/JSBSim/models/propulsion/FGElectric.h>
72 #include <FDM/JSBSim/models/propulsion/FGNozzle.h>
73 #include <FDM/JSBSim/models/propulsion/FGPropeller.h>
74 #include <FDM/JSBSim/models/propulsion/FGRotor.h>
75 #include <FDM/JSBSim/models/propulsion/FGTank.h>
76 #include <FDM/JSBSim/input_output/FGPropertyManager.h>
77 #include <FDM/JSBSim/input_output/FGGroundCallback.h>
79 using namespace JSBSim;
82 FMAX (double a, double b)
87 class FGFSGroundCallback : public FGGroundCallback {
89 FGFSGroundCallback(FGJSBsim* ifc) : mInterface(ifc) {}
90 virtual ~FGFSGroundCallback() {}
92 /** Get the altitude above sea level dependent on the location. */
93 virtual double GetAltitude(const FGLocation& l) const {
94 double pt[3] = { SG_FEET_TO_METER*l(FGJSBBase::eX),
95 SG_FEET_TO_METER*l(FGJSBBase::eY),
96 SG_FEET_TO_METER*l(FGJSBBase::eZ) };
98 sgCartToGeod( pt, &lat, &lon, &alt);
99 return alt * SG_METER_TO_FEET;
102 /** Compute the altitude above ground. */
103 virtual double GetAGLevel(double t, const FGLocation& l,
104 FGLocation& cont, FGColumnVector3& n,
105 FGColumnVector3& v, FGColumnVector3& w) const {
106 double loc_cart[3] = { l(FGJSBBase::eX), l(FGJSBBase::eY), l(FGJSBBase::eZ) };
107 double contact[3], normal[3], vel[3], angularVel[3], agl = 0;
108 mInterface->get_agl_ft(t, loc_cart, SG_METER_TO_FEET*2, contact, normal,
109 vel, angularVel, &agl);
110 n = FGColumnVector3( normal[0], normal[1], normal[2] );
111 v = FGColumnVector3( vel[0], vel[1], vel[2] );
112 w = FGColumnVector3( angularVel[0], angularVel[1], angularVel[2] );
113 cont = FGColumnVector3( contact[0], contact[1], contact[2] );
117 virtual double GetTerrainGeoCentRadius(double t, const FGLocation& l) const {
118 double loc_cart[3] = { l(FGJSBBase::eX), l(FGJSBBase::eY), l(FGJSBBase::eZ) };
119 double contact[3], normal[3], vel[3], angularVel[3], agl = 0;
120 mInterface->get_agl_ft(t, loc_cart, SG_METER_TO_FEET*2, contact, normal,
121 vel, angularVel, &agl);
122 return sqrt(contact[0]*contact[0]+contact[1]*contact[1]+contact[2]*contact[2]);
125 virtual double GetSeaLevelRadius(const FGLocation& l) const {
126 double seaLevelRadius, latGeoc;
128 sgGeodToGeoc(l.GetGeodLatitudeRad(), l.GetGeodAltitude(),
129 &seaLevelRadius, &latGeoc);
131 return seaLevelRadius * SG_METER_TO_FEET;
134 virtual void SetTerrainGeoCentRadius(double radius) {}
135 virtual void SetSeaLevelRadius(double radius) {}
137 FGJSBsim* mInterface;
140 // FG uses a squared normalized magnitude for turbulence
141 // this lookup table maps fg's severity levels
142 // none(0), light(1/3), moderate(2/3) and severe(3/3)
143 // to the POE table indexes 0, 3, 4 and 7
144 class FGTurbulenceSeverityTable : public FGTable {
146 FGTurbulenceSeverityTable() : FGTable(4) {
147 *this << (0.0/9.0) << 0.0;
148 *this << (1.0/9.0) << 3.0;
149 *this << (4.0/9.0) << 4.0;
150 *this << (9.0/9.0) << 7.0;
154 /******************************************************************************/
155 std::map<std::string,int> FGJSBsim::TURBULENCE_TYPE_NAMES;
157 static FGTurbulenceSeverityTable TurbulenceSeverityTable;
159 FGJSBsim::FGJSBsim( double dt )
160 : FGInterface(dt), got_wire(false)
163 if( TURBULENCE_TYPE_NAMES.empty() ) {
164 TURBULENCE_TYPE_NAMES["ttNone"] = FGWinds::ttNone;
165 TURBULENCE_TYPE_NAMES["ttStandard"] = FGWinds::ttStandard;
166 TURBULENCE_TYPE_NAMES["ttCulp"] = FGWinds::ttCulp;
167 TURBULENCE_TYPE_NAMES["ttMilspec"] = FGWinds::ttMilspec;
168 TURBULENCE_TYPE_NAMES["ttTustin"] = FGWinds::ttTustin;
171 // Set up the debugging level
172 // FIXME: this will not respond to
175 if ((sglog().get_log_classes() & SG_FLIGHT) != 0) {
177 // do a rough-and-ready mapping to
178 // the levels documented in FGFDMExec.h
179 switch (sglog().get_log_priority()) {
181 FGJSBBase::debug_lvl = 0x1f;
184 FGJSBBase::debug_lvl = 0x1f;
188 FGJSBBase::debug_lvl = 0x00;
192 // if flight is excluded, don't bother
193 FGJSBBase::debug_lvl = 0x00;
196 PropertyManager = new FGPropertyManager( (FGPropertyNode*)globals->get_props() );
197 fdmex = new FGFDMExec( PropertyManager );
199 // Register ground callback.
200 fdmex->SetGroundCallback( new FGFSGroundCallback(this) );
202 Atmosphere = fdmex->GetAtmosphere();
203 Winds = fdmex->GetWinds();
204 FCS = fdmex->GetFCS();
205 MassBalance = fdmex->GetMassBalance();
206 Propulsion = fdmex->GetPropulsion();
207 Aircraft = fdmex->GetAircraft();
208 Propagate = fdmex->GetPropagate();
209 Auxiliary = fdmex->GetAuxiliary();
210 Inertial = fdmex->GetInertial();
211 Aerodynamics = fdmex->GetAerodynamics();
212 GroundReactions = fdmex->GetGroundReactions();
213 Accelerations = fdmex->GetAccelerations();
218 SGPath aircraft_path( fgGetString("/sim/aircraft-dir") );
220 SGPath engine_path( fgGetString("/sim/fg-root") );
221 engine_path.append( "Aircraft/Generic/JSBSim/Engines" );
223 SGPath systems_path( fgGetString("/sim/fg-root") );
224 systems_path.append( "Aircraft/Generic/JSBSim/Systems" );
226 // deprecate sim-time-sec for simulation/sim-time-sec
227 // remove alias with increased configuration file version number (2.1 or later)
228 SGPropertyNode * node = fgGetNode("/fdm/jsbsim/simulation/sim-time-sec");
229 fgGetNode("/fdm/jsbsim/sim-time-sec", true)->alias( node );
230 // end of sim-time-sec deprecation patch
232 terrain = fgGetNode("/sim/fdm/surface", true);
236 result = fdmex->LoadModel( aircraft_path.local8BitStr(),
237 engine_path.local8BitStr(),
238 systems_path.local8BitStr(),
239 fgGetString("/sim/aero"), false );
242 SG_LOG( SG_FLIGHT, SG_INFO, " loaded aero.");
244 SG_LOG( SG_FLIGHT, SG_INFO,
245 " aero does not exist (you may have mis-typed the name).");
249 SG_LOG( SG_FLIGHT, SG_INFO, "" );
250 SG_LOG( SG_FLIGHT, SG_INFO, "" );
251 SG_LOG( SG_FLIGHT, SG_INFO, "After loading aero definition file ..." );
253 int Neng = Propulsion->GetNumEngines();
254 SG_LOG( SG_FLIGHT, SG_INFO, "num engines = " << Neng );
256 if ( GroundReactions->GetNumGearUnits() <= 0 ) {
257 SG_LOG( SG_FLIGHT, SG_ALERT, "num gear units = "
258 << GroundReactions->GetNumGearUnits() );
259 SG_LOG( SG_FLIGHT, SG_ALERT, "This is a very bad thing because with 0 gear units, the ground trimming");
260 SG_LOG( SG_FLIGHT, SG_ALERT, "routine (coming up later in the code) will core dump.");
261 SG_LOG( SG_FLIGHT, SG_ALERT, "Halting the sim now, and hoping a solution will present itself soon!");
267 // Set initial fuel levels if provided.
268 for (unsigned int i = 0; i < Propulsion->GetNumTanks(); i++) {
270 SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
271 FGTank* tank = Propulsion->GetTank(i);
273 d = node->getNode( "density-ppg", true )->getDoubleValue();
275 tank->SetDensity( d );
277 node->getNode( "density-ppg", true )->setDoubleValue( SG_MAX2<double>(tank->GetDensity(), 0.1) );
280 d = node->getNode( "level-lbs", true )->getDoubleValue();
282 tank->SetContents( d );
284 node->getNode( "level-lbs", true )->setDoubleValue( tank->GetContents() );
286 /* Capacity is read-only in FGTank and can't be overwritten from FlightGear */
287 node->getNode("capacity-gal_us", true )->setDoubleValue( tank->GetCapacityGallons() );
289 Propulsion->SetFuelFreeze((fgGetNode("/sim/freeze/fuel",true))->getBoolValue());
291 fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
292 fgSetDouble("/fdm/trim/throttle", FCS->GetThrottleCmd(0));
293 fgSetDouble("/fdm/trim/aileron", FCS->GetDaCmd());
294 fgSetDouble("/fdm/trim/rudder", FCS->GetDrCmd());
296 startup_trim = fgGetNode("/sim/presets/trim", true);
298 trimmed = fgGetNode("/fdm/trim/trimmed", true);
299 trimmed->setBoolValue(false);
301 pitch_trim = fgGetNode("/fdm/trim/pitch-trim", true );
302 throttle_trim = fgGetNode("/fdm/trim/throttle", true );
303 aileron_trim = fgGetNode("/fdm/trim/aileron", true );
304 rudder_trim = fgGetNode("/fdm/trim/rudder", true );
306 stall_warning = fgGetNode("/sim/alarms/stall-warning",true);
307 stall_warning->setDoubleValue(0);
310 flap_pos_pct=fgGetNode("/surface-positions/flap-pos-norm",true);
311 elevator_pos_pct=fgGetNode("/surface-positions/elevator-pos-norm",true);
313 =fgGetNode("/surface-positions/left-aileron-pos-norm",true);
314 right_aileron_pos_pct
315 =fgGetNode("/surface-positions/right-aileron-pos-norm",true);
316 rudder_pos_pct=fgGetNode("/surface-positions/rudder-pos-norm",true);
318 =fgGetNode("/surface-positions/speedbrake-pos-norm",true);
319 spoilers_pos_pct=fgGetNode("/surface-positions/spoilers-pos-norm",true);
320 tailhook_pos_pct=fgGetNode("/gear/tailhook/position-norm",true);
321 wing_fold_pos_pct=fgGetNode("surface-positions/wing-fold-pos-norm",true);
323 elevator_pos_pct->setDoubleValue(0);
324 left_aileron_pos_pct->setDoubleValue(0);
325 right_aileron_pos_pct->setDoubleValue(0);
326 rudder_pos_pct->setDoubleValue(0);
327 flap_pos_pct->setDoubleValue(0);
328 speedbrake_pos_pct->setDoubleValue(0);
329 spoilers_pos_pct->setDoubleValue(0);
331 ab_brake_engaged = fgGetNode("/autopilot/autobrake/engaged", true);
332 ab_brake_left_pct = fgGetNode("/autopilot/autobrake/brake-left-output", true);
333 ab_brake_right_pct = fgGetNode("/autopilot/autobrake/brake-right-output", true);
335 altitude = fgGetNode("/position/altitude-ft");
336 temperature = fgGetNode("/environment/temperature-degc",true);
337 pressure = fgGetNode("/environment/pressure-inhg",true);
338 pressureSL = fgGetNode("/environment/pressure-sea-level-inhg",true);
339 ground_wind = fgGetNode("/environment/config/boundary/entry[0]/wind-speed-kt",true);
340 turbulence_gain = fgGetNode("/environment/turbulence/magnitude-norm",true);
341 turbulence_rate = fgGetNode("/environment/turbulence/rate-hz",true);
342 turbulence_model = fgGetNode("/environment/params/jsbsim-turbulence-model",true);
344 wind_from_north= fgGetNode("/environment/wind-from-north-fps",true);
345 wind_from_east = fgGetNode("/environment/wind-from-east-fps" ,true);
346 wind_from_down = fgGetNode("/environment/wind-from-down-fps" ,true);
348 slaved = fgGetNode("/sim/slaved/enabled", true);
350 for (unsigned int i = 0; i < Propulsion->GetNumEngines(); i++) {
351 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
352 Propulsion->GetEngine(i)->GetThruster()->SetRPM(node->getDoubleValue("rpm") /
353 Propulsion->GetEngine(i)->GetThruster()->GetGearRatio());
356 hook_root_struct = FGColumnVector3(
357 fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-offset-x-in", 196),
358 fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-offset-y-in", 0),
359 fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-offset-z-in", -16));
360 last_hook_tip[0] = 0; last_hook_tip[1] = 0; last_hook_tip[2] = 0;
361 last_hook_root[0] = 0; last_hook_root[1] = 0; last_hook_root[2] = 0;
366 /******************************************************************************/
367 FGJSBsim::~FGJSBsim(void)
370 delete PropertyManager;
373 /******************************************************************************/
375 // Initialize the JSBsim flight model, dt is the time increment for
376 // each subsequent iteration through the EOM
378 void FGJSBsim::init()
380 SG_LOG( SG_FLIGHT, SG_INFO, "Starting and initializing JSBsim" );
382 // Explicitly call the superclass's
383 // init method first.
385 if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
386 Atmosphere->SetTemperature(temperature->getDoubleValue(), get_Altitude(), FGAtmosphere::eCelsius);
387 Atmosphere->SetPressureSL(FGAtmosphere::eInchesHg, pressureSL->getDoubleValue());
388 // initialize to no turbulence, these values get set in the update loop
389 Winds->SetTurbType(FGWinds::ttNone);
390 Winds->SetTurbGain(0.0);
391 Winds->SetTurbRate(0.0);
392 Winds->SetWindspeed20ft(0.0);
393 Winds->SetProbabilityOfExceedence(0.0);
396 fgic->SetWindNEDFpsIC(0.0, 0.0, 0.0);
398 SG_LOG(SG_FLIGHT,SG_INFO,"T,p,rho: " << Atmosphere->GetTemperature()
399 << ", " << Atmosphere->GetPressure()
400 << ", " << Atmosphere->GetDensity() );
402 FCS->SetDfPos( ofNorm, globals->get_controls()->get_flaps() );
404 needTrim = startup_trim->getBoolValue();
408 fdmex->RunIC(); //loop JSBSim once w/o integrating
409 if (fgGetBool("/sim/presets/running")) {
410 Propulsion->InitRunning(-1);
411 for (unsigned int i = 0; i < Propulsion->GetNumEngines(); i++) {
412 FGPiston* eng = (FGPiston*)Propulsion->GetEngine(i);
413 globals->get_controls()->set_magnetos(i, eng->GetMagnetos());
414 globals->get_controls()->set_mixture(i, FCS->GetMixtureCmd(i));
419 const FGLocation& cart = fgic->GetPosition();
420 double cart_pos[3], contact[3], d[3], vel[3], agl;
421 update_ground_cache(cart, cart_pos, 0.01);
423 get_agl_ft(fdmex->GetSimTime(), cart_pos, SG_METER_TO_FEET*2, contact,
425 double terrain_alt = sqrt(contact[0]*contact[0] + contact[1]*contact[1]
426 + contact[2]*contact[2]) - cart.GetSeaLevelRadius();
428 SG_LOG(SG_FLIGHT, SG_INFO, "Ready to trim, terrain elevation is: "
431 if (fgGetBool("/sim/presets/onground")) {
432 FGColumnVector3 gndVelNED = cart.GetTec2l()
433 * FGColumnVector3(vel[0], vel[1], vel[2]);
434 fgic->SetVNorthFpsIC(gndVelNED(1));
435 fgic->SetVEastFpsIC(gndVelNED(2));
436 fgic->SetVDownFpsIC(gndVelNED(3));
442 copy_from_JSBsim(); //update the bus
444 SG_LOG( SG_FLIGHT, SG_INFO, " Initialized JSBSim with:" );
446 switch(fgic->GetSpeedSet()) {
448 SG_LOG(SG_FLIGHT,SG_INFO, " Vn,Ve,Vd= "
449 << Propagate->GetVel(FGJSBBase::eNorth) << ", "
450 << Propagate->GetVel(FGJSBBase::eEast) << ", "
451 << Propagate->GetVel(FGJSBBase::eDown) << " ft/s");
454 SG_LOG(SG_FLIGHT,SG_INFO, " U,V,W= "
455 << Propagate->GetUVW(1) << ", "
456 << Propagate->GetUVW(2) << ", "
457 << Propagate->GetUVW(3) << " ft/s");
460 SG_LOG(SG_FLIGHT,SG_INFO, " Mach: "
461 << Auxiliary->GetMach() );
465 SG_LOG(SG_FLIGHT,SG_INFO, " Indicated Airspeed: "
466 << Auxiliary->GetVcalibratedKTS() << " knots" );
470 stall_warning->setDoubleValue(0);
472 SG_LOG( SG_FLIGHT, SG_INFO, " Bank Angle: "
473 << Propagate->GetEuler(FGJSBBase::ePhi)*RADTODEG << " deg" );
474 SG_LOG( SG_FLIGHT, SG_INFO, " Pitch Angle: "
475 << Propagate->GetEuler(FGJSBBase::eTht)*RADTODEG << " deg" );
476 SG_LOG( SG_FLIGHT, SG_INFO, " True Heading: "
477 << Propagate->GetEuler(FGJSBBase::ePsi)*RADTODEG << " deg" );
478 SG_LOG( SG_FLIGHT, SG_INFO, " Latitude: "
479 << Propagate->GetLocation().GetLatitudeDeg() << " deg" );
480 SG_LOG( SG_FLIGHT, SG_INFO, " Longitude: "
481 << Propagate->GetLocation().GetLongitudeDeg() << " deg" );
482 SG_LOG( SG_FLIGHT, SG_INFO, " Altitude: "
483 << Propagate->GetAltitudeASL() << " feet" );
484 SG_LOG( SG_FLIGHT, SG_INFO, " loaded initial conditions" );
486 SG_LOG( SG_FLIGHT, SG_INFO, " set dt" );
488 SG_LOG( SG_FLIGHT, SG_INFO, "Finished initializing JSBSim" );
490 SG_LOG( SG_FLIGHT, SG_INFO, "FGControls::get_gear_down()= " <<
491 globals->get_controls()->get_gear_down() );
494 /******************************************************************************/
496 void FGJSBsim::unbind()
499 FGInterface::unbind();
502 /******************************************************************************/
504 // Run an iteration of the EOM (equations of motion)
505 void FGJSBsim::update( double dt )
508 if(!fgGetBool("/sim/crashed"))
509 fgSetBool("/sim/crashed", true);
516 int multiloop = _calc_multiloop(dt);
517 FGLocation cart = Auxiliary->GetLocationVRP();
520 update_ground_cache(cart, cart_pos, dt);
524 trimmed->setBoolValue(false);
526 for ( int i=0; i < multiloop; i++ ) {
528 // The property fdm/jsbsim/simulation/terminate has been set to true
529 // by the user. The sim is considered crashed.
533 update_external_forces(fdmex->GetSimTime() + i * fdmex->GetDeltaT());
536 FGJSBBase::Message* msg;
537 while ((msg = fdmex->ProcessNextMessage()) != NULL) {
538 // msg = fdmex->ProcessNextMessage();
540 case FGJSBBase::Message::eText:
541 if (msg->text == "Crash Detected: Simulation FREEZE.")
543 SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text );
545 case FGJSBBase::Message::eBool:
546 SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->bVal );
548 case FGJSBBase::Message::eInteger:
549 SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->iVal );
551 case FGJSBBase::Message::eDouble:
552 SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->dVal );
555 SG_LOG( SG_FLIGHT, SG_INFO, "Unrecognized message type." );
560 // translate JSBsim back to FG structure so that the
561 // autopilot (and the rest of the sim can use the updated values
565 /******************************************************************************/
567 void FGJSBsim::suspend()
570 SGSubsystem::suspend();
573 /******************************************************************************/
575 void FGJSBsim::resume()
578 SGSubsystem::resume();
581 /******************************************************************************/
583 // Convert from the FGInterface struct to the JSBsim generic_ struct
585 bool FGJSBsim::copy_to_JSBsim()
589 // copy control positions into the JSBsim structure
591 FCS->SetDaCmd( globals->get_controls()->get_aileron());
592 FCS->SetRollTrimCmd( globals->get_controls()->get_aileron_trim() );
593 FCS->SetDeCmd( globals->get_controls()->get_elevator());
594 FCS->SetPitchTrimCmd( globals->get_controls()->get_elevator_trim() );
595 FCS->SetDrCmd( -globals->get_controls()->get_rudder() );
596 FCS->SetDsCmd( globals->get_controls()->get_rudder() );
597 FCS->SetYawTrimCmd( -globals->get_controls()->get_rudder_trim() );
598 FCS->SetDfCmd( globals->get_controls()->get_flaps() );
599 FCS->SetDsbCmd( globals->get_controls()->get_speedbrake() );
600 FCS->SetDspCmd( globals->get_controls()->get_spoilers() );
602 // Parking brake sets minimum braking
604 double parking_brake = globals->get_controls()->get_brake_parking();
605 double left_brake = globals->get_controls()->get_brake_left();
606 double right_brake = globals->get_controls()->get_brake_right();
608 if (ab_brake_engaged->getBoolValue()) {
609 left_brake = ab_brake_left_pct->getDoubleValue();
610 right_brake = ab_brake_right_pct->getDoubleValue();
613 FCS->SetLBrake(FMAX(left_brake, parking_brake));
614 FCS->SetRBrake(FMAX(right_brake, parking_brake));
617 FCS->SetCBrake( 0.0 );
618 // FCS->SetCBrake( globals->get_controls()->get_brake(2) );
620 FCS->SetGearCmd( globals->get_controls()->get_gear_down());
621 for (i = 0; i < Propulsion->GetNumEngines(); i++) {
622 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
624 FCS->SetThrottleCmd(i, globals->get_controls()->get_throttle(i));
625 FCS->SetMixtureCmd(i, globals->get_controls()->get_mixture(i));
626 FCS->SetPropAdvanceCmd(i, globals->get_controls()->get_prop_advance(i));
627 FCS->SetFeatherCmd(i, globals->get_controls()->get_feather(i));
629 switch (Propulsion->GetEngine(i)->GetType()) {
630 case FGEngine::etPiston:
631 { // FGPiston code block
632 FGPiston* eng = (FGPiston*)Propulsion->GetEngine(i);
633 eng->SetMagnetos( globals->get_controls()->get_magnetos(i) );
635 } // end FGPiston code block
636 case FGEngine::etTurbine:
637 { // FGTurbine code block
638 FGTurbine* eng = (FGTurbine*)Propulsion->GetEngine(i);
639 eng->SetAugmentation( globals->get_controls()->get_augmentation(i) );
640 eng->SetReverse( globals->get_controls()->get_reverser(i) );
641 //eng->SetInjection( globals->get_controls()->get_water_injection(i) );
642 eng->SetCutoff( globals->get_controls()->get_cutoff(i) );
643 eng->SetIgnition( globals->get_controls()->get_ignition(i) );
645 } // end FGTurbine code block
646 case FGEngine::etRocket:
647 { // FGRocket code block
648 // FGRocket* eng = (FGRocket*)Propulsion->GetEngine(i);
650 } // end FGRocket code block
651 case FGEngine::etTurboprop:
652 { // FGTurboProp code block
653 FGTurboProp* eng = (FGTurboProp*)Propulsion->GetEngine(i);
654 eng->SetReverse( globals->get_controls()->get_reverser(i) );
655 eng->SetCutoff( globals->get_controls()->get_cutoff(i) );
656 eng->SetIgnition( globals->get_controls()->get_ignition(i) );
658 eng->SetGeneratorPower( globals->get_controls()->get_generator_breaker(i) );
659 eng->SetCondition( globals->get_controls()->get_condition(i) );
661 } // end FGTurboProp code block
666 { // FGEngine code block
667 FGEngine* eng = Propulsion->GetEngine(i);
669 eng->SetStarter( globals->get_controls()->get_starter(i) );
670 eng->SetRunning( node->getBoolValue("running") );
671 } // end FGEngine code block
674 Atmosphere->SetTemperature(temperature->getDoubleValue(), get_Altitude(), FGAtmosphere::eCelsius);
675 Atmosphere->SetPressureSL(FGAtmosphere::eInchesHg, pressureSL->getDoubleValue());
677 Winds->SetTurbType((FGWinds::tType)TURBULENCE_TYPE_NAMES[turbulence_model->getStringValue()]);
678 switch( Winds->GetTurbType() ) {
679 case FGWinds::ttStandard:
680 case FGWinds::ttCulp: {
681 double tmp = turbulence_gain->getDoubleValue();
682 Winds->SetTurbGain(tmp * tmp * 100.0);
683 Winds->SetTurbRate(turbulence_rate->getDoubleValue());
686 case FGWinds::ttMilspec:
687 case FGWinds::ttTustin: {
688 // milspec turbulence: 3=light, 4=moderate, 6=severe turbulence
689 // turbulence_gain normalized: 0: none, 1/3: light, 2/3: moderate, 3/3: severe
690 double tmp = turbulence_gain->getDoubleValue();
691 Winds->SetProbabilityOfExceedence(
692 SGMiscd::roundToInt(TurbulenceSeverityTable.GetValue( tmp ) )
694 Winds->SetWindspeed20ft(ground_wind->getDoubleValue());
702 Winds->SetWindNED( -wind_from_north->getDoubleValue(),
703 -wind_from_east->getDoubleValue(),
704 -wind_from_down->getDoubleValue() );
705 // SG_LOG(SG_FLIGHT,SG_INFO, "Wind NED: "
706 // << get_V_north_airmass() << ", "
707 // << get_V_east_airmass() << ", "
708 // << get_V_down_airmass() );
710 for (i = 0; i < Propulsion->GetNumTanks(); i++) {
711 SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
712 FGTank * tank = Propulsion->GetTank(i);
713 double fuelDensity = node->getDoubleValue("density-ppg");
715 if (fuelDensity < 0.1)
716 fuelDensity = 6.0; // Use average fuel value
718 tank->SetDensity(fuelDensity);
719 tank->SetContents(node->getDoubleValue("level-lbs"));
722 Propulsion->SetFuelFreeze((fgGetNode("/sim/freeze/fuel",true))->getBoolValue());
723 fdmex->SetChild(slaved->getBoolValue());
728 /******************************************************************************/
730 // Convert from the JSBsim generic_ struct to the FGInterface struct
732 bool FGJSBsim::copy_from_JSBsim()
736 _set_Inertias( MassBalance->GetMass(),
737 MassBalance->GetIxx(),
738 MassBalance->GetIyy(),
739 MassBalance->GetIzz(),
740 MassBalance->GetIxz() );
742 _set_CG_Position( MassBalance->GetXYZcg(1),
743 MassBalance->GetXYZcg(2),
744 MassBalance->GetXYZcg(3) );
746 _set_Accels_Body( Accelerations->GetBodyAccel(1),
747 Accelerations->GetBodyAccel(2),
748 Accelerations->GetBodyAccel(3) );
750 _set_Accels_CG_Body_N ( Auxiliary->GetNcg(1),
751 Auxiliary->GetNcg(2),
752 Auxiliary->GetNcg(3) );
754 _set_Accels_Pilot_Body( Auxiliary->GetPilotAccel(1),
755 Auxiliary->GetPilotAccel(2),
756 Auxiliary->GetPilotAccel(3) );
758 _set_Nlf( Auxiliary->GetNlf() );
762 _set_Velocities_Local( Propagate->GetVel(FGJSBBase::eNorth),
763 Propagate->GetVel(FGJSBBase::eEast),
764 Propagate->GetVel(FGJSBBase::eDown) );
766 _set_Velocities_Body( Propagate->GetUVW(1),
767 Propagate->GetUVW(2),
768 Propagate->GetUVW(3) );
770 // Make the HUD work ...
771 _set_Velocities_Ground( Propagate->GetVel(FGJSBBase::eNorth),
772 Propagate->GetVel(FGJSBBase::eEast),
773 -Propagate->GetVel(FGJSBBase::eDown) );
775 _set_V_rel_wind( Auxiliary->GetVt() );
777 _set_V_equiv_kts( Auxiliary->GetVequivalentKTS() );
779 _set_V_calibrated_kts( Auxiliary->GetVcalibratedKTS() );
781 _set_V_ground_speed( Auxiliary->GetVground() );
783 _set_Omega_Body( Propagate->GetPQR(FGJSBBase::eP),
784 Propagate->GetPQR(FGJSBBase::eQ),
785 Propagate->GetPQR(FGJSBBase::eR) );
787 _set_Euler_Rates( Auxiliary->GetEulerRates(FGJSBBase::ePhi),
788 Auxiliary->GetEulerRates(FGJSBBase::eTht),
789 Auxiliary->GetEulerRates(FGJSBBase::ePsi) );
791 _set_Mach_number( Auxiliary->GetMach() );
793 // Positions of Visual Reference Point
794 FGLocation l = Auxiliary->GetLocationVRP();
795 _updatePosition(SGGeoc::fromRadFt( l.GetLongitude(), l.GetLatitude(),
798 _set_Altitude_AGL( Propagate->GetDistanceAGL() );
800 double loc_cart[3] = { l(FGJSBBase::eX), l(FGJSBBase::eY), l(FGJSBBase::eZ) };
801 double contact[3], d[3], sd, t;
802 is_valid_m(&t, d, &sd);
803 get_agl_ft(t, loc_cart, SG_METER_TO_FEET*2, contact, d, d, d, &sd);
805 = FGColumnVector3( contact[0], contact[1], contact[2] ).Magnitude();
806 _set_Runway_altitude( rwrad - get_Sea_level_radius() );
809 _set_Euler_Angles( Propagate->GetEuler(FGJSBBase::ePhi),
810 Propagate->GetEuler(FGJSBBase::eTht),
811 Propagate->GetEuler(FGJSBBase::ePsi) );
813 _set_Alpha( Auxiliary->Getalpha() );
814 _set_Beta( Auxiliary->Getbeta() );
817 _set_Gamma_vert_rad( Auxiliary->GetGamma() );
819 _set_Earth_position_angle( Propagate->GetEarthPositionAngle() );
821 _set_Climb_Rate( Propagate->Gethdot() );
823 const FGMatrix33& Tl2b = Propagate->GetTl2b();
824 for ( i = 1; i <= 3; i++ ) {
825 for ( j = 1; j <= 3; j++ ) {
826 _set_T_Local_to_Body( i, j, Tl2b(i,j) );
830 // Copy the engine values from JSBSim.
831 for ( i=0; i < Propulsion->GetNumEngines(); i++ ) {
832 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
833 SGPropertyNode * tnode = node->getChild("thruster", 0, true);
834 FGThruster * thruster = Propulsion->GetEngine(i)->GetThruster();
836 switch (Propulsion->GetEngine(i)->GetType()) {
837 case FGEngine::etPiston:
838 { // FGPiston code block
839 FGPiston* eng = (FGPiston*)Propulsion->GetEngine(i);
840 node->setDoubleValue("egt-degf", eng->getExhaustGasTemp_degF());
841 node->setDoubleValue("oil-temperature-degf", eng->getOilTemp_degF());
842 node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi());
843 node->setDoubleValue("mp-osi", eng->getManifoldPressure_inHg());
844 // NOTE: mp-osi is not in ounces per square inch.
845 // This error is left for reasons of backwards compatibility with
846 // existing FlightGear sound and instrument configurations.
847 node->setDoubleValue("mp-inhg", eng->getManifoldPressure_inHg());
848 node->setDoubleValue("cht-degf", eng->getCylinderHeadTemp_degF());
849 node->setDoubleValue("rpm", eng->getRPM());
850 } // end FGPiston code block
852 case FGEngine::etRocket:
853 { // FGRocket code block
854 // FGRocket* eng = (FGRocket*)Propulsion->GetEngine(i);
855 } // end FGRocket code block
857 case FGEngine::etTurbine:
858 { // FGTurbine code block
859 FGTurbine* eng = (FGTurbine*)Propulsion->GetEngine(i);
860 node->setDoubleValue("n1", eng->GetN1());
861 node->setDoubleValue("n2", eng->GetN2());
862 node->setDoubleValue("egt-degf", 32 + eng->GetEGT()*9/5);
863 node->setBoolValue("augmentation", eng->GetAugmentation());
864 node->setBoolValue("water-injection", eng->GetInjection());
865 node->setBoolValue("ignition", eng->GetIgnition() != 0);
866 node->setDoubleValue("nozzle-pos-norm", eng->GetNozzle());
867 node->setDoubleValue("inlet-pos-norm", eng->GetInlet());
868 node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi());
869 node->setBoolValue("reversed", eng->GetReversed());
870 node->setBoolValue("cutoff", eng->GetCutoff());
871 node->setDoubleValue("epr", eng->GetEPR());
872 globals->get_controls()->set_reverser(i, eng->GetReversed() );
873 globals->get_controls()->set_cutoff(i, eng->GetCutoff() );
874 globals->get_controls()->set_water_injection(i, eng->GetInjection() );
875 globals->get_controls()->set_augmentation(i, eng->GetAugmentation() );
876 } // end FGTurbine code block
878 case FGEngine::etTurboprop:
879 { // FGTurboProp code block
880 FGTurboProp* eng = (FGTurboProp*)Propulsion->GetEngine(i);
881 node->setDoubleValue("n1", eng->GetN1());
882 //node->setDoubleValue("n2", eng->GetN2());
883 node->setDoubleValue("itt_degf", 32 + eng->GetITT()*9/5);
884 node->setBoolValue("ignition", eng->GetIgnition() != 0);
885 node->setDoubleValue("nozzle-pos-norm", eng->GetNozzle());
886 node->setDoubleValue("inlet-pos-norm", eng->GetInlet());
887 node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi());
888 node->setBoolValue("reversed", eng->GetReversed());
889 node->setBoolValue("cutoff", eng->GetCutoff());
890 node->setBoolValue("starting", eng->GetEngStarting());
891 node->setBoolValue("generator-power", eng->GetGeneratorPower());
892 node->setBoolValue("damaged", eng->GetCondition() != 0);
893 node->setBoolValue("ielu-intervent", eng->GetIeluIntervent());
894 node->setDoubleValue("oil-temperature-degf", eng->getOilTemp_degF());
895 // node->setBoolValue("onfire", eng->GetFire());
896 globals->get_controls()->set_reverser(i, eng->GetReversed() );
897 globals->get_controls()->set_cutoff(i, eng->GetCutoff() );
898 } // end FGTurboProp code block
900 case FGEngine::etElectric:
901 { // FGElectric code block
902 FGElectric* eng = (FGElectric*)Propulsion->GetEngine(i);
903 node->setDoubleValue("rpm", eng->getRPM());
904 } // end FGElectric code block
906 case FGEngine::etUnknown:
910 { // FGEngine code block
911 FGEngine* eng = Propulsion->GetEngine(i);
912 node->setDoubleValue("fuel-flow-gph", eng->getFuelFlow_gph());
913 node->setDoubleValue("thrust_lb", thruster->GetThrust());
914 node->setDoubleValue("fuel-flow_pph", eng->getFuelFlow_pph());
915 node->setBoolValue("running", eng->GetRunning());
916 node->setBoolValue("starter", eng->GetStarter());
917 node->setBoolValue("cranking", eng->GetCranking());
918 globals->get_controls()->set_starter(i, eng->GetStarter() );
919 } // end FGEngine code block
921 switch (thruster->GetType()) {
922 case FGThruster::ttNozzle:
923 { // FGNozzle code block
924 // FGNozzle* noz = (FGNozzle*)thruster;
925 } // end FGNozzle code block
927 case FGThruster::ttPropeller:
928 { // FGPropeller code block
929 FGPropeller* prop = (FGPropeller*)thruster;
930 tnode->setDoubleValue("rpm", thruster->GetRPM());
931 tnode->setDoubleValue("pitch", prop->GetPitch());
932 tnode->setDoubleValue("torque", prop->GetTorque());
933 tnode->setBoolValue("feathered", prop->GetFeather());
934 } // end FGPropeller code block
936 case FGThruster::ttRotor:
937 { // FGRotor code block
938 // FGRotor* rotor = (FGRotor*)thruster;
939 } // end FGRotor code block
941 case FGThruster::ttDirect:
942 { // Direct code block
943 } // end Direct code block
949 // Copy the fuel levels from JSBSim if fuel
950 // freeze not enabled.
951 if ( ! Propulsion->GetFuelFreeze() ) {
952 for (i = 0; i < Propulsion->GetNumTanks(); i++) {
953 SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
954 FGTank* tank = Propulsion->GetTank(i);
955 double contents = tank->GetContents();
956 double temp = tank->GetTemperature_degC();
957 double fuelDensity = tank->GetDensity();
959 if (fuelDensity < 0.1)
960 fuelDensity = 6.0; // Use average fuel value
962 node->setDoubleValue("density-ppg" , fuelDensity);
963 node->setDoubleValue("level-lbs", contents);
964 if (temp != -9999.0) node->setDoubleValue("temperature_degC", temp);
966 node->setDoubleValue("arm-in", tank->GetXYZ(FGJSBBase::eX ) );
972 stall_warning->setDoubleValue( Aerodynamics->GetStallWarn() );
974 elevator_pos_pct->setDoubleValue( FCS->GetDePos(ofNorm) );
975 left_aileron_pos_pct->setDoubleValue( FCS->GetDaLPos(ofNorm) );
976 right_aileron_pos_pct->setDoubleValue( FCS->GetDaRPos(ofNorm) );
977 rudder_pos_pct->setDoubleValue( -1*FCS->GetDrPos(ofNorm) );
978 flap_pos_pct->setDoubleValue( FCS->GetDfPos(ofNorm) );
979 speedbrake_pos_pct->setDoubleValue( FCS->GetDsbPos(ofNorm) );
980 spoilers_pos_pct->setDoubleValue( FCS->GetDspPos(ofNorm) );
981 tailhook_pos_pct->setDoubleValue( FCS->GetTailhookPos() );
982 wing_fold_pos_pct->setDoubleValue( FCS->GetWingFoldPos() );
984 // force a sim crashed if crashed (altitude AGL < 0)
985 if (get_Altitude_AGL() < -100.0) {
986 fdmex->SuspendIntegration();
994 bool FGJSBsim::ToggleDataLogging(void)
996 // ToDo: handle this properly
997 fdmex->DisableOutput();
1002 bool FGJSBsim::ToggleDataLogging(bool state)
1005 fdmex->EnableOutput();
1008 fdmex->DisableOutput();
1015 void FGJSBsim::set_Latitude(double lat)
1017 double alt = altitude->getDoubleValue();
1018 double sea_level_radius_meters, lat_geoc;
1020 if ( alt < -9990 ) alt = 0.0;
1022 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat );
1023 SG_LOG(SG_FLIGHT,SG_INFO," cur alt (ft) = " << alt );
1025 sgGeodToGeoc( lat, alt * SG_FEET_TO_METER,
1026 &sea_level_radius_meters, &lat_geoc );
1028 double sea_level_radius_ft = sea_level_radius_meters * SG_METER_TO_FEET;
1029 _set_Sea_level_radius( sea_level_radius_ft );
1032 fgic->SetLatitudeRadIC( lat_geoc );
1034 Propagate->SetLatitude(lat_geoc);
1036 FGInterface::set_Latitude(lat);
1040 void FGJSBsim::set_Longitude(double lon)
1042 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon );
1045 fgic->SetLongitudeRadIC(lon);
1047 Propagate->SetLongitude(lon);
1049 FGInterface::set_Longitude(lon);
1052 // Sets the altitude above sea level.
1053 void FGJSBsim::set_Altitude(double alt)
1055 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt );
1058 FGLocation position = fgic->GetPosition();
1060 position.SetPositionGeodetic(0.0, position.GetGeodLatitudeRad(), alt);
1061 fgic->SetAltitudeASLFtIC(position.GetAltitudeASL());
1062 fgic->SetLatitudeRadIC(position.GetLatitude());
1065 Propagate->SetAltitudeASL(alt);
1067 FGInterface::set_Altitude(alt);
1070 void FGJSBsim::set_V_calibrated_kts(double vc)
1072 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " << vc );
1075 fgic->SetVcalibratedKtsIC(vc);
1077 double p=pressure->getDoubleValue();
1078 double psl=fdmex->GetAtmosphere()->GetPressureSL();
1079 double rhosl=fdmex->GetAtmosphere()->GetDensitySL();
1080 double mach = FGJSBBase::MachFromVcalibrated(vc, p, psl, rhosl);
1081 double temp = 1.8*(temperature->getDoubleValue()+273.15);
1082 double soundSpeed = sqrt(1.4*1716.0*temp);
1083 FGColumnVector3 vUVW = Propagate->GetUVW();
1085 vUVW *= mach * soundSpeed;
1086 Propagate->SetUVW(1, vUVW(1));
1087 Propagate->SetUVW(2, vUVW(2));
1088 Propagate->SetUVW(3, vUVW(3));
1091 FGInterface::set_V_calibrated_kts(vc);
1094 void FGJSBsim::set_Mach_number(double mach)
1096 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " << mach );
1099 fgic->SetMachIC(mach);
1101 double temp = 1.8*(temperature->getDoubleValue()+273.15);
1102 double soundSpeed = sqrt(1.4*1716.0*temp);
1103 FGColumnVector3 vUVW = Propagate->GetUVW();
1105 vUVW *= mach * soundSpeed;
1106 Propagate->SetUVW(1, vUVW(1));
1107 Propagate->SetUVW(2, vUVW(2));
1108 Propagate->SetUVW(3, vUVW(3));
1111 FGInterface::set_Mach_number(mach);
1114 void FGJSBsim::set_Velocities_Local( double north, double east, double down )
1116 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: "
1117 << north << ", " << east << ", " << down );
1120 fgic->SetVNorthFpsIC(north);
1121 fgic->SetVEastFpsIC(east);
1122 fgic->SetVDownFpsIC(down);
1125 FGColumnVector3 vNED(north, east, down);
1126 FGColumnVector3 vUVW = Propagate->GetTl2b() * vNED;
1127 Propagate->SetUVW(1, vUVW(1));
1128 Propagate->SetUVW(2, vUVW(2));
1129 Propagate->SetUVW(3, vUVW(3));
1132 FGInterface::set_Velocities_Local(north, east, down);
1135 void FGJSBsim::set_Velocities_Body( double u, double v, double w)
1137 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Body: "
1138 << u << ", " << v << ", " << w );
1141 fgic->SetUBodyFpsIC(u);
1142 fgic->SetVBodyFpsIC(v);
1143 fgic->SetWBodyFpsIC(w);
1146 Propagate->SetUVW(1, u);
1147 Propagate->SetUVW(2, v);
1148 Propagate->SetUVW(3, w);
1151 FGInterface::set_Velocities_Body(u, v, w);
1155 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi )
1157 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: "
1158 << phi << ", " << theta << ", " << psi );
1161 fgic->SetThetaRadIC(theta);
1162 fgic->SetPhiRadIC(phi);
1163 fgic->SetPsiRadIC(psi);
1166 FGQuaternion quat(phi, theta, psi);
1167 FGMatrix33 Tl2b = quat.GetT();
1168 FGMatrix33 Ti2b = Tl2b*Propagate->GetTi2l();
1169 FGQuaternion Qi = Ti2b.GetQuaternion();
1170 Propagate->SetInertialOrientation(Qi);
1173 FGInterface::set_Euler_Angles(phi, theta, psi);
1177 void FGJSBsim::set_Climb_Rate( double roc)
1179 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
1181 //since both climb rate and flight path angle are set in the FG
1182 //startup sequence, something is needed to keep one from cancelling
1184 if( fabs(roc) > 1 && fabs(fgic->GetFlightPathAngleRadIC()) < 0.01 ) {
1186 fgic->SetClimbRateFpsIC(roc);
1188 FGColumnVector3 vNED = Propagate->GetVel();
1189 vNED(FGJSBBase::eDown) = -roc;
1190 FGColumnVector3 vUVW = Propagate->GetTl2b() * vNED;
1191 Propagate->SetUVW(1, vUVW(1));
1192 Propagate->SetUVW(2, vUVW(2));
1193 Propagate->SetUVW(3, vUVW(3));
1196 FGInterface::set_Climb_Rate(roc);
1200 void FGJSBsim::set_Gamma_vert_rad( double gamma)
1202 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
1204 if( !(fabs(gamma) < 0.01 && fabs(fgic->GetClimbRateFpsIC()) > 1) ) {
1206 fgic->SetFlightPathAngleRadIC(gamma);
1208 FGColumnVector3 vNED = Propagate->GetVel();
1209 double vt = vNED.Magnitude();
1210 vNED(FGJSBBase::eDown) = -vt * sin(gamma);
1211 FGColumnVector3 vUVW = Propagate->GetTl2b() * vNED;
1212 Propagate->SetUVW(1, vUVW(1));
1213 Propagate->SetUVW(2, vUVW(2));
1214 Propagate->SetUVW(3, vUVW(3));
1217 FGInterface::set_Gamma_vert_rad(gamma);
1221 void FGJSBsim::init_gear(void )
1223 FGGroundReactions* gr=fdmex->GetGroundReactions();
1224 int Ngear=GroundReactions->GetNumGearUnits();
1225 for (int i=0;i<Ngear;i++) {
1226 FGLGear *gear = gr->GetGearUnit(i);
1227 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
1228 node->setDoubleValue("xoffset-in", gear->GetBodyLocation()(1) * 12);
1229 node->setDoubleValue("yoffset-in", gear->GetBodyLocation()(2) * 12);
1230 node->setDoubleValue("zoffset-in", gear->GetBodyLocation()(3) * 12);
1231 node->setBoolValue("wow", gear->GetWOW());
1232 node->setDoubleValue("rollspeed-ms", gear->GetWheelRollVel()*0.3043);
1233 node->setBoolValue("has-brake", gear->GetBrakeGroup() > 0);
1234 node->setDoubleValue("position-norm", gear->GetGearUnitPos());
1235 // node->setDoubleValue("tire-pressure-norm", gear->GetTirePressure());
1236 node->setDoubleValue("compression-norm", gear->GetCompLen());
1237 node->setDoubleValue("compression-ft", gear->GetCompLen());
1238 if ( gear->GetSteerable() )
1239 node->setDoubleValue("steering-norm", gear->GetSteerNorm());
1243 void FGJSBsim::update_gear(void)
1245 FGGroundReactions* gr=fdmex->GetGroundReactions();
1246 int Ngear=GroundReactions->GetNumGearUnits();
1247 for (int i=0;i<Ngear;i++) {
1248 FGLGear *gear = gr->GetGearUnit(i);
1249 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
1250 node->getChild("wow", 0, true)->setBoolValue( gear->GetWOW());
1251 node->getChild("rollspeed-ms", 0, true)->setDoubleValue(gear->GetWheelRollVel()*0.3043);
1252 node->getChild("position-norm", 0, true)->setDoubleValue(gear->GetGearUnitPos());
1253 // gear->SetTirePressure(node->getDoubleValue("tire-pressure-norm"));
1254 node->setDoubleValue("compression-norm", gear->GetCompLen());
1255 node->setDoubleValue("compression-ft", gear->GetCompLen());
1256 if ( gear->GetSteerable() )
1257 node->setDoubleValue("steering-norm", gear->GetSteerNorm());
1261 void FGJSBsim::do_trim(void)
1265 if ( fgGetBool("/sim/presets/onground") )
1267 fgtrim = new FGTrim(fdmex,tGround);
1269 fgtrim = new FGTrim(fdmex,tFull);
1272 if ( !fgtrim->DoTrim() ) {
1274 fgtrim->TrimStats();
1276 trimmed->setBoolValue(true);
1280 pitch_trim->setDoubleValue( FCS->GetPitchTrimCmd() );
1281 throttle_trim->setDoubleValue( FCS->GetThrottleCmd(0) );
1282 aileron_trim->setDoubleValue( FCS->GetDaCmd() );
1283 rudder_trim->setDoubleValue( -FCS->GetDrCmd() );
1285 globals->get_controls()->set_elevator_trim(FCS->GetPitchTrimCmd());
1286 globals->get_controls()->set_elevator(FCS->GetDeCmd());
1287 for( unsigned i = 0; i < Propulsion->GetNumEngines(); i++ )
1288 globals->get_controls()->set_throttle(i, FCS->GetThrottleCmd(i));
1290 globals->get_controls()->set_aileron(FCS->GetDaCmd());
1291 globals->get_controls()->set_rudder( -FCS->GetDrCmd());
1293 SG_LOG( SG_FLIGHT, SG_INFO, " Trim complete" );
1296 bool FGJSBsim::update_ground_cache(const FGLocation& cart, double* cart_pos,
1299 // Compute the radius of the aircraft. That is the radius of a ball
1300 // where all gear units are in. At the moment it is at least 10ft ...
1301 double acrad = 10.0;
1302 int n_gears = GroundReactions->GetNumGearUnits();
1303 for (int i=0; i<n_gears; ++i) {
1304 FGColumnVector3 bl = GroundReactions->GetGearUnit(i)->GetBodyLocation();
1305 double r = bl.Magnitude();
1310 // Compute the potential movement of this aircraft and query for the
1311 // ground in this area.
1312 double groundCacheRadius = acrad + 2*dt*Propagate->GetUVW().Magnitude();
1314 cart_pos[0] = cart(1);
1315 cart_pos[1] = cart(2);
1316 cart_pos[2] = cart(3);
1317 double t0 = fdmex->GetSimTime();
1318 bool cache_ok = prepare_ground_cache_ft( t0, t0 + dt, cart_pos,
1319 groundCacheRadius );
1321 SG_LOG(SG_FLIGHT, SG_WARN,
1322 "FGInterface is being called without scenery below the aircraft!");
1324 SG_LOG(SG_FLIGHT, SG_WARN, "altitude = "
1325 << fgic->GetAltitudeASLFtIC());
1327 SG_LOG(SG_FLIGHT, SG_WARN, "sea level radius = "
1328 << get_Sea_level_radius());
1330 SG_LOG(SG_FLIGHT, SG_WARN, "latitude = "
1331 << fgic->GetLatitudeRadIC());
1333 SG_LOG(SG_FLIGHT, SG_WARN, "longitude = "
1334 << fgic->GetLongitudeRadIC());
1341 FGJSBsim::get_agl_ft(double t, const double pt[3], double alt_off,
1342 double contact[3], double normal[3], double vel[3],
1343 double angularVel[3], double *agl)
1345 const simgear::BVHMaterial* material;
1346 simgear::BVHNode::Id id;
1348 // don't check the return value and continue above scenery discontinuity
1349 // see http://osdir.com/ml/flightgear-sim/2014-04/msg00145.html
1350 FGInterface::get_agl_ft(t, pt, alt_off, contact, normal, vel,
1351 angularVel, material, id);
1353 SGGeod geodPt = SGGeod::fromCart(SG_FEET_TO_METER*SGVec3d(pt));
1354 SGQuatd hlToEc = SGQuatd::fromLonLat(geodPt);
1355 *agl = dot(hlToEc.rotate(SGVec3d(0, 0, 1)), SGVec3d(contact) - SGVec3d(pt));
1357 #ifdef JSBSIM_USE_GROUNDREACTIONS
1358 bool terrain_active = (terrain->getIntValue("override-level", -1) > 0) ? false : true;
1359 terrain->setBoolValue("active", terrain_active);
1360 terrain->setBoolValue("valid", (material && terrain_active) ? true : false);
1363 static bool material_valid = false;
1365 GroundReactions->SetStaticFFactor((*material).get_friction_factor());
1366 GroundReactions->SetRollingFFactor((*material).get_rolling_friction()/0.02);
1367 // 1 Pascal = 0.00014503773800721815 lbs/in^2
1368 double pressure = (*material).get_load_resistance(); // N/m^2 (or Pascal)
1369 GroundReactions->SetMaximumForce(pressure*0.00014503773800721815);
1371 GroundReactions->SetBumpiness((*material).get_bumpiness());
1372 GroundReactions->SetSolid((*material).get_solid());
1373 GroundReactions->SetPosition(pt);
1374 material_valid = true;
1376 if (material_valid) {
1377 GroundReactions->resetValues();
1378 material_valid = false;
1383 terrain->setBoolValue("valid", false);
1388 inline static double sqr(double x)
1393 static double angle_diff(double a, double b)
1395 double diff = fabs(a - b);
1396 if (diff > 180) diff = 360 - diff;
1401 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)
1403 FGColumnVector3 tip(-hook_length * cos_fi_guess, 0, hook_length * sin_fi_guess);
1404 double dist = DotProduct(tip, ground_normal_body);
1405 if (fabs(dist + E) < 0.0001) {
1406 sin_fis[*points] = sin_fi_guess;
1407 cos_fis[*points] = cos_fi_guess;
1408 fis[*points] = atan2(sin_fi_guess, cos_fi_guess) * SG_RADIANS_TO_DEGREES;
1414 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)
1416 if (sin_fi_guess >= -1 && sin_fi_guess <= 1) {
1417 double cos_fi_guess = sqrt(1 - sqr(sin_fi_guess));
1418 check_hook_solution(ground_normal_body, E, hook_length, sin_fi_guess, cos_fi_guess, sin_fis, cos_fis, fis, points);
1419 if (fabs(cos_fi_guess) > SG_EPSILON) {
1420 check_hook_solution(ground_normal_body, E, hook_length, sin_fi_guess, -cos_fi_guess, sin_fis, cos_fis, fis, points);
1425 void FGJSBsim::update_external_forces(double t_off)
1427 const FGMatrix33& Tb2l = Propagate->GetTb2l();
1428 const FGMatrix33& Tl2b = Propagate->GetTl2b();
1429 const FGLocation& Location = Propagate->GetLocation();
1430 const FGMatrix33& Tec2l = Location.GetTec2l();
1432 double hook_area[4][3];
1434 FGColumnVector3 hook_root_body = MassBalance->StructuralToBody(hook_root_struct);
1435 FGColumnVector3 hook_root = Location.LocalToLocation(Tb2l * hook_root_body);
1436 hook_area[1][0] = hook_root(1);
1437 hook_area[1][1] = hook_root(2);
1438 hook_area[1][2] = hook_root(3);
1440 hook_length = fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-length-ft", 6.75);
1441 double fi_min = fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-pos-min-deg", -18);
1442 double fi_max = fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-pos-max-deg", 30);
1443 double fi = fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-pos-norm") * (fi_max - fi_min) + fi_min;
1444 double cos_fi = cos(fi * SG_DEGREES_TO_RADIANS);
1445 double sin_fi = sin(fi * SG_DEGREES_TO_RADIANS);
1447 FGColumnVector3 hook_tip_body = hook_root_body;
1448 hook_tip_body(1) -= hook_length * cos_fi;
1449 hook_tip_body(3) += hook_length * sin_fi;
1452 double ground_normal[3];
1453 double ground_vel[3];
1454 double ground_angular_vel[3];
1458 bool got = get_agl_ft(t_off, hook_area[1], 0, contact, ground_normal,
1459 ground_vel, ground_angular_vel, &root_agl_ft);
1460 if (got && root_agl_ft > 0 && root_agl_ft < hook_length) {
1461 FGColumnVector3 ground_normal_body = Tl2b * (Tec2l * FGColumnVector3(ground_normal[0], ground_normal[1], ground_normal[2]));
1462 FGColumnVector3 contact_body = Tl2b * Location.LocationToLocal(FGColumnVector3(contact[0], contact[1], contact[2]));
1463 double D = -DotProduct(contact_body, ground_normal_body);
1465 // check hook tip agl against same ground plane
1466 double hook_tip_agl_ft = DotProduct(hook_tip_body, ground_normal_body) + D;
1467 if (hook_tip_agl_ft < 0) {
1469 // hook tip: hx - l cos, hy, hz + l sin
1470 // on ground: - n0 l cos + n2 l sin + E = 0
1472 double E = D + DotProduct(hook_root_body, ground_normal_body);
1474 // substitue x = sin fi, cos fi = sqrt(1 - x * x)
1475 // and rearrange to get a quadratic with coeffs:
1476 double a = sqr(hook_length) * (sqr(ground_normal_body(1)) + sqr(ground_normal_body(3)));
1477 double b = 2 * E * ground_normal_body(3) * hook_length;
1478 double c = sqr(E) - sqr(ground_normal_body(1) * hook_length);
1480 double disc = sqr(b) - 4 * a * c;
1482 double delta = sqrt(disc) / (2 * a);
1484 // allow 4 solutions for safety, should never happen
1490 double sin_fi_guess = -b / (2 * a) - delta;
1491 check_hook_solution(ground_normal_body, E, hook_length, sin_fi_guess, sin_fis, cos_fis, fis, &points);
1492 check_hook_solution(ground_normal_body, E, hook_length, sin_fi_guess + 2 * delta, sin_fis, cos_fis, fis, &points);
1495 double diff1 = angle_diff(fi, fis[0]);
1496 double diff2 = angle_diff(fi, fis[1]);
1497 int point = diff1 < diff2 ? 0 : 1;
1499 sin_fi = sin_fis[point];
1500 cos_fi = cos_fis[point];
1501 hook_tip_body(1) = hook_root_body(1) - hook_length * cos_fi;
1502 hook_tip_body(3) = hook_root_body(3) + hook_length * sin_fi;
1508 FGColumnVector3 hook_root_vel = Propagate->GetVel() + (Tb2l * (Propagate->GetPQR() * hook_root_body));
1509 double wire_ends_ec[2][3];
1510 double wire_vel_ec[2][3];
1511 get_wire_ends_ft(t_off, wire_ends_ec, wire_vel_ec);
1512 FGColumnVector3 wire_vel_1 = Tec2l * FGColumnVector3(wire_vel_ec[0][0], wire_vel_ec[0][1], wire_vel_ec[0][2]);
1513 FGColumnVector3 wire_vel_2 = Tec2l * FGColumnVector3(wire_vel_ec[1][0], wire_vel_ec[1][1], wire_vel_ec[1][2]);
1514 FGColumnVector3 rel_vel = hook_root_vel - (wire_vel_1 + wire_vel_2) / 2;
1515 if (rel_vel.Magnitude() < 3) {
1518 fgSetDouble("/fdm/jsbsim/external_reactions/hook/magnitude", 0.0);
1520 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;
1521 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;
1522 FGColumnVector3 force_plane_normal = wire_end1_body * wire_end2_body;
1523 force_plane_normal.Normalize();
1524 cos_fi = DotProduct(force_plane_normal, FGColumnVector3(0, 0, 1));
1525 if (cos_fi < 0) cos_fi = -cos_fi;
1526 sin_fi = sqrt(1 - sqr(cos_fi));
1527 fi = atan2(sin_fi, cos_fi) * SG_RADIANS_TO_DEGREES;
1529 fgSetDouble("/fdm/jsbsim/external_reactions/hook/x", -cos_fi);
1530 fgSetDouble("/fdm/jsbsim/external_reactions/hook/y", 0);
1531 fgSetDouble("/fdm/jsbsim/external_reactions/hook/z", sin_fi);
1532 fgSetDouble("/fdm/jsbsim/external_reactions/hook/magnitude", fgGetDouble("/fdm/jsbsim/systems/hook/force"));
1536 FGColumnVector3 hook_tip = Location.LocalToLocation(Tb2l * hook_tip_body);
1538 hook_area[0][0] = hook_tip(1);
1539 hook_area[0][1] = hook_tip(2);
1540 hook_area[0][2] = hook_tip(3);
1543 // The previous positions.
1544 hook_area[2][0] = last_hook_root[0];
1545 hook_area[2][1] = last_hook_root[1];
1546 hook_area[2][2] = last_hook_root[2];
1547 hook_area[3][0] = last_hook_tip[0];
1548 hook_area[3][1] = last_hook_tip[1];
1549 hook_area[3][2] = last_hook_tip[2];
1551 // Check if we caught a wire.
1552 // Returns true if we caught one.
1553 if (caught_wire_ft(t_off, hook_area)) {
1558 // save actual position as old position ...
1559 last_hook_tip[0] = hook_area[0][0];
1560 last_hook_tip[1] = hook_area[0][1];
1561 last_hook_tip[2] = hook_area[0][2];
1562 last_hook_root[0] = hook_area[1][0];
1563 last_hook_root[1] = hook_area[1][1];
1564 last_hook_root[2] = hook_area[1][2];
1566 fgSetDouble("/fdm/jsbsim/systems/hook/tailhook-pos-deg", fi);