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>
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>
59 /******************************************************************************/
61 FGJSBsim::FGJSBsim( double dt ) {
67 fgic=new FGInitialCondition(fdmex);
70 FGPath aircraft_path( globals->get_fg_root() );
71 aircraft_path.append( "Aircraft" );
73 FGPath engine_path( globals->get_fg_root() );
74 engine_path.append( "Engine" );
76 fdmex->GetState()->Setdt( dt );
78 result = fdmex->LoadModel( aircraft_path.str(),
80 fgGetString("/sim/aircraft") );
81 int Neng=fdmex->GetAircraft()->GetNumEngines();
82 FG_LOG(FG_FLIGHT,FG_INFO, "Neng: " << Neng );
83 for(int i=0;i<Neng;i++) {
84 add_engine( FGEngInterface() );
89 /******************************************************************************/
90 FGJSBsim::~FGJSBsim(void) {
97 /******************************************************************************/
99 // Initialize the JSBsim flight model, dt is the time increment for
100 // each subsequent iteration through the EOM
102 void FGJSBsim::init() {
106 FG_LOG( FG_FLIGHT, FG_INFO, "Starting and initializing JSBsim" );
109 FGPath aircraft_path( globals->get_fg_root() );
110 aircraft_path.append( "Aircraft" );
112 FGPath engine_path( globals->get_fg_root() );
113 engine_path.append( "Engine" );
115 fdmex->GetState()->Setdt( get_delta_t() );
117 result = fdmex->LoadModel( aircraft_path.str(),
119 fgGetString("/sim/aircraft") );
123 FG_LOG( FG_FLIGHT, FG_INFO, " loaded aircraft " << fgGetString("/sim/aircraft") );
125 FG_LOG( FG_FLIGHT, FG_INFO, " aircraft "
126 << fgGetString("/sim/aircraft")
127 << " does not exist" );
131 fdmex->GetAtmosphere()->UseInternal();
133 FG_LOG( FG_FLIGHT, FG_INFO, " Initializing JSBSim with:" );
134 switch(fgic->GetSpeedSet()) {
136 FG_LOG(FG_FLIGHT,FG_INFO, " Vn,Ve,Vd= "
137 << fdmex->GetPosition()->GetVn()
138 << ", " << fdmex->GetPosition()->GetVe()
139 << ", " << fdmex->GetPosition()->GetVd()
143 FG_LOG(FG_FLIGHT,FG_INFO, " U,V,W= "
144 << fdmex->GetTranslation()->GetUVW()(1)
145 << ", " << fdmex->GetTranslation()->GetUVW()(2)
146 << ", " << fdmex->GetTranslation()->GetUVW()(3)
150 FG_LOG(FG_FLIGHT,FG_INFO, " Mach: "
151 << fdmex->GetTranslation()->GetMach() );
155 FG_LOG(FG_FLIGHT,FG_INFO, " Indicated Airspeed: "
156 << fdmex->GetAuxiliary()->GetVcalibratedKTS() << " knots" );
160 FG_LOG( FG_FLIGHT, FG_INFO, " Bank Angle: "
161 << fdmex->GetRotation()->Getphi()*RADTODEG << " deg");
162 FG_LOG( FG_FLIGHT, FG_INFO, " Pitch Angle: "
163 << fdmex->GetRotation()->Gettht()*RADTODEG << " deg" );
164 FG_LOG( FG_FLIGHT, FG_INFO, " True Heading: "
165 << fdmex->GetRotation()->Getpsi()*RADTODEG << " deg" );
166 FG_LOG( FG_FLIGHT, FG_INFO, " Latitude: "
167 << fdmex->GetPosition()->GetLatitude() << " deg" );
168 FG_LOG( FG_FLIGHT, FG_INFO, " Longitude: "
169 << fdmex->GetPosition()->GetLongitude() << " deg" );
172 /* FG_LOG( FG_FLIGHT, FG_DEBUG, " FGJSBSim::get_Altitude(): " << get_Altitude() );
173 FG_LOG( FG_FLIGHT, FG_DEBUG, " FGJSBSim::get_Sea_level_radius(): " << get_Sea_level_radius() );
174 FG_LOG( FG_FLIGHT, FG_DEBUG, " scenery.cur_radius*METER_TO_FEET: "
175 << scenery.cur_radius*METER_TO_FEET );
176 FG_LOG( FG_FLIGHT, FG_DEBUG, " Calculated Terrain ASL: " << endl
177 << " " << "scenery.cur_radius*METER_TO_FEET -get_Sea_level_radius()= "
178 << scenery.cur_radius*METER_TO_FEET - get_Sea_level_radius() );
180 FG_LOG( FG_FLIGHT, FG_DEBUG, " Calculated Aircraft AGL: " << endl
181 << " " << "get_Altitude() + get_Sea_level_radius() - scenery.cur_radius*METER_TO_FEET= "
182 << get_Altitude() + get_Sea_level_radius()- scenery.cur_radius*METER_TO_FEET );
183 FG_LOG( FG_FLIGHT, FG_DEBUG, " fgGetDouble("/position/altitude"): "
184 << fgGetDouble("/position/altitude") );
185 FG_LOG( FG_FLIGHT, FG_DEBUG, " FGBFI::getAltitude(): "
186 << FGBFI::getAltitude() ); */
189 FG_LOG( FG_FLIGHT, FG_INFO, " loaded initial conditions" );
191 FG_LOG( FG_FLIGHT, FG_INFO, " set dt" );
193 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;
209 if(needTrim && fgGetBool("/sim/startup/trim")) {
210 //fgic->SetSeaLevelRadiusFtIC( get_Sea_level_radius() );
211 //fgic->SetTerrainAltitudeFtIC( scenery.cur_elev * METER_TO_FEET );
213 if(fgic->GetVcalibratedKtsIC() < 10 ) {
214 fgic->SetVcalibratedKtsIC(0.0);
215 fgtrim=new FGTrim(fdmex,fgic,tGround);
217 fgtrim=new FGTrim(fdmex,fgic,tLongitudinal);
219 if(!fgtrim->DoTrim()) {
223 fgtrim->ReportState();
230 controls.set_elevator_trim(fdmex->GetFCS()->GetPitchTrimCmd());
231 controls.set_elevator(fdmex->GetFCS()->GetDeCmd());
232 controls.set_throttle(FGControls::ALL_ENGINES,
233 fdmex->GetFCS()->GetThrottleCmd(0)/100.0);
234 controls.set_aileron(fdmex->GetFCS()->GetDaCmd());
235 controls.set_rudder(fdmex->GetFCS()->GetDrCmd());
237 FG_LOG( FG_FLIGHT, FG_INFO, " Trim complete" );
240 for( i=0; i<get_num_engines(); i++ ) {
241 get_engine(i)->set_RPM( controls.get_throttle(i)*2700 );
242 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 // but lets restore our original bogus altitude when we are done
264 //climb rate now set from FDM in copy_from_x()
268 /******************************************************************************/
270 // Convert from the FGInterface struct to the JSBsim generic_ struct
272 bool FGJSBsim::copy_to_JSBsim() {
273 // copy control positions into the JSBsim structure
275 fdmex->GetFCS()->SetDaCmd( controls.get_aileron());
276 fdmex->GetFCS()->SetDeCmd( controls.get_elevator());
277 fdmex->GetFCS()->SetPitchTrimCmd(controls.get_elevator_trim());
278 fdmex->GetFCS()->SetDrCmd( -1*controls.get_rudder());
279 fdmex->GetFCS()->SetDfCmd( controls.get_flaps() );
280 fdmex->GetFCS()->SetDsbCmd( 0.0 ); //speedbrakes
281 fdmex->GetFCS()->SetDspCmd( 0.0 ); //spoilers
282 fdmex->GetFCS()->SetThrottleCmd( FGControls::ALL_ENGINES,
283 controls.get_throttle( 0 ) * 100.0 );
285 fdmex->GetFCS()->SetLBrake( controls.get_brake( 0 ) );
286 fdmex->GetFCS()->SetRBrake( controls.get_brake( 1 ) );
287 fdmex->GetFCS()->SetCBrake( controls.get_brake( 2 ) );
289 fdmex->GetPosition()->SetSeaLevelRadius( get_Sea_level_radius() );
290 fdmex->GetPosition()->SetRunwayRadius( scenery.cur_elev*METER_TO_FEET
291 + get_Sea_level_radius() );
293 fdmex->GetAtmosphere()->SetExTemperature(get_Static_temperature());
294 fdmex->GetAtmosphere()->SetExPressure(get_Static_pressure());
295 fdmex->GetAtmosphere()->SetExDensity(get_Density());
296 fdmex->GetAtmosphere()->SetWindNED(get_V_north_airmass(),
297 get_V_east_airmass(),
298 get_V_down_airmass());
303 /******************************************************************************/
305 // Convert from the JSBsim generic_ struct to the FGInterface struct
307 bool FGJSBsim::copy_from_JSBsim() {
310 _set_Inertias( fdmex->GetAircraft()->GetMass(),
311 fdmex->GetAircraft()->GetIxx(),
312 fdmex->GetAircraft()->GetIyy(),
313 fdmex->GetAircraft()->GetIzz(),
314 fdmex->GetAircraft()->GetIxz() );
316 _set_CG_Position( fdmex->GetAircraft()->GetXYZcg()(1),
317 fdmex->GetAircraft()->GetXYZcg()(2),
318 fdmex->GetAircraft()->GetXYZcg()(3) );
320 _set_Accels_Body( fdmex->GetTranslation()->GetUVWdot()(1),
321 fdmex->GetTranslation()->GetUVWdot()(2),
322 fdmex->GetTranslation()->GetUVWdot()(3) );
324 _set_Accels_CG_Body( fdmex->GetTranslation()->GetUVWdot()(1),
325 fdmex->GetTranslation()->GetUVWdot()(2),
326 fdmex->GetTranslation()->GetUVWdot()(3) );
328 //_set_Accels_CG_Body_N ( fdmex->GetTranslation()->GetNcg()(1),
329 // fdmex->GetTranslation()->GetNcg()(2),
330 // fdmex->GetTranslation()->GetNcg()(3) );
332 _set_Accels_Pilot_Body( fdmex->GetAuxiliary()->GetPilotAccel()(1),
333 fdmex->GetAuxiliary()->GetPilotAccel()(2),
334 fdmex->GetAuxiliary()->GetPilotAccel()(3) );
336 //_set_Accels_Pilot_Body_N( fdmex->GetAuxiliary()->GetNpilot()(1),
337 // fdmex->GetAuxiliary()->GetNpilot()(2),
338 // fdmex->GetAuxiliary()->GetNpilot()(3) );
340 _set_Nlf( fdmex->GetAircraft()->GetNlf() );
344 _set_Velocities_Local( fdmex->GetPosition()->GetVn(),
345 fdmex->GetPosition()->GetVe(),
346 fdmex->GetPosition()->GetVd() );
348 _set_Velocities_Wind_Body( fdmex->GetTranslation()->GetUVW()(1),
349 fdmex->GetTranslation()->GetUVW()(2),
350 fdmex->GetTranslation()->GetUVW()(3) );
352 _set_V_rel_wind( fdmex->GetTranslation()->GetVt() );
354 _set_V_equiv_kts( fdmex->GetAuxiliary()->GetVequivalentKTS() );
356 // _set_V_calibrated( fdmex->GetAuxiliary()->GetVcalibratedFPS() );
358 _set_V_calibrated_kts( fdmex->GetAuxiliary()->GetVcalibratedKTS() );
360 _set_V_ground_speed( fdmex->GetPosition()->GetVground() );
362 _set_Omega_Body( fdmex->GetRotation()->GetPQR()(1),
363 fdmex->GetRotation()->GetPQR()(2),
364 fdmex->GetRotation()->GetPQR()(3) );
366 _set_Euler_Rates( fdmex->GetRotation()->GetEulerRates()(1),
367 fdmex->GetRotation()->GetEulerRates()(2),
368 fdmex->GetRotation()->GetEulerRates()(3) );
370 _set_Geocentric_Rates(fdmex->GetPosition()->GetLatitudeDot(),
371 fdmex->GetPosition()->GetLongitudeDot(),
372 fdmex->GetPosition()->Gethdot() );
374 _set_Mach_number( fdmex->GetTranslation()->GetMach() );
377 _updatePosition( fdmex->GetPosition()->GetLatitude(),
378 fdmex->GetPosition()->GetLongitude(),
379 fdmex->GetPosition()->Geth() );
381 _set_Euler_Angles( fdmex->GetRotation()->Getphi(),
382 fdmex->GetRotation()->Gettht(),
383 fdmex->GetRotation()->Getpsi() );
385 _set_Alpha( fdmex->GetTranslation()->Getalpha() );
386 _set_Beta( fdmex->GetTranslation()->Getbeta() );
389 _set_Gamma_vert_rad( fdmex->GetPosition()->GetGamma() );
390 // set_Gamma_horiz_rad( Gamma_horiz_rad );
392 _set_Earth_position_angle( fdmex->GetAuxiliary()->GetEarthPositionAngle() );
394 _set_Climb_Rate( fdmex->GetPosition()->Gethdot() );
397 for ( i = 1; i <= 3; i++ ) {
398 for ( j = 1; j <= 3; j++ ) {
399 _set_T_Local_to_Body( i, j, fdmex->GetState()->GetTl2b()(i,j) );
405 void FGJSBsim::snap_shot(void) {
406 fgic->SetLatitudeRadIC(get_Lat_geocentric() );
407 fgic->SetLongitudeRadIC( get_Longitude() );
408 fgic->SetAltitudeFtIC( get_Altitude() );
409 fgic->SetTerrainAltitudeFtIC( get_Runway_altitude() );
410 fgic->SetVtrueFpsIC( get_V_rel_wind() );
411 fgic->SetPitchAngleRadIC( get_Theta() );
412 fgic->SetRollAngleRadIC( get_Phi() );
413 fgic->SetTrueHeadingRadIC( get_Psi() );
414 fgic->SetClimbRateFpsIC( get_Climb_Rate() );
419 void FGJSBsim::set_Latitude(double lat) {
420 double sea_level_radius_meters,lat_geoc;
422 FG_LOG(FG_FLIGHT,FG_INFO,"FGJSBsim::set_Latitude: " << lat );
425 sgGeodToGeoc( lat, get_Altitude() , &sea_level_radius_meters, &lat_geoc);
426 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * METER_TO_FEET );
427 fgic->SetLatitudeRadIC( lat_geoc );
428 fdmex->RunIC(fgic); //loop JSBSim once
429 copy_from_JSBsim(); //update the bus
433 void FGJSBsim::set_Longitude(double lon) {
435 FG_LOG(FG_FLIGHT,FG_INFO,"FGJSBsim::set_Longitude: " << lon );
438 fgic->SetLongitudeRadIC(lon);
439 fdmex->RunIC(fgic); //loop JSBSim once
440 copy_from_JSBsim(); //update the bus
444 void FGJSBsim::set_Altitude(double alt) {
445 double sea_level_radius_meters,lat_geoc;
447 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Altitude: " << alt );
450 sgGeodToGeoc( get_Latitude(), alt , &sea_level_radius_meters, &lat_geoc);
451 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * METER_TO_FEET );
452 fgic->SetLatitudeRadIC( lat_geoc );
453 fgic->SetAltitudeFtIC(alt);
454 fdmex->RunIC(fgic); //loop JSBSim once
455 copy_from_JSBsim(); //update the bus
459 void FGJSBsim::set_V_calibrated_kts(double vc) {
460 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_V_calibrated_kts: " << vc );
463 fgic->SetVcalibratedKtsIC(vc);
464 fdmex->RunIC(fgic); //loop JSBSim once
465 copy_from_JSBsim(); //update the bus
469 void FGJSBsim::set_Mach_number(double mach) {
470 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Mach_number: " << mach );
473 fgic->SetMachIC(mach);
474 fdmex->RunIC(fgic); //loop JSBSim once
475 copy_from_JSBsim(); //update the bus
479 void FGJSBsim::set_Velocities_Local( double north, double east, double down ){
480 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Velocities_Local: "
481 << north << ", " << east << ", " << down );
484 fgic->SetVnorthFpsIC(north);
485 fgic->SetVeastFpsIC(east);
486 fgic->SetVdownFpsIC(down);
487 fdmex->RunIC(fgic); //loop JSBSim once
488 copy_from_JSBsim(); //update the bus
492 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w){
493 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
494 << u << ", " << v << ", " << w );
497 fgic->SetUBodyFpsIC(u);
498 fgic->SetVBodyFpsIC(v);
499 fgic->SetWBodyFpsIC(w);
500 fdmex->RunIC(fgic); //loop JSBSim once
501 copy_from_JSBsim(); //update the bus
506 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi ) {
507 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Euler_Angles: "
508 << phi << ", " << theta << ", " << psi );
511 fgic->SetPitchAngleRadIC(theta);
512 fgic->SetRollAngleRadIC(phi);
513 fgic->SetTrueHeadingRadIC(psi);
514 fdmex->RunIC(fgic); //loop JSBSim once
515 copy_from_JSBsim(); //update the bus
520 void FGJSBsim::set_Climb_Rate( double roc) {
521 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
524 fgic->SetClimbRateFpsIC(roc);
525 fdmex->RunIC(fgic); //loop JSBSim once
526 copy_from_JSBsim(); //update the bus
530 void FGJSBsim::set_Gamma_vert_rad( double gamma) {
531 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
534 fgic->SetFlightPathAngleRadIC(gamma);
535 fdmex->RunIC(fgic); //loop JSBSim once
536 copy_from_JSBsim(); //update the bus
541 void FGJSBsim::set_Sea_level_radius(double slr) {
542 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Sea_level_radius: " << slr );
545 fgic->SetSeaLevelRadiusFtIC(slr);
546 fdmex->RunIC(fgic); //loop JSBSim once
547 copy_from_JSBsim(); //update the bus
551 void FGJSBsim::set_Runway_altitude(double ralt) {
552 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Runway_altitude: " << ralt );
555 _set_Runway_altitude( ralt );
556 fgic->SetTerrainAltitudeFtIC( ralt );
557 fdmex->RunIC(fgic); //loop JSBSim once
558 copy_from_JSBsim(); //update the bus
562 void FGJSBsim::set_Static_pressure(double p) {
563 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Static_pressure: " << p );
566 fdmex->GetAtmosphere()->SetExPressure(p);
567 if(fdmex->GetAtmosphere()->External() == true)
571 void FGJSBsim::set_Static_temperature(double T) {
572 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Static_temperature: " << T );
575 fdmex->GetAtmosphere()->SetExTemperature(T);
576 if(fdmex->GetAtmosphere()->External() == true)
581 void FGJSBsim::set_Density(double rho) {
582 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Density: " << rho );
585 fdmex->GetAtmosphere()->SetExDensity(rho);
586 if(fdmex->GetAtmosphere()->External() == true)
591 void FGJSBsim::set_Velocities_Local_Airmass (double wnorth,
594 FG_LOG(FG_FLIGHT,FG_INFO, "FGJSBsim::set_Velocities_Local_Airmass: "
595 << wnorth << ", " << weast << ", " << wdown );
598 fdmex->GetAtmosphere()->SetWindNED(wnorth, weast, wdown );
599 if(fdmex->GetAtmosphere()->External() == true)