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 <Time/fg_time.hxx>
41 #include <Time/light.hxx>
42 #include <Cockpit/radiostack.hxx>
43 #ifndef FG_OLD_WEATHER
44 # include <WeatherCM/FGLocalWeatherDatabase.h>
46 # include <Weather/weather.hxx>
49 #include "options.hxx"
51 #include "fg_init.hxx"
53 FG_USING_NAMESPACE(std);
55 // FIXME: these are not part of the
56 // published interface!!!
57 extern fgAPDataPtr APDataGlobal;
58 extern void fgAPAltitudeSet (double new_altitude);
59 extern void fgAPHeadingSet (double new_heading);
66 ////////////////////////////////////////////////////////////////////////
68 ////////////////////////////////////////////////////////////////////////
70 bool FGBFI::_needReinit = false;
74 ////////////////////////////////////////////////////////////////////////
76 ////////////////////////////////////////////////////////////////////////
80 * Reinitialize FGFS if required.
82 * Some changes (especially those in aircraft position) require that
83 * FGFS be reinitialized afterwards. Rather than reinitialize after
84 * every change, the setter methods simply set a flag so that there
85 * can be a single reinit at the end of the frame.
97 * Reinitialize FGFS to use the new BFI settings.
102 // Save the state of everything
103 // that's going to get clobbered
104 // when we reinit the subsystems.
106 // TODO: add more AP stuff
107 double elevator = getElevator();
108 double aileron = getAileron();
109 double rudder = getRudder();
110 double throttle = getThrottle();
111 double elevator_trim = getElevatorTrim();
112 double flaps = getFlaps();
113 double brake = getBrake();
114 bool apHeadingLock = getAPHeadingLock();
115 double apHeading = getAPHeading();
116 bool apAltitudeLock = getAPAltitudeLock();
117 double apAltitude = getAPAltitude();
118 const string &targetAirport = getTargetAirport();
119 bool gpsLock = getGPSLock();
120 double gpsLatitude = getGPSTargetLatitude();
121 double gpsLongitude = getGPSTargetLongitude();
123 fgReInitSubsystems();
124 // solarSystemRebuild();
125 cur_light_params.Update();
127 // Restore all of the old states.
128 setElevator(elevator);
131 setThrottle(throttle);
132 setElevatorTrim(elevator_trim);
135 setAPHeadingLock(apHeadingLock);
136 setAPHeading(apHeading);
137 setAPAltitudeLock(apAltitudeLock);
138 setAPAltitude(apAltitude);
139 setTargetAirport(targetAirport);
141 setGPSTargetLatitude(gpsLatitude);
142 setGPSTargetLongitude(gpsLongitude);
149 ////////////////////////////////////////////////////////////////////////
151 ////////////////////////////////////////////////////////////////////////
155 * Return the flight model as an integer.
157 * TODO: use a string instead.
160 FGBFI::getFlightModel ()
162 return current_options.get_flight_model();
167 * Set the flight model as an integer.
169 * TODO: use a string instead.
172 FGBFI::setFlightModel (int model)
174 current_options.set_flight_model(model);
180 * Return the current Zulu time.
185 // FIXME: inefficient
186 return mktime(FGTime::cur_time_params->getGmt());
191 * Set the current Zulu time.
194 FGBFI::setTimeGMT (time_t time)
196 // FIXME: need to update lighting
198 current_options.set_time_offset(time);
199 current_options.set_time_offset_type(fgOPTIONS::FG_TIME_GMT_ABSOLUTE);
200 FGTime::cur_time_params->init( cur_fdm_state->get_Longitude(),
201 cur_fdm_state->get_Latitude() );
202 FGTime::cur_time_params->update( cur_fdm_state->get_Longitude(),
203 cur_fdm_state->get_Latitude(),
204 cur_fdm_state->get_Altitude()
211 * Return true if the HUD is visible.
214 FGBFI::getHUDVisible ()
216 return current_options.get_hud_status();
221 * Ensure that the HUD is visible or hidden.
224 FGBFI::setHUDVisible (bool visible)
226 current_options.set_hud_status(visible);
231 * Return true if the 2D panel is visible.
234 FGBFI::getPanelVisible ()
236 return current_options.get_panel_status();
241 * Ensure that the 2D panel is visible or hidden.
244 FGBFI::setPanelVisible (bool visible)
246 if (current_options.get_panel_status() != visible) {
247 current_options.toggle_panel();
253 ////////////////////////////////////////////////////////////////////////
255 ////////////////////////////////////////////////////////////////////////
259 * Return the current latitude in degrees (negative for south).
262 FGBFI::getLatitude ()
264 return current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
269 * Set the current latitude in degrees (negative for south).
272 FGBFI::setLatitude (double latitude)
274 current_options.set_lat(latitude);
280 * Return the current longitude in degrees (negative for west).
283 FGBFI::getLongitude ()
285 return current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
290 * Set the current longitude in degrees (negative for west).
293 FGBFI::setLongitude (double longitude)
295 current_options.set_lon(longitude);
301 * Return the current altitude in feet.
304 FGBFI::getAltitude ()
306 return current_aircraft.fdm_state->get_Altitude();
311 * Set the current altitude in feet.
314 FGBFI::setAltitude (double altitude)
316 current_options.set_altitude(altitude * FEET_TO_METER);
322 ////////////////////////////////////////////////////////////////////////
324 ////////////////////////////////////////////////////////////////////////
328 * Return the current heading in degrees.
333 return current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG;
338 * Set the current heading in degrees.
341 FGBFI::setHeading (double heading)
343 current_options.set_heading(heading);
349 * Return the current pitch in degrees.
354 return current_aircraft.fdm_state->get_Theta() * RAD_TO_DEG;
359 * Set the current pitch in degrees.
362 FGBFI::setPitch (double pitch)
365 current_options.set_pitch(pitch);
371 * Return the current roll in degrees.
376 return current_aircraft.fdm_state->get_Phi() * RAD_TO_DEG;
381 * Set the current roll in degrees.
384 FGBFI::setRoll (double roll)
386 current_options.set_roll(roll);
392 ////////////////////////////////////////////////////////////////////////
394 ////////////////////////////////////////////////////////////////////////
398 * Return the current airspeed in knots.
401 FGBFI::getAirspeed ()
403 // FIXME: should we add speed-up?
404 return current_aircraft.fdm_state->get_V_calibrated_kts();
409 * Return the current sideslip (FIXME: units unknown).
412 FGBFI::getSideSlip ()
414 return current_aircraft.fdm_state->get_Beta();
419 * Return the current climb rate in feet/second (FIXME: verify).
422 FGBFI::getVerticalSpeed ()
424 // What about meters?
425 return current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
430 * Get the current north velocity (units??).
433 FGBFI::getSpeedNorth ()
435 return current_aircraft.fdm_state->get_V_north();
440 * Set the current north velocity (units??).
443 FGBFI::setSpeedNorth (double speed)
445 current_options.set_uBody(speed);
451 * Get the current east velocity (units??).
454 FGBFI::getSpeedEast ()
456 return current_aircraft.fdm_state->get_V_east();
461 * Set the current east velocity (units??).
464 FGBFI::setSpeedEast (double speed)
466 current_options.set_vBody(speed);
472 * Get the current down velocity (units??).
475 FGBFI::getSpeedDown ()
477 return current_aircraft.fdm_state->get_V_down();
482 * Set the current down velocity (units??).
485 FGBFI::setSpeedDown (double speed)
487 current_options.set_wBody(speed);
493 ////////////////////////////////////////////////////////////////////////
495 ////////////////////////////////////////////////////////////////////////
499 * Get the throttle setting, from 0.0 (none) to 1.0 (full).
502 FGBFI::getThrottle ()
504 // FIXME: add throttle selector
505 return controls.get_throttle(0);
510 * Set the throttle, from 0.0 (none) to 1.0 (full).
513 FGBFI::setThrottle (double throttle)
515 // FIXME: allow throttle selection
517 controls.set_throttle(0, throttle);
522 * Get the flaps setting, from 0.0 (none) to 1.0 (full).
527 return controls.get_flaps();
532 * Set the flaps, from 0.0 (none) to 1.0 (full).
535 FGBFI::setFlaps (double flaps)
538 controls.set_flaps(flaps);
543 * Get the aileron, from -1.0 (left) to 1.0 (right).
548 return controls.get_aileron();
553 * Set the aileron, from -1.0 (left) to 1.0 (right).
556 FGBFI::setAileron (double aileron)
559 controls.set_aileron(aileron);
564 * Get the rudder setting, from -1.0 (left) to 1.0 (right).
569 return controls.get_rudder();
574 * Set the rudder, from -1.0 (left) to 1.0 (right).
577 FGBFI::setRudder (double rudder)
580 controls.set_rudder(rudder);
585 * Get the elevator setting, from -1.0 (down) to 1.0 (up).
588 FGBFI::getElevator ()
590 return controls.get_elevator();
595 * Set the elevator, from -1.0 (down) to 1.0 (up).
598 FGBFI::setElevator (double elevator)
601 controls.set_elevator(elevator);
606 * Get the elevator trim, from -1.0 (down) to 1.0 (up).
609 FGBFI::getElevatorTrim ()
611 return controls.get_elevator_trim();
616 * Set the elevator trim, from -1.0 (down) to 1.0 (up).
619 FGBFI::setElevatorTrim (double trim)
622 controls.set_elevator_trim(trim);
627 * Get the brake setting, from 0.0 (none) to 1.0 (full).
632 // FIXME: add brake selector
633 return controls.get_brake(0);
638 * Set the brake, from 0.0 (none) to 1.0 (full).
641 FGBFI::setBrake (double brake)
644 // FIXME: allow brake selection
645 controls.set_brake(0, brake);
650 ////////////////////////////////////////////////////////////////////////
652 ////////////////////////////////////////////////////////////////////////
656 * Get the autopilot altitude lock (true=on).
659 FGBFI::getAPAltitudeLock ()
661 return fgAPAltitudeEnabled();
666 * Set the autopilot altitude lock (true=on).
669 FGBFI::setAPAltitudeLock (bool lock)
671 APDataGlobal->altitude_hold = lock;
676 * Get the autopilot target altitude in feet.
679 FGBFI::getAPAltitude ()
681 return fgAPget_TargetAltitude() * METER_TO_FEET;
686 * Set the autopilot target altitude in feet.
689 FGBFI::setAPAltitude (double altitude)
691 fgAPAltitudeSet(altitude);
696 * Get the autopilot heading lock (true=on).
699 FGBFI::getAPHeadingLock ()
701 return fgAPHeadingEnabled();
706 * Set the autopilot heading lock (true=on).
709 FGBFI::setAPHeadingLock (bool lock)
711 APDataGlobal->heading_hold = lock;
716 * Get the autopilot target heading in degrees.
719 FGBFI::getAPHeading ()
721 return fgAPget_TargetHeading();
726 * Set the autopilot target heading in degrees.
729 FGBFI::setAPHeading (double heading)
731 fgAPHeadingSet(heading);
736 ////////////////////////////////////////////////////////////////////////
738 ////////////////////////////////////////////////////////////////////////
741 FGBFI::getNAV1Freq ()
743 return current_radiostack->get_nav1_freq();
747 FGBFI::getNAV1AltFreq ()
749 return current_radiostack->get_nav1_alt_freq();
753 FGBFI::getNAV1SelRadial ()
755 return current_radiostack->get_nav1_sel_radial();
759 FGBFI::getNAV1Radial ()
761 return current_radiostack->get_nav1_radial();
765 FGBFI::getNAV2Freq ()
767 return current_radiostack->get_nav2_freq();
771 FGBFI::getNAV2AltFreq ()
773 return current_radiostack->get_nav2_alt_freq();
777 FGBFI::getNAV2SelRadial ()
779 return current_radiostack->get_nav2_sel_radial();
783 FGBFI::getNAV2Radial ()
785 return current_radiostack->get_nav2_radial();
791 return current_radiostack->get_adf_freq();
795 FGBFI::getADFAltFreq ()
797 return current_radiostack->get_adf_alt_freq();
801 FGBFI::getADFRotation ()
803 return current_radiostack->get_adf_rotation();
807 FGBFI::setNAV1Freq (double freq)
809 current_radiostack->set_nav1_freq(freq);
813 FGBFI::setNAV1AltFreq (double freq)
815 current_radiostack->set_nav1_alt_freq(freq);
819 FGBFI::setNAV1SelRadial (double radial)
821 current_radiostack->set_nav1_sel_radial(radial);
825 FGBFI::setNAV2Freq (double freq)
827 current_radiostack->set_nav2_freq(freq);
831 FGBFI::setNAV2AltFreq (double freq)
833 current_radiostack->set_nav2_alt_freq(freq);
837 FGBFI::setNAV2SelRadial (double radial)
839 current_radiostack->set_nav2_sel_radial(radial);
843 FGBFI::setADFFreq (double freq)
845 current_radiostack->set_adf_freq(freq);
849 FGBFI::setADFAltFreq (double freq)
851 current_radiostack->set_adf_alt_freq(freq);
855 FGBFI::setADFRotation (double rot)
857 current_radiostack->set_adf_rotation(rot);
862 ////////////////////////////////////////////////////////////////////////
864 ////////////////////////////////////////////////////////////////////////
868 * Get the autopilot GPS lock (true=on).
873 return fgAPWayPointEnabled();
878 * Set the autopilot GPS lock (true=on).
881 FGBFI::setGPSLock (bool lock)
883 APDataGlobal->waypoint_hold = lock;
888 * Get the GPS target airport code.
891 FGBFI::getTargetAirport ()
893 return current_options.get_airport_id();
898 * Set the GPS target airport code.
901 FGBFI::setTargetAirport (const string &airportId)
903 current_options.set_airport_id(airportId);
908 * Get the GPS target latitude in degrees (negative for south).
911 FGBFI::getGPSTargetLatitude ()
913 return fgAPget_TargetLatitude();
918 * Set the GPS target latitude in degrees (negative for south).
921 FGBFI::setGPSTargetLatitude (double latitude)
923 APDataGlobal->TargetLatitude = latitude;
928 * Get the GPS target longitude in degrees (negative for west).
931 FGBFI::getGPSTargetLongitude ()
933 return fgAPget_TargetLongitude();
938 * Set the GPS target longitude in degrees (negative for west).
941 FGBFI::setGPSTargetLongitude (double longitude)
943 APDataGlobal->TargetLongitude = longitude;
948 ////////////////////////////////////////////////////////////////////////
950 ////////////////////////////////////////////////////////////////////////
954 * Get the current visible (units??).
957 FGBFI::getVisibility ()
959 #ifndef FG_OLD_WEATHER
960 return WeatherDatabase->getWeatherVisibility();
962 return current_weather.get_visibility();
968 * Set the current visibility (units??).
971 FGBFI::setVisibility (double visibility)
973 #ifndef FG_OLD_WEATHER
974 WeatherDatabase->setWeatherVisibility(visibility);
976 current_weather.set_visibility(visibility);
982 ////////////////////////////////////////////////////////////////////////
984 ////////////////////////////////////////////////////////////////////////
987 * Return the magnetic variation
992 return FGTime::cur_time_params->getMagVar() * RAD_TO_DEG;
997 * Return the magnetic variation
1002 return FGTime::cur_time_params->getMagDip() * RAD_TO_DEG;