1 // UFO.cxx -- interface to the "UFO" flight model
3 // Written by Curtis Olson, started October 1999.
4 // Slightly modified from MagicCarpet.cxx by Jonathan Polley, April 2002
6 // Copyright (C) 1999-2002 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
28 #include <simgear/math/sg_geodesy.hxx>
30 #include <Aircraft/controls.hxx>
31 #include <Main/globals.hxx>
32 #include <Main/fg_props.hxx>
36 double FGUFO::lowpass::_dt;
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))
63 // Initialize the UFO flight model, dt is the time increment
64 // for each subsequent iteration through the EOM
67 if (Speed_Max->getDoubleValue() < 0.01)
68 Speed_Max->setDoubleValue(2000.0);
72 // Run an iteration of the EOM (equations of motion)
73 void FGUFO::update( double dt ) {
78 lowpass::set_delta(dt);
79 double time_step = dt;
80 FGControls *ctrl = globals->get_controls();
83 double throttle = ctrl->get_throttle( 0 );
84 double brake_left = ctrl->get_brake_left();
85 double brake_right = ctrl->get_brake_right();
87 if (brake_left > 0.5 || brake_right > 0.5)
90 double velocity = Throttle->filter(throttle) * Speed_Max->getDoubleValue(); // meters/sec
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());
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());
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;
106 if (old_pitch > target_pitch) // pitching down
109 double pitch = old_pitch + (pitch_rate * time_step);
111 if (pitch_rate > 0.0) { // pitching up
112 if (pitch > target_pitch)
113 pitch = target_pitch;
115 } else if (pitch_rate < 0.0) { // pitching down
116 if (pitch < target_pitch)
117 pitch = target_pitch;
120 double old_roll = get_Phi();
121 double roll_rate = SGD_PI_4;
122 double target_roll = aileron * SGD_PI_2;
124 if (old_roll > target_roll)
127 double roll = old_roll + (roll_rate * time_step);
129 if (roll_rate > 0.0) { // rolling CW
130 if (roll > target_roll)
133 } else if (roll_rate < 0.0) { // rolling CCW
134 if (roll < target_roll)
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;
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( kts );
152 double turn_rate = sin(roll) * SGD_PI_4; // radians/sec
153 double turn = turn_rate * time_step;
154 double yaw = fabs(rudder) < .2 ? 0.0 : rudder / (25 + fabs(speed) * .1);
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 );
165 _set_Geodetic_Position( lat2 * SGD_DEGREES_TO_RADIANS, lon2 * SGD_DEGREES_TO_RADIANS );
168 // cout << "lon error = " << fabs(end.x()*SGD_RADIANS_TO_DEGREES - lon2)
169 // << " lat error = " << fabs(end.y()*SGD_RADIANS_TO_DEGREES - lat2)
172 double sl_radius, lat_geoc;
173 sgGeodToGeoc( get_Latitude(), get_Altitude(), &sl_radius, &lat_geoc );
175 // update euler angles
176 double heading = fmod(get_Psi() + turn + yaw, SGD_2PI);
177 _set_Euler_Angles(roll, pitch, heading);
178 _set_Euler_Rates(0,0,0);
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() );
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);