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") );
98 int Neng = Propulsion->GetNumEngines();
99 SG_LOG(SG_FLIGHT,SG_INFO, "Neng: " << Neng );
101 for(int i=0;i<Neng;i++) {
102 add_engine( FGEngInterface() );
105 fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
106 fgSetDouble("/fdm/trim/throttle", FCS->GetThrottleCmd(0));
107 fgSetDouble("/fdm/trim/aileron", FCS->GetDaCmd());
108 fgSetDouble("/fdm/trim/rudder", FCS->GetDrCmd());
110 startup_trim = fgGetNode("/sim/startup/trim", true);
112 trimmed = fgGetNode("/fdm/trim/trimmed", true);
113 trimmed->setBoolValue(false);
115 pitch_trim = fgGetNode("/fdm/trim/pitch-trim", true );
116 throttle_trim = fgGetNode("/fdm/trim/throttle", true );
117 aileron_trim = fgGetNode("/fdm/trim/aileron", true );
118 rudder_trim = fgGetNode("/fdm/trim/rudder", true );
121 /******************************************************************************/
122 FGJSBsim::~FGJSBsim(void) {
123 if ( fdmex != NULL ) {
131 /******************************************************************************/
133 // Initialize the JSBsim flight model, dt is the time increment for
134 // each subsequent iteration through the EOM
136 void FGJSBsim::init() {
138 SG_LOG( SG_FLIGHT, SG_INFO, "Starting and initializing JSBsim" );
140 // Explicitly call the superclass's
141 // init method first.
144 SG_LOG( SG_FLIGHT, SG_INFO, " Initializing JSBSim with:" );
146 switch(fgic->GetSpeedSet()) {
148 SG_LOG(SG_FLIGHT,SG_INFO, " Vn,Ve,Vd= "
149 << Position->GetVn() << ", "
150 << Position->GetVe() << ", "
151 << Position->GetVd() << " ft/s");
154 SG_LOG(SG_FLIGHT,SG_INFO, " U,V,W= "
155 << Translation->GetUVW(1) << ", "
156 << Translation->GetUVW(2) << ", "
157 << Translation->GetUVW(3) << " ft/s");
160 SG_LOG(SG_FLIGHT,SG_INFO, " Mach: "
161 << Translation->GetMach() );
165 SG_LOG(SG_FLIGHT,SG_INFO, " Indicated Airspeed: "
166 << Auxiliary->GetVcalibratedKTS() << " knots" );
170 SG_LOG( SG_FLIGHT, SG_INFO, " Bank Angle: "
171 << Rotation->Getphi()*RADTODEG << " deg");
172 SG_LOG( SG_FLIGHT, SG_INFO, " Pitch Angle: "
173 << Rotation->Gettht()*RADTODEG << " deg" );
174 SG_LOG( SG_FLIGHT, SG_INFO, " True Heading: "
175 << Rotation->Getpsi()*RADTODEG << " deg" );
176 SG_LOG( SG_FLIGHT, SG_INFO, " Latitude: "
177 << Position->GetLatitude() << " deg" );
178 SG_LOG( SG_FLIGHT, SG_INFO, " Longitude: "
179 << Position->GetLongitude() << " deg" );
181 SG_LOG( SG_FLIGHT, SG_INFO, " loaded initial conditions" );
183 SG_LOG( SG_FLIGHT, SG_INFO, " set dt" );
185 SG_LOG( SG_FLIGHT, SG_INFO, "Finished initializing JSBSim" );
188 /******************************************************************************/
190 // Run an iteration of the EOM (equations of motion)
192 bool FGJSBsim::update( int multiloop ) {
196 double save_alt = 0.0;
200 trimmed->setBoolValue(false);
202 if ( needTrim && startup_trim->getBoolValue() ) {
204 //fgic->SetSeaLevelRadiusFtIC( get_Sea_level_radius() );
205 //fgic->SetTerrainAltitudeFtIC( scenery.cur_elev * SG_METER_TO_FEET );
208 if(fgic->GetVcalibratedKtsIC() < 10 ) {
209 fgic->SetVcalibratedKtsIC(0.0);
210 fgtrim=new FGTrim(fdmex,fgic,tGround);
212 fgtrim=new FGTrim(fdmex,fgic,tLongitudinal);
214 if(!fgtrim->DoTrim()) {
218 trimmed->setBoolValue(true);
220 fgtrim->ReportState();
225 pitch_trim->setDoubleValue( FCS->GetPitchTrimCmd() );
226 throttle_trim->setDoubleValue( FCS->GetThrottleCmd(0) );
227 aileron_trim->setDoubleValue( FCS->GetDaCmd() );
228 rudder_trim->setDoubleValue( FCS->GetDrCmd() );
230 controls.set_elevator_trim(FCS->GetPitchTrimCmd());
231 controls.set_elevator(FCS->GetDeCmd());
232 controls.set_throttle(FGControls::ALL_ENGINES,
233 FCS->GetThrottleCmd(0));
235 controls.set_aileron(FCS->GetDaCmd());
236 controls.set_rudder( FCS->GetDrCmd());
238 SG_LOG( SG_FLIGHT, SG_INFO, " Trim complete" );
241 for( i=0; i<get_num_engines(); i++ ) {
242 get_engine(i)->set_RPM( Propulsion->GetThruster(i)->GetRPM() );
243 get_engine(i)->set_Throttle( controls.get_throttle(i) );
246 for ( i=0; i < multiloop; i++ ) {
250 // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
251 // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
253 // translate JSBsim back to FG structure so that the
254 // autopilot (and the rest of the sim can use the updated values
260 /******************************************************************************/
262 // Convert from the FGInterface struct to the JSBsim generic_ struct
264 bool FGJSBsim::copy_to_JSBsim() {
265 // copy control positions into the JSBsim structure
267 FCS->SetDaCmd( controls.get_aileron());
268 FCS->SetDeCmd( controls.get_elevator());
269 FCS->SetPitchTrimCmd(controls.get_elevator_trim());
270 FCS->SetDrCmd( -controls.get_rudder());
271 FCS->SetDfCmd( controls.get_flaps() );
272 FCS->SetDsbCmd( 0.0 ); //speedbrakes
273 FCS->SetDspCmd( 0.0 ); //spoilers
274 FCS->SetThrottleCmd( FGControls::ALL_ENGINES,
275 controls.get_throttle( 0 ));
276 FCS->SetLBrake( controls.get_brake( 0 ) );
277 FCS->SetRBrake( controls.get_brake( 1 ) );
278 FCS->SetCBrake( controls.get_brake( 2 ) );
280 Position->SetSeaLevelRadius( get_Sea_level_radius() );
281 Position->SetRunwayRadius( scenery.cur_elev*SG_METER_TO_FEET
282 + get_Sea_level_radius() );
284 Atmosphere->SetExTemperature(get_Static_temperature());
285 Atmosphere->SetExPressure(get_Static_pressure());
286 Atmosphere->SetExDensity(get_Density());
287 Atmosphere->SetWindNED(get_V_north_airmass(),
288 get_V_east_airmass(),
289 get_V_down_airmass());
290 // SG_LOG(SG_FLIGHT,SG_INFO, "Wind NED: "
291 // << get_V_north_airmass() << ", "
292 // << get_V_east_airmass() << ", "
293 // << get_V_down_airmass() );
298 /******************************************************************************/
300 // Convert from the JSBsim generic_ struct to the FGInterface struct
302 bool FGJSBsim::copy_from_JSBsim() {
305 _set_Inertias( MassBalance->GetMass(),
306 MassBalance->GetIxx(),
307 MassBalance->GetIyy(),
308 MassBalance->GetIzz(),
309 MassBalance->GetIxz() );
311 _set_CG_Position( MassBalance->GetXYZcg(1),
312 MassBalance->GetXYZcg(2),
313 MassBalance->GetXYZcg(3) );
315 _set_Accels_Body( Translation->GetUVWdot(1),
316 Translation->GetUVWdot(2),
317 Translation->GetUVWdot(3) );
319 _set_Accels_CG_Body( Translation->GetUVWdot(1),
320 Translation->GetUVWdot(2),
321 Translation->GetUVWdot(3) );
323 //_set_Accels_CG_Body_N ( Translation->GetNcg(1),
324 // Translation->GetNcg(2),
325 // Translation->GetNcg(3) );
327 _set_Accels_Pilot_Body( Auxiliary->GetPilotAccel(1),
328 Auxiliary->GetPilotAccel(2),
329 Auxiliary->GetPilotAccel(3) );
331 //_set_Accels_Pilot_Body_N( Auxiliary->GetNpilot(1),
332 // Auxiliary->GetNpilot(2),
333 // Auxiliary->GetNpilot(3) );
335 _set_Nlf( Aerodynamics->GetNlf() );
339 _set_Velocities_Local( Position->GetVn(),
343 _set_Velocities_Wind_Body( Translation->GetUVW(1),
344 Translation->GetUVW(2),
345 Translation->GetUVW(3) );
347 _set_V_rel_wind( Translation->GetVt() );
349 _set_V_equiv_kts( Auxiliary->GetVequivalentKTS() );
351 // _set_V_calibrated( Auxiliary->GetVcalibratedFPS() );
353 _set_V_calibrated_kts( Auxiliary->GetVcalibratedKTS() );
355 _set_V_ground_speed( Position->GetVground() );
357 _set_Omega_Body( Rotation->GetPQR(1),
359 Rotation->GetPQR(3) );
361 _set_Euler_Rates( Rotation->GetEulerRates(1),
362 Rotation->GetEulerRates(2),
363 Rotation->GetEulerRates(3) );
365 _set_Geocentric_Rates(Position->GetLatitudeDot(),
366 Position->GetLongitudeDot(),
367 Position->Gethdot() );
369 _set_Mach_number( Translation->GetMach() );
372 _updatePosition( Position->GetLatitude(),
373 Position->GetLongitude(),
376 _set_Euler_Angles( Rotation->Getphi(),
378 Rotation->Getpsi() );
380 _set_Alpha( Translation->Getalpha() );
381 _set_Beta( Translation->Getbeta() );
384 _set_Gamma_vert_rad( Position->GetGamma() );
385 // set_Gamma_horiz_rad( Gamma_horiz_rad );
387 _set_Earth_position_angle( Auxiliary->GetEarthPositionAngle() );
389 _set_Climb_Rate( Position->Gethdot() );
392 for ( i = 1; i <= 3; i++ ) {
393 for ( j = 1; j <= 3; j++ ) {
394 _set_T_Local_to_Body( i, j, State->GetTl2b(i,j) );
400 void FGJSBsim::snap_shot(void) {
401 fgic->SetLatitudeRadIC(get_Lat_geocentric() );
402 fgic->SetLongitudeRadIC( get_Longitude() );
403 fgic->SetAltitudeFtIC( get_Altitude() );
404 fgic->SetTerrainAltitudeFtIC( get_Runway_altitude() );
405 fgic->SetVtrueFpsIC( get_V_rel_wind() );
406 fgic->SetPitchAngleRadIC( get_Theta() );
407 fgic->SetRollAngleRadIC( get_Phi() );
408 fgic->SetTrueHeadingRadIC( get_Psi() );
409 fgic->SetClimbRateFpsIC( get_Climb_Rate() );
413 bool FGJSBsim::ToggleDataLogging(void) {
414 return fdmex->GetOutput()->Toggle();
418 bool FGJSBsim::ToggleDataLogging(bool state) {
420 fdmex->GetOutput()->Enable();
423 fdmex->GetOutput()->Disable();
430 void FGJSBsim::set_Latitude(double lat) {
431 double sea_level_radius_meters,lat_geoc;
433 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat );
436 sgGeodToGeoc( lat, get_Altitude() , &sea_level_radius_meters, &lat_geoc);
437 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
438 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
439 fgic->SetLatitudeRadIC( lat_geoc );
440 fdmex->RunIC(fgic); //loop JSBSim once
441 copy_from_JSBsim(); //update the bus
445 void FGJSBsim::set_Longitude(double lon) {
447 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon );
450 fgic->SetLongitudeRadIC(lon);
451 fdmex->RunIC(fgic); //loop JSBSim once
452 copy_from_JSBsim(); //update the bus
456 void FGJSBsim::set_Altitude(double alt) {
457 double sea_level_radius_meters,lat_geoc;
459 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt );
462 sgGeodToGeoc( get_Latitude(), alt , &sea_level_radius_meters, &lat_geoc);
463 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
464 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
465 fgic->SetLatitudeRadIC( lat_geoc );
466 fgic->SetAltitudeFtIC(alt);
467 fdmex->RunIC(fgic); //loop JSBSim once
468 copy_from_JSBsim(); //update the bus
472 void FGJSBsim::set_V_calibrated_kts(double vc) {
473 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " << vc );
476 fgic->SetVcalibratedKtsIC(vc);
477 fdmex->RunIC(fgic); //loop JSBSim once
478 copy_from_JSBsim(); //update the bus
482 void FGJSBsim::set_Mach_number(double mach) {
483 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " << mach );
486 fgic->SetMachIC(mach);
487 fdmex->RunIC(fgic); //loop JSBSim once
488 copy_from_JSBsim(); //update the bus
492 void FGJSBsim::set_Velocities_Local( double north, double east, double down ){
493 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: "
494 << north << ", " << east << ", " << down );
497 fgic->SetVnorthFpsIC(north);
498 fgic->SetVeastFpsIC(east);
499 fgic->SetVdownFpsIC(down);
500 fdmex->RunIC(fgic); //loop JSBSim once
501 copy_from_JSBsim(); //update the bus
505 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w){
506 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
507 << u << ", " << v << ", " << w );
510 fgic->SetUBodyFpsIC(u);
511 fgic->SetVBodyFpsIC(v);
512 fgic->SetWBodyFpsIC(w);
513 fdmex->RunIC(fgic); //loop JSBSim once
514 copy_from_JSBsim(); //update the bus
519 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi ) {
520 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: "
521 << phi << ", " << theta << ", " << psi );
524 fgic->SetPitchAngleRadIC(theta);
525 fgic->SetRollAngleRadIC(phi);
526 fgic->SetTrueHeadingRadIC(psi);
527 fdmex->RunIC(fgic); //loop JSBSim once
528 copy_from_JSBsim(); //update the bus
533 void FGJSBsim::set_Climb_Rate( double roc) {
534 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
537 fgic->SetClimbRateFpsIC(roc);
538 fdmex->RunIC(fgic); //loop JSBSim once
539 copy_from_JSBsim(); //update the bus
543 void FGJSBsim::set_Gamma_vert_rad( double gamma) {
544 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
547 fgic->SetFlightPathAngleRadIC(gamma);
548 fdmex->RunIC(fgic); //loop JSBSim once
549 copy_from_JSBsim(); //update the bus
554 void FGJSBsim::set_Sea_level_radius(double slr) {
555 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Sea_level_radius: " << slr );
558 fgic->SetSeaLevelRadiusFtIC(slr);
559 fdmex->RunIC(fgic); //loop JSBSim once
560 copy_from_JSBsim(); //update the bus
564 void FGJSBsim::set_Runway_altitude(double ralt) {
565 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Runway_altitude: " << ralt );
568 _set_Runway_altitude( ralt );
569 fgic->SetTerrainAltitudeFtIC( ralt );
570 fdmex->RunIC(fgic); //loop JSBSim once
571 copy_from_JSBsim(); //update the bus
575 void FGJSBsim::set_Static_pressure(double p) {
576 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_pressure: " << p );
579 Atmosphere->SetExPressure(p);
580 if(Atmosphere->External() == true)
584 void FGJSBsim::set_Static_temperature(double T) {
585 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_temperature: " << T );
588 Atmosphere->SetExTemperature(T);
589 if(Atmosphere->External() == true)
594 void FGJSBsim::set_Density(double rho) {
595 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Density: " << rho );
598 Atmosphere->SetExDensity(rho);
599 if(Atmosphere->External() == true)
604 void FGJSBsim::set_Velocities_Local_Airmass (double wnorth,
607 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local_Airmass: "
608 << wnorth << ", " << weast << ", " << wdown );
610 _set_Velocities_Local_Airmass( wnorth, weast, wdown );
612 Atmosphere->SetWindNED(wnorth, weast, wdown );
613 if(Atmosphere->External() == true)