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.
24 #include <simgear/compiler.h>
26 #ifdef SG_MATH_EXCEPTION_CLASH
32 #include <simgear/constants.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/math/sg_geodesy.hxx>
35 #include <simgear/misc/sg_path.hxx>
37 #include <Scenery/scenery.hxx>
39 #include <Aircraft/aircraft.hxx>
40 #include <Controls/controls.hxx>
41 #include <Main/globals.hxx>
42 #include <Main/fg_props.hxx>
44 #include <FDM/JSBSim/FGFDMExec.h>
45 #include <FDM/JSBSim/FGAircraft.h>
46 #include <FDM/JSBSim/FGFCS.h>
47 #include <FDM/JSBSim/FGPosition.h>
48 #include <FDM/JSBSim/FGRotation.h>
49 #include <FDM/JSBSim/FGState.h>
50 #include <FDM/JSBSim/FGTranslation.h>
51 #include <FDM/JSBSim/FGAuxiliary.h>
52 #include <FDM/JSBSim/FGInitialCondition.h>
53 #include <FDM/JSBSim/FGTrim.h>
54 #include <FDM/JSBSim/FGAtmosphere.h>
55 #include <FDM/JSBSim/FGMassBalance.h>
56 #include <FDM/JSBSim/FGAerodynamics.h>
59 /******************************************************************************/
61 FGJSBsim::FGJSBsim( double dt )
66 fdmex = new FGFDMExec;
68 State = fdmex->GetState();
69 Atmosphere = fdmex->GetAtmosphere();
70 FCS = fdmex->GetFCS();
71 MassBalance = fdmex->GetMassBalance();
72 Propulsion = fdmex->GetPropulsion();
73 Aircraft = fdmex->GetAircraft();
74 Translation = fdmex->GetTranslation();
75 Rotation = fdmex->GetRotation();
76 Position = fdmex->GetPosition();
77 Auxiliary = fdmex->GetAuxiliary();
78 Aerodynamics = fdmex->GetAerodynamics();
79 GroundReactions = fdmex->GetGroundReactions();
82 Atmosphere->UseInternal();
84 fgic=new FGInitialCondition(fdmex);
87 SGPath aircraft_path( globals->get_fg_root() );
88 aircraft_path.append( "Aircraft" );
90 SGPath engine_path( globals->get_fg_root() );
91 engine_path.append( "Engine" );
95 result = fdmex->LoadModel( aircraft_path.str(),
97 fgGetString("/sim/aircraft") );
100 SG_LOG( SG_FLIGHT, SG_INFO, " loaded aircraft.");
102 SG_LOG( SG_FLIGHT, SG_INFO,
103 " aircraft does not exist (you may have mis-typed the name).");
107 SG_LOG( SG_FLIGHT, SG_INFO, "" );
108 SG_LOG( SG_FLIGHT, SG_INFO, "" );
109 SG_LOG( SG_FLIGHT, SG_INFO, "After loading aircraft definition file ..." );
111 int Neng = Propulsion->GetNumEngines();
112 SG_LOG( SG_FLIGHT, SG_INFO, "num engines = " << Neng );
113 for(int i=0;i<Neng;i++) {
114 add_engine( FGEngInterface() );
117 if ( GroundReactions->GetNumGearUnits() <= 0 ) {
118 SG_LOG( SG_FLIGHT, SG_ALERT, "num gear units = "
119 << GroundReactions->GetNumGearUnits() );
120 SG_LOG( SG_FLIGHT, SG_ALERT, "This is a very bad thing because with 0 gear units, the ground trimming");
121 SG_LOG( SG_FLIGHT, SG_ALERT, "routine (coming up later in the code) will core dump.");
122 SG_LOG( SG_FLIGHT, SG_ALERT, "Halting the sim now, and hoping a solution will present itself soon!");
129 fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
130 fgSetDouble("/fdm/trim/throttle", FCS->GetThrottleCmd(0));
131 fgSetDouble("/fdm/trim/aileron", FCS->GetDaCmd());
132 fgSetDouble("/fdm/trim/rudder", FCS->GetDrCmd());
134 startup_trim = fgGetNode("/sim/startup/trim", true);
136 trimmed = fgGetNode("/fdm/trim/trimmed", true);
137 trimmed->setBoolValue(false);
139 pitch_trim = fgGetNode("/fdm/trim/pitch-trim", true );
140 throttle_trim = fgGetNode("/fdm/trim/throttle", true );
141 aileron_trim = fgGetNode("/fdm/trim/aileron", true );
142 rudder_trim = fgGetNode("/fdm/trim/rudder", true );
145 stall_warning = fgGetNode("/sim/aircraft/alarms/stall-warning",true);
146 stall_warning->setDoubleValue(0);
149 /******************************************************************************/
150 FGJSBsim::~FGJSBsim(void) {
152 delete fdmex; fdmex=NULL;
153 delete fgic; fgic=NULL;
157 /******************************************************************************/
159 // Initialize the JSBsim flight model, dt is the time increment for
160 // each subsequent iteration through the EOM
162 void FGJSBsim::init() {
164 SG_LOG( SG_FLIGHT, SG_INFO, "Starting and initializing JSBsim" );
166 // Explicitly call the superclass's
167 // init method first.
170 fdmex->GetState()->Initialize(fgic);
171 fdmex->RunIC(fgic); //loop JSBSim once w/o integrating
172 // fdmex->Run(); //loop JSBSim once
173 copy_from_JSBsim(); //update the bus
175 SG_LOG( SG_FLIGHT, SG_INFO, " Initialized JSBSim with:" );
177 switch(fgic->GetSpeedSet()) {
179 SG_LOG(SG_FLIGHT,SG_INFO, " Vn,Ve,Vd= "
180 << Position->GetVn() << ", "
181 << Position->GetVe() << ", "
182 << Position->GetVd() << " ft/s");
185 SG_LOG(SG_FLIGHT,SG_INFO, " U,V,W= "
186 << Translation->GetUVW(1) << ", "
187 << Translation->GetUVW(2) << ", "
188 << Translation->GetUVW(3) << " ft/s");
191 SG_LOG(SG_FLIGHT,SG_INFO, " Mach: "
192 << Translation->GetMach() );
196 SG_LOG(SG_FLIGHT,SG_INFO, " Indicated Airspeed: "
197 << Auxiliary->GetVcalibratedKTS() << " knots" );
201 stall_warning->setBoolValue(false);
203 SG_LOG( SG_FLIGHT, SG_INFO, " Bank Angle: "
204 << Rotation->Getphi()*RADTODEG << " deg" );
205 SG_LOG( SG_FLIGHT, SG_INFO, " Pitch Angle: "
206 << Rotation->Gettht()*RADTODEG << " deg" );
207 SG_LOG( SG_FLIGHT, SG_INFO, " True Heading: "
208 << Rotation->Getpsi()*RADTODEG << " deg" );
209 SG_LOG( SG_FLIGHT, SG_INFO, " Latitude: "
210 << Position->GetLatitude() << " deg" );
211 SG_LOG( SG_FLIGHT, SG_INFO, " Longitude: "
212 << Position->GetLongitude() << " deg" );
213 SG_LOG( SG_FLIGHT, SG_INFO, " Altitude: "
214 << Position->Geth() << " feet" );
215 SG_LOG( SG_FLIGHT, SG_INFO, " loaded initial conditions" );
217 SG_LOG( SG_FLIGHT, SG_INFO, " set dt" );
219 SG_LOG( SG_FLIGHT, SG_INFO, "Finished initializing JSBSim" );
225 /******************************************************************************/
227 // Run an iteration of the EOM (equations of motion)
229 bool FGJSBsim::update( int multiloop ) {
233 double save_alt = 0.0;
237 trimmed->setBoolValue(false);
240 if ( startup_trim->getBoolValue() ) {
243 fdmex->RunIC(fgic); //apply any changes made through the set_ functions
248 for( i=0; i<get_num_engines(); i++ ) {
249 FGEngInterface * e = get_engine(i);
250 FGEngine * eng = Propulsion->GetEngine(i);
251 FGThruster * thrust = Propulsion->GetThruster(i);
252 eng->SetMagnetos( globals->get_controls()->get_magnetos(i) );
253 eng->SetStarter( globals->get_controls()->get_starter(i) );
254 e->set_Throttle( globals->get_controls()->get_throttle(i) );
258 for ( i=0; i < multiloop; i++ ) {
262 for( i=0; i<get_num_engines(); i++ ) {
263 FGEngInterface * e = get_engine(i);
264 FGEngine * eng = Propulsion->GetEngine(i);
265 FGThruster * thrust = Propulsion->GetThruster(i);
266 e->set_Manifold_Pressure( eng->getManifoldPressure_inHg() );
267 e->set_RPM( thrust->GetRPM() );
268 e->set_EGT( eng->getExhaustGasTemp_degF() );
269 e->set_CHT( eng->getCylinderHeadTemp_degF() );
270 e->set_Oil_Temp( eng->getOilTemp_degF() );
271 e->set_Running_Flag( eng->GetRunning() );
272 e->set_Cranking_Flag( eng->GetCranking() );
278 stall_warning->setDoubleValue( Aircraft->GetStallWarn() );
280 // translate JSBsim back to FG structure so that the
281 // autopilot (and the rest of the sim can use the updated values
286 /******************************************************************************/
288 // Convert from the FGInterface struct to the JSBsim generic_ struct
290 bool FGJSBsim::copy_to_JSBsim() {
291 // copy control positions into the JSBsim structure
293 FCS->SetDaCmd( globals->get_controls()->get_aileron());
294 FCS->SetDeCmd( globals->get_controls()->get_elevator());
295 FCS->SetPitchTrimCmd(globals->get_controls()->get_elevator_trim());
296 FCS->SetDrCmd( -globals->get_controls()->get_rudder());
297 FCS->SetDfCmd( globals->get_controls()->get_flaps() );
298 FCS->SetDsbCmd( 0.0 ); //speedbrakes
299 FCS->SetDspCmd( 0.0 ); //spoilers
300 FCS->SetLBrake( globals->get_controls()->get_brake( 0 ) );
301 FCS->SetRBrake( globals->get_controls()->get_brake( 1 ) );
302 FCS->SetCBrake( globals->get_controls()->get_brake( 2 ) );
303 for (int i = 0; i < get_num_engines(); i++) {
304 FCS->SetThrottleCmd(i, globals->get_controls()->get_throttle(i));
305 FCS->SetMixtureCmd(i, globals->get_controls()->get_mixture(i));
308 Position->SetSeaLevelRadius( get_Sea_level_radius() );
309 Position->SetRunwayRadius( scenery.get_cur_elev()*SG_METER_TO_FEET
310 + get_Sea_level_radius() );
312 Atmosphere->SetExTemperature(get_Static_temperature());
313 Atmosphere->SetExPressure(get_Static_pressure());
314 Atmosphere->SetExDensity(get_Density());
315 Atmosphere->SetWindNED(get_V_north_airmass(),
316 get_V_east_airmass(),
317 get_V_down_airmass());
318 // SG_LOG(SG_FLIGHT,SG_INFO, "Wind NED: "
319 // << get_V_north_airmass() << ", "
320 // << get_V_east_airmass() << ", "
321 // << get_V_down_airmass() );
326 /******************************************************************************/
328 // Convert from the JSBsim generic_ struct to the FGInterface struct
330 bool FGJSBsim::copy_from_JSBsim() {
333 _set_Inertias( MassBalance->GetMass(),
334 MassBalance->GetIxx(),
335 MassBalance->GetIyy(),
336 MassBalance->GetIzz(),
337 MassBalance->GetIxz() );
339 _set_CG_Position( MassBalance->GetXYZcg(1),
340 MassBalance->GetXYZcg(2),
341 MassBalance->GetXYZcg(3) );
343 _set_Accels_Body( Aircraft->GetBodyAccel()(1),
344 Aircraft->GetBodyAccel()(2),
345 Aircraft->GetBodyAccel()(3) );
347 //_set_Accels_CG_Body( Aircraft->GetBodyAccel()(1),
348 // Aircraft->GetBodyAccel()(2),
349 // Aircraft->GetBodyAccel()(3) );
351 _set_Accels_CG_Body_N ( Aircraft->GetNcg()(1),
352 Aircraft->GetNcg()(2),
353 Aircraft->GetNcg()(3) );
355 _set_Accels_Pilot_Body( Auxiliary->GetPilotAccel()(1),
356 Auxiliary->GetPilotAccel()(2),
357 Auxiliary->GetPilotAccel()(3) );
359 // _set_Accels_Pilot_Body_N( Auxiliary->GetPilotAccel()(1)/32.1739,
360 // Auxiliary->GetNpilot(2)/32.1739,
361 // Auxiliary->GetNpilot(3)/32.1739 );
363 _set_Nlf( Aerodynamics->GetNlf() );
367 _set_Velocities_Local( Position->GetVn(),
371 _set_Velocities_Wind_Body( Translation->GetUVW(1),
372 Translation->GetUVW(2),
373 Translation->GetUVW(3) );
375 _set_V_rel_wind( Translation->GetVt() );
377 _set_V_equiv_kts( Auxiliary->GetVequivalentKTS() );
379 // _set_V_calibrated( Auxiliary->GetVcalibratedFPS() );
381 _set_V_calibrated_kts( Auxiliary->GetVcalibratedKTS() );
383 _set_V_ground_speed( Position->GetVground() );
385 _set_Omega_Body( Rotation->GetPQR(1),
387 Rotation->GetPQR(3) );
389 _set_Euler_Rates( Rotation->GetEulerRates(1),
390 Rotation->GetEulerRates(2),
391 Rotation->GetEulerRates(3) );
393 _set_Geocentric_Rates(Position->GetLatitudeDot(),
394 Position->GetLongitudeDot(),
395 Position->Gethdot() );
397 _set_Mach_number( Translation->GetMach() );
400 _updatePosition( Position->GetLatitude(),
401 Position->GetLongitude(),
404 _set_Altitude_AGL( Position->GetDistanceAGL() );
406 _set_Euler_Angles( Rotation->Getphi(),
408 Rotation->Getpsi() );
410 _set_Alpha( Translation->Getalpha() );
411 _set_Beta( Translation->Getbeta() );
414 _set_Gamma_vert_rad( Position->GetGamma() );
415 // set_Gamma_horiz_rad( Gamma_horiz_rad );
417 _set_Earth_position_angle( Auxiliary->GetEarthPositionAngle() );
419 _set_Climb_Rate( Position->Gethdot() );
422 for ( i = 1; i <= 3; i++ ) {
423 for ( j = 1; j <= 3; j++ ) {
424 _set_T_Local_to_Body( i, j, State->GetTl2b(i,j) );
430 bool FGJSBsim::ToggleDataLogging(void) {
431 return fdmex->GetOutput()->Toggle();
435 bool FGJSBsim::ToggleDataLogging(bool state) {
437 fdmex->GetOutput()->Enable();
440 fdmex->GetOutput()->Disable();
447 void FGJSBsim::set_Latitude(double lat) {
448 static const SGPropertyNode *altitude
449 = fgGetNode("/position/altitude-ft");
451 if ( altitude->getDoubleValue() > -9990 ) {
452 alt = altitude->getDoubleValue();
457 double sea_level_radius_meters, lat_geoc;
459 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat );
460 SG_LOG(SG_FLIGHT,SG_INFO," cur alt (ft) = " << alt );
462 sgGeodToGeoc( lat, alt * SG_FEET_TO_METER,
463 &sea_level_radius_meters, &lat_geoc );
465 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
466 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
467 fgic->SetLatitudeRadIC( lat_geoc );
471 void FGJSBsim::set_Longitude(double lon) {
473 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon );
475 fgic->SetLongitudeRadIC( lon );
479 void FGJSBsim::set_Altitude(double alt) {
480 static const SGPropertyNode *latitude
481 = fgGetNode("/position/latitude-deg");
483 double sea_level_radius_meters,lat_geoc;
485 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt );
486 SG_LOG(SG_FLIGHT,SG_INFO, " lat (deg) = " << latitude->getDoubleValue() );
488 sgGeodToGeoc( latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS, alt,
489 &sea_level_radius_meters, &lat_geoc);
490 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
491 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
492 fgic->SetLatitudeRadIC( lat_geoc );
493 fgic->SetAltitudeFtIC(alt);
497 void FGJSBsim::set_V_calibrated_kts(double vc) {
498 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " << vc );
500 fgic->SetVcalibratedKtsIC(vc);
504 void FGJSBsim::set_Mach_number(double mach) {
505 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " << mach );
507 fgic->SetMachIC(mach);
511 void FGJSBsim::set_Velocities_Local( double north, double east, double down ){
512 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: "
513 << north << ", " << east << ", " << down );
515 fgic->SetVnorthFpsIC(north);
516 fgic->SetVeastFpsIC(east);
517 fgic->SetVdownFpsIC(down);
521 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w){
522 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
523 << u << ", " << v << ", " << w );
525 fgic->SetUBodyFpsIC(u);
526 fgic->SetVBodyFpsIC(v);
527 fgic->SetWBodyFpsIC(w);
532 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi ) {
533 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: "
534 << phi << ", " << theta << ", " << psi );
536 fgic->SetPitchAngleRadIC(theta);
537 fgic->SetRollAngleRadIC(phi);
538 fgic->SetTrueHeadingRadIC(psi);
543 void FGJSBsim::set_Climb_Rate( double roc) {
544 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
546 fgic->SetClimbRateFpsIC(roc);
550 void FGJSBsim::set_Gamma_vert_rad( double gamma) {
551 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
553 fgic->SetFlightPathAngleRadIC(gamma);
558 void FGJSBsim::set_Sea_level_radius(double slr) {
559 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Sea_level_radius: " << slr );
561 fgic->SetSeaLevelRadiusFtIC(slr);
565 void FGJSBsim::set_Runway_altitude(double ralt) {
566 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Runway_altitude: " << ralt );
568 _set_Runway_altitude( ralt );
569 fgic->SetTerrainAltitudeFtIC( ralt );
573 void FGJSBsim::set_Static_pressure(double p) {
574 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_pressure: " << p );
576 Atmosphere->SetExPressure(p);
577 if(Atmosphere->External() == true)
581 void FGJSBsim::set_Static_temperature(double T) {
582 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_temperature: " << T );
584 Atmosphere->SetExTemperature(T);
585 if(Atmosphere->External() == true)
590 void FGJSBsim::set_Density(double rho) {
591 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Density: " << rho );
593 Atmosphere->SetExDensity(rho);
594 if(Atmosphere->External() == true)
598 void FGJSBsim::set_Velocities_Local_Airmass (double wnorth,
601 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local_Airmass: "
602 << wnorth << ", " << weast << ", " << wdown );
604 _set_Velocities_Local_Airmass( wnorth, weast, wdown );
605 Atmosphere->SetWindNED(wnorth, weast, wdown );
606 if(Atmosphere->External() == true)
610 void FGJSBsim::init_gear(void ) {
612 FGGearInterface *gear;
613 FGGroundReactions* gr=fdmex->GetGroundReactions();
614 int Ngear=GroundReactions->GetNumGearUnits();
615 for (int i=0;i<Ngear;i++) {
616 add_gear_unit( FGGearInterface() );
617 gear=get_gear_unit(i);
618 gear->SetX( gr->GetGearUnit(i)->GetBodyLocation()(1) );
619 gear->SetY( gr->GetGearUnit(i)->GetBodyLocation()(2) );
620 gear->SetZ( gr->GetGearUnit(i)->GetBodyLocation()(3) );
621 gear->SetWoW( gr->GetGearUnit(i)->GetWOW() );
622 if ( gr->GetGearUnit(i)->GetBrakeGroup() > 0 ) {
623 gear->SetBrake(true);
625 if ( gr->GetGearUp() ) {
626 gear->SetPosition( 0.0 );
631 void FGJSBsim::update_gear(void) {
633 FGGearInterface* gear;
634 FGGroundReactions* gr=fdmex->GetGroundReactions();
635 int Ngear=GroundReactions->GetNumGearUnits();
636 for (int i=0;i<Ngear;i++) {
637 gear=get_gear_unit(i);
638 gear->SetWoW( gr->GetGearUnit(i)->GetWOW() );
639 if ( gr->GetGearUp() ) {
640 gear->SetPosition( 0.0 );
645 void FGJSBsim::do_trim(void) {
648 if(fgic->GetVcalibratedKtsIC() < 10 ) {
649 fgic->SetVcalibratedKtsIC(0.0);
650 fgtrim=new FGTrim(fdmex,fgic,tGround);
652 fgtrim=new FGTrim(fdmex,fgic,tLongitudinal);
654 if( !fgtrim->DoTrim() ) {
658 trimmed->setBoolValue(true);
660 State->ReportState();
662 pitch_trim->setDoubleValue( FCS->GetPitchTrimCmd() );
663 throttle_trim->setDoubleValue( FCS->GetThrottleCmd(0) );
664 aileron_trim->setDoubleValue( FCS->GetDaCmd() );
665 rudder_trim->setDoubleValue( FCS->GetDrCmd() );
667 globals->get_controls()->set_elevator_trim(FCS->GetPitchTrimCmd());
668 globals->get_controls()->set_elevator(FCS->GetDeCmd());
669 globals->get_controls()->set_throttle(FGControls::ALL_ENGINES,
670 FCS->GetThrottleCmd(0));
672 globals->get_controls()->set_aileron(FCS->GetDaCmd());
673 globals->get_controls()->set_rudder( FCS->GetDrCmd());
675 SG_LOG( SG_FLIGHT, SG_INFO, " Trim complete" );