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: JSBSim.cxx,v 1.64 2010/10/31 04:49:25 jberndt Exp $
28 #include <simgear/compiler.h>
29 #include <simgear/sg_inlines.h>
31 #include <stdio.h> // 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>
40 #include <FDM/flight.hxx>
42 #include <Aircraft/controls.hxx>
43 #include <Main/globals.hxx>
44 #include <Main/fg_props.hxx>
47 #include <FDM/JSBSim/FGFDMExec.h>
48 #include <FDM/JSBSim/FGJSBBase.h>
49 #include <FDM/JSBSim/initialization/FGInitialCondition.h>
50 #include <FDM/JSBSim/initialization/FGTrim.h>
51 #include <FDM/JSBSim/models/FGModel.h>
52 #include <FDM/JSBSim/models/FGAircraft.h>
53 #include <FDM/JSBSim/models/FGFCS.h>
54 #include <FDM/JSBSim/models/FGPropagate.h>
55 #include <FDM/JSBSim/models/FGAuxiliary.h>
56 #include <FDM/JSBSim/models/FGInertial.h>
57 #include <FDM/JSBSim/models/FGAtmosphere.h>
58 #include <FDM/JSBSim/models/FGMassBalance.h>
59 #include <FDM/JSBSim/models/FGAerodynamics.h>
60 #include <FDM/JSBSim/models/FGLGear.h>
61 #include <FDM/JSBSim/models/FGGroundReactions.h>
62 #include <FDM/JSBSim/models/FGPropulsion.h>
63 #include <FDM/JSBSim/models/FGAccelerations.h>
64 #include <FDM/JSBSim/models/atmosphere/FGWinds.h>
65 #include <FDM/JSBSim/models/propulsion/FGEngine.h>
66 #include <FDM/JSBSim/models/propulsion/FGPiston.h>
67 #include <FDM/JSBSim/models/propulsion/FGTurbine.h>
68 #include <FDM/JSBSim/models/propulsion/FGTurboProp.h>
69 #include <FDM/JSBSim/models/propulsion/FGRocket.h>
70 #include <FDM/JSBSim/models/propulsion/FGElectric.h>
71 #include <FDM/JSBSim/models/propulsion/FGNozzle.h>
72 #include <FDM/JSBSim/models/propulsion/FGPropeller.h>
73 #include <FDM/JSBSim/models/propulsion/FGRotor.h>
74 #include <FDM/JSBSim/models/propulsion/FGTank.h>
75 #include <FDM/JSBSim/input_output/FGPropertyManager.h>
76 #include <FDM/JSBSim/input_output/FGGroundCallback.h>
78 using namespace JSBSim;
81 FMAX (double a, double b)
86 class FGFSGroundCallback : public FGGroundCallback {
88 FGFSGroundCallback(FGJSBsim* ifc) : mInterface(ifc) {}
89 virtual ~FGFSGroundCallback() {}
91 /** Get the altitude above sea level dependent on the location. */
92 virtual double GetAltitude(const FGLocation& l) const {
93 double pt[3] = { SG_FEET_TO_METER*l(eX),
94 SG_FEET_TO_METER*l(eY),
95 SG_FEET_TO_METER*l(eZ) };
97 sgCartToGeod( pt, &lat, &lon, &alt);
98 return alt * SG_METER_TO_FEET;
101 /** Compute the altitude above ground. */
102 virtual double GetAGLevel(double t, const FGLocation& l,
103 FGLocation& cont, FGColumnVector3& n,
104 FGColumnVector3& v, FGColumnVector3& w) const {
105 double loc_cart[3] = { l(eX), l(eY), l(eZ) };
106 double contact[3], normal[3], vel[3], angularVel[3], agl = 0;
107 mInterface->get_agl_ft(t, loc_cart, SG_METER_TO_FEET*2, contact, normal,
108 vel, angularVel, &agl);
109 n = FGColumnVector3( normal[0], normal[1], normal[2] );
110 v = FGColumnVector3( vel[0], vel[1], vel[2] );
111 w = FGColumnVector3( angularVel[0], angularVel[1], angularVel[2] );
112 cont = FGColumnVector3( contact[0], contact[1], contact[2] );
116 virtual double GetTerrainGeoCentRadius(double t, const FGLocation& l) const {
117 double loc_cart[3] = { l(eX), l(eY), l(eZ) };
118 double contact[3], normal[3], vel[3], angularVel[3], agl = 0;
119 mInterface->get_agl_ft(t, loc_cart, SG_METER_TO_FEET*2, contact, normal,
120 vel, angularVel, &agl);
121 return sqrt(contact[0]*contact[0]+contact[1]*contact[1]+contact[2]*contact[2]);
124 virtual double GetSeaLevelRadius(const FGLocation& l) const {
125 double seaLevelRadius, latGeoc;
127 sgGeodToGeoc(l.GetGeodLatitudeRad(), l.GetGeodAltitude(),
128 &seaLevelRadius, &latGeoc);
130 return seaLevelRadius * SG_METER_TO_FEET;
133 virtual void SetTerrainGeoCentRadius(double radius) {}
134 virtual void SetSeaLevelRadius(double radius) {}
136 FGJSBsim* mInterface;
139 // FG uses a squared normalized magnitude for turbulence
140 // this lookup table maps fg's severity levels
141 // none(0), light(1/3), moderate(2/3) and severe(3/3)
142 // to the POE table indexes 0, 3, 4 and 7
143 class FGTurbulenceSeverityTable : public FGTable {
145 FGTurbulenceSeverityTable() : FGTable(4) {
146 *this << (0.0/9.0) << 0.0;
147 *this << (1.0/9.0) << 3.0;
148 *this << (4.0/9.0) << 4.0;
149 *this << (9.0/9.0) << 7.0;
153 /******************************************************************************/
154 std::map<std::string,int> FGJSBsim::TURBULENCE_TYPE_NAMES;
156 static FGTurbulenceSeverityTable TurbulenceSeverityTable;
158 FGJSBsim::FGJSBsim( double dt )
159 : FGInterface(dt), got_wire(false)
162 if( TURBULENCE_TYPE_NAMES.empty() ) {
163 TURBULENCE_TYPE_NAMES["ttNone"] = FGWinds::ttNone;
164 TURBULENCE_TYPE_NAMES["ttStandard"] = FGWinds::ttStandard;
165 TURBULENCE_TYPE_NAMES["ttCulp"] = FGWinds::ttCulp;
166 TURBULENCE_TYPE_NAMES["ttMilspec"] = FGWinds::ttMilspec;
167 TURBULENCE_TYPE_NAMES["ttTustin"] = FGWinds::ttTustin;
170 // Set up the debugging level
171 // FIXME: this will not respond to
174 // if flight is excluded, don't bother
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 = 0x0f;
186 FGJSBBase::debug_lvl = 0x01;
190 FGJSBBase::debug_lvl = 0x00;
195 fdmex = new FGFDMExec( (FGPropertyManager*)globals->get_props() );
197 // Register ground callback.
198 fdmex->SetGroundCallback( new FGFSGroundCallback(this) );
200 Atmosphere = fdmex->GetAtmosphere();
201 Winds = fdmex->GetWinds();
202 FCS = fdmex->GetFCS();
203 MassBalance = fdmex->GetMassBalance();
204 Propulsion = fdmex->GetPropulsion();
205 Aircraft = fdmex->GetAircraft();
206 Propagate = fdmex->GetPropagate();
207 Auxiliary = fdmex->GetAuxiliary();
208 Inertial = fdmex->GetInertial();
209 Aerodynamics = fdmex->GetAerodynamics();
210 GroundReactions = fdmex->GetGroundReactions();
211 Accelerations = fdmex->GetAccelerations();
216 SGPath aircraft_path( fgGetString("/sim/aircraft-dir") );
218 SGPath engine_path( fgGetString("/sim/aircraft-dir") );
219 engine_path.append( "Engine" );
221 SGPath systems_path( fgGetString("/sim/aircraft-dir") );
222 systems_path.append( "Systems" );
224 // deprecate sim-time-sec for simulation/sim-time-sec
225 // remove alias with increased configuration file version number (2.1 or later)
226 SGPropertyNode * node = fgGetNode("/fdm/jsbsim/simulation/sim-time-sec");
227 fgGetNode("/fdm/jsbsim/sim-time-sec", true)->alias( node );
228 // end of sim-time-sec deprecation patch
232 result = fdmex->LoadModel( aircraft_path.str(),
235 fgGetString("/sim/aero"), false );
238 SG_LOG( SG_FLIGHT, SG_INFO, " loaded aero.");
240 SG_LOG( SG_FLIGHT, SG_INFO,
241 " aero does not exist (you may have mis-typed the name).");
245 SG_LOG( SG_FLIGHT, SG_INFO, "" );
246 SG_LOG( SG_FLIGHT, SG_INFO, "" );
247 SG_LOG( SG_FLIGHT, SG_INFO, "After loading aero definition file ..." );
249 int Neng = Propulsion->GetNumEngines();
250 SG_LOG( SG_FLIGHT, SG_INFO, "num engines = " << Neng );
252 if ( GroundReactions->GetNumGearUnits() <= 0 ) {
253 SG_LOG( SG_FLIGHT, SG_ALERT, "num gear units = "
254 << GroundReactions->GetNumGearUnits() );
255 SG_LOG( SG_FLIGHT, SG_ALERT, "This is a very bad thing because with 0 gear units, the ground trimming");
256 SG_LOG( SG_FLIGHT, SG_ALERT, "routine (coming up later in the code) will core dump.");
257 SG_LOG( SG_FLIGHT, SG_ALERT, "Halting the sim now, and hoping a solution will present itself soon!");
263 // Set initial fuel levels if provided.
264 for (unsigned int i = 0; i < Propulsion->GetNumTanks(); i++) {
266 SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
267 FGTank* tank = Propulsion->GetTank(i);
269 d = node->getNode( "density-ppg", true )->getDoubleValue();
271 tank->SetDensity( d );
273 node->getNode( "density-ppg", true )->setDoubleValue( SG_MAX2<double>(tank->GetDensity(), 0.1) );
276 d = node->getNode( "level-lbs", true )->getDoubleValue();
278 tank->SetContents( d );
280 node->getNode( "level-lbs", true )->setDoubleValue( tank->GetContents() );
282 /* Capacity is read-only in FGTank and can't be overwritten from FlightGear */
283 node->getNode("capacity-gal_us", true )->setDoubleValue( tank->GetCapacityGallons() );
285 Propulsion->SetFuelFreeze((fgGetNode("/sim/freeze/fuel",true))->getBoolValue());
287 fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
288 fgSetDouble("/fdm/trim/throttle", FCS->GetThrottleCmd(0));
289 fgSetDouble("/fdm/trim/aileron", FCS->GetDaCmd());
290 fgSetDouble("/fdm/trim/rudder", FCS->GetDrCmd());
292 startup_trim = fgGetNode("/sim/presets/trim", true);
294 trimmed = fgGetNode("/fdm/trim/trimmed", true);
295 trimmed->setBoolValue(false);
297 pitch_trim = fgGetNode("/fdm/trim/pitch-trim", true );
298 throttle_trim = fgGetNode("/fdm/trim/throttle", true );
299 aileron_trim = fgGetNode("/fdm/trim/aileron", true );
300 rudder_trim = fgGetNode("/fdm/trim/rudder", true );
302 stall_warning = fgGetNode("/sim/alarms/stall-warning",true);
303 stall_warning->setDoubleValue(0);
306 flap_pos_pct=fgGetNode("/surface-positions/flap-pos-norm",true);
307 elevator_pos_pct=fgGetNode("/surface-positions/elevator-pos-norm",true);
309 =fgGetNode("/surface-positions/left-aileron-pos-norm",true);
310 right_aileron_pos_pct
311 =fgGetNode("/surface-positions/right-aileron-pos-norm",true);
312 rudder_pos_pct=fgGetNode("/surface-positions/rudder-pos-norm",true);
314 =fgGetNode("/surface-positions/speedbrake-pos-norm",true);
315 spoilers_pos_pct=fgGetNode("/surface-positions/spoilers-pos-norm",true);
316 tailhook_pos_pct=fgGetNode("/gear/tailhook/position-norm",true);
317 wing_fold_pos_pct=fgGetNode("surface-positions/wing-fold-pos-norm",true);
319 elevator_pos_pct->setDoubleValue(0);
320 left_aileron_pos_pct->setDoubleValue(0);
321 right_aileron_pos_pct->setDoubleValue(0);
322 rudder_pos_pct->setDoubleValue(0);
323 flap_pos_pct->setDoubleValue(0);
324 speedbrake_pos_pct->setDoubleValue(0);
325 spoilers_pos_pct->setDoubleValue(0);
327 ab_brake_engaged = fgGetNode("/autopilot/autobrake/engaged", true);
328 ab_brake_left_pct = fgGetNode("/autopilot/autobrake/brake-left-output", true);
329 ab_brake_right_pct = fgGetNode("/autopilot/autobrake/brake-right-output", true);
331 altitude = fgGetNode("/position/altitude-ft");
332 temperature = fgGetNode("/environment/temperature-degc",true);
333 pressure = fgGetNode("/environment/pressure-inhg",true);
334 pressureSL = fgGetNode("/environment/pressure-sea-level-inhg",true);
335 ground_wind = fgGetNode("/environment/config/boundary/entry[0]/wind-speed-kt",true);
336 turbulence_gain = fgGetNode("/environment/turbulence/magnitude-norm",true);
337 turbulence_rate = fgGetNode("/environment/turbulence/rate-hz",true);
338 turbulence_model = fgGetNode("/environment/params/jsbsim-turbulence-model",true);
340 wind_from_north= fgGetNode("/environment/wind-from-north-fps",true);
341 wind_from_east = fgGetNode("/environment/wind-from-east-fps" ,true);
342 wind_from_down = fgGetNode("/environment/wind-from-down-fps" ,true);
344 slaved = fgGetNode("/sim/slaved/enabled", true);
346 for (unsigned int i = 0; i < Propulsion->GetNumEngines(); i++) {
347 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
348 Propulsion->GetEngine(i)->GetThruster()->SetRPM(node->getDoubleValue("rpm") /
349 Propulsion->GetEngine(i)->GetThruster()->GetGearRatio());
352 hook_root_struct = FGColumnVector3(
353 fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-offset-x-in", 196),
354 fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-offset-y-in", 0),
355 fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-offset-z-in", -16));
356 last_hook_tip[0] = 0; last_hook_tip[1] = 0; last_hook_tip[2] = 0;
357 last_hook_root[0] = 0; last_hook_root[1] = 0; last_hook_root[2] = 0;
362 /******************************************************************************/
363 FGJSBsim::~FGJSBsim(void)
368 /******************************************************************************/
370 // Initialize the JSBsim flight model, dt is the time increment for
371 // each subsequent iteration through the EOM
373 void FGJSBsim::init()
375 SG_LOG( SG_FLIGHT, SG_INFO, "Starting and initializing JSBsim" );
377 // Explicitly call the superclass's
378 // init method first.
380 if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
381 Atmosphere->SetTemperature(temperature->getDoubleValue(), get_Altitude(), FGAtmosphere::eCelsius);
382 Atmosphere->SetPressureSL(FGAtmosphere::eInchesHg, pressureSL->getDoubleValue());
383 // initialize to no turbulence, these values get set in the update loop
384 Winds->SetTurbType(FGWinds::ttNone);
385 Winds->SetTurbGain(0.0);
386 Winds->SetTurbRate(0.0);
387 Winds->SetWindspeed20ft(0.0);
388 Winds->SetProbabilityOfExceedence(0.0);
391 fgic->SetWindNEDFpsIC( -wind_from_north->getDoubleValue(),
392 -wind_from_east->getDoubleValue(),
393 -wind_from_down->getDoubleValue() );
395 SG_LOG(SG_FLIGHT,SG_INFO,"T,p,rho: " << Atmosphere->GetTemperature()
396 << ", " << Atmosphere->GetPressure()
397 << ", " << Atmosphere->GetDensity() );
399 FCS->SetDfPos( ofNorm, globals->get_controls()->get_flaps() );
401 needTrim = startup_trim->getBoolValue();
405 fdmex->RunIC(); //loop JSBSim once w/o integrating
406 if (fgGetBool("/sim/presets/running")) {
407 Propulsion->InitRunning(-1);
408 for (unsigned int i = 0; i < Propulsion->GetNumEngines(); i++) {
409 FGPiston* eng = (FGPiston*)Propulsion->GetEngine(i);
410 globals->get_controls()->set_magnetos(i, eng->GetMagnetos());
411 globals->get_controls()->set_mixture(i, FCS->GetMixtureCmd(i));
416 FGLocation cart(fgic->GetLongitudeRadIC(), fgic->GetLatitudeRadIC(),
417 get_Sea_level_radius() + fgic->GetAltitudeASLFtIC());
418 double cart_pos[3], contact[3], d[3], vel[3], agl;
419 update_ground_cache(cart, cart_pos, 0.01);
421 get_agl_ft(fdmex->GetSimTime(), cart_pos, SG_METER_TO_FEET*2, contact,
423 double terrain_alt = sqrt(contact[0]*contact[0] + contact[1]*contact[1]
424 + contact[2]*contact[2]) - get_Sea_level_radius();
426 SG_LOG(SG_FLIGHT, SG_INFO, "Ready to trim, terrain elevation is: "
427 << terrain_alt * SG_METER_TO_FEET );
429 if (fgGetBool("/sim/presets/onground")) {
430 FGColumnVector3 gndVelNED = cart.GetTec2l()
431 * FGColumnVector3(vel[0], vel[1], vel[2]);
432 fgic->SetVNorthFpsIC(gndVelNED(1));
433 fgic->SetVEastFpsIC(gndVelNED(2));
434 fgic->SetVDownFpsIC(gndVelNED(3));
436 fgic->SetTerrainElevationFtIC( terrain_alt );
441 copy_from_JSBsim(); //update the bus
443 SG_LOG( SG_FLIGHT, SG_INFO, " Initialized JSBSim with:" );
445 switch(fgic->GetSpeedSet()) {
447 SG_LOG(SG_FLIGHT,SG_INFO, " Vn,Ve,Vd= "
448 << Propagate->GetVel(FGJSBBase::eNorth) << ", "
449 << Propagate->GetVel(FGJSBBase::eEast) << ", "
450 << Propagate->GetVel(FGJSBBase::eDown) << " ft/s");
453 SG_LOG(SG_FLIGHT,SG_INFO, " U,V,W= "
454 << Propagate->GetUVW(1) << ", "
455 << Propagate->GetUVW(2) << ", "
456 << Propagate->GetUVW(3) << " ft/s");
459 SG_LOG(SG_FLIGHT,SG_INFO, " Mach: "
460 << Auxiliary->GetMach() );
464 SG_LOG(SG_FLIGHT,SG_INFO, " Indicated Airspeed: "
465 << Auxiliary->GetVcalibratedKTS() << " knots" );
469 stall_warning->setDoubleValue(0);
471 SG_LOG( SG_FLIGHT, SG_INFO, " Bank Angle: "
472 << Propagate->GetEuler(FGJSBBase::ePhi)*RADTODEG << " deg" );
473 SG_LOG( SG_FLIGHT, SG_INFO, " Pitch Angle: "
474 << Propagate->GetEuler(FGJSBBase::eTht)*RADTODEG << " deg" );
475 SG_LOG( SG_FLIGHT, SG_INFO, " True Heading: "
476 << Propagate->GetEuler(FGJSBBase::ePsi)*RADTODEG << " deg" );
477 SG_LOG( SG_FLIGHT, SG_INFO, " Latitude: "
478 << Propagate->GetLocation().GetLatitudeDeg() << " deg" );
479 SG_LOG( SG_FLIGHT, SG_INFO, " Longitude: "
480 << Propagate->GetLocation().GetLongitudeDeg() << " deg" );
481 SG_LOG( SG_FLIGHT, SG_INFO, " Altitude: "
482 << Propagate->GetAltitudeASL() << " feet" );
483 SG_LOG( SG_FLIGHT, SG_INFO, " loaded initial conditions" );
485 SG_LOG( SG_FLIGHT, SG_INFO, " set dt" );
487 SG_LOG( SG_FLIGHT, SG_INFO, "Finished initializing JSBSim" );
489 SG_LOG( SG_FLIGHT, SG_INFO, "FGControls::get_gear_down()= " <<
490 globals->get_controls()->get_gear_down() );
493 /******************************************************************************/
495 void FGJSBsim::unbind()
498 FGInterface::unbind();
501 /******************************************************************************/
503 // 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 update_external_forces(fdmex->GetSimTime() + i * fdmex->GetDeltaT());
531 FGJSBBase::Message* msg;
532 while ((msg = fdmex->ProcessNextMessage()) != NULL) {
533 // msg = fdmex->ProcessNextMessage();
535 case FGJSBBase::Message::eText:
536 if (msg->text == "Crash Detected: Simulation FREEZE.")
538 SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text );
540 case FGJSBBase::Message::eBool:
541 SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->bVal );
543 case FGJSBBase::Message::eInteger:
544 SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->iVal );
546 case FGJSBBase::Message::eDouble:
547 SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->dVal );
550 SG_LOG( SG_FLIGHT, SG_INFO, "Unrecognized message type." );
555 // translate JSBsim back to FG structure so that the
556 // autopilot (and the rest of the sim can use the updated values
560 /******************************************************************************/
562 void FGJSBsim::suspend()
565 SGSubsystem::suspend();
568 /******************************************************************************/
570 void FGJSBsim::resume()
573 SGSubsystem::resume();
576 /******************************************************************************/
578 // Convert from the FGInterface struct to the JSBsim generic_ struct
580 bool FGJSBsim::copy_to_JSBsim()
584 // copy control positions into the JSBsim structure
586 FCS->SetDaCmd( globals->get_controls()->get_aileron());
587 FCS->SetRollTrimCmd( globals->get_controls()->get_aileron_trim() );
588 FCS->SetDeCmd( globals->get_controls()->get_elevator());
589 FCS->SetPitchTrimCmd( globals->get_controls()->get_elevator_trim() );
590 FCS->SetDrCmd( -globals->get_controls()->get_rudder() );
591 FCS->SetYawTrimCmd( -globals->get_controls()->get_rudder_trim() );
592 FCS->SetDsCmd( globals->get_controls()->get_rudder() );
593 FCS->SetDfCmd( globals->get_controls()->get_flaps() );
594 FCS->SetDsbCmd( globals->get_controls()->get_speedbrake() );
595 FCS->SetDspCmd( globals->get_controls()->get_spoilers() );
597 // Parking brake sets minimum braking
599 double parking_brake = globals->get_controls()->get_brake_parking();
600 double left_brake = globals->get_controls()->get_brake_left();
601 double right_brake = globals->get_controls()->get_brake_right();
603 if (ab_brake_engaged->getBoolValue()) {
604 left_brake = ab_brake_left_pct->getDoubleValue();
605 right_brake = ab_brake_right_pct->getDoubleValue();
608 FCS->SetLBrake(FMAX(left_brake, parking_brake));
609 FCS->SetRBrake(FMAX(right_brake, parking_brake));
612 FCS->SetCBrake( 0.0 );
613 // FCS->SetCBrake( globals->get_controls()->get_brake(2) );
615 FCS->SetGearCmd( globals->get_controls()->get_gear_down());
616 for (i = 0; i < Propulsion->GetNumEngines(); i++) {
617 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
619 FCS->SetThrottleCmd(i, globals->get_controls()->get_throttle(i));
620 FCS->SetMixtureCmd(i, globals->get_controls()->get_mixture(i));
621 FCS->SetPropAdvanceCmd(i, globals->get_controls()->get_prop_advance(i));
622 FCS->SetFeatherCmd(i, globals->get_controls()->get_feather(i));
624 switch (Propulsion->GetEngine(i)->GetType()) {
625 case FGEngine::etPiston:
626 { // FGPiston code block
627 FGPiston* eng = (FGPiston*)Propulsion->GetEngine(i);
628 eng->SetMagnetos( globals->get_controls()->get_magnetos(i) );
630 } // end FGPiston code block
631 case FGEngine::etTurbine:
632 { // FGTurbine code block
633 FGTurbine* eng = (FGTurbine*)Propulsion->GetEngine(i);
634 eng->SetAugmentation( globals->get_controls()->get_augmentation(i) );
635 eng->SetReverse( globals->get_controls()->get_reverser(i) );
636 //eng->SetInjection( globals->get_controls()->get_water_injection(i) );
637 eng->SetCutoff( globals->get_controls()->get_cutoff(i) );
638 eng->SetIgnition( globals->get_controls()->get_ignition(i) );
640 } // end FGTurbine code block
641 case FGEngine::etRocket:
642 { // FGRocket code block
643 // FGRocket* eng = (FGRocket*)Propulsion->GetEngine(i);
645 } // end FGRocket code block
646 case FGEngine::etTurboprop:
647 { // FGTurboProp code block
648 FGTurboProp* eng = (FGTurboProp*)Propulsion->GetEngine(i);
649 eng->SetReverse( globals->get_controls()->get_reverser(i) );
650 eng->SetCutoff( globals->get_controls()->get_cutoff(i) );
651 eng->SetIgnition( globals->get_controls()->get_ignition(i) );
653 eng->SetGeneratorPower( globals->get_controls()->get_generator_breaker(i) );
654 eng->SetCondition( globals->get_controls()->get_condition(i) );
656 } // end FGTurboProp code block
661 { // FGEngine code block
662 FGEngine* eng = Propulsion->GetEngine(i);
664 eng->SetStarter( globals->get_controls()->get_starter(i) );
665 eng->SetRunning( node->getBoolValue("running") );
666 } // end FGEngine code block
669 Atmosphere->SetTemperature(temperature->getDoubleValue(), get_Altitude(), FGAtmosphere::eCelsius);
670 Atmosphere->SetPressureSL(FGAtmosphere::eInchesHg, pressureSL->getDoubleValue());
672 Winds->SetTurbType((FGWinds::tType)TURBULENCE_TYPE_NAMES[turbulence_model->getStringValue()]);
673 switch( Winds->GetTurbType() ) {
674 case FGWinds::ttStandard:
675 case FGWinds::ttCulp: {
676 double tmp = turbulence_gain->getDoubleValue();
677 Winds->SetTurbGain(tmp * tmp * 100.0);
678 Winds->SetTurbRate(turbulence_rate->getDoubleValue());
681 case FGWinds::ttMilspec:
682 case FGWinds::ttTustin: {
683 // milspec turbulence: 3=light, 4=moderate, 6=severe turbulence
684 // turbulence_gain normalized: 0: none, 1/3: light, 2/3: moderate, 3/3: severe
685 double tmp = turbulence_gain->getDoubleValue();
686 Winds->SetProbabilityOfExceedence(
687 SGMiscd::roundToInt(TurbulenceSeverityTable.GetValue( tmp ) )
689 Winds->SetWindspeed20ft(ground_wind->getDoubleValue());
697 Winds->SetWindNED( -wind_from_north->getDoubleValue(),
698 -wind_from_east->getDoubleValue(),
699 -wind_from_down->getDoubleValue() );
700 // SG_LOG(SG_FLIGHT,SG_INFO, "Wind NED: "
701 // << get_V_north_airmass() << ", "
702 // << get_V_east_airmass() << ", "
703 // << get_V_down_airmass() );
705 for (i = 0; i < Propulsion->GetNumTanks(); i++) {
706 SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
707 FGTank * tank = Propulsion->GetTank(i);
708 double fuelDensity = node->getDoubleValue("density-ppg");
710 if (fuelDensity < 0.1)
711 fuelDensity = 6.0; // Use average fuel value
713 tank->SetDensity(fuelDensity);
714 tank->SetContents(node->getDoubleValue("level-lbs"));
717 Propulsion->SetFuelFreeze((fgGetNode("/sim/freeze/fuel",true))->getBoolValue());
718 fdmex->SetChild(slaved->getBoolValue());
723 /******************************************************************************/
725 // Convert from the JSBsim generic_ struct to the FGInterface struct
727 bool FGJSBsim::copy_from_JSBsim()
731 _set_Inertias( MassBalance->GetMass(),
732 MassBalance->GetIxx(),
733 MassBalance->GetIyy(),
734 MassBalance->GetIzz(),
735 MassBalance->GetIxz() );
737 _set_CG_Position( MassBalance->GetXYZcg(1),
738 MassBalance->GetXYZcg(2),
739 MassBalance->GetXYZcg(3) );
741 _set_Accels_Body( Accelerations->GetBodyAccel(1),
742 Accelerations->GetBodyAccel(2),
743 Accelerations->GetBodyAccel(3) );
745 _set_Accels_CG_Body_N ( Auxiliary->GetNcg(1),
746 Auxiliary->GetNcg(2),
747 Auxiliary->GetNcg(3) );
749 _set_Accels_Pilot_Body( Auxiliary->GetPilotAccel(1),
750 Auxiliary->GetPilotAccel(2),
751 Auxiliary->GetPilotAccel(3) );
753 _set_Nlf( Auxiliary->GetNlf() );
757 _set_Velocities_Local( Propagate->GetVel(FGJSBBase::eNorth),
758 Propagate->GetVel(FGJSBBase::eEast),
759 Propagate->GetVel(FGJSBBase::eDown) );
761 _set_Velocities_Wind_Body( Propagate->GetUVW(1),
762 Propagate->GetUVW(2),
763 Propagate->GetUVW(3) );
765 // Make the HUD work ...
766 _set_Velocities_Ground( Propagate->GetVel(FGJSBBase::eNorth),
767 Propagate->GetVel(FGJSBBase::eEast),
768 -Propagate->GetVel(FGJSBBase::eDown) );
770 _set_V_rel_wind( Auxiliary->GetVt() );
772 _set_V_equiv_kts( Auxiliary->GetVequivalentKTS() );
774 _set_V_calibrated_kts( Auxiliary->GetVcalibratedKTS() );
776 _set_V_ground_speed( Auxiliary->GetVground() );
778 _set_Omega_Body( Propagate->GetPQR(FGJSBBase::eP),
779 Propagate->GetPQR(FGJSBBase::eQ),
780 Propagate->GetPQR(FGJSBBase::eR) );
782 _set_Euler_Rates( Auxiliary->GetEulerRates(FGJSBBase::ePhi),
783 Auxiliary->GetEulerRates(FGJSBBase::eTht),
784 Auxiliary->GetEulerRates(FGJSBBase::ePsi) );
786 _set_Mach_number( Auxiliary->GetMach() );
788 // Positions of Visual Reference Point
789 FGLocation l = Auxiliary->GetLocationVRP();
790 _updatePosition(SGGeoc::fromRadFt( l.GetLongitude(), l.GetLatitude(),
793 _set_Altitude_AGL( Propagate->GetDistanceAGL() );
795 double loc_cart[3] = { l(FGJSBBase::eX), l(FGJSBBase::eY), l(FGJSBBase::eZ) };
796 double contact[3], d[3], sd, t;
797 is_valid_m(&t, d, &sd);
798 get_agl_ft(t, loc_cart, SG_METER_TO_FEET*2, contact, d, d, d, &sd);
800 = FGColumnVector3( contact[0], contact[1], contact[2] ).Magnitude();
801 _set_Runway_altitude( rwrad - get_Sea_level_radius() );
804 _set_Euler_Angles( Propagate->GetEuler(FGJSBBase::ePhi),
805 Propagate->GetEuler(FGJSBBase::eTht),
806 Propagate->GetEuler(FGJSBBase::ePsi) );
808 _set_Alpha( Auxiliary->Getalpha() );
809 _set_Beta( Auxiliary->Getbeta() );
812 _set_Gamma_vert_rad( Auxiliary->GetGamma() );
814 _set_Earth_position_angle( Propagate->GetEarthPositionAngle() );
816 _set_Climb_Rate( Propagate->Gethdot() );
818 const FGMatrix33& Tl2b = Propagate->GetTl2b();
819 for ( i = 1; i <= 3; i++ ) {
820 for ( j = 1; j <= 3; j++ ) {
821 _set_T_Local_to_Body( i, j, Tl2b(i,j) );
825 // Copy the engine values from JSBSim.
826 for ( i=0; i < Propulsion->GetNumEngines(); i++ ) {
827 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
828 SGPropertyNode * tnode = node->getChild("thruster", 0, true);
829 FGThruster * thruster = Propulsion->GetEngine(i)->GetThruster();
831 switch (Propulsion->GetEngine(i)->GetType()) {
832 case FGEngine::etPiston:
833 { // FGPiston code block
834 FGPiston* eng = (FGPiston*)Propulsion->GetEngine(i);
835 node->setDoubleValue("egt-degf", eng->getExhaustGasTemp_degF());
836 node->setDoubleValue("oil-temperature-degf", eng->getOilTemp_degF());
837 node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi());
838 node->setDoubleValue("mp-osi", eng->getManifoldPressure_inHg());
839 // NOTE: mp-osi is not in ounces per square inch.
840 // This error is left for reasons of backwards compatibility with
841 // existing FlightGear sound and instrument configurations.
842 node->setDoubleValue("mp-inhg", eng->getManifoldPressure_inHg());
843 node->setDoubleValue("cht-degf", eng->getCylinderHeadTemp_degF());
844 node->setDoubleValue("rpm", eng->getRPM());
845 } // end FGPiston code block
847 case FGEngine::etRocket:
848 { // FGRocket code block
849 // FGRocket* eng = (FGRocket*)Propulsion->GetEngine(i);
850 } // end FGRocket code block
852 case FGEngine::etTurbine:
853 { // FGTurbine code block
854 FGTurbine* eng = (FGTurbine*)Propulsion->GetEngine(i);
855 node->setDoubleValue("n1", eng->GetN1());
856 node->setDoubleValue("n2", eng->GetN2());
857 node->setDoubleValue("egt-degf", 32 + eng->GetEGT()*9/5);
858 node->setBoolValue("augmentation", eng->GetAugmentation());
859 node->setBoolValue("water-injection", eng->GetInjection());
860 node->setBoolValue("ignition", eng->GetIgnition() != 0);
861 node->setDoubleValue("nozzle-pos-norm", eng->GetNozzle());
862 node->setDoubleValue("inlet-pos-norm", eng->GetInlet());
863 node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi());
864 node->setBoolValue("reversed", eng->GetReversed());
865 node->setBoolValue("cutoff", eng->GetCutoff());
866 node->setDoubleValue("epr", eng->GetEPR());
867 globals->get_controls()->set_reverser(i, eng->GetReversed() );
868 globals->get_controls()->set_cutoff(i, eng->GetCutoff() );
869 globals->get_controls()->set_water_injection(i, eng->GetInjection() );
870 globals->get_controls()->set_augmentation(i, eng->GetAugmentation() );
871 } // end FGTurbine code block
873 case FGEngine::etTurboprop:
874 { // FGTurboProp code block
875 FGTurboProp* eng = (FGTurboProp*)Propulsion->GetEngine(i);
876 node->setDoubleValue("n1", eng->GetN1());
877 //node->setDoubleValue("n2", eng->GetN2());
878 node->setDoubleValue("itt_degf", 32 + eng->GetITT()*9/5);
879 node->setBoolValue("ignition", eng->GetIgnition() != 0);
880 node->setDoubleValue("nozzle-pos-norm", eng->GetNozzle());
881 node->setDoubleValue("inlet-pos-norm", eng->GetInlet());
882 node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi());
883 node->setBoolValue("reversed", eng->GetReversed());
884 node->setBoolValue("cutoff", eng->GetCutoff());
885 node->setBoolValue("starting", eng->GetEngStarting());
886 node->setBoolValue("generator-power", eng->GetGeneratorPower());
887 node->setBoolValue("damaged", eng->GetCondition() != 0);
888 node->setBoolValue("ielu-intervent", eng->GetIeluIntervent());
889 node->setDoubleValue("oil-temperature-degf", eng->getOilTemp_degF());
890 // node->setBoolValue("onfire", eng->GetFire());
891 globals->get_controls()->set_reverser(i, eng->GetReversed() );
892 globals->get_controls()->set_cutoff(i, eng->GetCutoff() );
893 } // end FGTurboProp code block
895 case FGEngine::etElectric:
896 { // FGElectric code block
897 FGElectric* eng = (FGElectric*)Propulsion->GetEngine(i);
898 node->setDoubleValue("rpm", eng->getRPM());
899 } // end FGElectric code block
901 case FGEngine::etUnknown:
905 { // FGEngine code block
906 FGEngine* eng = Propulsion->GetEngine(i);
907 node->setDoubleValue("fuel-flow-gph", eng->getFuelFlow_gph());
908 node->setDoubleValue("thrust_lb", thruster->GetThrust());
909 node->setDoubleValue("fuel-flow_pph", eng->getFuelFlow_pph());
910 node->setBoolValue("running", eng->GetRunning());
911 node->setBoolValue("starter", eng->GetStarter());
912 node->setBoolValue("cranking", eng->GetCranking());
913 globals->get_controls()->set_starter(i, eng->GetStarter() );
914 } // end FGEngine code block
916 switch (thruster->GetType()) {
917 case FGThruster::ttNozzle:
918 { // FGNozzle code block
919 // FGNozzle* noz = (FGNozzle*)thruster;
920 } // end FGNozzle code block
922 case FGThruster::ttPropeller:
923 { // FGPropeller code block
924 FGPropeller* prop = (FGPropeller*)thruster;
925 tnode->setDoubleValue("rpm", thruster->GetRPM());
926 tnode->setDoubleValue("pitch", prop->GetPitch());
927 tnode->setDoubleValue("torque", prop->GetTorque());
928 tnode->setBoolValue("feathered", prop->GetFeather());
929 } // end FGPropeller code block
931 case FGThruster::ttRotor:
932 { // FGRotor code block
933 // FGRotor* rotor = (FGRotor*)thruster;
934 } // end FGRotor code block
936 case FGThruster::ttDirect:
937 { // Direct code block
938 } // end Direct code block
944 // Copy the fuel levels from JSBSim if fuel
945 // freeze not enabled.
946 if ( ! Propulsion->GetFuelFreeze() ) {
947 for (i = 0; i < Propulsion->GetNumTanks(); i++) {
948 SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
949 FGTank* tank = Propulsion->GetTank(i);
950 double contents = tank->GetContents();
951 double temp = tank->GetTemperature_degC();
952 double fuelDensity = tank->GetDensity();
954 if (fuelDensity < 0.1)
955 fuelDensity = 6.0; // Use average fuel value
957 node->setDoubleValue("density-ppg" , fuelDensity);
958 node->setDoubleValue("level-lbs", contents);
959 if (temp != -9999.0) node->setDoubleValue("temperature_degC", temp);
965 stall_warning->setDoubleValue( Aerodynamics->GetStallWarn() );
967 elevator_pos_pct->setDoubleValue( FCS->GetDePos(ofNorm) );
968 left_aileron_pos_pct->setDoubleValue( FCS->GetDaLPos(ofNorm) );
969 right_aileron_pos_pct->setDoubleValue( FCS->GetDaRPos(ofNorm) );
970 rudder_pos_pct->setDoubleValue( -1*FCS->GetDrPos(ofNorm) );
971 flap_pos_pct->setDoubleValue( FCS->GetDfPos(ofNorm) );
972 speedbrake_pos_pct->setDoubleValue( FCS->GetDsbPos(ofNorm) );
973 spoilers_pos_pct->setDoubleValue( FCS->GetDspPos(ofNorm) );
974 tailhook_pos_pct->setDoubleValue( FCS->GetTailhookPos() );
975 wing_fold_pos_pct->setDoubleValue( FCS->GetWingFoldPos() );
977 // force a sim crashed if crashed (altitude AGL < 0)
978 if (get_Altitude_AGL() < -100.0) {
979 fdmex->SuspendIntegration();
987 bool FGJSBsim::ToggleDataLogging(void)
989 // ToDo: handle this properly
990 fdmex->DisableOutput();
995 bool FGJSBsim::ToggleDataLogging(bool state)
998 fdmex->EnableOutput();
1001 fdmex->DisableOutput();
1008 void FGJSBsim::set_Latitude(double lat)
1010 double alt = altitude->getDoubleValue();
1011 double sea_level_radius_meters, lat_geoc;
1013 if ( alt < -9990 ) alt = 0.0;
1015 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat );
1016 SG_LOG(SG_FLIGHT,SG_INFO," cur alt (ft) = " << alt );
1018 sgGeodToGeoc( lat, alt * SG_FEET_TO_METER,
1019 &sea_level_radius_meters, &lat_geoc );
1021 double sea_level_radius_ft = sea_level_radius_meters * SG_METER_TO_FEET;
1022 _set_Sea_level_radius( sea_level_radius_ft );
1025 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_ft );
1026 fgic->SetLatitudeRadIC( lat_geoc );
1029 Propagate->SetLatitude(lat_geoc);
1031 FGInterface::set_Latitude(lat);
1035 void FGJSBsim::set_Longitude(double lon)
1037 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon );
1040 fgic->SetLongitudeRadIC(lon);
1042 Propagate->SetLongitude(lon);
1044 FGInterface::set_Longitude(lon);
1047 // Sets the altitude above sea level.
1048 void FGJSBsim::set_Altitude(double alt)
1050 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt );
1053 fgic->SetAltitudeASLFtIC(alt);
1055 Propagate->SetAltitudeASL(alt);
1057 FGInterface::set_Altitude(alt);
1060 void FGJSBsim::set_V_calibrated_kts(double vc)
1062 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " << vc );
1065 fgic->SetVcalibratedKtsIC(vc);
1067 double p=pressure->getDoubleValue();
1068 double psl=fdmex->GetAtmosphere()->GetPressureSL();
1069 double rhosl=fdmex->GetAtmosphere()->GetDensitySL();
1070 double mach = FGJSBBase::MachFromVcalibrated(vc, p, psl, rhosl);
1071 double temp = 1.8*(temperature->getDoubleValue()+273.15);
1072 double soundSpeed = sqrt(1.4*1716.0*temp);
1073 FGColumnVector3 vUVW = Propagate->GetUVW();
1075 vUVW *= mach * soundSpeed;
1076 Propagate->SetUVW(1, vUVW(1));
1077 Propagate->SetUVW(2, vUVW(2));
1078 Propagate->SetUVW(3, vUVW(3));
1081 FGInterface::set_V_calibrated_kts(vc);
1084 void FGJSBsim::set_Mach_number(double mach)
1086 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " << mach );
1089 fgic->SetMachIC(mach);
1091 double temp = 1.8*(temperature->getDoubleValue()+273.15);
1092 double soundSpeed = sqrt(1.4*1716.0*temp);
1093 FGColumnVector3 vUVW = Propagate->GetUVW();
1095 vUVW *= mach * soundSpeed;
1096 Propagate->SetUVW(1, vUVW(1));
1097 Propagate->SetUVW(2, vUVW(2));
1098 Propagate->SetUVW(3, vUVW(3));
1101 FGInterface::set_Mach_number(mach);
1104 void FGJSBsim::set_Velocities_Local( double north, double east, double down )
1106 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: "
1107 << north << ", " << east << ", " << down );
1110 fgic->SetVNorthFpsIC(north);
1111 fgic->SetVEastFpsIC(east);
1112 fgic->SetVDownFpsIC(down);
1115 FGColumnVector3 vNED(north, east, down);
1116 FGColumnVector3 vUVW = Propagate->GetTl2b() * vNED;
1117 Propagate->SetUVW(1, vUVW(1));
1118 Propagate->SetUVW(2, vUVW(2));
1119 Propagate->SetUVW(3, vUVW(3));
1122 FGInterface::set_Velocities_Local(north, east, down);
1125 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w)
1127 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
1128 << u << ", " << v << ", " << w );
1131 fgic->SetUBodyFpsIC(u);
1132 fgic->SetVBodyFpsIC(v);
1133 fgic->SetWBodyFpsIC(w);
1136 Propagate->SetUVW(1, u);
1137 Propagate->SetUVW(2, v);
1138 Propagate->SetUVW(3, w);
1141 FGInterface::set_Velocities_Wind_Body(u, v, w);
1145 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi )
1147 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: "
1148 << phi << ", " << theta << ", " << psi );
1151 fgic->SetThetaRadIC(theta);
1152 fgic->SetPhiRadIC(phi);
1153 fgic->SetPsiRadIC(psi);
1156 FGQuaternion quat(phi, theta, psi);
1157 FGMatrix33 Tl2b = quat.GetT();
1158 FGMatrix33 Ti2b = Tl2b*Propagate->GetTi2l();
1159 FGQuaternion Qi = Ti2b.GetQuaternion();
1160 Propagate->SetInertialOrientation(Qi);
1163 FGInterface::set_Euler_Angles(phi, theta, psi);
1167 void FGJSBsim::set_Climb_Rate( double roc)
1169 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
1171 //since both climb rate and flight path angle are set in the FG
1172 //startup sequence, something is needed to keep one from cancelling
1174 if( fabs(roc) > 1 && fabs(fgic->GetFlightPathAngleRadIC()) < 0.01 ) {
1176 fgic->SetClimbRateFpsIC(roc);
1178 FGColumnVector3 vNED = Propagate->GetVel();
1179 vNED(FGJSBBase::eDown) = -roc;
1180 FGColumnVector3 vUVW = Propagate->GetTl2b() * vNED;
1181 Propagate->SetUVW(1, vUVW(1));
1182 Propagate->SetUVW(2, vUVW(2));
1183 Propagate->SetUVW(3, vUVW(3));
1186 FGInterface::set_Climb_Rate(roc);
1190 void FGJSBsim::set_Gamma_vert_rad( double gamma)
1192 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
1194 if( !(fabs(gamma) < 0.01 && fabs(fgic->GetClimbRateFpsIC()) > 1) ) {
1196 fgic->SetFlightPathAngleRadIC(gamma);
1198 FGColumnVector3 vNED = Propagate->GetVel();
1199 double vt = vNED.Magnitude();
1200 vNED(FGJSBBase::eDown) = -vt * sin(gamma);
1201 FGColumnVector3 vUVW = Propagate->GetTl2b() * vNED;
1202 Propagate->SetUVW(1, vUVW(1));
1203 Propagate->SetUVW(2, vUVW(2));
1204 Propagate->SetUVW(3, vUVW(3));
1207 FGInterface::set_Gamma_vert_rad(gamma);
1211 void FGJSBsim::init_gear(void )
1213 FGGroundReactions* gr=fdmex->GetGroundReactions();
1214 int Ngear=GroundReactions->GetNumGearUnits();
1215 for (int i=0;i<Ngear;i++) {
1216 FGLGear *gear = gr->GetGearUnit(i);
1217 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
1218 node->setDoubleValue("xoffset-in", gear->GetBodyLocation()(1));
1219 node->setDoubleValue("yoffset-in", gear->GetBodyLocation()(2));
1220 node->setDoubleValue("zoffset-in", gear->GetBodyLocation()(3));
1221 node->setBoolValue("wow", gear->GetWOW());
1222 node->setDoubleValue("rollspeed-ms", gear->GetWheelRollVel()*0.3043);
1223 node->setBoolValue("has-brake", gear->GetBrakeGroup() > 0);
1224 node->setDoubleValue("position-norm", gear->GetGearUnitPos());
1225 // node->setDoubleValue("tire-pressure-norm", gear->GetTirePressure());
1226 node->setDoubleValue("compression-norm", gear->GetCompLen());
1227 node->setDoubleValue("compression-ft", gear->GetCompLen());
1228 if ( gear->GetSteerable() )
1229 node->setDoubleValue("steering-norm", gear->GetSteerNorm());
1233 void FGJSBsim::update_gear(void)
1235 FGGroundReactions* gr=fdmex->GetGroundReactions();
1236 int Ngear=GroundReactions->GetNumGearUnits();
1237 for (int i=0;i<Ngear;i++) {
1238 FGLGear *gear = gr->GetGearUnit(i);
1239 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
1240 node->getChild("wow", 0, true)->setBoolValue( gear->GetWOW());
1241 node->getChild("rollspeed-ms", 0, true)->setDoubleValue(gear->GetWheelRollVel()*0.3043);
1242 node->getChild("position-norm", 0, true)->setDoubleValue(gear->GetGearUnitPos());
1243 // gear->SetTirePressure(node->getDoubleValue("tire-pressure-norm"));
1244 node->setDoubleValue("compression-norm", gear->GetCompLen());
1245 node->setDoubleValue("compression-ft", gear->GetCompLen());
1246 if ( gear->GetSteerable() )
1247 node->setDoubleValue("steering-norm", gear->GetSteerNorm());
1251 void FGJSBsim::do_trim(void)
1255 if ( fgGetBool("/sim/presets/onground") )
1257 fgtrim = new FGTrim(fdmex,tGround);
1259 fgtrim = new FGTrim(fdmex,tFull);
1262 if ( !fgtrim->DoTrim() ) {
1264 fgtrim->TrimStats();
1266 trimmed->setBoolValue(true);
1270 pitch_trim->setDoubleValue( FCS->GetPitchTrimCmd() );
1271 throttle_trim->setDoubleValue( FCS->GetThrottleCmd(0) );
1272 aileron_trim->setDoubleValue( FCS->GetDaCmd() );
1273 rudder_trim->setDoubleValue( -FCS->GetDrCmd() );
1275 globals->get_controls()->set_elevator_trim(FCS->GetPitchTrimCmd());
1276 globals->get_controls()->set_elevator(FCS->GetDeCmd());
1277 for( unsigned i = 0; i < Propulsion->GetNumEngines(); i++ )
1278 globals->get_controls()->set_throttle(i, FCS->GetThrottleCmd(i));
1280 globals->get_controls()->set_aileron(FCS->GetDaCmd());
1281 globals->get_controls()->set_rudder( -FCS->GetDrCmd());
1283 SG_LOG( SG_FLIGHT, SG_INFO, " Trim complete" );
1286 bool FGJSBsim::update_ground_cache(FGLocation cart, double* cart_pos, double dt)
1288 // Compute the radius of the aircraft. That is the radius of a ball
1289 // where all gear units are in. At the moment it is at least 10ft ...
1290 double acrad = 10.0;
1291 int n_gears = GroundReactions->GetNumGearUnits();
1292 for (int i=0; i<n_gears; ++i) {
1293 FGColumnVector3 bl = GroundReactions->GetGearUnit(i)->GetBodyLocation();
1294 double r = bl.Magnitude();
1299 // Compute the potential movement of this aircraft and query for the
1300 // ground in this area.
1301 double groundCacheRadius = acrad + 2*dt*Propagate->GetUVW().Magnitude();
1302 cart_pos[0] = cart(1);
1303 cart_pos[1] = cart(2);
1304 cart_pos[2] = cart(3);
1305 double t0 = fdmex->GetSimTime();
1306 bool cache_ok = prepare_ground_cache_ft( t0, t0 + dt, cart_pos,
1307 groundCacheRadius );
1309 SG_LOG(SG_FLIGHT, SG_WARN,
1310 "FGInterface is being called without scenery below the aircraft!");
1312 SG_LOG(SG_FLIGHT, SG_WARN, "altitude = "
1313 << fgic->GetAltitudeASLFtIC());
1315 SG_LOG(SG_FLIGHT, SG_WARN, "sea level radius = "
1316 << get_Sea_level_radius());
1318 SG_LOG(SG_FLIGHT, SG_WARN, "latitude = "
1319 << fgic->GetLatitudeRadIC());
1321 SG_LOG(SG_FLIGHT, SG_WARN, "longitude = "
1322 << fgic->GetLongitudeRadIC());
1329 FGJSBsim::get_agl_ft(double t, const double pt[3], double alt_off,
1330 double contact[3], double normal[3], double vel[3],
1331 double angularVel[3], double *agl)
1333 const simgear::BVHMaterial* material;
1334 simgear::BVHNode::Id id;
1335 if (!FGInterface::get_agl_ft(t, pt, alt_off, contact, normal, vel,
1336 angularVel, material, id))
1338 SGGeod geodPt = SGGeod::fromCart(SG_FEET_TO_METER*SGVec3d(pt));
1339 SGQuatd hlToEc = SGQuatd::fromLonLat(geodPt);
1340 *agl = dot(hlToEc.rotate(SGVec3d(0, 0, 1)), SGVec3d(contact) - SGVec3d(pt));
1344 inline static double sqr(double x)
1349 static double angle_diff(double a, double b)
1351 double diff = fabs(a - b);
1352 if (diff > 180) diff = 360 - diff;
1357 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)
1359 FGColumnVector3 tip(-hook_length * cos_fi_guess, 0, hook_length * sin_fi_guess);
1360 double dist = DotProduct(tip, ground_normal_body);
1361 if (fabs(dist + E) < 0.0001) {
1362 sin_fis[*points] = sin_fi_guess;
1363 cos_fis[*points] = cos_fi_guess;
1364 fis[*points] = atan2(sin_fi_guess, cos_fi_guess) * SG_RADIANS_TO_DEGREES;
1370 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)
1372 if (sin_fi_guess >= -1 && sin_fi_guess <= 1) {
1373 double cos_fi_guess = sqrt(1 - sqr(sin_fi_guess));
1374 check_hook_solution(ground_normal_body, E, hook_length, sin_fi_guess, cos_fi_guess, sin_fis, cos_fis, fis, points);
1375 if (fabs(cos_fi_guess) > SG_EPSILON) {
1376 check_hook_solution(ground_normal_body, E, hook_length, sin_fi_guess, -cos_fi_guess, sin_fis, cos_fis, fis, points);
1381 void FGJSBsim::update_external_forces(double t_off)
1383 const FGMatrix33& Tb2l = Propagate->GetTb2l();
1384 const FGMatrix33& Tl2b = Propagate->GetTl2b();
1385 const FGLocation& Location = Propagate->GetLocation();
1386 const FGMatrix33& Tec2l = Location.GetTec2l();
1388 double hook_area[4][3];
1390 FGColumnVector3 hook_root_body = MassBalance->StructuralToBody(hook_root_struct);
1391 FGColumnVector3 hook_root = Location.LocalToLocation(Tb2l * hook_root_body);
1392 hook_area[1][0] = hook_root(1);
1393 hook_area[1][1] = hook_root(2);
1394 hook_area[1][2] = hook_root(3);
1396 hook_length = fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-length-ft", 6.75);
1397 double fi_min = fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-pos-min-deg", -18);
1398 double fi_max = fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-pos-max-deg", 30);
1399 double fi = fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-pos-norm") * (fi_max - fi_min) + fi_min;
1400 double cos_fi = cos(fi * SG_DEGREES_TO_RADIANS);
1401 double sin_fi = sin(fi * SG_DEGREES_TO_RADIANS);
1403 FGColumnVector3 hook_tip_body = hook_root_body;
1404 hook_tip_body(1) -= hook_length * cos_fi;
1405 hook_tip_body(3) += hook_length * sin_fi;
1408 double ground_normal[3];
1409 double ground_vel[3];
1410 double ground_angular_vel[3];
1414 bool got = get_agl_ft(t_off, hook_area[1], 0, contact, ground_normal,
1415 ground_vel, ground_angular_vel, &root_agl_ft);
1416 if (got && root_agl_ft > 0 && root_agl_ft < hook_length) {
1417 FGColumnVector3 ground_normal_body = Tl2b * (Tec2l * FGColumnVector3(ground_normal[0], ground_normal[1], ground_normal[2]));
1418 FGColumnVector3 contact_body = Tl2b * Location.LocationToLocal(FGColumnVector3(contact[0], contact[1], contact[2]));
1419 double D = -DotProduct(contact_body, ground_normal_body);
1421 // check hook tip agl against same ground plane
1422 double hook_tip_agl_ft = DotProduct(hook_tip_body, ground_normal_body) + D;
1423 if (hook_tip_agl_ft < 0) {
1425 // hook tip: hx - l cos, hy, hz + l sin
1426 // on ground: - n0 l cos + n2 l sin + E = 0
1428 double E = D + DotProduct(hook_root_body, ground_normal_body);
1430 // substitue x = sin fi, cos fi = sqrt(1 - x * x)
1431 // and rearrange to get a quadratic with coeffs:
1432 double a = sqr(hook_length) * (sqr(ground_normal_body(1)) + sqr(ground_normal_body(3)));
1433 double b = 2 * E * ground_normal_body(3) * hook_length;
1434 double c = sqr(E) - sqr(ground_normal_body(1) * hook_length);
1436 double disc = sqr(b) - 4 * a * c;
1438 double delta = sqrt(disc) / (2 * a);
1440 // allow 4 solutions for safety, should never happen
1446 double sin_fi_guess = -b / (2 * a) - delta;
1447 check_hook_solution(ground_normal_body, E, hook_length, sin_fi_guess, sin_fis, cos_fis, fis, &points);
1448 check_hook_solution(ground_normal_body, E, hook_length, sin_fi_guess + 2 * delta, sin_fis, cos_fis, fis, &points);
1451 double diff1 = angle_diff(fi, fis[0]);
1452 double diff2 = angle_diff(fi, fis[1]);
1453 int point = diff1 < diff2 ? 0 : 1;
1455 sin_fi = sin_fis[point];
1456 cos_fi = cos_fis[point];
1457 hook_tip_body(1) = hook_root_body(1) - hook_length * cos_fi;
1458 hook_tip_body(3) = hook_root_body(3) + hook_length * sin_fi;
1464 FGColumnVector3 hook_root_vel = Propagate->GetVel() + (Tb2l * (Propagate->GetPQR() * hook_root_body));
1465 double wire_ends_ec[2][3];
1466 double wire_vel_ec[2][3];
1467 get_wire_ends_ft(t_off, wire_ends_ec, wire_vel_ec);
1468 FGColumnVector3 wire_vel_1 = Tec2l * FGColumnVector3(wire_vel_ec[0][0], wire_vel_ec[0][1], wire_vel_ec[0][2]);
1469 FGColumnVector3 wire_vel_2 = Tec2l * FGColumnVector3(wire_vel_ec[1][0], wire_vel_ec[1][1], wire_vel_ec[1][2]);
1470 FGColumnVector3 rel_vel = hook_root_vel - (wire_vel_1 + wire_vel_2) / 2;
1471 if (rel_vel.Magnitude() < 3) {
1474 fgSetDouble("/fdm/jsbsim/external_reactions/hook/magnitude", 0.0);
1476 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;
1477 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;
1478 FGColumnVector3 force_plane_normal = wire_end1_body * wire_end2_body;
1479 force_plane_normal.Normalize();
1480 cos_fi = DotProduct(force_plane_normal, FGColumnVector3(0, 0, 1));
1481 if (cos_fi < 0) cos_fi = -cos_fi;
1482 sin_fi = sqrt(1 - sqr(cos_fi));
1483 fi = atan2(sin_fi, cos_fi) * SG_RADIANS_TO_DEGREES;
1485 fgSetDouble("/fdm/jsbsim/external_reactions/hook/x", -cos_fi);
1486 fgSetDouble("/fdm/jsbsim/external_reactions/hook/y", 0);
1487 fgSetDouble("/fdm/jsbsim/external_reactions/hook/z", sin_fi);
1488 fgSetDouble("/fdm/jsbsim/external_reactions/hook/magnitude", fgGetDouble("/fdm/jsbsim/systems/hook/force"));
1492 FGColumnVector3 hook_tip = Location.LocalToLocation(Tb2l * hook_tip_body);
1494 hook_area[0][0] = hook_tip(1);
1495 hook_area[0][1] = hook_tip(2);
1496 hook_area[0][2] = hook_tip(3);
1499 // The previous positions.
1500 hook_area[2][0] = last_hook_root[0];
1501 hook_area[2][1] = last_hook_root[1];
1502 hook_area[2][2] = last_hook_root[2];
1503 hook_area[3][0] = last_hook_tip[0];
1504 hook_area[3][1] = last_hook_tip[1];
1505 hook_area[3][2] = last_hook_tip[2];
1507 // Check if we caught a wire.
1508 // Returns true if we caught one.
1509 if (caught_wire_ft(t_off, hook_area)) {
1514 // save actual position as old position ...
1515 last_hook_tip[0] = hook_area[0][0];
1516 last_hook_tip[1] = hook_area[0][1];
1517 last_hook_tip[2] = hook_area[0][2];
1518 last_hook_root[0] = hook_area[1][0];
1519 last_hook_root[1] = hook_area[1][1];
1520 last_hook_root[2] = hook_area[1][2];
1522 fgSetDouble("/fdm/jsbsim/systems/hook/tailhook-pos-deg", fi);