]> git.mxchange.org Git - flightgear.git/blob - src/FDM/UFO.cxx
add <mod-meta> and <mod-super> XML elements for key bindings
[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 double FGUFO::lowpass::_dt;
39
40
41 FGUFO::FGUFO( double dt ) :
42     Throttle(new lowpass(fgGetDouble("/controls/damping/throttle", 0.1))),
43     Aileron(new lowpass(fgGetDouble("/controls/damping/aileron", 0.65))),
44     Elevator(new lowpass(fgGetDouble("/controls/damping/elevator", 0.65))),
45     Rudder(new lowpass(fgGetDouble("/controls/damping/rudder", 0.05))),
46     Aileron_Trim(new lowpass(fgGetDouble("/controls/damping/aileron-trim", 0.65))),
47     Elevator_Trim(new lowpass(fgGetDouble("/controls/damping/elevator-trim", 0.65))),
48     Rudder_Trim(new lowpass(fgGetDouble("/controls/damping/rudder-trim", 0.05))),
49     Speed_Max(fgGetNode("/engines/engine/speed-max-mps", true))
50 {
51 }
52
53
54 FGUFO::~FGUFO() {
55     delete Throttle;
56     delete Aileron;
57     delete Elevator;
58     delete Rudder;
59     delete Aileron_Trim;
60     delete Elevator_Trim;
61     delete Rudder_Trim;
62 }
63
64
65 // Initialize the UFO flight model, dt is the time increment
66 // for each subsequent iteration through the EOM
67 void FGUFO::init() {
68     common_init();
69     if (Speed_Max->getDoubleValue() < 0.01)
70         Speed_Max->setDoubleValue(2000.0);
71 }
72
73
74 // Run an iteration of the EOM (equations of motion)
75 void FGUFO::update( double dt ) {
76
77     if (is_suspended())
78         return;
79
80     lowpass::set_delta(dt);
81     double time_step = dt;
82     FGControls *ctrl = globals->get_controls();
83
84     // read the throttle
85     double throttle = ctrl->get_throttle( 0 );
86     double brake_left = ctrl->get_brake_left();
87     double brake_right = ctrl->get_brake_right();
88
89     if (brake_left > 0.5 || brake_right > 0.5)
90         throttle = -throttle;
91
92     double velocity = Throttle->filter(throttle) * Speed_Max->getDoubleValue(); // meters/sec
93
94
95     // read and lowpass-filter the state of the control surfaces
96     double aileron = Aileron->filter(ctrl->get_aileron());
97     double elevator = Elevator->filter(ctrl->get_elevator());
98     double rudder = Rudder->filter(ctrl->get_rudder());
99
100     aileron += Aileron_Trim->filter(ctrl->get_aileron_trim());
101     elevator += Elevator_Trim->filter(ctrl->get_elevator_trim());
102     rudder += Rudder_Trim->filter(ctrl->get_rudder_trim());
103
104     double old_pitch = get_Theta();
105     double pitch_rate = SGD_PI_4;  // assume I will be pitching up
106     double target_pitch = -elevator * SGD_PI_2;
107
108     if (old_pitch > target_pitch)  // pitching down
109         pitch_rate *= -1;
110
111     double pitch = old_pitch + (pitch_rate * time_step);
112
113     if (pitch_rate > 0.0) {        // pitching up
114         if (pitch > target_pitch)
115             pitch = target_pitch;
116
117     } else if (pitch_rate < 0.0) { // pitching down
118         if (pitch < target_pitch)
119             pitch = target_pitch;
120     }
121
122     double old_roll    = get_Phi();
123     double roll_rate   = SGD_PI_4;
124     double target_roll = aileron * SGD_PI_2;
125
126     if (old_roll > target_roll)
127         roll_rate *= -1;
128
129     double roll = old_roll + (roll_rate * time_step);
130
131     if (roll_rate > 0.0) {         // rolling CW
132         if (roll > target_roll)
133             roll = target_roll;
134
135     } else if (roll_rate < 0.0) {  // rolling CCW
136         if (roll < target_roll)
137             roll = target_roll;
138     }
139
140     // the vertical speed of the aircraft
141     double real_climb_rate = sin (pitch) * SG_METER_TO_FEET * velocity; // feet/sec
142     _set_Climb_Rate( -elevator * 10.0 );
143     double climb = real_climb_rate * time_step;
144
145     // the lateral speed of the aircraft
146     double speed = cos (pitch) * velocity; // meters/sec
147     double dist = speed * time_step;
148     double kts = velocity * SG_METER_TO_NM * 3600.0;
149     _set_V_equiv_kts( kts );
150     _set_V_calibrated_kts( kts );
151     _set_V_ground_speed( kts );
152
153     // angle of turn
154     double turn_rate = sin(roll) * SGD_PI_4; // radians/sec
155     double turn = turn_rate * time_step;
156     double yaw = fabs(rudder) < .2 ? 0.0 : rudder / (25 + fabs(speed) * .1);
157
158     // update (lon/lat) position
159     double lat2, lon2, az2;
160     if ( fabs(speed) > SG_EPSILON ) {
161         geo_direct_wgs_84 ( get_Altitude(),
162                             get_Latitude() * SGD_RADIANS_TO_DEGREES,
163                             get_Longitude() * SGD_RADIANS_TO_DEGREES,
164                             get_Psi() * SGD_RADIANS_TO_DEGREES,
165                             dist, &lat2, &lon2, &az2 );
166
167         _set_Longitude( lon2 * SGD_DEGREES_TO_RADIANS );
168         _set_Latitude( lat2 * SGD_DEGREES_TO_RADIANS );
169     }
170
171     // cout << "lon error = " << fabs(end.x()*SGD_RADIANS_TO_DEGREES - lon2)
172     //      << "  lat error = " << fabs(end.y()*SGD_RADIANS_TO_DEGREES - lat2)
173     //      << endl;
174
175     double sl_radius, lat_geoc;
176     sgGeodToGeoc( get_Latitude(), get_Altitude(), &sl_radius, &lat_geoc );
177
178     // update euler angles
179     double heading = fmod(get_Psi() + turn + yaw, SGD_2PI);
180     _set_Euler_Angles(roll, pitch, heading);
181     _set_Euler_Rates(0,0,0);
182
183     _set_Geocentric_Position( lat_geoc, get_Longitude(),
184                              sl_radius + get_Altitude() + climb );
185     // cout << "sea level radius (ft) = " << sl_radius << endl;
186     // cout << "(setto) sea level radius (ft) = " << get_Sea_level_radius() << endl;
187     _update_ground_elev_at_pos();
188     _set_Sea_level_radius( sl_radius * SG_METER_TO_FEET);
189     _set_Altitude( get_Altitude() + climb );
190     _set_Altitude_AGL( get_Altitude() - get_Runway_altitude() );
191
192     set_V_north(cos(heading) * velocity * SG_METER_TO_FEET);
193     set_V_east(sin(heading) * velocity * SG_METER_TO_FEET);
194     set_V_down(-real_climb_rate);
195 }
196
197