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/debug/logstream.hxx>
36 #include <simgear/math/fg_types.hxx>
38 #include <Aircraft/aircraft.hxx>
39 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
40 #include <Controls/controls.hxx>
41 #include <Autopilot/newauto.hxx>
42 #include <Scenery/scenery.hxx>
43 #include <Time/fg_time.hxx>
44 #include <Time/light.hxx>
45 #include <Cockpit/radiostack.hxx>
46 #ifndef FG_OLD_WEATHER
47 # include <WeatherCM/FGLocalWeatherDatabase.h>
49 # include <Weather/weather.hxx>
52 #include "options.hxx"
54 #include "fg_init.hxx"
56 FG_USING_NAMESPACE(std);
58 // FIXME: these are not part of the
59 // published interface!!!
60 // extern fgAPDataPtr APDataGlobal;
61 // extern void fgAPAltitudeSet (double new_altitude);
62 // extern void fgAPHeadingSet (double new_heading);
69 ////////////////////////////////////////////////////////////////////////
71 ////////////////////////////////////////////////////////////////////////
73 bool FGBFI::_needReinit = false;
77 ////////////////////////////////////////////////////////////////////////
79 ////////////////////////////////////////////////////////////////////////
83 * Reinitialize FGFS if required.
85 * Some changes (especially those in aircraft position) require that
86 * FGFS be reinitialized afterwards. Rather than reinitialize after
87 * every change, the setter methods simply set a flag so that there
88 * can be a single reinit at the end of the frame.
100 * Reinitialize FGFS to use the new BFI settings.
105 // Save the state of everything
106 // that's going to get clobbered
107 // when we reinit the subsystems.
109 cout << "BFI: start reinit\n";
111 // TODO: add more AP stuff
112 double elevator = getElevator();
113 double aileron = getAileron();
114 double rudder = getRudder();
115 double throttle = getThrottle();
116 double elevator_trim = getElevatorTrim();
117 double flaps = getFlaps();
118 double brake = getBrake();
119 bool apHeadingLock = getAPHeadingLock();
120 double apHeading = getAPHeading();
121 bool apAltitudeLock = getAPAltitudeLock();
122 double apAltitude = getAPAltitude();
123 const string &targetAirport = getTargetAirport();
124 bool gpsLock = getGPSLock();
125 double gpsLatitude = getGPSTargetLatitude();
126 double gpsLongitude = getGPSTargetLongitude();
128 fgReInitSubsystems();
129 // solarSystemRebuild();
130 cur_light_params.Update();
132 // Restore all of the old states.
133 setElevator(elevator);
136 setThrottle(throttle);
137 setElevatorTrim(elevator_trim);
140 setAPHeadingLock(apHeadingLock);
141 setAPHeading(apHeading);
142 setAPAltitudeLock(apAltitudeLock);
143 setAPAltitude(apAltitude);
144 setTargetAirport(targetAirport);
146 setGPSTargetLatitude(gpsLatitude);
147 setGPSTargetLongitude(gpsLongitude);
151 cout << "BFI: end reinit\n";
156 ////////////////////////////////////////////////////////////////////////
158 ////////////////////////////////////////////////////////////////////////
162 * Return the flight model as an integer.
164 * TODO: use a string instead.
167 FGBFI::getFlightModel ()
169 return current_options.get_flight_model();
174 * Return the current aircraft as a string.
177 FGBFI::getAircraft ()
179 return current_options.get_aircraft();
184 * Return the current aircraft directory (UIUC) as a string.
187 FGBFI::getAircraftDir ()
194 * Set the flight model as an integer.
196 * TODO: use a string instead.
199 FGBFI::setFlightModel (int model)
201 current_options.set_flight_model(model);
207 * Set the current aircraft.
210 FGBFI::setAircraft (const string &aircraft)
212 current_options.set_aircraft(aircraft);
218 * Set the current aircraft directory (UIUC).
221 FGBFI::setAircraftDir (const string &dir)
229 * Return the current Zulu time.
234 // FIXME: inefficient
235 return mktime(FGTime::cur_time_params->getGmt());
240 * Set the current Zulu time.
243 FGBFI::setTimeGMT (time_t time)
245 // FIXME: need to update lighting
247 current_options.set_time_offset(time);
248 current_options.set_time_offset_type(fgOPTIONS::FG_TIME_GMT_ABSOLUTE);
249 FGTime::cur_time_params->init( cur_fdm_state->get_Longitude(),
250 cur_fdm_state->get_Latitude() );
251 FGTime::cur_time_params->update( cur_fdm_state->get_Longitude(),
252 cur_fdm_state->get_Latitude(),
253 cur_fdm_state->get_Altitude()
260 * Return true if the HUD is visible.
263 FGBFI::getHUDVisible ()
265 return current_options.get_hud_status();
270 * Ensure that the HUD is visible or hidden.
273 FGBFI::setHUDVisible (bool visible)
275 current_options.set_hud_status(visible);
280 * Return true if the 2D panel is visible.
283 FGBFI::getPanelVisible ()
285 return current_options.get_panel_status();
290 * Ensure that the 2D panel is visible or hidden.
293 FGBFI::setPanelVisible (bool visible)
295 if (current_options.get_panel_status() != visible) {
296 current_options.toggle_panel();
302 ////////////////////////////////////////////////////////////////////////
304 ////////////////////////////////////////////////////////////////////////
308 * Return the current latitude in degrees (negative for south).
311 FGBFI::getLatitude ()
313 return current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
318 * Set the current latitude in degrees (negative for south).
321 FGBFI::setLatitude (double latitude)
323 current_options.set_lat(latitude);
329 * Return the current longitude in degrees (negative for west).
332 FGBFI::getLongitude ()
334 return current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
339 * Set the current longitude in degrees (negative for west).
342 FGBFI::setLongitude (double longitude)
344 current_options.set_lon(longitude);
350 * Return the current altitude in feet.
353 FGBFI::getAltitude ()
355 return current_aircraft.fdm_state->get_Altitude();
361 * Return the current altitude in above the terrain.
366 return current_aircraft.fdm_state->get_Altitude()
367 - scenery.cur_elev * METER_TO_FEET;
372 * Set the current altitude in feet.
375 FGBFI::setAltitude (double altitude)
377 current_options.set_altitude(altitude * FEET_TO_METER);
383 ////////////////////////////////////////////////////////////////////////
385 ////////////////////////////////////////////////////////////////////////
389 * Return the current heading in degrees.
394 return current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG;
399 * Set the current heading in degrees.
402 FGBFI::setHeading (double heading)
404 current_options.set_heading(heading);
410 * Return the current pitch in degrees.
415 return current_aircraft.fdm_state->get_Theta() * RAD_TO_DEG;
420 * Set the current pitch in degrees.
423 FGBFI::setPitch (double pitch)
426 current_options.set_pitch(pitch);
432 * Return the current roll in degrees.
437 return current_aircraft.fdm_state->get_Phi() * RAD_TO_DEG;
442 * Set the current roll in degrees.
445 FGBFI::setRoll (double roll)
447 current_options.set_roll(roll);
453 ////////////////////////////////////////////////////////////////////////
455 ////////////////////////////////////////////////////////////////////////
459 * Return the current airspeed in knots.
462 FGBFI::getAirspeed ()
464 // FIXME: should we add speed-up?
465 return current_aircraft.fdm_state->get_V_calibrated_kts();
470 * Return the current sideslip (FIXME: units unknown).
473 FGBFI::getSideSlip ()
475 return current_aircraft.fdm_state->get_Beta();
480 * Return the current climb rate in feet/minute
483 FGBFI::getVerticalSpeed ()
485 // What about meters?
486 return current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
491 * Get the current north velocity (units??).
494 FGBFI::getSpeedNorth ()
496 return current_aircraft.fdm_state->get_V_north();
501 * Set the current north velocity (units??).
504 FGBFI::setSpeedNorth (double speed)
506 current_options.set_uBody(speed);
512 * Get the current east velocity (units??).
515 FGBFI::getSpeedEast ()
517 return current_aircraft.fdm_state->get_V_east();
522 * Set the current east velocity (units??).
525 FGBFI::setSpeedEast (double speed)
527 current_options.set_vBody(speed);
533 * Get the current down velocity (units??).
536 FGBFI::getSpeedDown ()
538 return current_aircraft.fdm_state->get_V_down();
543 * Set the current down velocity (units??).
546 FGBFI::setSpeedDown (double speed)
548 current_options.set_wBody(speed);
554 ////////////////////////////////////////////////////////////////////////
556 ////////////////////////////////////////////////////////////////////////
560 * Get the throttle setting, from 0.0 (none) to 1.0 (full).
563 FGBFI::getThrottle ()
565 // FIXME: add throttle selector
566 return controls.get_throttle(0);
571 * Set the throttle, from 0.0 (none) to 1.0 (full).
574 FGBFI::setThrottle (double throttle)
576 // FIXME: allow throttle selection
578 controls.set_throttle(0, throttle);
583 * Get the flaps setting, from 0.0 (none) to 1.0 (full).
588 return controls.get_flaps();
593 * Set the flaps, from 0.0 (none) to 1.0 (full).
596 FGBFI::setFlaps (double flaps)
599 controls.set_flaps(flaps);
604 * Get the aileron, from -1.0 (left) to 1.0 (right).
609 return controls.get_aileron();
614 * Set the aileron, from -1.0 (left) to 1.0 (right).
617 FGBFI::setAileron (double aileron)
620 controls.set_aileron(aileron);
625 * Get the rudder setting, from -1.0 (left) to 1.0 (right).
630 return controls.get_rudder();
635 * Set the rudder, from -1.0 (left) to 1.0 (right).
638 FGBFI::setRudder (double rudder)
641 controls.set_rudder(rudder);
646 * Get the elevator setting, from -1.0 (down) to 1.0 (up).
649 FGBFI::getElevator ()
651 return controls.get_elevator();
656 * Set the elevator, from -1.0 (down) to 1.0 (up).
659 FGBFI::setElevator (double elevator)
662 controls.set_elevator(elevator);
667 * Get the elevator trim, from -1.0 (down) to 1.0 (up).
670 FGBFI::getElevatorTrim ()
672 return controls.get_elevator_trim();
677 * Set the elevator trim, from -1.0 (down) to 1.0 (up).
680 FGBFI::setElevatorTrim (double trim)
683 controls.set_elevator_trim(trim);
688 * Get the brake setting, from 0.0 (none) to 1.0 (full).
693 // FIXME: add brake selector
694 return controls.get_brake(0);
699 * Set the brake, from 0.0 (none) to 1.0 (full).
702 FGBFI::setBrake (double brake)
705 // FIXME: allow brake selection
706 controls.set_brake(0, brake);
711 ////////////////////////////////////////////////////////////////////////
713 ////////////////////////////////////////////////////////////////////////
717 * Get the autopilot altitude lock (true=on).
720 FGBFI::getAPAltitudeLock ()
722 return current_autopilot->get_AltitudeEnabled();
727 * Set the autopilot altitude lock (true=on).
730 FGBFI::setAPAltitudeLock (bool lock)
732 current_autopilot->set_AltitudeMode(FGAutopilot::FG_ALTITUDE_LOCK);
733 current_autopilot->set_AltitudeEnabled(lock);
738 * Get the autopilot target altitude in feet.
741 FGBFI::getAPAltitude ()
743 return current_autopilot->get_TargetAltitude() * METER_TO_FEET;
748 * Set the autopilot target altitude in feet.
751 FGBFI::setAPAltitude (double altitude)
753 current_autopilot->set_TargetAltitude( altitude );
758 * Get the autopilot heading lock (true=on).
761 FGBFI::getAPHeadingLock ()
764 (current_autopilot->get_HeadingEnabled() &&
765 current_autopilot->get_HeadingMode() == FGAutopilot::FG_HEADING_LOCK);
770 * Set the autopilot heading lock (true=on).
773 FGBFI::setAPHeadingLock (bool lock)
776 current_autopilot->set_HeadingMode(FGAutopilot::FG_HEADING_LOCK);
777 current_autopilot->set_HeadingEnabled(true);
778 } else if (current_autopilot->get_HeadingMode() ==
779 FGAutopilot::FG_HEADING_LOCK) {
780 current_autopilot->set_HeadingEnabled(false);
786 * Get the autopilot target heading in degrees.
789 FGBFI::getAPHeading ()
791 return current_autopilot->get_TargetHeading();
796 * Set the autopilot target heading in degrees.
799 FGBFI::setAPHeading (double heading)
801 current_autopilot->set_TargetHeading( heading );
806 * Return true if the autopilot is locked to NAV1.
809 FGBFI::getAPNAV1Lock ()
812 (current_autopilot->get_HeadingEnabled() &&
813 current_autopilot->get_HeadingMode() == FGAutopilot::FG_HEADING_NAV1);
818 * Set the autopilot NAV1 lock.
821 FGBFI::setAPNAV1Lock (bool lock)
824 current_autopilot->set_HeadingMode(FGAutopilot::FG_HEADING_NAV1);
825 current_autopilot->set_HeadingEnabled(true);
826 } else if (current_autopilot->get_HeadingMode() ==
827 FGAutopilot::FG_HEADING_NAV1) {
828 current_autopilot->set_HeadingEnabled(false);
834 ////////////////////////////////////////////////////////////////////////
836 ////////////////////////////////////////////////////////////////////////
839 FGBFI::getNAV1Freq ()
841 return current_radiostack->get_nav1_freq();
845 FGBFI::getNAV1AltFreq ()
847 return current_radiostack->get_nav1_alt_freq();
851 FGBFI::getNAV1Radial ()
853 return current_radiostack->get_nav1_radial();
857 FGBFI::getNAV1SelRadial ()
859 return current_radiostack->get_nav1_sel_radial();
863 FGBFI::getNAV1DistDME ()
865 return current_radiostack->get_nav1_dme_dist();
869 FGBFI::getNAV2Freq ()
871 return current_radiostack->get_nav2_freq();
875 FGBFI::getNAV2AltFreq ()
877 return current_radiostack->get_nav2_alt_freq();
881 FGBFI::getNAV2Radial ()
883 return current_radiostack->get_nav2_radial();
887 FGBFI::getNAV2SelRadial ()
889 return current_radiostack->get_nav2_sel_radial();
893 FGBFI::getNAV2DistDME ()
895 return current_radiostack->get_nav2_dme_dist();
901 return current_radiostack->get_adf_freq();
905 FGBFI::getADFAltFreq ()
907 return current_radiostack->get_adf_alt_freq();
911 FGBFI::getADFRotation ()
913 return current_radiostack->get_adf_rotation();
917 FGBFI::setNAV1Freq (double freq)
919 current_radiostack->set_nav1_freq(freq);
923 FGBFI::setNAV1AltFreq (double freq)
925 current_radiostack->set_nav1_alt_freq(freq);
929 FGBFI::setNAV1SelRadial (double radial)
931 current_radiostack->set_nav1_sel_radial(radial);
935 FGBFI::setNAV2Freq (double freq)
937 current_radiostack->set_nav2_freq(freq);
941 FGBFI::setNAV2AltFreq (double freq)
943 current_radiostack->set_nav2_alt_freq(freq);
947 FGBFI::setNAV2SelRadial (double radial)
949 current_radiostack->set_nav2_sel_radial(radial);
953 FGBFI::setADFFreq (double freq)
955 current_radiostack->set_adf_freq(freq);
959 FGBFI::setADFAltFreq (double freq)
961 current_radiostack->set_adf_alt_freq(freq);
965 FGBFI::setADFRotation (double rot)
967 current_radiostack->set_adf_rotation(rot);
972 ////////////////////////////////////////////////////////////////////////
974 ////////////////////////////////////////////////////////////////////////
978 * Get the autopilot GPS lock (true=on).
983 return (current_autopilot->get_HeadingEnabled() &&
984 (current_autopilot->get_HeadingMode() ==
985 FGAutopilot::FG_HEADING_WAYPOINT ));
990 * Set the autopilot GPS lock (true=on).
993 FGBFI::setGPSLock (bool lock)
996 current_autopilot->set_HeadingMode(FGAutopilot::FG_HEADING_WAYPOINT);
997 current_autopilot->set_HeadingEnabled(true);
998 } else if (current_autopilot->get_HeadingMode() ==
999 FGAutopilot::FG_HEADING_WAYPOINT) {
1000 current_autopilot->set_HeadingEnabled(false);
1006 * Get the GPS target airport code.
1009 FGBFI::getTargetAirport ()
1011 return current_options.get_airport_id();
1016 * Set the GPS target airport code.
1019 FGBFI::setTargetAirport (const string &airportId)
1021 current_options.set_airport_id(airportId);
1026 * Get the GPS target latitude in degrees (negative for south).
1029 FGBFI::getGPSTargetLatitude ()
1031 return current_autopilot->get_TargetLatitude();
1036 * Set the GPS target latitude in degrees (negative for south).
1039 FGBFI::setGPSTargetLatitude (double latitude)
1041 current_autopilot->set_TargetLatitude( latitude );
1046 * Get the GPS target longitude in degrees (negative for west).
1049 FGBFI::getGPSTargetLongitude ()
1051 return current_autopilot->get_TargetLongitude();
1056 * Set the GPS target longitude in degrees (negative for west).
1059 FGBFI::setGPSTargetLongitude (double longitude)
1061 current_autopilot->set_TargetLongitude( longitude );
1066 ////////////////////////////////////////////////////////////////////////
1068 ////////////////////////////////////////////////////////////////////////
1072 * Get the current visible (units??).
1075 FGBFI::getVisibility ()
1077 #ifndef FG_OLD_WEATHER
1078 return WeatherDatabase->getWeatherVisibility();
1080 return current_weather.get_visibility();
1086 * Check whether clouds are enabled.
1091 return current_options.get_clouds();
1096 * Check the height of the clouds ASL (units?).
1099 FGBFI::getCloudsASL ()
1101 return current_options.get_clouds_asl();
1106 * Set the current visibility (units??).
1109 FGBFI::setVisibility (double visibility)
1111 #ifndef FG_OLD_WEATHER
1112 WeatherDatabase->setWeatherVisibility(visibility);
1114 current_weather.set_visibility(visibility);
1120 * Switch clouds on or off.
1123 FGBFI::setClouds (bool clouds)
1125 current_options.set_clouds(clouds);
1131 * Set the cloud height.
1134 FGBFI::setCloudsASL (double cloudsASL)
1136 current_options.set_clouds_asl(cloudsASL);
1142 ////////////////////////////////////////////////////////////////////////
1144 ////////////////////////////////////////////////////////////////////////
1147 * Return the magnetic variation
1152 return FGTime::cur_time_params->getMagVar() * RAD_TO_DEG;
1157 * Return the magnetic variation
1162 return FGTime::cur_time_params->getMagDip() * RAD_TO_DEG;