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)
26 #include <FDM/JSBsim/FGFDMExec.h>
31 #include <Aircraft/aircraft.hxx>
32 #include <Controls/controls.hxx>
33 #include <Debug/logstream.hxx>
34 #include <Include/fg_constants.h>
35 #include <Math/fg_geodesy.hxx>
38 // Initialize the JSBsim flight model, dt is the time increment for
39 // each subsequent iteration through the EOM
40 int fgJSBsimInit(double dt) {
41 FG_LOG( FG_FLIGHT, FG_INFO, "Starting initializing JSBsim" );
43 FDMExec = new FGFDMExec();
44 FG_LOG( FG_FLIGHT, FG_INFO, " created FDMExec" );
46 Aircraft->LoadAircraft("X15");
47 FG_LOG( FG_FLIGHT, FG_INFO, " loaded aircraft" );
49 State->Reset("reset00");
50 FG_LOG( FG_FLIGHT, FG_INFO, " loaded initial conditions" );
53 FG_LOG( FG_FLIGHT, FG_INFO, " set dt" );
55 FG_LOG( FG_FLIGHT, FG_INFO, "Finished initializing JSBsim" );
61 // Run an iteration of the EOM (equations of motion)
62 int fgJSBsimUpdate(FGInterface& f, int multiloop) {
63 double save_alt = 0.0;
65 // lets try to avoid really screwing up the JSBsim model
66 if ( f.get_Altitude() < -9000 ) {
67 save_alt = f.get_Altitude();
68 f.set_Altitude( 0.0 );
71 // copy control positions into the JSBsim structure
72 FCS->SetDa( controls.get_aileron() );
73 FCS->SetDe( controls.get_elevator() + controls.get_elevator_trim() );
74 FCS->SetDr( controls.get_rudder() );
77 FCS->SetThrottle( controls.get_throttle( 0 ) );
78 // FCS->SetBrake( controls.get_brake( 0 ) );
80 // Inform JSBsim of the local terrain altitude
81 // Runway_altitude = f.get_Runway_altitude();
83 // old -- FGInterface_2_JSBsim() not needed except for Init()
84 // translate FG to JSBsim structure
85 // FGInterface_2_JSBsim(f);
86 // printf("FG_Altitude = %.2f\n", FG_Altitude * 0.3048);
87 // printf("Altitude = %.2f\n", Altitude * 0.3048);
88 // printf("Radius to Vehicle = %.2f\n", Radius_to_vehicle * 0.3048);
90 State->Setsim_time(State->Getsim_time() + State->Getdt() * multiloop);
93 // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
94 // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
96 // translate JSBsim back to FG structure so that the
97 // autopilot (and the rest of the sim can use the updated
100 fgJSBsim_2_FGInterface(f);
102 // but lets restore our original bogus altitude when we are done
103 if ( save_alt < -9000.0 ) {
104 f.set_Altitude( save_alt );
111 // Convert from the FGInterface struct to the JSBsim generic_ struct
112 int FGInterface_2_JSBsim (FGInterface& f) {
118 // Convert from the JSBsim generic_ struct to the FGInterface struct
119 int fgJSBsim_2_FGInterface (FGInterface& f) {
122 f.set_Velocities_Local( State->GetVn(), State->GetVe(), State->GetVd() );
123 // f.set_Velocities_Ground( V_north_rel_ground, V_east_rel_ground,
124 // V_down_rel_ground );
125 // f.set_Velocities_Local_Airmass( V_north_airmass, V_east_airmass,
127 // f.set_Velocities_Local_Rel_Airmass( V_north_rel_airmass,
128 // V_east_rel_airmass, V_down_rel_airmass );
129 // f.set_Velocities_Gust( U_gust, V_gust, W_gust );
130 // f.set_Velocities_Wind_Body( U_body, V_body, W_body );
132 // f.set_V_rel_wind( V_rel_wind );
133 // f.set_V_true_kts( V_true_kts );
134 // f.set_V_rel_ground( V_rel_ground );
135 // f.set_V_inertial( V_inertial );
136 // f.set_V_ground_speed( V_ground_speed );
137 // f.set_V_equiv( V_equiv );
139 /* ***FIXME*** */ f.set_V_equiv_kts( State->GetVt() );
140 // f.set_V_calibrated( V_calibrated );
141 // f.set_V_calibrated_kts( V_calibrated_kts );
143 f.set_Omega_Body( State->GetP(), State->GetQ(), State->GetR() );
144 // f.set_Omega_Local( P_local, Q_local, R_local );
145 // f.set_Omega_Total( P_total, Q_total, R_total );
147 // f.set_Euler_Rates( Phi_dot, Theta_dot, Psi_dot );
148 // ***FIXME*** f.set_Geocentric_Rates( Latitude_dot, Longitude_dot, Radius_dot );
151 double lat = State->Getlatitude();
152 double lon = State->Getlongitude();
153 double alt = State->Geth();
154 double lat_geoc, sl_radius;
155 fgGeodToGeoc( lat, alt * FEET_TO_METER, &sl_radius, &lat_geoc );
157 FG_LOG( FG_FLIGHT, FG_DEBUG, "lon = " << lon << " lat = " << lat
158 << " lat_geoc = " << lat_geoc
160 << " sl_radius = " << sl_radius * METER_TO_FEET);
162 f.set_Geocentric_Position( lat_geoc, lon, sl_radius * METER_TO_FEET + alt );
163 f.set_Geodetic_Position( lat, lon, alt );
164 f.set_Euler_Angles( State->Getphi(), State->Gettht(), State->Getpsi() );
166 // Miscellaneous quantities
167 // f.set_T_Local_to_Body(T_local_to_body_m);
168 // f.set_Gravity( Gravity );
169 // f.set_Centrifugal_relief( Centrifugal_relief );
171 f.set_Alpha( State->Getalpha() );
172 f.set_Beta( State->Getbeta() );
173 // f.set_Alpha_dot( Alpha_dot );
174 // f.set_Beta_dot( Beta_dot );
176 // f.set_Cos_alpha( Cos_alpha );
177 // f.set_Sin_alpha( Sin_alpha );
178 // f.set_Cos_beta( Cos_beta );
179 // f.set_Sin_beta( Sin_beta );
181 // f.set_Cos_phi( Cos_phi );
182 // f.set_Sin_phi( Sin_phi );
183 // f.set_Cos_theta( Cos_theta );
184 // f.set_Sin_theta( Sin_theta );
185 // f.set_Cos_psi( Cos_psi );
186 // f.set_Sin_psi( Sin_psi );
188 // ***ATTENDTOME*** f.set_Gamma_vert_rad( Gamma_vert_rad );
189 // f.set_Gamma_horiz_rad( Gamma_horiz_rad );
191 // f.set_Sigma( Sigma );
192 // f.set_Density( Density );
193 // f.set_V_sound( V_sound );
194 // f.set_Mach_number( Mach_number );
196 // f.set_Static_pressure( Static_pressure );
197 // f.set_Total_pressure( Total_pressure );
198 // f.set_Impact_pressure( Impact_pressure );
199 // f.set_Dynamic_pressure( Dynamic_pressure );
201 // f.set_Static_temperature( Static_temperature );
202 // f.set_Total_temperature( Total_temperature );
204 /* **FIXME*** */ f.set_Sea_level_radius( sl_radius * METER_TO_FEET );
205 /* **FIXME*** */ f.set_Earth_position_angle( 0.0 );
207 /* ***FIXME*** */ f.set_Runway_altitude( 0.0 );
208 // f.set_Runway_latitude( Runway_latitude );
209 // f.set_Runway_longitude( Runway_longitude );
210 // f.set_Runway_heading( Runway_heading );
211 // f.set_Radius_to_rwy( Radius_to_rwy );
213 // f.set_CG_Rwy_Local( D_cg_north_of_rwy, D_cg_east_of_rwy, D_cg_above_rwy);
214 // f.set_CG_Rwy_Rwy( X_cg_rwy, Y_cg_rwy, H_cg_rwy );
215 // f.set_Pilot_Rwy_Local( D_pilot_north_of_rwy, D_pilot_east_of_rwy,
216 // D_pilot_above_rwy );
217 // f.set_Pilot_Rwy_Rwy( X_pilot_rwy, Y_pilot_rwy, H_pilot_rwy );
225 // Revision 1.1 1999/02/05 21:29:38 curt
226 // Incorporating Jon S. Berndt's flight model code.