]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim.cxx
993e11e56c7c60fb3d75383e30ea5820692d7a51
[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 <Scenery/scenery.hxx>
38
39 #include <Aircraft/aircraft.hxx>
40 #include <Controls/controls.hxx>
41 #include <Main/options.hxx>
42
43 #include <FDM/JSBsim/FGFDMExec.h>
44 #include <FDM/JSBsim/FGAircraft.h>
45 #include <FDM/JSBsim/FGFCS.h>
46 #include <FDM/JSBsim/FGPosition.h>
47 #include <FDM/JSBsim/FGRotation.h>
48 #include <FDM/JSBsim/FGState.h>
49 #include <FDM/JSBsim/FGTranslation.h>
50 #include <FDM/JSBsim/FGAuxiliary.h>
51 #include <FDM/JSBsim/FGDefs.h>
52 #include <FDM/JSBsim/FGInitialCondition.h>
53 #include <FDM/JSBsim/FGTrimLong.h>
54 #include <FDM/JSBsim/FGAtmosphere.h>
55
56 #include "JSBsim.hxx"
57
58 double geocRwyRadius;
59
60 /******************************************************************************/
61
62 // Initialize the JSBsim flight model, dt is the time increment for
63 // each subsequent iteration through the EOM
64
65 int FGJSBsim::init( double dt ) {
66
67
68
69   FG_LOG( FG_FLIGHT, FG_INFO, "Starting and initializing JSBsim" );
70   FG_LOG( FG_FLIGHT, FG_INFO, "  created FDMExec" );
71
72   FGPath aircraft_path( current_options.get_fg_root() );
73   aircraft_path.append( "Aircraft" );
74
75   FGPath engine_path( current_options.get_fg_root() );
76   engine_path.append( "Engine" );
77
78   FDMExec.GetState()->Setdt( dt );
79
80   FDMExec.GetAircraft()->LoadAircraft( aircraft_path.str(),
81                                        engine_path.str(),
82                                        current_options.get_aircraft() );
83
84   FG_LOG( FG_FLIGHT, FG_INFO, "  loaded aircraft" <<
85           current_options.get_aircraft() );
86
87   FDMExec.GetAtmosphere()->UseInternal();
88
89   FG_LOG( FG_FLIGHT, FG_INFO, "  Initializing JSBsim with:" );
90
91   FGInitialCondition *fgic = new FGInitialCondition(&FDMExec);
92   fgic->SetAltitudeFtIC(get_Altitude());
93   if((current_options.get_mach() < 0) && (current_options.get_vc() < 0 )) {
94     fgic->SetUBodyFpsIC(current_options.get_uBody());
95     fgic->SetVBodyFpsIC(current_options.get_vBody());
96     fgic->SetWBodyFpsIC(current_options.get_wBody());
97     FG_LOG(FG_FLIGHT,FG_INFO, "  u,v,w= " << current_options.get_uBody()
98            << ", " << current_options.get_vBody()
99            << ", " << current_options.get_wBody());
100   } else if (current_options.get_vc() < 0) {
101     fgic->SetMachIC(current_options.get_mach());
102     FG_LOG(FG_FLIGHT,FG_INFO, "  mach: " << current_options.get_mach() );
103   } else {
104     fgic->SetVcalibratedKtsIC(current_options.get_vc());
105     FG_LOG(FG_FLIGHT,FG_INFO, "  vc: " << current_options.get_vc() );
106     //this should cover the case in which no speed switches are used
107     //current_options.get_vc() will return zero by default
108   }
109
110
111   fgic->SetRollAngleRadIC(get_Phi());
112   fgic->SetPitchAngleRadIC(get_Theta());
113   fgic->SetHeadingRadIC(get_Psi());
114   fgic->SetLatitudeRadIC(get_Latitude());
115   fgic->SetLongitudeRadIC(get_Longitude());
116
117   FDMExec.GetPosition()->SetRunwayRadius(geocRwyRadius);
118
119   FG_LOG( FG_FLIGHT, FG_INFO, "  phi: " <<  get_Phi());
120   FG_LOG( FG_FLIGHT, FG_INFO, "  theta: " <<  get_Theta() );
121   FG_LOG( FG_FLIGHT, FG_INFO, "  psi: " <<  get_Psi() );
122   FG_LOG( FG_FLIGHT, FG_INFO, "  lat: " <<  get_Latitude() );
123   FG_LOG( FG_FLIGHT, FG_INFO, "  lon: " <<  get_Longitude() );
124   FG_LOG( FG_FLIGHT, FG_INFO, "  alt: " <<  get_Altitude() );
125
126   if(current_options.get_trim_mode() == true) {
127     FG_LOG( FG_FLIGHT, FG_INFO, "  Starting trim..." );
128     FGTrimLong *fgtrim=new FGTrimLong(&FDMExec,fgic);
129     fgtrim->DoTrim();
130     fgtrim->Report();
131     fgtrim->TrimStats();
132     fgtrim->ReportState();
133     
134     controls.set_elevator_trim(FDMExec.GetFCS()->GetDeCmd());
135     controls.set_trimmed_throttle(FGControls::ALL_ENGINES,FDMExec.GetFCS()->GetThrottleCmd(0)/100);
136     //the trimming routine only knows how to get 1 value for throttle
137     
138     //for(int i=0;i<FDMExec.GetAircraft()->GetNumEngines();i++) {
139     //  controls.set_throttle(i,FDMExec.GetFCS()->GetThrottleCmd(i)/100);
140     //}
141     delete fgtrim;
142     FG_LOG( FG_FLIGHT, FG_INFO, "  Trim complete." );
143   } else {
144     FG_LOG( FG_FLIGHT, FG_INFO, "  Initializing without trim" );
145     FDMExec.GetState()->Initialize(fgic);
146
147   }
148
149   delete fgic;
150
151   FG_LOG( FG_FLIGHT, FG_INFO, "  loaded initial conditions" );
152
153   FG_LOG( FG_FLIGHT, FG_INFO, "  set dt" );
154
155   FG_LOG( FG_FLIGHT, FG_INFO, "Finished initializing JSBsim" );
156
157   copy_from_JSBsim();
158
159   return 1;
160 }
161
162 /******************************************************************************/
163
164 // Run an iteration of the EOM (equations of motion)
165
166 int FGJSBsim::update( int multiloop ) {
167   double save_alt = 0.0;
168   double time_step = (1.0 / current_options.get_model_hz()) * multiloop;
169   double start_elev = get_Altitude();
170
171   // lets try to avoid really screwing up the JSBsim model
172   if ( get_Altitude() < -9000 ) {
173     save_alt = get_Altitude();
174     set_Altitude( 0.0 );
175   }
176
177   // copy control positions into the JSBsim structure
178
179   FDMExec.GetFCS()->SetDaCmd( controls.get_aileron());
180   FDMExec.GetFCS()->SetDeCmd( controls.get_elevator());
181   FDMExec.GetFCS()->SetPitchTrimCmd(controls.get_elevator_trim());
182   FDMExec.GetFCS()->SetDrCmd( controls.get_rudder());
183   FDMExec.GetFCS()->SetDfCmd( controls.get_flaps() );
184   FDMExec.GetFCS()->SetDsbCmd( 0.0 );
185   FDMExec.GetFCS()->SetDspCmd( 0.0 );
186   FDMExec.GetFCS()->SetThrottleCmd( FGControls::ALL_ENGINES,
187                                     controls.get_throttle( 0 ) * 100.0 );
188
189   // FCS->SetBrake( controls.get_brake( 0 ) );
190
191   // Inform JSBsim of the local terrain altitude; uncommented 5/3/00
192 //  FDMExec.GetPosition()->SetRunwayElevation(get_Runway_altitude()); // seems to work
193   FDMExec.GetPosition()->SetRunwayRadius(geocRwyRadius);
194
195   FDMExec.GetAtmosphere()->SetExTemperature(get_Static_temperature());
196   FDMExec.GetAtmosphere()->SetExPressure(get_Static_pressure());
197   FDMExec.GetAtmosphere()->SetExDensity(get_Density());
198   FDMExec.GetAtmosphere()->SetWindNED(get_V_north_airmass(),
199                                       get_V_east_airmass(),
200                                       get_V_down_airmass());
201
202   for ( int i = 0; i < multiloop; i++ ) {
203     FDMExec.Run();
204   }
205
206   // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
207   // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
208
209   // translate JSBsim back to FG structure so that the
210   // autopilot (and the rest of the sim can use the updated values
211
212   copy_from_JSBsim();
213
214   // but lets restore our original bogus altitude when we are done
215
216   if ( save_alt < -9000.0 ) {
217     set_Altitude( save_alt );
218   }
219
220   double end_elev = get_Altitude();
221   if ( time_step > 0.0 ) {
222     // feet per second
223     set_Climb_Rate( (end_elev - start_elev) / time_step );
224   }
225
226   return 1;
227 }
228
229 /******************************************************************************/
230
231 // Convert from the FGInterface struct to the JSBsim generic_ struct
232
233 int FGJSBsim::copy_to_JSBsim() {
234   return 1;
235 }
236
237 /******************************************************************************/
238
239 // Convert from the JSBsim generic_ struct to the FGInterface struct
240
241 int FGJSBsim::copy_from_JSBsim() {
242
243   // Velocities
244
245   set_Velocities_Local( FDMExec.GetPosition()->GetVn(),
246                         FDMExec.GetPosition()->GetVe(),
247                         FDMExec.GetPosition()->GetVd() );
248
249   set_V_equiv_kts( FDMExec.GetAuxiliary()->GetVequivalentKTS() );
250
251   //set_V_calibrated( FDMExec.GetAuxiliary()->GetVcalibratedFPS() );
252
253   set_V_calibrated_kts( FDMExec.GetAuxiliary()->GetVcalibratedKTS() );
254
255   set_Omega_Body( FDMExec.GetState()->GetParameter(FG_ROLLRATE),
256                   FDMExec.GetState()->GetParameter(FG_PITCHRATE),
257                   FDMExec.GetState()->GetParameter(FG_YAWRATE) );
258
259   set_Euler_Rates( FDMExec.GetRotation()->Getphi(),
260                    FDMExec.GetRotation()->Gettht(),
261                    FDMExec.GetRotation()->Getpsi() );
262
263   // ***FIXME*** set_Geocentric_Rates( Latitude_dot, Longitude_dot, Radius_dot );
264
265   set_Mach_number( FDMExec.GetTranslation()->GetMach());
266
267   // Positions
268
269   double lat_geoc = FDMExec.GetPosition()->GetLatitude();
270   double lon = FDMExec.GetPosition()->GetLongitude();
271   double alt = FDMExec.GetPosition()->Geth();
272   double lat_geod, tmp_alt, sl_radius1, sl_radius2, tmp_lat_geoc;
273
274   fgGeocToGeod( lat_geoc, EQUATORIAL_RADIUS_M + alt * FEET_TO_METER,
275                 &lat_geod, &tmp_alt, &sl_radius1 );
276   fgGeodToGeoc( lat_geod, alt * FEET_TO_METER, &sl_radius2, &tmp_lat_geoc );
277
278   FG_LOG( FG_FLIGHT, FG_DEBUG, "lon = " << lon << " lat_geod = " << lat_geod
279           << " lat_geoc = " << lat_geoc
280           << " alt = " << alt << " tmp_alt = " << tmp_alt * METER_TO_FEET
281           << " sl_radius1 = " << sl_radius1 * METER_TO_FEET
282           << " sl_radius2 = " << sl_radius2 * METER_TO_FEET
283           << " Equator = " << EQUATORIAL_RADIUS_FT );
284
285   set_Geocentric_Position( lat_geoc, lon,
286                            sl_radius2 * METER_TO_FEET + alt );
287   set_Geodetic_Position( lat_geod, lon, alt );
288   set_Euler_Angles( FDMExec.GetRotation()->Getphi(),
289                     FDMExec.GetRotation()->Gettht(),
290                     FDMExec.GetRotation()->Getpsi() );
291
292   set_Alpha( FDMExec.GetTranslation()->Getalpha() );
293   set_Beta( FDMExec.GetTranslation()->Getbeta() );
294
295   set_Gamma_vert_rad( FDMExec.GetPosition()->GetGamma() );
296   // set_Gamma_horiz_rad( Gamma_horiz_rad );
297
298   /* **FIXME*** */ set_Sea_level_radius( sl_radius2 * METER_TO_FEET );
299   /* **FIXME*** */ set_Earth_position_angle( 0.0 );
300
301   // /* ***FIXME*** */ set_Runway_altitude( 0.0 );
302
303   set_sin_lat_geocentric( lat_geoc );
304   set_cos_lat_geocentric( lat_geoc );
305   set_sin_cos_longitude( lon );
306   set_sin_cos_latitude( lat_geod );
307
308   return 1;
309 }
310
311 /******************************************************************************/
312