]> git.mxchange.org Git - flightgear.git/blob - FDM/flight.cxx
Set the runway height in cur_fdm_state as well as base_fdm_state.
[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 <Flight/External/external.hxx>
32 #include <Flight/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.13  1999/01/27 04:48:39  curt
193 // Set the runway height in cur_fdm_state as well as base_fdm_state.
194 //
195 // Revision 1.12  1999/01/20 13:42:22  curt
196 // Tweaked FDM interface.
197 // Testing check sum support for NMEA serial output.
198 //
199 // Revision 1.11  1999/01/19 17:52:06  curt
200 // Working on being able to extrapolate a new position and orientation
201 // based on a position, orientation, and time offset.
202 //
203 // Revision 1.10  1999/01/09 13:37:32  curt
204 // Convert fgTIMESTAMP to FGTimeStamp which holds usec instead of ms.
205 //
206 // Revision 1.9  1999/01/08 19:27:37  curt
207 // Fixed AOA reading on HUD.
208 // Continued work on time jitter compensation.
209 //
210 // Revision 1.8  1999/01/08 03:23:51  curt
211 // Beginning work on compensating for sim time vs. real world time "jitter".
212 //
213 // Revision 1.7  1998/12/18 23:37:07  curt
214 // Collapsed out the FGState variables not currently needed.  They are just
215 // commented out and can be readded easily at any time.  The point of this
216 // exersize is to determine which variables were or were not currently being
217 // used.
218 //
219 // Revision 1.6  1998/12/05 15:54:11  curt
220 // Renamed class fgFLIGHT to class FGState as per request by JSB.
221 //
222 // Revision 1.5  1998/12/04 01:29:39  curt
223 // Stubbed in a new flight model called "External" which is expected to be driven
224 // from some external source.
225 //
226 // Revision 1.4  1998/12/03 01:16:40  curt
227 // Converted fgFLIGHT to a class.
228 //
229 // Revision 1.3  1998/11/06 21:18:03  curt
230 // Converted to new logstream debugging facility.  This allows release
231 // builds with no messages at all (and no performance impact) by using
232 // the -DFG_NDEBUGNDEBUG flag.
233 //
234 // Revision 1.2  1998/10/16 23:27:40  curt
235 // C++-ifying.
236 //
237 // Revision 1.1  1998/10/16 20:16:41  curt
238 // Renamed flight.[ch] to flight.[ch]xx
239 //
240 // Revision 1.19  1998/09/29 14:57:38  curt
241 // c++-ified comments.
242 //
243 // Revision 1.18  1998/09/29 02:02:40  curt
244 // Added a rate of climb calculation.
245 //
246 // Revision 1.17  1998/08/24 20:09:07  curt
247 // .
248 //
249 // Revision 1.16  1998/08/22  14:49:55  curt
250 // Attempting to iron out seg faults and crashes.
251 // Did some shuffling to fix a initialization order problem between view
252 // position, scenery elevation.
253 //
254 // Revision 1.15  1998/07/30 23:44:36  curt
255 // Beginning to add support for multiple flight models.
256 //
257 // Revision 1.14  1998/07/12 03:08:27  curt
258 // Added fgFlightModelSetAltitude() to force the altitude to something
259 // other than the current altitude.  LaRCsim doesn't let you do this by just
260 // changing FG_Altitude.
261 //
262 // Revision 1.13  1998/04/25 22:06:28  curt
263 // Edited cvs log messages in source files ... bad bad bad!
264 //
265 // Revision 1.12  1998/04/21 16:59:33  curt
266 // Integrated autopilot.
267 // Prepairing for C++ integration.
268 //
269 // Revision 1.11  1998/04/18 04:14:04  curt
270 // Moved fg_debug.c to it's own library.
271 //
272 // Revision 1.10  1998/02/07 15:29:37  curt
273 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
274 // <chotchkiss@namg.us.anritsu.com>
275 //
276 // Revision 1.9  1998/01/27 00:47:53  curt
277 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
278 // system and commandline/config file processing code.
279 //
280 // Revision 1.8  1998/01/19 19:27:03  curt
281 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
282 // This should simplify things tremendously.
283 //
284 // Revision 1.7  1998/01/19 18:40:23  curt
285 // Tons of little changes to clean up the code and to remove fatal errors
286 // when building with the c++ compiler.
287 //
288 // Revision 1.6  1998/01/19 18:35:43  curt
289 // Minor tweaks and fixes for cygwin32.
290 //
291 // Revision 1.5  1997/12/30 20:47:37  curt
292 // Integrated new event manager with subsystem initializations.
293 //
294 // Revision 1.4  1997/12/10 22:37:42  curt
295 // Prepended "fg" on the name of all global structures that didn't have it yet.
296 // i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
297 //
298 // Revision 1.3  1997/08/27 03:30:04  curt
299 // Changed naming scheme of basic shared structures.
300 //
301 // Revision 1.2  1997/05/29 22:39:57  curt
302 // Working on incorporating the LaRCsim flight model.
303 //
304 // Revision 1.1  1997/05/29 02:35:04  curt
305 // Initial revision.
306 //