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