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 - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <simgear/math/sg_geodesy.hxx>
25 #include <simgear/math/point3d.hxx>
26 #include <simgear/math/polar3d.hxx>
28 #include <Controls/controls.hxx>
29 #include <Main/globals.hxx>
30 #include <Main/fg_props.hxx>
34 const double throttle_damp = 0.05;
35 const double aileron_damp = 0.01;
36 const double elevator_damp = 0.01;
38 FGUFO::FGUFO( double dt )
51 // Initialize the UFO flight model, dt is the time increment
52 // for each subsequent iteration through the EOM
58 // Run an iteration of the EOM (equations of motion)
59 void FGUFO::update( int multiloop ) {
60 // cout << "FGLaRCsim::update()" << endl;
62 double time_step = get_delta_t() * multiloop;
65 double th = globals->get_controls()->get_throttle( 0 );
66 if (globals->get_controls()->get_brake(0)) {
69 Throttle = th * throttle_damp + Throttle * (1 - throttle_damp);
71 // read the state of the control surfaces
72 Aileron = globals->get_controls()->get_aileron() * aileron_damp
73 + Aileron * (1 - aileron_damp);
74 Elevator = globals->get_controls()->get_elevator() * elevator_damp
75 + Elevator * (1 - elevator_damp);
77 // the velocity of the aircraft
78 double velocity = Throttle * 2000; // meters/sec
80 double old_pitch = get_Theta();
81 double pitch_rate = SGD_PI_4; // assume I will be pitching up
82 double target_pitch = -Elevator * SGD_PI_2;
84 // if I am pitching down
85 if (old_pitch > target_pitch)
86 // set the pitch rate to negative (down)
89 double pitch = old_pitch + (pitch_rate * time_step);
91 // if I am pitching up
94 // clip the pitch at the limit
95 if ( pitch > target_pitch)
100 // if I am pitching down
101 else if (pitch_rate < 0.0)
103 // clip the pitch at the limit
104 if ( pitch < target_pitch)
106 pitch = target_pitch;
110 double old_roll = get_Phi();
111 double roll_rate = SGD_PI_4;
112 double target_roll = Aileron * SGD_PI_2;
114 if (old_roll > target_roll)
117 double roll = old_roll + (roll_rate * time_step);
119 // if I am rolling CW
122 // clip the roll at the limit
123 if ( roll > target_roll)
128 // if I am rolling CCW
129 else if (roll_rate < 0.0)
131 // clip the roll at the limit
132 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;
155 // update (lon/lat) position
156 double lat2, lon2, az2;
157 if ( fabs(speed) > SG_EPSILON ) {
158 geo_direct_wgs_84 ( get_Altitude(),
159 get_Latitude() * SGD_RADIANS_TO_DEGREES,
160 get_Longitude() * SGD_RADIANS_TO_DEGREES,
161 get_Psi() * SGD_RADIANS_TO_DEGREES,
162 dist, &lat2, &lon2, &az2 );
164 _set_Longitude( lon2 * SGD_DEGREES_TO_RADIANS );
165 _set_Latitude( lat2 * 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 _set_Euler_Angles( roll, pitch,
177 fmod(get_Psi() + turn, SGD_2PI) );
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 _set_Sea_level_radius( sl_radius * SG_METER_TO_FEET);
185 _set_Altitude( get_Altitude() + climb );