]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/cockpit.cxx
Fix two bugs in the new autopilot code
[flightgear.git] / src / Cockpit / cockpit.cxx
1 // cockpit.cxx -- routines to draw a cockpit (initial draft)
2 //
3 // Written by Michele America, started September 1997.
4 //
5 // Copyright (C) 1997  Michele F. America  - nomimarketing@mail.telepac.pt
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/compiler.h>
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #include <simgear/constants.h>
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/props/props.hxx>
37 #include <simgear/timing/sg_time.hxx>
38
39 #include <Aircraft/aircraft.hxx>
40 #include <Include/general.hxx>
41
42 #include <Main/globals.hxx>
43 #include <Main/fg_props.hxx>
44 #include <Main/viewmgr.hxx>
45 #include <Main/viewer.hxx>
46 #include <Scenery/scenery.hxx>
47 #include <GUI/gui.h>
48
49 #include "cockpit.hxx"
50 #include "hud.hxx"
51
52 // The following routines obtain information concerntin the aircraft's
53 // current state and return it to calling instrument display routines.
54 // They should eventually be member functions of the aircraft.
55 //
56
57 float get_latitude( void )
58 {
59     return fgGetDouble("/position/latitude-deg");
60 }
61
62 float get_lat_min( void )
63 {
64     double a, d;
65
66     a = fgGetDouble("/position/latitude-deg");
67     if (a < 0.0) {
68         a = -a;
69     }
70     d = (double) ( (int) a);
71     float lat_min = (a - d) * 60.0;
72
73     return lat_min;
74 }
75
76
77 float get_longitude( void )
78 {
79     return fgGetDouble("/position/longitude-deg");
80 }
81
82
83 char*
84 get_formated_gmt_time( void )
85 {
86     static char buf[32];
87     const struct tm *p = globals->get_time_params()->getGmt();
88     sprintf( buf, "%d/%d/%4d %d:%02d:%02d",
89          p->tm_mon+1, p->tm_mday, 1900 + p->tm_year,
90          p->tm_hour, p->tm_min, p->tm_sec);
91
92     return buf;
93 }
94
95
96 float get_long_min( void )
97 {
98     double  a, d;
99     a = fgGetDouble("/position/longitude-deg");
100     if (a < 0.0) {
101         a = -a;
102     }
103     d = (double) ( (int) a);
104     float lon_min = (a - d) * 60.0;
105
106     return lon_min;
107 }
108
109 float get_throttleval( void )
110 {
111     // Hack limiting to one engine
112     return globals->get_controls()->get_throttle( 0 );
113 }
114
115 float get_aileronval( void )
116 {
117     return globals->get_controls()->get_aileron();
118 }
119
120 float get_elevatorval( void )
121 {
122     return globals->get_controls()->get_elevator();
123 }
124
125 float get_elev_trimval( void )
126 {
127     return globals->get_controls()->get_elevator_trim();
128 }
129
130 float get_rudderval( void )
131 {
132     return globals->get_controls()->get_rudder();
133 }
134
135 float get_speed( void )
136 {
137     static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
138
139     float speed = fgGetDouble("/velocities/airspeed-kt")
140         * speedup_node->getIntValue();
141
142     return speed;
143 }
144
145 float get_mach(void)
146 {
147     return fgGetDouble("/velocities/mach");
148 }
149
150 float get_aoa( void )
151 {
152     return fgGetDouble("/orientation/alpha-deg");
153 }
154
155 float get_roll( void )
156 {
157     return fgGetDouble("/orientation/roll-deg") * SG_DEGREES_TO_RADIANS;
158 }
159
160 float get_pitch( void )
161 {
162     return fgGetDouble("/orientation/pitch-deg") * SG_DEGREES_TO_RADIANS;
163 }
164
165 float get_heading( void )
166 {
167     return fgGetDouble("/orientation/heading-deg");
168 }
169
170 float get_altitude( void )
171 {
172     static const SGPropertyNode *startup_units_node
173         = fgGetNode("/sim/startup/units");
174
175     if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
176         return fgGetDouble("/position/altitude-ft");
177     } else {
178         return fgGetDouble("/position/altitude-ft") * SG_FEET_TO_METER;
179     }
180 }
181
182 float get_agl( void )
183 {
184     static const SGPropertyNode *startup_units_node
185         = fgGetNode("/sim/startup/units");
186
187     if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
188         return fgGetDouble("/position/altitude-agl-ft");
189     } else {
190         return fgGetDouble("/position/altitude-agl-ft") * SG_FEET_TO_METER;
191     }
192 }
193
194 float get_sideslip( void )
195 {
196     return fgGetDouble("/orientation/side-slip-rad");
197 }
198
199 float get_frame_rate( void )
200 {
201     return general.get_frame_rate();
202 }
203
204 float get_fov( void )
205 {
206     return globals->get_current_view()->get_fov();
207 }
208
209 float get_vfc_ratio( void )
210 {
211     // float vfc = current_view.get_vfc_ratio();
212     // return (vfc);
213     return 0.0;
214 }
215
216 float get_vfc_tris_drawn   ( void )
217 {
218     // float rendered = current_view.get_tris_rendered();
219     // return (rendered);
220     return 0.0;
221 }
222
223 float get_vfc_tris_culled   ( void )
224 {
225     // float culled = current_view.get_tris_culled();
226     // return (culled);
227     return 0.0;
228 }
229
230 float get_climb_rate( void )
231 {
232     static const SGPropertyNode *startup_units_node
233         = fgGetNode("/sim/startup/units");
234
235     float climb_rate = fgGetDouble("/velocities/vertical-speed-fps", 0.0);
236     if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
237         climb_rate *= 60.0;
238     } else {
239         climb_rate *= SG_FEET_TO_METER * 60.0;
240     }
241
242     return climb_rate;
243 }
244
245
246 float get_view_direction( void )
247 {
248     double view_off = 360.0 - globals->get_current_view()->getHeadingOffset_deg();
249     double view = SGMiscd::normalizeAngle(fgGetDouble("/orientation/heading-deg") + view_off);
250     return view;
251 }
252
253 // Added by Markus Hof on 5. Jan 2004
254 float get_dme( void )
255 {
256     static const SGPropertyNode * dme_node =
257         fgGetNode("/instrumentation/dme/indicated-distance-nm");
258
259     return dme_node->getFloatValue();
260 }
261
262 float get_Ax   ( void )
263 {
264     return fgGetDouble("/accelerations/ned/north-accel-fps_sec", 0.0);
265 }
266
267 float get_anzg   ( void )
268 {
269     return fgGetDouble("/accelerations/n-z-cg-fps_sec", 0.0);
270 }
271
272 #ifdef ENABLE_SP_FDM
273 float get_aux1 (void)
274 {
275     return fgGetDouble("/fdm-ada/ship-lat", 0.0);
276 }
277
278 float get_aux2 (void)
279 {
280     return fgGetDouble("/fdm-ada/ship-lon", 0.0);
281 }
282
283 float get_aux3 (void)
284 {
285     return fgGetDouble("/fdm-ada/ship-alt", 0.0);
286 }
287
288 float get_aux4 (void)
289 {
290     return fgGetDouble("/fdm-ada/skijump-dist", 0.0);
291 }
292
293 float get_aux5 (void)
294 {
295     return fgGetDouble("/fdm-ada/aux5", 0.0);
296 }
297
298 float get_aux6 (void)
299 {
300     return fgGetDouble("/fdm-ada/aux6", 0.0);
301 }
302
303 float get_aux7 (void)
304 {
305     return fgGetDouble("/fdm-ada/aux7", 0.0);
306 }
307
308 float get_aux8 (void)
309 {
310     return fgGetDouble("/fdm-ada/aux8", 0.0);}
311
312 float get_aux9 (void)
313 {
314     return fgGetDouble("/fdm-ada/aux9", 0.0);}
315
316 float get_aux10 (void)
317 {
318     return fgGetDouble("/fdm-ada/aux10", 0.0);
319 }
320
321 float get_aux11 (void)
322 {
323     return fgGetDouble("/fdm-ada/aux11", 0.0);
324 }
325
326 float get_aux12 (void)
327 {
328     return fgGetDouble("/fdm-ada/aux12", 0.0);
329 }
330
331 float get_aux13 (void)
332 {
333     return fgGetDouble("/fdm-ada/aux13", 0.0);
334 }
335
336 float get_aux14 (void)
337 {
338     return fgGetDouble("/fdm-ada/aux14", 0.0);
339 }
340
341 float get_aux15 (void)
342 {
343     return fgGetDouble("/fdm-ada/aux15", 0.0);
344 }
345
346 float get_aux16 (void)
347 {
348     return fgGetDouble("/fdm-ada/aux16", 0.0);
349 }
350
351 float get_aux17 (void)
352 {
353     return fgGetDouble("/fdm-ada/aux17", 0.0);
354 }
355
356 float get_aux18 (void)
357 {
358     return fgGetDouble("/fdm-ada/aux18", 0.0);
359 }
360 #endif
361
362
363 bool fgCockpitInit( fgAIRCRAFT *cur_aircraft )
364 {
365     SG_LOG( SG_COCKPIT, SG_INFO, "Initializing cockpit subsystem" );
366
367     //  cockpit->code = 1;  /* It will be aircraft dependent */
368     //  cockpit->status = 0;
369
370     // If aircraft has HUD specified we will get the specs from its def
371     // file. For now we will depend upon hard coding in hud?
372
373     // We must insure that the existing instrument link is purged.
374     // This is done by deleting the links in the list.
375
376     // HI_Head is now a null pointer so we can generate a new list from the
377     // current aircraft.
378
379     fgHUDInit( cur_aircraft );
380
381     return true;
382 }
383
384 void fgCockpitUpdate( osg::State* state ) {
385
386     static const SGPropertyNode * xsize_node = fgGetNode("/sim/startup/xsize");
387     static const SGPropertyNode * ysize_node = fgGetNode("/sim/startup/ysize");
388     static const SGPropertyNode * hud_visibility_node
389         = fgGetNode("/sim/hud/visibility");
390
391     int iwidth   = xsize_node->getIntValue();
392     int iheight  = ysize_node->getIntValue();
393
394                                 // FIXME: inefficient
395     if ( hud_visibility_node->getBoolValue() ) {
396         // This will check the global hud linked list pointer.
397         // If there is anything to draw it will.
398         fgUpdateHUD( state );
399     }
400
401     glViewport( 0, 0, iwidth, iheight );
402 }
403
404
405
406
407
408 struct FuncTable {
409     const char *name;
410     FLTFNPTR func;
411 } fn_table[] = {
412     { "agl", get_agl },
413     { "aileronval", get_aileronval },
414     { "altitude", get_altitude },
415     { "anzg", get_anzg },
416     { "aoa", get_aoa },
417     { "ax", get_Ax },
418     { "climb", get_climb_rate },
419     { "elevatortrimval", get_elev_trimval },
420     { "elevatorval", get_elevatorval },
421     { "fov", get_fov },
422     { "framerate", get_frame_rate },
423     { "heading", get_heading },
424     { "latitude", get_latitude },
425     { "longitude", get_longitude },
426     { "mach", get_mach },
427     { "rudderval", get_rudderval },
428     { "speed", get_speed },
429     { "throttleval", get_throttleval },
430     { "view_direction", get_view_direction },
431     { "vfc_tris_culled", get_vfc_tris_culled },
432     { "vfc_tris_drawn", get_vfc_tris_drawn },
433 #ifdef ENABLE_SP_FDM
434     { "aux1", get_aux1 },
435     { "aux2", get_aux2 },
436     { "aux3", get_aux3 },
437     { "aux4", get_aux4 },
438     { "aux5", get_aux5 },
439     { "aux6", get_aux6 },
440     { "aux7", get_aux7 },
441     { "aux8", get_aux8 },
442     { "aux9", get_aux9 },
443     { "aux10", get_aux10 },
444     { "aux11", get_aux11 },
445     { "aux12", get_aux12 },
446     { "aux13", get_aux13 },
447     { "aux14", get_aux14 },
448     { "aux15", get_aux15 },
449     { "aux16", get_aux16 },
450     { "aux17", get_aux17 },
451     { "aux18", get_aux18 },
452 #endif
453     { 0, 0 },
454 };
455
456
457 FLTFNPTR get_func(const char *name)
458 {
459     for (int i = 0; fn_table[i].name; i++)
460         if (!strcmp(fn_table[i].name, name))
461             return fn_table[i].func;
462
463     return 0;
464 }
465
466