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,
101 " loaded aircraft.");
103 SG_LOG( SG_FLIGHT, SG_INFO,
104 " aircraft does not exist (you may have mis-typed the name).");
108 int Neng = Propulsion->GetNumEngines();
109 SG_LOG(SG_FLIGHT,SG_INFO, "Neng: " << Neng );
111 for(int i=0;i<Neng;i++) {
112 add_engine( FGEngInterface() );
115 fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
116 fgSetDouble("/fdm/trim/throttle", FCS->GetThrottleCmd(0));
117 fgSetDouble("/fdm/trim/aileron", FCS->GetDaCmd());
118 fgSetDouble("/fdm/trim/rudder", FCS->GetDrCmd());
120 startup_trim = fgGetNode("/sim/startup/trim", true);
122 trimmed = fgGetNode("/fdm/trim/trimmed", true);
123 trimmed->setBoolValue(false);
125 pitch_trim = fgGetNode("/fdm/trim/pitch-trim", true );
126 throttle_trim = fgGetNode("/fdm/trim/throttle", true );
127 aileron_trim = fgGetNode("/fdm/trim/aileron", true );
128 rudder_trim = fgGetNode("/fdm/trim/rudder", true );
131 /******************************************************************************/
132 FGJSBsim::~FGJSBsim(void) {
134 delete fdmex; fdmex=NULL;
135 delete fgic; fgic=NULL;
139 /******************************************************************************/
141 // Initialize the JSBsim flight model, dt is the time increment for
142 // each subsequent iteration through the EOM
144 void FGJSBsim::init() {
146 SG_LOG( SG_FLIGHT, SG_INFO, "Starting and initializing JSBsim" );
148 // Explicitly call the superclass's
149 // init method first.
152 SG_LOG( SG_FLIGHT, SG_INFO, " Initializing JSBSim with:" );
154 switch(fgic->GetSpeedSet()) {
156 SG_LOG(SG_FLIGHT,SG_INFO, " Vn,Ve,Vd= "
157 << Position->GetVn() << ", "
158 << Position->GetVe() << ", "
159 << Position->GetVd() << " ft/s");
162 SG_LOG(SG_FLIGHT,SG_INFO, " U,V,W= "
163 << Translation->GetUVW(1) << ", "
164 << Translation->GetUVW(2) << ", "
165 << Translation->GetUVW(3) << " ft/s");
168 SG_LOG(SG_FLIGHT,SG_INFO, " Mach: "
169 << Translation->GetMach() );
173 SG_LOG(SG_FLIGHT,SG_INFO, " Indicated Airspeed: "
174 << Auxiliary->GetVcalibratedKTS() << " knots" );
178 SG_LOG( SG_FLIGHT, SG_INFO, " Bank Angle: "
179 << Rotation->Getphi()*RADTODEG << " deg");
180 SG_LOG( SG_FLIGHT, SG_INFO, " Pitch Angle: "
181 << Rotation->Gettht()*RADTODEG << " deg" );
182 SG_LOG( SG_FLIGHT, SG_INFO, " True Heading: "
183 << Rotation->Getpsi()*RADTODEG << " deg" );
184 SG_LOG( SG_FLIGHT, SG_INFO, " Latitude: "
185 << Position->GetLatitude() << " deg" );
186 SG_LOG( SG_FLIGHT, SG_INFO, " Longitude: "
187 << Position->GetLongitude() << " deg" );
189 SG_LOG( SG_FLIGHT, SG_INFO, " loaded initial conditions" );
191 SG_LOG( SG_FLIGHT, SG_INFO, " set dt" );
193 SG_LOG( SG_FLIGHT, SG_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;
208 trimmed->setBoolValue(false);
210 if ( needTrim && startup_trim->getBoolValue() ) {
212 //fgic->SetSeaLevelRadiusFtIC( get_Sea_level_radius() );
213 //fgic->SetTerrainAltitudeFtIC( scenery.cur_elev * SG_METER_TO_FEET );
216 if(fgic->GetVcalibratedKtsIC() < 10 ) {
217 fgic->SetVcalibratedKtsIC(0.0);
218 fgtrim=new FGTrim(fdmex,fgic,tGround);
220 fgtrim=new FGTrim(fdmex,fgic,tLongitudinal);
222 if(!fgtrim->DoTrim()) {
226 trimmed->setBoolValue(true);
228 fgtrim->ReportState();
233 pitch_trim->setDoubleValue( FCS->GetPitchTrimCmd() );
234 throttle_trim->setDoubleValue( FCS->GetThrottleCmd(0) );
235 aileron_trim->setDoubleValue( FCS->GetDaCmd() );
236 rudder_trim->setDoubleValue( FCS->GetDrCmd() );
238 globals->get_controls()->set_elevator_trim(FCS->GetPitchTrimCmd());
239 globals->get_controls()->set_elevator(FCS->GetDeCmd());
240 globals->get_controls()->set_throttle(FGControls::ALL_ENGINES,
241 FCS->GetThrottleCmd(0));
243 globals->get_controls()->set_aileron(FCS->GetDaCmd());
244 globals->get_controls()->set_rudder( FCS->GetDrCmd());
246 SG_LOG( SG_FLIGHT, SG_INFO, " Trim complete" );
249 for( i=0; i<get_num_engines(); i++ ) {
250 get_engine(i)->set_RPM( Propulsion->GetThruster(i)->GetRPM() );
251 get_engine(i)->set_Throttle( globals->get_controls()->get_throttle(i) );
254 for ( i=0; i < multiloop; i++ ) {
258 // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
259 // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
261 // translate JSBsim back to FG structure so that the
262 // autopilot (and the rest of the sim can use the updated values
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 FCS->SetDaCmd( globals->get_controls()->get_aileron());
276 FCS->SetDeCmd( globals->get_controls()->get_elevator());
277 FCS->SetPitchTrimCmd(globals->get_controls()->get_elevator_trim());
278 FCS->SetDrCmd( -globals->get_controls()->get_rudder());
279 FCS->SetDfCmd( globals->get_controls()->get_flaps() );
280 FCS->SetDsbCmd( 0.0 ); //speedbrakes
281 FCS->SetDspCmd( 0.0 ); //spoilers
282 FCS->SetThrottleCmd( FGControls::ALL_ENGINES,
283 globals->get_controls()->get_throttle( 0 ));
284 FCS->SetLBrake( globals->get_controls()->get_brake( 0 ) );
285 FCS->SetRBrake( globals->get_controls()->get_brake( 1 ) );
286 FCS->SetCBrake( globals->get_controls()->get_brake( 2 ) );
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_Euler_Angles( Rotation->Getphi(),
386 Rotation->Getpsi() );
388 _set_Alpha( Translation->Getalpha() );
389 _set_Beta( Translation->Getbeta() );
392 _set_Gamma_vert_rad( Position->GetGamma() );
393 // set_Gamma_horiz_rad( Gamma_horiz_rad );
395 _set_Earth_position_angle( Auxiliary->GetEarthPositionAngle() );
397 _set_Climb_Rate( Position->Gethdot() );
400 for ( i = 1; i <= 3; i++ ) {
401 for ( j = 1; j <= 3; j++ ) {
402 _set_T_Local_to_Body( i, j, State->GetTl2b(i,j) );
408 void FGJSBsim::snap_shot(void) {
409 fgic->SetLatitudeRadIC(get_Lat_geocentric() );
410 fgic->SetLongitudeRadIC( get_Longitude() );
411 fgic->SetAltitudeFtIC( get_Altitude() );
412 fgic->SetTerrainAltitudeFtIC( get_Runway_altitude() );
413 fgic->SetVtrueFpsIC( get_V_rel_wind() );
414 fgic->SetPitchAngleRadIC( get_Theta() );
415 fgic->SetRollAngleRadIC( get_Phi() );
416 fgic->SetTrueHeadingRadIC( get_Psi() );
417 fgic->SetClimbRateFpsIC( get_Climb_Rate() );
421 bool FGJSBsim::ToggleDataLogging(void) {
422 return fdmex->GetOutput()->Toggle();
426 bool FGJSBsim::ToggleDataLogging(bool state) {
428 fdmex->GetOutput()->Enable();
431 fdmex->GetOutput()->Disable();
438 void FGJSBsim::set_Latitude(double lat) {
439 double sea_level_radius_meters,lat_geoc;
441 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat );
444 sgGeodToGeoc( lat, get_Altitude() , &sea_level_radius_meters, &lat_geoc);
445 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
446 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
447 fgic->SetLatitudeRadIC( lat_geoc );
448 fdmex->RunIC(fgic); //loop JSBSim once
449 copy_from_JSBsim(); //update the bus
453 void FGJSBsim::set_Longitude(double lon) {
455 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon );
458 fgic->SetLongitudeRadIC(lon);
459 fdmex->RunIC(fgic); //loop JSBSim once
460 copy_from_JSBsim(); //update the bus
464 void FGJSBsim::set_Altitude(double alt) {
465 double sea_level_radius_meters,lat_geoc;
467 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt );
470 sgGeodToGeoc( get_Latitude(), alt , &sea_level_radius_meters, &lat_geoc);
471 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
472 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
473 fgic->SetLatitudeRadIC( lat_geoc );
474 fgic->SetAltitudeFtIC(alt);
475 fdmex->RunIC(fgic); //loop JSBSim once
476 copy_from_JSBsim(); //update the bus
480 void FGJSBsim::set_V_calibrated_kts(double vc) {
481 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " << vc );
484 fgic->SetVcalibratedKtsIC(vc);
485 fdmex->RunIC(fgic); //loop JSBSim once
486 copy_from_JSBsim(); //update the bus
490 void FGJSBsim::set_Mach_number(double mach) {
491 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " << mach );
494 fgic->SetMachIC(mach);
495 fdmex->RunIC(fgic); //loop JSBSim once
496 copy_from_JSBsim(); //update the bus
500 void FGJSBsim::set_Velocities_Local( double north, double east, double down ){
501 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: "
502 << north << ", " << east << ", " << down );
505 fgic->SetVnorthFpsIC(north);
506 fgic->SetVeastFpsIC(east);
507 fgic->SetVdownFpsIC(down);
508 fdmex->RunIC(fgic); //loop JSBSim once
509 copy_from_JSBsim(); //update the bus
513 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w){
514 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
515 << u << ", " << v << ", " << w );
518 fgic->SetUBodyFpsIC(u);
519 fgic->SetVBodyFpsIC(v);
520 fgic->SetWBodyFpsIC(w);
521 fdmex->RunIC(fgic); //loop JSBSim once
522 copy_from_JSBsim(); //update the bus
527 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi ) {
528 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: "
529 << phi << ", " << theta << ", " << psi );
532 fgic->SetPitchAngleRadIC(theta);
533 fgic->SetRollAngleRadIC(phi);
534 fgic->SetTrueHeadingRadIC(psi);
535 fdmex->RunIC(fgic); //loop JSBSim once
536 copy_from_JSBsim(); //update the bus
541 void FGJSBsim::set_Climb_Rate( double roc) {
542 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
545 fgic->SetClimbRateFpsIC(roc);
546 fdmex->RunIC(fgic); //loop JSBSim once
547 copy_from_JSBsim(); //update the bus
551 void FGJSBsim::set_Gamma_vert_rad( double gamma) {
552 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
555 fgic->SetFlightPathAngleRadIC(gamma);
556 fdmex->RunIC(fgic); //loop JSBSim once
557 copy_from_JSBsim(); //update the bus
562 void FGJSBsim::set_Sea_level_radius(double slr) {
563 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Sea_level_radius: " << slr );
566 fgic->SetSeaLevelRadiusFtIC(slr);
567 fdmex->RunIC(fgic); //loop JSBSim once
568 copy_from_JSBsim(); //update the bus
572 void FGJSBsim::set_Runway_altitude(double ralt) {
573 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Runway_altitude: " << ralt );
576 _set_Runway_altitude( ralt );
577 fgic->SetTerrainAltitudeFtIC( ralt );
578 fdmex->RunIC(fgic); //loop JSBSim once
579 copy_from_JSBsim(); //update the bus
583 void FGJSBsim::set_Static_pressure(double p) {
584 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_pressure: " << p );
587 Atmosphere->SetExPressure(p);
588 if(Atmosphere->External() == true)
592 void FGJSBsim::set_Static_temperature(double T) {
593 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_temperature: " << T );
596 Atmosphere->SetExTemperature(T);
597 if(Atmosphere->External() == true)
602 void FGJSBsim::set_Density(double rho) {
603 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Density: " << rho );
606 Atmosphere->SetExDensity(rho);
607 if(Atmosphere->External() == true)
612 void FGJSBsim::set_Velocities_Local_Airmass (double wnorth,
615 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local_Airmass: "
616 << wnorth << ", " << weast << ", " << wdown );
618 _set_Velocities_Local_Airmass( wnorth, weast, wdown );
620 Atmosphere->SetWindNED(wnorth, weast, wdown );
621 if(Atmosphere->External() == true)