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 )
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();
81 Atmosphere->UseInternal();
83 fgic=new FGInitialCondition(fdmex);
86 SGPath aircraft_path( globals->get_fg_root() );
87 aircraft_path.append( "Aircraft" );
89 SGPath engine_path( globals->get_fg_root() );
90 engine_path.append( "Engine" );
94 result = fdmex->LoadModel( aircraft_path.str(),
96 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 int Neng = Propulsion->GetNumEngines();
108 SG_LOG(SG_FLIGHT,SG_INFO, "Neng: " << Neng );
110 for(int i=0;i<Neng;i++) {
111 add_engine( FGEngInterface() );
114 fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
115 fgSetDouble("/fdm/trim/throttle", FCS->GetThrottleCmd(0));
116 fgSetDouble("/fdm/trim/aileron", FCS->GetDaCmd());
117 fgSetDouble("/fdm/trim/rudder", FCS->GetDrCmd());
119 startup_trim = fgGetNode("/sim/startup/trim", true);
121 trimmed = fgGetNode("/fdm/trim/trimmed", true);
122 trimmed->setBoolValue(false);
124 pitch_trim = fgGetNode("/fdm/trim/pitch-trim", true );
125 throttle_trim = fgGetNode("/fdm/trim/throttle", true );
126 aileron_trim = fgGetNode("/fdm/trim/aileron", true );
127 rudder_trim = fgGetNode("/fdm/trim/rudder", true );
130 /******************************************************************************/
131 FGJSBsim::~FGJSBsim(void) {
133 delete fdmex; fdmex=NULL;
134 delete fgic; fgic=NULL;
138 /******************************************************************************/
140 // Initialize the JSBsim flight model, dt is the time increment for
141 // each subsequent iteration through the EOM
143 void FGJSBsim::init() {
145 SG_LOG( SG_FLIGHT, SG_INFO, "Starting and initializing JSBsim" );
147 // Explicitly call the superclass's
148 // init method first.
151 SG_LOG( SG_FLIGHT, SG_INFO, " Initializing JSBSim with:" );
153 switch(fgic->GetSpeedSet()) {
155 SG_LOG(SG_FLIGHT,SG_INFO, " Vn,Ve,Vd= "
156 << Position->GetVn() << ", "
157 << Position->GetVe() << ", "
158 << Position->GetVd() << " ft/s");
161 SG_LOG(SG_FLIGHT,SG_INFO, " U,V,W= "
162 << Translation->GetUVW(1) << ", "
163 << Translation->GetUVW(2) << ", "
164 << Translation->GetUVW(3) << " ft/s");
167 SG_LOG(SG_FLIGHT,SG_INFO, " Mach: "
168 << Translation->GetMach() );
172 SG_LOG(SG_FLIGHT,SG_INFO, " Indicated Airspeed: "
173 << Auxiliary->GetVcalibratedKTS() << " knots" );
177 SG_LOG( SG_FLIGHT, SG_INFO, " Bank Angle: "
178 << Rotation->Getphi()*RADTODEG << " deg");
179 SG_LOG( SG_FLIGHT, SG_INFO, " Pitch Angle: "
180 << Rotation->Gettht()*RADTODEG << " deg" );
181 SG_LOG( SG_FLIGHT, SG_INFO, " True Heading: "
182 << Rotation->Getpsi()*RADTODEG << " deg" );
183 SG_LOG( SG_FLIGHT, SG_INFO, " Latitude: "
184 << Position->GetLatitude() << " deg" );
185 SG_LOG( SG_FLIGHT, SG_INFO, " Longitude: "
186 << Position->GetLongitude() << " deg" );
188 SG_LOG( SG_FLIGHT, SG_INFO, " loaded initial conditions" );
190 SG_LOG( SG_FLIGHT, SG_INFO, " set dt" );
192 SG_LOG( SG_FLIGHT, SG_INFO, "Finished initializing JSBSim" );
195 /******************************************************************************/
197 // Run an iteration of the EOM (equations of motion)
199 bool FGJSBsim::update( int multiloop ) {
203 double save_alt = 0.0;
207 trimmed->setBoolValue(false);
209 if ( needTrim && startup_trim->getBoolValue() ) {
211 //fgic->SetSeaLevelRadiusFtIC( get_Sea_level_radius() );
212 //fgic->SetTerrainAltitudeFtIC( scenery.cur_elev * SG_METER_TO_FEET );
215 if(fgic->GetVcalibratedKtsIC() < 10 ) {
216 fgic->SetVcalibratedKtsIC(0.0);
217 fgtrim=new FGTrim(fdmex,fgic,tGround);
219 fgtrim=new FGTrim(fdmex,fgic,tLongitudinal);
221 if(!fgtrim->DoTrim()) {
225 trimmed->setBoolValue(true);
227 fgtrim->ReportState();
232 pitch_trim->setDoubleValue( FCS->GetPitchTrimCmd() );
233 throttle_trim->setDoubleValue( FCS->GetThrottleCmd(0) );
234 aileron_trim->setDoubleValue( FCS->GetDaCmd() );
235 rudder_trim->setDoubleValue( FCS->GetDrCmd() );
237 globals->get_controls()->set_elevator_trim(FCS->GetPitchTrimCmd());
238 globals->get_controls()->set_elevator(FCS->GetDeCmd());
239 globals->get_controls()->set_throttle(FGControls::ALL_ENGINES,
240 FCS->GetThrottleCmd(0));
242 globals->get_controls()->set_aileron(FCS->GetDaCmd());
243 globals->get_controls()->set_rudder( FCS->GetDrCmd());
245 SG_LOG( SG_FLIGHT, SG_INFO, " Trim complete" );
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 e->set_Manifold_Pressure( eng->getManifoldPressure_inHg() );
253 e->set_RPM( thrust->GetRPM() );
254 e->set_EGT( eng->getExhaustGasTemp_degF() );
255 e->set_CHT( eng->getCylinderHeadTemp_degF() );
256 e->set_Oil_Temp( eng->getOilTemp_degF() );
257 e->set_Throttle( globals->get_controls()->get_throttle(i) );
260 for ( i=0; i < multiloop; i++ ) {
264 // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
265 // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
267 // translate JSBsim back to FG structure so that the
268 // autopilot (and the rest of the sim can use the updated values
274 /******************************************************************************/
276 // Convert from the FGInterface struct to the JSBsim generic_ struct
278 bool FGJSBsim::copy_to_JSBsim() {
279 // copy control positions into the JSBsim structure
281 FCS->SetDaCmd( globals->get_controls()->get_aileron());
282 FCS->SetDeCmd( globals->get_controls()->get_elevator());
283 FCS->SetPitchTrimCmd(globals->get_controls()->get_elevator_trim());
284 FCS->SetDrCmd( -globals->get_controls()->get_rudder());
285 FCS->SetDfCmd( globals->get_controls()->get_flaps() );
286 FCS->SetDsbCmd( 0.0 ); //speedbrakes
287 FCS->SetDspCmd( 0.0 ); //spoilers
288 FCS->SetLBrake( globals->get_controls()->get_brake( 0 ) );
289 FCS->SetRBrake( globals->get_controls()->get_brake( 1 ) );
290 FCS->SetCBrake( globals->get_controls()->get_brake( 2 ) );
291 for (int i = 0; i < get_num_engines(); i++) {
292 FCS->SetThrottleCmd(i, globals->get_controls()->get_throttle(i));
293 FCS->SetMixtureCmd(i, globals->get_controls()->get_mixture(i));
296 Position->SetSeaLevelRadius( get_Sea_level_radius() );
297 Position->SetRunwayRadius( scenery.get_cur_elev()*SG_METER_TO_FEET
298 + get_Sea_level_radius() );
300 Atmosphere->SetExTemperature(get_Static_temperature());
301 Atmosphere->SetExPressure(get_Static_pressure());
302 Atmosphere->SetExDensity(get_Density());
303 Atmosphere->SetWindNED(get_V_north_airmass(),
304 get_V_east_airmass(),
305 get_V_down_airmass());
306 // SG_LOG(SG_FLIGHT,SG_INFO, "Wind NED: "
307 // << get_V_north_airmass() << ", "
308 // << get_V_east_airmass() << ", "
309 // << get_V_down_airmass() );
314 /******************************************************************************/
316 // Convert from the JSBsim generic_ struct to the FGInterface struct
318 bool FGJSBsim::copy_from_JSBsim() {
321 _set_Inertias( MassBalance->GetMass(),
322 MassBalance->GetIxx(),
323 MassBalance->GetIyy(),
324 MassBalance->GetIzz(),
325 MassBalance->GetIxz() );
327 _set_CG_Position( MassBalance->GetXYZcg(1),
328 MassBalance->GetXYZcg(2),
329 MassBalance->GetXYZcg(3) );
331 _set_Accels_Body( Translation->GetUVWdot(1),
332 Translation->GetUVWdot(2),
333 Translation->GetUVWdot(3) );
335 _set_Accels_CG_Body( Translation->GetUVWdot(1),
336 Translation->GetUVWdot(2),
337 Translation->GetUVWdot(3) );
339 //_set_Accels_CG_Body_N ( Translation->GetNcg(1),
340 // Translation->GetNcg(2),
341 // Translation->GetNcg(3) );
343 _set_Accels_Pilot_Body( Auxiliary->GetPilotAccel(1),
344 Auxiliary->GetPilotAccel(2),
345 Auxiliary->GetPilotAccel(3) );
347 //_set_Accels_Pilot_Body_N( Auxiliary->GetNpilot(1),
348 // Auxiliary->GetNpilot(2),
349 // Auxiliary->GetNpilot(3) );
351 _set_Nlf( Aerodynamics->GetNlf() );
355 _set_Velocities_Local( Position->GetVn(),
359 _set_Velocities_Wind_Body( Translation->GetUVW(1),
360 Translation->GetUVW(2),
361 Translation->GetUVW(3) );
363 _set_V_rel_wind( Translation->GetVt() );
365 _set_V_equiv_kts( Auxiliary->GetVequivalentKTS() );
367 // _set_V_calibrated( Auxiliary->GetVcalibratedFPS() );
369 _set_V_calibrated_kts( Auxiliary->GetVcalibratedKTS() );
371 _set_V_ground_speed( Position->GetVground() );
373 _set_Omega_Body( Rotation->GetPQR(1),
375 Rotation->GetPQR(3) );
377 _set_Euler_Rates( Rotation->GetEulerRates(1),
378 Rotation->GetEulerRates(2),
379 Rotation->GetEulerRates(3) );
381 _set_Geocentric_Rates(Position->GetLatitudeDot(),
382 Position->GetLongitudeDot(),
383 Position->Gethdot() );
385 _set_Mach_number( Translation->GetMach() );
388 _updatePosition( Position->GetLatitude(),
389 Position->GetLongitude(),
392 _set_Altitude_AGL( Position->GetDistanceAGL() );
394 _set_Euler_Angles( Rotation->Getphi(),
396 Rotation->Getpsi() );
398 _set_Alpha( Translation->Getalpha() );
399 _set_Beta( Translation->Getbeta() );
402 _set_Gamma_vert_rad( Position->GetGamma() );
403 // set_Gamma_horiz_rad( Gamma_horiz_rad );
405 _set_Earth_position_angle( Auxiliary->GetEarthPositionAngle() );
407 _set_Climb_Rate( Position->Gethdot() );
410 for ( i = 1; i <= 3; i++ ) {
411 for ( j = 1; j <= 3; j++ ) {
412 _set_T_Local_to_Body( i, j, State->GetTl2b(i,j) );
418 void FGJSBsim::snap_shot(void) {
419 fgic->SetLatitudeRadIC(get_Lat_geocentric() );
420 fgic->SetLongitudeRadIC( get_Longitude() );
421 fgic->SetAltitudeFtIC( get_Altitude() );
422 fgic->SetTerrainAltitudeFtIC( get_Runway_altitude() );
423 fgic->SetVtrueFpsIC( get_V_rel_wind() );
424 fgic->SetPitchAngleRadIC( get_Theta() );
425 fgic->SetRollAngleRadIC( get_Phi() );
426 fgic->SetTrueHeadingRadIC( get_Psi() );
427 fgic->SetClimbRateFpsIC( get_Climb_Rate() );
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 double sea_level_radius_meters,lat_geoc;
451 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat );
454 sgGeodToGeoc( lat, get_Altitude() , &sea_level_radius_meters, &lat_geoc);
455 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
456 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
457 fgic->SetLatitudeRadIC( lat_geoc );
458 fdmex->RunIC(fgic); //loop JSBSim once
459 copy_from_JSBsim(); //update the bus
463 void FGJSBsim::set_Longitude(double lon) {
465 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon );
468 fgic->SetLongitudeRadIC(lon);
469 fdmex->RunIC(fgic); //loop JSBSim once
470 copy_from_JSBsim(); //update the bus
474 void FGJSBsim::set_Altitude(double alt) {
475 double sea_level_radius_meters,lat_geoc;
477 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt );
480 sgGeodToGeoc( get_Latitude(), alt , &sea_level_radius_meters, &lat_geoc);
481 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
482 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
483 fgic->SetLatitudeRadIC( lat_geoc );
484 fgic->SetAltitudeFtIC(alt);
485 fdmex->RunIC(fgic); //loop JSBSim once
486 copy_from_JSBsim(); //update the bus
490 void FGJSBsim::set_V_calibrated_kts(double vc) {
491 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " << vc );
494 fgic->SetVcalibratedKtsIC(vc);
495 fdmex->RunIC(fgic); //loop JSBSim once
496 copy_from_JSBsim(); //update the bus
500 void FGJSBsim::set_Mach_number(double mach) {
501 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " << mach );
504 fgic->SetMachIC(mach);
505 fdmex->RunIC(fgic); //loop JSBSim once
506 copy_from_JSBsim(); //update the bus
510 void FGJSBsim::set_Velocities_Local( double north, double east, double down ){
511 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: "
512 << north << ", " << east << ", " << down );
515 fgic->SetVnorthFpsIC(north);
516 fgic->SetVeastFpsIC(east);
517 fgic->SetVdownFpsIC(down);
518 fdmex->RunIC(fgic); //loop JSBSim once
519 copy_from_JSBsim(); //update the bus
523 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w){
524 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
525 << u << ", " << v << ", " << w );
528 fgic->SetUBodyFpsIC(u);
529 fgic->SetVBodyFpsIC(v);
530 fgic->SetWBodyFpsIC(w);
531 fdmex->RunIC(fgic); //loop JSBSim once
532 copy_from_JSBsim(); //update the bus
537 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi ) {
538 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: "
539 << phi << ", " << theta << ", " << psi );
542 fgic->SetPitchAngleRadIC(theta);
543 fgic->SetRollAngleRadIC(phi);
544 fgic->SetTrueHeadingRadIC(psi);
545 fdmex->RunIC(fgic); //loop JSBSim once
546 copy_from_JSBsim(); //update the bus
551 void FGJSBsim::set_Climb_Rate( double roc) {
552 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
555 fgic->SetClimbRateFpsIC(roc);
556 fdmex->RunIC(fgic); //loop JSBSim once
557 copy_from_JSBsim(); //update the bus
561 void FGJSBsim::set_Gamma_vert_rad( double gamma) {
562 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
565 fgic->SetFlightPathAngleRadIC(gamma);
566 fdmex->RunIC(fgic); //loop JSBSim once
567 copy_from_JSBsim(); //update the bus
572 void FGJSBsim::set_Sea_level_radius(double slr) {
573 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Sea_level_radius: " << slr );
576 fgic->SetSeaLevelRadiusFtIC(slr);
577 fdmex->RunIC(fgic); //loop JSBSim once
578 copy_from_JSBsim(); //update the bus
582 void FGJSBsim::set_Runway_altitude(double ralt) {
583 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Runway_altitude: " << ralt );
586 _set_Runway_altitude( ralt );
587 fgic->SetTerrainAltitudeFtIC( ralt );
588 fdmex->RunIC(fgic); //loop JSBSim once
589 copy_from_JSBsim(); //update the bus
593 void FGJSBsim::set_Static_pressure(double p) {
594 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_pressure: " << p );
597 Atmosphere->SetExPressure(p);
598 if(Atmosphere->External() == true)
602 void FGJSBsim::set_Static_temperature(double T) {
603 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_temperature: " << T );
606 Atmosphere->SetExTemperature(T);
607 if(Atmosphere->External() == true)
612 void FGJSBsim::set_Density(double rho) {
613 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Density: " << rho );
616 Atmosphere->SetExDensity(rho);
617 if(Atmosphere->External() == true)
622 void FGJSBsim::set_Velocities_Local_Airmass (double wnorth,
625 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local_Airmass: "
626 << wnorth << ", " << weast << ", " << wdown );
628 _set_Velocities_Local_Airmass( wnorth, weast, wdown );
630 Atmosphere->SetWindNED(wnorth, weast, wdown );
631 if(Atmosphere->External() == true)