1998-09-29 14:56:30 +00:00
|
|
|
// flight.c -- a general interface to the various flight models
|
|
|
|
//
|
|
|
|
// Written by Curtis Olson, started May 1997.
|
|
|
|
//
|
|
|
|
// Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
1997-05-29 02:35:04 +00:00
|
|
|
|
1997-05-29 22:39:49 +00:00
|
|
|
#include <stdio.h>
|
1998-04-18 04:13:51 +00:00
|
|
|
|
1998-11-06 21:17:31 +00:00
|
|
|
#include <Debug/logstream.hxx>
|
1999-02-01 21:33:23 +00:00
|
|
|
#include <FDM/External/external.hxx>
|
|
|
|
#include <FDM/LaRCsim/ls_interface.h>
|
1998-07-12 03:08:27 +00:00
|
|
|
#include <Include/fg_constants.h>
|
1999-09-01 20:24:54 +00:00
|
|
|
#include <Main/options.hxx>
|
1998-10-16 23:26:44 +00:00
|
|
|
#include <Math/fg_geodesy.hxx>
|
1999-01-19 17:52:06 +00:00
|
|
|
#include <Time/timestamp.hxx>
|
1997-05-29 02:35:04 +00:00
|
|
|
|
1999-02-26 22:09:10 +00:00
|
|
|
#include "flight.hxx"
|
|
|
|
#include "JSBsim.hxx"
|
|
|
|
#include "LaRCsim.hxx"
|
|
|
|
|
1998-02-07 15:29:31 +00:00
|
|
|
|
1999-01-08 03:23:51 +00:00
|
|
|
// base_fdm_state is the internal state that is updated in integer
|
|
|
|
// multiples of "dt". This leads to "jitter" with respect to the real
|
|
|
|
// world time, so we introduce cur_fdm_state which is extrapolated by
|
|
|
|
// the difference between sim time and real world time
|
|
|
|
|
1999-02-05 21:28:09 +00:00
|
|
|
FGInterface cur_fdm_state;
|
|
|
|
FGInterface base_fdm_state;
|
1998-02-07 15:29:31 +00:00
|
|
|
|
|
|
|
|
1999-01-19 17:52:06 +00:00
|
|
|
// Extrapolate fdm based on time_offset (in usec)
|
1999-02-05 21:28:09 +00:00
|
|
|
void FGInterface::extrapolate( int time_offset ) {
|
1999-01-19 17:52:06 +00:00
|
|
|
double dt = time_offset / 1000000.0;
|
1999-04-27 19:27:45 +00:00
|
|
|
|
|
|
|
// -dw- metrowerks complains about ambiguous access, not critical
|
|
|
|
// to keep this ;)
|
|
|
|
#ifndef __MWERKS__
|
1999-01-19 17:52:06 +00:00
|
|
|
cout << "extrapolating FDM by dt = " << dt << endl;
|
1999-04-27 19:27:45 +00:00
|
|
|
#endif
|
1999-01-19 17:52:06 +00:00
|
|
|
|
|
|
|
double lat = geodetic_position_v[0] + geocentric_rates_v[0] * dt;
|
|
|
|
double lat_geoc = geocentric_position_v[0] + geocentric_rates_v[0] * dt;
|
|
|
|
|
|
|
|
double lon = geodetic_position_v[1] + geocentric_rates_v[1] * dt;
|
|
|
|
double lon_geoc = geocentric_position_v[1] + geocentric_rates_v[1] * dt;
|
|
|
|
|
|
|
|
double alt = geodetic_position_v[2] + geocentric_rates_v[2] * dt;
|
|
|
|
double radius = geocentric_position_v[2] + geocentric_rates_v[2] * dt;
|
|
|
|
|
|
|
|
geodetic_position_v[0] = lat;
|
|
|
|
geocentric_position_v[0] = lat_geoc;
|
|
|
|
|
|
|
|
geodetic_position_v[1] = lon;
|
|
|
|
geocentric_position_v[1] = lon_geoc;
|
|
|
|
|
|
|
|
geodetic_position_v[2] = alt;
|
|
|
|
geocentric_position_v[2] = radius;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-12-04 01:29:37 +00:00
|
|
|
// Initialize the flight model parameters
|
1999-02-05 21:28:09 +00:00
|
|
|
int fgFDMInit(int model, FGInterface& f, double dt) {
|
1998-08-22 14:49:55 +00:00
|
|
|
double save_alt = 0.0;
|
1997-05-29 22:39:49 +00:00
|
|
|
|
1998-11-06 21:17:31 +00:00
|
|
|
FG_LOG( FG_FLIGHT ,FG_INFO, "Initializing flight model" );
|
1997-12-30 20:47:34 +00:00
|
|
|
|
1999-01-08 03:23:51 +00:00
|
|
|
base_fdm_state = f;
|
|
|
|
|
1999-02-05 21:28:09 +00:00
|
|
|
if ( model == FGInterface::FG_SLEW ) {
|
1998-07-30 23:44:36 +00:00
|
|
|
// fgSlewInit(dt);
|
1999-04-27 19:27:45 +00:00
|
|
|
#ifndef __MWERKS__ // -dw- 04/22/99 JSB sim not ported yet
|
1999-02-05 21:28:09 +00:00
|
|
|
} else if ( model == FGInterface::FG_JSBSIM ) {
|
1999-07-31 04:56:13 +00:00
|
|
|
fgJSBsimInit(dt, f);
|
1999-02-05 21:28:09 +00:00
|
|
|
fgJSBsim_2_FGInterface(base_fdm_state);
|
1999-04-27 19:27:45 +00:00
|
|
|
#endif
|
1999-02-05 21:28:09 +00:00
|
|
|
} else if ( model == FGInterface::FG_LARCSIM ) {
|
1998-12-04 01:29:37 +00:00
|
|
|
// lets try to avoid really screwing up the LaRCsim model
|
1999-01-08 03:23:51 +00:00
|
|
|
if ( base_fdm_state.get_Altitude() < -9000.0 ) {
|
|
|
|
save_alt = base_fdm_state.get_Altitude();
|
|
|
|
base_fdm_state.set_Altitude( 0.0 );
|
1998-08-22 14:49:55 +00:00
|
|
|
}
|
|
|
|
|
1999-01-08 03:23:51 +00:00
|
|
|
// translate FG to LaRCsim structure
|
1999-02-05 21:28:09 +00:00
|
|
|
FGInterface_2_LaRCsim(base_fdm_state);
|
1999-01-08 03:23:51 +00:00
|
|
|
|
|
|
|
// initialize LaRCsim
|
1997-05-29 22:39:49 +00:00
|
|
|
fgLaRCsimInit(dt);
|
1999-01-08 03:23:51 +00:00
|
|
|
|
|
|
|
FG_LOG( FG_FLIGHT, FG_INFO, "FG pos = " <<
|
|
|
|
base_fdm_state.get_Latitude() );
|
|
|
|
|
|
|
|
// translate LaRCsim back to FG structure
|
1999-02-05 21:28:09 +00:00
|
|
|
fgLaRCsim_2_FGInterface(base_fdm_state);
|
1998-08-22 14:49:55 +00:00
|
|
|
|
1998-12-04 01:29:37 +00:00
|
|
|
// but lets restore our original bogus altitude when we are done
|
1998-12-03 01:14:58 +00:00
|
|
|
if ( save_alt < -9000.0 ) {
|
1999-01-08 03:23:51 +00:00
|
|
|
base_fdm_state.set_Altitude( save_alt );
|
1998-08-22 14:49:55 +00:00
|
|
|
}
|
1999-02-05 21:28:09 +00:00
|
|
|
} else if ( model == FGInterface::FG_EXTERNAL ) {
|
1999-01-19 17:52:06 +00:00
|
|
|
fgExternalInit(base_fdm_state);
|
1997-05-29 22:39:49 +00:00
|
|
|
} else {
|
1998-11-06 21:17:31 +00:00
|
|
|
FG_LOG( FG_FLIGHT, FG_WARN,
|
1999-04-03 04:20:01 +00:00
|
|
|
"Unimplemented flight model == " << model );
|
1997-05-29 22:39:49 +00:00
|
|
|
}
|
|
|
|
|
1999-01-19 17:52:06 +00:00
|
|
|
// set valid time for this record
|
|
|
|
base_fdm_state.stamp_time();
|
|
|
|
|
1999-01-08 03:23:51 +00:00
|
|
|
f = base_fdm_state;
|
1998-01-19 18:35:40 +00:00
|
|
|
|
1999-01-08 03:23:51 +00:00
|
|
|
return 1;
|
1997-05-29 22:39:49 +00:00
|
|
|
}
|
1997-05-29 02:35:04 +00:00
|
|
|
|
|
|
|
|
1998-12-04 01:29:37 +00:00
|
|
|
// Run multiloop iterations of the flight model
|
1999-02-05 21:28:09 +00:00
|
|
|
int fgFDMUpdate(int model, FGInterface& f, int multiloop, int time_offset) {
|
1998-09-29 02:02:40 +00:00
|
|
|
double time_step, start_elev, end_elev;
|
1999-01-08 03:23:51 +00:00
|
|
|
|
1998-07-12 03:08:27 +00:00
|
|
|
// printf("Altitude = %.2f\n", FG_Altitude * 0.3048);
|
|
|
|
|
1999-01-19 17:52:06 +00:00
|
|
|
// set valid time for this record
|
|
|
|
base_fdm_state.stamp_time();
|
1999-01-08 03:23:51 +00:00
|
|
|
|
1999-09-01 20:24:54 +00:00
|
|
|
time_step = (1.0 / current_options.get_model_hz()) * multiloop;
|
1999-01-08 03:23:51 +00:00
|
|
|
start_elev = base_fdm_state.get_Altitude();
|
1998-09-29 02:02:40 +00:00
|
|
|
|
1999-02-05 21:28:09 +00:00
|
|
|
if ( model == FGInterface::FG_SLEW ) {
|
1998-07-30 23:44:36 +00:00
|
|
|
// fgSlewUpdate(f, multiloop);
|
1999-04-27 19:27:45 +00:00
|
|
|
#ifndef __MWERKS__ // -dw- 04/22/99 JSB sim not ported yet
|
1999-02-05 21:28:09 +00:00
|
|
|
} else if ( model == FGInterface::FG_JSBSIM ) {
|
|
|
|
fgJSBsimUpdate(base_fdm_state, multiloop);
|
|
|
|
f = base_fdm_state;
|
1999-04-27 19:27:45 +00:00
|
|
|
#endif
|
1999-02-05 21:28:09 +00:00
|
|
|
} else if ( model == FGInterface::FG_LARCSIM ) {
|
1999-01-08 03:23:51 +00:00
|
|
|
fgLaRCsimUpdate(base_fdm_state, multiloop);
|
1999-01-19 17:52:06 +00:00
|
|
|
// extrapolate position based on actual time
|
|
|
|
// f = extrapolate_fdm( base_fdm_state, time_offset );
|
|
|
|
f = base_fdm_state;
|
1999-02-05 21:28:09 +00:00
|
|
|
} else if ( model == FGInterface::FG_EXTERNAL ) {
|
1998-12-04 01:29:37 +00:00
|
|
|
// fgExternalUpdate(f, multiloop);
|
1999-01-19 17:52:06 +00:00
|
|
|
FGTimeStamp current;
|
|
|
|
current.stamp();
|
|
|
|
f = base_fdm_state;
|
|
|
|
f.extrapolate( current - base_fdm_state.get_time_stamp() );
|
1997-05-29 22:39:49 +00:00
|
|
|
} else {
|
1998-11-06 21:17:31 +00:00
|
|
|
FG_LOG( FG_FLIGHT, FG_WARN,
|
|
|
|
"Unimplemented flight model == " << model );
|
1997-05-29 22:39:49 +00:00
|
|
|
}
|
|
|
|
|
1999-01-08 03:23:51 +00:00
|
|
|
end_elev = base_fdm_state.get_Altitude();
|
1998-09-29 02:02:40 +00:00
|
|
|
|
1998-12-18 23:37:06 +00:00
|
|
|
if ( time_step > 0.0 ) {
|
|
|
|
// feet per second
|
1999-01-08 03:23:51 +00:00
|
|
|
base_fdm_state.set_Climb_Rate( (end_elev - start_elev) / time_step );
|
1998-12-18 23:37:06 +00:00
|
|
|
}
|
1998-09-29 02:02:40 +00:00
|
|
|
|
1999-01-08 03:23:51 +00:00
|
|
|
return 1;
|
1997-05-29 22:39:49 +00:00
|
|
|
}
|
1997-05-29 02:35:04 +00:00
|
|
|
|
|
|
|
|
1998-12-04 01:29:37 +00:00
|
|
|
// Set the altitude (force)
|
1999-01-20 13:42:22 +00:00
|
|
|
void fgFDMForceAltitude(int model, double alt_meters) {
|
1998-07-12 03:08:27 +00:00
|
|
|
double sea_level_radius_meters;
|
|
|
|
double lat_geoc;
|
1999-01-08 03:23:51 +00:00
|
|
|
|
1998-07-12 03:08:27 +00:00
|
|
|
// Set the FG variables first
|
1999-01-08 03:23:51 +00:00
|
|
|
fgGeodToGeoc( base_fdm_state.get_Latitude(), alt_meters,
|
1998-07-12 03:08:27 +00:00
|
|
|
&sea_level_radius_meters, &lat_geoc);
|
|
|
|
|
1999-01-08 03:23:51 +00:00
|
|
|
base_fdm_state.set_Altitude( alt_meters * METER_TO_FEET );
|
|
|
|
base_fdm_state.set_Radius_to_vehicle( base_fdm_state.get_Altitude() +
|
|
|
|
(sea_level_radius_meters *
|
|
|
|
METER_TO_FEET) );
|
1998-07-12 03:08:27 +00:00
|
|
|
|
1998-12-04 01:29:37 +00:00
|
|
|
// additional work needed for some flight models
|
1999-02-05 21:28:09 +00:00
|
|
|
if ( model == FGInterface::FG_LARCSIM ) {
|
1999-01-08 03:23:51 +00:00
|
|
|
ls_ForceAltitude( base_fdm_state.get_Altitude() );
|
1998-07-12 03:08:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-01-20 13:42:22 +00:00
|
|
|
// Set the local ground elevation
|
|
|
|
void fgFDMSetGroundElevation(int model, double ground_meters) {
|
|
|
|
base_fdm_state.set_Runway_altitude( ground_meters * METER_TO_FEET );
|
1999-01-27 04:48:39 +00:00
|
|
|
cur_fdm_state.set_Runway_altitude( ground_meters * METER_TO_FEET );
|
1999-01-20 13:42:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|