| 1 | // UFO.cxx -- interface to the "UFO" flight model |
| 2 | // |
| 3 | // Written by Curtis Olson, started October 1999. |
| 4 | // Slightly modified from MagicCarpet.cxx by Jonathan Polley, April 2002 |
| 5 | // |
| 6 | // Copyright (C) 1999-2002 Curtis L. Olson - http://www.flightgear.org/~curt |
| 7 | // |
| 8 | // This program is free software; you can redistribute it and/or |
| 9 | // modify it under the terms of the GNU General Public License as |
| 10 | // published by the Free Software Foundation; either version 2 of the |
| 11 | // License, or (at your option) any later version. |
| 12 | // |
| 13 | // This program is distributed in the hope that it will be useful, but |
| 14 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | // General Public License for more details. |
| 17 | // |
| 18 | // You should have received a copy of the GNU General Public License |
| 19 | // along with this program; if not, write to the Free Software |
| 20 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 | // |
| 22 | |
| 23 | |
| 24 | #ifdef HAVE_CONFIG_H |
| 25 | # include <config.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <simgear/math/sg_geodesy.hxx> |
| 29 | |
| 30 | #include <Aircraft/controls.hxx> |
| 31 | #include <Main/globals.hxx> |
| 32 | #include <Main/fg_props.hxx> |
| 33 | |
| 34 | #include "UFO.hxx" |
| 35 | |
| 36 | double FGUFO::lowpass::_dt; |
| 37 | |
| 38 | |
| 39 | FGUFO::FGUFO( double dt ) : |
| 40 | Throttle(new lowpass(fgGetDouble("/controls/damping/throttle", 0.1))), |
| 41 | Aileron(new lowpass(fgGetDouble("/controls/damping/aileron", 0.65))), |
| 42 | Elevator(new lowpass(fgGetDouble("/controls/damping/elevator", 0.65))), |
| 43 | Rudder(new lowpass(fgGetDouble("/controls/damping/rudder", 0.05))), |
| 44 | Aileron_Trim(new lowpass(fgGetDouble("/controls/damping/aileron-trim", 0.65))), |
| 45 | Elevator_Trim(new lowpass(fgGetDouble("/controls/damping/elevator-trim", 0.65))), |
| 46 | Rudder_Trim(new lowpass(fgGetDouble("/controls/damping/rudder-trim", 0.05))), |
| 47 | Speed_Max(fgGetNode("/engines/engine/speed-max-mps", true)) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | |
| 52 | FGUFO::~FGUFO() { |
| 53 | delete Throttle; |
| 54 | delete Aileron; |
| 55 | delete Elevator; |
| 56 | delete Rudder; |
| 57 | delete Aileron_Trim; |
| 58 | delete Elevator_Trim; |
| 59 | delete Rudder_Trim; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | // Initialize the UFO flight model, dt is the time increment |
| 64 | // for each subsequent iteration through the EOM |
| 65 | void FGUFO::init() { |
| 66 | common_init(); |
| 67 | if (Speed_Max->getDoubleValue() < 0.01) |
| 68 | Speed_Max->setDoubleValue(2000.0); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | // Run an iteration of the EOM (equations of motion) |
| 73 | void FGUFO::update( double dt ) { |
| 74 | |
| 75 | if (is_suspended()) |
| 76 | return; |
| 77 | |
| 78 | lowpass::set_delta(dt); |
| 79 | double time_step = dt; |
| 80 | FGControls *ctrl = globals->get_controls(); |
| 81 | |
| 82 | // read the throttle |
| 83 | double throttle = ctrl->get_throttle( 0 ); |
| 84 | double brake_left = ctrl->get_brake_left(); |
| 85 | double brake_right = ctrl->get_brake_right(); |
| 86 | |
| 87 | if (brake_left > 0.5 || brake_right > 0.5) |
| 88 | throttle = -throttle; |
| 89 | |
| 90 | double velocity = Throttle->filter(throttle) * Speed_Max->getDoubleValue(); // meters/sec |
| 91 | |
| 92 | |
| 93 | // read and lowpass-filter the state of the control surfaces |
| 94 | double aileron = Aileron->filter(ctrl->get_aileron()); |
| 95 | double elevator = Elevator->filter(ctrl->get_elevator()); |
| 96 | double rudder = Rudder->filter(ctrl->get_rudder()); |
| 97 | |
| 98 | aileron += Aileron_Trim->filter(ctrl->get_aileron_trim()); |
| 99 | elevator += Elevator_Trim->filter(ctrl->get_elevator_trim()); |
| 100 | rudder += Rudder_Trim->filter(ctrl->get_rudder_trim()); |
| 101 | |
| 102 | double old_pitch = get_Theta(); |
| 103 | double pitch_rate = SGD_PI_4; // assume I will be pitching up |
| 104 | double target_pitch = -elevator * SGD_PI_2; |
| 105 | |
| 106 | if (old_pitch > target_pitch) // pitching down |
| 107 | pitch_rate *= -1; |
| 108 | |
| 109 | double pitch = old_pitch + (pitch_rate * time_step); |
| 110 | |
| 111 | if (pitch_rate > 0.0) { // pitching up |
| 112 | if (pitch > target_pitch) |
| 113 | pitch = target_pitch; |
| 114 | |
| 115 | } else if (pitch_rate < 0.0) { // pitching down |
| 116 | if (pitch < target_pitch) |
| 117 | pitch = target_pitch; |
| 118 | } |
| 119 | |
| 120 | double old_roll = get_Phi(); |
| 121 | double roll_rate = SGD_PI_4; |
| 122 | double target_roll = aileron * SGD_PI_2; |
| 123 | |
| 124 | if (old_roll > target_roll) |
| 125 | roll_rate *= -1; |
| 126 | |
| 127 | double roll = old_roll + (roll_rate * time_step); |
| 128 | |
| 129 | if (roll_rate > 0.0) { // rolling CW |
| 130 | if (roll > target_roll) |
| 131 | roll = target_roll; |
| 132 | |
| 133 | } else if (roll_rate < 0.0) { // rolling CCW |
| 134 | if (roll < target_roll) |
| 135 | roll = target_roll; |
| 136 | } |
| 137 | |
| 138 | // the vertical speed of the aircraft |
| 139 | double real_climb_rate = sin (pitch) * SG_METER_TO_FEET * velocity; // feet/sec |
| 140 | _set_Climb_Rate( -elevator * 10.0 ); |
| 141 | double climb = real_climb_rate * time_step; |
| 142 | |
| 143 | // the lateral speed of the aircraft |
| 144 | double speed = cos (pitch) * velocity; // meters/sec |
| 145 | double dist = speed * time_step; |
| 146 | double kts = velocity * SG_METER_TO_NM * 3600.0; |
| 147 | _set_V_equiv_kts( kts ); |
| 148 | _set_V_calibrated_kts( kts ); |
| 149 | set_V_ground_speed_kt( kts ); |
| 150 | |
| 151 | // angle of turn |
| 152 | double turn_rate = sin(roll) * SGD_PI_4; // radians/sec |
| 153 | double turn = turn_rate * time_step; |
| 154 | double yaw = fabs(rudder) < .05 ? 0.0 : (rudder * (fabs(rudder) - 0.05) / 10); |
| 155 | |
| 156 | // update (lon/lat) position |
| 157 | double lat2 = 0.0, lon2 = 0.0, az2 = 0.0; |
| 158 | if ( fabs(speed) > SG_EPSILON ) { |
| 159 | geo_direct_wgs_84 ( get_Altitude(), |
| 160 | get_Latitude() * SGD_RADIANS_TO_DEGREES, |
| 161 | get_Longitude() * SGD_RADIANS_TO_DEGREES, |
| 162 | get_Psi() * SGD_RADIANS_TO_DEGREES, |
| 163 | dist, &lat2, &lon2, &az2 ); |
| 164 | |
| 165 | _set_Geodetic_Position( lat2 * SGD_DEGREES_TO_RADIANS, lon2 * SGD_DEGREES_TO_RADIANS ); |
| 166 | } |
| 167 | |
| 168 | // cout << "lon error = " << fabs(end.x()*SGD_RADIANS_TO_DEGREES - lon2) |
| 169 | // << " lat error = " << fabs(end.y()*SGD_RADIANS_TO_DEGREES - lat2) |
| 170 | // << endl; |
| 171 | |
| 172 | double sl_radius, lat_geoc; |
| 173 | sgGeodToGeoc( get_Latitude(), get_Altitude(), &sl_radius, &lat_geoc ); |
| 174 | |
| 175 | // update euler angles |
| 176 | double heading = SGMiscd::normalizePeriodic(0, SGD_2PI, get_Psi() + turn + yaw ); |
| 177 | _set_Euler_Angles(roll, pitch, heading); |
| 178 | _set_Euler_Rates(0,0,0); |
| 179 | |
| 180 | _set_Geocentric_Position( lat_geoc, get_Longitude(), |
| 181 | sl_radius + get_Altitude() + climb ); |
| 182 | // cout << "sea level radius (ft) = " << sl_radius << endl; |
| 183 | // cout << "(setto) sea level radius (ft) = " << get_Sea_level_radius() << endl; |
| 184 | _update_ground_elev_at_pos(); |
| 185 | _set_Sea_level_radius( sl_radius * SG_METER_TO_FEET); |
| 186 | _set_Altitude( get_Altitude() + climb ); |
| 187 | _set_Altitude_AGL( get_Altitude() - get_Runway_altitude() ); |
| 188 | |
| 189 | set_V_north(cos(heading) * velocity * SG_METER_TO_FEET); |
| 190 | set_V_east(sin(heading) * velocity * SG_METER_TO_FEET); |
| 191 | set_V_down(-real_climb_rate); |
| 192 | |
| 193 | //we assume the ufo only fly along the roll axis, providing a velocity to allow lag compensation |
| 194 | _set_Velocities_Body( velocity * SG_METER_TO_FEET, |
| 195 | 0 , |
| 196 | 0 ); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | // Register the subsystem. |
| 201 | #if 0 |
| 202 | SGSubsystemMgr::Registrant<FGUFO> registrantFGUFO; |
| 203 | #endif |