2002-04-25 16:23:16 +00:00
|
|
|
// UFO.cxx -- interface to the "UFO" flight model
|
|
|
|
//
|
|
|
|
// Written by Curtis Olson, started October 1999.
|
|
|
|
// Slightly modified from MagicCarpet.cxx by Jonathan Polley, April 2002
|
|
|
|
//
|
2004-11-19 22:10:41 +00:00
|
|
|
// Copyright (C) 1999-2002 Curtis L. Olson - http://www.flightgear.org/~curt
|
2002-04-25 16:23:16 +00:00
|
|
|
//
|
|
|
|
// 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
|
2006-02-21 01:16:04 +00:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-04-25 16:23:16 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
|
2005-10-22 11:22:05 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2002-04-25 16:23:16 +00:00
|
|
|
#include <simgear/math/sg_geodesy.hxx>
|
|
|
|
|
2005-11-01 13:41:49 +00:00
|
|
|
#include <Aircraft/controls.hxx>
|
2002-04-25 16:23:16 +00:00
|
|
|
#include <Main/globals.hxx>
|
|
|
|
#include <Main/fg_props.hxx>
|
|
|
|
|
|
|
|
#include "UFO.hxx"
|
|
|
|
|
2007-07-28 08:08:22 +00:00
|
|
|
double FGUFO::lowpass::_dt;
|
|
|
|
|
2007-07-27 21:57:55 +00:00
|
|
|
|
|
|
|
FGUFO::FGUFO( double dt ) :
|
2007-07-28 08:08:22 +00:00
|
|
|
Throttle(new lowpass(fgGetDouble("/controls/damping/throttle", 0.1))),
|
2007-07-27 21:57:55 +00:00
|
|
|
Aileron(new lowpass(fgGetDouble("/controls/damping/aileron", 0.65))),
|
|
|
|
Elevator(new lowpass(fgGetDouble("/controls/damping/elevator", 0.65))),
|
|
|
|
Rudder(new lowpass(fgGetDouble("/controls/damping/rudder", 0.05))),
|
|
|
|
Aileron_Trim(new lowpass(fgGetDouble("/controls/damping/aileron-trim", 0.65))),
|
|
|
|
Elevator_Trim(new lowpass(fgGetDouble("/controls/damping/elevator-trim", 0.65))),
|
|
|
|
Rudder_Trim(new lowpass(fgGetDouble("/controls/damping/rudder-trim", 0.05))),
|
2006-03-11 09:23:51 +00:00
|
|
|
Speed_Max(fgGetNode("/engines/engine/speed-max-mps", true))
|
2002-05-09 21:56:31 +00:00
|
|
|
{
|
2002-04-25 16:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FGUFO::~FGUFO() {
|
2007-07-27 21:57:55 +00:00
|
|
|
delete Throttle;
|
|
|
|
delete Aileron;
|
|
|
|
delete Elevator;
|
|
|
|
delete Rudder;
|
|
|
|
delete Aileron_Trim;
|
|
|
|
delete Elevator_Trim;
|
|
|
|
delete Rudder_Trim;
|
2002-04-25 16:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize the UFO flight model, dt is the time increment
|
|
|
|
// for each subsequent iteration through the EOM
|
|
|
|
void FGUFO::init() {
|
|
|
|
common_init();
|
2006-03-11 09:23:51 +00:00
|
|
|
if (Speed_Max->getDoubleValue() < 0.01)
|
|
|
|
Speed_Max->setDoubleValue(2000.0);
|
2002-04-25 16:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Run an iteration of the EOM (equations of motion)
|
2002-05-11 16:28:50 +00:00
|
|
|
void FGUFO::update( double dt ) {
|
2002-04-25 16:23:16 +00:00
|
|
|
|
2002-05-11 16:28:50 +00:00
|
|
|
if (is_suspended())
|
2007-07-27 21:57:55 +00:00
|
|
|
return;
|
2002-05-11 16:28:50 +00:00
|
|
|
|
2007-07-28 08:08:22 +00:00
|
|
|
lowpass::set_delta(dt);
|
2002-05-11 21:09:53 +00:00
|
|
|
double time_step = dt;
|
2007-07-29 17:36:30 +00:00
|
|
|
FGControls *ctrl = globals->get_controls();
|
2002-04-25 16:23:16 +00:00
|
|
|
|
|
|
|
// read the throttle
|
2007-07-29 17:36:30 +00:00
|
|
|
double throttle = ctrl->get_throttle( 0 );
|
|
|
|
double brake_left = ctrl->get_brake_left();
|
|
|
|
double brake_right = ctrl->get_brake_right();
|
2007-07-27 21:57:55 +00:00
|
|
|
|
|
|
|
if (brake_left > 0.5 || brake_right > 0.5)
|
|
|
|
throttle = -throttle;
|
|
|
|
|
2007-07-29 17:36:30 +00:00
|
|
|
double velocity = Throttle->filter(throttle) * Speed_Max->getDoubleValue(); // meters/sec
|
2007-07-27 21:57:55 +00:00
|
|
|
|
|
|
|
|
2007-07-29 17:36:30 +00:00
|
|
|
// read and lowpass-filter the state of the control surfaces
|
|
|
|
double aileron = Aileron->filter(ctrl->get_aileron());
|
|
|
|
double elevator = Elevator->filter(ctrl->get_elevator());
|
|
|
|
double rudder = Rudder->filter(ctrl->get_rudder());
|
2007-07-27 21:57:55 +00:00
|
|
|
|
2007-07-29 17:36:30 +00:00
|
|
|
aileron += Aileron_Trim->filter(ctrl->get_aileron_trim());
|
|
|
|
elevator += Elevator_Trim->filter(ctrl->get_elevator_trim());
|
|
|
|
rudder += Rudder_Trim->filter(ctrl->get_rudder_trim());
|
2002-04-25 16:23:16 +00:00
|
|
|
|
|
|
|
double old_pitch = get_Theta();
|
2007-07-27 21:57:55 +00:00
|
|
|
double pitch_rate = SGD_PI_4; // assume I will be pitching up
|
|
|
|
double target_pitch = -elevator * SGD_PI_2;
|
2002-04-25 16:23:16 +00:00
|
|
|
|
2007-07-27 21:57:55 +00:00
|
|
|
if (old_pitch > target_pitch) // pitching down
|
2002-04-25 16:23:16 +00:00
|
|
|
pitch_rate *= -1;
|
|
|
|
|
|
|
|
double pitch = old_pitch + (pitch_rate * time_step);
|
|
|
|
|
2007-07-27 21:57:55 +00:00
|
|
|
if (pitch_rate > 0.0) { // pitching up
|
|
|
|
if (pitch > target_pitch)
|
2002-04-25 16:23:16 +00:00
|
|
|
pitch = target_pitch;
|
2007-07-27 21:57:55 +00:00
|
|
|
|
|
|
|
} else if (pitch_rate < 0.0) { // pitching down
|
|
|
|
if (pitch < target_pitch)
|
2002-04-25 16:23:16 +00:00
|
|
|
pitch = target_pitch;
|
|
|
|
}
|
|
|
|
|
2007-07-27 21:57:55 +00:00
|
|
|
double old_roll = get_Phi();
|
|
|
|
double roll_rate = SGD_PI_4;
|
|
|
|
double target_roll = aileron * SGD_PI_2;
|
2002-04-25 16:23:16 +00:00
|
|
|
|
|
|
|
if (old_roll > target_roll)
|
|
|
|
roll_rate *= -1;
|
2007-07-27 21:57:55 +00:00
|
|
|
|
2002-04-25 16:23:16 +00:00
|
|
|
double roll = old_roll + (roll_rate * time_step);
|
|
|
|
|
2007-07-27 21:57:55 +00:00
|
|
|
if (roll_rate > 0.0) { // rolling CW
|
|
|
|
if (roll > target_roll)
|
2002-04-25 16:23:16 +00:00
|
|
|
roll = target_roll;
|
2007-07-27 21:57:55 +00:00
|
|
|
|
|
|
|
} else if (roll_rate < 0.0) { // rolling CCW
|
|
|
|
if (roll < target_roll)
|
2002-04-25 16:23:16 +00:00
|
|
|
roll = target_roll;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the vertical speed of the aircraft
|
|
|
|
double real_climb_rate = sin (pitch) * SG_METER_TO_FEET * velocity; // feet/sec
|
2007-07-27 21:57:55 +00:00
|
|
|
_set_Climb_Rate( -elevator * 10.0 );
|
2002-04-25 16:23:16 +00:00
|
|
|
double climb = real_climb_rate * time_step;
|
|
|
|
|
|
|
|
// the lateral speed of the aircraft
|
|
|
|
double speed = cos (pitch) * velocity; // meters/sec
|
|
|
|
double dist = speed * time_step;
|
|
|
|
double kts = velocity * 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 = sin(roll) * SGD_PI_4; // radians/sec
|
|
|
|
double turn = turn_rate * time_step;
|
2013-06-25 20:47:51 +00:00
|
|
|
double yaw = fabs(rudder) < .05 ? 0.0 : (rudder * (fabs(rudder) - 0.05) / 10);
|
2002-04-25 16:23:16 +00:00
|
|
|
|
|
|
|
// update (lon/lat) position
|
2009-08-24 15:54:48 +00:00
|
|
|
double lat2 = 0.0, lon2 = 0.0, az2 = 0.0;
|
2002-05-09 21:56:31 +00:00
|
|
|
if ( fabs(speed) > SG_EPSILON ) {
|
2007-07-27 21:57:55 +00:00
|
|
|
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 );
|
|
|
|
|
2010-01-02 18:29:01 +00:00
|
|
|
_set_Geodetic_Position( lat2 * SGD_DEGREES_TO_RADIANS, lon2 * SGD_DEGREES_TO_RADIANS );
|
2002-04-25 16:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 euler angles
|
2015-07-08 09:14:46 +00:00
|
|
|
double heading = SGMiscd::normalizePeriodic(0, SGD_2PI, get_Psi() + turn + yaw );
|
2007-06-27 15:28:23 +00:00
|
|
|
_set_Euler_Angles(roll, pitch, heading);
|
2002-04-25 16:23:16 +00:00
|
|
|
_set_Euler_Rates(0,0,0);
|
|
|
|
|
2007-07-27 21:57:55 +00:00
|
|
|
_set_Geocentric_Position( lat_geoc, get_Longitude(),
|
|
|
|
sl_radius + get_Altitude() + climb );
|
2002-04-25 16:23:16 +00:00
|
|
|
// cout << "sea level radius (ft) = " << sl_radius << endl;
|
|
|
|
// cout << "(setto) sea level radius (ft) = " << get_Sea_level_radius() << endl;
|
2005-10-08 12:33:06 +00:00
|
|
|
_update_ground_elev_at_pos();
|
2002-04-25 16:23:16 +00:00
|
|
|
_set_Sea_level_radius( sl_radius * SG_METER_TO_FEET);
|
|
|
|
_set_Altitude( get_Altitude() + climb );
|
2006-07-06 14:24:14 +00:00
|
|
|
_set_Altitude_AGL( get_Altitude() - get_Runway_altitude() );
|
2007-06-27 15:28:23 +00:00
|
|
|
|
|
|
|
set_V_north(cos(heading) * velocity * SG_METER_TO_FEET);
|
|
|
|
set_V_east(sin(heading) * velocity * SG_METER_TO_FEET);
|
|
|
|
set_V_down(-real_climb_rate);
|
2002-04-25 16:23:16 +00:00
|
|
|
}
|
2007-07-27 21:57:55 +00:00
|
|
|
|
|
|
|
|