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