]> git.mxchange.org Git - flightgear.git/blob - FDM/flight.cxx
5216c251b0133b1203c19afba2988ccace022304
[flightgear.git] / FDM / flight.cxx
1 // flight.c -- a general interface to the various flight models
2 //
3 // Written by Curtis Olson, started May 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22 // (Log is kept at end of this file)
23
24
25 #include <stdio.h>
26
27 #include "flight.hxx"
28 #include "LaRCsim.hxx"
29
30 #include <Debug/logstream.hxx>
31 #include <FDM/External/external.hxx>
32 #include <FDM/LaRCsim/ls_interface.h>
33 #include <Include/fg_constants.h>
34 #include <Math/fg_geodesy.hxx>
35 #include <Time/timestamp.hxx>
36
37
38 // base_fdm_state is the internal state that is updated in integer
39 // multiples of "dt".  This leads to "jitter" with respect to the real
40 // world time, so we introduce cur_fdm_state which is extrapolated by
41 // the difference between sim time and real world time
42
43 FGState cur_fdm_state;
44 FGState base_fdm_state;
45
46
47 // Extrapolate fdm based on time_offset (in usec)
48 void FGState::extrapolate( int time_offset ) {
49     double dt = time_offset / 1000000.0;
50     cout << "extrapolating FDM by dt = " << dt << endl;
51
52     double lat = geodetic_position_v[0] + geocentric_rates_v[0] * dt;
53     double lat_geoc = geocentric_position_v[0] + geocentric_rates_v[0] * dt;
54
55     double lon = geodetic_position_v[1] + geocentric_rates_v[1] * dt;
56     double lon_geoc = geocentric_position_v[1] + geocentric_rates_v[1] * dt;
57
58     double alt = geodetic_position_v[2] + geocentric_rates_v[2] * dt;
59     double radius = geocentric_position_v[2] + geocentric_rates_v[2] * dt;
60
61     geodetic_position_v[0] = lat;
62     geocentric_position_v[0] = lat_geoc;
63
64     geodetic_position_v[1] = lon;
65     geocentric_position_v[1] = lon_geoc;
66
67     geodetic_position_v[2] = alt;
68     geocentric_position_v[2] = radius;
69 }
70
71
72 // Initialize the flight model parameters
73 int fgFDMInit(int model, FGState& f, double dt) {
74     double save_alt = 0.0;
75
76     FG_LOG( FG_FLIGHT ,FG_INFO, "Initializing flight model" );
77
78     base_fdm_state = f;
79
80     if ( model == FGState::FG_SLEW ) {
81         // fgSlewInit(dt);
82     } else if ( model == FGState::FG_LARCSIM ) {
83
84         // lets try to avoid really screwing up the LaRCsim model
85         if ( base_fdm_state.get_Altitude() < -9000.0 ) {
86             save_alt = base_fdm_state.get_Altitude();
87             base_fdm_state.set_Altitude( 0.0 );
88         }
89
90         // translate FG to LaRCsim structure
91         FGState_2_LaRCsim(base_fdm_state);
92
93         // initialize LaRCsim
94         fgLaRCsimInit(dt);
95
96         FG_LOG( FG_FLIGHT, FG_INFO, "FG pos = " << 
97                 base_fdm_state.get_Latitude() );
98
99         // translate LaRCsim back to FG structure
100         fgLaRCsim_2_FGState(base_fdm_state);
101
102         // but lets restore our original bogus altitude when we are done
103         if ( save_alt < -9000.0 ) {
104             base_fdm_state.set_Altitude( save_alt );
105         }
106     } else if ( model == FGState::FG_EXTERNAL ) {
107         fgExternalInit(base_fdm_state);
108     } else {
109         FG_LOG( FG_FLIGHT, FG_WARN,
110                   "Unimplemented flight model == " << model );
111     }
112
113     // set valid time for this record
114     base_fdm_state.stamp_time();
115         
116     f = base_fdm_state;
117
118     return 1;
119 }
120
121
122 // Run multiloop iterations of the flight model
123 int fgFDMUpdate(int model, FGState& f, int multiloop, int time_offset) {
124     double time_step, start_elev, end_elev;
125
126     // printf("Altitude = %.2f\n", FG_Altitude * 0.3048);
127
128     // set valid time for this record
129     base_fdm_state.stamp_time();
130
131     time_step = (1.0 / DEFAULT_MODEL_HZ) * multiloop;
132     start_elev = base_fdm_state.get_Altitude();
133
134     if ( model == FGState::FG_SLEW ) {
135         // fgSlewUpdate(f, multiloop);
136     } else if ( model == FGState::FG_LARCSIM ) {
137         fgLaRCsimUpdate(base_fdm_state, multiloop);
138         // extrapolate position based on actual time
139         // f = extrapolate_fdm( base_fdm_state, time_offset );
140         f = base_fdm_state;
141     } else if ( model == FGState::FG_EXTERNAL ) {
142         // fgExternalUpdate(f, multiloop);
143         FGTimeStamp current;
144         current.stamp();
145         f = base_fdm_state;
146         f.extrapolate( current - base_fdm_state.get_time_stamp() );
147     } else {
148         FG_LOG( FG_FLIGHT, FG_WARN,
149                 "Unimplemented flight model == " <<  model );
150     }
151
152     end_elev = base_fdm_state.get_Altitude();
153
154     if ( time_step > 0.0 ) {
155         // feet per second
156         base_fdm_state.set_Climb_Rate( (end_elev - start_elev) / time_step );
157     }
158
159     return 1;
160 }
161
162
163 // Set the altitude (force)
164 void fgFDMForceAltitude(int model, double alt_meters) {
165     double sea_level_radius_meters;
166     double lat_geoc;
167
168     // Set the FG variables first
169     fgGeodToGeoc( base_fdm_state.get_Latitude(), alt_meters, 
170                   &sea_level_radius_meters, &lat_geoc);
171
172     base_fdm_state.set_Altitude( alt_meters * METER_TO_FEET );
173     base_fdm_state.set_Radius_to_vehicle( base_fdm_state.get_Altitude() + 
174                                           (sea_level_radius_meters * 
175                                            METER_TO_FEET) );
176
177     // additional work needed for some flight models
178     if ( model == FGState::FG_LARCSIM ) {
179         ls_ForceAltitude( base_fdm_state.get_Altitude() );
180     }
181 }
182
183
184 // Set the local ground elevation
185 void fgFDMSetGroundElevation(int model, double ground_meters) {
186     base_fdm_state.set_Runway_altitude( ground_meters * METER_TO_FEET );
187     cur_fdm_state.set_Runway_altitude( ground_meters * METER_TO_FEET );
188 }
189
190
191 // $Log$
192 // Revision 1.14  1999/02/01 21:33:31  curt
193 // Renamed FlightGear/Simulator/Flight to FlightGear/Simulator/FDM since
194 // Jon accepted my offer to do this and thought it was a good idea.
195 //
196 // Revision 1.13  1999/01/27 04:48:39  curt
197 // Set the runway height in cur_fdm_state as well as base_fdm_state.
198 //
199 // Revision 1.12  1999/01/20 13:42:22  curt
200 // Tweaked FDM interface.
201 // Testing check sum support for NMEA serial output.
202 //
203 // Revision 1.11  1999/01/19 17:52:06  curt
204 // Working on being able to extrapolate a new position and orientation
205 // based on a position, orientation, and time offset.
206 //
207 // Revision 1.10  1999/01/09 13:37:32  curt
208 // Convert fgTIMESTAMP to FGTimeStamp which holds usec instead of ms.
209 //
210 // Revision 1.9  1999/01/08 19:27:37  curt
211 // Fixed AOA reading on HUD.
212 // Continued work on time jitter compensation.
213 //
214 // Revision 1.8  1999/01/08 03:23:51  curt
215 // Beginning work on compensating for sim time vs. real world time "jitter".
216 //
217 // Revision 1.7  1998/12/18 23:37:07  curt
218 // Collapsed out the FGState variables not currently needed.  They are just
219 // commented out and can be readded easily at any time.  The point of this
220 // exersize is to determine which variables were or were not currently being
221 // used.
222 //
223 // Revision 1.6  1998/12/05 15:54:11  curt
224 // Renamed class fgFLIGHT to class FGState as per request by JSB.
225 //
226 // Revision 1.5  1998/12/04 01:29:39  curt
227 // Stubbed in a new flight model called "External" which is expected to be driven
228 // from some external source.
229 //
230 // Revision 1.4  1998/12/03 01:16:40  curt
231 // Converted fgFLIGHT to a class.
232 //
233 // Revision 1.3  1998/11/06 21:18:03  curt
234 // Converted to new logstream debugging facility.  This allows release
235 // builds with no messages at all (and no performance impact) by using
236 // the -DFG_NDEBUGNDEBUG flag.
237 //
238 // Revision 1.2  1998/10/16 23:27:40  curt
239 // C++-ifying.
240 //
241 // Revision 1.1  1998/10/16 20:16:41  curt
242 // Renamed flight.[ch] to flight.[ch]xx
243 //
244 // Revision 1.19  1998/09/29 14:57:38  curt
245 // c++-ified comments.
246 //
247 // Revision 1.18  1998/09/29 02:02:40  curt
248 // Added a rate of climb calculation.
249 //
250 // Revision 1.17  1998/08/24 20:09:07  curt
251 // .
252 //
253 // Revision 1.16  1998/08/22  14:49:55  curt
254 // Attempting to iron out seg faults and crashes.
255 // Did some shuffling to fix a initialization order problem between view
256 // position, scenery elevation.
257 //
258 // Revision 1.15  1998/07/30 23:44:36  curt
259 // Beginning to add support for multiple flight models.
260 //
261 // Revision 1.14  1998/07/12 03:08:27  curt
262 // Added fgFlightModelSetAltitude() to force the altitude to something
263 // other than the current altitude.  LaRCsim doesn't let you do this by just
264 // changing FG_Altitude.
265 //
266 // Revision 1.13  1998/04/25 22:06:28  curt
267 // Edited cvs log messages in source files ... bad bad bad!
268 //
269 // Revision 1.12  1998/04/21 16:59:33  curt
270 // Integrated autopilot.
271 // Prepairing for C++ integration.
272 //
273 // Revision 1.11  1998/04/18 04:14:04  curt
274 // Moved fg_debug.c to it's own library.
275 //
276 // Revision 1.10  1998/02/07 15:29:37  curt
277 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
278 // <chotchkiss@namg.us.anritsu.com>
279 //
280 // Revision 1.9  1998/01/27 00:47:53  curt
281 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
282 // system and commandline/config file processing code.
283 //
284 // Revision 1.8  1998/01/19 19:27:03  curt
285 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
286 // This should simplify things tremendously.
287 //
288 // Revision 1.7  1998/01/19 18:40:23  curt
289 // Tons of little changes to clean up the code and to remove fatal errors
290 // when building with the c++ compiler.
291 //
292 // Revision 1.6  1998/01/19 18:35:43  curt
293 // Minor tweaks and fixes for cygwin32.
294 //
295 // Revision 1.5  1997/12/30 20:47:37  curt
296 // Integrated new event manager with subsystem initializations.
297 //
298 // Revision 1.4  1997/12/10 22:37:42  curt
299 // Prepended "fg" on the name of all global structures that didn't have it yet.
300 // i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
301 //
302 // Revision 1.3  1997/08/27 03:30:04  curt
303 // Changed naming scheme of basic shared structures.
304 //
305 // Revision 1.2  1997/05/29 22:39:57  curt
306 // Working on incorporating the LaRCsim flight model.
307 //
308 // Revision 1.1  1997/05/29 02:35:04  curt
309 // Initial revision.
310 //