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/FGDefs.h>
53 #include <FDM/JSBSim/FGInitialCondition.h>
54 #include <FDM/JSBSim/FGTrim.h>
55 #include <FDM/JSBSim/FGAtmosphere.h>
56 #include <FDM/JSBSim/FGMassBalance.h>
57 #include <FDM/JSBSim/FGAerodynamics.h>
60 /******************************************************************************/
62 FGJSBsim::FGJSBsim( double dt )
67 fdmex = new FGFDMExec;
69 State = fdmex->GetState();
70 Atmosphere = fdmex->GetAtmosphere();
71 FCS = fdmex->GetFCS();
72 MassBalance = fdmex->GetMassBalance();
73 Propulsion = fdmex->GetPropulsion();
74 Aircraft = fdmex->GetAircraft();
75 Translation = fdmex->GetTranslation();
76 Rotation = fdmex->GetRotation();
77 Position = fdmex->GetPosition();
78 Auxiliary = fdmex->GetAuxiliary();
79 Aerodynamics = fdmex->GetAerodynamics();
80 GroundReactions = fdmex->GetGroundReactions();
83 Atmosphere->UseInternal();
85 fgic=new FGInitialCondition(fdmex);
88 SGPath aircraft_path( globals->get_fg_root() );
89 aircraft_path.append( "Aircraft" );
91 SGPath engine_path( globals->get_fg_root() );
92 engine_path.append( "Engine" );
96 result = fdmex->LoadModel( aircraft_path.str(),
98 fgGetString("/sim/aircraft") );
101 SG_LOG( SG_FLIGHT, SG_INFO, " loaded aircraft.");
103 SG_LOG( SG_FLIGHT, SG_INFO,
104 " aircraft does not exist (you may have mis-typed the name).");
108 SG_LOG( SG_FLIGHT, SG_INFO, "" );
109 SG_LOG( SG_FLIGHT, SG_INFO, "" );
110 SG_LOG( SG_FLIGHT, SG_INFO, "After loading aircraft definition file ..." );
112 int Neng = Propulsion->GetNumEngines();
113 SG_LOG( SG_FLIGHT, SG_INFO, "num engines = " << Neng );
114 for(int i=0;i<Neng;i++) {
115 add_engine( FGEngInterface() );
118 if ( GroundReactions->GetNumGearUnits() <= 0 ) {
119 SG_LOG( SG_FLIGHT, SG_ALERT, "num gear units = "
120 << GroundReactions->GetNumGearUnits() );
121 SG_LOG( SG_FLIGHT, SG_ALERT, "This is a very bad thing because with 0 gear units, the ground trimming");
122 SG_LOG( SG_FLIGHT, SG_ALERT, "routine (coming up later in the code) will core dump.");
123 SG_LOG( SG_FLIGHT, SG_ALERT, "Halting the sim now, and hoping a solution will present itself soon!");
130 fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
131 fgSetDouble("/fdm/trim/throttle", FCS->GetThrottleCmd(0));
132 fgSetDouble("/fdm/trim/aileron", FCS->GetDaCmd());
133 fgSetDouble("/fdm/trim/rudder", FCS->GetDrCmd());
135 startup_trim = fgGetNode("/sim/startup/trim", true);
137 trimmed = fgGetNode("/fdm/trim/trimmed", true);
138 trimmed->setBoolValue(false);
140 pitch_trim = fgGetNode("/fdm/trim/pitch-trim", true );
141 throttle_trim = fgGetNode("/fdm/trim/throttle", true );
142 aileron_trim = fgGetNode("/fdm/trim/aileron", true );
143 rudder_trim = fgGetNode("/fdm/trim/rudder", true );
146 stall_warning = fgGetNode("/sim/aircraft/alarms/stall-warning",true);
147 stall_warning->setDoubleValue(0);
150 /******************************************************************************/
151 FGJSBsim::~FGJSBsim(void) {
153 delete fdmex; fdmex=NULL;
154 delete fgic; fgic=NULL;
158 /******************************************************************************/
160 // Initialize the JSBsim flight model, dt is the time increment for
161 // each subsequent iteration through the EOM
163 void FGJSBsim::init() {
165 SG_LOG( SG_FLIGHT, SG_INFO, "Starting and initializing JSBsim" );
167 // Explicitly call the superclass's
168 // init method first.
171 fdmex->GetState()->Initialize(fgic);
172 fdmex->RunIC(fgic); //loop JSBSim once w/o integrating
173 // fdmex->Run(); //loop JSBSim once
174 copy_from_JSBsim(); //update the bus
176 SG_LOG( SG_FLIGHT, SG_INFO, " Initialized JSBSim with:" );
178 switch(fgic->GetSpeedSet()) {
180 SG_LOG(SG_FLIGHT,SG_INFO, " Vn,Ve,Vd= "
181 << Position->GetVn() << ", "
182 << Position->GetVe() << ", "
183 << Position->GetVd() << " ft/s");
186 SG_LOG(SG_FLIGHT,SG_INFO, " U,V,W= "
187 << Translation->GetUVW(1) << ", "
188 << Translation->GetUVW(2) << ", "
189 << Translation->GetUVW(3) << " ft/s");
192 SG_LOG(SG_FLIGHT,SG_INFO, " Mach: "
193 << Translation->GetMach() );
197 SG_LOG(SG_FLIGHT,SG_INFO, " Indicated Airspeed: "
198 << Auxiliary->GetVcalibratedKTS() << " knots" );
202 stall_warning->setBoolValue(false);
204 SG_LOG( SG_FLIGHT, SG_INFO, " Bank Angle: "
205 << Rotation->Getphi()*RADTODEG << " deg" );
206 SG_LOG( SG_FLIGHT, SG_INFO, " Pitch Angle: "
207 << Rotation->Gettht()*RADTODEG << " deg" );
208 SG_LOG( SG_FLIGHT, SG_INFO, " True Heading: "
209 << Rotation->Getpsi()*RADTODEG << " deg" );
210 SG_LOG( SG_FLIGHT, SG_INFO, " Latitude: "
211 << Position->GetLatitude() << " deg" );
212 SG_LOG( SG_FLIGHT, SG_INFO, " Longitude: "
213 << Position->GetLongitude() << " deg" );
214 SG_LOG( SG_FLIGHT, SG_INFO, " Altitude: "
215 << Position->Geth() << " feet" );
216 SG_LOG( SG_FLIGHT, SG_INFO, " loaded initial conditions" );
218 SG_LOG( SG_FLIGHT, SG_INFO, " set dt" );
220 SG_LOG( SG_FLIGHT, SG_INFO, "Finished initializing JSBSim" );
226 /******************************************************************************/
228 // Run an iteration of the EOM (equations of motion)
230 bool FGJSBsim::update( int multiloop ) {
234 double save_alt = 0.0;
238 trimmed->setBoolValue(false);
241 if ( startup_trim->getBoolValue() ) {
244 fdmex->RunIC(fgic); //apply any changes made through the set_ functions
249 for( i=0; i<get_num_engines(); i++ ) {
250 FGEngInterface * e = get_engine(i);
251 FGEngine * eng = Propulsion->GetEngine(i);
252 FGThruster * thrust = Propulsion->GetThruster(i);
253 eng->SetMagnetos( globals->get_controls()->get_magnetos(i) );
254 eng->SetStarter( globals->get_controls()->get_starter(i) );
255 e->set_Throttle( globals->get_controls()->get_throttle(i) );
259 for ( i=0; i < multiloop; i++ ) {
263 for( i=0; i<get_num_engines(); i++ ) {
264 FGEngInterface * e = get_engine(i);
265 FGEngine * eng = Propulsion->GetEngine(i);
266 FGThruster * thrust = Propulsion->GetThruster(i);
267 e->set_Manifold_Pressure( eng->getManifoldPressure_inHg() );
268 e->set_RPM( thrust->GetRPM() );
269 e->set_EGT( eng->getExhaustGasTemp_degF() );
270 e->set_CHT( eng->getCylinderHeadTemp_degF() );
271 e->set_Oil_Temp( eng->getOilTemp_degF() );
272 e->set_Running_Flag( eng->GetRunning() );
273 e->set_Cranking_Flag( eng->GetCranking() );
279 stall_warning->setDoubleValue( Aircraft->GetStallWarn() );
281 // translate JSBsim back to FG structure so that the
282 // autopilot (and the rest of the sim can use the updated values
287 /******************************************************************************/
289 // Convert from the FGInterface struct to the JSBsim generic_ struct
291 bool FGJSBsim::copy_to_JSBsim() {
292 // copy control positions into the JSBsim structure
294 FCS->SetDaCmd( globals->get_controls()->get_aileron());
295 FCS->SetDeCmd( globals->get_controls()->get_elevator());
296 FCS->SetPitchTrimCmd(globals->get_controls()->get_elevator_trim());
297 FCS->SetDrCmd( -globals->get_controls()->get_rudder());
298 FCS->SetDfCmd( globals->get_controls()->get_flaps() );
299 FCS->SetDsbCmd( 0.0 ); //speedbrakes
300 FCS->SetDspCmd( 0.0 ); //spoilers
301 FCS->SetLBrake( globals->get_controls()->get_brake( 0 ) );
302 FCS->SetRBrake( globals->get_controls()->get_brake( 1 ) );
303 FCS->SetCBrake( globals->get_controls()->get_brake( 2 ) );
304 for (int i = 0; i < get_num_engines(); i++) {
305 FCS->SetThrottleCmd(i, globals->get_controls()->get_throttle(i));
306 FCS->SetMixtureCmd(i, globals->get_controls()->get_mixture(i));
309 Position->SetSeaLevelRadius( get_Sea_level_radius() );
310 Position->SetRunwayRadius( scenery.get_cur_elev()*SG_METER_TO_FEET
311 + get_Sea_level_radius() );
313 Atmosphere->SetExTemperature(get_Static_temperature());
314 Atmosphere->SetExPressure(get_Static_pressure());
315 Atmosphere->SetExDensity(get_Density());
316 Atmosphere->SetWindNED(get_V_north_airmass(),
317 get_V_east_airmass(),
318 get_V_down_airmass());
319 // SG_LOG(SG_FLIGHT,SG_INFO, "Wind NED: "
320 // << get_V_north_airmass() << ", "
321 // << get_V_east_airmass() << ", "
322 // << get_V_down_airmass() );
327 /******************************************************************************/
329 // Convert from the JSBsim generic_ struct to the FGInterface struct
331 bool FGJSBsim::copy_from_JSBsim() {
334 _set_Inertias( MassBalance->GetMass(),
335 MassBalance->GetIxx(),
336 MassBalance->GetIyy(),
337 MassBalance->GetIzz(),
338 MassBalance->GetIxz() );
340 _set_CG_Position( MassBalance->GetXYZcg(1),
341 MassBalance->GetXYZcg(2),
342 MassBalance->GetXYZcg(3) );
344 _set_Accels_Body( Aircraft->GetBodyAccel()(1),
345 Aircraft->GetBodyAccel()(2),
346 Aircraft->GetBodyAccel()(3) );
348 //_set_Accels_CG_Body( Aircraft->GetBodyAccel()(1),
349 // Aircraft->GetBodyAccel()(2),
350 // Aircraft->GetBodyAccel()(3) );
352 _set_Accels_CG_Body_N ( Aircraft->GetNcg()(1),
353 Aircraft->GetNcg()(2),
354 Aircraft->GetNcg()(3) );
356 _set_Accels_Pilot_Body( Auxiliary->GetPilotAccel()(1),
357 Auxiliary->GetPilotAccel()(2),
358 Auxiliary->GetPilotAccel()(3) );
360 // _set_Accels_Pilot_Body_N( Auxiliary->GetPilotAccel()(1)/32.1739,
361 // Auxiliary->GetNpilot(2)/32.1739,
362 // Auxiliary->GetNpilot(3)/32.1739 );
364 _set_Nlf( Aerodynamics->GetNlf() );
368 _set_Velocities_Local( Position->GetVn(),
372 _set_Velocities_Wind_Body( Translation->GetUVW(1),
373 Translation->GetUVW(2),
374 Translation->GetUVW(3) );
376 _set_V_rel_wind( Translation->GetVt() );
378 _set_V_equiv_kts( Auxiliary->GetVequivalentKTS() );
380 // _set_V_calibrated( Auxiliary->GetVcalibratedFPS() );
382 _set_V_calibrated_kts( Auxiliary->GetVcalibratedKTS() );
384 _set_V_ground_speed( Position->GetVground() );
386 _set_Omega_Body( Rotation->GetPQR(1),
388 Rotation->GetPQR(3) );
390 _set_Euler_Rates( Rotation->GetEulerRates(1),
391 Rotation->GetEulerRates(2),
392 Rotation->GetEulerRates(3) );
394 _set_Geocentric_Rates(Position->GetLatitudeDot(),
395 Position->GetLongitudeDot(),
396 Position->Gethdot() );
398 _set_Mach_number( Translation->GetMach() );
401 _updatePosition( Position->GetLatitude(),
402 Position->GetLongitude(),
405 _set_Altitude_AGL( Position->GetDistanceAGL() );
407 _set_Euler_Angles( Rotation->Getphi(),
409 Rotation->Getpsi() );
411 _set_Alpha( Translation->Getalpha() );
412 _set_Beta( Translation->Getbeta() );
415 _set_Gamma_vert_rad( Position->GetGamma() );
416 // set_Gamma_horiz_rad( Gamma_horiz_rad );
418 _set_Earth_position_angle( Auxiliary->GetEarthPositionAngle() );
420 _set_Climb_Rate( Position->Gethdot() );
423 for ( i = 1; i <= 3; i++ ) {
424 for ( j = 1; j <= 3; j++ ) {
425 _set_T_Local_to_Body( i, j, State->GetTl2b(i,j) );
431 bool FGJSBsim::ToggleDataLogging(void) {
432 return fdmex->GetOutput()->Toggle();
436 bool FGJSBsim::ToggleDataLogging(bool state) {
438 fdmex->GetOutput()->Enable();
441 fdmex->GetOutput()->Disable();
448 void FGJSBsim::set_Latitude(double lat) {
449 static const SGPropertyNode *altitude
450 = fgGetNode("/position/altitude-ft");
452 if ( altitude->getDoubleValue() > -9990 ) {
453 alt = altitude->getDoubleValue();
458 double sea_level_radius_meters, lat_geoc;
460 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat );
461 SG_LOG(SG_FLIGHT,SG_INFO," cur alt (ft) = " << alt );
463 sgGeodToGeoc( lat, alt * SG_FEET_TO_METER,
464 &sea_level_radius_meters, &lat_geoc );
466 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
467 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
468 fgic->SetLatitudeRadIC( lat_geoc );
472 void FGJSBsim::set_Longitude(double lon) {
474 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon );
476 fgic->SetLongitudeRadIC( lon );
480 void FGJSBsim::set_Altitude(double alt) {
481 static const SGPropertyNode *latitude
482 = fgGetNode("/position/latitude-deg");
484 double sea_level_radius_meters,lat_geoc;
486 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt );
487 SG_LOG(SG_FLIGHT,SG_INFO, " lat (deg) = " << latitude->getDoubleValue() );
489 sgGeodToGeoc( latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS, alt,
490 &sea_level_radius_meters, &lat_geoc);
491 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
492 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
493 fgic->SetLatitudeRadIC( lat_geoc );
494 fgic->SetAltitudeFtIC(alt);
498 void FGJSBsim::set_V_calibrated_kts(double vc) {
499 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " << vc );
501 fgic->SetVcalibratedKtsIC(vc);
505 void FGJSBsim::set_Mach_number(double mach) {
506 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " << mach );
508 fgic->SetMachIC(mach);
512 void FGJSBsim::set_Velocities_Local( double north, double east, double down ){
513 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: "
514 << north << ", " << east << ", " << down );
516 fgic->SetVnorthFpsIC(north);
517 fgic->SetVeastFpsIC(east);
518 fgic->SetVdownFpsIC(down);
522 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w){
523 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
524 << u << ", " << v << ", " << w );
526 fgic->SetUBodyFpsIC(u);
527 fgic->SetVBodyFpsIC(v);
528 fgic->SetWBodyFpsIC(w);
533 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi ) {
534 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: "
535 << phi << ", " << theta << ", " << psi );
537 fgic->SetPitchAngleRadIC(theta);
538 fgic->SetRollAngleRadIC(phi);
539 fgic->SetTrueHeadingRadIC(psi);
544 void FGJSBsim::set_Climb_Rate( double roc) {
545 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
547 fgic->SetClimbRateFpsIC(roc);
551 void FGJSBsim::set_Gamma_vert_rad( double gamma) {
552 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
554 fgic->SetFlightPathAngleRadIC(gamma);
559 void FGJSBsim::set_Sea_level_radius(double slr) {
560 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Sea_level_radius: " << slr );
562 fgic->SetSeaLevelRadiusFtIC(slr);
566 void FGJSBsim::set_Runway_altitude(double ralt) {
567 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Runway_altitude: " << ralt );
569 _set_Runway_altitude( ralt );
570 fgic->SetTerrainAltitudeFtIC( ralt );
574 void FGJSBsim::set_Static_pressure(double p) {
575 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_pressure: " << p );
577 Atmosphere->SetExPressure(p);
578 if(Atmosphere->External() == true)
582 void FGJSBsim::set_Static_temperature(double T) {
583 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_temperature: " << T );
585 Atmosphere->SetExTemperature(T);
586 if(Atmosphere->External() == true)
591 void FGJSBsim::set_Density(double rho) {
592 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Density: " << rho );
594 Atmosphere->SetExDensity(rho);
595 if(Atmosphere->External() == true)
599 void FGJSBsim::set_Velocities_Local_Airmass (double wnorth,
602 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local_Airmass: "
603 << wnorth << ", " << weast << ", " << wdown );
605 _set_Velocities_Local_Airmass( wnorth, weast, wdown );
606 Atmosphere->SetWindNED(wnorth, weast, wdown );
607 if(Atmosphere->External() == true)
611 void FGJSBsim::init_gear(void ) {
613 FGGearInterface *gear;
614 FGGroundReactions* gr=fdmex->GetGroundReactions();
615 int Ngear=GroundReactions->GetNumGearUnits();
616 for (int i=0;i<Ngear;i++) {
617 add_gear_unit( FGGearInterface() );
618 gear=get_gear_unit(i);
619 gear->SetX( gr->GetGearUnit(i)->GetBodyLocation()(1) );
620 gear->SetY( gr->GetGearUnit(i)->GetBodyLocation()(2) );
621 gear->SetZ( gr->GetGearUnit(i)->GetBodyLocation()(3) );
622 gear->SetWoW( gr->GetGearUnit(i)->GetWOW() );
623 if ( gr->GetGearUnit(i)->GetBrakeGroup() > 0 ) {
624 gear->SetBrake(true);
626 if ( gr->GetGearUp() ) {
627 gear->SetPosition( 0.0 );
632 void FGJSBsim::update_gear(void) {
634 FGGearInterface* gear;
635 FGGroundReactions* gr=fdmex->GetGroundReactions();
636 int Ngear=GroundReactions->GetNumGearUnits();
637 for (int i=0;i<Ngear;i++) {
638 gear=get_gear_unit(i);
639 gear->SetWoW( gr->GetGearUnit(i)->GetWOW() );
640 if ( gr->GetGearUp() ) {
641 gear->SetPosition( 0.0 );
646 void FGJSBsim::do_trim(void) {
649 if(fgic->GetVcalibratedKtsIC() < 10 ) {
650 fgic->SetVcalibratedKtsIC(0.0);
651 fgtrim=new FGTrim(fdmex,fgic,tGround);
653 fgtrim=new FGTrim(fdmex,fgic,tLongitudinal);
655 if( !fgtrim->DoTrim() ) {
659 trimmed->setBoolValue(true);
661 State->ReportState();
663 pitch_trim->setDoubleValue( FCS->GetPitchTrimCmd() );
664 throttle_trim->setDoubleValue( FCS->GetThrottleCmd(0) );
665 aileron_trim->setDoubleValue( FCS->GetDaCmd() );
666 rudder_trim->setDoubleValue( FCS->GetDrCmd() );
668 globals->get_controls()->set_elevator_trim(FCS->GetPitchTrimCmd());
669 globals->get_controls()->set_elevator(FCS->GetDeCmd());
670 globals->get_controls()->set_throttle(FGControls::ALL_ENGINES,
671 FCS->GetThrottleCmd(0));
673 globals->get_controls()->set_aileron(FCS->GetDaCmd());
674 globals->get_controls()->set_rudder( FCS->GetDrCmd());
676 SG_LOG( SG_FLIGHT, SG_INFO, " Trim complete" );