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.
22 // (Log is kept at end of this file)
25 #include <Include/compiler.h>
29 #include <Aircraft/aircraft.hxx>
30 #include <Controls/controls.hxx>
31 #include <Debug/logstream.hxx>
32 #include <Include/fg_constants.h>
33 #include <Main/options.hxx>
34 #include <Math/fg_geodesy.hxx>
36 #include <FDM/JSBsim/FGFDMExec.h>
37 #include <FDM/JSBsim/FGAircraft.h>
38 #include <FDM/JSBsim/FGFCS.h>
39 #include <FDM/JSBsim/FGPosition.h>
40 #include <FDM/JSBsim/FGRotation.h>
41 #include <FDM/JSBsim/FGState.h>
42 #include <FDM/JSBsim/FGTranslation.h>
47 // The default aircraft
51 // Initialize the JSBsim flight model, dt is the time increment for
52 // each subsequent iteration through the EOM
53 int fgJSBsimInit(double dt) {
54 FG_LOG( FG_FLIGHT, FG_INFO, "Starting initializing JSBsim" );
56 FG_LOG( FG_FLIGHT, FG_INFO, " created FDMExec" );
58 string aircraft_path = current_options.get_fg_root() + "/Aircraft";
59 string engine_path = current_options.get_fg_root() + "/Engine";
61 FDMExec.GetAircraft()->LoadAircraft(aircraft_path, engine_path, "X15");
62 FG_LOG( FG_FLIGHT, FG_INFO, " loaded aircraft" );
64 FDMExec.GetState()->Reset(aircraft_path, "Reset00");
65 FG_LOG( FG_FLIGHT, FG_INFO, " loaded initial conditions" );
67 FDMExec.GetState()->Setdt(dt);
68 FG_LOG( FG_FLIGHT, FG_INFO, " set dt" );
70 FG_LOG( FG_FLIGHT, FG_INFO, "Finished initializing JSBsim" );
76 // Run an iteration of the EOM (equations of motion)
77 int fgJSBsimUpdate(FGInterface& f, int multiloop) {
78 double save_alt = 0.0;
80 // lets try to avoid really screwing up the JSBsim model
81 if ( f.get_Altitude() < -9000 ) {
82 save_alt = f.get_Altitude();
83 f.set_Altitude( 0.0 );
86 // copy control positions into the JSBsim structure
87 FDMExec.GetFCS()->SetDa( controls.get_aileron() );
88 FDMExec.GetFCS()->SetDe( controls.get_elevator()
89 + controls.get_elevator_trim() );
90 FDMExec.GetFCS()->SetDr( controls.get_rudder() );
91 FDMExec.GetFCS()->SetDf( 0.0 );
92 FDMExec.GetFCS()->SetDs( 0.0 );
93 FDMExec.GetFCS()->SetThrottle( FGControls::ALL_ENGINES,
94 controls.get_throttle( 0 ) );
95 // FCS->SetBrake( controls.get_brake( 0 ) );
97 // Inform JSBsim of the local terrain altitude
98 // Runway_altitude = f.get_Runway_altitude();
100 // old -- FGInterface_2_JSBsim() not needed except for Init()
101 // translate FG to JSBsim structure
102 // FGInterface_2_JSBsim(f);
103 // printf("FG_Altitude = %.2f\n", FG_Altitude * 0.3048);
104 // printf("Altitude = %.2f\n", Altitude * 0.3048);
105 // printf("Radius to Vehicle = %.2f\n", Radius_to_vehicle * 0.3048);
107 /* FDMExec.GetState()->Setsim_time(State->Getsim_time()
108 + State->Getdt() * multiloop); */
110 for ( int i = 0; i < multiloop; i++ ) {
114 // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
115 // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
117 // translate JSBsim back to FG structure so that the
118 // autopilot (and the rest of the sim can use the updated
121 fgJSBsim_2_FGInterface(f);
123 // but lets restore our original bogus altitude when we are done
124 if ( save_alt < -9000.0 ) {
125 f.set_Altitude( save_alt );
132 // Convert from the FGInterface struct to the JSBsim generic_ struct
133 int FGInterface_2_JSBsim (FGInterface& f) {
139 // Convert from the JSBsim generic_ struct to the FGInterface struct
140 int fgJSBsim_2_FGInterface (FGInterface& f) {
143 f.set_Velocities_Local( FDMExec.GetPosition()->GetVn(),
144 FDMExec.GetPosition()->GetVe(),
145 FDMExec.GetPosition()->GetVd() );
146 // f.set_Velocities_Ground( V_north_rel_ground, V_east_rel_ground,
147 // V_down_rel_ground );
148 // f.set_Velocities_Local_Airmass( V_north_airmass, V_east_airmass,
150 // f.set_Velocities_Local_Rel_Airmass( V_north_rel_airmass,
151 // V_east_rel_airmass, V_down_rel_airmass );
152 // f.set_Velocities_Gust( U_gust, V_gust, W_gust );
153 // f.set_Velocities_Wind_Body( U_body, V_body, W_body );
155 // f.set_V_rel_wind( V_rel_wind );
156 // f.set_V_true_kts( V_true_kts );
157 // f.set_V_rel_ground( V_rel_ground );
158 // f.set_V_inertial( V_inertial );
159 // f.set_V_ground_speed( V_ground_speed );
160 // f.set_V_equiv( V_equiv );
162 /* ***FIXME*** */ f.set_V_equiv_kts( FDMExec.GetState()->GetVt() );
163 // f.set_V_calibrated( V_calibrated );
164 // f.set_V_calibrated_kts( V_calibrated_kts );
166 f.set_Omega_Body( FDMExec.GetRotation()->GetP(),
167 FDMExec.GetRotation()->GetQ(),
168 FDMExec.GetRotation()->GetR() );
169 // f.set_Omega_Local( P_local, Q_local, R_local );
170 // f.set_Omega_Total( P_total, Q_total, R_total );
172 // f.set_Euler_Rates( Phi_dot, Theta_dot, Psi_dot );
173 // ***FIXME*** f.set_Geocentric_Rates( Latitude_dot, Longitude_dot, Radius_dot );
176 double lat_geoc = FDMExec.GetState()->Getlatitude();
177 double lon = FDMExec.GetState()->Getlongitude();
178 double alt = FDMExec.GetState()->Geth();
179 double lat_geod, tmp_alt, sl_radius1, sl_radius2, tmp_lat_geoc;
180 fgGeocToGeod( lat_geoc, EQUATORIAL_RADIUS_M + alt * FEET_TO_METER,
181 &lat_geod, &tmp_alt, &sl_radius1 );
182 fgGeodToGeoc( lat_geod, alt * FEET_TO_METER, &sl_radius2, &tmp_lat_geoc );
184 FG_LOG( FG_FLIGHT, FG_DEBUG, "lon = " << lon << " lat_geod = " << lat_geod
185 << " lat_geoc = " << lat_geoc
186 << " alt = " << alt << " tmp_alt = " << tmp_alt * METER_TO_FEET
187 << " sl_radius1 = " << sl_radius1 * METER_TO_FEET
188 << " sl_radius2 = " << sl_radius2 * METER_TO_FEET
189 << " Equator = " << EQUATORIAL_RADIUS_FT );
191 f.set_Geocentric_Position( lat_geoc, lon,
192 sl_radius2 * METER_TO_FEET + alt );
193 f.set_Geodetic_Position( lat_geod, lon, alt );
194 f.set_Euler_Angles( FDMExec.GetRotation()->Getphi(),
195 FDMExec.GetRotation()->Gettht(),
196 FDMExec.GetRotation()->Getpsi() );
198 // Miscellaneous quantities
199 // f.set_T_Local_to_Body(T_local_to_body_m);
200 // f.set_Gravity( Gravity );
201 // f.set_Centrifugal_relief( Centrifugal_relief );
203 f.set_Alpha( FDMExec.GetTranslation()->Getalpha() );
204 f.set_Beta( FDMExec.GetTranslation()->Getbeta() );
205 // f.set_Alpha_dot( Alpha_dot );
206 // f.set_Beta_dot( Beta_dot );
208 // f.set_Cos_alpha( Cos_alpha );
209 // f.set_Sin_alpha( Sin_alpha );
210 // f.set_Cos_beta( Cos_beta );
211 // f.set_Sin_beta( Sin_beta );
213 // f.set_Cos_phi( Cos_phi );
214 // f.set_Sin_phi( Sin_phi );
215 // f.set_Cos_theta( Cos_theta );
216 // f.set_Sin_theta( Sin_theta );
217 // f.set_Cos_psi( Cos_psi );
218 // f.set_Sin_psi( Sin_psi );
220 // ***ATTENDTOME*** f.set_Gamma_vert_rad( Gamma_vert_rad );
221 // f.set_Gamma_horiz_rad( Gamma_horiz_rad );
223 // f.set_Sigma( Sigma );
224 // f.set_Density( Density );
225 // f.set_V_sound( V_sound );
226 // f.set_Mach_number( Mach_number );
228 // f.set_Static_pressure( Static_pressure );
229 // f.set_Total_pressure( Total_pressure );
230 // f.set_Impact_pressure( Impact_pressure );
231 // f.set_Dynamic_pressure( Dynamic_pressure );
233 // f.set_Static_temperature( Static_temperature );
234 // f.set_Total_temperature( Total_temperature );
236 /* **FIXME*** */ f.set_Sea_level_radius( sl_radius2 * METER_TO_FEET );
237 /* **FIXME*** */ f.set_Earth_position_angle( 0.0 );
239 /* ***FIXME*** */ f.set_Runway_altitude( 0.0 );
240 // f.set_Runway_latitude( Runway_latitude );
241 // f.set_Runway_longitude( Runway_longitude );
242 // f.set_Runway_heading( Runway_heading );
243 // f.set_Radius_to_rwy( Radius_to_rwy );
245 // f.set_CG_Rwy_Local( D_cg_north_of_rwy, D_cg_east_of_rwy, D_cg_above_rwy);
246 // f.set_CG_Rwy_Rwy( X_cg_rwy, Y_cg_rwy, H_cg_rwy );
247 // f.set_Pilot_Rwy_Local( D_pilot_north_of_rwy, D_pilot_east_of_rwy,
248 // D_pilot_above_rwy );
249 // f.set_Pilot_Rwy_Rwy( X_pilot_rwy, Y_pilot_rwy, H_pilot_rwy );
251 f.set_sin_lat_geocentric( lat_geoc );
252 f.set_cos_lat_geocentric( lat_geoc );
253 f.set_sin_cos_longitude( lon );
254 f.set_sin_cos_latitude( lat_geod );
261 // Revision 1.4 1999/04/03 04:20:01 curt
262 // Optimizations (tm) by Norman Vine.
264 // Revision 1.3 1999/02/26 22:09:10 curt
265 // Added initial support for native SGI compilers.
266 // Integrated Jon's next version of JSBsim.
268 // Revision 1.2 1999/02/11 21:09:40 curt
269 // Interface with Jon's submitted JSBsim changes.
271 // Revision 1.1 1999/02/05 21:29:38 curt
272 // Incorporating Jon S. Berndt's flight model code.