]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim.cxx
48efe4341bba3fcd116d9ffa1d348f9bbbb1a45b
[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/sg_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/FGTrim.h>
54 #include <FDM/JSBSim/FGAtmosphere.h>
55
56 #include "JSBSim.hxx"
57
58 /******************************************************************************/
59
60 // Initialize the JSBsim flight model, dt is the time increment for
61 // each subsequent iteration through the EOM
62
63 static bool trimmed = false;
64 static bool trim_elev = false;
65 static bool trim_throttle = false;
66
67 int FGJSBsim::init( double dt ) {
68
69   bool result;
70
71   FG_LOG( FG_FLIGHT, FG_INFO, "Starting and initializing JSBsim" );
72   FG_LOG( FG_FLIGHT, FG_INFO, "  created FDMExec" );
73
74   FGPath aircraft_path( current_options.get_fg_root() );
75   aircraft_path.append( "Aircraft" );
76
77   FGPath engine_path( current_options.get_fg_root() );
78   engine_path.append( "Engine" );
79
80   //FDMExec.GetState()->Setdt( dt );
81
82   result = FDMExec.LoadModel( aircraft_path.str(),
83                                        engine_path.str(),
84                                        current_options.get_aircraft() );
85   FDMExec.GetState()->Setdt( dt );
86   
87   if (result) {
88     FG_LOG( FG_FLIGHT, FG_INFO, "  loaded aircraft " << current_options.get_aircraft() );
89   } else {
90     FG_LOG( FG_FLIGHT, FG_INFO, "  aircraft" << current_options.get_aircraft()
91                                 << " does not exist");
92     return 0;
93   }
94
95   FDMExec.GetAtmosphere()->SetExTemperature(get_Static_temperature());
96   FDMExec.GetAtmosphere()->SetExPressure(get_Static_pressure());
97   FDMExec.GetAtmosphere()->SetExDensity(get_Density());
98   FDMExec.GetAtmosphere()->SetWindNED(get_V_north_airmass(),
99                                       get_V_east_airmass(),
100                                       get_V_down_airmass());
101  
102   FDMExec.GetAtmosphere()->UseInternal();
103   
104  
105   FDMExec.GetPosition()->SetRunwayRadius(scenery.cur_radius*METER_TO_FEET);
106   FDMExec.GetPosition()->SetSeaLevelRadius(get_Sea_level_radius());
107
108   FG_LOG( FG_FLIGHT, FG_INFO, "  Initializing JSBSim with:" );
109   
110   FGInitialCondition *fgic = new FGInitialCondition(&FDMExec);
111   fgic->SetAltitudeFtIC(get_Altitude());
112   if((current_options.get_mach() < 0) && (current_options.get_vc() < 0 )) {
113     fgic->SetUBodyFpsIC(current_options.get_uBody());
114     fgic->SetVBodyFpsIC(current_options.get_vBody());
115     fgic->SetWBodyFpsIC(current_options.get_wBody());
116     FG_LOG(FG_FLIGHT,FG_INFO, "  u,v,w= " << current_options.get_uBody()
117            << ", " << current_options.get_vBody()
118            << ", " << current_options.get_wBody());
119   } else if (current_options.get_vc() < 0) {
120     fgic->SetMachIC(current_options.get_mach());
121     FG_LOG(FG_FLIGHT,FG_INFO, "  mach: " << current_options.get_mach() );
122   } else {
123     fgic->SetVcalibratedKtsIC(current_options.get_vc());
124     FG_LOG(FG_FLIGHT,FG_INFO, "  vc: " << current_options.get_vc() );
125     //this should cover the case in which no speed switches are used
126     //current_options.get_vc() will return zero by default
127   }
128
129   //fgic->SetFlightPathAngleDegIC(current_options.get_Gamma());
130   fgic->SetRollAngleRadIC(get_Phi());
131   fgic->SetPitchAngleRadIC(get_Theta());
132   fgic->SetHeadingRadIC(get_Psi());
133   fgic->SetLatitudeRadIC(get_Lat_geocentric());
134   fgic->SetLongitudeRadIC(get_Longitude());
135
136   
137   
138   
139   //FG_LOG( FG_FLIGHT, FG_INFO, "  gamma: " <<  current_options.get_Gamma());
140   FG_LOG( FG_FLIGHT, FG_INFO, "  phi: " <<  get_Phi());
141   //FG_LOG( FG_FLIGHT, FG_INFO, "  theta: " <<  get_Theta() );
142   FG_LOG( FG_FLIGHT, FG_INFO, "  psi: " <<  get_Psi() );
143   FG_LOG( FG_FLIGHT, FG_INFO, "  lat: " <<  get_Latitude() );
144   FG_LOG( FG_FLIGHT, FG_INFO, "  lon: " <<  get_Longitude() );
145   
146   FG_LOG( FG_FLIGHT, FG_INFO, "  Pressure Altiude: " <<  get_Altitude() );
147   FG_LOG( FG_FLIGHT, FG_INFO, "  Terrain Altitude: " 
148           <<  scenery.cur_radius*METER_TO_FEET );
149   FG_LOG( FG_FLIGHT, FG_INFO, "      AGL Altitude: " 
150           <<  get_Altitude() + get_Sea_level_radius()
151                 - scenery.cur_radius*METER_TO_FEET );
152   
153   FG_LOG( FG_FLIGHT, FG_INFO, "      current_options.get_altitude(): " 
154           <<  current_options.get_altitude() );
155   //must check > 0, != 0 will give bad result if --notrim set
156   if(current_options.get_trim_mode() > 0) {
157     FDMExec.RunIC(fgic);
158     FG_LOG( FG_FLIGHT, FG_INFO, "  Starting trim..." );
159     FGTrim *fgtrim=new FGTrim(&FDMExec,fgic,tLongitudinal);
160     fgtrim->DoTrim();
161     fgtrim->Report();
162     fgtrim->TrimStats();
163     fgtrim->ReportState();
164
165
166     controls.set_elevator_trim(FDMExec.GetFCS()->GetPitchTrimCmd());
167     controls.set_throttle(FGControls::ALL_ENGINES,FDMExec.GetFCS()->GetThrottleCmd(0)/100);
168     trimmed=true;
169     trim_elev=FDMExec.GetFCS()->GetPitchTrimCmd();
170     trim_throttle=FDMExec.GetFCS()->GetThrottleCmd(0)/100;
171     //the trimming routine only knows how to get 1 value for throttle
172     
173     delete fgtrim;
174     FG_LOG( FG_FLIGHT, FG_INFO, "  Trim complete." );
175   } else {
176     FG_LOG( FG_FLIGHT, FG_INFO, "  Initializing without trim" );
177     FDMExec.GetState()->Initialize(fgic);
178
179   }
180
181   delete fgic;
182
183   FG_LOG( FG_FLIGHT, FG_INFO, "  loaded initial conditions" );
184
185   FG_LOG( FG_FLIGHT, FG_INFO, "  set dt" );
186
187   FG_LOG( FG_FLIGHT, FG_INFO, "Finished initializing JSBSim" );
188
189   copy_from_JSBsim();
190
191   return 1;
192 }
193
194 /******************************************************************************/
195
196 // Run an iteration of the EOM (equations of motion)
197
198 int FGJSBsim::update( int multiloop ) {
199   double save_alt = 0.0;
200   double time_step = (1.0 / current_options.get_model_hz()) * multiloop;
201   double start_elev = get_Altitude();
202  
203   
204   // lets try to avoid really screwing up the JSBsim model
205   if ( get_Altitude() < -9000 ) {
206     save_alt = get_Altitude();
207     set_Altitude( 0.0 );
208   }
209   
210   if(trimmed) {
211     
212     controls.set_elevator_trim(trim_elev);
213     controls.set_throttle(FGControls::ALL_ENGINES,trim_throttle);
214     
215     controls.set_elevator(0.0);
216     controls.set_aileron(0.0);
217     controls.set_rudder(0.0);
218     trimmed=false;
219     
220   } 
221   
222   copy_to_JSBsim();
223
224   for ( int i = 0; i < multiloop; i++ ) {
225     FDMExec.Run();
226   }
227
228   // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
229   // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
230
231   // translate JSBsim back to FG structure so that the
232   // autopilot (and the rest of the sim can use the updated values
233
234   copy_from_JSBsim();
235
236   // but lets restore our original bogus altitude when we are done
237
238   if ( save_alt < -9000.0 ) {
239     set_Altitude( save_alt );
240   }
241
242   double end_elev = get_Altitude();
243
244   return 1;
245 }
246
247 /******************************************************************************/
248
249 // Convert from the FGInterface struct to the JSBsim generic_ struct
250
251 int FGJSBsim::copy_to_JSBsim() {
252   // copy control positions into the JSBsim structure
253
254   FDMExec.GetFCS()->SetDaCmd( controls.get_aileron());
255   FDMExec.GetFCS()->SetDeCmd( controls.get_elevator());
256   FDMExec.GetFCS()->SetPitchTrimCmd(controls.get_elevator_trim());
257   FDMExec.GetFCS()->SetDrCmd( controls.get_rudder());
258   FDMExec.GetFCS()->SetDfCmd( controls.get_flaps() );
259   FDMExec.GetFCS()->SetDsbCmd( 0.0 ); //speedbrakes
260   FDMExec.GetFCS()->SetDspCmd( 0.0 ); //spoilers
261   FDMExec.GetFCS()->SetThrottleCmd( FGControls::ALL_ENGINES,
262                                     controls.get_throttle( 0 ) * 100.0 );
263
264   FDMExec.GetFCS()->SetLBrake( controls.get_brake( 0 ) );
265   FDMExec.GetFCS()->SetRBrake( controls.get_brake( 1 ) );
266   FDMExec.GetFCS()->SetCBrake( controls.get_brake( 2 ) );
267
268   // Inform JSBsim of the local terrain altitude; uncommented 5/3/00
269   //  FDMExec.GetPosition()->SetRunwayElevation(get_Runway_altitude()); // seems to work
270   FDMExec.GetPosition()->SetRunwayRadius(scenery.cur_radius*METER_TO_FEET);
271   FDMExec.GetPosition()->SetSeaLevelRadius(get_Sea_level_radius());
272
273   FDMExec.GetAtmosphere()->SetExTemperature(get_Static_temperature());
274   FDMExec.GetAtmosphere()->SetExPressure(get_Static_pressure());
275   FDMExec.GetAtmosphere()->SetExDensity(get_Density());
276   FDMExec.GetAtmosphere()->SetWindNED(get_V_north_airmass(),
277                                       get_V_east_airmass(),
278                                       get_V_down_airmass());
279
280   return 1;
281 }
282
283 /******************************************************************************/
284
285 // Convert from the JSBsim generic_ struct to the FGInterface struct
286
287 int FGJSBsim::copy_from_JSBsim() {
288
289   set_Inertias( FDMExec.GetAircraft()->GetMass(),
290                 FDMExec.GetAircraft()->GetIxx(),
291                 FDMExec.GetAircraft()->GetIyy(),
292                 FDMExec.GetAircraft()->GetIzz(),
293                 FDMExec.GetAircraft()->GetIxz() );
294   
295   set_CG_Position ( FDMExec.GetAircraft()->GetXYZcg()(1),
296                     FDMExec.GetAircraft()->GetXYZcg()(2),
297                     FDMExec.GetAircraft()->GetXYZcg()(3) );
298   
299   set_Accels_Body ( FDMExec.GetTranslation()->GetUVWdot()(1),
300                     FDMExec.GetTranslation()->GetUVWdot()(2),
301                     FDMExec.GetTranslation()->GetUVWdot()(3) );
302   
303   set_Accels_CG_Body ( FDMExec.GetTranslation()->GetUVWdot()(1),
304                        FDMExec.GetTranslation()->GetUVWdot()(2),
305                        FDMExec.GetTranslation()->GetUVWdot()(3) );
306   
307   //set_Accels_CG_Body_N ( FDMExec.GetTranslation()->GetNcg()(1),
308   //                       FDMExec.GetTranslation()->GetNcg()(2),
309   //                       FDMExec.GetTranslation()->GetNcg()(3) );
310   //
311   set_Accels_Pilot_Body( FDMExec.GetAuxiliary()->GetPilotAccel()(1),
312                          FDMExec.GetAuxiliary()->GetPilotAccel()(2),
313                          FDMExec.GetAuxiliary()->GetPilotAccel()(3) );
314   
315   //set_Accels_Pilot_Body_N( FDMExec.GetAuxiliary()->GetNpilot()(1),
316   //                         FDMExec.GetAuxiliary()->GetNpilot()(2),
317   //                         FDMExec.GetAuxiliary()->GetNpilot()(3) );
318   
319                            
320   
321   set_Nlf( FDMExec.GetAircraft()->GetNlf());                       
322   
323   
324    
325   // Velocities
326
327   set_Velocities_Local( FDMExec.GetPosition()->GetVn(),
328                         FDMExec.GetPosition()->GetVe(),
329                         FDMExec.GetPosition()->GetVd() );
330
331   set_Velocities_Wind_Body( FDMExec.GetTranslation()->GetUVW()(1),
332                             FDMExec.GetTranslation()->GetUVW()(2),
333                             FDMExec.GetTranslation()->GetUVW()(3)  );
334   
335   set_V_equiv_kts( FDMExec.GetAuxiliary()->GetVequivalentKTS() );
336
337   //set_V_calibrated( FDMExec.GetAuxiliary()->GetVcalibratedFPS() );
338
339   set_V_calibrated_kts( FDMExec.GetAuxiliary()->GetVcalibratedKTS() );
340   
341   set_V_ground_speed ( FDMExec.GetPosition()->GetVground() );
342
343   set_Omega_Body( FDMExec.GetState()->GetParameter(FG_ROLLRATE),
344                   FDMExec.GetState()->GetParameter(FG_PITCHRATE),
345                   FDMExec.GetState()->GetParameter(FG_YAWRATE) );
346
347   set_Euler_Rates( FDMExec.GetRotation()->GetEulerRates()(2),
348                    FDMExec.GetRotation()->GetEulerRates()(1),
349                    FDMExec.GetRotation()->GetEulerRates()(3));
350
351   // ***FIXME*** set_Geocentric_Rates( Latitude_dot, Longitude_dot, Radius_dot );
352
353   set_Mach_number( FDMExec.GetTranslation()->GetMach());
354
355   // Positions
356
357   double lat_geoc = FDMExec.GetPosition()->GetLatitude();
358   double lon = FDMExec.GetPosition()->GetLongitude();
359   double alt = FDMExec.GetPosition()->Geth();
360   double lat_geod, tmp_alt, sl_radius1, sl_radius2, tmp_lat_geoc;
361
362   sgGeocToGeod( lat_geoc, EQUATORIAL_RADIUS_M + alt * FEET_TO_METER,
363                 &lat_geod, &tmp_alt, &sl_radius1 );
364   sgGeodToGeoc( lat_geod, alt * FEET_TO_METER, &sl_radius2, &tmp_lat_geoc );
365
366   FG_LOG( FG_FLIGHT, FG_DEBUG, "lon = " << lon << " lat_geod = " << lat_geod
367           << " lat_geoc = " << lat_geoc
368           << " alt = " << alt << " tmp_alt = " << tmp_alt * METER_TO_FEET
369           << " sl_radius1 = " << sl_radius1 * METER_TO_FEET
370           << " sl_radius2 = " << sl_radius2 * METER_TO_FEET
371           << " Equator = " << EQUATORIAL_RADIUS_FT );
372
373   set_Geocentric_Position( lat_geoc, lon,
374                            sl_radius2 * METER_TO_FEET + alt );
375   set_Geodetic_Position( lat_geod, lon, alt );
376   set_Euler_Angles( FDMExec.GetRotation()->Getphi(),
377                     FDMExec.GetRotation()->Gettht(),
378                     FDMExec.GetRotation()->Getpsi() );
379
380   set_Alpha( FDMExec.GetTranslation()->Getalpha() );
381   set_Beta( FDMExec.GetTranslation()->Getbeta() );
382
383   set_Gamma_vert_rad( FDMExec.GetPosition()->GetGamma() );
384   // set_Gamma_horiz_rad( Gamma_horiz_rad );
385
386   /* **FIXME*** */ set_Sea_level_radius( sl_radius2 * METER_TO_FEET );
387   /* **FIXME*** */ set_Earth_position_angle( 0.0 );
388
389   // /* ***FIXME*** */ set_Runway_altitude( 0.0 );
390
391   set_sin_lat_geocentric( lat_geoc );
392   set_cos_lat_geocentric( lat_geoc );
393   set_sin_cos_longitude( lon );
394   set_sin_cos_latitude( lat_geod );
395   
396   set_Climb_Rate(FDMExec.GetPosition()->Gethdot());
397
398   return 1;
399 }