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