1 // cockpit.cxx -- routines to draw a cockpit (initial draft)
3 // Written by Michele America, started September 1997.
5 // Copyright (C) 1997 Michele F. America - nomimarketing@mail.telepac.pt
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.
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.
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.
32 #include <simgear/compiler.h>
40 #include <simgear/constants.h>
41 #include <simgear/debug/logstream.hxx>
42 #include <simgear/math/polar3d.hxx>
43 #include <simgear/props/props.hxx>
44 #include <simgear/timing/sg_time.hxx>
46 #include <Aircraft/aircraft.hxx>
47 #include <Include/general.hxx>
49 #include <FDM/SP/ADA.hxx>
51 #include <Main/globals.hxx>
52 #include <Main/fg_props.hxx>
53 #include <Main/viewmgr.hxx>
54 #include <Scenery/scenery.hxx>
55 #include <Time/fg_timer.hxx>
58 #include "cockpit.hxx"
62 // This is a structure that contains all data related to
63 // cockpit/panel/hud system
65 static pCockpit ac_cockpit;
66 // The following routines obtain information concerntin the aircraft's
67 // current state and return it to calling instrument display routines.
68 // They should eventually be member functions of the aircraft.
71 float get_latitude( void )
73 return current_aircraft.fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
76 float get_lat_min( void )
80 a = current_aircraft.fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
84 d = (double) ( (int) a);
85 float lat_min = (a - d) * 60.0;
91 float get_longitude( void )
93 return current_aircraft.fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
98 get_formated_gmt_time( void )
101 const struct tm *p = globals->get_time_params()->getGmt();
102 sprintf( buf, "%d/%d/%4d %d:%02d:%02d",
103 p->tm_mon+1, p->tm_mday, 1900 + p->tm_year,
104 p->tm_hour, p->tm_min, p->tm_sec);
110 float get_long_min( void )
113 a = current_aircraft.fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
117 d = (double) ( (int) a);
118 float lon_min = (a - d) * 60.0;
123 float get_throttleval( void )
125 // Hack limiting to one engine
126 return globals->get_controls()->get_throttle( 0 );
129 float get_aileronval( void )
131 return globals->get_controls()->get_aileron();
134 float get_elevatorval( void )
136 return globals->get_controls()->get_elevator();
139 float get_elev_trimval( void )
141 return globals->get_controls()->get_elevator_trim();
144 float get_rudderval( void )
146 return globals->get_controls()->get_rudder();
149 float get_speed( void )
151 static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
153 float speed = current_aircraft.fdm_state->get_V_calibrated_kts()
154 * speedup_node->getIntValue();
161 return current_aircraft.fdm_state->get_Mach_number();
164 float get_aoa( void )
166 return current_aircraft.fdm_state->get_Alpha() * SGD_RADIANS_TO_DEGREES;
169 float get_roll( void )
171 return current_aircraft.fdm_state->get_Phi();
174 float get_pitch( void )
176 return current_aircraft.fdm_state->get_Theta();
179 float get_heading( void )
181 return current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES;
184 float get_altitude( void )
186 static const SGPropertyNode *startup_units_node
187 = fgGetNode("/sim/startup/units");
191 if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
192 altitude = current_aircraft.fdm_state->get_Altitude();
194 altitude = (current_aircraft.fdm_state->get_Altitude()
201 float get_agl( void )
203 static const SGPropertyNode *startup_units_node
204 = fgGetNode("/sim/startup/units");
208 if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
209 agl = (current_aircraft.fdm_state->get_Altitude()
210 - current_aircraft.fdm_state->get_Runway_altitude());
212 agl = (current_aircraft.fdm_state->get_Altitude()
213 - current_aircraft.fdm_state->get_Runway_altitude()) * SG_FEET_TO_METER;
219 float get_sideslip( void )
221 return current_aircraft.fdm_state->get_Beta();
224 float get_frame_rate( void )
226 return general.get_frame_rate();
229 float get_fov( void )
231 return globals->get_current_view()->get_fov();
234 float get_vfc_ratio( void )
236 // float vfc = current_view.get_vfc_ratio();
241 float get_vfc_tris_drawn ( void )
243 // float rendered = current_view.get_tris_rendered();
244 // return (rendered);
248 float get_vfc_tris_culled ( void )
250 // float culled = current_view.get_tris_culled();
255 float get_climb_rate( void )
257 static const SGPropertyNode *startup_units_node
258 = fgGetNode("/sim/startup/units");
261 if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
262 climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
264 climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * SG_FEET_TO_METER * 60.0;
271 float get_view_direction( void )
273 double view_off = SGD_2PI - globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS;
274 double view = ( current_aircraft.fdm_state->get_Psi() + view_off)
275 * SGD_RADIANS_TO_DEGREES;
285 // Added by Markus Hof on 5. Jan 2004
286 float get_dme( void )
288 static const SGPropertyNode * dme_node =
289 fgGetNode("/instrumentation/dme/indicated-distance-nm");
291 return dme_node->getFloatValue();
294 // $$$ begin - added, VS Renganathan 13 Oct 2K
295 // #ifdef FIGHTER_HUD
296 float get_Vx ( void )
298 // Curt dont comment this and return zero. - Ranga
299 // Please remove comments from get_V_..() function in flight.hxx
300 float Vxx = current_aircraft.fdm_state->get_V_north_rel_ground();
304 float get_Vy ( void )
306 // Curt dont comment this and return zero. - Ranga
307 // Please remove comments from get_V_..() function in flight.hxx
308 float Vyy = current_aircraft.fdm_state->get_V_east_rel_ground();
312 float get_Vz ( void )
314 // Curt dont comment this and return zero. - Ranga
315 // Please remove comments from get_V_..() function in flight.hxx
316 float Vzz = current_aircraft.fdm_state->get_V_down_rel_ground();
320 float get_Ax ( void )
322 float Ax = current_aircraft.fdm_state->get_V_dot_north();
326 float get_Ay ( void )
328 float Ay = current_aircraft.fdm_state->get_V_dot_east();
332 float get_Az ( void )
334 float Az = current_aircraft.fdm_state->get_V_dot_down();
338 float get_anzg ( void )
340 float anzg = current_aircraft.fdm_state->get_N_Z_cg();
344 #ifdef ENABLE_SP_FMDS
347 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
348 return fdm->get_iaux(1);
353 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
354 return fdm->get_iaux(2);
359 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
360 return fdm->get_iaux(3);
365 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
366 return fdm->get_iaux(4);
371 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
372 return fdm->get_iaux(5);
377 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
378 return fdm->get_iaux(6);
383 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
384 return fdm->get_iaux(7);
389 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
390 return fdm->get_iaux(8);
395 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
396 return fdm->get_iaux(9);
399 int get_iaux10 (void)
401 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
402 return fdm->get_iaux(10);
405 int get_iaux11 (void)
407 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
408 return fdm->get_iaux(11);
411 int get_iaux12 (void)
413 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
414 return fdm->get_iaux(12);
417 float get_aux1 (void)
419 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
420 return fdm->get_daux(1);
423 float get_aux2 (void)
425 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
426 return fdm->get_daux(2);
429 float get_aux3 (void)
431 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
432 return fdm->get_daux(3);
435 float get_aux4 (void)
437 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
438 return fdm->get_daux(4);
441 float get_aux5 (void)
443 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
444 return fdm->get_daux(5);
447 float get_aux6 (void)
449 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
450 return fdm->get_daux(6);
453 float get_aux7 (void)
455 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
456 return fdm->get_daux(7);
459 float get_aux8 (void)
461 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
462 return fdm->get_daux(8);
465 float get_aux9 (void)
467 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
468 return fdm->get_faux(1);
471 float get_aux10 (void)
473 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
474 return fdm->get_faux(2);
477 float get_aux11 (void)
479 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
480 return fdm->get_faux(3);
483 float get_aux12 (void)
485 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
486 return fdm->get_faux(4);
489 float get_aux13 (void)
491 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
492 return fdm->get_faux(5);
495 float get_aux14 (void)
497 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
498 return fdm->get_faux(6);
501 float get_aux15 (void)
503 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
504 return fdm->get_faux(7);
507 float get_aux16 (void)
509 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
510 return fdm->get_faux(8);
513 float get_aux17 (void)
515 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
516 return fdm->get_faux(9);
519 float get_aux18 (void)
521 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
522 return fdm->get_faux(10);
525 // $$$ end - added, VS Renganathan 13 Oct 2K
529 /****************************************************************************/
530 /* Convert degrees to dd mm'ss.s" (DMS-Format) */
531 /****************************************************************************/
532 char *dmshh_format(double degrees)
543 min_part = 60.0 * (degrees - deg_part);
544 sec_part = 3600.0 * (degrees - deg_part - min_part / 60.0);
546 /* Round off hundredths */
547 if (sec_part + 0.005 >= 60.0)
548 sec_part -= 60.0, min_part += 1;
550 min_part -= 60, deg_part += 1;
552 sprintf(buf,"%02d*%02d %05.2f",deg_part,min_part,sec_part);
559 /************************************************************************
560 Convert degrees to dd mm.mmm' (DMM-Format)
561 Description: Converts using a round-off factor tailored to the required
562 precision of the minutes field (three decimal places). Round-off
563 prevents function from returning a minutes value of 60.
565 Input arguments: Coordinate value in decimal degrees
567 ************************************************************************/
568 static char *toDM(float dd)
579 /* round for minutes expressed to three decimal places */
580 tempdd = fabs(dd) + (5.0E-4 / 60.0);
582 mn = fabs( (tempdd - (double)(deg)) * 60.0 - 4.999E-4 );
584 sprintf(dm, "%d*%06.3f", deg, mn);
589 /************************************************************************
590 Convert degrees to dd mm'ss.s'' (DMS-Format)
591 Description: Converts using a round-off factor tailored to the required
592 precision of the seconds field (one decimal place). Round-off
593 prevents function from returning a seconds value of 60.
595 Input arguments: Coordinate value in decimal degrees
597 ************************************************************************/
598 static char *toDMS(float dd)
601 double tempdd, tempmin;
610 /* round up for seconds expressed to one decimal place */
611 tempdd = fabs(dd) + (0.05 / 3600.0);
613 tempmin = (tempdd - (double)(deg)) * 60.0;
615 sec = fabs( (tempmin - (double)(mn)) * 60.0 - 0.049 );
617 sprintf(dms, "%d*%02d %04.1f", deg, mn, sec);
622 // Have to set the LatLon display type
623 //static char *(*fgLatLonFormat)(float) = toDM;
624 static char *(*fgLatLonFormat)(float);
626 char *coord_format_lat(float latitude)
631 // dmshh_format(latitude),
634 fgLatLonFormat(latitude),
635 latitude > 0 ? 'N' : 'S');
639 char *coord_format_lon(float longitude)
644 // dmshh_format(longitude),
647 fgLatLonFormat(longitude),
648 longitude > 0 ? 'E' : 'W');
652 void fgLatLonFormatToggle( puObject *)
654 static bool toggle = false;
657 fgLatLonFormat = toDM;
659 fgLatLonFormat = toDMS;
665 char *coord_format_latlon(double latitude, double longitude)
667 static char buf[1024];
669 sprintf(buf,"%s%c %s%c",
670 dmshh_format(latitude),
671 latitude > 0 ? 'N' : 'S',
672 dmshh_format(longitude),
673 longitude > 0 ? 'E' : 'W');
679 bool fgCockpitInit( fgAIRCRAFT *cur_aircraft )
681 SG_LOG( SG_COCKPIT, SG_INFO, "Initializing cockpit subsystem" );
683 // cockpit->code = 1; /* It will be aircraft dependent */
684 // cockpit->status = 0;
686 // If aircraft has HUD specified we will get the specs from its def
687 // file. For now we will depend upon hard coding in hud?
689 // We must insure that the existing instrument link is purged.
690 // This is done by deleting the links in the list.
692 // HI_Head is now a null pointer so we can generate a new list from the
695 fgHUDInit( cur_aircraft );
696 ac_cockpit = new fg_Cockpit();
698 // Have to set the LatLon display type
699 fgLatLonFormat = toDM;
701 SG_LOG( SG_COCKPIT, SG_INFO,
702 " Code " << ac_cockpit->code() << " Status "
703 << ac_cockpit->status() );
708 void fgCockpitUpdate( void ) {
710 SG_LOG( SG_COCKPIT, SG_DEBUG,
711 "Cockpit: code " << ac_cockpit->code() << " status "
712 << ac_cockpit->status() );
714 static const SGPropertyNode * xsize_node = fgGetNode("/sim/startup/xsize");
715 static const SGPropertyNode * ysize_node = fgGetNode("/sim/startup/ysize");
716 static const SGPropertyNode * hud_visibility_node
717 = fgGetNode("/sim/hud/visibility");
719 int iwidth = xsize_node->getIntValue();
720 int iheight = ysize_node->getIntValue();
722 // FIXME: inefficient
723 if ( hud_visibility_node->getBoolValue() ) {
724 // This will check the global hud linked list pointer.
725 // If there is anything to draw it will.
729 glViewport( 0, 0, iwidth, iheight );
741 { "aileronval", get_aileronval },
742 { "altitude", get_altitude },
743 { "anzg", get_anzg },
746 { "climb", get_climb_rate },
747 { "elevatortrimval", get_elev_trimval },
748 { "elevatorval", get_elevatorval },
750 { "framerate", get_frame_rate },
751 { "heading", get_heading },
752 { "latitude", get_latitude },
753 { "longitude", get_longitude },
754 { "mach", get_mach },
755 { "rudderval", get_rudderval },
756 { "speed", get_speed },
757 { "throttleval", get_throttleval },
758 { "view_direction", get_view_direction },
759 { "vfc_tris_culled", get_vfc_tris_culled },
760 { "vfc_tris_drawn", get_vfc_tris_drawn },
761 #ifdef ENABLE_SP_FMDS
762 { "aux1", get_aux1 },
763 { "aux2", get_aux2 },
764 { "aux3", get_aux3 },
765 { "aux4", get_aux4 },
766 { "aux5", get_aux5 },
767 { "aux6", get_aux6 },
768 { "aux7", get_aux7 },
769 { "aux8", get_aux8 },
770 { "aux9", get_aux9 },
771 { "aux10", get_aux10 },
772 { "aux11", get_aux11 },
773 { "aux12", get_aux12 },
774 { "aux13", get_aux13 },
775 { "aux14", get_aux14 },
776 { "aux15", get_aux15 },
777 { "aux16", get_aux16 },
778 { "aux17", get_aux17 },
779 { "aux18", get_aux18 },
785 FLTFNPTR get_func(const char *name)
787 for (int i = 0; fn_table[i].name; i++)
788 if (!strcmp(fn_table[i].name, name))
789 return fn_table[i].func;