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