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