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) {
129 /******************************************************************************/
131 // Initialize the JSBsim flight model, dt is the time increment for
132 // each subsequent iteration through the EOM
134 void FGJSBsim::init() {
136 SG_LOG( SG_FLIGHT, SG_INFO, "Starting and initializing JSBsim" );
138 // Explicitly call the superclass's
139 // init method first.
142 SG_LOG( SG_FLIGHT, SG_INFO, " Initializing JSBSim with:" );
144 switch(fgic->GetSpeedSet()) {
146 SG_LOG(SG_FLIGHT,SG_INFO, " Vn,Ve,Vd= "
147 << Position->GetVn() << ", "
148 << Position->GetVe() << ", "
149 << Position->GetVd() << " ft/s");
152 SG_LOG(SG_FLIGHT,SG_INFO, " U,V,W= "
153 << Translation->GetUVW(1) << ", "
154 << Translation->GetUVW(2) << ", "
155 << Translation->GetUVW(3) << " ft/s");
158 SG_LOG(SG_FLIGHT,SG_INFO, " Mach: "
159 << Translation->GetMach() );
163 SG_LOG(SG_FLIGHT,SG_INFO, " Indicated Airspeed: "
164 << Auxiliary->GetVcalibratedKTS() << " knots" );
168 SG_LOG( SG_FLIGHT, SG_INFO, " Bank Angle: "
169 << Rotation->Getphi()*RADTODEG << " deg");
170 SG_LOG( SG_FLIGHT, SG_INFO, " Pitch Angle: "
171 << Rotation->Gettht()*RADTODEG << " deg" );
172 SG_LOG( SG_FLIGHT, SG_INFO, " True Heading: "
173 << Rotation->Getpsi()*RADTODEG << " deg" );
174 SG_LOG( SG_FLIGHT, SG_INFO, " Latitude: "
175 << Position->GetLatitude() << " deg" );
176 SG_LOG( SG_FLIGHT, SG_INFO, " Longitude: "
177 << Position->GetLongitude() << " deg" );
179 SG_LOG( SG_FLIGHT, SG_INFO, " loaded initial conditions" );
181 SG_LOG( SG_FLIGHT, SG_INFO, " set dt" );
183 SG_LOG( SG_FLIGHT, SG_INFO, "Finished initializing JSBSim" );
186 /******************************************************************************/
188 // Run an iteration of the EOM (equations of motion)
190 bool FGJSBsim::update( int multiloop ) {
194 double save_alt = 0.0;
198 trimmed->setBoolValue(false);
200 if ( needTrim && startup_trim->getBoolValue() ) {
202 //fgic->SetSeaLevelRadiusFtIC( get_Sea_level_radius() );
203 //fgic->SetTerrainAltitudeFtIC( scenery.cur_elev * SG_METER_TO_FEET );
206 if(fgic->GetVcalibratedKtsIC() < 10 ) {
207 fgic->SetVcalibratedKtsIC(0.0);
208 fgtrim=new FGTrim(fdmex,fgic,tGround);
210 fgtrim=new FGTrim(fdmex,fgic,tLongitudinal);
212 if(!fgtrim->DoTrim()) {
216 trimmed->setBoolValue(true);
218 fgtrim->ReportState();
223 pitch_trim->setDoubleValue( FCS->GetPitchTrimCmd() );
224 throttle_trim->setDoubleValue( FCS->GetThrottleCmd(0) );
225 aileron_trim->setDoubleValue( FCS->GetDaCmd() );
226 rudder_trim->setDoubleValue( FCS->GetDrCmd() );
228 controls.set_elevator_trim(FCS->GetPitchTrimCmd());
229 controls.set_elevator(FCS->GetDeCmd());
230 controls.set_throttle(FGControls::ALL_ENGINES,
231 FCS->GetThrottleCmd(0));
233 controls.set_aileron(FCS->GetDaCmd());
234 controls.set_rudder( FCS->GetDrCmd());
236 SG_LOG( SG_FLIGHT, SG_INFO, " Trim complete" );
239 for( i=0; i<get_num_engines(); i++ ) {
240 get_engine(i)->set_RPM( Propulsion->GetThruster(i)->GetRPM() );
241 get_engine(i)->set_Throttle( controls.get_throttle(i) );
244 for ( i=0; i < multiloop; i++ ) {
248 // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
249 // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
251 // translate JSBsim back to FG structure so that the
252 // autopilot (and the rest of the sim can use the updated values
258 /******************************************************************************/
260 // Convert from the FGInterface struct to the JSBsim generic_ struct
262 bool FGJSBsim::copy_to_JSBsim() {
263 // copy control positions into the JSBsim structure
265 FCS->SetDaCmd( controls.get_aileron());
266 FCS->SetDeCmd( controls.get_elevator());
267 FCS->SetPitchTrimCmd(controls.get_elevator_trim());
268 FCS->SetDrCmd( -controls.get_rudder());
269 FCS->SetDfCmd( controls.get_flaps() );
270 FCS->SetDsbCmd( 0.0 ); //speedbrakes
271 FCS->SetDspCmd( 0.0 ); //spoilers
272 FCS->SetThrottleCmd( FGControls::ALL_ENGINES,
273 controls.get_throttle( 0 ));
274 FCS->SetLBrake( controls.get_brake( 0 ) );
275 FCS->SetRBrake( controls.get_brake( 1 ) );
276 FCS->SetCBrake( controls.get_brake( 2 ) );
278 Position->SetSeaLevelRadius( get_Sea_level_radius() );
279 Position->SetRunwayRadius( scenery.cur_elev*SG_METER_TO_FEET
280 + get_Sea_level_radius() );
282 Atmosphere->SetExTemperature(get_Static_temperature());
283 Atmosphere->SetExPressure(get_Static_pressure());
284 Atmosphere->SetExDensity(get_Density());
285 Atmosphere->SetWindNED(get_V_north_airmass(),
286 get_V_east_airmass(),
287 get_V_down_airmass());
288 // SG_LOG(SG_FLIGHT,SG_INFO, "Wind NED: "
289 // << get_V_north_airmass() << ", "
290 // << get_V_east_airmass() << ", "
291 // << get_V_down_airmass() );
296 /******************************************************************************/
298 // Convert from the JSBsim generic_ struct to the FGInterface struct
300 bool FGJSBsim::copy_from_JSBsim() {
303 _set_Inertias( MassBalance->GetMass(),
304 MassBalance->GetIxx(),
305 MassBalance->GetIyy(),
306 MassBalance->GetIzz(),
307 MassBalance->GetIxz() );
309 _set_CG_Position( MassBalance->GetXYZcg(1),
310 MassBalance->GetXYZcg(2),
311 MassBalance->GetXYZcg(3) );
313 _set_Accels_Body( Translation->GetUVWdot(1),
314 Translation->GetUVWdot(2),
315 Translation->GetUVWdot(3) );
317 _set_Accels_CG_Body( Translation->GetUVWdot(1),
318 Translation->GetUVWdot(2),
319 Translation->GetUVWdot(3) );
321 //_set_Accels_CG_Body_N ( Translation->GetNcg(1),
322 // Translation->GetNcg(2),
323 // Translation->GetNcg(3) );
325 _set_Accels_Pilot_Body( Auxiliary->GetPilotAccel(1),
326 Auxiliary->GetPilotAccel(2),
327 Auxiliary->GetPilotAccel(3) );
329 //_set_Accels_Pilot_Body_N( Auxiliary->GetNpilot(1),
330 // Auxiliary->GetNpilot(2),
331 // Auxiliary->GetNpilot(3) );
333 _set_Nlf( Aerodynamics->GetNlf() );
337 _set_Velocities_Local( Position->GetVn(),
341 _set_Velocities_Wind_Body( Translation->GetUVW(1),
342 Translation->GetUVW(2),
343 Translation->GetUVW(3) );
345 _set_V_rel_wind( Translation->GetVt() );
347 _set_V_equiv_kts( Auxiliary->GetVequivalentKTS() );
349 // _set_V_calibrated( Auxiliary->GetVcalibratedFPS() );
351 _set_V_calibrated_kts( Auxiliary->GetVcalibratedKTS() );
353 _set_V_ground_speed( Position->GetVground() );
355 _set_Omega_Body( Rotation->GetPQR(1),
357 Rotation->GetPQR(3) );
359 _set_Euler_Rates( Rotation->GetEulerRates(1),
360 Rotation->GetEulerRates(2),
361 Rotation->GetEulerRates(3) );
363 _set_Geocentric_Rates(Position->GetLatitudeDot(),
364 Position->GetLongitudeDot(),
365 Position->Gethdot() );
367 _set_Mach_number( Translation->GetMach() );
370 _updatePosition( Position->GetLatitude(),
371 Position->GetLongitude(),
374 _set_Euler_Angles( Rotation->Getphi(),
376 Rotation->Getpsi() );
378 _set_Alpha( Translation->Getalpha() );
379 _set_Beta( Translation->Getbeta() );
382 _set_Gamma_vert_rad( Position->GetGamma() );
383 // set_Gamma_horiz_rad( Gamma_horiz_rad );
385 _set_Earth_position_angle( Auxiliary->GetEarthPositionAngle() );
387 _set_Climb_Rate( Position->Gethdot() );
390 for ( i = 1; i <= 3; i++ ) {
391 for ( j = 1; j <= 3; j++ ) {
392 _set_T_Local_to_Body( i, j, State->GetTl2b(i,j) );
398 void FGJSBsim::snap_shot(void) {
399 fgic->SetLatitudeRadIC(get_Lat_geocentric() );
400 fgic->SetLongitudeRadIC( get_Longitude() );
401 fgic->SetAltitudeFtIC( get_Altitude() );
402 fgic->SetTerrainAltitudeFtIC( get_Runway_altitude() );
403 fgic->SetVtrueFpsIC( get_V_rel_wind() );
404 fgic->SetPitchAngleRadIC( get_Theta() );
405 fgic->SetRollAngleRadIC( get_Phi() );
406 fgic->SetTrueHeadingRadIC( get_Psi() );
407 fgic->SetClimbRateFpsIC( get_Climb_Rate() );
411 bool FGJSBsim::ToggleDataLogging(void) {
412 return fdmex->GetOutput()->Toggle();
416 bool FGJSBsim::ToggleDataLogging(bool state) {
418 fdmex->GetOutput()->Enable();
421 fdmex->GetOutput()->Disable();
428 void FGJSBsim::set_Latitude(double lat) {
429 double sea_level_radius_meters,lat_geoc;
431 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat );
434 sgGeodToGeoc( lat, get_Altitude() , &sea_level_radius_meters, &lat_geoc);
435 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
436 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
437 fgic->SetLatitudeRadIC( lat_geoc );
438 fdmex->RunIC(fgic); //loop JSBSim once
439 copy_from_JSBsim(); //update the bus
443 void FGJSBsim::set_Longitude(double lon) {
445 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon );
448 fgic->SetLongitudeRadIC(lon);
449 fdmex->RunIC(fgic); //loop JSBSim once
450 copy_from_JSBsim(); //update the bus
454 void FGJSBsim::set_Altitude(double alt) {
455 double sea_level_radius_meters,lat_geoc;
457 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt );
460 sgGeodToGeoc( get_Latitude(), alt , &sea_level_radius_meters, &lat_geoc);
461 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
462 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
463 fgic->SetLatitudeRadIC( lat_geoc );
464 fgic->SetAltitudeFtIC(alt);
465 fdmex->RunIC(fgic); //loop JSBSim once
466 copy_from_JSBsim(); //update the bus
470 void FGJSBsim::set_V_calibrated_kts(double vc) {
471 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " << vc );
474 fgic->SetVcalibratedKtsIC(vc);
475 fdmex->RunIC(fgic); //loop JSBSim once
476 copy_from_JSBsim(); //update the bus
480 void FGJSBsim::set_Mach_number(double mach) {
481 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " << mach );
484 fgic->SetMachIC(mach);
485 fdmex->RunIC(fgic); //loop JSBSim once
486 copy_from_JSBsim(); //update the bus
490 void FGJSBsim::set_Velocities_Local( double north, double east, double down ){
491 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: "
492 << north << ", " << east << ", " << down );
495 fgic->SetVnorthFpsIC(north);
496 fgic->SetVeastFpsIC(east);
497 fgic->SetVdownFpsIC(down);
498 fdmex->RunIC(fgic); //loop JSBSim once
499 copy_from_JSBsim(); //update the bus
503 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w){
504 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
505 << u << ", " << v << ", " << w );
508 fgic->SetUBodyFpsIC(u);
509 fgic->SetVBodyFpsIC(v);
510 fgic->SetWBodyFpsIC(w);
511 fdmex->RunIC(fgic); //loop JSBSim once
512 copy_from_JSBsim(); //update the bus
517 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi ) {
518 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: "
519 << phi << ", " << theta << ", " << psi );
522 fgic->SetPitchAngleRadIC(theta);
523 fgic->SetRollAngleRadIC(phi);
524 fgic->SetTrueHeadingRadIC(psi);
525 fdmex->RunIC(fgic); //loop JSBSim once
526 copy_from_JSBsim(); //update the bus
531 void FGJSBsim::set_Climb_Rate( double roc) {
532 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
535 fgic->SetClimbRateFpsIC(roc);
536 fdmex->RunIC(fgic); //loop JSBSim once
537 copy_from_JSBsim(); //update the bus
541 void FGJSBsim::set_Gamma_vert_rad( double gamma) {
542 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
545 fgic->SetFlightPathAngleRadIC(gamma);
546 fdmex->RunIC(fgic); //loop JSBSim once
547 copy_from_JSBsim(); //update the bus
552 void FGJSBsim::set_Sea_level_radius(double slr) {
553 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Sea_level_radius: " << slr );
556 fgic->SetSeaLevelRadiusFtIC(slr);
557 fdmex->RunIC(fgic); //loop JSBSim once
558 copy_from_JSBsim(); //update the bus
562 void FGJSBsim::set_Runway_altitude(double ralt) {
563 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Runway_altitude: " << ralt );
566 _set_Runway_altitude( ralt );
567 fgic->SetTerrainAltitudeFtIC( ralt );
568 fdmex->RunIC(fgic); //loop JSBSim once
569 copy_from_JSBsim(); //update the bus
573 void FGJSBsim::set_Static_pressure(double p) {
574 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 );
586 Atmosphere->SetExTemperature(T);
587 if(Atmosphere->External() == true)
592 void FGJSBsim::set_Density(double rho) {
593 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Density: " << rho );
596 Atmosphere->SetExDensity(rho);
597 if(Atmosphere->External() == true)
602 void FGJSBsim::set_Velocities_Local_Airmass (double wnorth,
605 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local_Airmass: "
606 << wnorth << ", " << weast << ", " << wdown );
608 _set_Velocities_Local_Airmass( wnorth, weast, wdown );
610 Atmosphere->SetWindNED(wnorth, weast, wdown );
611 if(Atmosphere->External() == true)