]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/gps.hxx
444f029d25d15aed55f5559d89929a57c834f428
[flightgear.git] / src / Instrumentation / gps.hxx
1 // gps.hxx - distance-measuring equipment.
2 // Written by David Megginson, started 2003.
3 //
4 // This file is in the Public Domain and comes with no warranty.
5
6
7 #ifndef __INSTRUMENTS_GPS_HXX
8 #define __INSTRUMENTS_GPS_HXX 1
9
10 #include <cassert>
11 #include <memory>
12
13 #include <simgear/props/props.hxx>
14 #include <simgear/structure/subsystem_mgr.hxx>
15 #include <simgear/props/tiedpropertylist.hxx>
16
17 #include <Navaids/positioned.hxx>
18 #include <Navaids/FlightPlan.hxx>
19 #include <Instrumentation/rnav_waypt_controller.hxx>
20
21 #define FG_210_COMPAT 1
22
23 /**
24  * Model a GPS radio.
25  *
26  * Input properties:
27  *
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
34  * 
35  *
36  * Output properties:
37  *
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
45  *
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
57  */
58 class GPS : public SGSubsystem,
59             public flightgear::RNAV,
60             public flightgear::FlightPlan::Delegate
61 {
62 public:
63     GPS (SGPropertyNode *node, bool defaultGPSMode = false);
64     GPS ();
65     virtual ~GPS ();
66
67   // SGSubsystem interface
68     virtual void init ();
69     virtual void reinit ();
70     virtual void update (double delta_time_sec);
71
72     virtual void bind();
73     virtual void unbind();
74
75   // RNAV interface
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);
86
87 private:
88     friend class SearchFilter;
89
90     /**
91      * Configuration manager, track data relating to aircraft installation
92      */
93     class Config
94     {
95     public:
96       Config();
97
98       void bind(GPS* aOwner, SGPropertyNode* aCfg);
99
100       bool turnAnticipationEnabled() const { return _enableTurnAnticipation; }
101
102       /**
103        * Desired turn rate in degrees/second. From this we derive the turn
104        * radius and hence how early we need to anticipate it.
105        */
106       double turnRateDegSec() const        { return _turnRate; }
107
108       /**
109        * Distance at which we switch to next waypoint.
110        */
111       double overflightDistanceNm() const { return _overflightDistance; }
112       /**
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.
116        */
117       double overflightArmDistanceNm() const { return _overflightArmDistance; }
118       /**
119                  * abs angle at which we arm overflight sequencing.
120                  */
121       double overflightArmAngleDeg() const { return _overflightArmAngle; }
122
123       /**
124        * Time before the next WP to activate an external annunciator
125        */
126       double waypointAlertTime() const     { return _waypointAlertTime; }
127
128       bool requireHardSurface() const      { return _requireHardSurface; }
129
130       bool cdiDeflectionIsAngular() const  { return (_cdiMaxDeflectionNm <= 0.0); }
131
132       double cdiDeflectionLinearPeg() const
133       {
134         assert(_cdiMaxDeflectionNm > 0.0);
135         return _cdiMaxDeflectionNm;
136       }
137
138       bool driveAutopilot() const          { return _driveAutopilot; }
139
140       bool courseSelectable() const        { return _courseSelectable; }
141
142     private:
143       bool _enableTurnAnticipation;
144
145       // desired turn rate in degrees per second
146       double _turnRate;
147
148       // distance from waypoint to arm overflight sequencing (in nm)
149       double _overflightDistance;
150
151       // distance from waypoint to arm overflight sequencing (in nm)
152       double _overflightArmDistance;
153
154       //abs angle from course to waypoint to arm overflight sequencing (in deg)
155       double _overflightArmAngle;
156
157       // time before reaching a waypoint to trigger annunciator light/sound
158       // (in seconds)
159       double _waypointAlertTime;
160
161       // should we require a hard-surfaced runway when filtering?
162       bool _requireHardSurface;
163
164       double _cdiMaxDeflectionNm;
165
166       // should we drive the autopilot directly or not?
167       bool _driveAutopilot;
168
169       // is selected-course-deg read to set desired-course or not?
170       bool _courseSelectable;
171     };
172
173     class SearchFilter : public FGPositioned::Filter
174     {
175     public:
176       virtual bool pass(FGPositioned* aPos) const;
177
178       virtual FGPositioned::Type minType() const;
179       virtual FGPositioned::Type maxType() const;
180     };
181
182     /** reset all output properties to default / non-service values */
183     void clearOutput();
184
185     void updateBasicData(double dt);
186
187     void updateTrackingBug();
188     void updateRouteData();
189     void driveAutopilot();
190
191     void updateTurn();
192     void updateOverflight();
193     void beginTurn();
194     void endTurn();
195
196     double computeTurnProgress(double aBearing) const;
197     void computeTurnData();
198     void updateTurnData();
199     double computeTurnRadiusNm(double aGroundSpeedKts) const;
200
201     /** Update one-shot things when WP1 / leg data change */
202     void wp1Changed();
203
204     void clearScratch();
205
206     /** Predicate, determine if the lon/lat position in the scratch is
207      * valid or not. */
208     bool isScratchPositionValid() const;
209
210 #if FG_210_COMPAT
211     void setScratchFromPositioned(FGPositioned* aPos, int aIndex);
212     void setScratchFromCachedSearchResult();
213     void setScratchFromRouteWaypoint(int aIndex);
214     
215     /** Add airport-specific information to a scratch result */
216     void addAirportToScratch(FGAirport* aAirport);
217     
218     FGPositioned::Filter* createFilter(FGPositioned::Type aTy);
219     
220     /** Search kernel - called each time we step through a result */
221     void performSearch();
222     
223     // command handlers
224     void loadRouteWaypoint();
225     void loadNearest();
226     void search();
227     void nextResult();
228     void previousResult();
229     void defineWaypoint();
230     void insertWaypointAtIndex(int aIndex);
231     void removeWaypointAtIndex(int aIndex);
232     
233     // tied-property getter/setters
234     double getScratchDistance() const;
235     double getScratchMagBearing() const;
236     double getScratchTrueBearing() const;
237     bool getScratchHasNext() const;
238
239 #endif
240     
241 // command handlers
242     void selectLegMode();
243     void selectOBSMode(flightgear::Waypt* waypt);
244     void directTo();
245
246 // tied-property getter/setters
247     void setCommand(const char* aCmd);
248     const char* getCommand() const { return ""; }
249
250     const char* getMode() const { return _mode.c_str(); }
251     bool getScratchValid() const { return _scratchValid; }
252
253     double getSelectedCourse() const { return _selectedCourse; }
254     void setSelectedCourse(double crs);
255     double getDesiredCourse() const { return _desiredCourse; }
256
257     double getCDIDeflection() const;
258
259     double getLegDistance() const;
260     double getLegCourse() const;
261     double getLegMagCourse() const;
262
263     double getTrueTrack() const { return _last_true_track; }
264     double getMagTrack() const;
265     double getGroundspeedKts() const { return _last_speed_kts; }
266     double getVerticalSpeed() const { return _last_vertical_speed; }
267
268     const char* getWP0Ident() const;
269     const char* getWP0Name() const;
270
271     const char* getWP1Ident() const;
272     const char* getWP1Name() const;
273
274     double getWP1Distance() const;
275     double getWP1TTW() const;
276     const char* getWP1TTWString() const;
277     double getWP1Bearing() const;
278     double getWP1MagBearing() const;
279     double getWP1CourseDeviation() const;
280     double getWP1CourseErrorNm() const;
281     bool getWP1ToFlag() const;
282     bool getWP1FromFlag() const;
283
284     // true-bearing-error and mag-bearing-error
285
286
287     /**
288      * Tied-properties helper, record nodes which are tied for easy un-tie-ing
289      */
290     template <typename T>
291     void tie(SGPropertyNode* aNode, const char* aRelPath, const SGRawValue<T>& aRawValue)
292     {
293         _tiedProperties.Tie(aNode->getNode(aRelPath, true), aRawValue);
294     }
295
296     /** helper, tie the lat/lon/elev of a SGGeod to the named children of aNode */
297     void tieSGGeod(SGPropertyNode* aNode, SGGeod& aRef,
298                    const char* lonStr, const char* latStr, const char* altStr);
299   
300     /** helper, tie a SGGeod to proeprties, but read-only */
301     void tieSGGeodReadOnly(SGPropertyNode* aNode, SGGeod& aRef,
302                            const char* lonStr, const char* latStr, const char* altStr);
303
304 // FlightPlan::Delegate
305     virtual void currentWaypointChanged();
306     virtual void waypointsChanged();
307     virtual void cleared();
308     virtual void endOfFlightPlan();
309     
310     void sequence();
311     void routeManagerFlightPlanChanged(SGPropertyNode*);
312     void routeActivated(SGPropertyNode*);
313     
314 // members
315     SGPropertyNode_ptr _gpsNode;
316     SGPropertyNode_ptr _currentWayptNode;
317     SGPropertyNode_ptr _magvar_node;
318     SGPropertyNode_ptr _serviceable_node;
319     SGPropertyNode_ptr _electrical_node;
320     SGPropertyNode_ptr _tracking_bug_node;
321     SGPropertyNode_ptr _raim_node;
322
323     SGPropertyNode_ptr _odometer_node;
324     SGPropertyNode_ptr _trip_odometer_node;
325     SGPropertyNode_ptr _true_bug_error_node;
326     SGPropertyNode_ptr _magnetic_bug_error_node;
327     SGPropertyNode_ptr _eastWestVelocity;
328     SGPropertyNode_ptr _northSouthVelocity;
329     
330   //  SGPropertyNode_ptr _route_active_node;
331     SGPropertyNode_ptr _route_current_wp_node;
332     SGPropertyNode_ptr _routeDistanceNm;
333     SGPropertyNode_ptr _routeETE;
334     SGPropertyNode_ptr _desiredCourseNode;
335     
336     double _selectedCourse;
337     double _desiredCourse;
338
339     bool _dataValid;
340     SGGeod _last_pos;
341     bool _lastPosValid;
342     double _last_speed_kts;
343     double _last_true_track;
344     double _last_vertical_speed;
345     double _lastEWVelocity;
346     double _lastNSVelocity;
347
348     /**
349      * the instrument manager creates a default instance of us,
350      * if no explicit GPS is specific in the aircraft's instruments.xml file.
351      * This allows default route-following to work with the generic autopilot.
352      * This flat is set in that case, to inform us we're a 'fake' installation,
353      * and not to worry about electrical power or similar.
354      */
355     bool _defaultGPSMode;
356     
357     std::string _mode;
358     Config _config;
359     std::string _name;
360     int _num;
361
362     SGGeod _wp0_position;
363     SGGeod _indicated_pos;
364     double _legDistanceNm;
365
366 // scratch data
367     SGGeod _scratchPos;
368     SGPropertyNode_ptr _scratchNode;
369     bool _scratchValid;
370 #if FG_210_COMPAT
371 // search data
372     int _searchResultIndex;
373     std::string _searchQuery;
374     FGPositioned::Type _searchType;
375     bool _searchExact;
376     FGPositionedList _searchResults;
377     bool _searchIsRoute; ///< set if 'search' is actually the current route
378     bool _searchHasNext; ///< is there a result after this one?
379     bool _searchNames; ///< set if we're searching names instead of idents
380 #endif
381     
382 // turn data
383     bool _computeTurnData; ///< do we need to update the turn data?
384     bool _anticipateTurn; ///< are we anticipating the next turn or not?
385     bool _inTurn; // is a turn in progress?
386     bool _turnSequenced; // have we sequenced the new leg?
387     double _turnAngle; // angle to turn through, in degrees
388     double _turnStartBearing; // bearing of inbound leg
389     double _turnRadius; // radius of turn in nm
390     SGGeod _turnPt;
391     SGGeod _turnCentre;
392
393     std::auto_ptr<flightgear::WayptController> _wayptController;
394
395     flightgear::WayptRef _prevWaypt;
396     flightgear::WayptRef _currentWaypt;
397
398 // autopilot drive properties
399     SGPropertyNode_ptr _apDrivingFlag;
400     SGPropertyNode_ptr _apTrueHeading;
401     
402     simgear::TiedPropertyList _tiedProperties;
403
404     flightgear::FlightPlanRef _route;
405     
406     SGPropertyChangeCallback<GPS> _callbackFlightPlanChanged;
407     SGPropertyChangeCallback<GPS> _callbackRouteActivated;
408 };
409
410 #endif // __INSTRUMENTS_GPS_HXX