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 #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->get_Longitude(),
200 cur_fdm_state->get_Latitude() );
201 FGTime::cur_time_params->update( cur_fdm_state->get_Longitude(),
202 cur_fdm_state->get_Latitude(),
203 cur_fdm_state->get_Altitude()
210 * Return true if the HUD is visible.
213 FGBFI::getHUDVisible ()
215 return current_options.get_hud_status();
220 * Ensure that the HUD is visible or hidden.
223 FGBFI::setHUDVisible (bool visible)
225 current_options.set_hud_status(visible);
230 * Return true if the 2D panel is visible.
233 FGBFI::getPanelVisible ()
235 return current_options.get_panel_status();
240 * Ensure that the 2D panel is visible or hidden.
243 FGBFI::setPanelVisible (bool visible)
245 if (current_options.get_panel_status() != visible) {
246 current_options.toggle_panel();
252 ////////////////////////////////////////////////////////////////////////
254 ////////////////////////////////////////////////////////////////////////
258 * Return the current latitude in degrees (negative for south).
261 FGBFI::getLatitude ()
263 return current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
268 * Set the current latitude in degrees (negative for south).
271 FGBFI::setLatitude (double latitude)
273 current_options.set_lat(latitude);
279 * Return the current longitude in degrees (negative for west).
282 FGBFI::getLongitude ()
284 return current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
289 * Set the current longitude in degrees (negative for west).
292 FGBFI::setLongitude (double longitude)
294 current_options.set_lon(longitude);
300 * Return the current altitude in feet.
303 FGBFI::getAltitude ()
305 return current_aircraft.fdm_state->get_Altitude();
310 * Set the current altitude in feet.
313 FGBFI::setAltitude (double altitude)
315 current_options.set_altitude(altitude * FEET_TO_METER);
321 ////////////////////////////////////////////////////////////////////////
323 ////////////////////////////////////////////////////////////////////////
327 * Return the current heading in degrees.
332 return current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG;
337 * Set the current heading in degrees.
340 FGBFI::setHeading (double heading)
342 current_options.set_heading(heading);
348 * Return the current pitch in degrees.
353 return current_aircraft.fdm_state->get_Theta() * RAD_TO_DEG;
358 * Set the current pitch in degrees.
361 FGBFI::setPitch (double pitch)
364 current_options.set_pitch(pitch);
370 * Return the current roll in degrees.
375 return current_aircraft.fdm_state->get_Phi() * RAD_TO_DEG;
380 * Set the current roll in degrees.
383 FGBFI::setRoll (double roll)
385 current_options.set_roll(roll);
391 ////////////////////////////////////////////////////////////////////////
393 ////////////////////////////////////////////////////////////////////////
397 * Return the current airspeed in knots.
400 FGBFI::getAirspeed ()
402 // FIXME: should we add speed-up?
403 return current_aircraft.fdm_state->get_V_calibrated_kts();
408 * Return the current sideslip (FIXME: units unknown).
411 FGBFI::getSideSlip ()
413 return current_aircraft.fdm_state->get_Beta();
418 * Return the current climb rate in feet/second (FIXME: verify).
421 FGBFI::getVerticalSpeed ()
423 // What about meters?
424 return current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
429 * Get the current north velocity (units??).
432 FGBFI::getSpeedNorth ()
434 return current_aircraft.fdm_state->get_V_north();
439 * Set the current north velocity (units??).
442 FGBFI::setSpeedNorth (double speed)
444 current_options.set_uBody(speed);
450 * Get the current east velocity (units??).
453 FGBFI::getSpeedEast ()
455 return current_aircraft.fdm_state->get_V_east();
460 * Set the current east velocity (units??).
463 FGBFI::setSpeedEast (double speed)
465 current_options.set_vBody(speed);
471 * Get the current down velocity (units??).
474 FGBFI::getSpeedDown ()
476 return current_aircraft.fdm_state->get_V_down();
481 * Set the current down velocity (units??).
484 FGBFI::setSpeedDown (double speed)
486 current_options.set_wBody(speed);
492 ////////////////////////////////////////////////////////////////////////
494 ////////////////////////////////////////////////////////////////////////
498 * Get the throttle setting, from 0.0 (none) to 1.0 (full).
501 FGBFI::getThrottle ()
503 // FIXME: add throttle selector
504 return controls.get_throttle(0);
509 * Set the throttle, from 0.0 (none) to 1.0 (full).
512 FGBFI::setThrottle (double throttle)
514 // FIXME: allow throttle selection
516 controls.set_throttle(0, throttle);
521 * Get the flaps setting, from 0.0 (none) to 1.0 (full).
526 return controls.get_flaps();
531 * Set the flaps, from 0.0 (none) to 1.0 (full).
534 FGBFI::setFlaps (double flaps)
537 controls.set_flaps(flaps);
542 * Get the aileron, from -1.0 (left) to 1.0 (right).
547 return controls.get_aileron();
552 * Set the aileron, from -1.0 (left) to 1.0 (right).
555 FGBFI::setAileron (double aileron)
558 controls.set_aileron(aileron);
563 * Get the rudder setting, from -1.0 (left) to 1.0 (right).
568 return controls.get_rudder();
573 * Set the rudder, from -1.0 (left) to 1.0 (right).
576 FGBFI::setRudder (double rudder)
579 controls.set_rudder(rudder);
584 * Get the elevator setting, from -1.0 (down) to 1.0 (up).
587 FGBFI::getElevator ()
589 return controls.get_elevator();
594 * Set the elevator, from -1.0 (down) to 1.0 (up).
597 FGBFI::setElevator (double elevator)
600 controls.set_elevator(elevator);
605 * Get the elevator trim, from -1.0 (down) to 1.0 (up).
608 FGBFI::getElevatorTrim ()
610 return controls.get_elevator_trim();
615 * Set the elevator trim, from -1.0 (down) to 1.0 (up).
618 FGBFI::setElevatorTrim (double trim)
621 controls.set_elevator_trim(trim);
626 * Get the brake setting, from 0.0 (none) to 1.0 (full).
631 // FIXME: add brake selector
632 return controls.get_brake(0);
637 * Set the brake, from 0.0 (none) to 1.0 (full).
640 FGBFI::setBrake (double brake)
643 // FIXME: allow brake selection
644 controls.set_brake(0, brake);
649 ////////////////////////////////////////////////////////////////////////
651 ////////////////////////////////////////////////////////////////////////
655 * Get the autopilot altitude lock (true=on).
658 FGBFI::getAPAltitudeLock ()
660 return fgAPAltitudeEnabled();
665 * Set the autopilot altitude lock (true=on).
668 FGBFI::setAPAltitudeLock (bool lock)
670 APDataGlobal->altitude_hold = lock;
675 * Get the autopilot target altitude in feet.
678 FGBFI::getAPAltitude ()
680 return fgAPget_TargetAltitude() * METER_TO_FEET;
685 * Set the autopilot target altitude in feet.
688 FGBFI::setAPAltitude (double altitude)
690 fgAPAltitudeSet(altitude);
695 * Get the autopilot heading lock (true=on).
698 FGBFI::getAPHeadingLock ()
700 return fgAPHeadingEnabled();
705 * Set the autopilot heading lock (true=on).
708 FGBFI::setAPHeadingLock (bool lock)
710 APDataGlobal->heading_hold = lock;
715 * Get the autopilot target heading in degrees.
718 FGBFI::getAPHeading ()
720 return fgAPget_TargetHeading();
725 * Set the autopilot target heading in degrees.
728 FGBFI::setAPHeading (double heading)
730 fgAPHeadingSet(heading);
735 ////////////////////////////////////////////////////////////////////////
737 ////////////////////////////////////////////////////////////////////////
741 * Get the autopilot GPS lock (true=on).
746 return fgAPWayPointEnabled();
751 * Set the autopilot GPS lock (true=on).
754 FGBFI::setGPSLock (bool lock)
756 APDataGlobal->waypoint_hold = lock;
761 * Get the GPS target airport code.
764 FGBFI::getTargetAirport ()
766 return current_options.get_airport_id();
771 * Set the GPS target airport code.
774 FGBFI::setTargetAirport (const string &airportId)
776 current_options.set_airport_id(airportId);
781 * Get the GPS target latitude in degrees (negative for south).
784 FGBFI::getGPSTargetLatitude ()
786 return fgAPget_TargetLatitude();
791 * Set the GPS target latitude in degrees (negative for south).
794 FGBFI::setGPSTargetLatitude (double latitude)
796 APDataGlobal->TargetLatitude = latitude;
801 * Get the GPS target longitude in degrees (negative for west).
804 FGBFI::getGPSTargetLongitude ()
806 return fgAPget_TargetLongitude();
811 * Set the GPS target longitude in degrees (negative for west).
814 FGBFI::setGPSTargetLongitude (double longitude)
816 APDataGlobal->TargetLongitude = longitude;
821 ////////////////////////////////////////////////////////////////////////
823 ////////////////////////////////////////////////////////////////////////
827 * Get the current visible (units??).
830 FGBFI::getVisibility ()
832 #ifndef FG_OLD_WEATHER
833 return WeatherDatabase->getWeatherVisibility();
835 return current_weather.get_visibility();
841 * Set the current visibility (units??).
844 FGBFI::setVisibility (double visibility)
846 #ifndef FG_OLD_WEATHER
847 WeatherDatabase->setWeatherVisibility(visibility);
849 current_weather.set_visibility(visibility);
855 ////////////////////////////////////////////////////////////////////////
857 ////////////////////////////////////////////////////////////////////////
860 * Return the magnetic variation
865 return FGTime::cur_time_params->getMagVar() * RAD_TO_DEG;
870 * Return the magnetic variation
875 return FGTime::cur_time_params->getMagDip() * RAD_TO_DEG;