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 <Time/event.hxx>
46 #include <Time/sunpos.hxx>
47 #include <Cockpit/radiostack.hxx>
48 #include <Ephemeris/ephemeris.hxx>
49 #ifndef FG_OLD_WEATHER
50 # include <WeatherCM/FGLocalWeatherDatabase.h>
52 # include <Weather/weather.hxx>
55 #include "options.hxx"
57 #include "fg_init.hxx"
59 FG_USING_NAMESPACE(std);
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 cout << "BFI: start reinit\n";
108 setHeading(getHeading());
109 setPitch(getPitch());
111 setSpeedNorth(getSpeedNorth());
112 setSpeedEast(getSpeedEast());
113 setSpeedDown(getSpeedDown());
114 setLatitude(getLatitude());
115 setLongitude(getLongitude());
116 setAltitude(getAltitude());
118 // TODO: add more AP stuff
119 double elevator = getElevator();
120 double aileron = getAileron();
121 double rudder = getRudder();
122 double throttle = getThrottle();
123 double elevator_trim = getElevatorTrim();
124 double flaps = getFlaps();
125 double brake = getBrake();
126 bool apHeadingLock = getAPHeadingLock();
127 double apHeadingMag = getAPHeadingMag();
128 bool apAltitudeLock = getAPAltitudeLock();
129 double apAltitude = getAPAltitude();
130 const string &targetAirport = getTargetAirport();
131 bool gpsLock = getGPSLock();
132 double gpsLatitude = getGPSTargetLatitude();
133 double gpsLongitude = getGPSTargetLongitude();
135 setTargetAirport("");
136 cout << "Target airport is " << current_options.get_airport_id() << endl;
137 fgReInitSubsystems();
139 // FIXME: this is wrong.
140 // All of these are scheduled events,
141 // and it should be possible to force
142 // them all to run once.
145 cur_light_params.Update();
146 FGTime::cur_time_params->updateLocal();
147 fgUpdateWeatherDatabase();
150 // Restore all of the old states.
151 setElevator(elevator);
154 setThrottle(throttle);
155 setElevatorTrim(elevator_trim);
158 setAPHeadingLock(apHeadingLock);
159 setAPHeadingMag(apHeadingMag);
160 setAPAltitudeLock(apAltitudeLock);
161 setAPAltitude(apAltitude);
162 setTargetAirport(targetAirport);
164 setGPSTargetLatitude(gpsLatitude);
165 setGPSTargetLongitude(gpsLongitude);
169 cout << "BFI: end reinit\n";
174 ////////////////////////////////////////////////////////////////////////
176 ////////////////////////////////////////////////////////////////////////
180 * Return the flight model as an integer.
182 * TODO: use a string instead.
185 FGBFI::getFlightModel ()
187 return current_options.get_flight_model();
192 * Return the current aircraft as a string.
195 FGBFI::getAircraft ()
197 return current_options.get_aircraft();
202 * Return the current aircraft directory (UIUC) as a string.
205 FGBFI::getAircraftDir ()
212 * Set the flight model as an integer.
214 * TODO: use a string instead.
217 FGBFI::setFlightModel (int model)
219 current_options.set_flight_model(model);
225 * Set the current aircraft.
228 FGBFI::setAircraft (const string &aircraft)
230 current_options.set_aircraft(aircraft);
236 * Set the current aircraft directory (UIUC).
239 FGBFI::setAircraftDir (const string &dir)
247 * Return the current Zulu time.
252 return FGTime::cur_time_params->get_cur_time();
257 * Set the current Zulu time.
260 FGBFI::setTimeGMT (time_t time)
262 // FIXME: need to update lighting
264 current_options.set_time_offset(time);
265 current_options.set_time_offset_type(fgOPTIONS::FG_TIME_GMT_ABSOLUTE);
266 FGTime::cur_time_params->init( cur_fdm_state->get_Longitude(),
267 cur_fdm_state->get_Latitude() );
268 FGTime::cur_time_params->update( cur_fdm_state->get_Longitude(),
269 cur_fdm_state->get_Latitude(),
270 cur_fdm_state->get_Altitude()
277 * Return true if the HUD is visible.
280 FGBFI::getHUDVisible ()
282 return current_options.get_hud_status();
287 * Ensure that the HUD is visible or hidden.
290 FGBFI::setHUDVisible (bool visible)
292 current_options.set_hud_status(visible);
297 * Return true if the 2D panel is visible.
300 FGBFI::getPanelVisible ()
302 return current_options.get_panel_status();
307 * Ensure that the 2D panel is visible or hidden.
310 FGBFI::setPanelVisible (bool visible)
312 if (current_options.get_panel_status() != visible) {
313 current_options.toggle_panel();
319 ////////////////////////////////////////////////////////////////////////
321 ////////////////////////////////////////////////////////////////////////
325 * Return the current latitude in degrees (negative for south).
328 FGBFI::getLatitude ()
330 return current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
335 * Set the current latitude in degrees (negative for south).
338 FGBFI::setLatitude (double latitude)
340 current_options.set_lat(latitude);
341 current_aircraft.fdm_state->set_Latitude(latitude * DEG_TO_RAD);
347 * Return the current longitude in degrees (negative for west).
350 FGBFI::getLongitude ()
352 return current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
357 * Set the current longitude in degrees (negative for west).
360 FGBFI::setLongitude (double longitude)
362 current_options.set_lon(longitude);
363 current_aircraft.fdm_state->set_Longitude(longitude * DEG_TO_RAD);
369 * Return the current altitude in feet.
372 FGBFI::getAltitude ()
374 return current_aircraft.fdm_state->get_Altitude();
380 * Return the current altitude in above the terrain.
385 return current_aircraft.fdm_state->get_Altitude()
386 - scenery.cur_elev * METER_TO_FEET;
391 * Set the current altitude in feet.
394 FGBFI::setAltitude (double altitude)
396 current_options.set_altitude(altitude * FEET_TO_METER);
397 current_aircraft.fdm_state->set_Altitude(altitude);
403 ////////////////////////////////////////////////////////////////////////
405 ////////////////////////////////////////////////////////////////////////
409 * Return the current heading in degrees.
414 return current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG;
419 * Return the current heading in degrees.
422 FGBFI::getHeadingMag ()
424 return current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG - getMagVar();
429 * Set the current heading in degrees.
432 FGBFI::setHeading (double heading)
434 current_options.set_heading(heading);
435 current_aircraft.fdm_state->set_Euler_Angles(getRoll() * DEG_TO_RAD,
436 getPitch() * DEG_TO_RAD,
437 heading * DEG_TO_RAD);
443 * Return the current pitch in degrees.
448 return current_aircraft.fdm_state->get_Theta() * RAD_TO_DEG;
453 * Set the current pitch in degrees.
456 FGBFI::setPitch (double pitch)
459 current_options.set_pitch(pitch);
460 current_aircraft.fdm_state->set_Euler_Angles(getRoll() * DEG_TO_RAD,
462 getHeading() * DEG_TO_RAD);
468 * Return the current roll in degrees.
473 return current_aircraft.fdm_state->get_Phi() * RAD_TO_DEG;
478 * Set the current roll in degrees.
481 FGBFI::setRoll (double roll)
483 current_options.set_roll(roll);
484 current_aircraft.fdm_state->set_Euler_Angles(roll * DEG_TO_RAD,
485 getPitch() * DEG_TO_RAD,
486 getHeading() * DEG_TO_RAD);
492 ////////////////////////////////////////////////////////////////////////
494 ////////////////////////////////////////////////////////////////////////
498 * Return the current airspeed in knots.
501 FGBFI::getAirspeed ()
503 // FIXME: should we add speed-up?
504 return current_aircraft.fdm_state->get_V_calibrated_kts();
509 * Return the current sideslip (FIXME: units unknown).
512 FGBFI::getSideSlip ()
514 return current_aircraft.fdm_state->get_Beta();
519 * Return the current climb rate in feet/minute
522 FGBFI::getVerticalSpeed ()
524 // What about meters?
525 return current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
530 * Get the current north velocity (units??).
533 FGBFI::getSpeedNorth ()
535 return current_aircraft.fdm_state->get_V_north();
540 * Set the current north velocity (units??).
543 FGBFI::setSpeedNorth (double speed)
545 current_options.set_uBody(speed);
546 current_aircraft.fdm_state->set_Velocities_Local(speed,
554 * Get the current east velocity (units??).
557 FGBFI::getSpeedEast ()
559 return current_aircraft.fdm_state->get_V_east();
564 * Set the current east velocity (units??).
567 FGBFI::setSpeedEast (double speed)
569 current_options.set_vBody(speed);
570 current_aircraft.fdm_state->set_Velocities_Local(getSpeedNorth(),
578 * Get the current down velocity (units??).
581 FGBFI::getSpeedDown ()
583 return current_aircraft.fdm_state->get_V_down();
588 * Set the current down velocity (units??).
591 FGBFI::setSpeedDown (double speed)
593 current_options.set_wBody(speed);
594 current_aircraft.fdm_state->set_Velocities_Local(getSpeedNorth(),
602 ////////////////////////////////////////////////////////////////////////
604 ////////////////////////////////////////////////////////////////////////
608 * Get the throttle setting, from 0.0 (none) to 1.0 (full).
611 FGBFI::getThrottle ()
613 // FIXME: add throttle selector
614 return controls.get_throttle(0);
619 * Set the throttle, from 0.0 (none) to 1.0 (full).
622 FGBFI::setThrottle (double throttle)
624 // FIXME: allow throttle selection
626 controls.set_throttle(0, throttle);
631 * Get the flaps setting, from 0.0 (none) to 1.0 (full).
636 return controls.get_flaps();
641 * Set the flaps, from 0.0 (none) to 1.0 (full).
644 FGBFI::setFlaps (double flaps)
647 controls.set_flaps(flaps);
652 * Get the aileron, from -1.0 (left) to 1.0 (right).
657 return controls.get_aileron();
662 * Set the aileron, from -1.0 (left) to 1.0 (right).
665 FGBFI::setAileron (double aileron)
668 controls.set_aileron(aileron);
673 * Get the rudder setting, from -1.0 (left) to 1.0 (right).
678 return controls.get_rudder();
683 * Set the rudder, from -1.0 (left) to 1.0 (right).
686 FGBFI::setRudder (double rudder)
689 controls.set_rudder(rudder);
694 * Get the elevator setting, from -1.0 (down) to 1.0 (up).
697 FGBFI::getElevator ()
699 return controls.get_elevator();
704 * Set the elevator, from -1.0 (down) to 1.0 (up).
707 FGBFI::setElevator (double elevator)
710 controls.set_elevator(elevator);
715 * Get the elevator trim, from -1.0 (down) to 1.0 (up).
718 FGBFI::getElevatorTrim ()
720 return controls.get_elevator_trim();
725 * Set the elevator trim, from -1.0 (down) to 1.0 (up).
728 FGBFI::setElevatorTrim (double trim)
731 controls.set_elevator_trim(trim);
736 * Get the brake setting, from 0.0 (none) to 1.0 (full).
741 // FIXME: add brake selector
742 return controls.get_brake(0);
747 * Set the brake, from 0.0 (none) to 1.0 (full).
750 FGBFI::setBrake (double brake)
753 // FIXME: allow brake selection
754 controls.set_brake(0, brake);
759 ////////////////////////////////////////////////////////////////////////
761 ////////////////////////////////////////////////////////////////////////
765 * Get the autopilot altitude lock (true=on).
768 FGBFI::getAPAltitudeLock ()
770 return current_autopilot->get_AltitudeEnabled();
775 * Set the autopilot altitude lock (true=on).
778 FGBFI::setAPAltitudeLock (bool lock)
780 current_autopilot->set_AltitudeMode(FGAutopilot::FG_ALTITUDE_LOCK);
781 current_autopilot->set_AltitudeEnabled(lock);
786 * Get the autopilot target altitude in feet.
789 FGBFI::getAPAltitude ()
791 return current_autopilot->get_TargetAltitude() * METER_TO_FEET;
796 * Set the autopilot target altitude in feet.
799 FGBFI::setAPAltitude (double altitude)
801 current_autopilot->set_TargetAltitude( altitude );
806 * Get the autopilot heading lock (true=on).
809 FGBFI::getAPHeadingLock ()
812 (current_autopilot->get_HeadingEnabled() &&
813 current_autopilot->get_HeadingMode() == FGAutopilot::FG_HEADING_LOCK);
818 * Set the autopilot heading lock (true=on).
821 FGBFI::setAPHeadingLock (bool lock)
824 // We need to do this so that
825 // it's possible to lock onto a
826 // heading other than the current
828 double heading = getAPHeadingMag();
829 current_autopilot->set_HeadingMode(FGAutopilot::FG_HEADING_LOCK);
830 current_autopilot->set_HeadingEnabled(true);
831 setAPHeadingMag(heading);
832 } else if (current_autopilot->get_HeadingMode() ==
833 FGAutopilot::FG_HEADING_LOCK) {
834 current_autopilot->set_HeadingEnabled(false);
840 * Get the autopilot target heading in degrees.
843 FGBFI::getAPHeadingMag ()
845 return current_autopilot->get_TargetHeading() - getMagVar();
850 * Set the autopilot target heading in degrees.
853 FGBFI::setAPHeadingMag (double heading)
855 current_autopilot->set_TargetHeading( heading + getMagVar() );
860 * Return true if the autopilot is locked to NAV1.
863 FGBFI::getAPNAV1Lock ()
866 (current_autopilot->get_HeadingEnabled() &&
867 current_autopilot->get_HeadingMode() == FGAutopilot::FG_HEADING_NAV1);
872 * Set the autopilot NAV1 lock.
875 FGBFI::setAPNAV1Lock (bool lock)
878 current_autopilot->set_HeadingMode(FGAutopilot::FG_HEADING_NAV1);
879 current_autopilot->set_HeadingEnabled(true);
880 } else if (current_autopilot->get_HeadingMode() ==
881 FGAutopilot::FG_HEADING_NAV1) {
882 current_autopilot->set_HeadingEnabled(false);
888 ////////////////////////////////////////////////////////////////////////
890 ////////////////////////////////////////////////////////////////////////
893 FGBFI::getNAV1Freq ()
895 return current_radiostack->get_nav1_freq();
899 FGBFI::getNAV1AltFreq ()
901 return current_radiostack->get_nav1_alt_freq();
905 FGBFI::getNAV1Radial ()
907 return current_radiostack->get_nav1_radial();
911 FGBFI::getNAV1SelRadial ()
913 return current_radiostack->get_nav1_sel_radial();
917 FGBFI::getNAV1DistDME ()
919 return current_radiostack->get_nav1_dme_dist();
923 FGBFI::getNAV1InRange ()
925 return current_radiostack->get_nav1_inrange();
929 FGBFI::getNAV1DMEInRange ()
931 return (current_radiostack->get_nav1_inrange() &&
932 current_radiostack->get_nav1_has_dme());
936 FGBFI::getNAV2Freq ()
938 return current_radiostack->get_nav2_freq();
942 FGBFI::getNAV2AltFreq ()
944 return current_radiostack->get_nav2_alt_freq();
948 FGBFI::getNAV2Radial ()
950 return current_radiostack->get_nav2_radial();
954 FGBFI::getNAV2SelRadial ()
956 return current_radiostack->get_nav2_sel_radial();
960 FGBFI::getNAV2DistDME ()
962 return current_radiostack->get_nav2_dme_dist();
966 FGBFI::getNAV2InRange ()
968 return current_radiostack->get_nav2_inrange();
972 FGBFI::getNAV2DMEInRange ()
974 return (current_radiostack->get_nav2_inrange() &&
975 current_radiostack->get_nav2_has_dme());
981 return current_radiostack->get_adf_freq();
985 FGBFI::getADFAltFreq ()
987 return current_radiostack->get_adf_alt_freq();
991 FGBFI::getADFRotation ()
993 return current_radiostack->get_adf_rotation();
997 FGBFI::setNAV1Freq (double freq)
999 current_radiostack->set_nav1_freq(freq);
1003 FGBFI::setNAV1AltFreq (double freq)
1005 current_radiostack->set_nav1_alt_freq(freq);
1009 FGBFI::setNAV1SelRadial (double radial)
1011 current_radiostack->set_nav1_sel_radial(radial);
1015 FGBFI::setNAV2Freq (double freq)
1017 current_radiostack->set_nav2_freq(freq);
1021 FGBFI::setNAV2AltFreq (double freq)
1023 current_radiostack->set_nav2_alt_freq(freq);
1027 FGBFI::setNAV2SelRadial (double radial)
1029 current_radiostack->set_nav2_sel_radial(radial);
1033 FGBFI::setADFFreq (double freq)
1035 current_radiostack->set_adf_freq(freq);
1039 FGBFI::setADFAltFreq (double freq)
1041 current_radiostack->set_adf_alt_freq(freq);
1045 FGBFI::setADFRotation (double rot)
1047 current_radiostack->set_adf_rotation(rot);
1052 ////////////////////////////////////////////////////////////////////////
1054 ////////////////////////////////////////////////////////////////////////
1058 * Get the autopilot GPS lock (true=on).
1061 FGBFI::getGPSLock ()
1063 return (current_autopilot->get_HeadingEnabled() &&
1064 (current_autopilot->get_HeadingMode() ==
1065 FGAutopilot::FG_HEADING_WAYPOINT ));
1070 * Set the autopilot GPS lock (true=on).
1073 FGBFI::setGPSLock (bool lock)
1076 current_autopilot->set_HeadingMode(FGAutopilot::FG_HEADING_WAYPOINT);
1077 current_autopilot->set_HeadingEnabled(true);
1078 } else if (current_autopilot->get_HeadingMode() ==
1079 FGAutopilot::FG_HEADING_WAYPOINT) {
1080 current_autopilot->set_HeadingEnabled(false);
1086 * Get the GPS target airport code.
1089 FGBFI::getTargetAirport ()
1091 return current_options.get_airport_id();
1096 * Set the GPS target airport code.
1099 FGBFI::setTargetAirport (const string &airportId)
1101 current_options.set_airport_id(airportId);
1106 * Get the GPS target latitude in degrees (negative for south).
1109 FGBFI::getGPSTargetLatitude ()
1111 return current_autopilot->get_TargetLatitude();
1116 * Set the GPS target latitude in degrees (negative for south).
1119 FGBFI::setGPSTargetLatitude (double latitude)
1121 current_autopilot->set_TargetLatitude( latitude );
1126 * Get the GPS target longitude in degrees (negative for west).
1129 FGBFI::getGPSTargetLongitude ()
1131 return current_autopilot->get_TargetLongitude();
1136 * Set the GPS target longitude in degrees (negative for west).
1139 FGBFI::setGPSTargetLongitude (double longitude)
1141 current_autopilot->set_TargetLongitude( longitude );
1146 ////////////////////////////////////////////////////////////////////////
1148 ////////////////////////////////////////////////////////////////////////
1152 * Get the current visible (units??).
1155 FGBFI::getVisibility ()
1157 #ifndef FG_OLD_WEATHER
1158 return WeatherDatabase->getWeatherVisibility();
1160 return current_weather.get_visibility();
1166 * Check whether clouds are enabled.
1171 return current_options.get_clouds();
1176 * Check the height of the clouds ASL (units?).
1179 FGBFI::getCloudsASL ()
1181 return current_options.get_clouds_asl();
1186 * Set the current visibility (units??).
1189 FGBFI::setVisibility (double visibility)
1191 #ifndef FG_OLD_WEATHER
1192 WeatherDatabase->setWeatherVisibility(visibility);
1194 current_weather.set_visibility(visibility);
1200 * Switch clouds on or off.
1203 FGBFI::setClouds (bool clouds)
1205 cout << "Set clouds to " << clouds << endl;
1206 current_options.set_clouds(clouds);
1212 * Set the cloud height.
1215 FGBFI::setCloudsASL (double cloudsASL)
1217 current_options.set_clouds_asl(cloudsASL);
1223 ////////////////////////////////////////////////////////////////////////
1225 ////////////////////////////////////////////////////////////////////////
1228 * Return the magnetic variation
1233 return FGTime::cur_time_params->getMagVar() * RAD_TO_DEG;
1238 * Return the magnetic variation
1243 return FGTime::cur_time_params->getMagDip() * RAD_TO_DEG;