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