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 <Include/compiler.h>
26 #ifdef FG_MATH_EXCEPTION_CLASH
32 #include <Aircraft/aircraft.hxx>
33 #include <Controls/controls.hxx>
34 #include <Debug/logstream.hxx>
35 #include <Include/fg_constants.h>
36 #include <Main/options.hxx>
37 #include <Math/fg_geodesy.hxx>
38 #include <Misc/fgpath.hxx>
40 #include <FDM/JSBsim/FGFDMExec.h>
41 #include <FDM/JSBsim/FGAircraft.h>
42 #include <FDM/JSBsim/FGFCS.h>
43 #include <FDM/JSBsim/FGPosition.h>
44 #include <FDM/JSBsim/FGRotation.h>
45 #include <FDM/JSBsim/FGState.h>
46 #include <FDM/JSBsim/FGTranslation.h>
51 // Initialize the JSBsim flight model, dt is the time increment for
52 // each subsequent iteration through the EOM
53 int FGJSBsim::init( double dt ) {
54 FG_LOG( FG_FLIGHT, FG_INFO, "Starting and initializing JSBsim" );
56 FG_LOG( FG_FLIGHT, FG_INFO, " created FDMExec" );
58 FGPath aircraft_path( current_options.get_fg_root() );
59 aircraft_path.append( "Aircraft" );
61 FGPath engine_path( current_options.get_fg_root() );
62 engine_path.append( "Engine" );
64 FDMExec.GetAircraft()->LoadAircraft(aircraft_path.str(),
65 engine_path.str(), "X15");
66 FG_LOG( FG_FLIGHT, FG_INFO, " loaded aircraft" );
68 // FDMExec.GetState()->Reset(aircraft_path.str(), "Reset00");
70 FDMExec.GetState()->Initialize(
71 current_options.get_uBody(),
72 current_options.get_vBody(),
73 current_options.get_wBody(),
82 // FDMExec.GetState()->Setlatitude(f.get_Latitude());
83 // FDMExec.GetState()->Setlongitude(f.get_Longitude());
84 // FDMExec.GetState()->Seth(f.get_Altitude());
85 // FDMExec.GetRotation()->Setphi(f.get_Phi());
86 // FDMExec.GetRotation()->Settht(f.get_Theta());
87 // FDMExec.GetRotation()->Setpsi(f.get_Psi());
89 FG_LOG( FG_FLIGHT, FG_INFO, " loaded initial conditions" );
91 FDMExec.GetState()->Setdt( dt );
92 FG_LOG( FG_FLIGHT, FG_INFO, " set dt" );
94 FG_LOG( FG_FLIGHT, FG_INFO, "Finished initializing JSBsim" );
102 // Run an iteration of the EOM (equations of motion)
103 int FGJSBsim::update( int multiloop ) {
104 double save_alt = 0.0;
105 double time_step = (1.0 / current_options.get_model_hz()) * multiloop;
106 double start_elev = get_Altitude();
108 // lets try to avoid really screwing up the JSBsim model
109 if ( get_Altitude() < -9000 ) {
110 save_alt = get_Altitude();
114 // copy control positions into the JSBsim structure
115 FDMExec.GetFCS()->SetDa( controls.get_aileron());
116 FDMExec.GetFCS()->SetDe( controls.get_elevator()
117 + controls.get_elevator_trim() );
118 FDMExec.GetFCS()->SetDr( controls.get_rudder());
119 FDMExec.GetFCS()->SetDf( 0.0 );
120 FDMExec.GetFCS()->SetDs( 0.0 );
121 FDMExec.GetFCS()->SetThrottle( FGControls::ALL_ENGINES,
122 controls.get_throttle( 0 ) * 100.0 );
123 // FCS->SetBrake( controls.get_brake( 0 ) );
125 // Inform JSBsim of the local terrain altitude
126 // Runway_altitude = get_Runway_altitude();
128 // old -- FGInterface_2_JSBsim() not needed except for Init()
129 // translate FG to JSBsim structure
130 // FGInterface_2_JSBsim(f);
131 // printf("FG_Altitude = %.2f\n", FG_Altitude * 0.3048);
132 // printf("Altitude = %.2f\n", Altitude * 0.3048);
133 // printf("Radius to Vehicle = %.2f\n", Radius_to_vehicle * 0.3048);
135 /* FDMExec.GetState()->Setsim_time(State->Getsim_time()
136 + State->Getdt() * multiloop); */
138 for ( int i = 0; i < multiloop; i++ ) {
142 // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
143 // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
145 // translate JSBsim back to FG structure so that the
146 // autopilot (and the rest of the sim can use the updated
151 // but lets restore our original bogus altitude when we are done
152 if ( save_alt < -9000.0 ) {
153 set_Altitude( save_alt );
156 double end_elev = get_Altitude();
157 if ( time_step > 0.0 ) {
159 set_Climb_Rate( (end_elev - start_elev) / time_step );
166 // Convert from the FGInterface struct to the JSBsim generic_ struct
167 int FGJSBsim::copy_to_JSBsim() {
172 // Convert from the JSBsim generic_ struct to the FGInterface struct
173 int FGJSBsim::copy_from_JSBsim() {
176 set_Velocities_Local( FDMExec.GetPosition()->GetVn(),
177 FDMExec.GetPosition()->GetVe(),
178 FDMExec.GetPosition()->GetVd() );
179 // set_Velocities_Ground( V_north_rel_ground, V_east_rel_ground,
180 // V_down_rel_ground );
181 // set_Velocities_Local_Airmass( V_north_airmass, V_east_airmass,
183 // set_Velocities_Local_Rel_Airmass( V_north_rel_airmass,
184 // V_east_rel_airmass, V_down_rel_airmass );
185 // set_Velocities_Gust( U_gust, V_gust, W_gust );
186 // set_Velocities_Wind_Body( U_body, V_body, W_body );
188 // set_V_rel_wind( V_rel_wind );
189 // set_V_true_kts( V_true_kts );
190 // set_V_rel_ground( V_rel_ground );
191 // set_V_inertial( V_inertial );
192 // set_V_ground_speed( V_ground_speed );
193 // set_V_equiv( V_equiv );
195 /* ***FIXME*** */ set_V_equiv_kts( FDMExec.GetState()->GetVt() );
196 // set_V_calibrated( V_calibrated );
197 // set_V_calibrated_kts( V_calibrated_kts );
199 set_Omega_Body( FDMExec.GetRotation()->GetP(),
200 FDMExec.GetRotation()->GetQ(),
201 FDMExec.GetRotation()->GetR() );
202 // set_Omega_Local( P_local, Q_local, R_local );
203 // set_Omega_Total( P_total, Q_total, R_total );
205 // set_Euler_Rates( Phi_dot, Theta_dot, Psi_dot );
206 // ***FIXME*** set_Geocentric_Rates( Latitude_dot, Longitude_dot, Radius_dot );
209 double lat_geoc = FDMExec.GetState()->Getlatitude();
210 double lon = FDMExec.GetState()->Getlongitude();
211 double alt = FDMExec.GetState()->Geth();
212 double lat_geod, tmp_alt, sl_radius1, sl_radius2, tmp_lat_geoc;
213 fgGeocToGeod( lat_geoc, EQUATORIAL_RADIUS_M + alt * FEET_TO_METER,
214 &lat_geod, &tmp_alt, &sl_radius1 );
215 fgGeodToGeoc( lat_geod, alt * FEET_TO_METER, &sl_radius2, &tmp_lat_geoc );
217 FG_LOG( FG_FLIGHT, FG_DEBUG, "lon = " << lon << " lat_geod = " << lat_geod
218 << " lat_geoc = " << lat_geoc
219 << " alt = " << alt << " tmp_alt = " << tmp_alt * METER_TO_FEET
220 << " sl_radius1 = " << sl_radius1 * METER_TO_FEET
221 << " sl_radius2 = " << sl_radius2 * METER_TO_FEET
222 << " Equator = " << EQUATORIAL_RADIUS_FT );
224 set_Geocentric_Position( lat_geoc, lon,
225 sl_radius2 * METER_TO_FEET + alt );
226 set_Geodetic_Position( lat_geod, lon, alt );
227 set_Euler_Angles( FDMExec.GetRotation()->Getphi(),
228 FDMExec.GetRotation()->Gettht(),
229 FDMExec.GetRotation()->Getpsi() );
231 // Miscellaneous quantities
232 // set_T_Local_to_Body(T_local_to_body_m);
233 // set_Gravity( Gravity );
234 // set_Centrifugal_relief( Centrifugal_relief );
236 set_Alpha( FDMExec.GetTranslation()->Getalpha() );
237 set_Beta( FDMExec.GetTranslation()->Getbeta() );
238 // set_Alpha_dot( Alpha_dot );
239 // set_Beta_dot( Beta_dot );
241 // set_Cos_alpha( Cos_alpha );
242 // set_Sin_alpha( Sin_alpha );
243 // set_Cos_beta( Cos_beta );
244 // set_Sin_beta( Sin_beta );
246 // set_Cos_phi( Cos_phi );
247 // set_Sin_phi( Sin_phi );
248 // set_Cos_theta( Cos_theta );
249 // set_Sin_theta( Sin_theta );
250 // set_Cos_psi( Cos_psi );
251 // set_Sin_psi( Sin_psi );
253 // ***ATTENDTOME*** set_Gamma_vert_rad( Gamma_vert_rad );
254 // set_Gamma_horiz_rad( Gamma_horiz_rad );
256 // set_Sigma( Sigma );
257 // set_Density( Density );
258 // set_V_sound( V_sound );
259 // set_Mach_number( Mach_number );
261 // set_Static_pressure( Static_pressure );
262 // set_Total_pressure( Total_pressure );
263 // set_Impact_pressure( Impact_pressure );
264 // set_Dynamic_pressure( Dynamic_pressure );
266 // set_Static_temperature( Static_temperature );
267 // set_Total_temperature( Total_temperature );
269 /* **FIXME*** */ set_Sea_level_radius( sl_radius2 * METER_TO_FEET );
270 /* **FIXME*** */ set_Earth_position_angle( 0.0 );
272 /* ***FIXME*** */ set_Runway_altitude( 0.0 );
273 // set_Runway_latitude( Runway_latitude );
274 // set_Runway_longitude( Runway_longitude );
275 // set_Runway_heading( Runway_heading );
276 // set_Radius_to_rwy( Radius_to_rwy );
278 // set_CG_Rwy_Local( D_cg_north_of_rwy, D_cg_east_of_rwy, D_cg_above_rwy);
279 // set_CG_Rwy_Rwy( X_cg_rwy, Y_cg_rwy, H_cg_rwy );
280 // set_Pilot_Rwy_Local( D_pilot_north_of_rwy, D_pilot_east_of_rwy,
281 // D_pilot_above_rwy );
282 // set_Pilot_Rwy_Rwy( X_pilot_rwy, Y_pilot_rwy, H_pilot_rwy );
284 set_sin_lat_geocentric( lat_geoc );
285 set_cos_lat_geocentric( lat_geoc );
286 set_sin_cos_longitude( lon );
287 set_sin_cos_latitude( lat_geod );