1 // gps.hxx - distance-measuring equipment.
2 // Written by David Megginson, started 2003.
4 // This file is in the Public Domain and comes with no warranty.
7 #ifndef __INSTRUMENTS_GPS_HXX
8 #define __INSTRUMENTS_GPS_HXX 1
13 #include <simgear/props/props.hxx>
14 #include <simgear/structure/subsystem_mgr.hxx>
15 #include <simgear/props/tiedpropertylist.hxx>
17 #include <Navaids/positioned.hxx>
18 #include <Instrumentation/rnav_waypt_controller.hxx>
33 void init(SGPropertyNode* base, const char* lonStr, const char* latStr, const char* altStr = NULL);
34 void init(const char* lonStr, const char* latStr, const char* altStr = NULL);
36 void operator=(const SGGeod& geod);
40 SGPropertyNode_ptr _lon, _lat, _alt;
48 * /position/longitude-deg
49 * /position/latitude-deg
50 * /position/altitude-ft
51 * /environment/magnetic-variation-deg
52 * /systems/electrical/outputs/gps
53 * /instrumentation/gps/serviceable
58 * /instrumentation/gps/indicated-longitude-deg
59 * /instrumentation/gps/indicated-latitude-deg
60 * /instrumentation/gps/indicated-altitude-ft
61 * /instrumentation/gps/indicated-vertical-speed-fpm
62 * /instrumentation/gps/indicated-track-true-deg
63 * /instrumentation/gps/indicated-track-magnetic-deg
64 * /instrumentation/gps/indicated-ground-speed-kt
66 * /instrumentation/gps/wp-distance-nm
67 * /instrumentation/gps/wp-bearing-deg
68 * /instrumentation/gps/wp-bearing-mag-deg
69 * /instrumentation/gps/TTW
70 * /instrumentation/gps/course-deviation-deg
71 * /instrumentation/gps/course-error-nm
72 * /instrumentation/gps/to-flag
73 * /instrumentation/gps/odometer
74 * /instrumentation/gps/trip-odometer
75 * /instrumentation/gps/true-bug-error-deg
76 * /instrumentation/gps/magnetic-bug-error-deg
78 class GPS : public SGSubsystem, public flightgear::RNAV
81 GPS (SGPropertyNode *node);
85 // SGSubsystem interface
87 virtual void update (double delta_time_sec);
90 virtual void unbind();
93 virtual SGGeod position();
94 virtual double trackDeg();
95 virtual double groundSpeedKts();
96 virtual double vspeedFPM();
97 virtual double magvarDeg();
98 virtual double selectedMagCourse();
99 virtual double overflightArmDistanceM();
102 friend class GPSListener;
103 friend class SearchFilter;
106 * Configuration manager, track data relating to aircraft installation
113 void bind(GPS* aOwner, SGPropertyNode* aCfg);
115 bool turnAnticipationEnabled() const { return _enableTurnAnticipation; }
118 * Desired turn rate in degrees/second. From this we derive the turn
119 * radius and hence how early we need to anticipate it.
121 double turnRateDegSec() const { return _turnRate; }
124 * Distance at which we arm overflight sequencing. Once inside this
125 * distance, a change of the wp1 'TO' flag to false will be considered
126 * overlight of the wp.
128 double overflightArmDistanceNm() const { return _overflightArmDistance; }
131 * Time before the next WP to activate an external annunciator
133 double waypointAlertTime() const { return _waypointAlertTime; }
135 bool requireHardSurface() const { return _requireHardSurface; }
137 bool cdiDeflectionIsAngular() const { return (_cdiMaxDeflectionNm <= 0.0); }
139 double cdiDeflectionLinearPeg() const
141 assert(_cdiMaxDeflectionNm > 0.0);
142 return _cdiMaxDeflectionNm;
145 bool driveAutopilot() const { return _driveAutopilot; }
147 bool courseSelectable() const { return _courseSelectable; }
150 bool _enableTurnAnticipation;
152 // desired turn rate in degrees per second
155 // distance from waypoint to arm overflight sequencing (in nm)
156 double _overflightArmDistance;
158 // time before reaching a waypoint to trigger annunciator light/sound
160 double _waypointAlertTime;
162 // minimum runway length to require when filtering
163 double _minRunwayLengthFt;
165 // should we require a hard-surfaced runway when filtering?
166 bool _requireHardSurface;
168 double _cdiMaxDeflectionNm;
170 // should we drive the autopilot directly or not?
171 bool _driveAutopilot;
173 // is selected-course-deg read to set desired-course or not?
174 bool _courseSelectable;
177 class SearchFilter : public FGPositioned::Filter
180 virtual bool pass(FGPositioned* aPos) const;
182 virtual FGPositioned::Type minType() const;
183 virtual FGPositioned::Type maxType() const;
186 /** reset all output properties to default / non-service values */
189 void updateBasicData(double dt);
191 void updateTrackingBug();
192 void updateReferenceNavaid(double dt);
193 void referenceNavaidSet(const std::string& aNavaid);
194 void updateRouteData();
195 void driveAutopilot();
197 void routeActivated();
198 void routeManagerSequenced();
200 void routeFinished();
203 void updateOverflight();
207 double computeTurnProgress(double aBearing) const;
208 void computeTurnData();
209 void updateTurnData();
210 double computeTurnRadiusNm(double aGroundSpeedKts) const;
212 /** Update one-shot things when WP1 / leg data change */
215 // scratch maintenance utilities
216 void setScratchFromPositioned(FGPositioned* aPos, int aIndex);
217 void setScratchFromCachedSearchResult();
218 void setScratchFromRouteWaypoint(int aIndex);
220 /** Add airport-specific information to a scratch result */
221 void addAirportToScratch(FGAirport* aAirport);
225 /** Predicate, determine if the lon/lat position in the scratch is
227 bool isScratchPositionValid() const;
229 FGPositioned::Filter* createFilter(FGPositioned::Type aTy);
231 /** Search kernel - called each time we step through a result */
232 void performSearch();
235 void selectLegMode();
236 void selectOBSMode();
238 void loadRouteWaypoint();
242 void previousResult();
243 void defineWaypoint();
244 void insertWaypointAtIndex(int aIndex);
245 void removeWaypointAtIndex(int aIndex);
247 // tied-property getter/setters
248 void setCommand(const char* aCmd);
249 const char* getCommand() const { return ""; }
251 const char* getMode() const { return _mode.c_str(); }
253 bool getScratchValid() const { return _scratchValid; }
254 double getScratchDistance() const;
255 double getScratchMagBearing() const;
256 double getScratchTrueBearing() const;
257 bool getScratchHasNext() const;
259 double getSelectedCourse() const { return _selectedCourse; }
260 void setSelectedCourse(double crs);
261 double getDesiredCourse() const { return _desiredCourse; }
263 double getCDIDeflection() const;
265 double getLegDistance() const;
266 double getLegCourse() const;
267 double getLegMagCourse() const;
269 double getTrueTrack() const { return _last_true_track; }
270 double getMagTrack() const;
271 double getGroundspeedKts() const { return _last_speed_kts; }
272 double getVerticalSpeed() const { return _last_vertical_speed; }
274 //bool getLegMode() const { return _mode == "leg"; }
275 //bool getObsMode() const { return _mode == "obs"; }
277 const char* getWP0Ident() const;
278 const char* getWP0Name() const;
280 const char* getWP1Ident() const;
281 const char* getWP1Name() const;
283 double getWP1Distance() const;
284 double getWP1TTW() const;
285 const char* getWP1TTWString() const;
286 double getWP1Bearing() const;
287 double getWP1MagBearing() const;
288 double getWP1CourseDeviation() const;
289 double getWP1CourseErrorNm() const;
290 bool getWP1ToFlag() const;
291 bool getWP1FromFlag() const;
293 // true-bearing-error and mag-bearing-error
297 * Tied-properties helper, record nodes which are tied for easy un-tie-ing
299 template <typename T>
300 void tie(SGPropertyNode* aNode, const char* aRelPath, const SGRawValue<T>& aRawValue)
302 _tiedProperties.Tie(aNode->getNode(aRelPath, true), aRawValue);
305 /** helper, tie the lat/lon/elev of a SGGeod to the named children of aNode */
306 void tieSGGeod(SGPropertyNode* aNode, SGGeod& aRef,
307 const char* lonStr, const char* latStr, const char* altStr);
309 /** helper, tie a SGGeod to proeprties, but read-only */
310 void tieSGGeodReadOnly(SGPropertyNode* aNode, SGGeod& aRef,
311 const char* lonStr, const char* latStr, const char* altStr);
314 SGPropertyNode_ptr _gpsNode;
315 SGPropertyNode_ptr _currentWayptNode;
316 SGPropertyNode_ptr _magvar_node;
317 SGPropertyNode_ptr _serviceable_node;
318 SGPropertyNode_ptr _electrical_node;
319 SGPropertyNode_ptr _tracking_bug_node;
320 SGPropertyNode_ptr _raim_node;
322 SGPropertyNode_ptr _odometer_node;
323 SGPropertyNode_ptr _trip_odometer_node;
324 SGPropertyNode_ptr _true_bug_error_node;
325 SGPropertyNode_ptr _magnetic_bug_error_node;
326 SGPropertyNode_ptr _eastWestVelocity;
327 SGPropertyNode_ptr _northSouthVelocity;
329 SGPropertyNode_ptr _ref_navaid_id_node;
330 SGPropertyNode_ptr _ref_navaid_bearing_node;
331 SGPropertyNode_ptr _ref_navaid_distance_node;
332 SGPropertyNode_ptr _ref_navaid_mag_bearing_node;
333 SGPropertyNode_ptr _ref_navaid_frequency_node;
334 SGPropertyNode_ptr _ref_navaid_name_node;
336 SGPropertyNode_ptr _route_active_node;
337 SGPropertyNode_ptr _route_current_wp_node;
338 SGPropertyNode_ptr _routeDistanceNm;
339 SGPropertyNode_ptr _routeETE;
340 SGPropertyNode_ptr _routeEditedSignal;
341 SGPropertyNode_ptr _routeFinishedSignal;
342 SGPropertyNode_ptr _desiredCourseNode;
344 double _selectedCourse;
345 double _desiredCourse;
350 double _last_speed_kts;
351 double _last_true_track;
352 double _last_vertical_speed;
353 double _lastEWVelocity;
354 double _lastNSVelocity;
357 GPSListener* _listener;
359 FGRouteMgr* _routeMgr;
361 bool _ref_navaid_set;
362 double _ref_navaid_elapsed;
363 FGPositionedRef _ref_navaid;
368 SGGeodProperty _position;
369 SGGeod _wp0_position;
370 SGGeod _indicated_pos;
371 double _legDistanceNm;
375 SGPropertyNode_ptr _scratchNode;
379 int _searchResultIndex;
380 std::string _searchQuery;
381 FGPositioned::Type _searchType;
383 FGPositioned::List _searchResults;
384 bool _searchIsRoute; ///< set if 'search' is actually the current route
385 bool _searchHasNext; ///< is there a result after this one?
386 bool _searchNames; ///< set if we're searching names instead of idents
389 bool _computeTurnData; ///< do we need to update the turn data?
390 bool _anticipateTurn; ///< are we anticipating the next turn or not?
391 bool _inTurn; // is a turn in progress?
392 bool _turnSequenced; // have we sequenced the new leg?
393 double _turnAngle; // angle to turn through, in degrees
394 double _turnStartBearing; // bearing of inbound leg
395 double _turnRadius; // radius of turn in nm
399 std::auto_ptr<flightgear::WayptController> _wayptController;
401 SGPropertyNode_ptr _realismSimpleGps; ///< should the GPS be simple or realistic?
402 flightgear::WayptRef _prevWaypt;
403 flightgear::WayptRef _currentWaypt;
405 // autopilot drive properties
406 SGPropertyNode_ptr _apDrivingFlag;
407 SGPropertyNode_ptr _apTrueHeading;
408 SGPropertyNode_ptr _apTargetAltitudeFt;
409 SGPropertyNode_ptr _apAltitudeLock;
411 simgear::TiedPropertyList _tiedProperties;
415 #endif // __INSTRUMENTS_GPS_HXX