1 // JSBsim.cxx -- interface to the JSBsim flight model
3 // Written by Curtis Olson, started February 1999.
5 // Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <simgear/compiler.h>
30 #ifdef SG_MATH_EXCEPTION_CLASH
36 #include <simgear/constants.h>
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/math/sg_geodesy.hxx>
39 #include <simgear/misc/sg_path.hxx>
41 #include <FDM/flight.hxx>
43 #include <Aircraft/aircraft.hxx>
44 #include <Controls/controls.hxx>
45 #include <Main/globals.hxx>
46 #include <Main/fg_props.hxx>
48 #include <FDM/JSBSim/FGFDMExec.h>
49 #include <FDM/JSBSim/FGAircraft.h>
50 #include <FDM/JSBSim/FGFCS.h>
51 #include <FDM/JSBSim/FGPosition.h>
52 #include <FDM/JSBSim/FGRotation.h>
53 #include <FDM/JSBSim/FGState.h>
54 #include <FDM/JSBSim/FGTranslation.h>
55 #include <FDM/JSBSim/FGAuxiliary.h>
56 #include <FDM/JSBSim/FGInitialCondition.h>
57 #include <FDM/JSBSim/FGTrim.h>
58 #include <FDM/JSBSim/FGAtmosphere.h>
59 #include <FDM/JSBSim/FGMassBalance.h>
60 #include <FDM/JSBSim/FGAerodynamics.h>
61 #include <FDM/JSBSim/FGLGear.h>
62 #include <FDM/JSBSim/FGPropertyManager.h>
66 FMAX (double a, double b)
72 /******************************************************************************/
74 FGJSBsim::FGJSBsim( double dt )
79 fdmex = new FGFDMExec( (FGPropertyManager*)globals->get_props() );
81 State = fdmex->GetState();
82 Atmosphere = fdmex->GetAtmosphere();
83 FCS = fdmex->GetFCS();
84 MassBalance = fdmex->GetMassBalance();
85 Propulsion = fdmex->GetPropulsion();
86 Aircraft = fdmex->GetAircraft();
87 Translation = fdmex->GetTranslation();
88 Rotation = fdmex->GetRotation();
89 Position = fdmex->GetPosition();
90 Auxiliary = fdmex->GetAuxiliary();
91 Aerodynamics = fdmex->GetAerodynamics();
92 GroundReactions = fdmex->GetGroundReactions();
97 SGPath aircraft_path( globals->get_fg_root() );
98 aircraft_path.append( "Aircraft" );
100 SGPath engine_path( globals->get_fg_root() );
101 engine_path.append( "Engine" );
104 result = fdmex->LoadModel( aircraft_path.str(),
106 fgGetString("/sim/aero") );
109 SG_LOG( SG_FLIGHT, SG_INFO, " loaded aero.");
111 SG_LOG( SG_FLIGHT, SG_INFO,
112 " aero does not exist (you may have mis-typed the name).");
116 SG_LOG( SG_FLIGHT, SG_INFO, "" );
117 SG_LOG( SG_FLIGHT, SG_INFO, "" );
118 SG_LOG( SG_FLIGHT, SG_INFO, "After loading aero definition file ..." );
120 int Neng = Propulsion->GetNumEngines();
121 SG_LOG( SG_FLIGHT, SG_INFO, "num engines = " << Neng );
123 if ( GroundReactions->GetNumGearUnits() <= 0 ) {
124 SG_LOG( SG_FLIGHT, SG_ALERT, "num gear units = "
125 << GroundReactions->GetNumGearUnits() );
126 SG_LOG( SG_FLIGHT, SG_ALERT, "This is a very bad thing because with 0 gear units, the ground trimming");
127 SG_LOG( SG_FLIGHT, SG_ALERT, "routine (coming up later in the code) will core dump.");
128 SG_LOG( SG_FLIGHT, SG_ALERT, "Halting the sim now, and hoping a solution will present itself soon!");
135 // Set initial fuel levels if provided.
136 for (unsigned int i = 0; i < Propulsion->GetNumTanks(); i++) {
137 SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
138 if (node->getChild("level-gal_us", 0, false) != 0)
139 Propulsion->GetTank(i)
140 ->SetContents(node->getDoubleValue("level-gal_us") * 6.6);
143 fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
144 fgSetDouble("/fdm/trim/throttle", FCS->GetThrottleCmd(0));
145 fgSetDouble("/fdm/trim/aileron", FCS->GetDaCmd());
146 fgSetDouble("/fdm/trim/rudder", FCS->GetDrCmd());
148 startup_trim = fgGetNode("/sim/presets/trim", true);
150 trimmed = fgGetNode("/fdm/trim/trimmed", true);
151 trimmed->setBoolValue(false);
153 pitch_trim = fgGetNode("/fdm/trim/pitch-trim", true );
154 throttle_trim = fgGetNode("/fdm/trim/throttle", true );
155 aileron_trim = fgGetNode("/fdm/trim/aileron", true );
156 rudder_trim = fgGetNode("/fdm/trim/rudder", true );
158 stall_warning = fgGetNode("/sim/alarms/stall-warning",true);
159 stall_warning->setDoubleValue(0);
162 flap_pos_pct=fgGetNode("/surface-positions/flap-pos-norm",true);
163 elevator_pos_pct=fgGetNode("/surface-positions/elevator-pos-norm",true);
165 =fgGetNode("/surface-positions/left-aileron-pos-norm",true);
166 right_aileron_pos_pct
167 =fgGetNode("/surface-positions/right-aileron-pos-norm",true);
168 rudder_pos_pct=fgGetNode("/surface-positions/rudder-pos-norm",true);
172 elevator_pos_pct->setDoubleValue(0);
173 left_aileron_pos_pct->setDoubleValue(0);
174 right_aileron_pos_pct->setDoubleValue(0);
175 rudder_pos_pct->setDoubleValue(0);
176 flap_pos_pct->setDoubleValue(0);
178 temperature = fgGetNode("/environment/temperature-degc",true);
179 pressure = fgGetNode("/environment/pressure-inhg",true);
180 density = fgGetNode("/environment/density-slugft3",true);
181 turbulence = fgGetNode("environment/turbulence-norm",true);
183 wind_from_north= fgGetNode("/environment/wind-from-north-fps",true);
184 wind_from_east = fgGetNode("/environment/wind-from-east-fps" ,true);
185 wind_from_down = fgGetNode("/environment/wind-from-down-fps" ,true);
189 /******************************************************************************/
190 FGJSBsim::~FGJSBsim(void) {
194 /******************************************************************************/
196 // Initialize the JSBsim flight model, dt is the time increment for
197 // each subsequent iteration through the EOM
199 void FGJSBsim::init() {
201 SG_LOG( SG_FLIGHT, SG_INFO, "Starting and initializing JSBsim" );
203 // Explicitly call the superclass's
204 // init method first.
207 Atmosphere->UseInternal();
209 if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
210 Atmosphere->UseExternal();
211 Atmosphere->SetExTemperature(
212 9.0/5.0*(temperature->getDoubleValue()+273.15) );
213 Atmosphere->SetExPressure(pressure->getDoubleValue()*70.726566);
214 Atmosphere->SetExDensity(density->getDoubleValue());
215 Atmosphere->SetTurbGain(turbulence->getDoubleValue()*100.0);
217 Atmosphere->UseInternal();
221 fgic->SetVnorthFpsIC( wind_from_north->getDoubleValue() );
222 fgic->SetVeastFpsIC( wind_from_east->getDoubleValue() );
223 fgic->SetVdownFpsIC( wind_from_down->getDoubleValue() );
225 //Atmosphere->SetExTemperature(get_Static_temperature());
226 //Atmosphere->SetExPressure(get_Static_pressure());
227 //Atmosphere->SetExDensity(get_Density());
228 SG_LOG(SG_FLIGHT,SG_INFO,"T,p,rho: " << fdmex->GetAtmosphere()->GetTemperature()
229 << ", " << fdmex->GetAtmosphere()->GetPressure()
230 << ", " << fdmex->GetAtmosphere()->GetDensity() );
236 fdmex->RunIC(); //loop JSBSim once w/o integrating
237 copy_from_JSBsim(); //update the bus
239 SG_LOG( SG_FLIGHT, SG_INFO, " Initialized JSBSim with:" );
241 switch(fgic->GetSpeedSet()) {
243 SG_LOG(SG_FLIGHT,SG_INFO, " Vn,Ve,Vd= "
244 << Position->GetVn() << ", "
245 << Position->GetVe() << ", "
246 << Position->GetVd() << " ft/s");
249 SG_LOG(SG_FLIGHT,SG_INFO, " U,V,W= "
250 << Translation->GetUVW(1) << ", "
251 << Translation->GetUVW(2) << ", "
252 << Translation->GetUVW(3) << " ft/s");
255 SG_LOG(SG_FLIGHT,SG_INFO, " Mach: "
256 << Translation->GetMach() );
260 SG_LOG(SG_FLIGHT,SG_INFO, " Indicated Airspeed: "
261 << Auxiliary->GetVcalibratedKTS() << " knots" );
265 stall_warning->setDoubleValue(0);
267 SG_LOG( SG_FLIGHT, SG_INFO, " Bank Angle: "
268 << Rotation->Getphi()*RADTODEG << " deg" );
269 SG_LOG( SG_FLIGHT, SG_INFO, " Pitch Angle: "
270 << Rotation->Gettht()*RADTODEG << " deg" );
271 SG_LOG( SG_FLIGHT, SG_INFO, " True Heading: "
272 << Rotation->Getpsi()*RADTODEG << " deg" );
273 SG_LOG( SG_FLIGHT, SG_INFO, " Latitude: "
274 << Position->GetLatitude() << " deg" );
275 SG_LOG( SG_FLIGHT, SG_INFO, " Longitude: "
276 << Position->GetLongitude() << " deg" );
277 SG_LOG( SG_FLIGHT, SG_INFO, " Altitude: "
278 << Position->Geth() << " feet" );
279 SG_LOG( SG_FLIGHT, SG_INFO, " loaded initial conditions" );
281 SG_LOG( SG_FLIGHT, SG_INFO, " set dt" );
283 SG_LOG( SG_FLIGHT, SG_INFO, "Finished initializing JSBSim" );
285 SG_LOG( SG_FLIGHT, SG_INFO, "FGControls::get_gear_down()= " <<
286 globals->get_controls()->get_gear_down() );
292 /******************************************************************************/
294 // Run an iteration of the EOM (equations of motion)
297 FGJSBsim::update( double dt ) {
302 int multiloop = _calc_multiloop(dt);
306 // double save_alt = 0.0;
310 trimmed->setBoolValue(false);
313 if ( startup_trim->getBoolValue() ) {
314 SG_LOG(SG_FLIGHT, SG_INFO,
315 "Ready to trim, terrain altitude is: "
316 << cur_fdm_state->get_Runway_altitude() * SG_METER_TO_FEET );
317 fgic->SetTerrainAltitudeFtIC( cur_fdm_state->get_ground_elev_ft() );
320 fdmex->RunIC(); //apply any changes made through the set_ functions
325 for ( i=0; i < multiloop; i++ ) {
329 FGJSBBase::Message* msg;
330 while (fdmex->ReadMessage()) {
331 msg = fdmex->ProcessMessage();
333 case FGJSBBase::Message::eText:
334 SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text );
336 case FGJSBBase::Message::eBool:
337 SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->bVal );
339 case FGJSBBase::Message::eInteger:
340 SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->iVal );
342 case FGJSBBase::Message::eDouble:
343 SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->dVal );
346 SG_LOG( SG_FLIGHT, SG_INFO, "Unrecognized message type." );
351 // translate JSBsim back to FG structure so that the
352 // autopilot (and the rest of the sim can use the updated values
356 /******************************************************************************/
358 // Convert from the FGInterface struct to the JSBsim generic_ struct
360 bool FGJSBsim::copy_to_JSBsim() {
363 // copy control positions into the JSBsim structure
365 FCS->SetDaCmd( globals->get_controls()->get_aileron());
366 FCS->SetRollTrimCmd( globals->get_controls()->get_aileron_trim() );
367 FCS->SetDeCmd( globals->get_controls()->get_elevator());
368 FCS->SetPitchTrimCmd( globals->get_controls()->get_elevator_trim() );
369 FCS->SetDrCmd( -globals->get_controls()->get_rudder() );
370 FCS->SetYawTrimCmd( -globals->get_controls()->get_rudder_trim() );
371 FCS->SetDfCmd( globals->get_controls()->get_flaps() );
372 FCS->SetDsbCmd( 0.0 ); //speedbrakes
373 FCS->SetDspCmd( 0.0 ); //spoilers
375 // Parking brake sets minimum braking
377 double parking_brake = globals->get_controls()->get_parking_brake();
378 FCS->SetLBrake(FMAX(globals->get_controls()->get_brake(0), parking_brake));
379 FCS->SetRBrake(FMAX(globals->get_controls()->get_brake(1), parking_brake));
380 FCS->SetCBrake( globals->get_controls()->get_brake( 2 ) );
382 FCS->SetGearCmd( globals->get_controls()->get_gear_down());
383 for (i = 0; i < Propulsion->GetNumEngines(); i++) {
384 FGEngine * eng = Propulsion->GetEngine(i);
385 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
386 FCS->SetThrottleCmd(i, globals->get_controls()->get_throttle(i));
387 FCS->SetMixtureCmd(i, globals->get_controls()->get_mixture(i));
388 FCS->SetPropAdvanceCmd(i, globals->get_controls()->get_prop_advance(i));
389 Propulsion->GetThruster(i)->SetRPM(node->getDoubleValue("rpm"));
390 eng->SetMagnetos( globals->get_controls()->get_magnetos(i) );
391 eng->SetStarter( globals->get_controls()->get_starter(i) );
394 _set_Runway_altitude( cur_fdm_state->get_Runway_altitude() );
395 Position->SetSeaLevelRadius( get_Sea_level_radius() );
396 Position->SetRunwayRadius( get_Runway_altitude()
397 + get_Sea_level_radius() );
399 Atmosphere->SetExTemperature(
400 9.0/5.0*(temperature->getDoubleValue()+273.15) );
401 Atmosphere->SetExPressure(pressure->getDoubleValue()*70.726566);
402 Atmosphere->SetExDensity(density->getDoubleValue());
403 Atmosphere->SetTurbGain(turbulence->getDoubleValue()*100.0);
405 Atmosphere->SetWindNED( wind_from_north->getDoubleValue(),
406 wind_from_east->getDoubleValue(),
407 wind_from_down->getDoubleValue() );
408 // SG_LOG(SG_FLIGHT,SG_INFO, "Wind NED: "
409 // << get_V_north_airmass() << ", "
410 // << get_V_east_airmass() << ", "
411 // << get_V_down_airmass() );
413 for (i = 0; i < Propulsion->GetNumTanks(); i++) {
414 SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
415 FGTank * tank = Propulsion->GetTank(i);
416 tank->SetContents(node->getDoubleValue("level-gal_us") * 6.6);
417 // tank->SetContents(node->getDoubleValue("level-lb"));
423 /******************************************************************************/
425 // Convert from the JSBsim generic_ struct to the FGInterface struct
427 bool FGJSBsim::copy_from_JSBsim() {
430 _set_Inertias( MassBalance->GetMass(),
431 MassBalance->GetIxx(),
432 MassBalance->GetIyy(),
433 MassBalance->GetIzz(),
434 MassBalance->GetIxz() );
436 _set_CG_Position( MassBalance->GetXYZcg(1),
437 MassBalance->GetXYZcg(2),
438 MassBalance->GetXYZcg(3) );
440 _set_Accels_Body( Aircraft->GetBodyAccel()(1),
441 Aircraft->GetBodyAccel()(2),
442 Aircraft->GetBodyAccel()(3) );
444 //_set_Accels_CG_Body( Aircraft->GetBodyAccel()(1),
445 // Aircraft->GetBodyAccel()(2),
446 // Aircraft->GetBodyAccel()(3) );
448 _set_Accels_CG_Body_N ( Aircraft->GetNcg()(1),
449 Aircraft->GetNcg()(2),
450 Aircraft->GetNcg()(3) );
452 _set_Accels_Pilot_Body( Auxiliary->GetPilotAccel()(1),
453 Auxiliary->GetPilotAccel()(2),
454 Auxiliary->GetPilotAccel()(3) );
456 // _set_Accels_Pilot_Body_N( Auxiliary->GetPilotAccel()(1)/32.1739,
457 // Auxiliary->GetNpilot(2)/32.1739,
458 // Auxiliary->GetNpilot(3)/32.1739 );
460 _set_Nlf( Aircraft->GetNlf() );
464 _set_Velocities_Local( Position->GetVn(),
468 _set_Velocities_Wind_Body( Translation->GetUVW(1),
469 Translation->GetUVW(2),
470 Translation->GetUVW(3) );
472 _set_V_rel_wind( Translation->GetVt() );
474 _set_V_equiv_kts( Auxiliary->GetVequivalentKTS() );
476 // _set_V_calibrated( Auxiliary->GetVcalibratedFPS() );
478 _set_V_calibrated_kts( Auxiliary->GetVcalibratedKTS() );
480 _set_V_ground_speed( Position->GetVground() );
482 _set_Omega_Body( Rotation->GetPQR(1),
484 Rotation->GetPQR(3) );
486 _set_Euler_Rates( Rotation->GetEulerRates(1),
487 Rotation->GetEulerRates(2),
488 Rotation->GetEulerRates(3) );
490 _set_Geocentric_Rates(Position->GetLatitudeDot(),
491 Position->GetLongitudeDot(),
492 Position->Gethdot() );
494 _set_Mach_number( Translation->GetMach() );
497 _updateGeocentricPosition( Position->GetLatitude(),
498 Position->GetLongitude(),
501 _set_Altitude_AGL( Position->GetDistanceAGL() );
503 _set_Euler_Angles( Rotation->Getphi(),
505 Rotation->Getpsi() );
507 _set_Alpha( Translation->Getalpha() );
508 _set_Beta( Translation->Getbeta() );
511 _set_Gamma_vert_rad( Position->GetGamma() );
512 // set_Gamma_horiz_rad( Gamma_horiz_rad );
514 _set_Earth_position_angle( Auxiliary->GetEarthPositionAngle() );
516 _set_Climb_Rate( Position->Gethdot() );
519 for ( i = 1; i <= 3; i++ ) {
520 for ( j = 1; j <= 3; j++ ) {
521 _set_T_Local_to_Body( i, j, State->GetTl2b(i,j) );
525 // Copy the engine values from JSBSim.
526 for( i=0; i < Propulsion->GetNumEngines(); i++ ) {
527 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
528 FGEngine * eng = Propulsion->GetEngine(i);
529 FGThruster * thrust = Propulsion->GetThruster(i);
531 node->setDoubleValue("mp-osi", eng->getManifoldPressure_inHg());
532 node->setDoubleValue("rpm", thrust->GetRPM());
533 node->setDoubleValue("egt-degf", eng->getExhaustGasTemp_degF());
534 node->setDoubleValue("fuel-flow-gph", eng->getFuelFlow_gph());
535 node->setDoubleValue("cht-degf", eng->getCylinderHeadTemp_degF());
536 node->setDoubleValue("oil-temperature-degf", eng->getOilTemp_degF());
537 node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi());
538 node->setBoolValue("running", eng->GetRunning());
539 node->setBoolValue("cranking", eng->GetCranking());
542 static const SGPropertyNode *fuel_freeze
543 = fgGetNode("/sim/freeze/fuel");
545 // Copy the fuel levels from JSBSim if fuel
546 // freeze not enabled.
547 if ( ! fuel_freeze->getBoolValue() ) {
548 for (i = 0; i < Propulsion->GetNumTanks(); i++) {
549 SGPropertyNode * node
550 = fgGetNode("/consumables/fuel/tank", i, true);
551 double contents = Propulsion->GetTank(i)->GetContents();
552 node->setDoubleValue("level-gal_us", contents/6.6);
553 // node->setDoubleValue("level-lb", contents);
559 stall_warning->setDoubleValue( Aerodynamics->GetStallWarn() );
561 /* elevator_pos_deg->setDoubleValue( FCS->GetDePos()*SG_RADIANS_TO_DEGREES );
562 left_aileron_pos_deg->setDoubleValue( FCS->GetDaLPos()*SG_RADIANS_TO_DEGREES );
563 right_aileron_pos_deg->setDoubleValue( FCS->GetDaRPos()*SG_RADIANS_TO_DEGREES );
564 rudder_pos_deg->setDoubleValue( -1*FCS->GetDrPos()*SG_RADIANS_TO_DEGREES );
565 flap_pos_deg->setDoubleValue( FCS->GetDfPos() ); */
568 elevator_pos_pct->setDoubleValue( FCS->GetDePos(ofNorm) );
569 left_aileron_pos_pct->setDoubleValue( FCS->GetDaLPos(ofNorm) );
570 right_aileron_pos_pct->setDoubleValue( -1*FCS->GetDaLPos(ofNorm) );
571 rudder_pos_pct->setDoubleValue( -1*FCS->GetDrPos(ofNorm) );
572 flap_pos_pct->setDoubleValue( FCS->GetDfPos(ofNorm) );
578 bool FGJSBsim::ToggleDataLogging(void) {
579 return fdmex->GetOutput()->Toggle();
583 bool FGJSBsim::ToggleDataLogging(bool state) {
585 fdmex->GetOutput()->Enable();
588 fdmex->GetOutput()->Disable();
595 void FGJSBsim::set_Latitude(double lat) {
596 static const SGPropertyNode *altitude = fgGetNode("/position/altitude-ft");
598 double sea_level_radius_meters, lat_geoc;
600 if ( altitude->getDoubleValue() > -9990 ) {
601 alt = altitude->getDoubleValue();
607 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat );
608 SG_LOG(SG_FLIGHT,SG_INFO," cur alt (ft) = " << alt );
610 sgGeodToGeoc( lat, alt * SG_FEET_TO_METER,
611 &sea_level_radius_meters, &lat_geoc );
612 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
613 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
614 _set_Runway_altitude( cur_fdm_state->get_Runway_altitude() );
615 fgic->SetTerrainAltitudeFtIC( cur_fdm_state->get_ground_elev_ft() );
616 fgic->SetLatitudeRadIC( lat_geoc );
620 void FGJSBsim::set_Longitude(double lon) {
622 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon );
624 fgic->SetLongitudeRadIC( lon );
625 _set_Runway_altitude( cur_fdm_state->get_Runway_altitude() );
626 fgic->SetTerrainAltitudeFtIC( cur_fdm_state->get_ground_elev_ft() );
630 void FGJSBsim::set_Altitude(double alt) {
631 static const SGPropertyNode *latitude = fgGetNode("/position/latitude-deg");
633 double sea_level_radius_meters,lat_geoc;
635 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt );
636 SG_LOG(SG_FLIGHT,SG_INFO, " lat (deg) = " << latitude->getDoubleValue() );
639 sgGeodToGeoc( latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS, alt,
640 &sea_level_radius_meters, &lat_geoc);
641 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
642 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
643 _set_Runway_altitude( cur_fdm_state->get_Runway_altitude() );
644 fgic->SetTerrainAltitudeFtIC( cur_fdm_state->get_ground_elev_ft() );
645 SG_LOG(SG_FLIGHT, SG_INFO,
646 "Terrain altitude: " << cur_fdm_state->get_Runway_altitude() * SG_METER_TO_FEET );
647 fgic->SetLatitudeRadIC( lat_geoc );
648 fgic->SetAltitudeFtIC(alt);
652 void FGJSBsim::set_V_calibrated_kts(double vc) {
653 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " << vc );
656 fgic->SetVcalibratedKtsIC(vc);
660 void FGJSBsim::set_Mach_number(double mach) {
661 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " << mach );
664 fgic->SetMachIC(mach);
668 void FGJSBsim::set_Velocities_Local( double north, double east, double down ){
669 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: "
670 << north << ", " << east << ", " << down );
673 fgic->SetVnorthFpsIC(north);
674 fgic->SetVeastFpsIC(east);
675 fgic->SetVdownFpsIC(down);
679 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w){
680 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
681 << u << ", " << v << ", " << w );
684 fgic->SetUBodyFpsIC(u);
685 fgic->SetVBodyFpsIC(v);
686 fgic->SetWBodyFpsIC(w);
691 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi ) {
692 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: "
693 << phi << ", " << theta << ", " << psi );
696 fgic->SetPitchAngleRadIC(theta);
697 fgic->SetRollAngleRadIC(phi);
698 fgic->SetTrueHeadingRadIC(psi);
703 void FGJSBsim::set_Climb_Rate( double roc) {
704 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
707 //since both climb rate and flight path angle are set in the FG
708 //startup sequence, something is needed to keep one from cancelling
710 if( !(fabs(roc) > 1 && fabs(fgic->GetFlightPathAngleRadIC()) < 0.01) ) {
711 fgic->SetClimbRateFpsIC(roc);
716 void FGJSBsim::set_Gamma_vert_rad( double gamma) {
717 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
720 if( !(fabs(gamma) < 0.01 && fabs(fgic->GetClimbRateFpsIC()) > 1) ) {
721 fgic->SetFlightPathAngleRadIC(gamma);
726 void FGJSBsim::init_gear(void ) {
728 FGGroundReactions* gr=fdmex->GetGroundReactions();
729 int Ngear=GroundReactions->GetNumGearUnits();
730 for (int i=0;i<Ngear;i++) {
731 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
732 node->setDoubleValue("xoffset-in",
733 gr->GetGearUnit(i)->GetBodyLocation()(1));
734 node->setDoubleValue("yoffset-in",
735 gr->GetGearUnit(i)->GetBodyLocation()(2));
736 node->setDoubleValue("zoffset-in",
737 gr->GetGearUnit(i)->GetBodyLocation()(3));
738 node->setBoolValue("wow", gr->GetGearUnit(i)->GetWOW());
739 node->setBoolValue("has-brake", gr->GetGearUnit(i)->GetBrakeGroup() > 0);
740 node->setDoubleValue("position-norm", FCS->GetGearPos());
744 void FGJSBsim::update_gear(void) {
746 FGGroundReactions* gr=fdmex->GetGroundReactions();
747 int Ngear=GroundReactions->GetNumGearUnits();
748 for (int i=0;i<Ngear;i++) {
749 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
750 node->getChild("wow", 0, true)
751 ->setBoolValue(gr->GetGearUnit(i)->GetWOW());
752 node->getChild("position-norm", 0, true)
753 ->setDoubleValue(FCS->GetGearPos());
757 void FGJSBsim::do_trim(void) {
760 if( fgGetBool("/sim/presets/onground") ) {
761 fgic->SetVcalibratedKtsIC(0.0);
762 fgtrim=new FGTrim(fdmex,tGround);
764 fgtrim=new FGTrim(fdmex,tLongitudinal);
766 if( !fgtrim->DoTrim() ) {
770 trimmed->setBoolValue(true);
772 State->ReportState();
774 pitch_trim->setDoubleValue( FCS->GetPitchTrimCmd() );
775 throttle_trim->setDoubleValue( FCS->GetThrottleCmd(0) );
776 aileron_trim->setDoubleValue( FCS->GetDaCmd() );
777 rudder_trim->setDoubleValue( FCS->GetDrCmd() );
779 globals->get_controls()->set_elevator_trim(FCS->GetPitchTrimCmd());
780 globals->get_controls()->set_elevator(FCS->GetDeCmd());
781 globals->get_controls()->set_throttle(FGControls::ALL_ENGINES,
782 FCS->GetThrottleCmd(0));
784 globals->get_controls()->set_aileron(FCS->GetDaCmd());
785 globals->get_controls()->set_rudder( FCS->GetDrCmd());
787 SG_LOG( SG_FLIGHT, SG_INFO, " Trim complete" );
790 void FGJSBsim::update_ic(void) {
792 fgic->SetLatitudeRadIC(get_Lat_geocentric() );
793 fgic->SetLongitudeRadIC( get_Longitude() );
794 fgic->SetAltitudeFtIC( get_Altitude() );
795 fgic->SetVcalibratedKtsIC( get_V_calibrated_kts() );
796 fgic->SetPitchAngleRadIC( get_Theta() );
797 fgic->SetRollAngleRadIC( get_Phi() );
798 fgic->SetTrueHeadingRadIC( get_Psi() );
799 fgic->SetClimbRateFpsIC( get_Climb_Rate() );