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 fgic=new FGInitialCondition(fdmex);
84 SGPath aircraft_path( globals->get_fg_root() );
85 aircraft_path.append( "Aircraft" );
87 SGPath engine_path( globals->get_fg_root() );
88 engine_path.append( "Engine" );
92 result = fdmex->LoadModel( aircraft_path.str(),
94 fgGetString("/sim/aircraft") );
96 int Neng = Propulsion->GetNumEngines();
97 SG_LOG(SG_FLIGHT,SG_INFO, "Neng: " << Neng );
99 for(int i=0;i<Neng;i++) {
100 add_engine( FGEngInterface() );
103 fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
104 fgSetDouble("/fdm/trim/throttle", FCS->GetThrottleCmd(0));
105 fgSetDouble("/fdm/trim/aileron", FCS->GetDaCmd());
106 fgSetDouble("/fdm/trim/rudder", FCS->GetDrCmd());
108 startup_trim = fgGetNode("/sim/startup/trim", true);
110 trimmed = fgGetNode("/fdm/trim/trimmed", true);
111 trimmed->setBoolValue(false);
113 pitch_trim = fgGetNode("/fdm/trim/pitch-trim", true );
114 throttle_trim = fgGetNode("/fdm/trim/throttle", true );
115 aileron_trim = fgGetNode("/fdm/trim/aileron", true );
116 rudder_trim = fgGetNode("/fdm/trim/rudder", true );
119 /******************************************************************************/
120 FGJSBsim::~FGJSBsim(void) {
127 /******************************************************************************/
129 // Initialize the JSBsim flight model, dt is the time increment for
130 // each subsequent iteration through the EOM
132 void FGJSBsim::init() {
133 // Explicitly call the superclass's
134 // init method first.
139 SG_LOG( SG_FLIGHT, SG_INFO, "Starting and initializing JSBsim" );
141 Atmosphere->UseInternal();
143 SG_LOG( SG_FLIGHT, SG_INFO, " Initializing JSBSim with:" );
145 switch(fgic->GetSpeedSet()) {
147 SG_LOG(SG_FLIGHT,SG_INFO, " Vn,Ve,Vd= "
148 << Position->GetVn() << ", "
149 << Position->GetVe() << ", "
150 << Position->GetVd() << " ft/s");
153 SG_LOG(SG_FLIGHT,SG_INFO, " U,V,W= "
154 << Translation->GetUVW(1) << ", "
155 << Translation->GetUVW(2) << ", "
156 << Translation->GetUVW(3) << " ft/s");
159 SG_LOG(SG_FLIGHT,SG_INFO, " Mach: "
160 << Translation->GetMach() );
164 SG_LOG(SG_FLIGHT,SG_INFO, " Indicated Airspeed: "
165 << Auxiliary->GetVcalibratedKTS() << " knots" );
169 SG_LOG( SG_FLIGHT, SG_INFO, " Bank Angle: "
170 << Rotation->Getphi()*RADTODEG << " deg");
171 SG_LOG( SG_FLIGHT, SG_INFO, " Pitch Angle: "
172 << Rotation->Gettht()*RADTODEG << " deg" );
173 SG_LOG( SG_FLIGHT, SG_INFO, " True Heading: "
174 << Rotation->Getpsi()*RADTODEG << " deg" );
175 SG_LOG( SG_FLIGHT, SG_INFO, " Latitude: "
176 << Position->GetLatitude() << " deg" );
177 SG_LOG( SG_FLIGHT, SG_INFO, " Longitude: "
178 << Position->GetLongitude() << " deg" );
180 SG_LOG( SG_FLIGHT, SG_INFO, " loaded initial conditions" );
182 SG_LOG( SG_FLIGHT, SG_INFO, " set dt" );
184 SG_LOG( SG_FLIGHT, SG_INFO, "Finished initializing JSBSim" );
187 /******************************************************************************/
189 // Run an iteration of the EOM (equations of motion)
191 bool FGJSBsim::update( int multiloop ) {
195 double save_alt = 0.0;
199 trimmed->setBoolValue(false);
201 if ( needTrim && startup_trim->getBoolValue() ) {
203 //fgic->SetSeaLevelRadiusFtIC( get_Sea_level_radius() );
204 //fgic->SetTerrainAltitudeFtIC( scenery.cur_elev * SG_METER_TO_FEET );
207 if(fgic->GetVcalibratedKtsIC() < 10 ) {
208 fgic->SetVcalibratedKtsIC(0.0);
209 fgtrim=new FGTrim(fdmex,fgic,tGround);
211 fgtrim=new FGTrim(fdmex,fgic,tLongitudinal);
213 if(!fgtrim->DoTrim()) {
217 trimmed->setBoolValue(true);
219 fgtrim->ReportState();
224 pitch_trim->setDoubleValue( FCS->GetPitchTrimCmd() );
225 throttle_trim->setDoubleValue( FCS->GetThrottleCmd(0) );
226 aileron_trim->setDoubleValue( FCS->GetDaCmd() );
227 rudder_trim->setDoubleValue( FCS->GetDrCmd() );
229 controls.set_elevator_trim(FCS->GetPitchTrimCmd());
230 controls.set_elevator(FCS->GetDeCmd());
231 controls.set_throttle(FGControls::ALL_ENGINES,
232 FCS->GetThrottleCmd(0));
234 controls.set_aileron(FCS->GetDaCmd());
235 controls.set_rudder( FCS->GetDrCmd());
237 SG_LOG( SG_FLIGHT, SG_INFO, " Trim complete" );
240 for( i=0; i<get_num_engines(); i++ ) {
241 get_engine(i)->set_RPM( Propulsion->GetThruster(i)->GetRPM() );
242 get_engine(i)->set_Throttle( controls.get_throttle(i) );
245 for ( i=0; i < multiloop; i++ ) {
249 // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
250 // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
252 // translate JSBsim back to FG structure so that the
253 // autopilot (and the rest of the sim can use the updated values
259 /******************************************************************************/
261 // Convert from the FGInterface struct to the JSBsim generic_ struct
263 bool FGJSBsim::copy_to_JSBsim() {
264 // copy control positions into the JSBsim structure
266 FCS->SetDaCmd( controls.get_aileron());
267 FCS->SetDeCmd( controls.get_elevator());
268 FCS->SetPitchTrimCmd(controls.get_elevator_trim());
269 FCS->SetDrCmd( -controls.get_rudder());
270 FCS->SetDfCmd( controls.get_flaps() );
271 FCS->SetDsbCmd( 0.0 ); //speedbrakes
272 FCS->SetDspCmd( 0.0 ); //spoilers
273 FCS->SetThrottleCmd( FGControls::ALL_ENGINES,
274 controls.get_throttle( 0 ));
275 FCS->SetLBrake( controls.get_brake( 0 ) );
276 FCS->SetRBrake( controls.get_brake( 1 ) );
277 FCS->SetCBrake( controls.get_brake( 2 ) );
279 Position->SetSeaLevelRadius( get_Sea_level_radius() );
280 Position->SetRunwayRadius( scenery.cur_elev*SG_METER_TO_FEET
281 + get_Sea_level_radius() );
283 Atmosphere->SetExTemperature(get_Static_temperature());
284 Atmosphere->SetExPressure(get_Static_pressure());
285 Atmosphere->SetExDensity(get_Density());
286 Atmosphere->SetWindNED(get_V_north_airmass(),
287 get_V_east_airmass(),
288 get_V_down_airmass());
289 // SG_LOG(SG_FLIGHT,SG_INFO, "Wind NED: "
290 // << get_V_north_airmass() << ", "
291 // << get_V_east_airmass() << ", "
292 // << get_V_down_airmass() );
297 /******************************************************************************/
299 // Convert from the JSBsim generic_ struct to the FGInterface struct
301 bool FGJSBsim::copy_from_JSBsim() {
304 _set_Inertias( MassBalance->GetMass(),
305 MassBalance->GetIxx(),
306 MassBalance->GetIyy(),
307 MassBalance->GetIzz(),
308 MassBalance->GetIxz() );
310 _set_CG_Position( MassBalance->GetXYZcg(1),
311 MassBalance->GetXYZcg(2),
312 MassBalance->GetXYZcg(3) );
314 _set_Accels_Body( Translation->GetUVWdot(1),
315 Translation->GetUVWdot(2),
316 Translation->GetUVWdot(3) );
318 _set_Accels_CG_Body( Translation->GetUVWdot(1),
319 Translation->GetUVWdot(2),
320 Translation->GetUVWdot(3) );
322 //_set_Accels_CG_Body_N ( Translation->GetNcg(1),
323 // Translation->GetNcg(2),
324 // Translation->GetNcg(3) );
326 _set_Accels_Pilot_Body( Auxiliary->GetPilotAccel(1),
327 Auxiliary->GetPilotAccel(2),
328 Auxiliary->GetPilotAccel(3) );
330 //_set_Accels_Pilot_Body_N( Auxiliary->GetNpilot(1),
331 // Auxiliary->GetNpilot(2),
332 // Auxiliary->GetNpilot(3) );
334 _set_Nlf( Aerodynamics->GetNlf() );
338 _set_Velocities_Local( Position->GetVn(),
342 _set_Velocities_Wind_Body( Translation->GetUVW(1),
343 Translation->GetUVW(2),
344 Translation->GetUVW(3) );
346 _set_V_rel_wind( Translation->GetVt() );
348 _set_V_equiv_kts( Auxiliary->GetVequivalentKTS() );
350 // _set_V_calibrated( Auxiliary->GetVcalibratedFPS() );
352 _set_V_calibrated_kts( Auxiliary->GetVcalibratedKTS() );
354 _set_V_ground_speed( Position->GetVground() );
356 _set_Omega_Body( Rotation->GetPQR(1),
358 Rotation->GetPQR(3) );
360 _set_Euler_Rates( Rotation->GetEulerRates(1),
361 Rotation->GetEulerRates(2),
362 Rotation->GetEulerRates(3) );
364 _set_Geocentric_Rates(Position->GetLatitudeDot(),
365 Position->GetLongitudeDot(),
366 Position->Gethdot() );
368 _set_Mach_number( Translation->GetMach() );
371 _updatePosition( Position->GetLatitude(),
372 Position->GetLongitude(),
375 _set_Euler_Angles( Rotation->Getphi(),
377 Rotation->Getpsi() );
379 _set_Alpha( Translation->Getalpha() );
380 _set_Beta( Translation->Getbeta() );
383 _set_Gamma_vert_rad( Position->GetGamma() );
384 // set_Gamma_horiz_rad( Gamma_horiz_rad );
386 _set_Earth_position_angle( Auxiliary->GetEarthPositionAngle() );
388 _set_Climb_Rate( Position->Gethdot() );
391 for ( i = 1; i <= 3; i++ ) {
392 for ( j = 1; j <= 3; j++ ) {
393 _set_T_Local_to_Body( i, j, State->GetTl2b(i,j) );
399 void FGJSBsim::snap_shot(void) {
400 fgic->SetLatitudeRadIC(get_Lat_geocentric() );
401 fgic->SetLongitudeRadIC( get_Longitude() );
402 fgic->SetAltitudeFtIC( get_Altitude() );
403 fgic->SetTerrainAltitudeFtIC( get_Runway_altitude() );
404 fgic->SetVtrueFpsIC( get_V_rel_wind() );
405 fgic->SetPitchAngleRadIC( get_Theta() );
406 fgic->SetRollAngleRadIC( get_Phi() );
407 fgic->SetTrueHeadingRadIC( get_Psi() );
408 fgic->SetClimbRateFpsIC( get_Climb_Rate() );
412 bool FGJSBsim::ToggleDataLogging(void) {
413 return fdmex->GetOutput()->Toggle();
417 bool FGJSBsim::ToggleDataLogging(bool state) {
419 fdmex->GetOutput()->Enable();
422 fdmex->GetOutput()->Disable();
429 void FGJSBsim::set_Latitude(double lat) {
430 double sea_level_radius_meters,lat_geoc;
432 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat );
435 sgGeodToGeoc( lat, get_Altitude() , &sea_level_radius_meters, &lat_geoc);
436 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
437 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
438 fgic->SetLatitudeRadIC( lat_geoc );
439 fdmex->RunIC(fgic); //loop JSBSim once
440 copy_from_JSBsim(); //update the bus
444 void FGJSBsim::set_Longitude(double lon) {
446 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon );
449 fgic->SetLongitudeRadIC(lon);
450 fdmex->RunIC(fgic); //loop JSBSim once
451 copy_from_JSBsim(); //update the bus
455 void FGJSBsim::set_Altitude(double alt) {
456 double sea_level_radius_meters,lat_geoc;
458 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt );
461 sgGeodToGeoc( get_Latitude(), alt , &sea_level_radius_meters, &lat_geoc);
462 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
463 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
464 fgic->SetLatitudeRadIC( lat_geoc );
465 fgic->SetAltitudeFtIC(alt);
466 fdmex->RunIC(fgic); //loop JSBSim once
467 copy_from_JSBsim(); //update the bus
471 void FGJSBsim::set_V_calibrated_kts(double vc) {
472 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " << vc );
475 fgic->SetVcalibratedKtsIC(vc);
476 fdmex->RunIC(fgic); //loop JSBSim once
477 copy_from_JSBsim(); //update the bus
481 void FGJSBsim::set_Mach_number(double mach) {
482 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " << mach );
485 fgic->SetMachIC(mach);
486 fdmex->RunIC(fgic); //loop JSBSim once
487 copy_from_JSBsim(); //update the bus
491 void FGJSBsim::set_Velocities_Local( double north, double east, double down ){
492 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: "
493 << north << ", " << east << ", " << down );
496 fgic->SetVnorthFpsIC(north);
497 fgic->SetVeastFpsIC(east);
498 fgic->SetVdownFpsIC(down);
499 fdmex->RunIC(fgic); //loop JSBSim once
500 copy_from_JSBsim(); //update the bus
504 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w){
505 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
506 << u << ", " << v << ", " << w );
509 fgic->SetUBodyFpsIC(u);
510 fgic->SetVBodyFpsIC(v);
511 fgic->SetWBodyFpsIC(w);
512 fdmex->RunIC(fgic); //loop JSBSim once
513 copy_from_JSBsim(); //update the bus
518 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi ) {
519 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: "
520 << phi << ", " << theta << ", " << psi );
523 fgic->SetPitchAngleRadIC(theta);
524 fgic->SetRollAngleRadIC(phi);
525 fgic->SetTrueHeadingRadIC(psi);
526 fdmex->RunIC(fgic); //loop JSBSim once
527 copy_from_JSBsim(); //update the bus
532 void FGJSBsim::set_Climb_Rate( double roc) {
533 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
536 fgic->SetClimbRateFpsIC(roc);
537 fdmex->RunIC(fgic); //loop JSBSim once
538 copy_from_JSBsim(); //update the bus
542 void FGJSBsim::set_Gamma_vert_rad( double gamma) {
543 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
546 fgic->SetFlightPathAngleRadIC(gamma);
547 fdmex->RunIC(fgic); //loop JSBSim once
548 copy_from_JSBsim(); //update the bus
553 void FGJSBsim::set_Sea_level_radius(double slr) {
554 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Sea_level_radius: " << slr );
557 fgic->SetSeaLevelRadiusFtIC(slr);
558 fdmex->RunIC(fgic); //loop JSBSim once
559 copy_from_JSBsim(); //update the bus
563 void FGJSBsim::set_Runway_altitude(double ralt) {
564 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Runway_altitude: " << ralt );
567 _set_Runway_altitude( ralt );
568 fgic->SetTerrainAltitudeFtIC( ralt );
569 fdmex->RunIC(fgic); //loop JSBSim once
570 copy_from_JSBsim(); //update the bus
574 void FGJSBsim::set_Static_pressure(double p) {
575 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_pressure: " << p );
578 Atmosphere->SetExPressure(p);
579 if(Atmosphere->External() == true)
583 void FGJSBsim::set_Static_temperature(double T) {
584 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_temperature: " << T );
587 Atmosphere->SetExTemperature(T);
588 if(Atmosphere->External() == true)
593 void FGJSBsim::set_Density(double rho) {
594 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Density: " << rho );
597 Atmosphere->SetExDensity(rho);
598 if(Atmosphere->External() == true)
603 void FGJSBsim::set_Velocities_Local_Airmass (double wnorth,
606 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local_Airmass: "
607 << wnorth << ", " << weast << ", " << wdown );
609 _set_Velocities_Local_Airmass( wnorth, weast, wdown );
611 Atmosphere->SetWindNED(wnorth, weast, wdown );
612 if(Atmosphere->External() == true)