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 <Astro/solarsystem.hxx>
42 #ifndef FG_OLD_WEATHER
43 # include <WeatherCM/FGLocalWeatherDatabase.h>
45 # include <Weather/weather.hxx>
48 #include "options.hxx"
50 #include "fg_init.hxx"
52 FG_USING_NAMESPACE(std);
54 // FIXME: these are not part of the
55 // published interface!!!
56 extern fgAPDataPtr APDataGlobal;
57 extern void fgAPAltitudeSet (double new_altitude);
58 extern void fgAPHeadingSet (double new_heading);
65 ////////////////////////////////////////////////////////////////////////
67 ////////////////////////////////////////////////////////////////////////
69 bool FGBFI::_needReinit = false;
73 ////////////////////////////////////////////////////////////////////////
75 ////////////////////////////////////////////////////////////////////////
79 * Reinitialize FGFS if required.
81 * Some changes (especially those in aircraft position) require that
82 * FGFS be reinitialized afterwards. Rather than reinitialize after
83 * every change, the setter methods simply set a flag so that there
84 * can be a single reinit at the end of the frame.
96 * Reinitialize FGFS to use the new BFI settings.
101 // Save the state of everything
102 // that's going to get clobbered
103 // when we reinit the subsystems.
105 // TODO: add more AP stuff
106 double elevator = getElevator();
107 double aileron = getAileron();
108 double rudder = getRudder();
109 double throttle = getThrottle();
110 double elevator_trim = getElevatorTrim();
111 double flaps = getFlaps();
112 double brake = getBrake();
113 bool apHeadingLock = getAPHeadingLock();
114 double apHeading = getAPHeading();
115 bool apAltitudeLock = getAPAltitudeLock();
116 double apAltitude = getAPAltitude();
117 const string &targetAirport = getTargetAirport();
118 bool gpsLock = getGPSLock();
119 double gpsLatitude = getGPSTargetLatitude();
120 double gpsLongitude = getGPSTargetLongitude();
122 fgReInitSubsystems();
123 solarSystemRebuild();
124 cur_light_params.Update();
126 // Restore all of the old states.
127 setElevator(elevator);
130 setThrottle(throttle);
131 setElevatorTrim(elevator_trim);
134 setAPHeadingLock(apHeadingLock);
135 setAPHeading(apHeading);
136 setAPAltitudeLock(apAltitudeLock);
137 setAPAltitude(apAltitude);
138 setTargetAirport(targetAirport);
140 setGPSTargetLatitude(gpsLatitude);
141 setGPSTargetLongitude(gpsLongitude);
148 ////////////////////////////////////////////////////////////////////////
150 ////////////////////////////////////////////////////////////////////////
154 * Return the flight model as an integer.
156 * TODO: use a string instead.
159 FGBFI::getFlightModel ()
161 return current_options.get_flight_model();
166 * Set the flight model as an integer.
168 * TODO: use a string instead.
171 FGBFI::setFlightModel (int model)
173 current_options.set_flight_model(model);
179 * Return the current Zulu time.
184 // FIXME: inefficient
185 return mktime(FGTime::cur_time_params->getGmt());
190 * Set the current Zulu time.
193 FGBFI::setTimeGMT (time_t time)
195 // FIXME: need to update lighting
197 current_options.set_time_offset(time);
198 current_options.set_time_offset_type(fgOPTIONS::FG_TIME_GMT_ABSOLUTE);
199 FGTime::cur_time_params->init(*cur_fdm_state);
200 FGTime::cur_time_params->update(*cur_fdm_state);
206 * Return true if the HUD is visible.
209 FGBFI::getHUDVisible ()
211 return current_options.get_hud_status();
216 * Ensure that the HUD is visible or hidden.
219 FGBFI::setHUDVisible (bool visible)
221 current_options.set_hud_status(visible);
226 * Return true if the 2D panel is visible.
229 FGBFI::getPanelVisible ()
231 return current_options.get_panel_status();
236 * Ensure that the 2D panel is visible or hidden.
239 FGBFI::setPanelVisible (bool visible)
241 if (current_options.get_panel_status() != visible) {
242 current_options.toggle_panel();
248 ////////////////////////////////////////////////////////////////////////
250 ////////////////////////////////////////////////////////////////////////
254 * Return the current latitude in degrees (negative for south).
257 FGBFI::getLatitude ()
259 return current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
264 * Set the current latitude in degrees (negative for south).
267 FGBFI::setLatitude (double latitude)
269 current_options.set_lat(latitude);
275 * Return the current longitude in degrees (negative for west).
278 FGBFI::getLongitude ()
280 return current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
285 * Set the current longitude in degrees (negative for west).
288 FGBFI::setLongitude (double longitude)
290 current_options.set_lon(longitude);
296 * Return the current altitude in feet.
299 FGBFI::getAltitude ()
301 return current_aircraft.fdm_state->get_Altitude();
306 * Set the current altitude in feet.
309 FGBFI::setAltitude (double altitude)
311 current_options.set_altitude(altitude * FEET_TO_METER);
317 ////////////////////////////////////////////////////////////////////////
319 ////////////////////////////////////////////////////////////////////////
323 * Return the current heading in degrees.
328 return current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG;
333 * Set the current heading in degrees.
336 FGBFI::setHeading (double heading)
338 current_options.set_heading(heading);
344 * Return the current pitch in degrees.
349 return current_aircraft.fdm_state->get_Theta() * RAD_TO_DEG;
354 * Set the current pitch in degrees.
357 FGBFI::setPitch (double pitch)
360 current_options.set_pitch(pitch);
366 * Return the current roll in degrees.
371 return current_aircraft.fdm_state->get_Phi() * RAD_TO_DEG;
376 * Set the current roll in degrees.
379 FGBFI::setRoll (double roll)
381 current_options.set_roll(roll);
387 ////////////////////////////////////////////////////////////////////////
389 ////////////////////////////////////////////////////////////////////////
393 * Return the current airspeed in knots.
396 FGBFI::getAirspeed ()
398 // FIXME: should we add speed-up?
399 return current_aircraft.fdm_state->get_V_calibrated_kts();
404 * Return the current sideslip (FIXME: units unknown).
407 FGBFI::getSideSlip ()
409 return current_aircraft.fdm_state->get_Beta();
414 * Return the current climb rate in feet/second (FIXME: verify).
417 FGBFI::getVerticalSpeed ()
419 // What about meters?
420 return current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
425 * Get the current north velocity (units??).
428 FGBFI::getSpeedNorth ()
430 return current_aircraft.fdm_state->get_V_north();
435 * Set the current north velocity (units??).
438 FGBFI::setSpeedNorth (double speed)
440 current_options.set_uBody(speed);
446 * Get the current east velocity (units??).
449 FGBFI::getSpeedEast ()
451 return current_aircraft.fdm_state->get_V_east();
456 * Set the current east velocity (units??).
459 FGBFI::setSpeedEast (double speed)
461 current_options.set_vBody(speed);
467 * Get the current down velocity (units??).
470 FGBFI::getSpeedDown ()
472 return current_aircraft.fdm_state->get_V_down();
477 * Set the current down velocity (units??).
480 FGBFI::setSpeedDown (double speed)
482 current_options.set_wBody(speed);
488 ////////////////////////////////////////////////////////////////////////
490 ////////////////////////////////////////////////////////////////////////
494 * Get the throttle setting, from 0.0 (none) to 1.0 (full).
497 FGBFI::getThrottle ()
499 // FIXME: add throttle selector
500 return controls.get_throttle(0);
505 * Set the throttle, from 0.0 (none) to 1.0 (full).
508 FGBFI::setThrottle (double throttle)
510 // FIXME: allow throttle selection
512 controls.set_throttle(0, throttle);
517 * Get the flaps setting, from 0.0 (none) to 1.0 (full).
522 return controls.get_flaps();
527 * Set the flaps, from 0.0 (none) to 1.0 (full).
530 FGBFI::setFlaps (double flaps)
533 controls.set_flaps(flaps);
538 * Get the aileron, from -1.0 (left) to 1.0 (right).
543 return controls.get_aileron();
548 * Set the aileron, from -1.0 (left) to 1.0 (right).
551 FGBFI::setAileron (double aileron)
554 controls.set_aileron(aileron);
559 * Get the rudder setting, from -1.0 (left) to 1.0 (right).
564 return controls.get_rudder();
569 * Set the rudder, from -1.0 (left) to 1.0 (right).
572 FGBFI::setRudder (double rudder)
575 controls.set_rudder(rudder);
580 * Get the elevator setting, from -1.0 (down) to 1.0 (up).
583 FGBFI::getElevator ()
585 return controls.get_elevator();
590 * Set the elevator, from -1.0 (down) to 1.0 (up).
593 FGBFI::setElevator (double elevator)
596 controls.set_elevator(elevator);
601 * Get the elevator trim, from -1.0 (down) to 1.0 (up).
604 FGBFI::getElevatorTrim ()
606 return controls.get_elevator_trim();
611 * Set the elevator trim, from -1.0 (down) to 1.0 (up).
614 FGBFI::setElevatorTrim (double trim)
617 controls.set_elevator_trim(trim);
622 * Get the brake setting, from 0.0 (none) to 1.0 (full).
627 // FIXME: add brake selector
628 return controls.get_brake(0);
633 * Set the brake, from 0.0 (none) to 1.0 (full).
636 FGBFI::setBrake (double brake)
639 // FIXME: allow brake selection
640 controls.set_brake(0, brake);
645 ////////////////////////////////////////////////////////////////////////
647 ////////////////////////////////////////////////////////////////////////
651 * Get the autopilot altitude lock (true=on).
654 FGBFI::getAPAltitudeLock ()
656 return fgAPAltitudeEnabled();
661 * Set the autopilot altitude lock (true=on).
664 FGBFI::setAPAltitudeLock (bool lock)
666 APDataGlobal->altitude_hold = lock;
671 * Get the autopilot target altitude in feet.
674 FGBFI::getAPAltitude ()
676 return fgAPget_TargetAltitude() * METER_TO_FEET;
681 * Set the autopilot target altitude in feet.
684 FGBFI::setAPAltitude (double altitude)
686 fgAPAltitudeSet(altitude);
691 * Get the autopilot heading lock (true=on).
694 FGBFI::getAPHeadingLock ()
696 return fgAPHeadingEnabled();
701 * Set the autopilot heading lock (true=on).
704 FGBFI::setAPHeadingLock (bool lock)
706 APDataGlobal->heading_hold = lock;
711 * Get the autopilot target heading in degrees.
714 FGBFI::getAPHeading ()
716 return fgAPget_TargetHeading();
721 * Set the autopilot target heading in degrees.
724 FGBFI::setAPHeading (double heading)
726 fgAPHeadingSet(heading);
731 ////////////////////////////////////////////////////////////////////////
733 ////////////////////////////////////////////////////////////////////////
737 * Get the autopilot GPS lock (true=on).
742 return fgAPWayPointEnabled();
747 * Set the autopilot GPS lock (true=on).
750 FGBFI::setGPSLock (bool lock)
752 APDataGlobal->waypoint_hold = lock;
757 * Get the GPS target airport code.
760 FGBFI::getTargetAirport ()
762 return current_options.get_airport_id();
767 * Set the GPS target airport code.
770 FGBFI::setTargetAirport (const string &airportId)
772 current_options.set_airport_id(airportId);
777 * Get the GPS target latitude in degrees (negative for south).
780 FGBFI::getGPSTargetLatitude ()
782 return fgAPget_TargetLatitude();
787 * Set the GPS target latitude in degrees (negative for south).
790 FGBFI::setGPSTargetLatitude (double latitude)
792 APDataGlobal->TargetLatitude = latitude;
797 * Get the GPS target longitude in degrees (negative for west).
800 FGBFI::getGPSTargetLongitude ()
802 return fgAPget_TargetLongitude();
807 * Set the GPS target longitude in degrees (negative for west).
810 FGBFI::setGPSTargetLongitude (double longitude)
812 APDataGlobal->TargetLongitude = longitude;
817 ////////////////////////////////////////////////////////////////////////
819 ////////////////////////////////////////////////////////////////////////
823 * Get the current visible (units??).
826 FGBFI::getVisibility ()
828 #ifndef FG_OLD_WEATHER
829 return WeatherDatabase->getWeatherVisibility();
831 return current_weather.get_visibility();
837 * Set the current visibility (units??).
840 FGBFI::setVisibility (double visibility)
842 #ifndef FG_OLD_WEATHER
843 WeatherDatabase->setWeatherVisibility(visibility);
845 current_weather.set_visibility(visibility);