1 // JSBsim.cxx -- interface to the JSBsim flight model
3 // Written by Curtis Olson, started February 1999.
5 // Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
24 #include <simgear/compiler.h>
26 #ifdef SG_MATH_EXCEPTION_CLASH
32 #include <simgear/constants.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/math/sg_geodesy.hxx>
35 #include <simgear/misc/sg_path.hxx>
37 #include <Scenery/scenery.hxx>
39 #include <Aircraft/aircraft.hxx>
40 #include <Controls/controls.hxx>
41 #include <Main/globals.hxx>
42 #include <Main/fg_props.hxx>
44 #include <FDM/JSBSim/FGFDMExec.h>
45 #include <FDM/JSBSim/FGAircraft.h>
46 #include <FDM/JSBSim/FGFCS.h>
47 #include <FDM/JSBSim/FGPosition.h>
48 #include <FDM/JSBSim/FGRotation.h>
49 #include <FDM/JSBSim/FGState.h>
50 #include <FDM/JSBSim/FGTranslation.h>
51 #include <FDM/JSBSim/FGAuxiliary.h>
52 #include <FDM/JSBSim/FGDefs.h>
53 #include <FDM/JSBSim/FGInitialCondition.h>
54 #include <FDM/JSBSim/FGTrim.h>
55 #include <FDM/JSBSim/FGAtmosphere.h>
56 #include <FDM/JSBSim/FGMassBalance.h>
57 #include <FDM/JSBSim/FGAerodynamics.h>
60 /******************************************************************************/
62 FGJSBsim::FGJSBsim( double dt )
69 State = fdmex->GetState();
70 Atmosphere = fdmex->GetAtmosphere();
71 FCS = fdmex->GetFCS();
72 MassBalance = fdmex->GetMassBalance();
73 Propulsion = fdmex->GetPropulsion();
74 Aircraft = fdmex->GetAircraft();
75 Translation = fdmex->GetTranslation();
76 Rotation = fdmex->GetRotation();
77 Position = fdmex->GetPosition();
78 Auxiliary = fdmex->GetAuxiliary();
79 Aerodynamics = fdmex->GetAerodynamics();
81 fgic=new FGInitialCondition(fdmex);
84 SGPath aircraft_path( globals->get_fg_root() );
85 aircraft_path.append( "Aircraft" );
87 SGPath engine_path( globals->get_fg_root() );
88 engine_path.append( "Engine" );
92 result = fdmex->LoadModel( aircraft_path.str(),
94 fgGetString("/sim/aircraft") );
96 int Neng = Propulsion->GetNumEngines();
97 SG_LOG(SG_FLIGHT,SG_INFO, "Neng: " << Neng );
99 for(int i=0;i<Neng;i++) {
100 add_engine( FGEngInterface() );
103 fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
104 fgSetDouble("/fdm/trim/throttle", FCS->GetThrottleCmd(0));
105 fgSetDouble("/fdm/trim/aileron", FCS->GetDaCmd());
106 fgSetDouble("/fdm/trim/rudder", FCS->GetDrCmd());
108 trimmed = fgGetValue("/fdm/trim/trimmed",true);
109 trimmed->setBoolValue(false);
113 /******************************************************************************/
114 FGJSBsim::~FGJSBsim(void) {
121 /******************************************************************************/
123 // Initialize the JSBsim flight model, dt is the time increment for
124 // each subsequent iteration through the EOM
126 void FGJSBsim::init() {
127 // Explicitly call the superclass's
128 // init method first.
133 SG_LOG( SG_FLIGHT, SG_INFO, "Starting and initializing JSBsim" );
135 Atmosphere->UseInternal();
137 SG_LOG( SG_FLIGHT, SG_INFO, " Initializing JSBSim with:" );
139 switch(fgic->GetSpeedSet()) {
141 SG_LOG(SG_FLIGHT,SG_INFO, " Vn,Ve,Vd= "
142 << Position->GetVn() << ", "
143 << Position->GetVe() << ", "
144 << Position->GetVd() << " ft/s");
147 SG_LOG(SG_FLIGHT,SG_INFO, " U,V,W= "
148 << Translation->GetUVW(1) << ", "
149 << Translation->GetUVW(2) << ", "
150 << Translation->GetUVW(3) << " ft/s");
153 SG_LOG(SG_FLIGHT,SG_INFO, " Mach: "
154 << Translation->GetMach() );
158 SG_LOG(SG_FLIGHT,SG_INFO, " Indicated Airspeed: "
159 << Auxiliary->GetVcalibratedKTS() << " knots" );
163 SG_LOG( SG_FLIGHT, SG_INFO, " Bank Angle: "
164 << Rotation->Getphi()*RADTODEG << " deg");
165 SG_LOG( SG_FLIGHT, SG_INFO, " Pitch Angle: "
166 << Rotation->Gettht()*RADTODEG << " deg" );
167 SG_LOG( SG_FLIGHT, SG_INFO, " True Heading: "
168 << Rotation->Getpsi()*RADTODEG << " deg" );
169 SG_LOG( SG_FLIGHT, SG_INFO, " Latitude: "
170 << Position->GetLatitude() << " deg" );
171 SG_LOG( SG_FLIGHT, SG_INFO, " Longitude: "
172 << Position->GetLongitude() << " deg" );
174 SG_LOG( SG_FLIGHT, SG_INFO, " loaded initial conditions" );
176 SG_LOG( SG_FLIGHT, SG_INFO, " set dt" );
178 SG_LOG( SG_FLIGHT, SG_INFO, "Finished initializing JSBSim" );
181 /******************************************************************************/
183 // Run an iteration of the EOM (equations of motion)
185 bool FGJSBsim::update( int multiloop ) {
189 double save_alt = 0.0;
193 trimmed->setBoolValue(false);
195 if (needTrim && fgGetBool("/sim/startup/trim")) {
197 //fgic->SetSeaLevelRadiusFtIC( get_Sea_level_radius() );
198 //fgic->SetTerrainAltitudeFtIC( scenery.cur_elev * SG_METER_TO_FEET );
201 if(fgic->GetVcalibratedKtsIC() < 10 ) {
202 fgic->SetVcalibratedKtsIC(0.0);
203 fgtrim=new FGTrim(fdmex,fgic,tGround);
205 fgtrim=new FGTrim(fdmex,fgic,tLongitudinal);
207 if(!fgtrim->DoTrim()) {
211 trimmed->setBoolValue(true);
213 fgtrim->ReportState();
218 fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
219 fgSetDouble("/fdm/trim/throttle", FCS->GetThrottleCmd(0));
220 fgSetDouble("/fdm/trim/aileron", FCS->GetDaCmd());
221 fgSetDouble("/fdm/trim/rudder", FCS->GetDrCmd());
223 controls.set_elevator_trim(FCS->GetPitchTrimCmd());
224 controls.set_elevator(FCS->GetDeCmd());
225 controls.set_throttle(FGControls::ALL_ENGINES,
226 FCS->GetThrottleCmd(0));
228 controls.set_aileron(FCS->GetDaCmd());
229 controls.set_rudder( FCS->GetDrCmd());
231 SG_LOG( SG_FLIGHT, SG_INFO, " Trim complete" );
234 for( i=0; i<get_num_engines(); i++ ) {
235 get_engine(i)->set_RPM( Propulsion->GetThruster(i)->GetRPM() );
236 get_engine(i)->set_Throttle( controls.get_throttle(i) );
239 for ( i=0; i < multiloop; i++ ) {
243 // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
244 // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
246 // translate JSBsim back to FG structure so that the
247 // autopilot (and the rest of the sim can use the updated values
253 /******************************************************************************/
255 // Convert from the FGInterface struct to the JSBsim generic_ struct
257 bool FGJSBsim::copy_to_JSBsim() {
258 // copy control positions into the JSBsim structure
260 FCS->SetDaCmd( controls.get_aileron());
261 FCS->SetDeCmd( controls.get_elevator());
262 FCS->SetPitchTrimCmd(controls.get_elevator_trim());
263 FCS->SetDrCmd( -controls.get_rudder());
264 FCS->SetDfCmd( controls.get_flaps() );
265 FCS->SetDsbCmd( 0.0 ); //speedbrakes
266 FCS->SetDspCmd( 0.0 ); //spoilers
267 FCS->SetThrottleCmd( FGControls::ALL_ENGINES,
268 controls.get_throttle( 0 ));
269 FCS->SetLBrake( controls.get_brake( 0 ) );
270 FCS->SetRBrake( controls.get_brake( 1 ) );
271 FCS->SetCBrake( controls.get_brake( 2 ) );
273 Position->SetSeaLevelRadius( get_Sea_level_radius() );
274 Position->SetRunwayRadius( scenery.cur_elev*SG_METER_TO_FEET
275 + get_Sea_level_radius() );
277 Atmosphere->SetExTemperature(get_Static_temperature());
278 Atmosphere->SetExPressure(get_Static_pressure());
279 Atmosphere->SetExDensity(get_Density());
280 Atmosphere->SetWindNED(get_V_north_airmass(),
281 get_V_east_airmass(),
282 get_V_down_airmass());
283 // SG_LOG(SG_FLIGHT,SG_INFO, "Wind NED: "
284 // << get_V_north_airmass() << ", "
285 // << get_V_east_airmass() << ", "
286 // << get_V_down_airmass() );
291 /******************************************************************************/
293 // Convert from the JSBsim generic_ struct to the FGInterface struct
295 bool FGJSBsim::copy_from_JSBsim() {
298 _set_Inertias( MassBalance->GetMass(),
299 MassBalance->GetIxx(),
300 MassBalance->GetIyy(),
301 MassBalance->GetIzz(),
302 MassBalance->GetIxz() );
304 _set_CG_Position( MassBalance->GetXYZcg(1),
305 MassBalance->GetXYZcg(2),
306 MassBalance->GetXYZcg(3) );
308 _set_Accels_Body( Translation->GetUVWdot(1),
309 Translation->GetUVWdot(2),
310 Translation->GetUVWdot(3) );
312 _set_Accels_CG_Body( Translation->GetUVWdot(1),
313 Translation->GetUVWdot(2),
314 Translation->GetUVWdot(3) );
316 //_set_Accels_CG_Body_N ( Translation->GetNcg(1),
317 // Translation->GetNcg(2),
318 // Translation->GetNcg(3) );
320 _set_Accels_Pilot_Body( Auxiliary->GetPilotAccel(1),
321 Auxiliary->GetPilotAccel(2),
322 Auxiliary->GetPilotAccel(3) );
324 //_set_Accels_Pilot_Body_N( Auxiliary->GetNpilot(1),
325 // Auxiliary->GetNpilot(2),
326 // Auxiliary->GetNpilot(3) );
328 _set_Nlf( Aerodynamics->GetNlf() );
332 _set_Velocities_Local( Position->GetVn(),
336 _set_Velocities_Wind_Body( Translation->GetUVW(1),
337 Translation->GetUVW(2),
338 Translation->GetUVW(3) );
340 _set_V_rel_wind( Translation->GetVt() );
342 _set_V_equiv_kts( Auxiliary->GetVequivalentKTS() );
344 // _set_V_calibrated( Auxiliary->GetVcalibratedFPS() );
346 _set_V_calibrated_kts( Auxiliary->GetVcalibratedKTS() );
348 _set_V_ground_speed( Position->GetVground() );
350 _set_Omega_Body( Rotation->GetPQR(1),
352 Rotation->GetPQR(3) );
354 _set_Euler_Rates( Rotation->GetEulerRates(1),
355 Rotation->GetEulerRates(2),
356 Rotation->GetEulerRates(3) );
358 _set_Geocentric_Rates(Position->GetLatitudeDot(),
359 Position->GetLongitudeDot(),
360 Position->Gethdot() );
362 _set_Mach_number( Translation->GetMach() );
365 _updatePosition( Position->GetLatitude(),
366 Position->GetLongitude(),
369 _set_Euler_Angles( Rotation->Getphi(),
371 Rotation->Getpsi() );
373 _set_Alpha( Translation->Getalpha() );
374 _set_Beta( Translation->Getbeta() );
377 _set_Gamma_vert_rad( Position->GetGamma() );
378 // set_Gamma_horiz_rad( Gamma_horiz_rad );
380 _set_Earth_position_angle( Auxiliary->GetEarthPositionAngle() );
382 _set_Climb_Rate( Position->Gethdot() );
385 for ( i = 1; i <= 3; i++ ) {
386 for ( j = 1; j <= 3; j++ ) {
387 _set_T_Local_to_Body( i, j, State->GetTl2b(i,j) );
393 void FGJSBsim::snap_shot(void) {
394 fgic->SetLatitudeRadIC(get_Lat_geocentric() );
395 fgic->SetLongitudeRadIC( get_Longitude() );
396 fgic->SetAltitudeFtIC( get_Altitude() );
397 fgic->SetTerrainAltitudeFtIC( get_Runway_altitude() );
398 fgic->SetVtrueFpsIC( get_V_rel_wind() );
399 fgic->SetPitchAngleRadIC( get_Theta() );
400 fgic->SetRollAngleRadIC( get_Phi() );
401 fgic->SetTrueHeadingRadIC( get_Psi() );
402 fgic->SetClimbRateFpsIC( get_Climb_Rate() );
406 bool FGJSBsim::ToggleDataLogging(void) {
407 return fdmex->GetOutput()->Toggle();
411 bool FGJSBsim::ToggleDataLogging(bool state) {
413 fdmex->GetOutput()->Enable();
416 fdmex->GetOutput()->Disable();
423 void FGJSBsim::set_Latitude(double lat) {
424 double sea_level_radius_meters,lat_geoc;
426 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat );
429 sgGeodToGeoc( lat, get_Altitude() , &sea_level_radius_meters, &lat_geoc);
430 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
431 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
432 fgic->SetLatitudeRadIC( lat_geoc );
433 fdmex->RunIC(fgic); //loop JSBSim once
434 copy_from_JSBsim(); //update the bus
438 void FGJSBsim::set_Longitude(double lon) {
440 SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon );
443 fgic->SetLongitudeRadIC(lon);
444 fdmex->RunIC(fgic); //loop JSBSim once
445 copy_from_JSBsim(); //update the bus
449 void FGJSBsim::set_Altitude(double alt) {
450 double sea_level_radius_meters,lat_geoc;
452 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt );
455 sgGeodToGeoc( get_Latitude(), alt , &sea_level_radius_meters, &lat_geoc);
456 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
457 fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
458 fgic->SetLatitudeRadIC( lat_geoc );
459 fgic->SetAltitudeFtIC(alt);
460 fdmex->RunIC(fgic); //loop JSBSim once
461 copy_from_JSBsim(); //update the bus
465 void FGJSBsim::set_V_calibrated_kts(double vc) {
466 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " << vc );
469 fgic->SetVcalibratedKtsIC(vc);
470 fdmex->RunIC(fgic); //loop JSBSim once
471 copy_from_JSBsim(); //update the bus
475 void FGJSBsim::set_Mach_number(double mach) {
476 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " << mach );
479 fgic->SetMachIC(mach);
480 fdmex->RunIC(fgic); //loop JSBSim once
481 copy_from_JSBsim(); //update the bus
485 void FGJSBsim::set_Velocities_Local( double north, double east, double down ){
486 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: "
487 << north << ", " << east << ", " << down );
490 fgic->SetVnorthFpsIC(north);
491 fgic->SetVeastFpsIC(east);
492 fgic->SetVdownFpsIC(down);
493 fdmex->RunIC(fgic); //loop JSBSim once
494 copy_from_JSBsim(); //update the bus
498 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w){
499 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
500 << u << ", " << v << ", " << w );
503 fgic->SetUBodyFpsIC(u);
504 fgic->SetVBodyFpsIC(v);
505 fgic->SetWBodyFpsIC(w);
506 fdmex->RunIC(fgic); //loop JSBSim once
507 copy_from_JSBsim(); //update the bus
512 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi ) {
513 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: "
514 << phi << ", " << theta << ", " << psi );
517 fgic->SetPitchAngleRadIC(theta);
518 fgic->SetRollAngleRadIC(phi);
519 fgic->SetTrueHeadingRadIC(psi);
520 fdmex->RunIC(fgic); //loop JSBSim once
521 copy_from_JSBsim(); //update the bus
526 void FGJSBsim::set_Climb_Rate( double roc) {
527 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
530 fgic->SetClimbRateFpsIC(roc);
531 fdmex->RunIC(fgic); //loop JSBSim once
532 copy_from_JSBsim(); //update the bus
536 void FGJSBsim::set_Gamma_vert_rad( double gamma) {
537 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
540 fgic->SetFlightPathAngleRadIC(gamma);
541 fdmex->RunIC(fgic); //loop JSBSim once
542 copy_from_JSBsim(); //update the bus
547 void FGJSBsim::set_Sea_level_radius(double slr) {
548 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Sea_level_radius: " << slr );
551 fgic->SetSeaLevelRadiusFtIC(slr);
552 fdmex->RunIC(fgic); //loop JSBSim once
553 copy_from_JSBsim(); //update the bus
557 void FGJSBsim::set_Runway_altitude(double ralt) {
558 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Runway_altitude: " << ralt );
561 _set_Runway_altitude( ralt );
562 fgic->SetTerrainAltitudeFtIC( ralt );
563 fdmex->RunIC(fgic); //loop JSBSim once
564 copy_from_JSBsim(); //update the bus
568 void FGJSBsim::set_Static_pressure(double p) {
569 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_pressure: " << p );
572 Atmosphere->SetExPressure(p);
573 if(Atmosphere->External() == true)
577 void FGJSBsim::set_Static_temperature(double T) {
578 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_temperature: " << T );
581 Atmosphere->SetExTemperature(T);
582 if(Atmosphere->External() == true)
587 void FGJSBsim::set_Density(double rho) {
588 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Density: " << rho );
591 Atmosphere->SetExDensity(rho);
592 if(Atmosphere->External() == true)
597 void FGJSBsim::set_Velocities_Local_Airmass (double wnorth,
600 SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local_Airmass: "
601 << wnorth << ", " << weast << ", " << wdown );
603 _set_Velocities_Local_Airmass( wnorth, weast, wdown );
605 Atmosphere->SetWindNED(wnorth, weast, wdown );
606 if(Atmosphere->External() == true)