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
10 #include <simgear/props/props.hxx>
11 #include <simgear/structure/subsystem_mgr.hxx>
12 #include <simgear/math/SGMath.hxx>
14 #include "Navaids/positioned.hxx"
29 void init(SGPropertyNode* base, const char* lonStr, const char* latStr, const char* altStr = NULL);
30 void init(const char* lonStr, const char* latStr, const char* altStr = NULL);
32 void operator=(const SGGeod& geod);
35 SGPropertyNode_ptr _lon, _lat, _alt;
43 * /position/longitude-deg
44 * /position/latitude-deg
45 * /position/altitude-ft
46 * /environment/magnetic-variation-deg
47 * /systems/electrical/outputs/gps
48 * /instrumentation/gps/serviceable
53 * /instrumentation/gps/indicated-longitude-deg
54 * /instrumentation/gps/indicated-latitude-deg
55 * /instrumentation/gps/indicated-altitude-ft
56 * /instrumentation/gps/indicated-vertical-speed-fpm
57 * /instrumentation/gps/indicated-track-true-deg
58 * /instrumentation/gps/indicated-track-magnetic-deg
59 * /instrumentation/gps/indicated-ground-speed-kt
61 * /instrumentation/gps/wp-distance-nm
62 * /instrumentation/gps/wp-bearing-deg
63 * /instrumentation/gps/wp-bearing-mag-deg
64 * /instrumentation/gps/TTW
65 * /instrumentation/gps/course-deviation-deg
66 * /instrumentation/gps/course-error-nm
67 * /instrumentation/gps/to-flag
68 * /instrumentation/gps/odometer
69 * /instrumentation/gps/trip-odometer
70 * /instrumentation/gps/true-bug-error-deg
71 * /instrumentation/gps/magnetic-bug-error-deg
74 class GPS : public SGSubsystem
79 GPS (SGPropertyNode *node);
84 virtual void update (double delta_time_sec);
87 virtual void unbind();
89 friend class GPSListener;
90 friend class SearchFilter;
93 * Configuration manager, track data relating to aircraft installation
100 void bind(GPS* aOwner, SGPropertyNode* aCfg);
102 bool turnAnticipationEnabled() const
103 { return _enableTurnAnticipation; }
106 * Desired turn rate in degrees/second. From this we derive the turn
107 * radius and hence how early we need to anticipate it.
109 double turnRateDegSec() const
110 { return _turnRate; }
113 * Distance at which we arm overflight sequencing. Once inside this
114 * distance, a change of the wp1 'TO' flag to false will be considered
115 * overlight of the wp.
117 double overflightArmDistanceNm() const
118 { return _overflightArmDistance; }
121 * Time before the next WP to activate an external annunciator
123 double waypointAlertTime() const
124 { return _waypointAlertTime; }
126 bool tuneNavRadioToRefVor() const
127 { return _tuneRadio1ToRefVor; }
129 bool requireHardSurface() const
130 { return _requireHardSurface; }
132 double minRunwayLengthFt() const
133 { return _minRunwayLengthFt; }
135 double getExternalCourse() const;
137 void setExternalCourse(double aCourseDeg);
139 bool cdiDeflectionIsAngular() const
140 { return (_cdiMaxDeflectionNm <= 0.0); }
142 double cdiDeflectionLinearPeg() const
144 assert(_cdiMaxDeflectionNm > 0.0);
145 return _cdiMaxDeflectionNm;
148 bool driveAutopilot() const
149 { return _driveAutopilot; }
151 bool _enableTurnAnticipation;
153 // desired turn rate in degrees per second
156 // distance from waypoint to arm overflight sequencing (in nm)
157 double _overflightArmDistance;
159 // time before reaching a waypoint to trigger annunicator light/sound
161 double _waypointAlertTime;
163 // should GPS automatically tune NAV1 to the reference VOR?
164 bool _tuneRadio1ToRefVor;
166 // minimum runway length to require when filtering
167 double _minRunwayLengthFt;
169 // should we require a hard-surfaced runway when filtering?
170 bool _requireHardSurface;
172 // helpers to tie course-source property
173 const char* getCourseSource() const;
174 void setCourseSource(const char* aPropPath);
176 // property to retrieve the external course from
177 SGPropertyNode_ptr _extCourseSource;
179 double _cdiMaxDeflectionNm;
181 bool _driveAutopilot;
184 class SearchFilter : public FGPositioned::Filter
187 virtual bool pass(FGPositioned* aPos) const;
189 virtual FGPositioned::Type minType() const;
190 virtual FGPositioned::Type maxType() const;
194 * reset all output properties to default / non-service values
198 void updateBasicData(double dt);
199 void updateWaypoints();
201 void updateTrackingBug();
202 void updateReferenceNavaid(double dt);
203 void referenceNavaidSet(const std::string& aNavaid);
204 void tuneNavRadios();
205 void updateRouteData();
206 void driveAutopilot();
208 void routeActivated();
209 void routeManagerSequenced();
211 void routeFinished();
214 void updateOverflight();
218 double computeTurnProgress(double aBearing) const;
219 void computeTurnData();
220 void updateTurnData();
221 double computeTurnRadiusNm(double aGroundSpeedKts) const;
224 * Update one-shot things when WP1 / leg data change
228 // scratch maintenence utilities
229 void setScratchFromPositioned(FGPositioned* aPos, int aIndex);
230 void setScratchFromCachedSearchResult();
231 void setScratchFromRouteWaypoint(int aIndex);
234 * Add airport-specific information to a scratch result
236 void addAirportToScratch(FGAirport* aAirport);
241 * Predicate, determine if the lon/lat position in the scratch is
244 bool isScratchPositionValid() const;
246 FGPositioned::Filter* createFilter(FGPositioned::Type aTy);
249 * Search kernel - called each time we step through a result
251 void performSearch();
254 void selectLegMode();
255 void selectOBSMode();
257 void loadRouteWaypoint();
261 void previousResult();
262 void defineWaypoint();
263 void insertWaypointAtIndex(int aIndex);
264 void removeWaypointAtIndex(int aIndex);
266 // tied-property getter/setters
267 void setCommand(const char* aCmd);
268 const char* getCommand() const { return ""; }
270 const char* getMode() const { return _mode.c_str(); }
272 bool getScratchValid() const { return _scratchValid; }
273 double getScratchDistance() const;
274 double getScratchMagBearing() const;
275 double getScratchTrueBearing() const;
276 bool getScratchHasNext() const { return _searchHasNext; }
278 double getSelectedCourse() const { return _selectedCourse; }
279 double getCDIDeflection() const;
281 double getLegDistance() const;
282 double getLegCourse() const;
283 double getLegMagCourse() const;
284 double getAltDistanceRatio() const;
286 double getTrueTrack() const { return _last_true_track; }
287 double getMagTrack() const;
288 double getGroundspeedKts() const { return _last_speed_kts; }
289 double getVerticalSpeed() const { return _last_vertical_speed; }
291 //bool getLegMode() const { return _mode == "leg"; }
292 //bool getObsMode() const { return _mode == "obs"; }
294 const char* getWP0Ident() const;
295 const char* getWP0Name() const;
297 const char* getWP1Ident() const;
298 const char* getWP1Name() const;
300 double getWP1Distance() const;
301 double getWP1TTW() const;
302 const char* getWP1TTWString() const;
303 double getWP1Bearing() const;
304 double getWP1MagBearing() const;
305 double getWP1CourseDeviation() const;
306 double getWP1CourseErrorNm() const;
307 bool getWP1ToFlag() const;
308 bool getWP1FromFlag() const;
310 // true-bearing-error and mag-bearing-error
314 * Tied-properties helper, record nodes which are tied for easy un-tie-ing
316 template <typename T>
317 void tie(SGPropertyNode* aNode, const char* aRelPath, const SGRawValue<T>& aRawValue)
319 SGPropertyNode* nd = aNode->getNode(aRelPath, true);
320 _tiedNodes.push_back(nd);
324 /// helper, tie the lat/lon/elev of a SGGeod to the named children of aNode
325 void tieSGGeod(SGPropertyNode* aNode, SGGeod& aRef,
326 const char* lonStr, const char* latStr, const char* altStr);
328 /// helper, tie a SGGeod to proeprties, but read-only
329 void tieSGGeodReadOnly(SGPropertyNode* aNode, SGGeod& aRef,
330 const char* lonStr, const char* latStr, const char* altStr);
333 SGPropertyNode_ptr _gpsNode;
334 SGPropertyNode_ptr _magvar_node;
335 SGPropertyNode_ptr _serviceable_node;
336 SGPropertyNode_ptr _electrical_node;
337 SGPropertyNode_ptr _tracking_bug_node;
338 SGPropertyNode_ptr _raim_node;
340 SGPropertyNode_ptr _odometer_node;
341 SGPropertyNode_ptr _trip_odometer_node;
342 SGPropertyNode_ptr _true_bug_error_node;
343 SGPropertyNode_ptr _magnetic_bug_error_node;
345 SGPropertyNode_ptr _ref_navaid_id_node;
346 SGPropertyNode_ptr _ref_navaid_bearing_node;
347 SGPropertyNode_ptr _ref_navaid_distance_node;
348 SGPropertyNode_ptr _ref_navaid_mag_bearing_node;
349 SGPropertyNode_ptr _ref_navaid_frequency_node;
350 SGPropertyNode_ptr _ref_navaid_name_node;
352 SGPropertyNode_ptr _route_active_node;
353 SGPropertyNode_ptr _route_current_wp_node;
354 SGPropertyNode_ptr _routeDistanceNm;
355 SGPropertyNode_ptr _routeETE;
356 SGPropertyNode_ptr _routeEditedSignal;
357 SGPropertyNode_ptr _routeFinishedSignal;
359 double _selectedCourse;
364 double _last_speed_kts;
365 double _last_true_track;
366 double _last_vertical_speed;
369 GPSListener* _listener;
371 FGRouteMgr* _routeMgr;
373 bool _ref_navaid_set;
374 double _ref_navaid_elapsed;
375 FGPositionedRef _ref_navaid;
380 SGGeodProperty _position;
381 SGGeod _wp0_position;
382 SGGeod _wp1_position;
383 SGGeod _indicated_pos;
384 std::string _wp0Ident, _wp0Name, _wp1Ident, _wp1Name;
385 double _wp1DistanceM, _wp1TrueBearing;
389 SGPropertyNode_ptr _scratchNode;
393 int _searchResultIndex;
394 std::string _searchQuery;
395 FGPositioned::Type _searchType;
397 bool _searchOrderByRange;
398 bool _searchResultsCached;
399 FGPositioned::List _searchResults;
400 bool _searchIsRoute; ///< set if 'search' is actually the current route
401 bool _searchHasNext; ///< is there a result after this one?
402 bool _searchNames; ///< set if we're searching names instead of idents
405 bool _computeTurnData; ///< do we need to update the turn data?
406 bool _anticipateTurn; ///< are we anticipating the next turn or not?
407 bool _inTurn; // is a turn in progress?
408 bool _turnSequenced; // have we sequenced the new leg?
409 double _turnAngle; // angle to turn through, in degrees
410 double _turnStartBearing; // bearing of inbound leg
411 double _turnRadius; // radius of turn in nm
415 SGPropertyNode_ptr _realismSimpleGps; ///< should the GPS be simple or realistic?
417 // autopilot drive properties
418 SGPropertyNode_ptr _apTrueHeading;
419 SGPropertyNode_ptr _apTargetAltitudeFt;
420 SGPropertyNode_ptr _apAltitudeLock;
422 std::vector<SGPropertyNode_ptr> _tiedNodes;
426 #endif // __INSTRUMENTS_GPS_HXX