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() );
207 * Return true if the HUD is visible.
210 FGBFI::getHUDVisible ()
212 return current_options.get_hud_status();
217 * Ensure that the HUD is visible or hidden.
220 FGBFI::setHUDVisible (bool visible)
222 current_options.set_hud_status(visible);
227 * Return true if the 2D panel is visible.
230 FGBFI::getPanelVisible ()
232 return current_options.get_panel_status();
237 * Ensure that the 2D panel is visible or hidden.
240 FGBFI::setPanelVisible (bool visible)
242 if (current_options.get_panel_status() != visible) {
243 current_options.toggle_panel();
249 ////////////////////////////////////////////////////////////////////////
251 ////////////////////////////////////////////////////////////////////////
255 * Return the current latitude in degrees (negative for south).
258 FGBFI::getLatitude ()
260 return current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
265 * Set the current latitude in degrees (negative for south).
268 FGBFI::setLatitude (double latitude)
270 current_options.set_lat(latitude);
276 * Return the current longitude in degrees (negative for west).
279 FGBFI::getLongitude ()
281 return current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
286 * Set the current longitude in degrees (negative for west).
289 FGBFI::setLongitude (double longitude)
291 current_options.set_lon(longitude);
297 * Return the current altitude in feet.
300 FGBFI::getAltitude ()
302 return current_aircraft.fdm_state->get_Altitude();
307 * Set the current altitude in feet.
310 FGBFI::setAltitude (double altitude)
312 current_options.set_altitude(altitude * FEET_TO_METER);
318 ////////////////////////////////////////////////////////////////////////
320 ////////////////////////////////////////////////////////////////////////
324 * Return the current heading in degrees.
329 return current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG;
334 * Set the current heading in degrees.
337 FGBFI::setHeading (double heading)
339 current_options.set_heading(heading);
345 * Return the current pitch in degrees.
350 return current_aircraft.fdm_state->get_Theta() * RAD_TO_DEG;
355 * Set the current pitch in degrees.
358 FGBFI::setPitch (double pitch)
361 current_options.set_pitch(pitch);
367 * Return the current roll in degrees.
372 return current_aircraft.fdm_state->get_Phi() * RAD_TO_DEG;
377 * Set the current roll in degrees.
380 FGBFI::setRoll (double roll)
382 current_options.set_roll(roll);
388 ////////////////////////////////////////////////////////////////////////
390 ////////////////////////////////////////////////////////////////////////
394 * Return the current airspeed in knots.
397 FGBFI::getAirspeed ()
399 // FIXME: should we add speed-up?
400 return current_aircraft.fdm_state->get_V_calibrated_kts();
405 * Return the current sideslip (FIXME: units unknown).
408 FGBFI::getSideSlip ()
410 return current_aircraft.fdm_state->get_Beta();
415 * Return the current climb rate in feet/second (FIXME: verify).
418 FGBFI::getVerticalSpeed ()
420 // What about meters?
421 return current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
426 * Get the current north velocity (units??).
429 FGBFI::getSpeedNorth ()
431 return current_aircraft.fdm_state->get_V_north();
436 * Set the current north velocity (units??).
439 FGBFI::setSpeedNorth (double speed)
441 current_options.set_uBody(speed);
447 * Get the current east velocity (units??).
450 FGBFI::getSpeedEast ()
452 return current_aircraft.fdm_state->get_V_east();
457 * Set the current east velocity (units??).
460 FGBFI::setSpeedEast (double speed)
462 current_options.set_vBody(speed);
468 * Get the current down velocity (units??).
471 FGBFI::getSpeedDown ()
473 return current_aircraft.fdm_state->get_V_down();
478 * Set the current down velocity (units??).
481 FGBFI::setSpeedDown (double speed)
483 current_options.set_wBody(speed);
489 ////////////////////////////////////////////////////////////////////////
491 ////////////////////////////////////////////////////////////////////////
495 * Get the throttle setting, from 0.0 (none) to 1.0 (full).
498 FGBFI::getThrottle ()
500 // FIXME: add throttle selector
501 return controls.get_throttle(0);
506 * Set the throttle, from 0.0 (none) to 1.0 (full).
509 FGBFI::setThrottle (double throttle)
511 // FIXME: allow throttle selection
513 controls.set_throttle(0, throttle);
518 * Get the flaps setting, from 0.0 (none) to 1.0 (full).
523 return controls.get_flaps();
528 * Set the flaps, from 0.0 (none) to 1.0 (full).
531 FGBFI::setFlaps (double flaps)
534 controls.set_flaps(flaps);
539 * Get the aileron, from -1.0 (left) to 1.0 (right).
544 return controls.get_aileron();
549 * Set the aileron, from -1.0 (left) to 1.0 (right).
552 FGBFI::setAileron (double aileron)
555 controls.set_aileron(aileron);
560 * Get the rudder setting, from -1.0 (left) to 1.0 (right).
565 return controls.get_rudder();
570 * Set the rudder, from -1.0 (left) to 1.0 (right).
573 FGBFI::setRudder (double rudder)
576 controls.set_rudder(rudder);
581 * Get the elevator setting, from -1.0 (down) to 1.0 (up).
584 FGBFI::getElevator ()
586 return controls.get_elevator();
591 * Set the elevator, from -1.0 (down) to 1.0 (up).
594 FGBFI::setElevator (double elevator)
597 controls.set_elevator(elevator);
602 * Get the elevator trim, from -1.0 (down) to 1.0 (up).
605 FGBFI::getElevatorTrim ()
607 return controls.get_elevator_trim();
612 * Set the elevator trim, from -1.0 (down) to 1.0 (up).
615 FGBFI::setElevatorTrim (double trim)
618 controls.set_elevator_trim(trim);
623 * Get the brake setting, from 0.0 (none) to 1.0 (full).
628 // FIXME: add brake selector
629 return controls.get_brake(0);
634 * Set the brake, from 0.0 (none) to 1.0 (full).
637 FGBFI::setBrake (double brake)
640 // FIXME: allow brake selection
641 controls.set_brake(0, brake);
646 ////////////////////////////////////////////////////////////////////////
648 ////////////////////////////////////////////////////////////////////////
652 * Get the autopilot altitude lock (true=on).
655 FGBFI::getAPAltitudeLock ()
657 return fgAPAltitudeEnabled();
662 * Set the autopilot altitude lock (true=on).
665 FGBFI::setAPAltitudeLock (bool lock)
667 APDataGlobal->altitude_hold = lock;
672 * Get the autopilot target altitude in feet.
675 FGBFI::getAPAltitude ()
677 return fgAPget_TargetAltitude() * METER_TO_FEET;
682 * Set the autopilot target altitude in feet.
685 FGBFI::setAPAltitude (double altitude)
687 fgAPAltitudeSet(altitude);
692 * Get the autopilot heading lock (true=on).
695 FGBFI::getAPHeadingLock ()
697 return fgAPHeadingEnabled();
702 * Set the autopilot heading lock (true=on).
705 FGBFI::setAPHeadingLock (bool lock)
707 APDataGlobal->heading_hold = lock;
712 * Get the autopilot target heading in degrees.
715 FGBFI::getAPHeading ()
717 return fgAPget_TargetHeading();
722 * Set the autopilot target heading in degrees.
725 FGBFI::setAPHeading (double heading)
727 fgAPHeadingSet(heading);
732 ////////////////////////////////////////////////////////////////////////
734 ////////////////////////////////////////////////////////////////////////
738 * Get the autopilot GPS lock (true=on).
743 return fgAPWayPointEnabled();
748 * Set the autopilot GPS lock (true=on).
751 FGBFI::setGPSLock (bool lock)
753 APDataGlobal->waypoint_hold = lock;
758 * Get the GPS target airport code.
761 FGBFI::getTargetAirport ()
763 return current_options.get_airport_id();
768 * Set the GPS target airport code.
771 FGBFI::setTargetAirport (const string &airportId)
773 current_options.set_airport_id(airportId);
778 * Get the GPS target latitude in degrees (negative for south).
781 FGBFI::getGPSTargetLatitude ()
783 return fgAPget_TargetLatitude();
788 * Set the GPS target latitude in degrees (negative for south).
791 FGBFI::setGPSTargetLatitude (double latitude)
793 APDataGlobal->TargetLatitude = latitude;
798 * Get the GPS target longitude in degrees (negative for west).
801 FGBFI::getGPSTargetLongitude ()
803 return fgAPget_TargetLongitude();
808 * Set the GPS target longitude in degrees (negative for west).
811 FGBFI::setGPSTargetLongitude (double longitude)
813 APDataGlobal->TargetLongitude = longitude;
818 ////////////////////////////////////////////////////////////////////////
820 ////////////////////////////////////////////////////////////////////////
824 * Get the current visible (units??).
827 FGBFI::getVisibility ()
829 #ifndef FG_OLD_WEATHER
830 return WeatherDatabase->getWeatherVisibility();
832 return current_weather.get_visibility();
838 * Set the current visibility (units??).
841 FGBFI::setVisibility (double visibility)
843 #ifndef FG_OLD_WEATHER
844 WeatherDatabase->setWeatherVisibility(visibility);
846 current_weather.set_visibility(visibility);