]> git.mxchange.org Git - flightgear.git/blob - src/FDM/UFO.cxx
UFO FDM patches from Jonathan Polley:
[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  - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22
23
24 #include <simgear/math/sg_geodesy.hxx>
25 #include <simgear/math/point3d.hxx>
26 #include <simgear/math/polar3d.hxx>
27
28 #include <Controls/controls.hxx>
29 #include <Main/globals.hxx>
30 #include <Main/fg_props.hxx>
31
32 #include "UFO.hxx"
33
34
35 FGUFO::FGUFO( double dt ) {
36     set_delta_t( dt );
37 }
38
39
40 FGUFO::~FGUFO() {
41 }
42
43
44 // Initialize the UFO flight model, dt is the time increment
45 // for each subsequent iteration through the EOM
46 void FGUFO::init() {
47     common_init();
48 }
49
50
51 // Run an iteration of the EOM (equations of motion)
52 void FGUFO::update( int multiloop ) {
53     // cout << "FGLaRCsim::update()" << endl;
54
55     double time_step = get_delta_t() * multiloop;
56
57     // read the throttle
58     double Throttle = globals->get_controls()->get_throttle( 0 );
59
60     // read the state of the control surfaces
61     double Aileron  = globals->get_controls()->get_aileron();
62     double Elevator = globals->get_controls()->get_elevator();
63
64     // the velocity of the aircraft
65     double velocity = Throttle * 2000; // meters/sec
66
67     double old_pitch = get_Theta();
68     double pitch_rate = SGD_PI_4; // assume I will be pitching up
69     double target_pitch = -Elevator * SGD_PI_2;
70
71     // if I am pitching down
72     if (old_pitch > target_pitch)
73         // set the pitch rate to negative (down)
74         pitch_rate *= -1;
75
76     double pitch = old_pitch + (pitch_rate * time_step);
77
78     // if I am pitching up
79     if (pitch_rate > 0.0)
80     {
81         // clip the pitch at the limit
82         if ( pitch > target_pitch)
83         {
84             pitch = target_pitch;
85         }
86     }
87     // if I am pitching down
88     else if (pitch_rate < 0.0)
89     {
90         // clip the pitch at the limit
91         if ( pitch < target_pitch)
92         {
93             pitch = target_pitch;
94         }
95     }
96
97     double old_roll     = get_Phi();
98     double roll_rate    = SGD_PI_4;
99     double target_roll  = Aileron * SGD_PI_2;
100
101     if (old_roll > target_roll)
102         roll_rate *= -1;
103     
104     double roll = old_roll + (roll_rate * time_step);
105
106     // if I am rolling CW
107     if (roll_rate > 0.0)
108     {
109         // clip the roll at the limit
110         if ( roll > target_roll)
111         {
112             roll = target_roll;
113         }
114     }
115     // if I am rolling CCW
116     else if (roll_rate < 0.0)
117     {
118         // clip the roll at the limit
119         if ( roll < target_roll)
120         {
121             roll = target_roll;
122         }
123     }
124
125     // the vertical speed of the aircraft
126     double real_climb_rate = sin (pitch) * SG_METER_TO_FEET * velocity; // feet/sec
127     _set_Climb_Rate( -Elevator * 10.0 );
128     double climb = real_climb_rate * time_step;
129
130     // the lateral speed of the aircraft
131     double speed = cos (pitch) * velocity; // meters/sec
132     double dist = speed * time_step;
133     double kts = velocity * SG_METER_TO_NM * 3600.0;
134     _set_V_equiv_kts( kts );
135     _set_V_calibrated_kts( kts );
136     _set_V_ground_speed( kts );
137
138     // angle of turn
139     double turn_rate = sin(roll) * SGD_PI_4; // radians/sec
140     double turn = turn_rate * time_step;
141
142     // update (lon/lat) position
143     double lat2, lon2, az2;
144     if ( speed > SG_EPSILON ) {
145         geo_direct_wgs_84 ( get_Altitude(),
146                             get_Latitude() * SGD_RADIANS_TO_DEGREES,
147                             get_Longitude() * SGD_RADIANS_TO_DEGREES,
148                             get_Psi() * SGD_RADIANS_TO_DEGREES,
149                             dist, &lat2, &lon2, &az2 );
150
151         _set_Longitude( lon2 * SGD_DEGREES_TO_RADIANS );
152         _set_Latitude( lat2 * SGD_DEGREES_TO_RADIANS );
153     }
154
155     // cout << "lon error = " << fabs(end.x()*SGD_RADIANS_TO_DEGREES - lon2)
156     //      << "  lat error = " << fabs(end.y()*SGD_RADIANS_TO_DEGREES - lat2)
157     //      << endl;
158
159     double sl_radius, lat_geoc;
160     sgGeodToGeoc( get_Latitude(), get_Altitude(), &sl_radius, &lat_geoc );
161
162     // update euler angles
163     _set_Euler_Angles( roll, pitch,
164                        fmod(get_Psi() + turn, SGD_2PI) );
165     _set_Euler_Rates(0,0,0);
166
167     _set_Geocentric_Position( lat_geoc, get_Longitude(), 
168                              sl_radius + get_Altitude() + climb );
169     // cout << "sea level radius (ft) = " << sl_radius << endl;
170     // cout << "(setto) sea level radius (ft) = " << get_Sea_level_radius() << endl;
171     _set_Sea_level_radius( sl_radius * SG_METER_TO_FEET);
172     _set_Altitude( get_Altitude() + climb );
173 }