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 FG_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/fgpath.hxx>
37 #include <Scenery/scenery.hxx>
39 #include <Aircraft/aircraft.hxx>
40 #include <Controls/controls.hxx>
41 #include <Main/globals.hxx>
43 #include <FDM/JSBSim/FGFDMExec.h>
44 #include <FDM/JSBSim/FGAircraft.h>
45 #include <FDM/JSBSim/FGFCS.h>
46 #include <FDM/JSBSim/FGPosition.h>
47 #include <FDM/JSBSim/FGRotation.h>
48 #include <FDM/JSBSim/FGState.h>
49 #include <FDM/JSBSim/FGTranslation.h>
50 #include <FDM/JSBSim/FGAuxiliary.h>
51 #include <FDM/JSBSim/FGDefs.h>
52 #include <FDM/JSBSim/FGInitialCondition.h>
53 #include <FDM/JSBSim/FGTrim.h>
54 #include <FDM/JSBSim/FGAtmosphere.h>
58 /******************************************************************************/
60 FGJSBsim::FGJSBsim(void) {
64 fgic=new FGInitialCondition(fdmex);
67 FGPath aircraft_path( globals->get_options()->get_fg_root() );
68 aircraft_path.append( "Aircraft" );
70 FGPath engine_path( globals->get_options()->get_fg_root() );
71 engine_path.append( "Engine" );
72 float dt = 1.0 / globals->get_options()->get_model_hz();
73 fdmex->GetState()->Setdt( dt );
75 result = fdmex->LoadModel( aircraft_path.str(),
77 globals->get_options()->get_aircraft() );
78 int Neng=fdmex->GetAircraft()->GetNumEngines();
79 FG_LOG(FG_FLIGHT,FG_INFO, "Neng: " << Neng );
80 for(int i=0;i<Neng;i++) {
81 add_engine( FGEngInterface() );
86 /******************************************************************************/
87 FGJSBsim::~FGJSBsim(void) {
94 /******************************************************************************/
96 // Initialize the JSBsim flight model, dt is the time increment for
97 // each subsequent iteration through the EOM
99 bool FGJSBsim::init( double dt ) {
103 FG_LOG( FG_FLIGHT, FG_INFO, "Starting and initializing JSBsim" );
106 FGPath aircraft_path( globals->get_options()->get_fg_root() );
107 aircraft_path.append( "Aircraft" );
109 FGPath engine_path( globals->get_options()->get_fg_root() );
110 engine_path.append( "Engine" );
112 fdmex->GetState()->Setdt( dt );
114 result = fdmex->LoadModel( aircraft_path.str(),
116 globals->get_options()->get_aircraft() );
120 FG_LOG( FG_FLIGHT, FG_INFO, " loaded aircraft" << globals->get_options()->get_aircraft() );
122 FG_LOG( FG_FLIGHT, FG_INFO, " aircraft "
123 << globals->get_options()->get_aircraft()
124 << " does not exist" );
128 fdmex->GetAtmosphere()->UseInternal();
130 FG_LOG( FG_FLIGHT, FG_INFO, " Initializing JSBSim with:" );
131 switch(fgic->GetSpeedSet()) {
133 FG_LOG(FG_FLIGHT,FG_INFO, " Vn,Ve,Vd= "
134 << fdmex->GetPosition()->GetVn()
135 << ", " << fdmex->GetPosition()->GetVe()
136 << ", " << fdmex->GetPosition()->GetVd()
140 FG_LOG(FG_FLIGHT,FG_INFO, " U,V,W= "
141 << fdmex->GetTranslation()->GetUVW()(1)
142 << ", " << fdmex->GetTranslation()->GetUVW()(2)
143 << ", " << fdmex->GetTranslation()->GetUVW()(3)
147 FG_LOG(FG_FLIGHT,FG_INFO, " Mach: "
148 << fdmex->GetTranslation()->GetMach() );
152 FG_LOG(FG_FLIGHT,FG_INFO, " Indicated Airspeed: "
153 << fdmex->GetAuxiliary()->GetVcalibratedKTS() << " knots" );
157 //FG_LOG( FG_FLIGHT, FG_INFO, " gamma: " << globals->get_options()->get_Gamma());
158 FG_LOG( FG_FLIGHT, FG_INFO, " Bank Angle: "
159 << fdmex->GetRotation()->Getphi()*RADTODEG << " deg");
160 FG_LOG( FG_FLIGHT, FG_INFO, " Pitch Angle: "
161 << fdmex->GetRotation()->Gettht()*RADTODEG << " deg" );
162 FG_LOG( FG_FLIGHT, FG_INFO, " True Heading: "
163 << fdmex->GetRotation()->Getpsi()*RADTODEG << " deg" );
164 FG_LOG( FG_FLIGHT, FG_INFO, " Latitude: "
165 << fdmex->GetPosition()->GetLatitude() << " deg" );
166 FG_LOG( FG_FLIGHT, FG_INFO, " Longitude: "
167 << fdmex->GetPosition()->GetLongitude() << " deg" );
170 /* FG_LOG( FG_FLIGHT, FG_DEBUG, " FGJSBSim::get_Altitude(): " << get_Altitude() );
171 FG_LOG( FG_FLIGHT, FG_DEBUG, " FGJSBSim::get_Sea_level_radius(): " << get_Sea_level_radius() );
172 FG_LOG( FG_FLIGHT, FG_DEBUG, " scenery.cur_radius*METER_TO_FEET: "
173 << scenery.cur_radius*METER_TO_FEET );
174 FG_LOG( FG_FLIGHT, FG_DEBUG, " Calculated Terrain ASL: " << endl
175 << " " << "scenery.cur_radius*METER_TO_FEET -get_Sea_level_radius()= "
176 << scenery.cur_radius*METER_TO_FEET - get_Sea_level_radius() );
178 FG_LOG( FG_FLIGHT, FG_DEBUG, " Calculated Aircraft AGL: " << endl
179 << " " << "get_Altitude() + get_Sea_level_radius() - scenery.cur_radius*METER_TO_FEET= "
180 << get_Altitude() + get_Sea_level_radius()- scenery.cur_radius*METER_TO_FEET );
181 FG_LOG( FG_FLIGHT, FG_DEBUG, " globals->get_options()->get_altitude(): "
182 << globals->get_options()->get_altitude() );
183 FG_LOG( FG_FLIGHT, FG_DEBUG, " FGBFI::getAltitude(): "
184 << FGBFI::getAltitude() ); */
187 FG_LOG( FG_FLIGHT, FG_INFO, " loaded initial conditions" );
189 FG_LOG( FG_FLIGHT, FG_INFO, " set dt" );
191 FG_LOG( FG_FLIGHT, FG_INFO, "Finished initializing JSBSim" );
196 /******************************************************************************/
198 // Run an iteration of the EOM (equations of motion)
200 bool FGJSBsim::update( int multiloop ) {
204 double save_alt = 0.0;
207 // lets try to avoid really screwing up the JSBsim model
208 if ( get_Altitude() < -9000 ) {
209 save_alt = get_Altitude();
216 FGTrim *fgtrim=new FGTrim(fdmex,fgic,tLongitudinal);
217 if(!fgtrim->DoTrim()) {
221 fgtrim->ReportState();
226 controls.set_elevator_trim(fdmex->GetFCS()->GetPitchTrimCmd());
227 controls.set_elevator(fdmex->GetFCS()->GetDeCmd());
228 controls.set_throttle(FGControls::ALL_ENGINES,
229 fdmex->GetFCS()->GetThrottleCmd(0)/100.0);
230 controls.set_aileron(fdmex->GetFCS()->GetDaCmd());
231 controls.set_rudder(fdmex->GetFCS()->GetDrCmd());
233 FG_LOG( FG_FLIGHT, FG_INFO, " Trim complete" );
236 for( i=0; i<get_num_engines(); i++ ) {
237 get_engine(i)->set_RPM( controls.get_throttle(i)*2700 );
238 get_engine(i)->set_Throttle( controls.get_throttle(i) );
242 for ( int i = 0; i < multiloop; i++ ) {
246 // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
247 // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
249 // translate JSBsim back to FG structure so that the
250 // autopilot (and the rest of the sim can use the updated values
254 // but lets restore our original bogus altitude when we are done
256 if ( save_alt < -9000.0 ) {
257 set_Altitude( save_alt );
260 //climb rate now set from FDM in copy_from_x()
264 /******************************************************************************/
266 // Convert from the FGInterface struct to the JSBsim generic_ struct
268 bool FGJSBsim::copy_to_JSBsim() {
269 // copy control positions into the JSBsim structure
271 fdmex->GetFCS()->SetDaCmd( controls.get_aileron());
272 fdmex->GetFCS()->SetDeCmd( controls.get_elevator());
273 fdmex->GetFCS()->SetPitchTrimCmd(controls.get_elevator_trim());
274 fdmex->GetFCS()->SetDrCmd( -1*controls.get_rudder());
275 fdmex->GetFCS()->SetDfCmd( controls.get_flaps() );
276 fdmex->GetFCS()->SetDsbCmd( 0.0 ); //speedbrakes
277 fdmex->GetFCS()->SetDspCmd( 0.0 ); //spoilers
278 fdmex->GetFCS()->SetThrottleCmd( FGControls::ALL_ENGINES,
279 controls.get_throttle( 0 ) * 100.0 );
281 fdmex->GetFCS()->SetLBrake( controls.get_brake( 0 ) );
282 fdmex->GetFCS()->SetRBrake( controls.get_brake( 1 ) );
283 fdmex->GetFCS()->SetCBrake( controls.get_brake( 2 ) );
285 fdmex->GetPosition()->SetRunwayRadius(scenery.cur_radius*METER_TO_FEET);
286 fdmex->GetPosition()->SetSeaLevelRadius(get_Sea_level_radius());
288 fdmex->GetAtmosphere()->SetExTemperature(get_Static_temperature());
289 fdmex->GetAtmosphere()->SetExPressure(get_Static_pressure());
290 fdmex->GetAtmosphere()->SetExDensity(get_Density());
291 fdmex->GetAtmosphere()->SetWindNED(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( fdmex->GetAircraft()->GetMass(),
306 fdmex->GetAircraft()->GetIxx(),
307 fdmex->GetAircraft()->GetIyy(),
308 fdmex->GetAircraft()->GetIzz(),
309 fdmex->GetAircraft()->GetIxz() );
311 _set_CG_Position( fdmex->GetAircraft()->GetXYZcg()(1),
312 fdmex->GetAircraft()->GetXYZcg()(2),
313 fdmex->GetAircraft()->GetXYZcg()(3) );
315 _set_Accels_Body( fdmex->GetTranslation()->GetUVWdot()(1),
316 fdmex->GetTranslation()->GetUVWdot()(2),
317 fdmex->GetTranslation()->GetUVWdot()(3) );
319 _set_Accels_CG_Body( fdmex->GetTranslation()->GetUVWdot()(1),
320 fdmex->GetTranslation()->GetUVWdot()(2),
321 fdmex->GetTranslation()->GetUVWdot()(3) );
323 //_set_Accels_CG_Body_N ( fdmex->GetTranslation()->GetNcg()(1),
324 // fdmex->GetTranslation()->GetNcg()(2),
325 // fdmex->GetTranslation()->GetNcg()(3) );
327 _set_Accels_Pilot_Body( fdmex->GetAuxiliary()->GetPilotAccel()(1),
328 fdmex->GetAuxiliary()->GetPilotAccel()(2),
329 fdmex->GetAuxiliary()->GetPilotAccel()(3) );
331 //_set_Accels_Pilot_Body_N( fdmex->GetAuxiliary()->GetNpilot()(1),
332 // fdmex->GetAuxiliary()->GetNpilot()(2),
333 // fdmex->GetAuxiliary()->GetNpilot()(3) );
335 _set_Nlf( fdmex->GetAircraft()->GetNlf() );
339 _set_Velocities_Local( fdmex->GetPosition()->GetVn(),
340 fdmex->GetPosition()->GetVe(),
341 fdmex->GetPosition()->GetVd() );
343 _set_Velocities_Wind_Body( fdmex->GetTranslation()->GetUVW()(1),
344 fdmex->GetTranslation()->GetUVW()(2),
345 fdmex->GetTranslation()->GetUVW()(3) );
347 _set_V_equiv_kts( fdmex->GetAuxiliary()->GetVequivalentKTS() );
349 // _set_V_calibrated( fdmex->GetAuxiliary()->GetVcalibratedFPS() );
351 _set_V_calibrated_kts( fdmex->GetAuxiliary()->GetVcalibratedKTS() );
353 _set_V_ground_speed( fdmex->GetPosition()->GetVground() );
355 _set_Omega_Body( fdmex->GetRotation()->GetPQR()(1),
356 fdmex->GetRotation()->GetPQR()(2),
357 fdmex->GetRotation()->GetPQR()(3) );
359 _set_Euler_Rates( fdmex->GetRotation()->GetEulerRates()(1),
360 fdmex->GetRotation()->GetEulerRates()(2),
361 fdmex->GetRotation()->GetEulerRates()(3) );
363 _set_Geocentric_Rates(fdmex->GetPosition()->GetLatitudeDot(),
364 fdmex->GetPosition()->GetLongitudeDot(),
365 fdmex->GetPosition()->Gethdot() );
367 _set_Mach_number( fdmex->GetTranslation()->GetMach() );
371 double lat_geoc = fdmex->GetPosition()->GetLatitude();
372 double lon = fdmex->GetPosition()->GetLongitude();
373 double alt = fdmex->GetPosition()->Geth();
374 double lat_geod, tmp_alt, sl_radius1, sl_radius2, tmp_lat_geoc;
376 sgGeocToGeod( lat_geoc, EQUATORIAL_RADIUS_M + alt * FEET_TO_METER,
377 &lat_geod, &tmp_alt, &sl_radius1 );
378 sgGeodToGeoc( lat_geod, alt * FEET_TO_METER, &sl_radius2, &tmp_lat_geoc );
380 FG_LOG( FG_FLIGHT, FG_DEBUG, "lon = " << lon << " lat_geod = " << lat_geod
381 << " lat_geoc = " << lat_geoc
382 << " alt = " << alt << " tmp_alt = " << tmp_alt * METER_TO_FEET
383 << " sl_radius1 = " << sl_radius1 * METER_TO_FEET
384 << " sl_radius2 = " << sl_radius2 * METER_TO_FEET
385 << " Equator = " << EQUATORIAL_RADIUS_FT );
387 _set_Geocentric_Position( lat_geoc, lon, sl_radius2 * METER_TO_FEET + alt );
389 _set_Geodetic_Position( lat_geod, lon, alt );
391 _set_Euler_Angles( fdmex->GetRotation()->Getphi(),
392 fdmex->GetRotation()->Gettht(),
393 fdmex->GetRotation()->Getpsi() );
395 _set_Alpha( fdmex->GetTranslation()->Getalpha() );
396 _set_Beta( fdmex->GetTranslation()->Getbeta() );
398 _set_Gamma_vert_rad( fdmex->GetPosition()->GetGamma() );
399 // set_Gamma_horiz_rad( Gamma_horiz_rad );
401 /* **FIXME*** */ _set_Sea_level_radius( sl_radius2 * METER_TO_FEET );
402 /* **FIXME*** */ _set_Earth_position_angle( fdmex->GetAuxiliary()->
403 GetEarthPositionAngle() );
405 /* ***FIXME*** */ _set_Runway_altitude( scenery.cur_radius * METERS_TO_FEET -
406 get_Sea_level_radius() );
408 _set_sin_lat_geocentric( lat_geoc );
409 _set_cos_lat_geocentric( lat_geoc );
411 _set_sin_cos_longitude( lon );
413 _set_sin_cos_latitude( lat_geod );
415 _set_Climb_Rate( fdmex->GetPosition()->Gethdot() );
417 for ( i = 0; i < 3; i++ ) {
418 for ( j = 0; j < 3; j++ ) {
419 _set_T_Local_to_Body( i, j, fdmex->GetState()->GetTl2b()(i,j) );
426 void FGJSBsim::set_Latitude(double lat) {
427 FG_LOG(FG_FLIGHT,FG_INFO,"FGJSBsim::set_Latitude: " << lat);
428 fgic->SetLatitudeRadIC(lat);
429 fdmex->RunIC(fgic); //loop JSBSim once
430 copy_from_JSBsim(); //update the bus
434 void FGJSBsim::set_Longitude(double lon) {
435 FG_LOG(FG_FLIGHT,FG_INFO,"FGJSBsim::set_Longitude: " << lon);
436 fgic->SetLongitudeRadIC(lon);
437 fdmex->RunIC(fgic); //loop JSBSim once
438 copy_from_JSBsim(); //update the bus
442 void FGJSBsim::set_Altitude(double alt) {
443 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Altitude: " << alt );
444 fgic->SetAltitudeFtIC(alt);
445 fdmex->RunIC(fgic); //loop JSBSim once
446 copy_from_JSBsim(); //update the bus
450 void FGJSBsim::set_V_calibrated_kts(double vc) {
451 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_V_calibrated_kts: " << vc );
452 fgic->SetVcalibratedKtsIC(vc);
453 fdmex->RunIC(fgic); //loop JSBSim once
454 copy_from_JSBsim(); //update the bus
458 void FGJSBsim::set_Mach_number(double mach) {
459 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Mach_number: " << mach );
460 fgic->SetMachIC(mach);
461 fdmex->RunIC(fgic); //loop JSBSim once
462 copy_from_JSBsim(); //update the bus
466 void FGJSBsim::set_Velocities_Local( double north, double east, double down ){
467 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Velocities_Local: "
468 << north << ", " << east << ", " << down );
469 fgic->SetVnorthFpsIC(north);
470 fgic->SetVeastFpsIC(east);
471 fgic->SetVdownFpsIC(down);
472 fdmex->RunIC(fgic); //loop JSBSim once
473 cout << "fdmex->GetTranslation()->GetVt(): " << fdmex->GetTranslation()->GetVt() << endl;
474 cout << "fdmex->GetPosition()->GetVn(): " << fdmex->GetPosition()->GetVn() << endl;
476 copy_from_JSBsim(); //update the bus
481 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w){
482 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
483 << u << ", " << v << ", " << w );
485 fgic->SetUBodyFpsIC(u);
486 fgic->SetVBodyFpsIC(v);
487 fgic->SetWBodyFpsIC(w);
488 fdmex->RunIC(fgic); //loop JSBSim once
489 copy_from_JSBsim(); //update the bus
494 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi ) {
495 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Euler_Angles: "
496 << phi << ", " << theta << ", " << psi );
497 fgic->SetPitchAngleRadIC(theta);
498 fgic->SetRollAngleRadIC(phi);
499 fgic->SetTrueHeadingRadIC(psi);
500 fdmex->RunIC(fgic); //loop JSBSim once
501 copy_from_JSBsim(); //update the bus
506 void FGJSBsim::set_Climb_Rate( double roc) {
507 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
508 fgic->SetClimbRateFpsIC(roc);
509 fdmex->RunIC(fgic); //loop JSBSim once
510 copy_from_JSBsim(); //update the bus
514 void FGJSBsim::set_Gamma_vert_rad( double gamma) {
515 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
516 fgic->SetFlightPathAngleRadIC(gamma);
517 fdmex->RunIC(fgic); //loop JSBSim once
518 copy_from_JSBsim(); //update the bus
523 void FGJSBsim::set_Sea_level_radius(double slr) {
524 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Sea_level_radius: " << slr );
525 fgic->SetSeaLevelRadiusFtIC(slr);
526 fdmex->RunIC(fgic); //loop JSBSim once
527 copy_from_JSBsim(); //update the bus
531 void FGJSBsim::set_Runway_altitude(double ralt) {
532 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Runway_altitude: " << ralt );
533 _set_Runway_altitude( ralt );
534 fdmex->RunIC(fgic); //loop JSBSim once
535 copy_from_JSBsim(); //update the bus
539 void FGJSBsim::set_Static_pressure(double p) {
540 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Static_pressure: " << p );
541 fdmex->GetAtmosphere()->SetExPressure(p);
542 if(fdmex->GetAtmosphere()->External() == true)
546 void FGJSBsim::set_Static_temperature(double T) {
547 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Static_temperature: " << T );
548 fdmex->GetAtmosphere()->SetExTemperature(T);
549 if(fdmex->GetAtmosphere()->External() == true)
554 void FGJSBsim::set_Density(double rho) {
555 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Density: " << rho );
556 fdmex->GetAtmosphere()->SetExDensity(rho);
557 if(fdmex->GetAtmosphere()->External() == true)
562 void FGJSBsim::set_Velocities_Local_Airmass (double wnorth,
565 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Velocities_Local_Airmass: "
566 << wnorth << ", " << weast << ", " << wdown );
567 fdmex->GetAtmosphere()->SetWindNED(wnorth, weast, wdown );
568 if(fdmex->GetAtmosphere()->External() == true)