1
0
Fork 0
flightgear/src/FDM/MagicCarpet.cxx
curt 65f6e343ab From David Megginson:
The files in the attached tarball make the following changes to
FlightGear:

- rename the existing FGInterface::init() method to
FGInterface::_setup to get it out of the way

- move *all* FDM initialization code out of src/Main/fg_init.cxx and
into FGInterface::init(), and clean up fg_init.cxx a little
(especially by removing the zillions of attempts to place the plane on
the ground at various locations in the code)

- modify FGInterface::bind() so that no values are picked up
automatically at bind time (they are set previously by init() instead)

- modify the init() methods of the classes derived from FGInterface
(i.e. larcsim, jsbsim, balloon, magic, and ada) to invoke
FGInterface::init() explicitly before doing their own setup

I don't claim that the code in FGInterface::init() is optimal (or even
correct), but it seems to work for on-ground starts with both LaRCSim
and JSBSim on runways pointing various directions from near sea level
to about 700' ASL (the range I happened to test).  I expect that Jon
and Tony will want to look at the code and refactor and correct it now
that they can see what's going on in one place.

Here's a quick outline of what is invoked:

  cur_fdm_state = new <whatever>(dt);
  cur_fdm_state->init();
  cur_fdm_state->bind();

The constructor allocates memory and sets default values only (with
the help of the FGInterface::_setup() method).  The init() method pull
any required properties out of the property tree and sets up the
initial state of the FDM.  The bind() method takes ownership of
FDM-related properties so that the FDM can publish them to the rest of
the sim.

Note that bind() and init() are virtual, so any implementation in a
subclass will hide the implementation in FGInterface; that's why
subclass implementations of init() and bind() have to invoke
FGInterface::init() and FGInterface::bind() explicitly, probably at
the start, so that they get the basic initialization.
2001-04-05 20:20:44 +00:00

109 lines
3.4 KiB
C++

// MagicCarpet.cxx -- interface to the "Magic Carpet" flight model
//
// Written by Curtis Olson, started October 1999.
//
// Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
//
// 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$
#include <simgear/math/sg_geodesy.hxx>
#include <simgear/math/point3d.hxx>
#include <simgear/math/polar3d.hxx>
#include <Controls/controls.hxx>
#include <Main/globals.hxx>
#include <Main/fg_props.hxx>
#include "MagicCarpet.hxx"
FGMagicCarpet::FGMagicCarpet( double dt ) {
set_delta_t( dt );
}
FGMagicCarpet::~FGMagicCarpet() {
}
// Initialize the Magic Carpet flight model, dt is the time increment
// for each subsequent iteration through the EOM
void FGMagicCarpet::init() {
// explicitly call the superclass's
// init method first
FGInterface::init();
}
// Run an iteration of the EOM (equations of motion)
bool FGMagicCarpet::update( int multiloop ) {
// cout << "FGLaRCsim::update()" << endl;
double time_step = get_delta_t() * multiloop;
// speed and distance traveled
double speed = controls.get_throttle( 0 ) * 2000; // meters/sec
double dist = speed * time_step;
double kts = speed * SG_METER_TO_NM * 3600.0;
_set_V_equiv_kts( kts );
_set_V_calibrated_kts( kts );
_set_V_ground_speed( kts );
// angle of turn
double turn_rate = controls.get_aileron() * SGD_PI_4; // radians/sec
double turn = turn_rate * time_step;
// update euler angles
_set_Euler_Angles( get_Phi(), get_Theta(),
fmod(get_Psi() + turn, SGD_2PI) );
_set_Euler_Rates(0,0,0);
// update (lon/lat) position
double lat2, lon2, az2;
if ( speed > SG_EPSILON ) {
geo_direct_wgs_84 ( get_Altitude(),
get_Latitude() * SGD_RADIANS_TO_DEGREES,
get_Longitude() * SGD_RADIANS_TO_DEGREES,
get_Psi() * SGD_RADIANS_TO_DEGREES,
dist, &lat2, &lon2, &az2 );
_set_Longitude( lon2 * SGD_DEGREES_TO_RADIANS );
_set_Latitude( lat2 * SGD_DEGREES_TO_RADIANS );
}
// cout << "lon error = " << fabs(end.x()*SGD_RADIANS_TO_DEGREES - lon2)
// << " lat error = " << fabs(end.y()*SGD_RADIANS_TO_DEGREES - lat2)
// << endl;
double sl_radius, lat_geoc;
sgGeodToGeoc( get_Latitude(), get_Altitude(), &sl_radius, &lat_geoc );
// update altitude
double real_climb_rate = -controls.get_elevator() * 5000; // feet/sec
_set_Climb_Rate( real_climb_rate / 500.0 );
double climb = real_climb_rate * time_step;
_set_Geocentric_Position( lat_geoc, get_Longitude(),
sl_radius + get_Altitude() + climb );
// cout << "sea level radius (ft) = " << sl_radius << endl;
// cout << "(setto) sea level radius (ft) = " << get_Sea_level_radius() << endl;
_set_Sea_level_radius( sl_radius * SG_METER_TO_FEET);
_set_Altitude( get_Altitude() + climb );
return true;
}