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