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>
40 #include "fg_props.hxx"
42 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
43 SG_USING_STD(istream);
44 SG_USING_STD(ostream);
47 static double getWindNorth ();
48 static double getWindEast ();
49 static double getWindDown ();
51 // Allow the view to be set from two axes (i.e. a joystick hat)
52 // This needs to be in FGViewer itself, somehow.
53 static double axisLong = 0.0;
54 static double axisLat = 0.0;
60 _set_view_from_axes ()
62 // Take no action when hat is centered
63 if ( ( axisLong < 0.01 ) &&
64 ( axisLong > -0.01 ) &&
72 /* Do all the quick and easy cases */
73 if (axisLong < 0) { // Longitudinal axis forward
74 if (axisLat == axisLong)
76 else if (axisLat == - axisLong)
78 else if (axisLat == 0)
80 } else if (axisLong > 0) { // Longitudinal axis backward
81 if (axisLat == - axisLong)
83 else if (axisLat == axisLong)
85 else if (axisLat == 0)
87 } else if (axisLong = 0) { // Longitudinal axis neutral
92 else return; /* And assertion failure maybe? */
95 /* Do all the difficult cases */
97 viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axisLat, -axisLong );
98 if ( viewDir < -1 ) viewDir += 360;
100 // SG_LOG(SG_INPUT, SG_ALERT, "Joystick Lat=" << axisLat << " and Long="
101 // << axisLong << " gave angle=" << viewDir );
103 globals->get_current_view()->set_goal_view_offset(viewDir*SGD_DEGREES_TO_RADIANS);
104 // globals->get_current_view()->set_view_offset(viewDir*SGD_DEGREES_TO_RADIANS);
108 ////////////////////////////////////////////////////////////////////////
109 // Default property bindings (not yet handled by any module).
110 ////////////////////////////////////////////////////////////////////////
113 * Return the current aircraft directory (UIUC) as a string.
123 * Set the current aircraft directory (UIUC).
126 setAircraftDir (string dir)
128 if (getAircraftDir() != dir) {
130 // needReinit(); FIXME!!
136 * Get the current view offset in degrees.
141 return (globals->get_current_view()
142 ->get_view_offset() * SGD_RADIANS_TO_DEGREES);
147 setViewOffset (double offset)
149 globals->get_current_view()->set_view_offset(offset * SGD_DEGREES_TO_RADIANS);
155 return (globals->get_current_view()
156 ->get_goal_view_offset() * SGD_RADIANS_TO_DEGREES);
160 setGoalViewOffset (double offset)
162 globals->get_current_view()
163 ->set_goal_view_offset(offset * SGD_DEGREES_TO_RADIANS);
168 * Return the current Zulu time.
175 struct tm * t = globals->get_time_params()->getGmt();
176 sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
177 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
178 t->tm_hour, t->tm_min, t->tm_sec);
185 * Set the current Zulu time.
188 setDateString (string date_string)
190 SGTime * st = globals->get_time_params();
191 struct tm * current_time = st->getGmt();
194 // Scan for basic ISO format
195 // YYYY-MM-DDTHH:MM:SS
196 int ret = sscanf(date_string.c_str(), "%d-%d-%dT%d:%d:%d",
197 &(new_time.tm_year), &(new_time.tm_mon),
198 &(new_time.tm_mday), &(new_time.tm_hour),
199 &(new_time.tm_min), &(new_time.tm_sec));
201 // Be pretty picky about this, so
202 // that strange things don't happen
203 // if the save file has been edited
206 SG_LOG(SG_INPUT, SG_ALERT, "Date/time string " << date_string
207 << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
211 // OK, it looks like we got six
212 // values, one way or another.
213 new_time.tm_year -= 1900;
214 new_time.tm_mon -= 1;
216 // Now, tell flight gear to use
217 // the new time. This was far
218 // too difficult, by the way.
220 mktime(&new_time) - mktime(current_time) + globals->get_warp();
221 double lon = current_aircraft.fdm_state->get_Longitude();
222 double lat = current_aircraft.fdm_state->get_Latitude();
223 globals->set_warp(warp);
224 st->update(lon, lat, warp);
225 fgUpdateSkyAndLightingParams();
229 * Return the GMT as a string.
236 struct tm * t = globals->get_time_params()->getGmt();
237 sprintf(buf, " %.2d:%.2d:%.2d",
238 t->tm_hour, t->tm_min, t->tm_sec);
244 * Return the magnetic variation
249 return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
254 * Return the magnetic dip
259 return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES;
264 * Return the current heading in degrees.
269 return current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES - getMagVar();
274 * Return the current engine0 rpm
279 if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
280 return current_aircraft.fdm_state->get_engine(0)->get_RPM();
288 * Return the current engine0 EGT.
293 if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
294 return current_aircraft.fdm_state->get_engine(0)->get_EGT();
301 * Return the current engine0 CHT.
306 if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
307 return current_aircraft.fdm_state->get_engine(0)->get_CHT();
314 * Return the current engine0 Manifold Pressure.
319 if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
320 return current_aircraft.fdm_state->get_engine(0)->get_Manifold_Pressure();
328 * Return the current engine0 fuel flow
333 if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
334 return current_aircraft.fdm_state->get_engine(0)->get_Fuel_Flow();
341 * Return the fuel level in tank 1
346 return current_aircraft.fdm_state->get_Tank1Fuel();
350 setTank1Fuel ( double gals )
352 current_aircraft.fdm_state->set_Tank1Fuel( gals );
356 * Return the fuel level in tank 2
361 return current_aircraft.fdm_state->get_Tank2Fuel();
365 setTank2Fuel ( double gals )
367 current_aircraft.fdm_state->set_Tank2Fuel( gals );
372 * Get the autopilot altitude lock (true=on).
377 return current_autopilot->get_AltitudeEnabled();
382 * Set the autopilot altitude lock (true=on).
385 setAPAltitudeLock (bool lock)
387 current_autopilot->set_AltitudeMode(FGAutopilot::FG_ALTITUDE_LOCK);
388 current_autopilot->set_AltitudeEnabled(lock);
392 * Get the autopilot target altitude in feet.
397 return current_autopilot->get_TargetAltitude() * SG_METER_TO_FEET;
402 * Set the autopilot target altitude in feet.
405 setAPAltitude (double altitude)
407 current_autopilot->set_TargetAltitude( altitude * SG_FEET_TO_METER );
411 * Get the autopilot altitude lock (true=on).
416 return current_autopilot->get_AltitudeEnabled();
421 * Set the autopilot altitude lock (true=on).
424 setAPGSLock (bool lock)
426 current_autopilot->set_AltitudeMode(FGAutopilot::FG_ALTITUDE_GS1);
427 current_autopilot->set_AltitudeEnabled(lock);
432 * Get the autopilot target altitude in feet.
437 return current_autopilot->get_TargetClimbRate() * SG_METER_TO_FEET;
442 * Set the autopilot target altitude in feet.
445 setAPClimb (double rate)
447 current_autopilot->set_TargetClimbRate( rate * SG_FEET_TO_METER );
452 * Get the autopilot heading lock (true=on).
458 (current_autopilot->get_HeadingEnabled() &&
459 current_autopilot->get_HeadingMode() == DEFAULT_AP_HEADING_LOCK);
464 * Set the autopilot heading lock (true=on).
467 setAPHeadingLock (bool lock)
470 current_autopilot->set_HeadingMode(DEFAULT_AP_HEADING_LOCK);
471 current_autopilot->set_HeadingEnabled(true);
473 current_autopilot->set_HeadingEnabled(false);
479 * Get the autopilot heading bug in degrees.
484 return current_autopilot->get_DGTargetHeading();
489 * Set the autopilot heading bug in degrees.
492 setAPHeadingBug (double heading)
494 current_autopilot->set_DGTargetHeading( heading );
499 * Get the autopilot wing leveler lock (true=on).
505 (current_autopilot->get_HeadingEnabled() &&
506 current_autopilot->get_HeadingMode() == FGAutopilot::FG_TC_HEADING_LOCK);
511 * Set the autopilot wing leveler lock (true=on).
514 setAPWingLeveler (bool lock)
517 current_autopilot->set_HeadingMode(FGAutopilot::FG_TC_HEADING_LOCK);
518 current_autopilot->set_HeadingEnabled(true);
520 current_autopilot->set_HeadingEnabled(false);
525 * Return true if the autopilot is locked to NAV1.
531 (current_autopilot->get_HeadingEnabled() &&
532 current_autopilot->get_HeadingMode() == FGAutopilot::FG_HEADING_NAV1);
537 * Set the autopilot NAV1 lock.
540 setAPNAV1Lock (bool lock)
543 current_autopilot->set_HeadingMode(FGAutopilot::FG_HEADING_NAV1);
544 current_autopilot->set_HeadingEnabled(true);
545 } else if (current_autopilot->get_HeadingMode() ==
546 FGAutopilot::FG_HEADING_NAV1) {
547 current_autopilot->set_HeadingEnabled(false);
552 * Get the autopilot autothrottle lock.
555 getAPAutoThrottleLock ()
557 return current_autopilot->get_AutoThrottleEnabled();
562 * Set the autothrottle lock.
565 setAPAutoThrottleLock (bool lock)
567 current_autopilot->set_AutoThrottleEnabled(lock);
573 getAPRudderControl ()
575 if (getAPHeadingLock())
576 return current_autopilot->get_TargetHeading();
578 return controls.get_rudder();
583 setAPRudderControl (double value)
585 if (getAPHeadingLock()) {
586 SG_LOG(SG_GENERAL, SG_DEBUG, "setAPRudderControl " << value );
587 value -= current_autopilot->get_TargetHeading();
588 current_autopilot->HeadingAdjust(value < 0.0 ? -1.0 : 1.0);
590 controls.set_rudder(value);
596 getAPElevatorControl ()
598 if (getAPAltitudeLock())
599 return current_autopilot->get_TargetAltitude();
601 return controls.get_elevator();
606 setAPElevatorControl (double value)
608 if (getAPAltitudeLock()) {
609 SG_LOG(SG_GENERAL, SG_DEBUG, "setAPElevatorControl " << value );
610 value -= current_autopilot->get_TargetAltitude();
611 current_autopilot->AltitudeAdjust(value < 0.0 ? 100.0 : -100.0);
613 controls.set_elevator(value);
619 getAPThrottleControl ()
621 if (getAPAutoThrottleLock())
622 return 0.0; // always resets
624 return controls.get_throttle(0);
629 setAPThrottleControl (double value)
631 if (getAPAutoThrottleLock())
632 current_autopilot->AutoThrottleAdjust(value < 0.0 ? -0.01 : 0.01);
634 controls.set_throttle(0, value);
639 * Get the current visibility (meters).
644 #ifndef FG_OLD_WEATHER
645 return WeatherDatabase->getWeatherVisibility();
647 return current_weather.get_visibility();
653 * Set the current visibility (meters).
656 setVisibility (double visibility)
658 #ifndef FG_OLD_WEATHER
659 WeatherDatabase->setWeatherVisibility(visibility);
661 current_weather.set_visibility(visibility);
666 * Get the current wind north velocity (feet/second).
671 return current_aircraft.fdm_state->get_V_north_airmass();
676 * Set the current wind north velocity (feet/second).
679 setWindNorth (double speed)
681 current_aircraft.fdm_state
682 ->set_Velocities_Local_Airmass(speed, getWindEast(), getWindDown());
687 * Get the current wind east velocity (feet/second).
692 return current_aircraft.fdm_state->get_V_east_airmass();
697 * Set the current wind east velocity (feet/second).
700 setWindEast (double speed)
702 cout << "Set wind-east to " << speed << endl;
703 current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
710 * Get the current wind down velocity (feet/second).
715 return current_aircraft.fdm_state->get_V_down_airmass();
720 * Set the current wind down velocity (feet/second).
723 setWindDown (double speed)
725 current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
733 return globals->get_current_view()->get_fov();
739 globals->get_current_view()->set_fov( fov );
744 setViewAxisLong (double axis)
750 setViewAxisLat (double axis)
760 fgTie("/sim/aircraft-dir", getAircraftDir, setAircraftDir);
761 fgTie("/sim/view/offset", getViewOffset, setViewOffset);
762 fgTie("/sim/view/goal-offset", getGoalViewOffset, setGoalViewOffset);
763 fgTie("/sim/time/gmt", getDateString, setDateString);
764 fgTie("/sim/time/gmt-string", getGMTString);
767 fgTie("/orientation/heading-magnetic", getHeadingMag);
770 fgTie("/engines/engine0/rpm", getRPM);
771 fgTie("/engines/engine0/egt", getEGT);
772 fgTie("/engines/engine0/cht", getCHT);
773 fgTie("/engines/engine0/mp", getMP);
774 fgTie("/engines/engine0/fuel-flow", getFuelFlow);
777 fgTie("/consumables/fuel/tank1/level", getTank1Fuel, setTank1Fuel, false);
778 fgTie("/consumables/fuel/tank2/level", getTank2Fuel, setTank2Fuel, false);
781 fgTie("/autopilot/locks/altitude", getAPAltitudeLock, setAPAltitudeLock);
782 fgTie("/autopilot/settings/altitude", getAPAltitude, setAPAltitude);
783 fgTie("/autopilot/locks/glide-slope", getAPGSLock, setAPGSLock);
784 fgTie("/autopilot/settings/climb-rate", getAPClimb, setAPClimb, false);
785 fgTie("/autopilot/locks/heading", getAPHeadingLock, setAPHeadingLock);
786 fgTie("/autopilot/settings/heading-bug", getAPHeadingBug, setAPHeadingBug,
788 fgTie("/autopilot/locks/wing-leveler", getAPWingLeveler, setAPWingLeveler);
789 fgTie("/autopilot/locks/nav1", getAPNAV1Lock, setAPNAV1Lock);
790 fgTie("/autopilot/locks/auto-throttle",
791 getAPAutoThrottleLock, setAPAutoThrottleLock);
792 fgTie("/autopilot/control-overrides/rudder",
793 getAPRudderControl, setAPRudderControl);
794 fgTie("/autopilot/control-overrides/elevator",
795 getAPElevatorControl, setAPElevatorControl);
796 fgTie("/autopilot/control-overrides/throttle",
797 getAPThrottleControl, setAPThrottleControl);
800 fgTie("/environment/visibility", getVisibility, setVisibility);
801 fgTie("/environment/wind-north", getWindNorth, setWindNorth);
802 fgTie("/environment/wind-east", getWindEast, setWindEast);
803 fgTie("/environment/wind-down", getWindDown, setWindDown);
804 fgTie("/environment/magnetic-variation", getMagVar);
805 fgTie("/environment/magnetic-dip", getMagDip);
808 fgTie("/sim/field-of-view", getFOV, setFOV);
809 fgTie("/sim/view/axes/long", (double(*)())0, setViewAxisLong);
810 fgTie("/sim/view/axes/lat", (double(*)())0, setViewAxisLat);
817 _set_view_from_axes();
822 ////////////////////////////////////////////////////////////////////////
824 ////////////////////////////////////////////////////////////////////////
828 * Save the current state of the simulator to a stream.
831 fgSaveFlight (ostream &output)
833 return writeProperties(output, globals->get_props());
838 * Restore the current state of the simulator from a stream.
841 fgLoadFlight (istream &input)
843 SGPropertyNode props;
844 if (readProperties(input, &props)) {
845 copyProperties(&props, globals->get_props());
846 // When loading a flight, make it the
847 // new initial state.
848 globals->saveInitialState();
850 SG_LOG(SG_INPUT, SG_ALERT, "Error restoring flight; aborted");
857 // end of fg_props.cxx