]> git.mxchange.org Git - flightgear.git/blob - src/FDM/UFO.cxx
Sync. w. JSB CVS as of 15/01/2007
[flightgear.git] / src / FDM / UFO.cxx
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 #include <simgear/math/point3d.hxx>
30 #include <simgear/math/polar3d.hxx>
31
32 #include <Aircraft/controls.hxx>
33 #include <Main/globals.hxx>
34 #include <Main/fg_props.hxx>
35
36 #include "UFO.hxx"
37
38 const double throttle_damp = 0.2;
39 const double aileron_damp = 0.05;
40 const double elevator_damp = 0.05;
41 const double elevator_trim_damp = 0.05;
42 const double rudder_damp = 0.4;
43
44 FGUFO::FGUFO( double dt )
45   : Throttle(0.0),
46     Aileron(0.0),
47     Elevator(0.0),
48     Elevator_Trim(0.0),
49     Rudder(0.0),
50     Speed_Max(fgGetNode("/engines/engine/speed-max-mps", true))
51 {
52 //     set_delta_t( dt );
53 }
54
55
56 FGUFO::~FGUFO() {
57 }
58
59
60 // Initialize the UFO flight model, dt is the time increment
61 // for each subsequent iteration through the EOM
62 void FGUFO::init() {
63     common_init();
64     if (Speed_Max->getDoubleValue() < 0.01)
65         Speed_Max->setDoubleValue(2000.0);
66 }
67
68
69 // Run an iteration of the EOM (equations of motion)
70 void FGUFO::update( double dt ) {
71     // cout << "FGLaRCsim::update()" << endl;
72
73     if (is_suspended())
74       return;
75
76     double time_step = dt;
77
78     // read the throttle
79     double th = globals->get_controls()->get_throttle( 0 );
80     if ( globals->get_controls()->get_brake_left() > 0.5
81          || globals->get_controls()->get_brake_right() > 0.5 )
82     {
83         th = -th;
84     }
85     Throttle = th * throttle_damp + Throttle * (1 - throttle_damp);
86
87     // read the state of the control surfaces
88     Aileron  = globals->get_controls()->get_aileron() * aileron_damp
89                + Aileron * (1 - aileron_damp);
90     Elevator = globals->get_controls()->get_elevator() * elevator_damp
91                + Elevator * (1 - elevator_damp);
92     Elevator_Trim = globals->get_controls()->get_elevator_trim()
93                     * elevator_trim_damp
94                     + Elevator_Trim * (1 - elevator_trim_damp);
95     Rudder = globals->get_controls()->get_rudder() * rudder_damp
96                + Rudder * (1 - rudder_damp);
97
98     // the velocity of the aircraft
99     double velocity = Throttle * Speed_Max->getDoubleValue(); // meters/sec
100
101     double old_pitch = get_Theta();
102     double pitch_rate = SGD_PI_4; // assume I will be pitching up
103     double target_pitch = (-Elevator - Elevator_Trim) * SGD_PI_2;
104
105     // if I am pitching down
106     if (old_pitch > target_pitch)
107         // set the pitch rate to negative (down)
108         pitch_rate *= -1;
109
110     double pitch = old_pitch + (pitch_rate * time_step);
111
112     // if I am pitching up
113     if (pitch_rate > 0.0)
114     {
115         // clip the pitch at the limit
116         if ( pitch > target_pitch)
117         {
118             pitch = target_pitch;
119         }
120     }
121     // if I am pitching down
122     else if (pitch_rate < 0.0)
123     {
124         // clip the pitch at the limit
125         if ( pitch < target_pitch)
126         {
127             pitch = target_pitch;
128         }
129     }
130
131     double old_roll     = get_Phi();
132     double roll_rate    = SGD_PI_4;
133     double target_roll  = Aileron * SGD_PI_2;
134
135     if (old_roll > target_roll)
136         roll_rate *= -1;
137     
138     double roll = old_roll + (roll_rate * time_step);
139
140     // if I am rolling CW
141     if (roll_rate > 0.0)
142     {
143         // clip the roll at the limit
144         if ( roll > target_roll)
145         {
146             roll = target_roll;
147         }
148     }
149     // if I am rolling CCW
150     else if (roll_rate < 0.0)
151     {
152         // clip the roll at the limit
153         if ( roll < target_roll)
154         {
155             roll = target_roll;
156         }
157     }
158
159     // the vertical speed of the aircraft
160     double real_climb_rate = sin (pitch) * SG_METER_TO_FEET * velocity; // feet/sec
161     _set_Climb_Rate( -Elevator * 10.0 );
162     double climb = real_climb_rate * time_step;
163
164     // the lateral speed of the aircraft
165     double speed = cos (pitch) * velocity; // meters/sec
166     double dist = speed * time_step;
167     double kts = velocity * SG_METER_TO_NM * 3600.0;
168     _set_V_equiv_kts( kts );
169     _set_V_calibrated_kts( kts );
170     _set_V_ground_speed( kts );
171
172     // angle of turn
173     double turn_rate = sin(roll) * SGD_PI_4; // radians/sec
174     double turn = turn_rate * time_step;
175     double yaw = fabs(Rudder) < .2 ? 0.0 : Rudder / (25 + fabs(speed) * .1);
176
177     // update (lon/lat) position
178     double lat2, lon2, az2;
179     if ( fabs(speed) > SG_EPSILON ) {
180         geo_direct_wgs_84 ( get_Altitude(),
181                             get_Latitude() * SGD_RADIANS_TO_DEGREES,
182                             get_Longitude() * SGD_RADIANS_TO_DEGREES,
183                             get_Psi() * SGD_RADIANS_TO_DEGREES,
184                             dist, &lat2, &lon2, &az2 );
185
186         _set_Longitude( lon2 * SGD_DEGREES_TO_RADIANS );
187         _set_Latitude( lat2 * SGD_DEGREES_TO_RADIANS );
188     }
189
190     // cout << "lon error = " << fabs(end.x()*SGD_RADIANS_TO_DEGREES - lon2)
191     //      << "  lat error = " << fabs(end.y()*SGD_RADIANS_TO_DEGREES - lat2)
192     //      << endl;
193
194     double sl_radius, lat_geoc;
195     sgGeodToGeoc( get_Latitude(), get_Altitude(), &sl_radius, &lat_geoc );
196
197     // update euler angles
198     _set_Euler_Angles( roll, pitch,
199                        fmod(get_Psi() + turn + yaw, SGD_2PI) );
200     _set_Euler_Rates(0,0,0);
201
202     _set_Geocentric_Position( lat_geoc, get_Longitude(), 
203                              sl_radius + get_Altitude() + climb );
204     // cout << "sea level radius (ft) = " << sl_radius << endl;
205     // cout << "(setto) sea level radius (ft) = " << get_Sea_level_radius() << endl;
206     _update_ground_elev_at_pos();
207     _set_Sea_level_radius( sl_radius * SG_METER_TO_FEET);
208     _set_Altitude( get_Altitude() + climb );
209     _set_Altitude_AGL( get_Altitude() - get_Runway_altitude() );
210 }