]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/gps.hxx
MSVC9 fix
[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     virtual void bind();
87     virtual void unbind();
88 private:
89     friend class GPSListener;
90     friend class SearchFilter;
91     
92     /**
93      * Configuration manager, track data relating to aircraft installation
94      */
95     class Config
96     {
97     public:
98       Config();
99             
100       void bind(GPS* aOwner, SGPropertyNode* aCfg);
101       
102       bool turnAnticipationEnabled() const
103       { return _enableTurnAnticipation; }
104       
105       /**
106        * Desired turn rate in degrees/second. From this we derive the turn
107        * radius and hence how early we need to anticipate it.
108        */
109       double turnRateDegSec() const
110       { return _turnRate; }
111       
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
118       { return _overflightArmDistance; }
119       
120       /**
121        * Time before the next WP to activate an external annunciator
122        */
123       double waypointAlertTime() const
124       { return _waypointAlertTime; }
125             
126       bool tuneNavRadioToRefVor() const
127       { return _tuneRadio1ToRefVor; }
128       
129       bool requireHardSurface() const
130       { return _requireHardSurface; }
131       
132       double minRunwayLengthFt() const
133       { return _minRunwayLengthFt; }
134       
135       double getExternalCourse() const;
136       
137       void setExternalCourse(double aCourseDeg);
138       
139       bool cdiDeflectionIsAngular() const
140       { return (_cdiMaxDeflectionNm <= 0.0); }
141       
142       double cdiDeflectionLinearPeg() const
143       {
144         assert(_cdiMaxDeflectionNm > 0.0);
145         return _cdiMaxDeflectionNm;
146       }
147       
148       bool driveAutopilot() const
149       { return _driveAutopilot; }
150     private:
151       bool _enableTurnAnticipation;
152       
153       // desired turn rate in degrees per second
154       double _turnRate;
155       
156       // distance from waypoint to arm overflight sequencing (in nm)
157       double _overflightArmDistance;
158       
159       // time before reaching a waypoint to trigger annunicator light/sound 
160       // (in seconds)
161       double _waypointAlertTime;
162       
163       // should GPS automatically tune NAV1 to the reference VOR?
164       bool _tuneRadio1ToRefVor;
165       
166       // minimum runway length to require when filtering
167       double _minRunwayLengthFt;
168       
169       // should we require a hard-surfaced runway when filtering?
170       bool _requireHardSurface;
171       
172       // helpers to tie course-source property
173       const char* getCourseSource() const;
174       void setCourseSource(const char* aPropPath);
175       
176       // property to retrieve the external course from
177       SGPropertyNode_ptr _extCourseSource;
178       
179       double _cdiMaxDeflectionNm;
180       
181       bool _driveAutopilot;
182     };
183     
184     class SearchFilter : public FGPositioned::Filter
185     {
186     public:      
187       virtual bool pass(FGPositioned* aPos) const;
188           
189       virtual FGPositioned::Type minType() const;
190       virtual FGPositioned::Type maxType() const;
191     };
192     
193     /**
194      * reset all output properties to default / non-service values
195      */
196     void clearOutput();
197
198     void updateBasicData(double dt);
199     void updateWaypoints();
200
201     void updateTrackingBug();
202     void updateReferenceNavaid(double dt);
203     void referenceNavaidSet(const std::string& aNavaid);
204     void tuneNavRadios();
205     void updateRouteData();
206     void driveAutopilot();
207     
208     void routeActivated();
209     void routeManagerSequenced();
210     void routeEdited();
211     void routeFinished();
212     
213     void updateTurn();  
214     void updateOverflight();    
215     void beginTurn();
216     void endTurn();
217     
218     double computeTurnProgress(double aBearing) const;
219     void computeTurnData();
220     void updateTurnData();
221     double computeTurnRadiusNm(double aGroundSpeedKts) const;
222   
223   /**
224    * Update one-shot things when WP1 / leg data change
225    */
226   void wp1Changed();
227   
228 // scratch maintenence utilities
229   void setScratchFromPositioned(FGPositioned* aPos, int aIndex);
230   void setScratchFromCachedSearchResult();
231   void setScratchFromRouteWaypoint(int aIndex);
232   
233   /**
234    * Add airport-specific information to a scratch result
235    */
236   void addAirportToScratch(FGAirport* aAirport);
237   
238   void clearScratch();
239   
240   /**
241    * Predicate, determine if the lon/lat position in the scratch is 
242    * valid or not.
243    */
244   bool isScratchPositionValid() const;
245   
246   FGPositioned::Filter* createFilter(FGPositioned::Type aTy);
247   
248   /**
249    * Search kernel - called each time we step through a result
250    */
251   void performSearch();
252   
253 // command handlers
254   void selectLegMode();
255   void selectOBSMode();
256   void directTo();
257   void loadRouteWaypoint();
258   void loadNearest();
259   void search();
260   void nextResult();
261   void previousResult();
262   void defineWaypoint();
263   void insertWaypointAtIndex(int aIndex);
264   void removeWaypointAtIndex(int aIndex);
265   
266 // tied-property getter/setters
267   void setCommand(const char* aCmd);
268   const char* getCommand() const { return ""; }
269   
270   const char* getMode() const { return _mode.c_str(); }
271   
272   bool getScratchValid() const { return _scratchValid; }
273   double getScratchDistance() const;
274   double getScratchMagBearing() const;
275   double getScratchTrueBearing() const;
276   bool getScratchHasNext() const { return _searchHasNext; }
277   
278   double getSelectedCourse() const { return _selectedCourse; }
279   double getCDIDeflection() const;
280   
281   double getLegDistance() const;
282   double getLegCourse() const;
283   double getLegMagCourse() const;
284   double getAltDistanceRatio() const;
285   
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; }
290   
291   //bool getLegMode() const { return _mode == "leg"; }
292   //bool getObsMode() const { return _mode == "obs"; }
293   
294   const char* getWP0Ident() const;
295   const char* getWP0Name() const;
296   
297   const char* getWP1Ident() const;
298   const char* getWP1Name() const;
299   
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;
309   
310   // true-bearing-error and mag-bearing-error
311   
312
313   /**
314    * Tied-properties helper, record nodes which are tied for easy un-tie-ing
315    */
316   template <typename T>
317   void tie(SGPropertyNode* aNode, const char* aRelPath, const SGRawValue<T>& aRawValue)
318   {
319     SGPropertyNode* nd = aNode->getNode(aRelPath, true);
320     _tiedNodes.push_back(nd);
321     nd->tie(aRawValue);
322   }
323
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);
327   
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);
331
332 // members
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;
339
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;
344     
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;
351     
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;
358
359     double _selectedCourse;
360     
361     bool _dataValid;
362     SGGeod _last_pos;
363     bool _lastPosValid;
364     double _last_speed_kts;
365     double _last_true_track;
366     double _last_vertical_speed;
367     
368     std::string _mode;
369     GPSListener* _listener;
370     Config _config;
371     FGRouteMgr* _routeMgr;
372     
373     bool _ref_navaid_set;
374     double _ref_navaid_elapsed;
375     FGPositionedRef _ref_navaid;
376     
377     std::string _name;
378     int _num;
379   
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;
386   
387 // scratch data
388   SGGeod _scratchPos;
389   SGPropertyNode_ptr _scratchNode;
390   bool _scratchValid;
391   
392 // search data
393   int _searchResultIndex;
394   std::string _searchQuery;
395   FGPositioned::Type _searchType;
396   bool _searchExact;
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
403   
404   // turn data
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
412     SGGeod _turnPt;
413     SGGeod _turnCentre;
414   
415   SGPropertyNode_ptr _realismSimpleGps; ///< should the GPS be simple or realistic?
416   
417 // autopilot drive properties
418   SGPropertyNode_ptr _apTrueHeading;
419   SGPropertyNode_ptr _apTargetAltitudeFt;
420   SGPropertyNode_ptr _apAltitudeLock;
421   
422   std::vector<SGPropertyNode_ptr> _tiedNodes;
423 };
424
425
426 #endif // __INSTRUMENTS_GPS_HXX