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>
28 #include <Aircraft/aircraft.hxx>
29 #include <Controls/controls.hxx>
30 #include <Debug/logstream.hxx>
31 #include <Include/fg_constants.h>
32 #include <Main/options.hxx>
33 #include <Math/fg_geodesy.hxx>
35 #include <FDM/JSBsim/FGFDMExec.h>
36 #include <FDM/JSBsim/FGAircraft.h>
37 #include <FDM/JSBsim/FGFCS.h>
38 #include <FDM/JSBsim/FGPosition.h>
39 #include <FDM/JSBsim/FGRotation.h>
40 #include <FDM/JSBsim/FGState.h>
41 #include <FDM/JSBsim/FGTranslation.h>
46 // The default aircraft
50 // Initialize the JSBsim flight model, dt is the time increment for
51 // each subsequent iteration through the EOM
52 int fgJSBsimInit(double dt) {
53 FG_LOG( FG_FLIGHT, FG_INFO, "Starting initializing JSBsim" );
55 FG_LOG( FG_FLIGHT, FG_INFO, " created FDMExec" );
57 string aircraft_path = current_options.get_fg_root() + "/Aircraft";
58 string engine_path = current_options.get_fg_root() + "/Engine";
60 FDMExec.GetAircraft()->LoadAircraft(aircraft_path, engine_path, "X15");
61 FG_LOG( FG_FLIGHT, FG_INFO, " loaded aircraft" );
63 FDMExec.GetState()->Reset(aircraft_path, "Reset00");
64 FG_LOG( FG_FLIGHT, FG_INFO, " loaded initial conditions" );
66 FDMExec.GetState()->Setdt(dt);
67 FG_LOG( FG_FLIGHT, FG_INFO, " set dt" );
69 FG_LOG( FG_FLIGHT, FG_INFO, "Finished initializing JSBsim" );
75 // Run an iteration of the EOM (equations of motion)
76 int fgJSBsimUpdate(FGInterface& f, int multiloop) {
77 double save_alt = 0.0;
79 // lets try to avoid really screwing up the JSBsim model
80 if ( f.get_Altitude() < -9000 ) {
81 save_alt = f.get_Altitude();
82 f.set_Altitude( 0.0 );
85 // copy control positions into the JSBsim structure
86 FDMExec.GetFCS()->SetDa( controls.get_aileron() );
87 FDMExec.GetFCS()->SetDe( controls.get_elevator()
88 + controls.get_elevator_trim() );
89 FDMExec.GetFCS()->SetDr( controls.get_rudder() );
90 FDMExec.GetFCS()->SetDf( 0.0 );
91 FDMExec.GetFCS()->SetDs( 0.0 );
92 FDMExec.GetFCS()->SetThrottle( FGControls::ALL_ENGINES,
93 controls.get_throttle( 0 ) );
94 // FCS->SetBrake( controls.get_brake( 0 ) );
96 // Inform JSBsim of the local terrain altitude
97 // Runway_altitude = f.get_Runway_altitude();
99 // old -- FGInterface_2_JSBsim() not needed except for Init()
100 // translate FG to JSBsim structure
101 // FGInterface_2_JSBsim(f);
102 // printf("FG_Altitude = %.2f\n", FG_Altitude * 0.3048);
103 // printf("Altitude = %.2f\n", Altitude * 0.3048);
104 // printf("Radius to Vehicle = %.2f\n", Radius_to_vehicle * 0.3048);
106 /* FDMExec.GetState()->Setsim_time(State->Getsim_time()
107 + State->Getdt() * multiloop); */
109 for ( int i = 0; i < multiloop; i++ ) {
113 // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
114 // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
116 // translate JSBsim back to FG structure so that the
117 // autopilot (and the rest of the sim can use the updated
120 fgJSBsim_2_FGInterface(f);
122 // but lets restore our original bogus altitude when we are done
123 if ( save_alt < -9000.0 ) {
124 f.set_Altitude( save_alt );
131 // Convert from the FGInterface struct to the JSBsim generic_ struct
132 int FGInterface_2_JSBsim (FGInterface& f) {
138 // Convert from the JSBsim generic_ struct to the FGInterface struct
139 int fgJSBsim_2_FGInterface (FGInterface& f) {
142 f.set_Velocities_Local( FDMExec.GetPosition()->GetVn(),
143 FDMExec.GetPosition()->GetVe(),
144 FDMExec.GetPosition()->GetVd() );
145 // f.set_Velocities_Ground( V_north_rel_ground, V_east_rel_ground,
146 // V_down_rel_ground );
147 // f.set_Velocities_Local_Airmass( V_north_airmass, V_east_airmass,
149 // f.set_Velocities_Local_Rel_Airmass( V_north_rel_airmass,
150 // V_east_rel_airmass, V_down_rel_airmass );
151 // f.set_Velocities_Gust( U_gust, V_gust, W_gust );
152 // f.set_Velocities_Wind_Body( U_body, V_body, W_body );
154 // f.set_V_rel_wind( V_rel_wind );
155 // f.set_V_true_kts( V_true_kts );
156 // f.set_V_rel_ground( V_rel_ground );
157 // f.set_V_inertial( V_inertial );
158 // f.set_V_ground_speed( V_ground_speed );
159 // f.set_V_equiv( V_equiv );
161 /* ***FIXME*** */ f.set_V_equiv_kts( FDMExec.GetState()->GetVt() );
162 // f.set_V_calibrated( V_calibrated );
163 // f.set_V_calibrated_kts( V_calibrated_kts );
165 f.set_Omega_Body( FDMExec.GetRotation()->GetP(),
166 FDMExec.GetRotation()->GetQ(),
167 FDMExec.GetRotation()->GetR() );
168 // f.set_Omega_Local( P_local, Q_local, R_local );
169 // f.set_Omega_Total( P_total, Q_total, R_total );
171 // f.set_Euler_Rates( Phi_dot, Theta_dot, Psi_dot );
172 // ***FIXME*** f.set_Geocentric_Rates( Latitude_dot, Longitude_dot, Radius_dot );
175 double lat_geoc = FDMExec.GetState()->Getlatitude();
176 double lon = FDMExec.GetState()->Getlongitude();
177 double alt = FDMExec.GetState()->Geth();
178 double lat_geod, tmp_alt, sl_radius1, sl_radius2, tmp_lat_geoc;
179 fgGeocToGeod( lat_geoc, EQUATORIAL_RADIUS_M + alt * FEET_TO_METER,
180 &lat_geod, &tmp_alt, &sl_radius1 );
181 fgGeodToGeoc( lat_geod, alt * FEET_TO_METER, &sl_radius2, &tmp_lat_geoc );
183 FG_LOG( FG_FLIGHT, FG_DEBUG, "lon = " << lon << " lat_geod = " << lat_geod
184 << " lat_geoc = " << lat_geoc
185 << " alt = " << alt << " tmp_alt = " << tmp_alt * METER_TO_FEET
186 << " sl_radius1 = " << sl_radius1 * METER_TO_FEET
187 << " sl_radius2 = " << sl_radius2 * METER_TO_FEET
188 << " Equator = " << EQUATORIAL_RADIUS_FT );
190 f.set_Geocentric_Position( lat_geoc, lon,
191 sl_radius2 * METER_TO_FEET + alt );
192 f.set_Geodetic_Position( lat_geod, lon, alt );
193 f.set_Euler_Angles( FDMExec.GetRotation()->Getphi(),
194 FDMExec.GetRotation()->Gettht(),
195 FDMExec.GetRotation()->Getpsi() );
197 // Miscellaneous quantities
198 // f.set_T_Local_to_Body(T_local_to_body_m);
199 // f.set_Gravity( Gravity );
200 // f.set_Centrifugal_relief( Centrifugal_relief );
202 f.set_Alpha( FDMExec.GetTranslation()->Getalpha() );
203 f.set_Beta( FDMExec.GetTranslation()->Getbeta() );
204 // f.set_Alpha_dot( Alpha_dot );
205 // f.set_Beta_dot( Beta_dot );
207 // f.set_Cos_alpha( Cos_alpha );
208 // f.set_Sin_alpha( Sin_alpha );
209 // f.set_Cos_beta( Cos_beta );
210 // f.set_Sin_beta( Sin_beta );
212 // f.set_Cos_phi( Cos_phi );
213 // f.set_Sin_phi( Sin_phi );
214 // f.set_Cos_theta( Cos_theta );
215 // f.set_Sin_theta( Sin_theta );
216 // f.set_Cos_psi( Cos_psi );
217 // f.set_Sin_psi( Sin_psi );
219 // ***ATTENDTOME*** f.set_Gamma_vert_rad( Gamma_vert_rad );
220 // f.set_Gamma_horiz_rad( Gamma_horiz_rad );
222 // f.set_Sigma( Sigma );
223 // f.set_Density( Density );
224 // f.set_V_sound( V_sound );
225 // f.set_Mach_number( Mach_number );
227 // f.set_Static_pressure( Static_pressure );
228 // f.set_Total_pressure( Total_pressure );
229 // f.set_Impact_pressure( Impact_pressure );
230 // f.set_Dynamic_pressure( Dynamic_pressure );
232 // f.set_Static_temperature( Static_temperature );
233 // f.set_Total_temperature( Total_temperature );
235 /* **FIXME*** */ f.set_Sea_level_radius( sl_radius2 * METER_TO_FEET );
236 /* **FIXME*** */ f.set_Earth_position_angle( 0.0 );
238 /* ***FIXME*** */ f.set_Runway_altitude( 0.0 );
239 // f.set_Runway_latitude( Runway_latitude );
240 // f.set_Runway_longitude( Runway_longitude );
241 // f.set_Runway_heading( Runway_heading );
242 // f.set_Radius_to_rwy( Radius_to_rwy );
244 // f.set_CG_Rwy_Local( D_cg_north_of_rwy, D_cg_east_of_rwy, D_cg_above_rwy);
245 // f.set_CG_Rwy_Rwy( X_cg_rwy, Y_cg_rwy, H_cg_rwy );
246 // f.set_Pilot_Rwy_Local( D_pilot_north_of_rwy, D_pilot_east_of_rwy,
247 // D_pilot_above_rwy );
248 // f.set_Pilot_Rwy_Rwy( X_pilot_rwy, Y_pilot_rwy, H_pilot_rwy );
250 f.set_sin_lat_geocentric( lat_geoc );
251 f.set_cos_lat_geocentric( lat_geoc );
252 f.set_sin_cos_longitude( lon );
253 f.set_sin_cos_latitude( lat_geod );