1 // bfi.cxx - Big Friendly Interface implementation
3 // Written by David Megginson, started February, 2000.
5 // Copyright (C) 2000 David Megginson - david@megginson.com
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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #if defined( FG_HAVE_NATIVE_SGI_COMPILERS )
29 # include <iostream.h>
34 #include <simgear/constants.h>
35 #include <simgear/math/fg_types.hxx>
37 #include <Aircraft/aircraft.hxx>
38 #include <Controls/controls.hxx>
39 #include <Autopilot/autopilot.hxx>
40 #include <Scenery/scenery.hxx>
41 #include <Time/fg_time.hxx>
42 #include <Time/light.hxx>
43 #include <Cockpit/radiostack.hxx>
44 #ifndef FG_OLD_WEATHER
45 # include <WeatherCM/FGLocalWeatherDatabase.h>
47 # include <Weather/weather.hxx>
50 #include "options.hxx"
52 #include "fg_init.hxx"
54 FG_USING_NAMESPACE(std);
56 // FIXME: these are not part of the
57 // published interface!!!
58 extern fgAPDataPtr APDataGlobal;
59 extern void fgAPAltitudeSet (double new_altitude);
60 extern void fgAPHeadingSet (double new_heading);
67 ////////////////////////////////////////////////////////////////////////
69 ////////////////////////////////////////////////////////////////////////
71 bool FGBFI::_needReinit = false;
75 ////////////////////////////////////////////////////////////////////////
77 ////////////////////////////////////////////////////////////////////////
81 * Reinitialize FGFS if required.
83 * Some changes (especially those in aircraft position) require that
84 * FGFS be reinitialized afterwards. Rather than reinitialize after
85 * every change, the setter methods simply set a flag so that there
86 * can be a single reinit at the end of the frame.
98 * Reinitialize FGFS to use the new BFI settings.
103 // Save the state of everything
104 // that's going to get clobbered
105 // when we reinit the subsystems.
107 // TODO: add more AP stuff
108 double elevator = getElevator();
109 double aileron = getAileron();
110 double rudder = getRudder();
111 double throttle = getThrottle();
112 double elevator_trim = getElevatorTrim();
113 double flaps = getFlaps();
114 double brake = getBrake();
115 bool apHeadingLock = getAPHeadingLock();
116 double apHeading = getAPHeading();
117 bool apAltitudeLock = getAPAltitudeLock();
118 double apAltitude = getAPAltitude();
119 const string &targetAirport = getTargetAirport();
120 bool gpsLock = getGPSLock();
121 double gpsLatitude = getGPSTargetLatitude();
122 double gpsLongitude = getGPSTargetLongitude();
124 fgReInitSubsystems();
125 // solarSystemRebuild();
126 cur_light_params.Update();
128 // Restore all of the old states.
129 setElevator(elevator);
132 setThrottle(throttle);
133 setElevatorTrim(elevator_trim);
136 setAPHeadingLock(apHeadingLock);
137 setAPHeading(apHeading);
138 setAPAltitudeLock(apAltitudeLock);
139 setAPAltitude(apAltitude);
140 setTargetAirport(targetAirport);
142 setGPSTargetLatitude(gpsLatitude);
143 setGPSTargetLongitude(gpsLongitude);
150 ////////////////////////////////////////////////////////////////////////
152 ////////////////////////////////////////////////////////////////////////
156 * Return the flight model as an integer.
158 * TODO: use a string instead.
161 FGBFI::getFlightModel ()
163 return current_options.get_flight_model();
168 * Set the flight model as an integer.
170 * TODO: use a string instead.
173 FGBFI::setFlightModel (int model)
175 current_options.set_flight_model(model);
181 * Return the current Zulu time.
186 // FIXME: inefficient
187 return mktime(FGTime::cur_time_params->getGmt());
192 * Set the current Zulu time.
195 FGBFI::setTimeGMT (time_t time)
197 // FIXME: need to update lighting
199 current_options.set_time_offset(time);
200 current_options.set_time_offset_type(fgOPTIONS::FG_TIME_GMT_ABSOLUTE);
201 FGTime::cur_time_params->init( cur_fdm_state->get_Longitude(),
202 cur_fdm_state->get_Latitude() );
203 FGTime::cur_time_params->update( cur_fdm_state->get_Longitude(),
204 cur_fdm_state->get_Latitude(),
205 cur_fdm_state->get_Altitude()
212 * Return true if the HUD is visible.
215 FGBFI::getHUDVisible ()
217 return current_options.get_hud_status();
222 * Ensure that the HUD is visible or hidden.
225 FGBFI::setHUDVisible (bool visible)
227 current_options.set_hud_status(visible);
232 * Return true if the 2D panel is visible.
235 FGBFI::getPanelVisible ()
237 return current_options.get_panel_status();
242 * Ensure that the 2D panel is visible or hidden.
245 FGBFI::setPanelVisible (bool visible)
247 if (current_options.get_panel_status() != visible) {
248 current_options.toggle_panel();
254 ////////////////////////////////////////////////////////////////////////
256 ////////////////////////////////////////////////////////////////////////
260 * Return the current latitude in degrees (negative for south).
263 FGBFI::getLatitude ()
265 return current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
270 * Set the current latitude in degrees (negative for south).
273 FGBFI::setLatitude (double latitude)
275 current_options.set_lat(latitude);
281 * Return the current longitude in degrees (negative for west).
284 FGBFI::getLongitude ()
286 return current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
291 * Set the current longitude in degrees (negative for west).
294 FGBFI::setLongitude (double longitude)
296 current_options.set_lon(longitude);
302 * Return the current altitude in feet.
305 FGBFI::getAltitude ()
307 return current_aircraft.fdm_state->get_Altitude();
313 * Return the current altitude in above the terrain.
318 return current_aircraft.fdm_state->get_Altitude()
319 - scenery.cur_elev * METER_TO_FEET;
324 * Set the current altitude in feet.
327 FGBFI::setAltitude (double altitude)
329 current_options.set_altitude(altitude * FEET_TO_METER);
335 ////////////////////////////////////////////////////////////////////////
337 ////////////////////////////////////////////////////////////////////////
341 * Return the current heading in degrees.
346 return current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG;
351 * Set the current heading in degrees.
354 FGBFI::setHeading (double heading)
356 current_options.set_heading(heading);
362 * Return the current pitch in degrees.
367 return current_aircraft.fdm_state->get_Theta() * RAD_TO_DEG;
372 * Set the current pitch in degrees.
375 FGBFI::setPitch (double pitch)
378 current_options.set_pitch(pitch);
384 * Return the current roll in degrees.
389 return current_aircraft.fdm_state->get_Phi() * RAD_TO_DEG;
394 * Set the current roll in degrees.
397 FGBFI::setRoll (double roll)
399 current_options.set_roll(roll);
405 ////////////////////////////////////////////////////////////////////////
407 ////////////////////////////////////////////////////////////////////////
411 * Return the current airspeed in knots.
414 FGBFI::getAirspeed ()
416 // FIXME: should we add speed-up?
417 return current_aircraft.fdm_state->get_V_calibrated_kts();
422 * Return the current sideslip (FIXME: units unknown).
425 FGBFI::getSideSlip ()
427 return current_aircraft.fdm_state->get_Beta();
432 * Return the current climb rate in feet/second (FIXME: verify).
435 FGBFI::getVerticalSpeed ()
437 // What about meters?
438 return current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
443 * Get the current north velocity (units??).
446 FGBFI::getSpeedNorth ()
448 return current_aircraft.fdm_state->get_V_north();
453 * Set the current north velocity (units??).
456 FGBFI::setSpeedNorth (double speed)
458 current_options.set_uBody(speed);
464 * Get the current east velocity (units??).
467 FGBFI::getSpeedEast ()
469 return current_aircraft.fdm_state->get_V_east();
474 * Set the current east velocity (units??).
477 FGBFI::setSpeedEast (double speed)
479 current_options.set_vBody(speed);
485 * Get the current down velocity (units??).
488 FGBFI::getSpeedDown ()
490 return current_aircraft.fdm_state->get_V_down();
495 * Set the current down velocity (units??).
498 FGBFI::setSpeedDown (double speed)
500 current_options.set_wBody(speed);
506 ////////////////////////////////////////////////////////////////////////
508 ////////////////////////////////////////////////////////////////////////
512 * Get the throttle setting, from 0.0 (none) to 1.0 (full).
515 FGBFI::getThrottle ()
517 // FIXME: add throttle selector
518 return controls.get_throttle(0);
523 * Set the throttle, from 0.0 (none) to 1.0 (full).
526 FGBFI::setThrottle (double throttle)
528 // FIXME: allow throttle selection
530 controls.set_throttle(0, throttle);
535 * Get the flaps setting, from 0.0 (none) to 1.0 (full).
540 return controls.get_flaps();
545 * Set the flaps, from 0.0 (none) to 1.0 (full).
548 FGBFI::setFlaps (double flaps)
551 controls.set_flaps(flaps);
556 * Get the aileron, from -1.0 (left) to 1.0 (right).
561 return controls.get_aileron();
566 * Set the aileron, from -1.0 (left) to 1.0 (right).
569 FGBFI::setAileron (double aileron)
572 controls.set_aileron(aileron);
577 * Get the rudder setting, from -1.0 (left) to 1.0 (right).
582 return controls.get_rudder();
587 * Set the rudder, from -1.0 (left) to 1.0 (right).
590 FGBFI::setRudder (double rudder)
593 controls.set_rudder(rudder);
598 * Get the elevator setting, from -1.0 (down) to 1.0 (up).
601 FGBFI::getElevator ()
603 return controls.get_elevator();
608 * Set the elevator, from -1.0 (down) to 1.0 (up).
611 FGBFI::setElevator (double elevator)
614 controls.set_elevator(elevator);
619 * Get the elevator trim, from -1.0 (down) to 1.0 (up).
622 FGBFI::getElevatorTrim ()
624 return controls.get_elevator_trim();
629 * Set the elevator trim, from -1.0 (down) to 1.0 (up).
632 FGBFI::setElevatorTrim (double trim)
635 controls.set_elevator_trim(trim);
640 * Get the brake setting, from 0.0 (none) to 1.0 (full).
645 // FIXME: add brake selector
646 return controls.get_brake(0);
651 * Set the brake, from 0.0 (none) to 1.0 (full).
654 FGBFI::setBrake (double brake)
657 // FIXME: allow brake selection
658 controls.set_brake(0, brake);
663 ////////////////////////////////////////////////////////////////////////
665 ////////////////////////////////////////////////////////////////////////
669 * Get the autopilot altitude lock (true=on).
672 FGBFI::getAPAltitudeLock ()
674 return fgAPAltitudeEnabled();
679 * Set the autopilot altitude lock (true=on).
682 FGBFI::setAPAltitudeLock (bool lock)
684 APDataGlobal->altitude_hold = lock;
689 * Get the autopilot target altitude in feet.
692 FGBFI::getAPAltitude ()
694 return fgAPget_TargetAltitude() * METER_TO_FEET;
699 * Set the autopilot target altitude in feet.
702 FGBFI::setAPAltitude (double altitude)
704 fgAPAltitudeSet(altitude);
709 * Get the autopilot heading lock (true=on).
712 FGBFI::getAPHeadingLock ()
714 return fgAPHeadingEnabled();
719 * Set the autopilot heading lock (true=on).
722 FGBFI::setAPHeadingLock (bool lock)
724 APDataGlobal->heading_hold = lock;
729 * Get the autopilot target heading in degrees.
732 FGBFI::getAPHeading ()
734 return fgAPget_TargetHeading();
739 * Set the autopilot target heading in degrees.
742 FGBFI::setAPHeading (double heading)
744 fgAPHeadingSet(heading);
749 ////////////////////////////////////////////////////////////////////////
751 ////////////////////////////////////////////////////////////////////////
754 FGBFI::getNAV1Freq ()
756 return current_radiostack->get_nav1_freq();
760 FGBFI::getNAV1AltFreq ()
762 return current_radiostack->get_nav1_alt_freq();
766 FGBFI::getNAV1SelRadial ()
768 return current_radiostack->get_nav1_sel_radial();
772 FGBFI::getNAV1Radial ()
774 return current_radiostack->get_nav1_radial();
778 FGBFI::getNAV2Freq ()
780 return current_radiostack->get_nav2_freq();
784 FGBFI::getNAV2AltFreq ()
786 return current_radiostack->get_nav2_alt_freq();
790 FGBFI::getNAV2SelRadial ()
792 return current_radiostack->get_nav2_sel_radial();
796 FGBFI::getNAV2Radial ()
798 return current_radiostack->get_nav2_radial();
804 return current_radiostack->get_adf_freq();
808 FGBFI::getADFAltFreq ()
810 return current_radiostack->get_adf_alt_freq();
814 FGBFI::getADFRotation ()
816 return current_radiostack->get_adf_rotation();
820 FGBFI::setNAV1Freq (double freq)
822 current_radiostack->set_nav1_freq(freq);
826 FGBFI::setNAV1AltFreq (double freq)
828 current_radiostack->set_nav1_alt_freq(freq);
832 FGBFI::setNAV1SelRadial (double radial)
834 current_radiostack->set_nav1_sel_radial(radial);
838 FGBFI::setNAV2Freq (double freq)
840 current_radiostack->set_nav2_freq(freq);
844 FGBFI::setNAV2AltFreq (double freq)
846 current_radiostack->set_nav2_alt_freq(freq);
850 FGBFI::setNAV2SelRadial (double radial)
852 current_radiostack->set_nav2_sel_radial(radial);
856 FGBFI::setADFFreq (double freq)
858 current_radiostack->set_adf_freq(freq);
862 FGBFI::setADFAltFreq (double freq)
864 current_radiostack->set_adf_alt_freq(freq);
868 FGBFI::setADFRotation (double rot)
870 current_radiostack->set_adf_rotation(rot);
875 ////////////////////////////////////////////////////////////////////////
877 ////////////////////////////////////////////////////////////////////////
881 * Get the autopilot GPS lock (true=on).
886 return fgAPWayPointEnabled();
891 * Set the autopilot GPS lock (true=on).
894 FGBFI::setGPSLock (bool lock)
896 APDataGlobal->waypoint_hold = lock;
901 * Get the GPS target airport code.
904 FGBFI::getTargetAirport ()
906 return current_options.get_airport_id();
911 * Set the GPS target airport code.
914 FGBFI::setTargetAirport (const string &airportId)
916 current_options.set_airport_id(airportId);
921 * Get the GPS target latitude in degrees (negative for south).
924 FGBFI::getGPSTargetLatitude ()
926 return fgAPget_TargetLatitude();
931 * Set the GPS target latitude in degrees (negative for south).
934 FGBFI::setGPSTargetLatitude (double latitude)
936 APDataGlobal->TargetLatitude = latitude;
941 * Get the GPS target longitude in degrees (negative for west).
944 FGBFI::getGPSTargetLongitude ()
946 return fgAPget_TargetLongitude();
951 * Set the GPS target longitude in degrees (negative for west).
954 FGBFI::setGPSTargetLongitude (double longitude)
956 APDataGlobal->TargetLongitude = longitude;
961 ////////////////////////////////////////////////////////////////////////
963 ////////////////////////////////////////////////////////////////////////
967 * Get the current visible (units??).
970 FGBFI::getVisibility ()
972 #ifndef FG_OLD_WEATHER
973 return WeatherDatabase->getWeatherVisibility();
975 return current_weather.get_visibility();
981 * Set the current visibility (units??).
984 FGBFI::setVisibility (double visibility)
986 #ifndef FG_OLD_WEATHER
987 WeatherDatabase->setWeatherVisibility(visibility);
989 current_weather.set_visibility(visibility);
995 ////////////////////////////////////////////////////////////////////////
997 ////////////////////////////////////////////////////////////////////////
1000 * Return the magnetic variation
1005 return FGTime::cur_time_params->getMagVar() * RAD_TO_DEG;
1010 * Return the magnetic variation
1015 return FGTime::cur_time_params->getMagDip() * RAD_TO_DEG;