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 double minRunwayLengthFt() const { return _minRunwayLengthFt; }
139 bool cdiDeflectionIsAngular() const { return (_cdiMaxDeflectionNm <= 0.0); }
141 double cdiDeflectionLinearPeg() const
143 assert(_cdiMaxDeflectionNm > 0.0);
144 return _cdiMaxDeflectionNm;
147 bool driveAutopilot() const { return _driveAutopilot; }
149 bool courseSelectable() const { return _courseSelectable; }
152 bool _enableTurnAnticipation;
154 // desired turn rate in degrees per second
157 // distance from waypoint to arm overflight sequencing (in nm)
158 double _overflightArmDistance;
160 // time before reaching a waypoint to trigger annunciator light/sound
162 double _waypointAlertTime;
164 // minimum runway length to require when filtering
165 double _minRunwayLengthFt;
167 // should we require a hard-surfaced runway when filtering?
168 bool _requireHardSurface;
170 double _cdiMaxDeflectionNm;
172 // should we drive the autopilot directly or not?
173 bool _driveAutopilot;
175 // is selected-course-deg read to set desired-course or not?
176 bool _courseSelectable;
179 class SearchFilter : public FGPositioned::Filter
182 virtual bool pass(FGPositioned* aPos) const;
184 virtual FGPositioned::Type minType() const;
185 virtual FGPositioned::Type maxType() const;
188 /** reset all output properties to default / non-service values */
191 void updateBasicData(double dt);
193 void updateTrackingBug();
194 void updateReferenceNavaid(double dt);
195 void referenceNavaidSet(const std::string& aNavaid);
196 void updateRouteData();
197 void driveAutopilot();
199 void routeActivated();
200 void routeManagerSequenced();
202 void routeFinished();
205 void updateOverflight();
209 double computeTurnProgress(double aBearing) const;
210 void computeTurnData();
211 void updateTurnData();
212 double computeTurnRadiusNm(double aGroundSpeedKts) const;
214 /** Update one-shot things when WP1 / leg data change */
217 // scratch maintenance utilities
218 void setScratchFromPositioned(FGPositioned* aPos, int aIndex);
219 void setScratchFromCachedSearchResult();
220 void setScratchFromRouteWaypoint(int aIndex);
222 /** Add airport-specific information to a scratch result */
223 void addAirportToScratch(FGAirport* aAirport);
227 /** Predicate, determine if the lon/lat position in the scratch is
229 bool isScratchPositionValid() const;
231 FGPositioned::Filter* createFilter(FGPositioned::Type aTy);
233 /** Search kernel - called each time we step through a result */
234 void performSearch();
237 void selectLegMode();
238 void selectOBSMode();
240 void loadRouteWaypoint();
244 void previousResult();
245 void defineWaypoint();
246 void insertWaypointAtIndex(int aIndex);
247 void removeWaypointAtIndex(int aIndex);
249 // tied-property getter/setters
250 void setCommand(const char* aCmd);
251 const char* getCommand() const { return ""; }
253 const char* getMode() const { return _mode.c_str(); }
255 bool getScratchValid() const { return _scratchValid; }
256 double getScratchDistance() const;
257 double getScratchMagBearing() const;
258 double getScratchTrueBearing() const;
259 bool getScratchHasNext() const;
261 double getSelectedCourse() const { return _selectedCourse; }
262 void setSelectedCourse(double crs);
263 double getDesiredCourse() const { return _desiredCourse; }
265 double getCDIDeflection() const;
267 double getLegDistance() const;
268 double getLegCourse() const;
269 double getLegMagCourse() const;
271 double getTrueTrack() const { return _last_true_track; }
272 double getMagTrack() const;
273 double getGroundspeedKts() const { return _last_speed_kts; }
274 double getVerticalSpeed() const { return _last_vertical_speed; }
276 //bool getLegMode() const { return _mode == "leg"; }
277 //bool getObsMode() const { return _mode == "obs"; }
279 const char* getWP0Ident() const;
280 const char* getWP0Name() const;
282 const char* getWP1Ident() const;
283 const char* getWP1Name() const;
285 double getWP1Distance() const;
286 double getWP1TTW() const;
287 const char* getWP1TTWString() const;
288 double getWP1Bearing() const;
289 double getWP1MagBearing() const;
290 double getWP1CourseDeviation() const;
291 double getWP1CourseErrorNm() const;
292 bool getWP1ToFlag() const;
293 bool getWP1FromFlag() const;
295 // true-bearing-error and mag-bearing-error
299 * Tied-properties helper, record nodes which are tied for easy un-tie-ing
301 template <typename T>
302 void tie(SGPropertyNode* aNode, const char* aRelPath, const SGRawValue<T>& aRawValue)
304 _tiedProperties.Tie(aNode->getNode(aRelPath, true), aRawValue);
307 /** helper, tie the lat/lon/elev of a SGGeod to the named children of aNode */
308 void tieSGGeod(SGPropertyNode* aNode, SGGeod& aRef,
309 const char* lonStr, const char* latStr, const char* altStr);
311 /** helper, tie a SGGeod to proeprties, but read-only */
312 void tieSGGeodReadOnly(SGPropertyNode* aNode, SGGeod& aRef,
313 const char* lonStr, const char* latStr, const char* altStr);
316 SGPropertyNode_ptr _gpsNode;
317 SGPropertyNode_ptr _currentWayptNode;
318 SGPropertyNode_ptr _magvar_node;
319 SGPropertyNode_ptr _serviceable_node;
320 SGPropertyNode_ptr _electrical_node;
321 SGPropertyNode_ptr _tracking_bug_node;
322 SGPropertyNode_ptr _raim_node;
324 SGPropertyNode_ptr _odometer_node;
325 SGPropertyNode_ptr _trip_odometer_node;
326 SGPropertyNode_ptr _true_bug_error_node;
327 SGPropertyNode_ptr _magnetic_bug_error_node;
328 SGPropertyNode_ptr _eastWestVelocity;
329 SGPropertyNode_ptr _northSouthVelocity;
331 SGPropertyNode_ptr _ref_navaid_id_node;
332 SGPropertyNode_ptr _ref_navaid_bearing_node;
333 SGPropertyNode_ptr _ref_navaid_distance_node;
334 SGPropertyNode_ptr _ref_navaid_mag_bearing_node;
335 SGPropertyNode_ptr _ref_navaid_frequency_node;
336 SGPropertyNode_ptr _ref_navaid_name_node;
338 SGPropertyNode_ptr _route_active_node;
339 SGPropertyNode_ptr _route_current_wp_node;
340 SGPropertyNode_ptr _routeDistanceNm;
341 SGPropertyNode_ptr _routeETE;
342 SGPropertyNode_ptr _routeEditedSignal;
343 SGPropertyNode_ptr _routeFinishedSignal;
344 SGPropertyNode_ptr _desiredCourseNode;
346 double _selectedCourse;
347 double _desiredCourse;
352 double _last_speed_kts;
353 double _last_true_track;
354 double _last_vertical_speed;
355 double _lastEWVelocity;
356 double _lastNSVelocity;
359 GPSListener* _listener;
361 FGRouteMgr* _routeMgr;
363 bool _ref_navaid_set;
364 double _ref_navaid_elapsed;
365 FGPositionedRef _ref_navaid;
370 SGGeodProperty _position;
371 SGGeod _wp0_position;
372 SGGeod _indicated_pos;
373 double _legDistanceNm;
377 SGPropertyNode_ptr _scratchNode;
381 int _searchResultIndex;
382 std::string _searchQuery;
383 FGPositioned::Type _searchType;
385 FGPositioned::List _searchResults;
386 bool _searchIsRoute; ///< set if 'search' is actually the current route
387 bool _searchHasNext; ///< is there a result after this one?
388 bool _searchNames; ///< set if we're searching names instead of idents
391 bool _computeTurnData; ///< do we need to update the turn data?
392 bool _anticipateTurn; ///< are we anticipating the next turn or not?
393 bool _inTurn; // is a turn in progress?
394 bool _turnSequenced; // have we sequenced the new leg?
395 double _turnAngle; // angle to turn through, in degrees
396 double _turnStartBearing; // bearing of inbound leg
397 double _turnRadius; // radius of turn in nm
401 std::auto_ptr<flightgear::WayptController> _wayptController;
403 SGPropertyNode_ptr _realismSimpleGps; ///< should the GPS be simple or realistic?
404 flightgear::WayptRef _prevWaypt;
405 flightgear::WayptRef _currentWaypt;
407 // autopilot drive properties
408 SGPropertyNode_ptr _apDrivingFlag;
409 SGPropertyNode_ptr _apTrueHeading;
410 SGPropertyNode_ptr _apTargetAltitudeFt;
411 SGPropertyNode_ptr _apAltitudeLock;
413 simgear::TiedPropertyList _tiedProperties;
417 #endif // __INSTRUMENTS_GPS_HXX