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(),
66 current_options.get_aircraft() );
67 FG_LOG( FG_FLIGHT, FG_INFO, " loaded aircraft" <<
68 current_options.get_aircraft() );
70 // FDMExec.GetState()->Reset(aircraft_path.str(), "Reset00");
72 FDMExec.GetState()->Initialize(
73 current_options.get_uBody(),
74 current_options.get_vBody(),
75 current_options.get_wBody(),
84 // FDMExec.GetState()->Setlatitude(f.get_Latitude());
85 // FDMExec.GetState()->Setlongitude(f.get_Longitude());
86 // FDMExec.GetState()->Seth(f.get_Altitude());
87 // FDMExec.GetRotation()->Setphi(f.get_Phi());
88 // FDMExec.GetRotation()->Settht(f.get_Theta());
89 // FDMExec.GetRotation()->Setpsi(f.get_Psi());
91 FG_LOG( FG_FLIGHT, FG_INFO, " loaded initial conditions" );
93 FDMExec.GetState()->Setdt( dt );
94 FG_LOG( FG_FLIGHT, FG_INFO, " set dt" );
96 FG_LOG( FG_FLIGHT, FG_INFO, "Finished initializing JSBsim" );
104 // Run an iteration of the EOM (equations of motion)
105 int FGJSBsim::update( int multiloop ) {
106 double save_alt = 0.0;
107 double time_step = (1.0 / current_options.get_model_hz()) * multiloop;
108 double start_elev = get_Altitude();
110 // lets try to avoid really screwing up the JSBsim model
111 if ( get_Altitude() < -9000 ) {
112 save_alt = get_Altitude();
116 // copy control positions into the JSBsim structure
117 FDMExec.GetFCS()->SetDa( controls.get_aileron());
118 FDMExec.GetFCS()->SetDe( controls.get_elevator()
119 + controls.get_elevator_trim() );
120 FDMExec.GetFCS()->SetDr( controls.get_rudder());
121 FDMExec.GetFCS()->SetDf( 0.0 );
122 FDMExec.GetFCS()->SetDs( 0.0 );
123 FDMExec.GetFCS()->SetThrottle( FGControls::ALL_ENGINES,
124 controls.get_throttle( 0 ) * 100.0 );
125 // FCS->SetBrake( controls.get_brake( 0 ) );
127 // Inform JSBsim of the local terrain altitude
128 // Runway_altitude = get_Runway_altitude();
130 // old -- FGInterface_2_JSBsim() not needed except for Init()
131 // translate FG to JSBsim structure
132 // FGInterface_2_JSBsim(f);
133 // printf("FG_Altitude = %.2f\n", FG_Altitude * 0.3048);
134 // printf("Altitude = %.2f\n", Altitude * 0.3048);
135 // printf("Radius to Vehicle = %.2f\n", Radius_to_vehicle * 0.3048);
137 /* FDMExec.GetState()->Setsim_time(State->Getsim_time()
138 + State->Getdt() * multiloop); */
140 for ( int i = 0; i < multiloop; i++ ) {
144 // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
145 // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
147 // translate JSBsim back to FG structure so that the
148 // autopilot (and the rest of the sim can use the updated
153 // but lets restore our original bogus altitude when we are done
154 if ( save_alt < -9000.0 ) {
155 set_Altitude( save_alt );
158 double end_elev = get_Altitude();
159 if ( time_step > 0.0 ) {
161 set_Climb_Rate( (end_elev - start_elev) / time_step );
168 // Convert from the FGInterface struct to the JSBsim generic_ struct
169 int FGJSBsim::copy_to_JSBsim() {
174 // Convert from the JSBsim generic_ struct to the FGInterface struct
175 int FGJSBsim::copy_from_JSBsim() {
178 set_Velocities_Local( FDMExec.GetPosition()->GetVn(),
179 FDMExec.GetPosition()->GetVe(),
180 FDMExec.GetPosition()->GetVd() );
181 // set_Velocities_Ground( V_north_rel_ground, V_east_rel_ground,
182 // V_down_rel_ground );
183 // set_Velocities_Local_Airmass( V_north_airmass, V_east_airmass,
185 // set_Velocities_Local_Rel_Airmass( V_north_rel_airmass,
186 // V_east_rel_airmass, V_down_rel_airmass );
187 // set_Velocities_Gust( U_gust, V_gust, W_gust );
188 // set_Velocities_Wind_Body( U_body, V_body, W_body );
190 // set_V_rel_wind( V_rel_wind );
191 // set_V_true_kts( V_true_kts );
192 // set_V_rel_ground( V_rel_ground );
193 // set_V_inertial( V_inertial );
194 // set_V_ground_speed( V_ground_speed );
195 // set_V_equiv( V_equiv );
197 /* ***FIXME*** */ set_V_equiv_kts( FDMExec.GetState()->GetVt() );
198 // set_V_calibrated( V_calibrated );
199 // set_V_calibrated_kts( V_calibrated_kts );
201 set_Omega_Body( FDMExec.GetRotation()->GetP(),
202 FDMExec.GetRotation()->GetQ(),
203 FDMExec.GetRotation()->GetR() );
204 // set_Omega_Local( P_local, Q_local, R_local );
205 // set_Omega_Total( P_total, Q_total, R_total );
207 // set_Euler_Rates( Phi_dot, Theta_dot, Psi_dot );
208 // ***FIXME*** set_Geocentric_Rates( Latitude_dot, Longitude_dot, Radius_dot );
211 double lat_geoc = FDMExec.GetState()->Getlatitude();
212 double lon = FDMExec.GetState()->Getlongitude();
213 double alt = FDMExec.GetState()->Geth();
214 double lat_geod, tmp_alt, sl_radius1, sl_radius2, tmp_lat_geoc;
215 fgGeocToGeod( lat_geoc, EQUATORIAL_RADIUS_M + alt * FEET_TO_METER,
216 &lat_geod, &tmp_alt, &sl_radius1 );
217 fgGeodToGeoc( lat_geod, alt * FEET_TO_METER, &sl_radius2, &tmp_lat_geoc );
219 FG_LOG( FG_FLIGHT, FG_DEBUG, "lon = " << lon << " lat_geod = " << lat_geod
220 << " lat_geoc = " << lat_geoc
221 << " alt = " << alt << " tmp_alt = " << tmp_alt * METER_TO_FEET
222 << " sl_radius1 = " << sl_radius1 * METER_TO_FEET
223 << " sl_radius2 = " << sl_radius2 * METER_TO_FEET
224 << " Equator = " << EQUATORIAL_RADIUS_FT );
226 set_Geocentric_Position( lat_geoc, lon,
227 sl_radius2 * METER_TO_FEET + alt );
228 set_Geodetic_Position( lat_geod, lon, alt );
229 set_Euler_Angles( FDMExec.GetRotation()->Getphi(),
230 FDMExec.GetRotation()->Gettht(),
231 FDMExec.GetRotation()->Getpsi() );
233 // Miscellaneous quantities
234 // set_T_Local_to_Body(T_local_to_body_m);
235 // set_Gravity( Gravity );
236 // set_Centrifugal_relief( Centrifugal_relief );
238 set_Alpha( FDMExec.GetTranslation()->Getalpha() );
239 set_Beta( FDMExec.GetTranslation()->Getbeta() );
240 // set_Alpha_dot( Alpha_dot );
241 // set_Beta_dot( Beta_dot );
243 // set_Cos_alpha( Cos_alpha );
244 // set_Sin_alpha( Sin_alpha );
245 // set_Cos_beta( Cos_beta );
246 // set_Sin_beta( Sin_beta );
248 // set_Cos_phi( Cos_phi );
249 // set_Sin_phi( Sin_phi );
250 // set_Cos_theta( Cos_theta );
251 // set_Sin_theta( Sin_theta );
252 // set_Cos_psi( Cos_psi );
253 // set_Sin_psi( Sin_psi );
255 // ***ATTENDTOME*** set_Gamma_vert_rad( Gamma_vert_rad );
256 // set_Gamma_horiz_rad( Gamma_horiz_rad );
258 // set_Sigma( Sigma );
259 // set_Density( Density );
260 // set_V_sound( V_sound );
261 // set_Mach_number( Mach_number );
263 // set_Static_pressure( Static_pressure );
264 // set_Total_pressure( Total_pressure );
265 // set_Impact_pressure( Impact_pressure );
266 // set_Dynamic_pressure( Dynamic_pressure );
268 // set_Static_temperature( Static_temperature );
269 // set_Total_temperature( Total_temperature );
271 /* **FIXME*** */ set_Sea_level_radius( sl_radius2 * METER_TO_FEET );
272 /* **FIXME*** */ set_Earth_position_angle( 0.0 );
274 /* ***FIXME*** */ set_Runway_altitude( 0.0 );
275 // set_Runway_latitude( Runway_latitude );
276 // set_Runway_longitude( Runway_longitude );
277 // set_Runway_heading( Runway_heading );
278 // set_Radius_to_rwy( Radius_to_rwy );
280 // set_CG_Rwy_Local( D_cg_north_of_rwy, D_cg_east_of_rwy, D_cg_above_rwy);
281 // set_CG_Rwy_Rwy( X_cg_rwy, Y_cg_rwy, H_cg_rwy );
282 // set_Pilot_Rwy_Local( D_pilot_north_of_rwy, D_pilot_east_of_rwy,
283 // D_pilot_above_rwy );
284 // set_Pilot_Rwy_Rwy( X_pilot_rwy, Y_pilot_rwy, H_pilot_rwy );
286 set_sin_lat_geocentric( lat_geoc );
287 set_cos_lat_geocentric( lat_geoc );
288 set_sin_cos_longitude( lon );
289 set_sin_cos_latitude( lat_geod );