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/logstream.hxx>
33 #include <simgear/constants.h>
34 #include <simgear/fg_geodesy.hxx>
35 #include <simgear/fgpath.hxx>
37 #include <Aircraft/aircraft.hxx>
38 #include <Controls/controls.hxx>
39 #include <Main/options.hxx>
41 #include <FDM/JSBsim/FGFDMExec.h>
42 #include <FDM/JSBsim/FGAircraft.h>
43 #include <FDM/JSBsim/FGFCS.h>
44 #include <FDM/JSBsim/FGPosition.h>
45 #include <FDM/JSBsim/FGRotation.h>
46 #include <FDM/JSBsim/FGState.h>
47 #include <FDM/JSBsim/FGTranslation.h>
48 #include <FDM/JSBsim/FGAuxiliary.h>
49 #include <FDM/JSBsim/FGDefs.h>
54 // Initialize the JSBsim flight model, dt is the time increment for
55 // each subsequent iteration through the EOM
56 int FGJSBsim::init( double dt ) {
57 FG_LOG( FG_FLIGHT, FG_INFO, "Starting and initializing JSBsim" );
59 FG_LOG( FG_FLIGHT, FG_INFO, " created FDMExec" );
61 FGPath aircraft_path( current_options.get_fg_root() );
62 aircraft_path.append( "Aircraft" );
64 FGPath engine_path( current_options.get_fg_root() );
65 engine_path.append( "Engine" );
67 FDMExec.GetAircraft()->LoadAircraft( aircraft_path.str(),
69 current_options.get_aircraft() );
70 FG_LOG( FG_FLIGHT, FG_INFO, " loaded aircraft" <<
71 current_options.get_aircraft() );
73 FG_LOG( FG_FLIGHT, FG_INFO, "Initializing JSBsim with:" );
74 FG_LOG( FG_FLIGHT, FG_INFO, " U: " << current_options.get_uBody() );
75 FG_LOG( FG_FLIGHT, FG_INFO, " V: " <<current_options.get_vBody() );
76 FG_LOG( FG_FLIGHT, FG_INFO, " W: " <<current_options.get_wBody() );
77 FG_LOG( FG_FLIGHT, FG_INFO, " phi: " <<get_Phi() );
78 FG_LOG( FG_FLIGHT, FG_INFO, "theta: " << get_Theta() );
79 FG_LOG( FG_FLIGHT, FG_INFO, " psi: " << get_Psi() );
80 FG_LOG( FG_FLIGHT, FG_INFO, " lat: " << get_Latitude() );
81 FG_LOG( FG_FLIGHT, FG_INFO, " lon: " << get_Longitude() );
82 FG_LOG( FG_FLIGHT, FG_INFO, " alt: " << get_Altitude() );
84 FDMExec.GetState()->Initialize(
85 current_options.get_uBody(),
86 current_options.get_vBody(),
87 current_options.get_wBody(),
89 get_Theta() * DEGTORAD,
96 FG_LOG( FG_FLIGHT, FG_INFO, " loaded initial conditions" );
98 FDMExec.GetState()->Setdt( dt );
99 FG_LOG( FG_FLIGHT, FG_INFO, " set dt" );
101 FG_LOG( FG_FLIGHT, FG_INFO, "Finished initializing JSBsim" );
109 // Run an iteration of the EOM (equations of motion)
110 int FGJSBsim::update( int multiloop ) {
111 double save_alt = 0.0;
112 double time_step = (1.0 / current_options.get_model_hz()) * multiloop;
113 double start_elev = get_Altitude();
115 // lets try to avoid really screwing up the JSBsim model
116 if ( get_Altitude() < -9000 ) {
117 save_alt = get_Altitude();
121 // copy control positions into the JSBsim structure
122 FDMExec.GetFCS()->SetDa( controls.get_aileron());
123 FDMExec.GetFCS()->SetDe( controls.get_elevator()
124 + controls.get_elevator_trim() );
125 FDMExec.GetFCS()->SetDr( controls.get_rudder());
126 FDMExec.GetFCS()->SetDf( 0.0 );
127 FDMExec.GetFCS()->SetDs( 0.0 );
128 FDMExec.GetFCS()->SetThrottle( FGControls::ALL_ENGINES,
129 controls.get_throttle( 0 ) * 100.0 );
130 // FCS->SetBrake( controls.get_brake( 0 ) );
132 // Inform JSBsim of the local terrain altitude
133 // Runway_altitude = get_Runway_altitude();
135 // old -- FGInterface_2_JSBsim() not needed except for Init()
136 // translate FG to JSBsim structure
137 // FGInterface_2_JSBsim(f);
138 // printf("FG_Altitude = %.2f\n", FG_Altitude * 0.3048);
139 // printf("Altitude = %.2f\n", Altitude * 0.3048);
140 // printf("Radius to Vehicle = %.2f\n", Radius_to_vehicle * 0.3048);
142 /* FDMExec.GetState()->Setsim_time(State->Getsim_time()
143 + State->Getdt() * multiloop); */
145 for ( int i = 0; i < multiloop; i++ ) {
149 // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
150 // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
152 // translate JSBsim back to FG structure so that the
153 // autopilot (and the rest of the sim can use the updated
158 // but lets restore our original bogus altitude when we are done
159 if ( save_alt < -9000.0 ) {
160 set_Altitude( save_alt );
163 double end_elev = get_Altitude();
164 if ( time_step > 0.0 ) {
166 set_Climb_Rate( (end_elev - start_elev) / time_step );
173 // Convert from the FGInterface struct to the JSBsim generic_ struct
174 int FGJSBsim::copy_to_JSBsim() {
179 // Convert from the JSBsim generic_ struct to the FGInterface struct
180 int FGJSBsim::copy_from_JSBsim() {
183 set_Velocities_Local( FDMExec.GetPosition()->GetVn(),
184 FDMExec.GetPosition()->GetVe(),
185 FDMExec.GetPosition()->GetVd() );
186 // set_Velocities_Ground( V_north_rel_ground, V_east_rel_ground,
187 // V_down_rel_ground );
188 // set_Velocities_Local_Airmass( V_north_airmass, V_east_airmass,
190 // set_Velocities_Local_Rel_Airmass( V_north_rel_airmass,
191 // V_east_rel_airmass, V_down_rel_airmass );
192 // set_Velocities_Gust( U_gust, V_gust, W_gust );
193 // set_Velocities_Wind_Body( U_body, V_body, W_body );
195 // set_V_rel_wind( V_rel_wind );
196 // set_V_true_kts( V_true_kts );
197 // set_V_rel_ground( V_rel_ground );
198 // set_V_inertial( V_inertial );
199 // set_V_ground_speed( V_ground_speed );
200 // set_V_equiv( V_equiv );
202 set_V_equiv_kts( FDMExec.GetAuxiliary()->GetVequivalentKTS() );
203 //set_V_calibrated( FDMExec.GetAuxiliary()->GetVcalibratedFPS() );
204 set_V_calibrated_kts( FDMExec.GetAuxiliary()->GetVcalibratedKTS() );
206 set_Omega_Body( FDMExec.GetRotation()->GetP(),
207 FDMExec.GetRotation()->GetQ(),
208 FDMExec.GetRotation()->GetR() );
209 // set_Omega_Local( P_local, Q_local, R_local );
210 // set_Omega_Total( P_total, Q_total, R_total );
212 // set_Euler_Rates( Phi_dot, Theta_dot, Psi_dot );
213 // ***FIXME*** set_Geocentric_Rates( Latitude_dot, Longitude_dot, Radius_dot );
215 set_Mach_number( FDMExec.GetState()->GetMach());
218 double lat_geoc = FDMExec.GetState()->Getlatitude();
219 double lon = FDMExec.GetState()->Getlongitude();
220 double alt = FDMExec.GetState()->Geth();
221 double lat_geod, tmp_alt, sl_radius1, sl_radius2, tmp_lat_geoc;
222 fgGeocToGeod( lat_geoc, EQUATORIAL_RADIUS_M + alt * FEET_TO_METER,
223 &lat_geod, &tmp_alt, &sl_radius1 );
224 fgGeodToGeoc( lat_geod, alt * FEET_TO_METER, &sl_radius2, &tmp_lat_geoc );
226 FG_LOG( FG_FLIGHT, FG_DEBUG, "lon = " << lon << " lat_geod = " << lat_geod
227 << " lat_geoc = " << lat_geoc
228 << " alt = " << alt << " tmp_alt = " << tmp_alt * METER_TO_FEET
229 << " sl_radius1 = " << sl_radius1 * METER_TO_FEET
230 << " sl_radius2 = " << sl_radius2 * METER_TO_FEET
231 << " Equator = " << EQUATORIAL_RADIUS_FT );
233 set_Geocentric_Position( lat_geoc, lon,
234 sl_radius2 * METER_TO_FEET + alt );
235 set_Geodetic_Position( lat_geod, lon, alt );
236 set_Euler_Angles( FDMExec.GetRotation()->Getphi(),
237 FDMExec.GetRotation()->Gettht(),
238 FDMExec.GetRotation()->Getpsi() );
240 // Miscellaneous quantities
241 // set_T_Local_to_Body(T_local_to_body_m);
242 // set_Gravity( Gravity );
243 // set_Centrifugal_relief( Centrifugal_relief );
245 set_Alpha( FDMExec.GetTranslation()->Getalpha() );
246 set_Beta( FDMExec.GetTranslation()->Getbeta() );
247 // set_Alpha_dot( Alpha_dot );
248 // set_Beta_dot( Beta_dot );
250 // set_Cos_alpha( Cos_alpha );
251 // set_Sin_alpha( Sin_alpha );
252 // set_Cos_beta( Cos_beta );
253 // set_Sin_beta( Sin_beta );
255 // set_Cos_phi( Cos_phi );
256 // set_Sin_phi( Sin_phi );
257 // set_Cos_theta( Cos_theta );
258 // set_Sin_theta( Sin_theta );
259 // set_Cos_psi( Cos_psi );
260 // set_Sin_psi( Sin_psi );
262 // ***ATTENDTOME*** set_Gamma_vert_rad( Gamma_vert_rad );
263 // set_Gamma_horiz_rad( Gamma_horiz_rad );
265 // set_Sigma( Sigma );
266 // set_Density( Density );
267 // set_V_sound( V_sound );
268 // set_Mach_number( Mach_number );
270 // set_Static_pressure( Static_pressure );
271 // set_Total_pressure( Total_pressure );
272 // set_Impact_pressure( Impact_pressure );
273 // set_Dynamic_pressure( Dynamic_pressure );
275 // set_Static_temperature( Static_temperature );
276 // set_Total_temperature( Total_temperature );
278 /* **FIXME*** */ set_Sea_level_radius( sl_radius2 * METER_TO_FEET );
279 /* **FIXME*** */ set_Earth_position_angle( 0.0 );
281 /* ***FIXME*** */ set_Runway_altitude( 0.0 );
282 // set_Runway_latitude( Runway_latitude );
283 // set_Runway_longitude( Runway_longitude );
284 // set_Runway_heading( Runway_heading );
285 // set_Radius_to_rwy( Radius_to_rwy );
287 // set_CG_Rwy_Local( D_cg_north_of_rwy, D_cg_east_of_rwy, D_cg_above_rwy);
288 // set_CG_Rwy_Rwy( X_cg_rwy, Y_cg_rwy, H_cg_rwy );
289 // set_Pilot_Rwy_Local( D_pilot_north_of_rwy, D_pilot_east_of_rwy,
290 // D_pilot_above_rwy );
291 // set_Pilot_Rwy_Rwy( X_pilot_rwy, Y_pilot_rwy, H_pilot_rwy );
293 set_sin_lat_geocentric( lat_geoc );
294 set_cos_lat_geocentric( lat_geoc );
295 set_sin_cos_longitude( lon );
296 set_sin_cos_latitude( lat_geod );