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/options.hxx>
43 #include <FDM/JSBSim/FGFDMExec.h>
44 #include <FDM/JSBSim/FGAircraft.h>
45 #include <FDM/JSBSim/FGFCS.h>
46 #include <FDM/JSBSim/FGPosition.h>
47 #include <FDM/JSBSim/FGRotation.h>
48 #include <FDM/JSBSim/FGState.h>
49 #include <FDM/JSBSim/FGTranslation.h>
50 #include <FDM/JSBSim/FGAuxiliary.h>
51 #include <FDM/JSBSim/FGDefs.h>
52 #include <FDM/JSBSim/FGInitialCondition.h>
53 #include <FDM/JSBSim/FGTrim.h>
54 #include <FDM/JSBSim/FGAtmosphere.h>
58 /******************************************************************************/
60 // Initialize the JSBsim flight model, dt is the time increment for
61 // each subsequent iteration through the EOM
63 int FGJSBsim::init( double dt ) {
67 FG_LOG( FG_FLIGHT, FG_INFO, "Starting and initializing JSBsim" );
68 FG_LOG( FG_FLIGHT, FG_INFO, " created FDMExec" );
70 FGPath aircraft_path( current_options.get_fg_root() );
71 aircraft_path.append( "Aircraft" );
73 FGPath engine_path( current_options.get_fg_root() );
74 engine_path.append( "Engine" );
76 //FDMExec.GetState()->Setdt( dt );
78 result = FDMExec.LoadModel( aircraft_path.str(),
80 current_options.get_aircraft() );
81 FDMExec.GetState()->Setdt( dt );
84 FG_LOG( FG_FLIGHT, FG_INFO, " loaded aircraft " << current_options.get_aircraft() );
86 FG_LOG( FG_FLIGHT, FG_INFO, " aircraft" << current_options.get_aircraft()
87 << " does not exist");
91 FDMExec.GetAtmosphere()->SetExTemperature(get_Static_temperature());
92 FDMExec.GetAtmosphere()->SetExPressure(get_Static_pressure());
93 FDMExec.GetAtmosphere()->SetExDensity(get_Density());
94 FDMExec.GetAtmosphere()->SetWindNED(get_V_north_airmass(),
96 get_V_down_airmass());
98 FDMExec.GetAtmosphere()->UseInternal();
101 FDMExec.GetPosition()->SetRunwayRadius(scenery.cur_radius*METER_TO_FEET);
102 FDMExec.GetPosition()->SetSeaLevelRadius(get_Sea_level_radius());
104 FG_LOG( FG_FLIGHT, FG_INFO, " Initializing JSBSim with:" );
106 FGInitialCondition *fgic = new FGInitialCondition(&FDMExec);
107 fgic->SetAltitudeFtIC(get_Altitude());
108 if((current_options.get_mach() < 0) && (current_options.get_vc() < 0 )) {
109 fgic->SetUBodyFpsIC(current_options.get_uBody());
110 fgic->SetVBodyFpsIC(current_options.get_vBody());
111 fgic->SetWBodyFpsIC(current_options.get_wBody());
112 FG_LOG(FG_FLIGHT,FG_INFO, " u,v,w= " << current_options.get_uBody()
113 << ", " << current_options.get_vBody()
114 << ", " << current_options.get_wBody());
115 } else if (current_options.get_vc() < 0) {
116 fgic->SetMachIC(current_options.get_mach());
117 FG_LOG(FG_FLIGHT,FG_INFO, " mach: " << current_options.get_mach() );
119 fgic->SetVcalibratedKtsIC(current_options.get_vc());
120 FG_LOG(FG_FLIGHT,FG_INFO, " vc: " << current_options.get_vc() );
121 //this should cover the case in which no speed switches are used
122 //current_options.get_vc() will return zero by default
125 //fgic->SetFlightPathAngleDegIC(current_options.get_Gamma());
126 fgic->SetRollAngleRadIC(get_Phi());
127 fgic->SetPitchAngleRadIC(get_Theta());
128 fgic->SetHeadingRadIC(get_Psi());
129 fgic->SetLatitudeRadIC(get_Lat_geocentric());
130 fgic->SetLongitudeRadIC(get_Longitude());
135 //FG_LOG( FG_FLIGHT, FG_INFO, " gamma: " << current_options.get_Gamma());
136 FG_LOG( FG_FLIGHT, FG_INFO, " phi: " << get_Phi());
137 //FG_LOG( FG_FLIGHT, FG_INFO, " theta: " << get_Theta() );
138 FG_LOG( FG_FLIGHT, FG_INFO, " psi: " << get_Psi() );
139 FG_LOG( FG_FLIGHT, FG_INFO, " lat: " << get_Latitude() );
140 FG_LOG( FG_FLIGHT, FG_INFO, " lon: " << get_Longitude() );
142 FG_LOG( FG_FLIGHT, FG_INFO, " Pressure Altiude: " << get_Altitude() );
143 FG_LOG( FG_FLIGHT, FG_INFO, " Terrain Altitude: "
144 << scenery.cur_radius*METER_TO_FEET );
145 FG_LOG( FG_FLIGHT, FG_INFO, " AGL Altitude: "
146 << get_Altitude() + get_Sea_level_radius()
147 - scenery.cur_radius*METER_TO_FEET );
149 FG_LOG( FG_FLIGHT, FG_INFO, " current_options.get_altitude(): "
150 << current_options.get_altitude() );
151 //must check > 0, != 0 will give bad result if --notrim set
152 if(current_options.get_trim_mode() > 0) {
154 FG_LOG( FG_FLIGHT, FG_INFO, " Starting trim..." );
155 FGTrim *fgtrim=new FGTrim(&FDMExec,fgic,tLongitudinal);
159 fgtrim->ReportState();
162 controls.set_elevator_trim(FDMExec.GetFCS()->GetPitchTrimCmd());
163 controls.set_throttle(FGControls::ALL_ENGINES,FDMExec.GetFCS()->GetThrottleCmd(0)/100);
165 trim_elev=FDMExec.GetFCS()->GetPitchTrimCmd();
166 trim_throttle=FDMExec.GetFCS()->GetThrottleCmd(0)/100;
167 //the trimming routine only knows how to get 1 value for throttle
170 FG_LOG( FG_FLIGHT, FG_INFO, " Trim complete." );
172 FG_LOG( FG_FLIGHT, FG_INFO, " Initializing without trim" );
173 FDMExec.GetState()->Initialize(fgic);
179 FG_LOG( FG_FLIGHT, FG_INFO, " loaded initial conditions" );
181 FG_LOG( FG_FLIGHT, FG_INFO, " set dt" );
183 FG_LOG( FG_FLIGHT, FG_INFO, "Finished initializing JSBSim" );
190 /******************************************************************************/
192 // Run an iteration of the EOM (equations of motion)
194 int FGJSBsim::update( int multiloop ) {
195 double save_alt = 0.0;
196 double time_step = (1.0 / current_options.get_model_hz()) * multiloop;
197 double start_elev = get_Altitude();
200 // lets try to avoid really screwing up the JSBsim model
201 if ( get_Altitude() < -9000 ) {
202 save_alt = get_Altitude();
208 controls.set_elevator_trim(trim_elev);
209 controls.set_throttle(FGControls::ALL_ENGINES,trim_throttle);
211 controls.set_elevator(0.0);
212 controls.set_aileron(0.0);
213 controls.set_rudder(0.0);
220 for ( int i = 0; i < multiloop; i++ ) {
224 // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
225 // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
227 // translate JSBsim back to FG structure so that the
228 // autopilot (and the rest of the sim can use the updated values
232 // but lets restore our original bogus altitude when we are done
234 if ( save_alt < -9000.0 ) {
235 set_Altitude( save_alt );
238 double end_elev = get_Altitude();
243 /******************************************************************************/
245 // Convert from the FGInterface struct to the JSBsim generic_ struct
247 int FGJSBsim::copy_to_JSBsim() {
248 // copy control positions into the JSBsim structure
250 FDMExec.GetFCS()->SetDaCmd( controls.get_aileron());
251 FDMExec.GetFCS()->SetDeCmd( controls.get_elevator());
252 FDMExec.GetFCS()->SetPitchTrimCmd(controls.get_elevator_trim());
253 FDMExec.GetFCS()->SetDrCmd( controls.get_rudder());
254 FDMExec.GetFCS()->SetDfCmd( controls.get_flaps() );
255 FDMExec.GetFCS()->SetDsbCmd( 0.0 ); //speedbrakes
256 FDMExec.GetFCS()->SetDspCmd( 0.0 ); //spoilers
257 FDMExec.GetFCS()->SetThrottleCmd( FGControls::ALL_ENGINES,
258 controls.get_throttle( 0 ) * 100.0 );
260 FDMExec.GetFCS()->SetLBrake( controls.get_brake( 0 ) );
261 FDMExec.GetFCS()->SetRBrake( controls.get_brake( 1 ) );
262 FDMExec.GetFCS()->SetCBrake( controls.get_brake( 2 ) );
264 // Inform JSBsim of the local terrain altitude; uncommented 5/3/00
265 // FDMExec.GetPosition()->SetRunwayElevation(get_Runway_altitude()); // seems to work
266 FDMExec.GetPosition()->SetRunwayRadius(scenery.cur_radius*METER_TO_FEET);
267 FDMExec.GetPosition()->SetSeaLevelRadius(get_Sea_level_radius());
269 FDMExec.GetAtmosphere()->SetExTemperature(get_Static_temperature());
270 FDMExec.GetAtmosphere()->SetExPressure(get_Static_pressure());
271 FDMExec.GetAtmosphere()->SetExDensity(get_Density());
272 FDMExec.GetAtmosphere()->SetWindNED(get_V_north_airmass(),
273 get_V_east_airmass(),
274 get_V_down_airmass());
279 /******************************************************************************/
281 // Convert from the JSBsim generic_ struct to the FGInterface struct
283 int FGJSBsim::copy_from_JSBsim() {
285 set_Inertias( FDMExec.GetAircraft()->GetMass(),
286 FDMExec.GetAircraft()->GetIxx(),
287 FDMExec.GetAircraft()->GetIyy(),
288 FDMExec.GetAircraft()->GetIzz(),
289 FDMExec.GetAircraft()->GetIxz() );
291 set_CG_Position ( FDMExec.GetAircraft()->GetXYZcg()(1),
292 FDMExec.GetAircraft()->GetXYZcg()(2),
293 FDMExec.GetAircraft()->GetXYZcg()(3) );
295 set_Accels_Body ( FDMExec.GetTranslation()->GetUVWdot()(1),
296 FDMExec.GetTranslation()->GetUVWdot()(2),
297 FDMExec.GetTranslation()->GetUVWdot()(3) );
299 set_Accels_CG_Body ( FDMExec.GetTranslation()->GetUVWdot()(1),
300 FDMExec.GetTranslation()->GetUVWdot()(2),
301 FDMExec.GetTranslation()->GetUVWdot()(3) );
303 //set_Accels_CG_Body_N ( FDMExec.GetTranslation()->GetNcg()(1),
304 // FDMExec.GetTranslation()->GetNcg()(2),
305 // FDMExec.GetTranslation()->GetNcg()(3) );
307 set_Accels_Pilot_Body( FDMExec.GetAuxiliary()->GetPilotAccel()(1),
308 FDMExec.GetAuxiliary()->GetPilotAccel()(2),
309 FDMExec.GetAuxiliary()->GetPilotAccel()(3) );
311 //set_Accels_Pilot_Body_N( FDMExec.GetAuxiliary()->GetNpilot()(1),
312 // FDMExec.GetAuxiliary()->GetNpilot()(2),
313 // FDMExec.GetAuxiliary()->GetNpilot()(3) );
317 set_Nlf( FDMExec.GetAircraft()->GetNlf());
323 set_Velocities_Local( FDMExec.GetPosition()->GetVn(),
324 FDMExec.GetPosition()->GetVe(),
325 FDMExec.GetPosition()->GetVd() );
327 set_Velocities_Wind_Body( FDMExec.GetTranslation()->GetUVW()(1),
328 FDMExec.GetTranslation()->GetUVW()(2),
329 FDMExec.GetTranslation()->GetUVW()(3) );
331 set_V_equiv_kts( FDMExec.GetAuxiliary()->GetVequivalentKTS() );
333 //set_V_calibrated( FDMExec.GetAuxiliary()->GetVcalibratedFPS() );
335 set_V_calibrated_kts( FDMExec.GetAuxiliary()->GetVcalibratedKTS() );
337 set_V_ground_speed ( FDMExec.GetPosition()->GetVground() );
339 set_Omega_Body( FDMExec.GetState()->GetParameter(FG_ROLLRATE),
340 FDMExec.GetState()->GetParameter(FG_PITCHRATE),
341 FDMExec.GetState()->GetParameter(FG_YAWRATE) );
343 set_Euler_Rates( FDMExec.GetRotation()->GetEulerRates()(2),
344 FDMExec.GetRotation()->GetEulerRates()(1),
345 FDMExec.GetRotation()->GetEulerRates()(3));
347 // ***FIXME*** set_Geocentric_Rates( Latitude_dot, Longitude_dot, Radius_dot );
349 set_Mach_number( FDMExec.GetTranslation()->GetMach());
353 double lat_geoc = FDMExec.GetPosition()->GetLatitude();
354 double lon = FDMExec.GetPosition()->GetLongitude();
355 double alt = FDMExec.GetPosition()->Geth();
356 double lat_geod, tmp_alt, sl_radius1, sl_radius2, tmp_lat_geoc;
358 sgGeocToGeod( lat_geoc, EQUATORIAL_RADIUS_M + alt * FEET_TO_METER,
359 &lat_geod, &tmp_alt, &sl_radius1 );
360 sgGeodToGeoc( lat_geod, alt * FEET_TO_METER, &sl_radius2, &tmp_lat_geoc );
362 FG_LOG( FG_FLIGHT, FG_DEBUG, "lon = " << lon << " lat_geod = " << lat_geod
363 << " lat_geoc = " << lat_geoc
364 << " alt = " << alt << " tmp_alt = " << tmp_alt * METER_TO_FEET
365 << " sl_radius1 = " << sl_radius1 * METER_TO_FEET
366 << " sl_radius2 = " << sl_radius2 * METER_TO_FEET
367 << " Equator = " << EQUATORIAL_RADIUS_FT );
369 set_Geocentric_Position( lat_geoc, lon,
370 sl_radius2 * METER_TO_FEET + alt );
371 set_Geodetic_Position( lat_geod, lon, alt );
372 set_Euler_Angles( FDMExec.GetRotation()->Getphi(),
373 FDMExec.GetRotation()->Gettht(),
374 FDMExec.GetRotation()->Getpsi() );
376 set_Alpha( FDMExec.GetTranslation()->Getalpha() );
377 set_Beta( FDMExec.GetTranslation()->Getbeta() );
379 set_Gamma_vert_rad( FDMExec.GetPosition()->GetGamma() );
380 // set_Gamma_horiz_rad( Gamma_horiz_rad );
382 /* **FIXME*** */ set_Sea_level_radius( sl_radius2 * METER_TO_FEET );
383 /* **FIXME*** */ set_Earth_position_angle( 0.0 );
385 // /* ***FIXME*** */ set_Runway_altitude( 0.0 );
387 set_sin_lat_geocentric( lat_geoc );
388 set_cos_lat_geocentric( lat_geoc );
389 set_sin_cos_longitude( lon );
390 set_sin_cos_latitude( lat_geod );
392 set_Climb_Rate(FDMExec.GetPosition()->Gethdot());