]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim.cxx
dc40c5a325103e99fcfb19c7351971fb0747dd93
[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.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
123     FDMExec.GetFCS()->SetDaCmd( controls.get_aileron());
124     FDMExec.GetFCS()->SetDeCmd( controls.get_elevator()
125                                                + controls.get_elevator_trim() );
126     FDMExec.GetFCS()->SetDrCmd( controls.get_rudder());
127     FDMExec.GetFCS()->SetDfCmd( 0.0 );
128     FDMExec.GetFCS()->SetDsbCmd( 0.0 );
129     FDMExec.GetFCS()->SetDspCmd( 0.0 );
130     FDMExec.GetFCS()->SetThrottleCmd( FGControls::ALL_ENGINES,
131                                            controls.get_throttle( 0 ) * 100.0 );
132     // FCS->SetBrake( controls.get_brake( 0 ) );
133
134     // Inform JSBsim of the local terrain altitude
135     // Runway_altitude =   get_Runway_altitude();
136
137     // old -- FGInterface_2_JSBsim() not needed except for Init()
138     // translate FG to JSBsim structure
139     // FGInterface_2_JSBsim(f);
140     // printf("FG_Altitude = %.2f\n", FG_Altitude * 0.3048);
141     // printf("Altitude = %.2f\n", Altitude * 0.3048);
142     // printf("Radius to Vehicle = %.2f\n", Radius_to_vehicle * 0.3048);
143
144     /* FDMExec.GetState()->Setsim_time(State->Getsim_time()
145                                     + State->Getdt() * multiloop); */
146
147     for ( int i = 0; i < multiloop; i++ ) {
148       FDMExec.Run();
149     }
150
151     // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
152     // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
153     
154     // translate JSBsim back to FG structure so that the
155     // autopilot (and the rest of the sim can use the updated
156     // values
157
158     copy_from_JSBsim();
159
160     // but lets restore our original bogus altitude when we are done
161     if ( save_alt < -9000.0 ) {
162       set_Altitude( save_alt );
163     }
164
165     double end_elev = get_Altitude();
166     if ( time_step > 0.0 ) {
167       // feet per second
168       set_Climb_Rate( (end_elev - start_elev) / time_step );
169     }
170
171     return 1;
172 }
173
174
175 // Convert from the FGInterface struct to the JSBsim generic_ struct
176 int FGJSBsim::copy_to_JSBsim() {
177     return 1;
178 }
179
180
181 // Convert from the JSBsim generic_ struct to the FGInterface struct
182 int FGJSBsim::copy_from_JSBsim() {
183
184     // Velocities
185     set_Velocities_Local( FDMExec.GetPosition()->GetVn(),
186                           FDMExec.GetPosition()->GetVe(),
187                           FDMExec.GetPosition()->GetVd() );
188     // set_Velocities_Ground( V_north_rel_ground, V_east_rel_ground,
189     //               V_down_rel_ground );
190     // set_Velocities_Local_Airmass( V_north_airmass, V_east_airmass,
191     //                      V_down_airmass );
192     // set_Velocities_Local_Rel_Airmass( V_north_rel_airmass,
193     //                          V_east_rel_airmass, V_down_rel_airmass );
194     // set_Velocities_Gust( U_gust, V_gust, W_gust );
195     // set_Velocities_Wind_Body( U_body, V_body, W_body );
196
197     // set_V_rel_wind( V_rel_wind );
198     // set_V_true_kts( V_true_kts );
199     // set_V_rel_ground( V_rel_ground );
200     // set_V_inertial( V_inertial );
201     // set_V_ground_speed( V_ground_speed );
202     // set_V_equiv( V_equiv );
203
204     set_V_equiv_kts( FDMExec.GetAuxiliary()->GetVequivalentKTS() );
205     //set_V_calibrated( FDMExec.GetAuxiliary()->GetVcalibratedFPS() );
206     set_V_calibrated_kts( FDMExec.GetAuxiliary()->GetVcalibratedKTS() );
207
208     set_Omega_Body( FDMExec.GetState()->GetParameter(FG_ROLLRATE),
209                     FDMExec.GetState()->GetParameter(FG_PITCHRATE),
210                     FDMExec.GetState()->GetParameter(FG_YAWRATE) );
211     // set_Omega_Local( P_local, Q_local, R_local );
212     // set_Omega_Total( P_total, Q_total, R_total );
213
214     set_Euler_Rates( FDMExec.GetRotation()->Getphi(),
215                      FDMExec.GetRotation()->Gettht(),
216                      FDMExec.GetRotation()->Getpsi() );
217
218     // ***FIXME*** set_Geocentric_Rates( Latitude_dot, Longitude_dot, Radius_dot );
219         
220     set_Mach_number( FDMExec.GetTranslation()->GetMach());
221
222     // Positions
223     double lat_geoc = FDMExec.GetPosition()->GetLatitude();
224     double lon = FDMExec.GetPosition()->GetLongitude();
225     double alt = FDMExec.GetPosition()->Geth();
226     double lat_geod, tmp_alt, sl_radius1, sl_radius2, tmp_lat_geoc;
227     fgGeocToGeod( lat_geoc, EQUATORIAL_RADIUS_M + alt * FEET_TO_METER,
228                   &lat_geod, &tmp_alt, &sl_radius1 );
229     fgGeodToGeoc( lat_geod, alt * FEET_TO_METER, &sl_radius2, &tmp_lat_geoc );
230
231     FG_LOG( FG_FLIGHT, FG_DEBUG, "lon = " << lon << " lat_geod = " << lat_geod
232             << " lat_geoc = " << lat_geoc
233             << " alt = " << alt << " tmp_alt = " << tmp_alt * METER_TO_FEET
234             << " sl_radius1 = " << sl_radius1 * METER_TO_FEET
235             << " sl_radius2 = " << sl_radius2 * METER_TO_FEET
236             << " Equator = " << EQUATORIAL_RADIUS_FT );
237             
238     set_Geocentric_Position( lat_geoc, lon, 
239                                sl_radius2 * METER_TO_FEET + alt );
240     set_Geodetic_Position( lat_geod, lon, alt );
241     set_Euler_Angles( FDMExec.GetRotation()->Getphi(),
242                       FDMExec.GetRotation()->Gettht(),
243                       FDMExec.GetRotation()->Getpsi() );
244
245     // Miscellaneous quantities
246     // set_T_Local_to_Body(T_local_to_body_m);
247     // set_Gravity( Gravity );
248     // set_Centrifugal_relief( Centrifugal_relief );
249
250     set_Alpha( FDMExec.GetTranslation()->Getalpha() );
251     set_Beta( FDMExec.GetTranslation()->Getbeta() );
252     // set_Alpha_dot( Alpha_dot );
253     // set_Beta_dot( Beta_dot );
254
255     // set_Cos_alpha( Cos_alpha );
256     // set_Sin_alpha( Sin_alpha );
257     // set_Cos_beta( Cos_beta );
258     // set_Sin_beta( Sin_beta );
259
260     // set_Cos_phi( Cos_phi );
261     // set_Sin_phi( Sin_phi );
262     // set_Cos_theta( Cos_theta );
263     // set_Sin_theta( Sin_theta );
264     // set_Cos_psi( Cos_psi );
265     // set_Sin_psi( Sin_psi );
266
267     // ***ATTENDTOME*** set_Gamma_vert_rad( Gamma_vert_rad );
268     // set_Gamma_horiz_rad( Gamma_horiz_rad );
269
270     // set_Sigma( Sigma );
271     // set_Density( Density );
272     // set_V_sound( V_sound );
273     // set_Mach_number( Mach_number );
274
275     // set_Static_pressure( Static_pressure );
276     // set_Total_pressure( Total_pressure );
277     // set_Impact_pressure( Impact_pressure );
278     // set_Dynamic_pressure( Dynamic_pressure );
279
280     // set_Static_temperature( Static_temperature );
281     // set_Total_temperature( Total_temperature );
282
283     /* **FIXME*** */ set_Sea_level_radius( sl_radius2 * METER_TO_FEET );
284     /* **FIXME*** */ set_Earth_position_angle( 0.0 );
285
286     /* ***FIXME*** */ set_Runway_altitude( 0.0 );
287     // set_Runway_latitude( Runway_latitude );
288     // set_Runway_longitude( Runway_longitude );
289     // set_Runway_heading( Runway_heading );
290     // set_Radius_to_rwy( Radius_to_rwy );
291
292     // set_CG_Rwy_Local( D_cg_north_of_rwy, D_cg_east_of_rwy, D_cg_above_rwy);
293     // set_CG_Rwy_Rwy( X_cg_rwy, Y_cg_rwy, H_cg_rwy );
294     // set_Pilot_Rwy_Local( D_pilot_north_of_rwy, D_pilot_east_of_rwy, 
295     //                        D_pilot_above_rwy );
296     // set_Pilot_Rwy_Rwy( X_pilot_rwy, Y_pilot_rwy, H_pilot_rwy );
297
298     set_sin_lat_geocentric( lat_geoc );
299     set_cos_lat_geocentric( lat_geoc );
300     set_sin_cos_longitude( lon );
301     set_sin_cos_latitude( lat_geod );
302
303     return 1;
304 }
305
306