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/newauto.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 current_autopilot->get_AltitudeEnabled();
679 * Set the autopilot altitude lock (true=on).
682 FGBFI::setAPAltitudeLock (bool lock)
684 current_autopilot->set_AltitudeEnabled( true );
685 current_autopilot->set_AltitudeMode( FGAutopilot::FG_ALTITUDE_LOCK );
690 * Get the autopilot target altitude in feet.
693 FGBFI::getAPAltitude ()
695 return current_autopilot->get_TargetAltitude() * METER_TO_FEET;
700 * Set the autopilot target altitude in feet.
703 FGBFI::setAPAltitude (double altitude)
705 current_autopilot->set_TargetAltitude( altitude );
710 * Get the autopilot heading lock (true=on).
713 FGBFI::getAPHeadingLock ()
715 return current_autopilot->get_HeadingEnabled();
720 * Set the autopilot heading lock (true=on).
723 FGBFI::setAPHeadingLock (bool lock)
725 current_autopilot->set_HeadingEnabled( true );
726 current_autopilot->set_HeadingMode( FGAutopilot::FG_HEADING_LOCK );
731 * Get the autopilot target heading in degrees.
734 FGBFI::getAPHeading ()
736 return current_autopilot->get_TargetHeading();
741 * Set the autopilot target heading in degrees.
744 FGBFI::setAPHeading (double heading)
746 current_autopilot->set_TargetHeading( heading );
751 ////////////////////////////////////////////////////////////////////////
753 ////////////////////////////////////////////////////////////////////////
756 FGBFI::getNAV1Freq ()
758 return current_radiostack->get_nav1_freq();
762 FGBFI::getNAV1AltFreq ()
764 return current_radiostack->get_nav1_alt_freq();
768 FGBFI::getNAV1Radial ()
770 return current_radiostack->get_nav1_radial();
774 FGBFI::getNAV2Freq ()
776 return current_radiostack->get_nav2_freq();
780 FGBFI::getNAV2AltFreq ()
782 return current_radiostack->get_nav2_alt_freq();
786 FGBFI::getNAV2Radial ()
788 return current_radiostack->get_nav2_radial();
794 return current_radiostack->get_adf_freq();
798 FGBFI::getADFAltFreq ()
800 return current_radiostack->get_adf_alt_freq();
804 FGBFI::getADFRotation ()
806 return current_radiostack->get_adf_rotation();
810 FGBFI::setNAV1Freq (double freq)
812 current_radiostack->set_nav1_freq(freq);
816 FGBFI::setNAV1AltFreq (double freq)
818 current_radiostack->set_nav1_alt_freq(freq);
822 FGBFI::setNAV1Radial (double radial)
824 current_radiostack->set_nav1_radial(radial);
828 FGBFI::setNAV2Freq (double freq)
830 current_radiostack->set_nav2_freq(freq);
834 FGBFI::setNAV2AltFreq (double freq)
836 current_radiostack->set_nav2_alt_freq(freq);
840 FGBFI::setNAV2Radial (double radial)
842 current_radiostack->set_nav2_radial(radial);
846 FGBFI::setADFFreq (double freq)
848 current_radiostack->set_adf_freq(freq);
852 FGBFI::setADFAltFreq (double freq)
854 current_radiostack->set_adf_alt_freq(freq);
858 FGBFI::setADFRotation (double rot)
860 current_radiostack->set_adf_rotation(rot);
865 ////////////////////////////////////////////////////////////////////////
867 ////////////////////////////////////////////////////////////////////////
871 * Get the autopilot GPS lock (true=on).
876 return ( current_autopilot->get_HeadingEnabled() &&
877 ( current_autopilot->get_HeadingMode() ==
878 FGAutopilot::FG_HEADING_WAYPOINT )
884 * Set the autopilot GPS lock (true=on).
887 FGBFI::setGPSLock (bool lock)
889 current_autopilot->set_HeadingEnabled( true );
890 current_autopilot->set_HeadingMode( FGAutopilot::FG_HEADING_WAYPOINT );
895 * Get the GPS target airport code.
898 FGBFI::getTargetAirport ()
900 return current_options.get_airport_id();
905 * Set the GPS target airport code.
908 FGBFI::setTargetAirport (const string &airportId)
910 current_options.set_airport_id(airportId);
915 * Get the GPS target latitude in degrees (negative for south).
918 FGBFI::getGPSTargetLatitude ()
920 return current_autopilot->get_TargetLatitude();
925 * Set the GPS target latitude in degrees (negative for south).
928 FGBFI::setGPSTargetLatitude (double latitude)
930 current_autopilot->set_TargetLatitude( latitude );
935 * Get the GPS target longitude in degrees (negative for west).
938 FGBFI::getGPSTargetLongitude ()
940 return current_autopilot->get_TargetLongitude();
945 * Set the GPS target longitude in degrees (negative for west).
948 FGBFI::setGPSTargetLongitude (double longitude)
950 current_autopilot->set_TargetLongitude( longitude );
955 ////////////////////////////////////////////////////////////////////////
957 ////////////////////////////////////////////////////////////////////////
961 * Get the current visible (units??).
964 FGBFI::getVisibility ()
966 #ifndef FG_OLD_WEATHER
967 return WeatherDatabase->getWeatherVisibility();
969 return current_weather.get_visibility();
975 * Set the current visibility (units??).
978 FGBFI::setVisibility (double visibility)
980 #ifndef FG_OLD_WEATHER
981 WeatherDatabase->setWeatherVisibility(visibility);
983 current_weather.set_visibility(visibility);
989 ////////////////////////////////////////////////////////////////////////
991 ////////////////////////////////////////////////////////////////////////
994 * Return the magnetic variation
999 return FGTime::cur_time_params->getMagVar() * RAD_TO_DEG;
1004 * Return the magnetic variation
1009 return FGTime::cur_time_params->getMagDip() * RAD_TO_DEG;