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>
59 /******************************************************************************/
61 FGJSBsim::FGJSBsim( double dt )
68 State = fdmex->GetState();
69 Atmosphere = fdmex->GetAtmosphere();
70 FCS = fdmex->GetFCS();
71 Propulsion = fdmex->GetPropulsion();
72 Aircraft = fdmex->GetAircraft();
73 Translation = fdmex->GetTranslation();
74 Rotation = fdmex->GetRotation();
75 Position = fdmex->GetPosition();
76 Auxiliary = fdmex->GetAuxiliary();
78 fgic=new FGInitialCondition(fdmex);
81 SGPath aircraft_path( globals->get_fg_root() );
82 aircraft_path.append( "Aircraft" );
84 SGPath engine_path( globals->get_fg_root() );
85 engine_path.append( "Engine" );
89 result = fdmex->LoadModel( aircraft_path.str(),
91 fgGetString("/sim/aircraft") );
93 int Neng = Propulsion->GetNumEngines();
94 SG_LOG(SG_FLIGHT,SG_INFO, "Neng: " << Neng );
96 for(int i=0;i<Neng;i++) {
97 add_engine( FGEngInterface() );
100 fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
101 fgSetDouble("/fdm/trim/throttle", FCS->GetThrottleCmd(0));
102 fgSetDouble("/fdm/trim/aileron", FCS->GetDaCmd());
103 fgSetDouble("/fdm/trim/rudder", FCS->GetDrCmd());
105 trimmed = fgGetValue("/fdm/trim/trimmed",true);
106 trimmed->setBoolValue(false);
110 /******************************************************************************/
111 FGJSBsim::~FGJSBsim(void) {
118 /******************************************************************************/
120 // Initialize the JSBsim flight model, dt is the time increment for
121 // each subsequent iteration through the EOM
123 void FGJSBsim::init() {
124 // Explicitly call the superclass's
125 // init method first.
130 SG_LOG( SG_FLIGHT, SG_INFO, "Starting and initializing JSBsim" );
132 Atmosphere->UseInternal();
134 SG_LOG( SG_FLIGHT, SG_INFO, " Initializing JSBSim with:" );
136 switch(fgic->GetSpeedSet()) {
138 SG_LOG(SG_FLIGHT,SG_INFO, " Vn,Ve,Vd= "
139 << Position->GetVn() << ", "
140 << Position->GetVe() << ", "
141 << Position->GetVd() << " ft/s");
144 SG_LOG(SG_FLIGHT,SG_INFO, " U,V,W= "
145 << Translation->GetUVW(1) << ", "
146 << Translation->GetUVW(2) << ", "
147 << Translation->GetUVW(3) << " ft/s");
150 SG_LOG(SG_FLIGHT,SG_INFO, " Mach: "
151 << Translation->GetMach() );
155 SG_LOG(SG_FLIGHT,SG_INFO, " Indicated Airspeed: "
156 << Auxiliary->GetVcalibratedKTS() << " knots" );
160 SG_LOG( SG_FLIGHT, SG_INFO, " Bank Angle: "
161 << Rotation->Getphi()*RADTODEG << " deg");
162 SG_LOG( SG_FLIGHT, SG_INFO, " Pitch Angle: "
163 << Rotation->Gettht()*RADTODEG << " deg" );
164 SG_LOG( SG_FLIGHT, SG_INFO, " True Heading: "
165 << Rotation->Getpsi()*RADTODEG << " deg" );
166 SG_LOG( SG_FLIGHT, SG_INFO, " Latitude: "
167 << Position->GetLatitude() << " deg" );
168 SG_LOG( SG_FLIGHT, SG_INFO, " Longitude: "
169 << Position->GetLongitude() << " deg" );
171 SG_LOG( SG_FLIGHT, SG_INFO, " loaded initial conditions" );
173 SG_LOG( SG_FLIGHT, SG_INFO, " set dt" );
175 SG_LOG( SG_FLIGHT, SG_INFO, "Finished initializing JSBSim" );
178 /******************************************************************************/
180 // Run an iteration of the EOM (equations of motion)
182 bool FGJSBsim::update( int multiloop ) {
186 double save_alt = 0.0;
190 trimmed->setBoolValue(false);
192 if (needTrim && fgGetBool("/sim/startup/trim")) {
194 //fgic->SetSeaLevelRadiusFtIC( get_Sea_level_radius() );
195 //fgic->SetTerrainAltitudeFtIC( scenery.cur_elev * SG_METER_TO_FEET );
198 if(fgic->GetVcalibratedKtsIC() < 10 ) {
199 fgic->SetVcalibratedKtsIC(0.0);
200 fgtrim=new FGTrim(fdmex,fgic,tGround);
202 fgtrim=new FGTrim(fdmex,fgic,tLongitudinal);
204 if(!fgtrim->DoTrim()) {
208 trimmed->setBoolValue(true);
210 fgtrim->ReportState();
215 fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
216 fgSetDouble("/fdm/trim/throttle", FCS->GetThrottleCmd(0));
217 fgSetDouble("/fdm/trim/aileron", FCS->GetDaCmd());
218 fgSetDouble("/fdm/trim/rudder", FCS->GetDrCmd());
220 controls.set_elevator_trim(FCS->GetPitchTrimCmd());
221 controls.set_elevator(FCS->GetDeCmd());
222 controls.set_throttle(FGControls::ALL_ENGINES,
223 FCS->GetThrottleCmd(0));
225 controls.set_aileron(FCS->GetDaCmd());
226 controls.set_rudder( FCS->GetDrCmd());
228 SG_LOG( SG_FLIGHT, SG_INFO, " Trim complete" );
231 for( i=0; i<get_num_engines(); i++ ) {
232 get_engine(i)->set_RPM( Propulsion->GetThruster(i)->GetRPM() );
233 get_engine(i)->set_Throttle( controls.get_throttle(i) );
236 for ( i=0; i < multiloop; i++ ) {
240 // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
241 // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
243 // translate JSBsim back to FG structure so that the
244 // autopilot (and the rest of the sim can use the updated values
250 /******************************************************************************/
252 // Convert from the FGInterface struct to the JSBsim generic_ struct
254 bool FGJSBsim::copy_to_JSBsim() {
255 // copy control positions into the JSBsim structure
257 FCS->SetDaCmd( controls.get_aileron());
258 FCS->SetDeCmd( controls.get_elevator());
259 FCS->SetPitchTrimCmd(controls.get_elevator_trim());
260 FCS->SetDrCmd( -controls.get_rudder());
261 FCS->SetDfCmd( controls.get_flaps() );
262 FCS->SetDsbCmd( 0.0 ); //speedbrakes
263 FCS->SetDspCmd( 0.0 ); //spoilers
264 FCS->SetThrottleCmd( FGControls::ALL_ENGINES,
265 controls.get_throttle( 0 ));
266 FCS->SetLBrake( controls.get_brake( 0 ) );
267 FCS->SetRBrake( controls.get_brake( 1 ) );
268 FCS->SetCBrake( controls.get_brake( 2 ) );
270 Position->SetSeaLevelRadius( get_Sea_level_radius() );
271 Position->SetRunwayRadius( scenery.cur_elev*SG_METER_TO_FEET
272 + get_Sea_level_radius() );
274 Atmosphere->SetExTemperature(get_Static_temperature());
275 Atmosphere->SetExPressure(get_Static_pressure());
276 Atmosphere->SetExDensity(get_Density());
277 Atmosphere->SetWindNED(get_V_north_airmass(),
278 get_V_east_airmass(),
279 get_V_down_airmass());
280 // SG_LOG(SG_FLIGHT,SG_INFO, "Wind NED: "
281 // << get_V_north_airmass() << ", "
282 // << get_V_east_airmass() << ", "
283 // << get_V_down_airmass() );
288 /******************************************************************************/
290 // Convert from the JSBsim generic_ struct to the FGInterface struct
292 bool FGJSBsim::copy_from_JSBsim() {
295 _set_Inertias( Aircraft->GetMass(),
299 Aircraft->GetIxz() );
301 _set_CG_Position( Aircraft->GetXYZcg(1),
302 Aircraft->GetXYZcg(2),
303 Aircraft->GetXYZcg(3) );
305 _set_Accels_Body( Translation->GetUVWdot(1),
306 Translation->GetUVWdot(2),
307 Translation->GetUVWdot(3) );
309 _set_Accels_CG_Body( Translation->GetUVWdot(1),
310 Translation->GetUVWdot(2),
311 Translation->GetUVWdot(3) );
313 //_set_Accels_CG_Body_N ( Translation->GetNcg(1),
314 // Translation->GetNcg(2),
315 // Translation->GetNcg(3) );
317 _set_Accels_Pilot_Body( Auxiliary->GetPilotAccel(1),
318 Auxiliary->GetPilotAccel(2),
319 Auxiliary->GetPilotAccel(3) );
321 //_set_Accels_Pilot_Body_N( Auxiliary->GetNpilot(1),
322 // Auxiliary->GetNpilot(2),
323 // Auxiliary->GetNpilot(3) );
325 _set_Nlf( Aircraft->GetNlf() );
329 _set_Velocities_Local( Position->GetVn(),
333 _set_Velocities_Wind_Body( Translation->GetUVW(1),
334 Translation->GetUVW(2),
335 Translation->GetUVW(3) );
337 _set_V_rel_wind( Translation->GetVt() );
339 _set_V_equiv_kts( Auxiliary->GetVequivalentKTS() );
341 // _set_V_calibrated( Auxiliary->GetVcalibratedFPS() );
343 _set_V_calibrated_kts( Auxiliary->GetVcalibratedKTS() );
345 _set_V_ground_speed( Position->GetVground() );
347 _set_Omega_Body( Rotation->GetPQR(1),
349 Rotation->GetPQR(3) );
351 _set_Euler_Rates( Rotation->GetEulerRates(1),
352 Rotation->GetEulerRates(2),
353 Rotation->GetEulerRates(3) );
355 _set_Geocentric_Rates(Position->GetLatitudeDot(),
356 Position->GetLongitudeDot(),
357 Position->Gethdot() );
359 _set_Mach_number( Translation->GetMach() );
362 _updatePosition( Position->GetLatitude(),
363 Position->GetLongitude(),
366 _set_Euler_Angles( Rotation->Getphi(),
368 Rotation->Getpsi() );
370 _set_Alpha( Translation->Getalpha() );
371 _set_Beta( Translation->Getbeta() );
374 _set_Gamma_vert_rad( Position->GetGamma() );
375 // set_Gamma_horiz_rad( Gamma_horiz_rad );
377 _set_Earth_position_angle( Auxiliary->GetEarthPositionAngle() );
379 _set_Climb_Rate( Position->Gethdot() );
382 for ( i = 1; i <= 3; i++ ) {
383 for ( j = 1; j <= 3; j++ ) {
384 _set_T_Local_to_Body( i, j, State->GetTl2b(i,j) );
390 void FGJSBsim::snap_shot(void) {
391 fgic->SetLatitudeRadIC(get_Lat_geocentric() );
392 fgic->SetLongitudeRadIC( get_Longitude() );
393 fgic->SetAltitudeFtIC( get_Altitude() );
394 fgic->SetTerrainAltitudeFtIC( get_Runway_altitude() );
395 fgic->SetVtrueFpsIC( get_V_rel_wind() );
396 fgic->SetPitchAngleRadIC( get_Theta() );
397 fgic->SetRollAngleRadIC( get_Phi() );
398 fgic->SetTrueHeadingRadIC( get_Psi() );
399 fgic->SetClimbRateFpsIC( get_Climb_Rate() );
403 bool FGJSBsim::ToggleDataLogging(void) {
404 return fdmex->GetOutput()->Toggle();
408 bool FGJSBsim::ToggleDataLogging(bool state) {
410 fdmex->GetOutput()->Enable();
413 fdmex->GetOutput()->Disable();
420 void FGJSBsim::set_Latitude(double lat) {
421 double sea_level_radius_meters,lat_geoc;
423 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat );
426 sgGeodToGeoc( lat, get_Altitude() , &sea_level_radius_meters, &lat_geoc);
427 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
428 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
429 fgic->SetLatitudeRadIC( lat_geoc );
430 fdmex->RunIC(fgic); //loop JSBSim once
431 copy_from_JSBsim(); //update the bus
435 void FGJSBsim::set_Longitude(double lon) {
437 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon );
440 fgic->SetLongitudeRadIC(lon);
441 fdmex->RunIC(fgic); //loop JSBSim once
442 copy_from_JSBsim(); //update the bus
446 void FGJSBsim::set_Altitude(double alt) {
447 double sea_level_radius_meters,lat_geoc;
449 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt );
452 sgGeodToGeoc( get_Latitude(), alt , &sea_level_radius_meters, &lat_geoc);
453 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
454 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
455 fgic->SetLatitudeRadIC( lat_geoc );
456 fgic->SetAltitudeFtIC(alt);
457 fdmex->RunIC(fgic); //loop JSBSim once
458 copy_from_JSBsim(); //update the bus
462 void FGJSBsim::set_V_calibrated_kts(double vc) {
463 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " << vc );
466 fgic->SetVcalibratedKtsIC(vc);
467 fdmex->RunIC(fgic); //loop JSBSim once
468 copy_from_JSBsim(); //update the bus
472 void FGJSBsim::set_Mach_number(double mach) {
473 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " << mach );
476 fgic->SetMachIC(mach);
477 fdmex->RunIC(fgic); //loop JSBSim once
478 copy_from_JSBsim(); //update the bus
482 void FGJSBsim::set_Velocities_Local( double north, double east, double down ){
483 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: "
484 << north << ", " << east << ", " << down );
487 fgic->SetVnorthFpsIC(north);
488 fgic->SetVeastFpsIC(east);
489 fgic->SetVdownFpsIC(down);
490 fdmex->RunIC(fgic); //loop JSBSim once
491 copy_from_JSBsim(); //update the bus
495 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w){
496 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
497 << u << ", " << v << ", " << w );
500 fgic->SetUBodyFpsIC(u);
501 fgic->SetVBodyFpsIC(v);
502 fgic->SetWBodyFpsIC(w);
503 fdmex->RunIC(fgic); //loop JSBSim once
504 copy_from_JSBsim(); //update the bus
509 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi ) {
510 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: "
511 << phi << ", " << theta << ", " << psi );
514 fgic->SetPitchAngleRadIC(theta);
515 fgic->SetRollAngleRadIC(phi);
516 fgic->SetTrueHeadingRadIC(psi);
517 fdmex->RunIC(fgic); //loop JSBSim once
518 copy_from_JSBsim(); //update the bus
523 void FGJSBsim::set_Climb_Rate( double roc) {
524 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
527 fgic->SetClimbRateFpsIC(roc);
528 fdmex->RunIC(fgic); //loop JSBSim once
529 copy_from_JSBsim(); //update the bus
533 void FGJSBsim::set_Gamma_vert_rad( double gamma) {
534 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
537 fgic->SetFlightPathAngleRadIC(gamma);
538 fdmex->RunIC(fgic); //loop JSBSim once
539 copy_from_JSBsim(); //update the bus
544 void FGJSBsim::set_Sea_level_radius(double slr) {
545 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Sea_level_radius: " << slr );
548 fgic->SetSeaLevelRadiusFtIC(slr);
549 fdmex->RunIC(fgic); //loop JSBSim once
550 copy_from_JSBsim(); //update the bus
554 void FGJSBsim::set_Runway_altitude(double ralt) {
555 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Runway_altitude: " << ralt );
558 _set_Runway_altitude( ralt );
559 fgic->SetTerrainAltitudeFtIC( ralt );
560 fdmex->RunIC(fgic); //loop JSBSim once
561 copy_from_JSBsim(); //update the bus
565 void FGJSBsim::set_Static_pressure(double p) {
566 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_pressure: " << p );
569 Atmosphere->SetExPressure(p);
570 if(Atmosphere->External() == true)
574 void FGJSBsim::set_Static_temperature(double T) {
575 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_temperature: " << T );
578 Atmosphere->SetExTemperature(T);
579 if(Atmosphere->External() == true)
584 void FGJSBsim::set_Density(double rho) {
585 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Density: " << rho );
588 Atmosphere->SetExDensity(rho);
589 if(Atmosphere->External() == true)
594 void FGJSBsim::set_Velocities_Local_Airmass (double wnorth,
597 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local_Airmass: "
598 << wnorth << ", " << weast << ", " << wdown );
600 _set_Velocities_Local_Airmass( wnorth, weast, wdown );
602 Atmosphere->SetWindNED(wnorth, weast, wdown );
603 if(Atmosphere->External() == true)