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 #define DEFAULT_AP_HEADING_LOCK FGAutopilot::FG_DG_HEADING_LOCK
49 static double getWindNorth ();
50 static double getWindEast ();
51 static double getWindDown ();
53 // Allow the view to be set from two axes (i.e. a joystick hat)
54 // This needs to be in FGViewer itself, somehow.
55 static double axisLong = 0.0;
56 static double axisLat = 0.0;
62 _set_view_from_axes ()
64 // Take no action when hat is centered
65 if (axisLong == 0 && axisLat == 0)
70 if (axisLong < 0) { // Longitudinal axis forward
77 } else if (axisLong > 0) { // Longitudinal axis backward
84 } else { // Longitudinal axis neutral
91 globals->get_current_view()->set_goal_view_offset(viewDir*SGD_DEGREES_TO_RADIANS);
92 // globals->get_current_view()->set_view_offset(viewDir*SGD_DEGREES_TO_RADIANS);
96 ////////////////////////////////////////////////////////////////////////
97 // Default property bindings (not yet handled by any module).
98 ////////////////////////////////////////////////////////////////////////
101 * Return the current aircraft directory (UIUC) as a string.
111 * Set the current aircraft directory (UIUC).
114 setAircraftDir (string dir)
116 if (getAircraftDir() != dir) {
118 // needReinit(); FIXME!!
124 * Get the current view offset in degrees.
129 return (globals->get_current_view()
130 ->get_view_offset() * SGD_RADIANS_TO_DEGREES);
135 setViewOffset (double offset)
137 globals->get_current_view()->set_view_offset(offset * SGD_DEGREES_TO_RADIANS);
143 return (globals->get_current_view()
144 ->get_goal_view_offset() * SGD_RADIANS_TO_DEGREES);
148 setGoalViewOffset (double offset)
150 globals->get_current_view()
151 ->set_goal_view_offset(offset * SGD_DEGREES_TO_RADIANS);
156 * Return the current Zulu time.
163 struct tm * t = globals->get_time_params()->getGmt();
164 sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
165 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
166 t->tm_hour, t->tm_min, t->tm_sec);
173 * Set the current Zulu time.
176 setDateString (string date_string)
178 SGTime * st = globals->get_time_params();
179 struct tm * current_time = st->getGmt();
182 // Scan for basic ISO format
183 // YYYY-MM-DDTHH:MM:SS
184 int ret = sscanf(date_string.c_str(), "%d-%d-%dT%d:%d:%d",
185 &(new_time.tm_year), &(new_time.tm_mon),
186 &(new_time.tm_mday), &(new_time.tm_hour),
187 &(new_time.tm_min), &(new_time.tm_sec));
189 // Be pretty picky about this, so
190 // that strange things don't happen
191 // if the save file has been edited
194 SG_LOG(SG_INPUT, SG_ALERT, "Date/time string " << date_string
195 << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
199 // OK, it looks like we got six
200 // values, one way or another.
201 new_time.tm_year -= 1900;
202 new_time.tm_mon -= 1;
204 // Now, tell flight gear to use
205 // the new time. This was far
206 // too difficult, by the way.
208 mktime(&new_time) - mktime(current_time) + globals->get_warp();
209 double lon = current_aircraft.fdm_state->get_Longitude();
210 double lat = current_aircraft.fdm_state->get_Latitude();
211 globals->set_warp(warp);
212 st->update(lon, lat, warp);
213 fgUpdateSkyAndLightingParams();
217 * Return the GMT as a string.
224 struct tm * t = globals->get_time_params()->getGmt();
225 sprintf(buf, " %.2d:%.2d:%.2d",
226 t->tm_hour, t->tm_min, t->tm_sec);
232 * Return the magnetic variation
237 return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
242 * Return the magnetic dip
247 return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES;
252 * Return the current heading in degrees.
257 return current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES - getMagVar();
262 * Return the current engine0 rpm
267 if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
268 return current_aircraft.fdm_state->get_engine(0)->get_RPM();
276 * Return the current engine0 EGT.
281 if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
282 return current_aircraft.fdm_state->get_engine(0)->get_EGT();
289 * Return the current engine0 CHT.
294 if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
295 return current_aircraft.fdm_state->get_engine(0)->get_CHT();
302 * Return the current engine0 Manifold Pressure.
307 if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
308 return current_aircraft.fdm_state->get_engine(0)->get_Manifold_Pressure();
316 * Return the current engine0 fuel flow
321 if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
322 return current_aircraft.fdm_state->get_engine(0)->get_Fuel_Flow();
329 * Return the fuel level in tank 1
334 return current_aircraft.fdm_state->get_Tank1Fuel();
338 setTank1Fuel ( double gals )
340 current_aircraft.fdm_state->set_Tank1Fuel( gals );
344 * Return the fuel level in tank 2
349 return current_aircraft.fdm_state->get_Tank2Fuel();
353 setTank2Fuel ( double gals )
355 current_aircraft.fdm_state->set_Tank2Fuel( gals );
360 * Get the autopilot altitude lock (true=on).
365 return current_autopilot->get_AltitudeEnabled();
370 * Set the autopilot altitude lock (true=on).
373 setAPAltitudeLock (bool lock)
375 current_autopilot->set_AltitudeMode(FGAutopilot::FG_ALTITUDE_LOCK);
376 current_autopilot->set_AltitudeEnabled(lock);
380 * Get the autopilot target altitude in feet.
385 return current_autopilot->get_TargetAltitude() * SG_METER_TO_FEET;
390 * Set the autopilot target altitude in feet.
393 setAPAltitude (double altitude)
395 current_autopilot->set_TargetAltitude( altitude * SG_FEET_TO_METER );
399 * Get the autopilot altitude lock (true=on).
404 return current_autopilot->get_AltitudeEnabled();
409 * Set the autopilot altitude lock (true=on).
412 setAPGSLock (bool lock)
414 current_autopilot->set_AltitudeMode(FGAutopilot::FG_ALTITUDE_GS1);
415 current_autopilot->set_AltitudeEnabled(lock);
420 * Get the autopilot target altitude in feet.
425 return current_autopilot->get_TargetClimbRate() * SG_METER_TO_FEET;
430 * Set the autopilot target altitude in feet.
433 setAPClimb (double rate)
435 current_autopilot->set_TargetClimbRate( rate * SG_FEET_TO_METER );
440 * Get the autopilot heading lock (true=on).
446 (current_autopilot->get_HeadingEnabled() &&
447 current_autopilot->get_HeadingMode() == DEFAULT_AP_HEADING_LOCK);
452 * Set the autopilot heading lock (true=on).
455 setAPHeadingLock (bool lock)
458 current_autopilot->set_HeadingMode(DEFAULT_AP_HEADING_LOCK);
459 current_autopilot->set_HeadingEnabled(true);
461 current_autopilot->set_HeadingEnabled(false);
467 * Get the autopilot heading bug in degrees.
472 return current_autopilot->get_DGTargetHeading();
477 * Set the autopilot heading bug in degrees.
480 setAPHeadingBug (double heading)
482 current_autopilot->set_DGTargetHeading( heading );
487 * Get the autopilot wing leveler lock (true=on).
493 (current_autopilot->get_HeadingEnabled() &&
494 current_autopilot->get_HeadingMode() == FGAutopilot::FG_TC_HEADING_LOCK);
499 * Set the autopilot wing leveler lock (true=on).
502 setAPWingLeveler (bool lock)
505 current_autopilot->set_HeadingMode(FGAutopilot::FG_TC_HEADING_LOCK);
506 current_autopilot->set_HeadingEnabled(true);
508 current_autopilot->set_HeadingEnabled(false);
513 * Return true if the autopilot is locked to NAV1.
519 (current_autopilot->get_HeadingEnabled() &&
520 current_autopilot->get_HeadingMode() == FGAutopilot::FG_HEADING_NAV1);
525 * Set the autopilot NAV1 lock.
528 setAPNAV1Lock (bool lock)
531 current_autopilot->set_HeadingMode(FGAutopilot::FG_HEADING_NAV1);
532 current_autopilot->set_HeadingEnabled(true);
533 } else if (current_autopilot->get_HeadingMode() ==
534 FGAutopilot::FG_HEADING_NAV1) {
535 current_autopilot->set_HeadingEnabled(false);
540 * Get the autopilot autothrottle lock.
543 getAPAutoThrottleLock ()
545 return current_autopilot->get_AutoThrottleEnabled();
550 * Set the autothrottle lock.
553 setAPAutoThrottleLock (bool lock)
555 current_autopilot->set_AutoThrottleEnabled(lock);
561 getAPRudderControl ()
563 if (getAPHeadingLock())
564 return current_autopilot->get_TargetHeading();
566 return controls.get_rudder();
571 setAPRudderControl (double value)
573 if (getAPHeadingLock()) {
574 SG_LOG(SG_GENERAL, SG_DEBUG, "setAPRudderControl " << value );
575 value -= current_autopilot->get_TargetHeading();
576 current_autopilot->HeadingAdjust(value < 0.0 ? -1.0 : 1.0);
578 controls.set_rudder(value);
584 getAPElevatorControl ()
586 if (getAPAltitudeLock())
587 return current_autopilot->get_TargetAltitude();
589 return controls.get_elevator();
594 setAPElevatorControl (double value)
596 if (getAPAltitudeLock()) {
597 SG_LOG(SG_GENERAL, SG_DEBUG, "setAPElevatorControl " << value );
598 value -= current_autopilot->get_TargetAltitude();
599 current_autopilot->AltitudeAdjust(value < 0.0 ? 100.0 : -100.0);
601 controls.set_elevator(value);
607 getAPThrottleControl ()
609 if (getAPAutoThrottleLock())
610 return 0.0; // always resets
612 return controls.get_throttle(0);
617 setAPThrottleControl (double value)
619 if (getAPAutoThrottleLock())
620 current_autopilot->AutoThrottleAdjust(value < 0.0 ? -0.01 : 0.01);
622 controls.set_throttle(0, value);
627 * Get the current visibility (meters).
632 #ifndef FG_OLD_WEATHER
633 return WeatherDatabase->getWeatherVisibility();
635 return current_weather.get_visibility();
641 * Set the current visibility (meters).
644 setVisibility (double visibility)
646 #ifndef FG_OLD_WEATHER
647 WeatherDatabase->setWeatherVisibility(visibility);
649 current_weather.set_visibility(visibility);
654 * Get the current wind north velocity (feet/second).
659 return current_aircraft.fdm_state->get_V_north_airmass();
664 * Set the current wind north velocity (feet/second).
667 setWindNorth (double speed)
669 current_aircraft.fdm_state
670 ->set_Velocities_Local_Airmass(speed, getWindEast(), getWindDown());
675 * Get the current wind east velocity (feet/second).
680 return current_aircraft.fdm_state->get_V_east_airmass();
685 * Set the current wind east velocity (feet/second).
688 setWindEast (double speed)
690 cout << "Set wind-east to " << speed << endl;
691 current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
698 * Get the current wind down velocity (feet/second).
703 return current_aircraft.fdm_state->get_V_down_airmass();
708 * Set the current wind down velocity (feet/second).
711 setWindDown (double speed)
713 current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
721 return globals->get_current_view()->get_fov();
727 globals->get_current_view()->set_fov( fov );
732 setViewAxisLong (double axis)
738 setViewAxisLat (double axis)
748 fgTie("/sim/aircraft-dir", getAircraftDir, setAircraftDir);
749 fgTie("/sim/view/offset", getViewOffset, setViewOffset);
750 fgTie("/sim/view/goal-offset", getGoalViewOffset, setGoalViewOffset);
751 fgTie("/sim/time/gmt", getDateString, setDateString);
752 fgTie("/sim/time/gmt-string", getGMTString);
755 fgTie("/orientation/heading-magnetic", getHeadingMag);
758 fgTie("/engines/engine0/rpm", getRPM);
759 fgTie("/engines/engine0/egt", getEGT);
760 fgTie("/engines/engine0/cht", getCHT);
761 fgTie("/engines/engine0/mp", getMP);
762 fgTie("/engines/engine0/fuel-flow", getFuelFlow);
765 fgTie("/consumables/fuel/tank1/level", getTank1Fuel, setTank1Fuel, false);
766 fgTie("/consumables/fuel/tank2/level", getTank2Fuel, setTank2Fuel, false);
769 fgTie("/autopilot/locks/altitude", getAPAltitudeLock, setAPAltitudeLock);
770 fgTie("/autopilot/settings/altitude", getAPAltitude, setAPAltitude);
771 fgTie("/autopilot/locks/glide-slope", getAPGSLock, setAPGSLock);
772 fgTie("/autopilot/settings/climb-rate", getAPClimb, setAPClimb, false);
773 fgTie("/autopilot/locks/heading", getAPHeadingLock, setAPHeadingLock);
774 fgTie("/autopilot/settings/heading-bug", getAPHeadingBug, setAPHeadingBug,
776 fgTie("/autopilot/locks/wing-leveler", getAPWingLeveler, setAPWingLeveler);
777 fgTie("/autopilot/locks/nav1", getAPNAV1Lock, setAPNAV1Lock);
778 fgTie("/autopilot/locks/auto-throttle",
779 getAPAutoThrottleLock, setAPAutoThrottleLock);
780 fgTie("/autopilot/control-overrides/rudder",
781 getAPRudderControl, setAPRudderControl);
782 fgTie("/autopilot/control-overrides/elevator",
783 getAPElevatorControl, setAPElevatorControl);
784 fgTie("/autopilot/control-overrides/throttle",
785 getAPThrottleControl, setAPThrottleControl);
788 fgTie("/environment/visibility", getVisibility, setVisibility);
789 fgTie("/environment/wind-north", getWindNorth, setWindNorth);
790 fgTie("/environment/wind-east", getWindEast, setWindEast);
791 fgTie("/environment/wind-down", getWindDown, setWindDown);
792 fgTie("/environment/magnetic-variation", getMagVar);
793 fgTie("/environment/magnetic-dip", getMagDip);
796 fgTie("/sim/field-of-view", getFOV, setFOV);
797 fgTie("/sim/view/axes/long", (double(*)())0, setViewAxisLong);
798 fgTie("/sim/view/axes/lat", (double(*)())0, setViewAxisLat);
805 _set_view_from_axes();
810 ////////////////////////////////////////////////////////////////////////
812 ////////////////////////////////////////////////////////////////////////
816 * Save the current state of the simulator to a stream.
819 fgSaveFlight (ostream &output)
821 return writeProperties(output, globals->get_props());
826 * Restore the current state of the simulator from a stream.
829 fgLoadFlight (istream &input)
831 SGPropertyNode props;
832 if (readProperties(input, &props)) {
833 copyProperties(&props, globals->get_props());
834 // When loading a flight, make it the
835 // new initial state.
836 globals->saveInitialState();
838 SG_LOG(SG_INPUT, SG_ALERT, "Error restoring flight; aborted");
845 // end of fg_props.cxx