]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim.cxx
52d860fe2988eb616344d5fa3485a0093b9e0646
[flightgear.git] / src / FDM / JSBSim.cxx
1 // JSBsim.cxx -- interface to the JSBsim flight model
2 //
3 // Written by Curtis Olson, started February 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson  - curt@flightgear.org
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #include <Include/compiler.h>
25
26 #ifdef FG_MATH_EXCEPTION_CLASH
27 #  include <math.h>
28 #endif
29
30 #include STL_STRING
31
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>
39
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>
47 #include <FDM/JSBsim/FGAuxiliary.h>
48 #include <FDM/JSBsim/FGDefs.h>
49
50
51 #include "JSBsim.hxx"
52
53
54 // Initialize the JSBsim flight model, dt is the time increment for
55 // each subsequent iteration through the EOM
56 int FGJSBsim::init( double dt ) {
57     FG_LOG( FG_FLIGHT, FG_INFO, "Starting and initializing JSBsim" );
58
59     FG_LOG( FG_FLIGHT, FG_INFO, "  created FDMExec" );
60
61     FGPath aircraft_path( current_options.get_fg_root() );
62     aircraft_path.append( "Aircraft" );
63
64     FGPath engine_path( current_options.get_fg_root() );
65     engine_path.append( "Engine" );
66
67     FDMExec.GetAircraft()->LoadAircraft( aircraft_path.str(), 
68                                          engine_path.str(), 
69                                          current_options.get_aircraft() );
70     FG_LOG( FG_FLIGHT, FG_INFO, "  loaded aircraft" << 
71             current_options.get_aircraft() );
72
73     FG_LOG( FG_FLIGHT, FG_INFO, "Initializing JSBsim with:" );
74     FG_LOG( FG_FLIGHT, FG_INFO, "    U: " << current_options.get_uBody() );
75     FG_LOG( FG_FLIGHT, FG_INFO, "    V: " <<current_options.get_vBody() );
76     FG_LOG( FG_FLIGHT, FG_INFO, "    W: " <<current_options.get_wBody() );
77     FG_LOG( FG_FLIGHT, FG_INFO, "  phi: " <<get_Phi() );
78     FG_LOG( FG_FLIGHT, FG_INFO, "theta: " <<  get_Theta() );
79     FG_LOG( FG_FLIGHT, FG_INFO, "  psi: " <<  get_Psi() );
80     FG_LOG( FG_FLIGHT, FG_INFO, "  lat: " <<  get_Latitude() );
81     FG_LOG( FG_FLIGHT, FG_INFO, "  lon: " <<  get_Longitude() );
82     FG_LOG( FG_FLIGHT, FG_INFO, "  alt: " <<  get_Altitude() );
83
84     FDMExec.GetState()->Initialize(
85       current_options.get_uBody(),
86       current_options.get_vBody(),
87       current_options.get_wBody(),
88       get_Phi() * DEGTORAD,
89       get_Theta() * DEGTORAD,
90       get_Psi() * DEGTORAD,
91       get_Latitude(),
92       get_Longitude(),
93       get_Altitude()
94     );
95
96     FG_LOG( FG_FLIGHT, FG_INFO, "  loaded initial conditions" );
97
98     FDMExec.GetState()->Setdt( dt );
99     FG_LOG( FG_FLIGHT, FG_INFO, "  set dt" );
100
101     FG_LOG( FG_FLIGHT, FG_INFO, "Finished initializing JSBsim" );
102
103     copy_from_JSBsim();
104
105     return 1;
106 }
107
108
109 // Run an iteration of the EOM (equations of motion)
110 int FGJSBsim::update( int multiloop ) {
111     double save_alt = 0.0;
112     double time_step = (1.0 / current_options.get_model_hz()) * multiloop;
113     double start_elev = get_Altitude();
114
115     // lets try to avoid really screwing up the JSBsim model
116     if ( get_Altitude() < -9000 ) {
117         save_alt = get_Altitude();
118         set_Altitude( 0.0 );
119     }
120
121     // copy control positions into the JSBsim structure
122     FDMExec.GetFCS()->SetDa( controls.get_aileron());
123     FDMExec.GetFCS()->SetDe( controls.get_elevator() 
124                              + controls.get_elevator_trim() );
125     FDMExec.GetFCS()->SetDr( controls.get_rudder());
126     FDMExec.GetFCS()->SetDf( 0.0 );
127     FDMExec.GetFCS()->SetDs( 0.0 );
128     FDMExec.GetFCS()->SetThrottle( FGControls::ALL_ENGINES, 
129                                    controls.get_throttle( 0 ) * 100.0 );
130     // FCS->SetBrake( controls.get_brake( 0 ) );
131
132     // Inform JSBsim of the local terrain altitude
133     // Runway_altitude =   get_Runway_altitude();
134
135     // old -- FGInterface_2_JSBsim() not needed except for Init()
136     // translate FG to JSBsim structure
137     // FGInterface_2_JSBsim(f);
138     // printf("FG_Altitude = %.2f\n", FG_Altitude * 0.3048);
139     // printf("Altitude = %.2f\n", Altitude * 0.3048);
140     // printf("Radius to Vehicle = %.2f\n", Radius_to_vehicle * 0.3048);
141
142     /* FDMExec.GetState()->Setsim_time(State->Getsim_time() 
143                                     + State->Getdt() * multiloop); */
144
145     for ( int i = 0; i < multiloop; i++ ) {
146         FDMExec.Run();
147     }
148
149     // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
150     // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
151     
152     // translate JSBsim back to FG structure so that the
153     // autopilot (and the rest of the sim can use the updated
154     // values
155
156     copy_from_JSBsim();
157
158     // but lets restore our original bogus altitude when we are done
159     if ( save_alt < -9000.0 ) {
160         set_Altitude( save_alt );
161     }
162
163     double end_elev = get_Altitude();
164     if ( time_step > 0.0 ) {
165         // feet per second
166         set_Climb_Rate( (end_elev - start_elev) / time_step );
167     }
168
169     return 1;
170 }
171
172
173 // Convert from the FGInterface struct to the JSBsim generic_ struct
174 int FGJSBsim::copy_to_JSBsim() {
175     return 1;
176 }
177
178
179 // Convert from the JSBsim generic_ struct to the FGInterface struct
180 int FGJSBsim::copy_from_JSBsim() {
181
182     // Velocities
183     set_Velocities_Local( FDMExec.GetPosition()->GetVn(), 
184                           FDMExec.GetPosition()->GetVe(), 
185                           FDMExec.GetPosition()->GetVd() );
186     // set_Velocities_Ground( V_north_rel_ground, V_east_rel_ground, 
187     //               V_down_rel_ground );
188     // set_Velocities_Local_Airmass( V_north_airmass, V_east_airmass,
189     //                      V_down_airmass );
190     // set_Velocities_Local_Rel_Airmass( V_north_rel_airmass, 
191     //                          V_east_rel_airmass, V_down_rel_airmass );
192     // set_Velocities_Gust( U_gust, V_gust, W_gust );
193     // set_Velocities_Wind_Body( U_body, V_body, W_body );
194
195     // set_V_rel_wind( V_rel_wind );
196     // set_V_true_kts( V_true_kts );
197     // set_V_rel_ground( V_rel_ground );
198     // set_V_inertial( V_inertial );
199     // set_V_ground_speed( V_ground_speed );
200     // set_V_equiv( V_equiv );
201
202     set_V_equiv_kts( FDMExec.GetAuxiliary()->GetVequivalentKTS() );
203     //set_V_calibrated( FDMExec.GetAuxiliary()->GetVcalibratedFPS() );
204     set_V_calibrated_kts( FDMExec.GetAuxiliary()->GetVcalibratedKTS() );
205
206     set_Omega_Body( FDMExec.GetRotation()->GetP(), 
207                     FDMExec.GetRotation()->GetQ(), 
208                     FDMExec.GetRotation()->GetR() );
209     // set_Omega_Local( P_local, Q_local, R_local );
210     // set_Omega_Total( P_total, Q_total, R_total );
211     
212     // set_Euler_Rates( Phi_dot, Theta_dot, Psi_dot );
213     // ***FIXME*** set_Geocentric_Rates( Latitude_dot, Longitude_dot, Radius_dot );
214         
215     set_Mach_number( FDMExec.GetState()->GetMach());
216
217     // Positions
218     double lat_geoc = FDMExec.GetState()->Getlatitude();
219     double lon = FDMExec.GetState()->Getlongitude();
220     double alt = FDMExec.GetState()->Geth();
221     double lat_geod, tmp_alt, sl_radius1, sl_radius2, tmp_lat_geoc;
222     fgGeocToGeod( lat_geoc, EQUATORIAL_RADIUS_M + alt * FEET_TO_METER,
223                   &lat_geod, &tmp_alt, &sl_radius1 );
224     fgGeodToGeoc( lat_geod, alt * FEET_TO_METER, &sl_radius2, &tmp_lat_geoc );
225
226     FG_LOG( FG_FLIGHT, FG_DEBUG, "lon = " << lon << " lat_geod = " << lat_geod
227             << " lat_geoc = " << lat_geoc
228             << " alt = " << alt << " tmp_alt = " << tmp_alt * METER_TO_FEET
229             << " sl_radius1 = " << sl_radius1 * METER_TO_FEET
230             << " sl_radius2 = " << sl_radius2 * METER_TO_FEET
231             << " Equator = " << EQUATORIAL_RADIUS_FT );
232             
233     set_Geocentric_Position( lat_geoc, lon, 
234                                sl_radius2 * METER_TO_FEET + alt );
235     set_Geodetic_Position( lat_geod, lon, alt );
236     set_Euler_Angles( FDMExec.GetRotation()->Getphi(), 
237                       FDMExec.GetRotation()->Gettht(),
238                       FDMExec.GetRotation()->Getpsi() );
239
240     // Miscellaneous quantities
241     // set_T_Local_to_Body(T_local_to_body_m);
242     // set_Gravity( Gravity );
243     // set_Centrifugal_relief( Centrifugal_relief );
244
245     set_Alpha( FDMExec.GetTranslation()->Getalpha() );
246     set_Beta( FDMExec.GetTranslation()->Getbeta() );
247     // set_Alpha_dot( Alpha_dot );
248     // set_Beta_dot( Beta_dot );
249
250     // set_Cos_alpha( Cos_alpha );
251     // set_Sin_alpha( Sin_alpha );
252     // set_Cos_beta( Cos_beta );
253     // set_Sin_beta( Sin_beta );
254
255     // set_Cos_phi( Cos_phi );
256     // set_Sin_phi( Sin_phi );
257     // set_Cos_theta( Cos_theta );
258     // set_Sin_theta( Sin_theta );
259     // set_Cos_psi( Cos_psi );
260     // set_Sin_psi( Sin_psi );
261
262     // ***ATTENDTOME*** set_Gamma_vert_rad( Gamma_vert_rad );
263     // set_Gamma_horiz_rad( Gamma_horiz_rad );
264
265     // set_Sigma( Sigma );
266     // set_Density( Density );
267     // set_V_sound( V_sound );
268     // set_Mach_number( Mach_number );
269
270     // set_Static_pressure( Static_pressure );
271     // set_Total_pressure( Total_pressure );
272     // set_Impact_pressure( Impact_pressure );
273     // set_Dynamic_pressure( Dynamic_pressure );
274
275     // set_Static_temperature( Static_temperature );
276     // set_Total_temperature( Total_temperature );
277
278     /* **FIXME*** */ set_Sea_level_radius( sl_radius2 * METER_TO_FEET );
279     /* **FIXME*** */ set_Earth_position_angle( 0.0 );
280
281     /* ***FIXME*** */ set_Runway_altitude( 0.0 );
282     // set_Runway_latitude( Runway_latitude );
283     // set_Runway_longitude( Runway_longitude );
284     // set_Runway_heading( Runway_heading );
285     // set_Radius_to_rwy( Radius_to_rwy );
286
287     // set_CG_Rwy_Local( D_cg_north_of_rwy, D_cg_east_of_rwy, D_cg_above_rwy);
288     // set_CG_Rwy_Rwy( X_cg_rwy, Y_cg_rwy, H_cg_rwy );
289     // set_Pilot_Rwy_Local( D_pilot_north_of_rwy, D_pilot_east_of_rwy, 
290     //                        D_pilot_above_rwy );
291     // set_Pilot_Rwy_Rwy( X_pilot_rwy, Y_pilot_rwy, H_pilot_rwy );
292
293     set_sin_lat_geocentric( lat_geoc );
294     set_cos_lat_geocentric( lat_geoc );
295     set_sin_cos_longitude( lon );
296     set_sin_cos_latitude( lat_geod );
297
298     return 1;
299 }
300
301