]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/gps.hxx
Make the GPS drive the autopilot directly (if configured), also update external cours...
[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 <simgear/props/props.hxx>
11 #include <simgear/structure/subsystem_mgr.hxx>
12 #include <simgear/math/SGMath.hxx>
13
14 #include "Navaids/positioned.hxx"
15
16 // forward decls
17 class SGRoute;
18 class FGRouteMgr;
19 class FGAirport;
20 class GPSListener;
21
22 class SGGeodProperty
23 {
24 public:
25     SGGeodProperty()
26     {
27     }
28         
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);    
31     void clear();    
32     void operator=(const SGGeod& geod);    
33     SGGeod get() const;
34 private:
35     SGPropertyNode_ptr _lon, _lat, _alt;
36 };
37
38 /**
39  * Model a GPS radio.
40  *
41  * Input properties:
42  *
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
49  * 
50  *
51  * Output properties:
52  *
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
60  *
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
72
73  */
74 class GPS : public SGSubsystem
75 {
76
77 public:
78
79     GPS (SGPropertyNode *node);
80     GPS ();
81     virtual ~GPS ();
82
83     virtual void init ();
84     virtual void update (double delta_time_sec);
85
86 private:
87     friend class GPSListener;
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 init(SGPropertyNode*);
99       
100       bool turnAnticipationEnabled() const
101       { return _enableTurnAnticipation; }
102       
103       /**
104        * Desired turn rate in degrees/second. From this we derive the turn
105        * radius and hence how early we need to anticipate it.
106        */
107       double turnRateDegSec() const
108       { return _turnRate; }
109       
110       /**
111        * Distance at which we arm overflight sequencing. Once inside this
112        * distance, a change of the wp1 'TO' flag to false will be considered
113        * overlight of the wp.
114        */
115       double overflightArmDistanceNm() const
116       { return _overflightArmDistance; }
117       
118       /**
119        * Time before the next WP to activate an external annunciator
120        */
121       double waypointAlertTime() const
122       { return _waypointAlertTime; }
123             
124       bool tuneNavRadioToRefVor() const
125       { return _tuneRadio1ToRefVor; }
126       
127       bool requireHardSurface() const
128       { return _requireHardSurface; }
129       
130       double minRunwayLengthFt() const
131       { return _minRunwayLengthFt; }
132       
133       double getExternalCourse() const;
134       
135       void setExternalCourse(double aCourseDeg);
136       
137       bool cdiDeflectionIsAngular() const
138       { return (_cdiMaxDeflectionNm <= 0.0); }
139       
140       double cdiDeflectionLinearPeg() const
141       {
142         assert(_cdiMaxDeflectionNm > 0.0);
143         return _cdiMaxDeflectionNm;
144       }
145       
146       bool driveAutopilot() const
147       { return _driveAutopilot; }
148     private:
149       bool _enableTurnAnticipation;
150       
151       // desired turn rate in degrees per second
152       double _turnRate;
153       
154       // distance from waypoint to arm overflight sequencing (in nm)
155       double _overflightArmDistance;
156       
157       // time before reaching a waypoint to trigger annunicator light/sound 
158       // (in seconds)
159       double _waypointAlertTime;
160       
161       // should GPS automatically tune NAV1 to the reference VOR?
162       bool _tuneRadio1ToRefVor;
163       
164       // minimum runway length to require when filtering
165       double _minRunwayLengthFt;
166       
167       // should we require a hard-surfaced runway when filtering?
168       bool _requireHardSurface;
169       
170       // helpers to tie course-source property
171       const char* getCourseSource() const;
172       void setCourseSource(const char* aPropPath);
173       
174       // property to retrieve the external course from
175       SGPropertyNode_ptr _extCourseSource;
176       
177       double _cdiMaxDeflectionNm;
178       
179       bool _driveAutopilot;
180     };
181     
182     class SearchFilter : public FGPositioned::Filter
183     {
184     public:      
185       virtual bool pass(FGPositioned* aPos) const;
186           
187       virtual FGPositioned::Type minType() const;
188       virtual FGPositioned::Type maxType() const;
189     };
190     
191     /**
192      * reset all output properties to default / non-service values
193      */
194     void clearOutput();
195
196     void updateWithValid(double dt);
197     void updateBasicData(double dt);
198     void updateWaypoints();
199
200     void updateTrackingBug();
201     void updateReferenceNavaid(double dt);
202     void referenceNavaidSet(const std::string& aNavaid);
203     void tuneNavRadios();
204     void updateRouteData();
205     void driveAutopilot();
206     
207     void routeActivated();
208     void routeManagerSequenced();
209     
210     void updateTurn();  
211     void updateOverflight();    
212     void beginTurn();
213     void endTurn();
214     
215     double computeTurnProgress(double aBearing) const;
216     void computeTurnData();
217     void updateTurnData();
218     double computeTurnRadiusNm(double aGroundSpeedKts) const;
219   
220   /**
221    * Update one-shot things when WP1 / leg data change
222    */
223   void wp1Changed();
224   
225 // scratch maintenence utilities
226   void setScratchFromPositioned(FGPositioned* aPos, int aIndex);
227   void setScratchFromCachedSearchResult();
228   void setScratchFromRouteWaypoint(int aIndex);
229   
230   /**
231    * Add airport-specific information to a scratch result
232    */
233   void addAirportToScratch(FGAirport* aAirport);
234   
235   void clearScratch();
236   
237   /**
238    * Predicate, determine if the lon/lat position in the scratch is 
239    * valid or not.
240    */
241   bool isScratchPositionValid() const;
242   
243   FGPositioned::Filter* createFilter(FGPositioned::Type aTy);
244   
245   /**
246    * Search kernel - called each time we step through a result
247    */
248   void performSearch();
249   
250 // command handlers
251   void selectLegMode();
252   void selectOBSMode();
253   void directTo();
254   void loadRouteWaypoint();
255   void loadNearest();
256   void search();
257   void nextResult();
258   void previousResult();
259   void defineWaypoint();
260   
261 // tied-property getter/setters
262   void setCommand(const char* aCmd);
263   const char* getCommand() const { return ""; }
264   
265   const char* getMode() const { return _mode.c_str(); }
266   
267   bool getScratchValid() const { return _scratchValid; }
268   double getScratchDistance() const;
269   double getScratchMagBearing() const;
270   double getScratchTrueBearing() const;
271   bool getScratchHasNext() const { return _searchHasNext; }
272   
273   double getSelectedCourse() const { return _selectedCourse; }
274   double getCDIDeflection() const;
275   
276   double getLegDistance() const;
277   double getLegCourse() const;
278   double getLegMagCourse() const;
279   double getAltDistanceRatio() const;
280   
281   double getTrueTrack() const { return _last_true_track; }
282   double getMagTrack() const;
283   double getGroundspeedKts() const { return _last_speed_kts; }
284   double getVerticalSpeed() const { return _last_vertical_speed; }
285   
286   //bool getLegMode() const { return _mode == "leg"; }
287   //bool getObsMode() const { return _mode == "obs"; }
288   
289   const char* getWP0Ident() const;
290   const char* getWP0Name() const;
291   
292   const char* getWP1Ident() const;
293   const char* getWP1Name() const;
294   
295   double getWP1Distance() const;
296   double getWP1TTW() const;
297   const char* getWP1TTWString() const;
298   double getWP1Bearing() const;
299   double getWP1MagBearing() const;
300   double getWP1CourseDeviation() const;
301   double getWP1CourseErrorNm() const;
302   bool getWP1ToFlag() const;
303   bool getWP1FromFlag() const;
304   
305   // true-bearing-error and mag-bearing-error
306   
307   
308   
309 // members
310   SGPropertyNode_ptr _magvar_node;
311   SGPropertyNode_ptr _serviceable_node;
312   SGPropertyNode_ptr _electrical_node;
313   SGPropertyNode_ptr _tracking_bug_node;
314   SGPropertyNode_ptr _raim_node;
315
316       SGPropertyNode_ptr _odometer_node;
317     SGPropertyNode_ptr _trip_odometer_node;
318     SGPropertyNode_ptr _true_bug_error_node;
319     SGPropertyNode_ptr _magnetic_bug_error_node;
320     
321     SGPropertyNode_ptr _ref_navaid_id_node;
322     SGPropertyNode_ptr _ref_navaid_bearing_node;
323     SGPropertyNode_ptr _ref_navaid_distance_node;
324     SGPropertyNode_ptr _ref_navaid_mag_bearing_node;
325     SGPropertyNode_ptr _ref_navaid_frequency_node;
326     SGPropertyNode_ptr _ref_navaid_name_node;
327     
328     SGPropertyNode_ptr _route_active_node;
329     SGPropertyNode_ptr _route_current_wp_node;
330     SGPropertyNode_ptr _routeDistanceNm;
331     SGPropertyNode_ptr _routeETE;
332         
333     double _selectedCourse;
334     
335     bool _last_valid;
336     SGGeod _last_pos;
337     double _last_speed_kts;
338     double _last_true_track;
339     double _last_vertical_speed;
340     
341     std::string _mode;
342     GPSListener* _listener;
343     Config _config;
344     FGRouteMgr* _routeMgr;
345     
346     bool _ref_navaid_set;
347     double _ref_navaid_elapsed;
348     FGPositionedRef _ref_navaid;
349     
350     std::string _name;
351     int _num;
352   
353   SGGeodProperty _position;
354   SGGeod _wp0_position;
355   SGGeod _wp1_position;
356   SGGeod _indicated_pos;
357   std::string _wp0Ident, _wp0Name, _wp1Ident, _wp1Name;
358   double _wp1DistanceM, _wp1TrueBearing;
359   
360 // scratch data
361   SGGeod _scratchPos;
362   SGPropertyNode_ptr _scratchNode;
363   bool _scratchValid;
364   
365 // search data
366   int _searchResultIndex;
367   std::string _searchQuery;
368   FGPositioned::Type _searchType;
369   bool _searchExact;
370   bool _searchOrderByRange;
371   bool _searchResultsCached;
372   FGPositioned::List _searchResults;
373   bool _searchIsRoute; ///< set if 'search' is actually the current route
374   bool _searchHasNext; ///< is there a result after this one?
375   bool _searchNames; ///< set if we're searching names instead of idents
376   
377   // turn data
378     bool _computeTurnData; ///< do we need to update the turn data?
379     bool _anticipateTurn; ///< are we anticipating the next turn or not?
380     bool _inTurn; // is a turn in progress?
381     bool _turnSequenced; // have we sequenced the new leg?
382     double _turnAngle; // angle to turn through, in degrees
383     double _turnStartBearing; // bearing of inbound leg
384     double _turnRadius; // radius of turn in nm
385     SGGeod _turnPt;
386     SGGeod _turnCentre;
387   
388   SGPropertyNode_ptr _realismSimpleGps; ///< should the GPS be simple or realistic?
389   
390 // autopilot drive properties
391   SGPropertyNode_ptr _apTrueHeading;
392   SGPropertyNode_ptr _apTargetAltitudeFt;
393   SGPropertyNode_ptr _apAltitudeLock;
394 };
395
396
397 #endif // __INSTRUMENTS_GPS_HXX