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