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 <Navaids/FlightPlan.hxx>
19 #include <Instrumentation/rnav_waypt_controller.hxx>
21 #define FG_210_COMPAT 1
28 * /position/longitude-deg
29 * /position/latitude-deg
30 * /position/altitude-ft
31 * /environment/magnetic-variation-deg
32 * /systems/electrical/outputs/gps
33 * /instrumentation/gps/serviceable
38 * /instrumentation/gps/indicated-longitude-deg
39 * /instrumentation/gps/indicated-latitude-deg
40 * /instrumentation/gps/indicated-altitude-ft
41 * /instrumentation/gps/indicated-vertical-speed-fpm
42 * /instrumentation/gps/indicated-track-true-deg
43 * /instrumentation/gps/indicated-track-magnetic-deg
44 * /instrumentation/gps/indicated-ground-speed-kt
46 * /instrumentation/gps/wp-distance-nm
47 * /instrumentation/gps/wp-bearing-deg
48 * /instrumentation/gps/wp-bearing-mag-deg
49 * /instrumentation/gps/TTW
50 * /instrumentation/gps/course-deviation-deg
51 * /instrumentation/gps/course-error-nm
52 * /instrumentation/gps/to-flag
53 * /instrumentation/gps/odometer
54 * /instrumentation/gps/trip-odometer
55 * /instrumentation/gps/true-bug-error-deg
56 * /instrumentation/gps/magnetic-bug-error-deg
58 class GPS : public SGSubsystem,
59 public flightgear::RNAV,
60 public flightgear::FlightPlan::Delegate
63 GPS (SGPropertyNode *node, bool defaultGPSMode = false);
67 // SGSubsystem interface
69 virtual void reinit ();
70 virtual void update (double delta_time_sec);
73 virtual void unbind();
76 virtual SGGeod position();
77 virtual double trackDeg();
78 virtual double groundSpeedKts();
79 virtual double vspeedFPM();
80 virtual double magvarDeg();
81 virtual double selectedMagCourse();
82 virtual double overflightDistanceM();
83 virtual double overflightArmDistanceM();
84 virtual double overflightArmAngleDeg();
85 virtual SGGeod previousLegWaypointPosition(bool& isValid);
88 friend class SearchFilter;
91 * Configuration manager, track data relating to aircraft installation
98 void bind(GPS* aOwner, SGPropertyNode* aCfg);
100 bool turnAnticipationEnabled() const { return _enableTurnAnticipation; }
103 * Desired turn rate in degrees/second. From this we derive the turn
104 * radius and hence how early we need to anticipate it.
106 double turnRateDegSec() const { return _turnRate; }
109 * Distance at which we switch to next waypoint.
111 double overflightDistanceNm() const { return _overflightDistance; }
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 { return _overflightArmDistance; }
119 * abs angle at which we arm overflight sequencing.
121 double overflightArmAngleDeg() const { return _overflightArmAngle; }
124 * Time before the next WP to activate an external annunciator
126 double waypointAlertTime() const { return _waypointAlertTime; }
128 bool requireHardSurface() const { return _requireHardSurface; }
130 bool cdiDeflectionIsAngular() const { return (_cdiMaxDeflectionNm <= 0.0); }
132 double cdiDeflectionLinearPeg() const
134 assert(_cdiMaxDeflectionNm > 0.0);
135 return _cdiMaxDeflectionNm;
138 bool driveAutopilot() const { return _driveAutopilot; }
140 bool courseSelectable() const { return _courseSelectable; }
143 bool _enableTurnAnticipation;
145 // desired turn rate in degrees per second
148 // distance from waypoint to arm overflight sequencing (in nm)
149 double _overflightDistance;
151 // distance from waypoint to arm overflight sequencing (in nm)
152 double _overflightArmDistance;
154 //abs angle from course to waypoint to arm overflight sequencing (in deg)
155 double _overflightArmAngle;
157 // time before reaching a waypoint to trigger annunciator light/sound
159 double _waypointAlertTime;
161 // should we require a hard-surfaced runway when filtering?
162 bool _requireHardSurface;
164 double _cdiMaxDeflectionNm;
166 // should we drive the autopilot directly or not?
167 bool _driveAutopilot;
169 // is selected-course-deg read to set desired-course or not?
170 bool _courseSelectable;
173 class SearchFilter : public FGPositioned::Filter
176 virtual bool pass(FGPositioned* aPos) const;
178 virtual FGPositioned::Type minType() const;
179 virtual FGPositioned::Type maxType() const;
182 /** reset all output properties to default / non-service values */
185 void updateBasicData(double dt);
187 void updateTrackingBug();
188 void updateRouteData();
189 void driveAutopilot();
192 void updateOverflight();
196 double computeTurnProgress(double aBearing) const;
197 void computeTurnData();
198 void updateTurnData();
199 double computeTurnRadiusNm(double aGroundSpeedKts) const;
201 /** Update one-shot things when WP1 / leg data change */
206 /** Predicate, determine if the lon/lat position in the scratch is
208 bool isScratchPositionValid() const;
209 FGPositionedRef positionedFromScratch() const;
212 void setScratchFromPositioned(FGPositioned* aPos, int aIndex);
213 void setScratchFromCachedSearchResult();
214 void setScratchFromRouteWaypoint(int aIndex);
216 /** Add airport-specific information to a scratch result */
217 void addAirportToScratch(FGAirport* aAirport);
219 FGPositioned::Filter* createFilter(FGPositioned::Type aTy);
221 /** Search kernel - called each time we step through a result */
222 void performSearch();
225 void loadRouteWaypoint();
229 void previousResult();
230 void defineWaypoint();
231 void insertWaypointAtIndex(int aIndex);
232 void removeWaypointAtIndex(int aIndex);
234 // tied-property getter/setters
235 double getScratchDistance() const;
236 double getScratchMagBearing() const;
237 double getScratchTrueBearing() const;
238 bool getScratchHasNext() const;
243 void selectLegMode();
244 void selectOBSMode(flightgear::Waypt* waypt);
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(); }
252 bool getScratchValid() const { return _scratchValid; }
254 double getSelectedCourse() const { return _selectedCourse; }
255 void setSelectedCourse(double crs);
256 double getDesiredCourse() const { return _desiredCourse; }
258 double getCDIDeflection() const;
260 double getLegDistance() const;
261 double getLegCourse() const;
262 double getLegMagCourse() const;
264 double getTrueTrack() const { return _last_true_track; }
265 double getMagTrack() const;
266 double getGroundspeedKts() const { return _last_speed_kts; }
267 double getVerticalSpeed() const { return _last_vertical_speed; }
269 const char* getWP0Ident() const;
270 const char* getWP0Name() const;
272 bool getWP1IValid() const;
273 const char* getWP1Ident() const;
274 const char* getWP1Name() const;
276 double getWP1Distance() const;
277 double getWP1TTW() const;
278 const char* getWP1TTWString() const;
279 double getWP1Bearing() const;
280 double getWP1MagBearing() const;
281 double getWP1CourseDeviation() const;
282 double getWP1CourseErrorNm() const;
283 bool getWP1ToFlag() const;
284 bool getWP1FromFlag() const;
286 // true-bearing-error and mag-bearing-error
290 * Tied-properties helper, record nodes which are tied for easy un-tie-ing
292 template <typename T>
293 void tie(SGPropertyNode* aNode, const char* aRelPath, const SGRawValue<T>& aRawValue)
295 _tiedProperties.Tie(aNode->getNode(aRelPath, true), aRawValue);
298 /** helper, tie the lat/lon/elev of a SGGeod to the named children of aNode */
299 void tieSGGeod(SGPropertyNode* aNode, SGGeod& aRef,
300 const char* lonStr, const char* latStr, const char* altStr);
302 /** helper, tie a SGGeod to proeprties, but read-only */
303 void tieSGGeodReadOnly(SGPropertyNode* aNode, SGGeod& aRef,
304 const char* lonStr, const char* latStr, const char* altStr);
306 // FlightPlan::Delegate
307 virtual void currentWaypointChanged();
308 virtual void waypointsChanged();
309 virtual void cleared();
310 virtual void endOfFlightPlan();
313 void routeManagerFlightPlanChanged(SGPropertyNode*);
314 void routeActivated(SGPropertyNode*);
317 SGPropertyNode_ptr _gpsNode;
318 SGPropertyNode_ptr _currentWayptNode;
319 SGPropertyNode_ptr _magvar_node;
320 SGPropertyNode_ptr _serviceable_node;
321 SGPropertyNode_ptr _electrical_node;
322 SGPropertyNode_ptr _tracking_bug_node;
323 SGPropertyNode_ptr _raim_node;
325 SGPropertyNode_ptr _odometer_node;
326 SGPropertyNode_ptr _trip_odometer_node;
327 SGPropertyNode_ptr _true_bug_error_node;
328 SGPropertyNode_ptr _magnetic_bug_error_node;
329 SGPropertyNode_ptr _eastWestVelocity;
330 SGPropertyNode_ptr _northSouthVelocity;
332 // SGPropertyNode_ptr _route_active_node;
333 SGPropertyNode_ptr _route_current_wp_node;
334 SGPropertyNode_ptr _routeDistanceNm;
335 SGPropertyNode_ptr _routeETE;
336 SGPropertyNode_ptr _desiredCourseNode;
338 double _selectedCourse;
339 double _desiredCourse;
344 double _last_speed_kts;
345 double _last_true_track;
346 double _last_vertical_speed;
347 double _lastEWVelocity;
348 double _lastNSVelocity;
351 * the instrument manager creates a default instance of us,
352 * if no explicit GPS is specific in the aircraft's instruments.xml file.
353 * This allows default route-following to work with the generic autopilot.
354 * This flag is set in that case, to inform us we're a 'fake' installation,
355 * and not to worry about electrical power or similar.
357 bool _defaultGPSMode;
364 SGGeod _wp0_position;
365 SGGeod _indicated_pos;
366 double _legDistanceNm;
370 SGPropertyNode_ptr _scratchNode;
374 int _searchResultIndex;
375 std::string _searchQuery;
376 FGPositioned::Type _searchType;
378 FGPositionedList _searchResults;
379 bool _searchIsRoute; ///< set if 'search' is actually the current route
380 bool _searchHasNext; ///< is there a result after this one?
381 bool _searchNames; ///< set if we're searching names instead of idents
385 bool _computeTurnData; ///< do we need to update the turn data?
386 bool _anticipateTurn; ///< are we anticipating the next turn or not?
387 bool _inTurn; // is a turn in progress?
388 bool _turnSequenced; // have we sequenced the new leg?
389 double _turnAngle; // angle to turn through, in degrees
390 double _turnStartBearing; // bearing of inbound leg
391 double _turnRadius; // radius of turn in nm
395 std::auto_ptr<flightgear::WayptController> _wayptController;
397 flightgear::WayptRef _prevWaypt;
398 flightgear::WayptRef _currentWaypt;
400 // autopilot drive properties
401 SGPropertyNode_ptr _apDrivingFlag;
402 SGPropertyNode_ptr _apTrueHeading;
404 simgear::TiedPropertyList _tiedProperties;
406 flightgear::FlightPlanRef _route;
408 SGPropertyChangeCallback<GPS> _callbackFlightPlanChanged;
409 SGPropertyChangeCallback<GPS> _callbackRouteActivated;
412 #endif // __INSTRUMENTS_GPS_HXX