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/fg_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/FGTrimLong.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.GetAircraft()->LoadAircraft( aircraft_path.str(),
80 current_options.get_aircraft() );
83 FG_LOG( FG_FLIGHT, FG_INFO, " loaded aircraft" << current_options.get_aircraft() );
85 FG_LOG( FG_FLIGHT, FG_INFO, " aircraft" << current_options.get_aircraft()
86 << " does not exist");
90 FDMExec.GetAtmosphere()->UseInternal();
92 FG_LOG( FG_FLIGHT, FG_INFO, " Initializing JSBSim with:" );
94 FGInitialCondition *fgic = new FGInitialCondition(&FDMExec);
95 fgic->SetAltitudeFtIC(get_Altitude());
96 if((current_options.get_mach() < 0) && (current_options.get_vc() < 0 )) {
97 fgic->SetUBodyFpsIC(current_options.get_uBody());
98 fgic->SetVBodyFpsIC(current_options.get_vBody());
99 fgic->SetWBodyFpsIC(current_options.get_wBody());
100 FG_LOG(FG_FLIGHT,FG_INFO, " u,v,w= " << current_options.get_uBody()
101 << ", " << current_options.get_vBody()
102 << ", " << current_options.get_wBody());
103 } else if (current_options.get_vc() < 0) {
104 fgic->SetMachIC(current_options.get_mach());
105 FG_LOG(FG_FLIGHT,FG_INFO, " mach: " << current_options.get_mach() );
107 fgic->SetVcalibratedKtsIC(current_options.get_vc());
108 FG_LOG(FG_FLIGHT,FG_INFO, " vc: " << current_options.get_vc() );
109 //this should cover the case in which no speed switches are used
110 //current_options.get_vc() will return zero by default
114 fgic->SetRollAngleRadIC(get_Phi());
115 fgic->SetPitchAngleRadIC(get_Theta());
116 fgic->SetHeadingRadIC(get_Psi());
117 // fgic->SetLatitudeRadIC(get_Latitude());
118 fgic->SetLatitudeRadIC(get_Lat_geocentric());
119 fgic->SetLongitudeRadIC(get_Longitude());
121 FDMExec.GetPosition()->SetRunwayRadius(scenery.cur_radius*METER_TO_FEET);
122 FDMExec.GetPosition()->SetSeaLevelRadius(get_Sea_level_radius());
124 FG_LOG( FG_FLIGHT, FG_INFO, " phi: " << get_Phi());
125 FG_LOG( FG_FLIGHT, FG_INFO, " theta: " << get_Theta() );
126 FG_LOG( FG_FLIGHT, FG_INFO, " psi: " << get_Psi() );
127 FG_LOG( FG_FLIGHT, FG_INFO, " lat: " << get_Latitude() );
128 FG_LOG( FG_FLIGHT, FG_INFO, " lon: " << get_Longitude() );
129 FG_LOG( FG_FLIGHT, FG_INFO, " alt: " << get_Altitude() );
131 //must check > 0, != 0 will give bad result if --notrim set
132 if(current_options.get_trim_mode() > 0) {
134 FG_LOG( FG_FLIGHT, FG_INFO, " Starting trim..." );
135 FGTrimLong *fgtrim=new FGTrimLong(&FDMExec,fgic);
139 fgtrim->ReportState();
141 controls.set_elevator_trim(FDMExec.GetFCS()->GetPitchTrimCmd());
142 controls.set_throttle(FGControls::ALL_ENGINES,FDMExec.GetFCS()->GetThrottleCmd(0)/100);
143 //the trimming routine only knows how to get 1 value for throttle
147 FG_LOG( FG_FLIGHT, FG_INFO, " Trim complete." );
149 FG_LOG( FG_FLIGHT, FG_INFO, " Initializing without trim" );
150 FDMExec.GetState()->Initialize(fgic);
156 FG_LOG( FG_FLIGHT, FG_INFO, " loaded initial conditions" );
158 FG_LOG( FG_FLIGHT, FG_INFO, " set dt" );
160 FG_LOG( FG_FLIGHT, FG_INFO, "Finished initializing JSBSim" );
167 /******************************************************************************/
169 // Run an iteration of the EOM (equations of motion)
171 int FGJSBsim::update( int multiloop ) {
172 double save_alt = 0.0;
173 double time_step = (1.0 / current_options.get_model_hz()) * multiloop;
174 double start_elev = get_Altitude();
176 // lets try to avoid really screwing up the JSBsim model
177 if ( get_Altitude() < -9000 ) {
178 save_alt = get_Altitude();
182 // copy control positions into the JSBsim structure
184 FDMExec.GetFCS()->SetDaCmd( controls.get_aileron());
185 FDMExec.GetFCS()->SetDeCmd( controls.get_elevator());
186 FDMExec.GetFCS()->SetPitchTrimCmd(controls.get_elevator_trim());
187 FDMExec.GetFCS()->SetDrCmd( controls.get_rudder());
188 FDMExec.GetFCS()->SetDfCmd( controls.get_flaps() );
189 FDMExec.GetFCS()->SetDsbCmd( 0.0 ); //speedbrakes
190 FDMExec.GetFCS()->SetDspCmd( 0.0 ); //spoilers
191 FDMExec.GetFCS()->SetThrottleCmd( FGControls::ALL_ENGINES,
192 controls.get_throttle( 0 ) * 100.0 );
194 // FCS->SetBrake( controls.get_brake( 0 ) );
196 // Inform JSBsim of the local terrain altitude; uncommented 5/3/00
197 // FDMExec.GetPosition()->SetRunwayElevation(get_Runway_altitude()); // seems to work
198 FDMExec.GetPosition()->SetRunwayRadius(scenery.cur_radius*METER_TO_FEET);
199 FDMExec.GetPosition()->SetSeaLevelRadius(get_Sea_level_radius());
201 FDMExec.GetAtmosphere()->SetExTemperature(get_Static_temperature());
202 FDMExec.GetAtmosphere()->SetExPressure(get_Static_pressure());
203 FDMExec.GetAtmosphere()->SetExDensity(get_Density());
204 FDMExec.GetAtmosphere()->SetWindNED(get_V_north_airmass(),
205 get_V_east_airmass(),
206 get_V_down_airmass());
208 for ( int i = 0; i < multiloop; i++ ) {
212 // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
213 // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
215 // translate JSBsim back to FG structure so that the
216 // autopilot (and the rest of the sim can use the updated values
220 // but lets restore our original bogus altitude when we are done
222 if ( save_alt < -9000.0 ) {
223 set_Altitude( save_alt );
226 double end_elev = get_Altitude();
227 //if ( time_step > 0.0 ) {
229 // set_Climb_Rate( (end_elev - start_elev) / time_step );
235 /******************************************************************************/
237 // Convert from the FGInterface struct to the JSBsim generic_ struct
239 int FGJSBsim::copy_to_JSBsim() {
243 /******************************************************************************/
245 // Convert from the JSBsim generic_ struct to the FGInterface struct
247 int FGJSBsim::copy_from_JSBsim() {
249 set_Inertias( FDMExec.GetAircraft()->GetMass(),
250 FDMExec.GetAircraft()->GetIxx(),
251 FDMExec.GetAircraft()->GetIyy(),
252 FDMExec.GetAircraft()->GetIzz(),
253 FDMExec.GetAircraft()->GetIxz() );
255 set_CG_Position ( FDMExec.GetAircraft()->GetXYZcg()(1),
256 FDMExec.GetAircraft()->GetXYZcg()(2),
257 FDMExec.GetAircraft()->GetXYZcg()(3) );
259 set_Accels_Body ( FDMExec.GetTranslation()->GetUVWdot()(1),
260 FDMExec.GetTranslation()->GetUVWdot()(2),
261 FDMExec.GetTranslation()->GetUVWdot()(3) );
263 set_Accels_CG_Body ( FDMExec.GetTranslation()->GetUVWdot()(1),
264 FDMExec.GetTranslation()->GetUVWdot()(2),
265 FDMExec.GetTranslation()->GetUVWdot()(3) );
267 set_Accels_CG_Body_N ( FDMExec.GetTranslation()->GetNcg()(1),
268 FDMExec.GetTranslation()->GetNcg()(2),
269 FDMExec.GetTranslation()->GetNcg()(3) );
271 set_Accels_Pilot_Body( FDMExec.GetAuxiliary()->GetPilotAccel()(1),
272 FDMExec.GetAuxiliary()->GetPilotAccel()(2),
273 FDMExec.GetAuxiliary()->GetPilotAccel()(3) );
275 set_Accels_Pilot_Body_N( FDMExec.GetAuxiliary()->GetNpilot()(1),
276 FDMExec.GetAuxiliary()->GetNpilot()(2),
277 FDMExec.GetAuxiliary()->GetNpilot()(3) );
281 set_Nlf( FDMExec.GetAircraft()->GetNlf());
287 set_Velocities_Local( FDMExec.GetPosition()->GetVn(),
288 FDMExec.GetPosition()->GetVe(),
289 FDMExec.GetPosition()->GetVd() );
291 set_Velocities_Wind_Body( FDMExec.GetTranslation()->GetUVW()(1),
292 FDMExec.GetTranslation()->GetUVW()(2),
293 FDMExec.GetTranslation()->GetUVW()(3) );
295 set_V_equiv_kts( FDMExec.GetAuxiliary()->GetVequivalentKTS() );
297 //set_V_calibrated( FDMExec.GetAuxiliary()->GetVcalibratedFPS() );
299 set_V_calibrated_kts( FDMExec.GetAuxiliary()->GetVcalibratedKTS() );
301 set_V_ground_speed ( FDMExec.GetPosition()->GetVground() );
303 set_Omega_Body( FDMExec.GetState()->GetParameter(FG_ROLLRATE),
304 FDMExec.GetState()->GetParameter(FG_PITCHRATE),
305 FDMExec.GetState()->GetParameter(FG_YAWRATE) );
307 /* HUH!?! */ set_Euler_Rates( FDMExec.GetRotation()->Getphi(),
308 FDMExec.GetRotation()->Gettht(),
309 FDMExec.GetRotation()->Getpsi() );
311 // ***FIXME*** set_Geocentric_Rates( Latitude_dot, Longitude_dot, Radius_dot );
313 set_Mach_number( FDMExec.GetTranslation()->GetMach());
317 double lat_geoc = FDMExec.GetPosition()->GetLatitude();
318 double lon = FDMExec.GetPosition()->GetLongitude();
319 double alt = FDMExec.GetPosition()->Geth();
320 double lat_geod, tmp_alt, sl_radius1, sl_radius2, tmp_lat_geoc;
322 fgGeocToGeod( lat_geoc, EQUATORIAL_RADIUS_M + alt * FEET_TO_METER,
323 &lat_geod, &tmp_alt, &sl_radius1 );
324 fgGeodToGeoc( lat_geod, alt * FEET_TO_METER, &sl_radius2, &tmp_lat_geoc );
326 FG_LOG( FG_FLIGHT, FG_DEBUG, "lon = " << lon << " lat_geod = " << lat_geod
327 << " lat_geoc = " << lat_geoc
328 << " alt = " << alt << " tmp_alt = " << tmp_alt * METER_TO_FEET
329 << " sl_radius1 = " << sl_radius1 * METER_TO_FEET
330 << " sl_radius2 = " << sl_radius2 * METER_TO_FEET
331 << " Equator = " << EQUATORIAL_RADIUS_FT );
333 set_Geocentric_Position( lat_geoc, lon,
334 sl_radius2 * METER_TO_FEET + alt );
335 set_Geodetic_Position( lat_geod, lon, alt );
336 set_Euler_Angles( FDMExec.GetRotation()->Getphi(),
337 FDMExec.GetRotation()->Gettht(),
338 FDMExec.GetRotation()->Getpsi() );
340 set_Alpha( FDMExec.GetTranslation()->Getalpha() );
341 set_Beta( FDMExec.GetTranslation()->Getbeta() );
343 set_Gamma_vert_rad( FDMExec.GetPosition()->GetGamma() );
344 // set_Gamma_horiz_rad( Gamma_horiz_rad );
346 /* **FIXME*** */ set_Sea_level_radius( sl_radius2 * METER_TO_FEET );
347 /* **FIXME*** */ set_Earth_position_angle( 0.0 );
349 // /* ***FIXME*** */ set_Runway_altitude( 0.0 );
351 set_sin_lat_geocentric( lat_geoc );
352 set_cos_lat_geocentric( lat_geoc );
353 set_sin_cos_longitude( lon );
354 set_sin_cos_latitude( lat_geod );
356 set_Climb_Rate(FDMExec.GetPosition()->Gethdot());