1 // fg_props.cxx -- support for FlightGear properties.
3 // Written by David Megginson, started 2000.
5 // Copyright (C) 2000, 2001 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.
24 # include <simgear/compiler.h>
29 #include <Autopilot/newauto.hxx>
30 #include <Aircraft/aircraft.hxx>
31 #include <Time/tmp.hxx>
32 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
33 #ifndef FG_OLD_WEATHER
34 # include <WeatherCM/FGLocalWeatherDatabase.h>
36 # include <Weather/weather.hxx>
38 #include <Objects/matlib.hxx>
41 #include "fg_props.hxx"
43 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
44 SG_USING_STD(istream);
45 SG_USING_STD(ostream);
48 static double getWindNorth ();
49 static double getWindEast ();
50 static double getWindDown ();
52 // Allow the view to be set from two axes (i.e. a joystick hat)
53 // This needs to be in FGViewer itself, somehow.
54 static double axisLong = 0.0;
55 static double axisLat = 0.0;
61 _set_view_from_axes ()
63 // Take no action when hat is centered
64 if ( ( axisLong < 0.01 ) &&
65 ( axisLong > -0.01 ) &&
73 /* Do all the quick and easy cases */
74 if (axisLong < 0) { // Longitudinal axis forward
75 if (axisLat == axisLong)
77 else if (axisLat == - axisLong)
79 else if (axisLat == 0)
81 } else if (axisLong > 0) { // Longitudinal axis backward
82 if (axisLat == - axisLong)
84 else if (axisLat == axisLong)
86 else if (axisLat == 0)
88 } else if (axisLong == 0) { // Longitudinal axis neutral
93 else return; /* And assertion failure maybe? */
96 /* Do all the difficult cases */
98 viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axisLat, -axisLong );
99 if ( viewDir < -1 ) viewDir += 360;
101 // SG_LOG(SG_INPUT, SG_ALERT, "Joystick Lat=" << axisLat << " and Long="
102 // << axisLong << " gave angle=" << viewDir );
104 globals->get_current_view()->set_goal_view_offset(viewDir*SGD_DEGREES_TO_RADIANS);
105 // globals->get_current_view()->set_view_offset(viewDir*SGD_DEGREES_TO_RADIANS);
109 ////////////////////////////////////////////////////////////////////////
110 // Default property bindings (not yet handled by any module).
111 ////////////////////////////////////////////////////////////////////////
115 * Get the pause state of the sim.
120 return globals->get_freeze();
125 * Set the pause state of the sim.
128 setFreeze (bool freeze)
130 globals->set_freeze(freeze);
134 * Return the current aircraft directory (UIUC) as a string.
144 * Set the current aircraft directory (UIUC).
147 setAircraftDir (string dir)
149 if (getAircraftDir() != dir) {
151 // needReinit(); FIXME!!
157 * Get the current view offset in degrees.
162 return (globals->get_current_view()
163 ->get_view_offset() * SGD_RADIANS_TO_DEGREES);
168 setViewOffset (double offset)
170 globals->get_current_view()->set_view_offset(offset * SGD_DEGREES_TO_RADIANS);
176 return (globals->get_current_view()
177 ->get_goal_view_offset() * SGD_RADIANS_TO_DEGREES);
181 setGoalViewOffset (double offset)
183 globals->get_current_view()
184 ->set_goal_view_offset(offset * SGD_DEGREES_TO_RADIANS);
189 * Return the current Zulu time.
196 struct tm * t = globals->get_time_params()->getGmt();
197 sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
198 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
199 t->tm_hour, t->tm_min, t->tm_sec);
206 * Set the current Zulu time.
209 setDateString (string date_string)
211 SGTime * st = globals->get_time_params();
212 struct tm * current_time = st->getGmt();
215 // Scan for basic ISO format
216 // YYYY-MM-DDTHH:MM:SS
217 int ret = sscanf(date_string.c_str(), "%d-%d-%dT%d:%d:%d",
218 &(new_time.tm_year), &(new_time.tm_mon),
219 &(new_time.tm_mday), &(new_time.tm_hour),
220 &(new_time.tm_min), &(new_time.tm_sec));
222 // Be pretty picky about this, so
223 // that strange things don't happen
224 // if the save file has been edited
227 SG_LOG(SG_INPUT, SG_ALERT, "Date/time string " << date_string
228 << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
232 // OK, it looks like we got six
233 // values, one way or another.
234 new_time.tm_year -= 1900;
235 new_time.tm_mon -= 1;
237 // Now, tell flight gear to use
238 // the new time. This was far
239 // too difficult, by the way.
241 mktime(&new_time) - mktime(current_time) + globals->get_warp();
242 double lon = current_aircraft.fdm_state->get_Longitude();
243 double lat = current_aircraft.fdm_state->get_Latitude();
244 globals->set_warp(warp);
245 st->update(lon, lat, warp);
246 fgUpdateSkyAndLightingParams();
250 * Return the GMT as a string.
257 struct tm * t = globals->get_time_params()->getGmt();
258 sprintf(buf, " %.2d:%.2d:%.2d",
259 t->tm_hour, t->tm_min, t->tm_sec);
266 * Get the texture rendering state.
271 return (material_lib.get_step() == 0);
276 * Set the texture rendering state.
279 setTextures (bool textures)
282 material_lib.set_step(0);
284 material_lib.set_step(1);
289 * Return the magnetic variation
294 return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
299 * Return the magnetic dip
304 return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES;
309 * Return the current heading in degrees.
314 return current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES - getMagVar();
319 * Return the current engine0 rpm
324 if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
325 return current_aircraft.fdm_state->get_engine(0)->get_RPM();
333 * Return the current engine0 EGT.
338 if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
339 return current_aircraft.fdm_state->get_engine(0)->get_EGT();
346 * Return the current engine0 CHT.
351 if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
352 return current_aircraft.fdm_state->get_engine(0)->get_CHT();
359 * Return the current engine0 Manifold Pressure.
364 if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
365 return current_aircraft.fdm_state->get_engine(0)->get_Manifold_Pressure();
373 * Return the current engine0 fuel flow
378 if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
379 return current_aircraft.fdm_state->get_engine(0)->get_Fuel_Flow();
386 * Return the fuel level in tank 1
391 return current_aircraft.fdm_state->get_Tank1Fuel();
395 setTank1Fuel ( double gals )
397 current_aircraft.fdm_state->set_Tank1Fuel( gals );
401 * Return the fuel level in tank 2
406 return current_aircraft.fdm_state->get_Tank2Fuel();
410 setTank2Fuel ( double gals )
412 current_aircraft.fdm_state->set_Tank2Fuel( gals );
417 * Get the autopilot altitude lock (true=on).
422 return (current_autopilot->get_AltitudeEnabled() &&
423 current_autopilot->get_AltitudeMode()
424 == FGAutopilot::FG_ALTITUDE_LOCK);
429 * Set the autopilot altitude lock (true=on).
432 setAPAltitudeLock (bool lock)
434 current_autopilot->set_AltitudeMode(FGAutopilot::FG_ALTITUDE_LOCK);
435 current_autopilot->set_AltitudeEnabled(lock);
440 * Get the autopilot target altitude in feet.
445 return current_autopilot->get_TargetAltitude() * SG_METER_TO_FEET;
450 * Set the autopilot target altitude in feet.
453 setAPAltitude (double altitude)
455 current_autopilot->set_TargetAltitude( altitude * SG_FEET_TO_METER );
459 * Get the autopilot altitude lock (true=on).
464 return (current_autopilot->get_AltitudeEnabled() &&
465 (current_autopilot->get_AltitudeMode()
466 == FGAutopilot::FG_ALTITUDE_GS1));
471 * Set the autopilot altitude lock (true=on).
474 setAPGSLock (bool lock)
476 current_autopilot->set_AltitudeMode(FGAutopilot::FG_ALTITUDE_GS1);
477 current_autopilot->set_AltitudeEnabled(lock);
482 * Get the autopilot terrain lock (true=on).
487 return (current_autopilot->get_AltitudeEnabled() &&
488 (current_autopilot->get_AltitudeMode()
489 == FGAutopilot::FG_ALTITUDE_TERRAIN));
494 * Set the autopilot terrain lock (true=on).
497 setAPTerrainLock (bool lock)
499 current_autopilot->set_AltitudeMode(FGAutopilot::FG_ALTITUDE_TERRAIN);
500 current_autopilot->set_AltitudeEnabled(lock);
505 * Get the autopilot target altitude in feet.
510 return current_autopilot->get_TargetClimbRate() * SG_METER_TO_FEET;
515 * Set the autopilot target altitude in feet.
518 setAPClimb (double rate)
520 current_autopilot->set_TargetClimbRate( rate * SG_FEET_TO_METER );
525 * Get the autopilot heading lock (true=on).
531 (current_autopilot->get_HeadingEnabled() &&
532 current_autopilot->get_HeadingMode() == DEFAULT_AP_HEADING_LOCK);
537 * Set the autopilot heading lock (true=on).
540 setAPHeadingLock (bool lock)
543 current_autopilot->set_HeadingMode(DEFAULT_AP_HEADING_LOCK);
544 current_autopilot->set_HeadingEnabled(true);
546 current_autopilot->set_HeadingEnabled(false);
552 * Get the autopilot heading bug in degrees.
557 return current_autopilot->get_DGTargetHeading();
562 * Set the autopilot heading bug in degrees.
565 setAPHeadingBug (double heading)
567 current_autopilot->set_DGTargetHeading( heading );
572 * Get the autopilot wing leveler lock (true=on).
578 (current_autopilot->get_HeadingEnabled() &&
579 current_autopilot->get_HeadingMode() == FGAutopilot::FG_TC_HEADING_LOCK);
584 * Set the autopilot wing leveler lock (true=on).
587 setAPWingLeveler (bool lock)
590 current_autopilot->set_HeadingMode(FGAutopilot::FG_TC_HEADING_LOCK);
591 current_autopilot->set_HeadingEnabled(true);
593 current_autopilot->set_HeadingEnabled(false);
598 * Return true if the autopilot is locked to NAV1.
604 (current_autopilot->get_HeadingEnabled() &&
605 current_autopilot->get_HeadingMode() == FGAutopilot::FG_HEADING_NAV1);
610 * Set the autopilot NAV1 lock.
613 setAPNAV1Lock (bool lock)
616 current_autopilot->set_HeadingMode(FGAutopilot::FG_HEADING_NAV1);
617 current_autopilot->set_HeadingEnabled(true);
618 } else if (current_autopilot->get_HeadingMode() ==
619 FGAutopilot::FG_HEADING_NAV1) {
620 current_autopilot->set_HeadingEnabled(false);
625 * Get the autopilot autothrottle lock.
628 getAPAutoThrottleLock ()
630 return current_autopilot->get_AutoThrottleEnabled();
635 * Set the autothrottle lock.
638 setAPAutoThrottleLock (bool lock)
640 current_autopilot->set_AutoThrottleEnabled(lock);
646 getAPRudderControl ()
648 if (getAPHeadingLock())
649 return current_autopilot->get_TargetHeading();
651 return controls.get_rudder();
656 setAPRudderControl (double value)
658 if (getAPHeadingLock()) {
659 SG_LOG(SG_GENERAL, SG_DEBUG, "setAPRudderControl " << value );
660 value -= current_autopilot->get_TargetHeading();
661 current_autopilot->HeadingAdjust(value < 0.0 ? -1.0 : 1.0);
663 controls.set_rudder(value);
669 getAPElevatorControl ()
671 if (getAPAltitudeLock())
672 return current_autopilot->get_TargetAltitude();
674 return controls.get_elevator();
679 setAPElevatorControl (double value)
681 if (getAPAltitudeLock()) {
682 SG_LOG(SG_GENERAL, SG_DEBUG, "setAPElevatorControl " << value );
683 value -= current_autopilot->get_TargetAltitude();
684 current_autopilot->AltitudeAdjust(value < 0.0 ? 100.0 : -100.0);
686 controls.set_elevator(value);
692 getAPThrottleControl ()
694 if (getAPAutoThrottleLock())
695 return 0.0; // always resets
697 return controls.get_throttle(0);
702 setAPThrottleControl (double value)
704 if (getAPAutoThrottleLock())
705 current_autopilot->AutoThrottleAdjust(value < 0.0 ? -0.01 : 0.01);
707 controls.set_throttle(0, value);
712 * Get the current visibility (meters).
717 #ifndef FG_OLD_WEATHER
718 return WeatherDatabase->getWeatherVisibility();
720 return current_weather.get_visibility();
726 * Set the current visibility (meters).
729 setVisibility (double visibility)
731 #ifndef FG_OLD_WEATHER
732 WeatherDatabase->setWeatherVisibility(visibility);
734 current_weather.set_visibility(visibility);
739 * Get the current wind north velocity (feet/second).
744 return current_aircraft.fdm_state->get_V_north_airmass();
749 * Set the current wind north velocity (feet/second).
752 setWindNorth (double speed)
754 current_aircraft.fdm_state
755 ->set_Velocities_Local_Airmass(speed, getWindEast(), getWindDown());
760 * Get the current wind east velocity (feet/second).
765 return current_aircraft.fdm_state->get_V_east_airmass();
770 * Set the current wind east velocity (feet/second).
773 setWindEast (double speed)
775 cout << "Set wind-east to " << speed << endl;
776 current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
783 * Get the current wind down velocity (feet/second).
788 return current_aircraft.fdm_state->get_V_down_airmass();
793 * Set the current wind down velocity (feet/second).
796 setWindDown (double speed)
798 current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
806 return globals->get_current_view()->get_fov();
812 globals->get_current_view()->set_fov( fov );
818 return globals->get_warp();
824 globals->set_warp(warp);
830 return globals->get_warp_delta();
834 setWarpDelta (long delta)
836 globals->set_warp_delta(delta);
840 setViewAxisLong (double axis)
846 setViewAxisLat (double axis)
856 fgTie("/sim/freeze", getFreeze, setFreeze);
857 fgTie("/sim/aircraft-dir", getAircraftDir, setAircraftDir);
858 fgTie("/sim/view/offset", getViewOffset, setViewOffset);
859 fgTie("/sim/view/goal-offset", getGoalViewOffset, setGoalViewOffset);
860 fgTie("/sim/time/gmt", getDateString, setDateString);
861 fgTie("/sim/time/gmt-string", getGMTString);
862 fgTie("/sim/rendering/textures", getTextures, setTextures);
865 fgTie("/orientation/heading-magnetic", getHeadingMag);
868 fgTie("/engines/engine0/rpm", getRPM);
869 fgTie("/engines/engine0/egt", getEGT);
870 fgTie("/engines/engine0/cht", getCHT);
871 fgTie("/engines/engine0/mp", getMP);
872 fgTie("/engines/engine0/fuel-flow", getFuelFlow);
875 fgTie("/consumables/fuel/tank1/level", getTank1Fuel, setTank1Fuel, false);
876 fgTie("/consumables/fuel/tank2/level", getTank2Fuel, setTank2Fuel, false);
879 fgTie("/autopilot/locks/altitude", getAPAltitudeLock, setAPAltitudeLock);
880 fgTie("/autopilot/settings/altitude", getAPAltitude, setAPAltitude);
881 fgTie("/autopilot/locks/glide-slope", getAPGSLock, setAPGSLock);
882 fgTie("/autopilot/locks/terrain", getAPTerrainLock, setAPTerrainLock);
883 fgTie("/autopilot/settings/climb-rate", getAPClimb, setAPClimb, false);
884 fgTie("/autopilot/locks/heading", getAPHeadingLock, setAPHeadingLock);
885 fgTie("/autopilot/settings/heading-bug", getAPHeadingBug, setAPHeadingBug,
887 fgTie("/autopilot/locks/wing-leveler", getAPWingLeveler, setAPWingLeveler);
888 fgTie("/autopilot/locks/nav1", getAPNAV1Lock, setAPNAV1Lock);
889 fgTie("/autopilot/locks/auto-throttle",
890 getAPAutoThrottleLock, setAPAutoThrottleLock);
891 fgTie("/autopilot/control-overrides/rudder",
892 getAPRudderControl, setAPRudderControl);
893 fgTie("/autopilot/control-overrides/elevator",
894 getAPElevatorControl, setAPElevatorControl);
895 fgTie("/autopilot/control-overrides/throttle",
896 getAPThrottleControl, setAPThrottleControl);
899 fgTie("/environment/visibility", getVisibility, setVisibility);
900 fgTie("/environment/wind-north", getWindNorth, setWindNorth);
901 fgTie("/environment/wind-east", getWindEast, setWindEast);
902 fgTie("/environment/wind-down", getWindDown, setWindDown);
903 fgTie("/environment/magnetic-variation", getMagVar);
904 fgTie("/environment/magnetic-dip", getMagDip);
907 fgTie("/sim/field-of-view", getFOV, setFOV);
908 fgTie("/sim/time/warp", getWarp, setWarp, false);
909 fgTie("/sim/time/warp-delta", getWarpDelta, setWarpDelta);
910 fgTie("/sim/view/axes/long", (double(*)())0, setViewAxisLong);
911 fgTie("/sim/view/axes/lat", (double(*)())0, setViewAxisLat);
918 _set_view_from_axes();
923 ////////////////////////////////////////////////////////////////////////
925 ////////////////////////////////////////////////////////////////////////
929 * Save the current state of the simulator to a stream.
932 fgSaveFlight (ostream &output)
934 return writeProperties(output, globals->get_props());
939 * Restore the current state of the simulator from a stream.
942 fgLoadFlight (istream &input)
944 SGPropertyNode props;
945 if (readProperties(input, &props)) {
946 copyProperties(&props, globals->get_props());
947 // When loading a flight, make it the
948 // new initial state.
949 globals->saveInitialState();
951 SG_LOG(SG_INPUT, SG_ALERT, "Error restoring flight; aborted");
958 // end of fg_props.cxx