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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <simgear/math/sg_geodesy.hxx>
29 #include <simgear/math/point3d.hxx>
30 #include <simgear/math/polar3d.hxx>
32 #include <Aircraft/controls.hxx>
33 #include <Main/globals.hxx>
34 #include <Main/fg_props.hxx>
38 const double throttle_damp = 0.2;
39 const double aileron_damp = 0.05;
40 const double elevator_damp = 0.05;
41 const double rudder_damp = 0.4;
43 FGUFO::FGUFO( double dt )
57 // Initialize the UFO flight model, dt is the time increment
58 // for each subsequent iteration through the EOM
64 // Run an iteration of the EOM (equations of motion)
65 void FGUFO::update( double dt ) {
66 // cout << "FGLaRCsim::update()" << endl;
71 double time_step = dt;
74 double th = globals->get_controls()->get_throttle( 0 );
75 if ( globals->get_controls()->get_brake_left() > 0.0
76 || globals->get_controls()->get_brake_right() > 0.0 )
80 Throttle = th * throttle_damp + Throttle * (1 - throttle_damp);
82 // read the state of the control surfaces
83 Aileron = globals->get_controls()->get_aileron() * aileron_damp
84 + Aileron * (1 - aileron_damp);
85 Elevator = globals->get_controls()->get_elevator() * elevator_damp
86 + Elevator * (1 - elevator_damp);
87 Rudder = globals->get_controls()->get_rudder() * rudder_damp
88 + Rudder * (1 - rudder_damp);
90 // the velocity of the aircraft
91 double velocity = Throttle * 2000; // meters/sec
93 double old_pitch = get_Theta();
94 double pitch_rate = SGD_PI_4; // assume I will be pitching up
95 double target_pitch = -Elevator * SGD_PI_2;
97 // if I am pitching down
98 if (old_pitch > target_pitch)
99 // set the pitch rate to negative (down)
102 double pitch = old_pitch + (pitch_rate * time_step);
104 // if I am pitching up
105 if (pitch_rate > 0.0)
107 // clip the pitch at the limit
108 if ( pitch > target_pitch)
110 pitch = target_pitch;
113 // if I am pitching down
114 else if (pitch_rate < 0.0)
116 // clip the pitch at the limit
117 if ( pitch < target_pitch)
119 pitch = target_pitch;
123 double old_roll = get_Phi();
124 double roll_rate = SGD_PI_4;
125 double target_roll = Aileron * SGD_PI_2;
127 if (old_roll > target_roll)
130 double roll = old_roll + (roll_rate * time_step);
132 // if I am rolling CW
135 // clip the roll at the limit
136 if ( roll > target_roll)
141 // if I am rolling CCW
142 else if (roll_rate < 0.0)
144 // clip the roll at the limit
145 if ( roll < target_roll)
151 // the vertical speed of the aircraft
152 double real_climb_rate = sin (pitch) * SG_METER_TO_FEET * velocity; // feet/sec
153 _set_Climb_Rate( -Elevator * 10.0 );
154 double climb = real_climb_rate * time_step;
156 // the lateral speed of the aircraft
157 double speed = cos (pitch) * velocity; // meters/sec
158 double dist = speed * time_step;
159 double kts = velocity * SG_METER_TO_NM * 3600.0;
160 _set_V_equiv_kts( kts );
161 _set_V_calibrated_kts( kts );
162 _set_V_ground_speed( kts );
165 double turn_rate = sin(roll) * SGD_PI_4; // radians/sec
166 double turn = turn_rate * time_step;
167 double yaw = fabs(Rudder) < .2 ? 0.0 : Rudder / (25 + fabs(speed) * .1);
169 // update (lon/lat) position
170 double lat2, lon2, az2;
171 if ( fabs(speed) > SG_EPSILON ) {
172 geo_direct_wgs_84 ( get_Altitude(),
173 get_Latitude() * SGD_RADIANS_TO_DEGREES,
174 get_Longitude() * SGD_RADIANS_TO_DEGREES,
175 get_Psi() * SGD_RADIANS_TO_DEGREES,
176 dist, &lat2, &lon2, &az2 );
178 _set_Longitude( lon2 * SGD_DEGREES_TO_RADIANS );
179 _set_Latitude( lat2 * SGD_DEGREES_TO_RADIANS );
182 // cout << "lon error = " << fabs(end.x()*SGD_RADIANS_TO_DEGREES - lon2)
183 // << " lat error = " << fabs(end.y()*SGD_RADIANS_TO_DEGREES - lat2)
186 double sl_radius, lat_geoc;
187 sgGeodToGeoc( get_Latitude(), get_Altitude(), &sl_radius, &lat_geoc );
189 // update euler angles
190 _set_Euler_Angles( roll, pitch,
191 fmod(get_Psi() + turn + yaw, SGD_2PI) );
192 _set_Euler_Rates(0,0,0);
194 _set_Geocentric_Position( lat_geoc, get_Longitude(),
195 sl_radius + get_Altitude() + climb );
196 // cout << "sea level radius (ft) = " << sl_radius << endl;
197 // cout << "(setto) sea level radius (ft) = " << get_Sea_level_radius() << endl;
198 _update_ground_elev_at_pos();
199 _set_Sea_level_radius( sl_radius * SG_METER_TO_FEET);
200 _set_Altitude( get_Altitude() + climb );