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") );
100 SG_LOG( SG_FLIGHT, SG_INFO, " loaded aircraft.");
102 SG_LOG( SG_FLIGHT, SG_INFO,
103 " aircraft does not exist (you may have mis-typed the name).");
107 int Neng = Propulsion->GetNumEngines();
108 SG_LOG(SG_FLIGHT,SG_INFO, "Neng: " << Neng );
110 for(int i=0;i<Neng;i++) {
111 add_engine( FGEngInterface() );
114 fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
115 fgSetDouble("/fdm/trim/throttle", FCS->GetThrottleCmd(0));
116 fgSetDouble("/fdm/trim/aileron", FCS->GetDaCmd());
117 fgSetDouble("/fdm/trim/rudder", FCS->GetDrCmd());
119 startup_trim = fgGetNode("/sim/startup/trim", true);
121 trimmed = fgGetNode("/fdm/trim/trimmed", true);
122 trimmed->setBoolValue(false);
124 pitch_trim = fgGetNode("/fdm/trim/pitch-trim", true );
125 throttle_trim = fgGetNode("/fdm/trim/throttle", true );
126 aileron_trim = fgGetNode("/fdm/trim/aileron", true );
127 rudder_trim = fgGetNode("/fdm/trim/rudder", true );
130 /******************************************************************************/
131 FGJSBsim::~FGJSBsim(void) {
133 delete fdmex; fdmex=NULL;
134 delete fgic; fgic=NULL;
138 /******************************************************************************/
140 // Initialize the JSBsim flight model, dt is the time increment for
141 // each subsequent iteration through the EOM
143 void FGJSBsim::init() {
145 SG_LOG( SG_FLIGHT, SG_INFO, "Starting and initializing JSBsim" );
147 // Explicitly call the superclass's
148 // init method first.
151 SG_LOG( SG_FLIGHT, SG_INFO, " Initializing JSBSim with:" );
153 switch(fgic->GetSpeedSet()) {
155 SG_LOG(SG_FLIGHT,SG_INFO, " Vn,Ve,Vd= "
156 << Position->GetVn() << ", "
157 << Position->GetVe() << ", "
158 << Position->GetVd() << " ft/s");
161 SG_LOG(SG_FLIGHT,SG_INFO, " U,V,W= "
162 << Translation->GetUVW(1) << ", "
163 << Translation->GetUVW(2) << ", "
164 << Translation->GetUVW(3) << " ft/s");
167 SG_LOG(SG_FLIGHT,SG_INFO, " Mach: "
168 << Translation->GetMach() );
172 SG_LOG(SG_FLIGHT,SG_INFO, " Indicated Airspeed: "
173 << Auxiliary->GetVcalibratedKTS() << " knots" );
177 SG_LOG( SG_FLIGHT, SG_INFO, " Bank Angle: "
178 << Rotation->Getphi()*RADTODEG << " deg");
179 SG_LOG( SG_FLIGHT, SG_INFO, " Pitch Angle: "
180 << Rotation->Gettht()*RADTODEG << " deg" );
181 SG_LOG( SG_FLIGHT, SG_INFO, " True Heading: "
182 << Rotation->Getpsi()*RADTODEG << " deg" );
183 SG_LOG( SG_FLIGHT, SG_INFO, " Latitude: "
184 << Position->GetLatitude() << " deg" );
185 SG_LOG( SG_FLIGHT, SG_INFO, " Longitude: "
186 << Position->GetLongitude() << " deg" );
188 SG_LOG( SG_FLIGHT, SG_INFO, " loaded initial conditions" );
190 SG_LOG( SG_FLIGHT, SG_INFO, " set dt" );
192 SG_LOG( SG_FLIGHT, SG_INFO, "Finished initializing JSBSim" );
195 /******************************************************************************/
197 // Run an iteration of the EOM (equations of motion)
199 bool FGJSBsim::update( int multiloop ) {
203 double save_alt = 0.0;
207 trimmed->setBoolValue(false);
209 if ( needTrim && startup_trim->getBoolValue() ) {
211 //fgic->SetSeaLevelRadiusFtIC( get_Sea_level_radius() );
212 //fgic->SetTerrainAltitudeFtIC( scenery.cur_elev * SG_METER_TO_FEET );
215 if(fgic->GetVcalibratedKtsIC() < 10 ) {
216 fgic->SetVcalibratedKtsIC(0.0);
217 fgtrim=new FGTrim(fdmex,fgic,tGround);
219 fgtrim=new FGTrim(fdmex,fgic,tLongitudinal);
221 if(!fgtrim->DoTrim()) {
225 trimmed->setBoolValue(true);
227 fgtrim->ReportState();
232 pitch_trim->setDoubleValue( FCS->GetPitchTrimCmd() );
233 throttle_trim->setDoubleValue( FCS->GetThrottleCmd(0) );
234 aileron_trim->setDoubleValue( FCS->GetDaCmd() );
235 rudder_trim->setDoubleValue( FCS->GetDrCmd() );
237 globals->get_controls()->set_elevator_trim(FCS->GetPitchTrimCmd());
238 globals->get_controls()->set_elevator(FCS->GetDeCmd());
239 globals->get_controls()->set_throttle(FGControls::ALL_ENGINES,
240 FCS->GetThrottleCmd(0));
242 globals->get_controls()->set_aileron(FCS->GetDaCmd());
243 globals->get_controls()->set_rudder( FCS->GetDrCmd());
245 SG_LOG( SG_FLIGHT, SG_INFO, " Trim complete" );
248 for( i=0; i<get_num_engines(); i++ ) {
249 get_engine(i)->set_RPM( Propulsion->GetThruster(i)->GetRPM() );
250 get_engine(i)->set_Throttle( globals->get_controls()->get_throttle(i) );
253 for ( i=0; i < multiloop; i++ ) {
257 // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
258 // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
260 // translate JSBsim back to FG structure so that the
261 // autopilot (and the rest of the sim can use the updated values
267 /******************************************************************************/
269 // Convert from the FGInterface struct to the JSBsim generic_ struct
271 bool FGJSBsim::copy_to_JSBsim() {
272 // copy control positions into the JSBsim structure
274 FCS->SetDaCmd( globals->get_controls()->get_aileron());
275 FCS->SetDeCmd( globals->get_controls()->get_elevator());
276 FCS->SetPitchTrimCmd(globals->get_controls()->get_elevator_trim());
277 FCS->SetDrCmd( -globals->get_controls()->get_rudder());
278 FCS->SetDfCmd( globals->get_controls()->get_flaps() );
279 FCS->SetDsbCmd( 0.0 ); //speedbrakes
280 FCS->SetDspCmd( 0.0 ); //spoilers
281 FCS->SetLBrake( globals->get_controls()->get_brake( 0 ) );
282 FCS->SetRBrake( globals->get_controls()->get_brake( 1 ) );
283 FCS->SetCBrake( globals->get_controls()->get_brake( 2 ) );
284 for (int i = 0; i < get_num_engines(); i++) {
285 FCS->SetThrottleCmd(i, globals->get_controls()->get_throttle(i));
288 Position->SetSeaLevelRadius( get_Sea_level_radius() );
289 Position->SetRunwayRadius( scenery.cur_elev*SG_METER_TO_FEET
290 + get_Sea_level_radius() );
292 Atmosphere->SetExTemperature(get_Static_temperature());
293 Atmosphere->SetExPressure(get_Static_pressure());
294 Atmosphere->SetExDensity(get_Density());
295 Atmosphere->SetWindNED(get_V_north_airmass(),
296 get_V_east_airmass(),
297 get_V_down_airmass());
298 // SG_LOG(SG_FLIGHT,SG_INFO, "Wind NED: "
299 // << get_V_north_airmass() << ", "
300 // << get_V_east_airmass() << ", "
301 // << get_V_down_airmass() );
306 /******************************************************************************/
308 // Convert from the JSBsim generic_ struct to the FGInterface struct
310 bool FGJSBsim::copy_from_JSBsim() {
313 _set_Inertias( MassBalance->GetMass(),
314 MassBalance->GetIxx(),
315 MassBalance->GetIyy(),
316 MassBalance->GetIzz(),
317 MassBalance->GetIxz() );
319 _set_CG_Position( MassBalance->GetXYZcg(1),
320 MassBalance->GetXYZcg(2),
321 MassBalance->GetXYZcg(3) );
323 _set_Accels_Body( Translation->GetUVWdot(1),
324 Translation->GetUVWdot(2),
325 Translation->GetUVWdot(3) );
327 _set_Accels_CG_Body( Translation->GetUVWdot(1),
328 Translation->GetUVWdot(2),
329 Translation->GetUVWdot(3) );
331 //_set_Accels_CG_Body_N ( Translation->GetNcg(1),
332 // Translation->GetNcg(2),
333 // Translation->GetNcg(3) );
335 _set_Accels_Pilot_Body( Auxiliary->GetPilotAccel(1),
336 Auxiliary->GetPilotAccel(2),
337 Auxiliary->GetPilotAccel(3) );
339 //_set_Accels_Pilot_Body_N( Auxiliary->GetNpilot(1),
340 // Auxiliary->GetNpilot(2),
341 // Auxiliary->GetNpilot(3) );
343 _set_Nlf( Aerodynamics->GetNlf() );
347 _set_Velocities_Local( Position->GetVn(),
351 _set_Velocities_Wind_Body( Translation->GetUVW(1),
352 Translation->GetUVW(2),
353 Translation->GetUVW(3) );
355 _set_V_rel_wind( Translation->GetVt() );
357 _set_V_equiv_kts( Auxiliary->GetVequivalentKTS() );
359 // _set_V_calibrated( Auxiliary->GetVcalibratedFPS() );
361 _set_V_calibrated_kts( Auxiliary->GetVcalibratedKTS() );
363 _set_V_ground_speed( Position->GetVground() );
365 _set_Omega_Body( Rotation->GetPQR(1),
367 Rotation->GetPQR(3) );
369 _set_Euler_Rates( Rotation->GetEulerRates(1),
370 Rotation->GetEulerRates(2),
371 Rotation->GetEulerRates(3) );
373 _set_Geocentric_Rates(Position->GetLatitudeDot(),
374 Position->GetLongitudeDot(),
375 Position->Gethdot() );
377 _set_Mach_number( Translation->GetMach() );
380 _updatePosition( Position->GetLatitude(),
381 Position->GetLongitude(),
384 _set_Altitude_AGL( Position->GetDistanceAGL() );
386 _set_Euler_Angles( Rotation->Getphi(),
388 Rotation->Getpsi() );
390 _set_Alpha( Translation->Getalpha() );
391 _set_Beta( Translation->Getbeta() );
394 _set_Gamma_vert_rad( Position->GetGamma() );
395 // set_Gamma_horiz_rad( Gamma_horiz_rad );
397 _set_Earth_position_angle( Auxiliary->GetEarthPositionAngle() );
399 _set_Climb_Rate( Position->Gethdot() );
402 for ( i = 1; i <= 3; i++ ) {
403 for ( j = 1; j <= 3; j++ ) {
404 _set_T_Local_to_Body( i, j, State->GetTl2b(i,j) );
410 void FGJSBsim::snap_shot(void) {
411 fgic->SetLatitudeRadIC(get_Lat_geocentric() );
412 fgic->SetLongitudeRadIC( get_Longitude() );
413 fgic->SetAltitudeFtIC( get_Altitude() );
414 fgic->SetTerrainAltitudeFtIC( get_Runway_altitude() );
415 fgic->SetVtrueFpsIC( get_V_rel_wind() );
416 fgic->SetPitchAngleRadIC( get_Theta() );
417 fgic->SetRollAngleRadIC( get_Phi() );
418 fgic->SetTrueHeadingRadIC( get_Psi() );
419 fgic->SetClimbRateFpsIC( get_Climb_Rate() );
423 bool FGJSBsim::ToggleDataLogging(void) {
424 return fdmex->GetOutput()->Toggle();
428 bool FGJSBsim::ToggleDataLogging(bool state) {
430 fdmex->GetOutput()->Enable();
433 fdmex->GetOutput()->Disable();
440 void FGJSBsim::set_Latitude(double lat) {
441 double sea_level_radius_meters,lat_geoc;
443 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat );
446 sgGeodToGeoc( lat, get_Altitude() , &sea_level_radius_meters, &lat_geoc);
447 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
448 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
449 fgic->SetLatitudeRadIC( lat_geoc );
450 fdmex->RunIC(fgic); //loop JSBSim once
451 copy_from_JSBsim(); //update the bus
455 void FGJSBsim::set_Longitude(double lon) {
457 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon );
460 fgic->SetLongitudeRadIC(lon);
461 fdmex->RunIC(fgic); //loop JSBSim once
462 copy_from_JSBsim(); //update the bus
466 void FGJSBsim::set_Altitude(double alt) {
467 double sea_level_radius_meters,lat_geoc;
469 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt );
472 sgGeodToGeoc( get_Latitude(), alt , &sea_level_radius_meters, &lat_geoc);
473 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
474 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
475 fgic->SetLatitudeRadIC( lat_geoc );
476 fgic->SetAltitudeFtIC(alt);
477 fdmex->RunIC(fgic); //loop JSBSim once
478 copy_from_JSBsim(); //update the bus
482 void FGJSBsim::set_V_calibrated_kts(double vc) {
483 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " << vc );
486 fgic->SetVcalibratedKtsIC(vc);
487 fdmex->RunIC(fgic); //loop JSBSim once
488 copy_from_JSBsim(); //update the bus
492 void FGJSBsim::set_Mach_number(double mach) {
493 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " << mach );
496 fgic->SetMachIC(mach);
497 fdmex->RunIC(fgic); //loop JSBSim once
498 copy_from_JSBsim(); //update the bus
502 void FGJSBsim::set_Velocities_Local( double north, double east, double down ){
503 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: "
504 << north << ", " << east << ", " << down );
507 fgic->SetVnorthFpsIC(north);
508 fgic->SetVeastFpsIC(east);
509 fgic->SetVdownFpsIC(down);
510 fdmex->RunIC(fgic); //loop JSBSim once
511 copy_from_JSBsim(); //update the bus
515 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w){
516 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
517 << u << ", " << v << ", " << w );
520 fgic->SetUBodyFpsIC(u);
521 fgic->SetVBodyFpsIC(v);
522 fgic->SetWBodyFpsIC(w);
523 fdmex->RunIC(fgic); //loop JSBSim once
524 copy_from_JSBsim(); //update the bus
529 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi ) {
530 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: "
531 << phi << ", " << theta << ", " << psi );
534 fgic->SetPitchAngleRadIC(theta);
535 fgic->SetRollAngleRadIC(phi);
536 fgic->SetTrueHeadingRadIC(psi);
537 fdmex->RunIC(fgic); //loop JSBSim once
538 copy_from_JSBsim(); //update the bus
543 void FGJSBsim::set_Climb_Rate( double roc) {
544 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
547 fgic->SetClimbRateFpsIC(roc);
548 fdmex->RunIC(fgic); //loop JSBSim once
549 copy_from_JSBsim(); //update the bus
553 void FGJSBsim::set_Gamma_vert_rad( double gamma) {
554 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
557 fgic->SetFlightPathAngleRadIC(gamma);
558 fdmex->RunIC(fgic); //loop JSBSim once
559 copy_from_JSBsim(); //update the bus
564 void FGJSBsim::set_Sea_level_radius(double slr) {
565 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Sea_level_radius: " << slr );
568 fgic->SetSeaLevelRadiusFtIC(slr);
569 fdmex->RunIC(fgic); //loop JSBSim once
570 copy_from_JSBsim(); //update the bus
574 void FGJSBsim::set_Runway_altitude(double ralt) {
575 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Runway_altitude: " << ralt );
578 _set_Runway_altitude( ralt );
579 fgic->SetTerrainAltitudeFtIC( ralt );
580 fdmex->RunIC(fgic); //loop JSBSim once
581 copy_from_JSBsim(); //update the bus
585 void FGJSBsim::set_Static_pressure(double p) {
586 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_pressure: " << p );
589 Atmosphere->SetExPressure(p);
590 if(Atmosphere->External() == true)
594 void FGJSBsim::set_Static_temperature(double T) {
595 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_temperature: " << T );
598 Atmosphere->SetExTemperature(T);
599 if(Atmosphere->External() == true)
604 void FGJSBsim::set_Density(double rho) {
605 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Density: " << rho );
608 Atmosphere->SetExDensity(rho);
609 if(Atmosphere->External() == true)
614 void FGJSBsim::set_Velocities_Local_Airmass (double wnorth,
617 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local_Airmass: "
618 << wnorth << ", " << weast << ", " << wdown );
620 _set_Velocities_Local_Airmass( wnorth, weast, wdown );
622 Atmosphere->SetWindNED(wnorth, weast, wdown );
623 if(Atmosphere->External() == true)