PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/flightgear-2.6.0/src/Instrumentation/gps.hxx

#
C++ Header | 437 lines | 255 code | 84 blank | 98 comment | 0 complexity | cf20f570301567c1ddc362f7bb4a755b MD5 | raw file
Possible License(s): GPL-2.0
  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. #ifndef __INSTRUMENTS_GPS_HXX
  6. #define __INSTRUMENTS_GPS_HXX 1
  7. #include <cassert>
  8. #include <memory>
  9. #include <simgear/props/props.hxx>
  10. #include <simgear/structure/subsystem_mgr.hxx>
  11. #include <simgear/math/SGMath.hxx>
  12. #include <Navaids/positioned.hxx>
  13. #include <Instrumentation/rnav_waypt_controller.hxx>
  14. // forward decls
  15. class SGRoute;
  16. class FGRouteMgr;
  17. class FGAirport;
  18. class GPSListener;
  19. class SGGeodProperty
  20. {
  21. public:
  22. SGGeodProperty()
  23. {
  24. }
  25. void init(SGPropertyNode* base, const char* lonStr, const char* latStr, const char* altStr = NULL);
  26. void init(const char* lonStr, const char* latStr, const char* altStr = NULL);
  27. void clear();
  28. void operator=(const SGGeod& geod);
  29. SGGeod get() const;
  30. private:
  31. SGPropertyNode_ptr _lon, _lat, _alt;
  32. };
  33. /**
  34. * Model a GPS radio.
  35. *
  36. * Input properties:
  37. *
  38. * /position/longitude-deg
  39. * /position/latitude-deg
  40. * /position/altitude-ft
  41. * /environment/magnetic-variation-deg
  42. * /systems/electrical/outputs/gps
  43. * /instrumentation/gps/serviceable
  44. *
  45. *
  46. * Output properties:
  47. *
  48. * /instrumentation/gps/indicated-longitude-deg
  49. * /instrumentation/gps/indicated-latitude-deg
  50. * /instrumentation/gps/indicated-altitude-ft
  51. * /instrumentation/gps/indicated-vertical-speed-fpm
  52. * /instrumentation/gps/indicated-track-true-deg
  53. * /instrumentation/gps/indicated-track-magnetic-deg
  54. * /instrumentation/gps/indicated-ground-speed-kt
  55. *
  56. * /instrumentation/gps/wp-distance-nm
  57. * /instrumentation/gps/wp-bearing-deg
  58. * /instrumentation/gps/wp-bearing-mag-deg
  59. * /instrumentation/gps/TTW
  60. * /instrumentation/gps/course-deviation-deg
  61. * /instrumentation/gps/course-error-nm
  62. * /instrumentation/gps/to-flag
  63. * /instrumentation/gps/odometer
  64. * /instrumentation/gps/trip-odometer
  65. * /instrumentation/gps/true-bug-error-deg
  66. * /instrumentation/gps/magnetic-bug-error-deg
  67. */
  68. class GPS : public SGSubsystem, public flightgear::RNAV
  69. {
  70. public:
  71. GPS (SGPropertyNode *node);
  72. GPS ();
  73. virtual ~GPS ();
  74. // SGSubsystem interface
  75. virtual void init ();
  76. virtual void update (double delta_time_sec);
  77. virtual void bind();
  78. virtual void unbind();
  79. // RNAV interface
  80. virtual SGGeod position();
  81. virtual double trackDeg();
  82. virtual double groundSpeedKts();
  83. virtual double vspeedFPM();
  84. virtual double magvarDeg();
  85. virtual double selectedMagCourse();
  86. virtual double overflightArmDistanceM();
  87. private:
  88. friend class GPSListener;
  89. friend class SearchFilter;
  90. /**
  91. * Configuration manager, track data relating to aircraft installation
  92. */
  93. class Config
  94. {
  95. public:
  96. Config();
  97. void bind(GPS* aOwner, SGPropertyNode* aCfg);
  98. bool turnAnticipationEnabled() const
  99. { return _enableTurnAnticipation; }
  100. /**
  101. * Desired turn rate in degrees/second. From this we derive the turn
  102. * radius and hence how early we need to anticipate it.
  103. */
  104. double turnRateDegSec() const
  105. { return _turnRate; }
  106. /**
  107. * Distance at which we arm overflight sequencing. Once inside this
  108. * distance, a change of the wp1 'TO' flag to false will be considered
  109. * overlight of the wp.
  110. */
  111. double overflightArmDistanceNm() const
  112. { return _overflightArmDistance; }
  113. /**
  114. * Time before the next WP to activate an external annunciator
  115. */
  116. double waypointAlertTime() const
  117. { return _waypointAlertTime; }
  118. bool requireHardSurface() const
  119. { return _requireHardSurface; }
  120. double minRunwayLengthFt() const
  121. { return _minRunwayLengthFt; }
  122. bool cdiDeflectionIsAngular() const
  123. { return (_cdiMaxDeflectionNm <= 0.0); }
  124. double cdiDeflectionLinearPeg() const
  125. {
  126. assert(_cdiMaxDeflectionNm > 0.0);
  127. return _cdiMaxDeflectionNm;
  128. }
  129. bool driveAutopilot() const
  130. { return _driveAutopilot; }
  131. bool courseSelectable() const
  132. { return _courseSelectable; }
  133. private:
  134. bool _enableTurnAnticipation;
  135. // desired turn rate in degrees per second
  136. double _turnRate;
  137. // distance from waypoint to arm overflight sequencing (in nm)
  138. double _overflightArmDistance;
  139. // time before reaching a waypoint to trigger annunicator light/sound
  140. // (in seconds)
  141. double _waypointAlertTime;
  142. // minimum runway length to require when filtering
  143. double _minRunwayLengthFt;
  144. // should we require a hard-surfaced runway when filtering?
  145. bool _requireHardSurface;
  146. double _cdiMaxDeflectionNm;
  147. // should we drive the autopilot directly or not?
  148. bool _driveAutopilot;
  149. // is selected-course-deg read to set desired-course or not?
  150. bool _courseSelectable;
  151. };
  152. class SearchFilter : public FGPositioned::Filter
  153. {
  154. public:
  155. virtual bool pass(FGPositioned* aPos) const;
  156. virtual FGPositioned::Type minType() const;
  157. virtual FGPositioned::Type maxType() const;
  158. };
  159. /**
  160. * reset all output properties to default / non-service values
  161. */
  162. void clearOutput();
  163. void updateBasicData(double dt);
  164. void updateTrackingBug();
  165. void updateReferenceNavaid(double dt);
  166. void referenceNavaidSet(const std::string& aNavaid);
  167. void updateRouteData();
  168. void driveAutopilot();
  169. void routeActivated();
  170. void routeManagerSequenced();
  171. void routeEdited();
  172. void routeFinished();
  173. void updateTurn();
  174. void updateOverflight();
  175. void beginTurn();
  176. void endTurn();
  177. double computeTurnProgress(double aBearing) const;
  178. void computeTurnData();
  179. void updateTurnData();
  180. double computeTurnRadiusNm(double aGroundSpeedKts) const;
  181. /**
  182. * Update one-shot things when WP1 / leg data change
  183. */
  184. void wp1Changed();
  185. // scratch maintenence utilities
  186. void setScratchFromPositioned(FGPositioned* aPos, int aIndex);
  187. void setScratchFromCachedSearchResult();
  188. void setScratchFromRouteWaypoint(int aIndex);
  189. /**
  190. * Add airport-specific information to a scratch result
  191. */
  192. void addAirportToScratch(FGAirport* aAirport);
  193. void clearScratch();
  194. /**
  195. * Predicate, determine if the lon/lat position in the scratch is
  196. * valid or not.
  197. */
  198. bool isScratchPositionValid() const;
  199. FGPositioned::Filter* createFilter(FGPositioned::Type aTy);
  200. /**
  201. * Search kernel - called each time we step through a result
  202. */
  203. void performSearch();
  204. // command handlers
  205. void selectLegMode();
  206. void selectOBSMode();
  207. void directTo();
  208. void loadRouteWaypoint();
  209. void loadNearest();
  210. void search();
  211. void nextResult();
  212. void previousResult();
  213. void defineWaypoint();
  214. void insertWaypointAtIndex(int aIndex);
  215. void removeWaypointAtIndex(int aIndex);
  216. // tied-property getter/setters
  217. void setCommand(const char* aCmd);
  218. const char* getCommand() const { return ""; }
  219. const char* getMode() const { return _mode.c_str(); }
  220. bool getScratchValid() const { return _scratchValid; }
  221. double getScratchDistance() const;
  222. double getScratchMagBearing() const;
  223. double getScratchTrueBearing() const;
  224. bool getScratchHasNext() const;
  225. double getSelectedCourse() const { return _selectedCourse; }
  226. void setSelectedCourse(double crs);
  227. double getDesiredCourse() const { return _desiredCourse; }
  228. double getCDIDeflection() const;
  229. double getLegDistance() const;
  230. double getLegCourse() const;
  231. double getLegMagCourse() const;
  232. double getTrueTrack() const { return _last_true_track; }
  233. double getMagTrack() const;
  234. double getGroundspeedKts() const { return _last_speed_kts; }
  235. double getVerticalSpeed() const { return _last_vertical_speed; }
  236. //bool getLegMode() const { return _mode == "leg"; }
  237. //bool getObsMode() const { return _mode == "obs"; }
  238. const char* getWP0Ident() const;
  239. const char* getWP0Name() const;
  240. const char* getWP1Ident() const;
  241. const char* getWP1Name() const;
  242. double getWP1Distance() const;
  243. double getWP1TTW() const;
  244. const char* getWP1TTWString() const;
  245. double getWP1Bearing() const;
  246. double getWP1MagBearing() const;
  247. double getWP1CourseDeviation() const;
  248. double getWP1CourseErrorNm() const;
  249. bool getWP1ToFlag() const;
  250. bool getWP1FromFlag() const;
  251. // true-bearing-error and mag-bearing-error
  252. /**
  253. * Tied-properties helper, record nodes which are tied for easy un-tie-ing
  254. */
  255. template <typename T>
  256. void tie(SGPropertyNode* aNode, const char* aRelPath, const SGRawValue<T>& aRawValue)
  257. {
  258. SGPropertyNode* nd = aNode->getNode(aRelPath, true);
  259. _tiedNodes.push_back(nd);
  260. nd->tie(aRawValue);
  261. }
  262. /// helper, tie the lat/lon/elev of a SGGeod to the named children of aNode
  263. void tieSGGeod(SGPropertyNode* aNode, SGGeod& aRef,
  264. const char* lonStr, const char* latStr, const char* altStr);
  265. /// helper, tie a SGGeod to proeprties, but read-only
  266. void tieSGGeodReadOnly(SGPropertyNode* aNode, SGGeod& aRef,
  267. const char* lonStr, const char* latStr, const char* altStr);
  268. // members
  269. SGPropertyNode_ptr _gpsNode;
  270. SGPropertyNode_ptr _currentWayptNode;
  271. SGPropertyNode_ptr _magvar_node;
  272. SGPropertyNode_ptr _serviceable_node;
  273. SGPropertyNode_ptr _electrical_node;
  274. SGPropertyNode_ptr _tracking_bug_node;
  275. SGPropertyNode_ptr _raim_node;
  276. SGPropertyNode_ptr _odometer_node;
  277. SGPropertyNode_ptr _trip_odometer_node;
  278. SGPropertyNode_ptr _true_bug_error_node;
  279. SGPropertyNode_ptr _magnetic_bug_error_node;
  280. SGPropertyNode_ptr _eastWestVelocity;
  281. SGPropertyNode_ptr _northSouthVelocity;
  282. SGPropertyNode_ptr _ref_navaid_id_node;
  283. SGPropertyNode_ptr _ref_navaid_bearing_node;
  284. SGPropertyNode_ptr _ref_navaid_distance_node;
  285. SGPropertyNode_ptr _ref_navaid_mag_bearing_node;
  286. SGPropertyNode_ptr _ref_navaid_frequency_node;
  287. SGPropertyNode_ptr _ref_navaid_name_node;
  288. SGPropertyNode_ptr _route_active_node;
  289. SGPropertyNode_ptr _route_current_wp_node;
  290. SGPropertyNode_ptr _routeDistanceNm;
  291. SGPropertyNode_ptr _routeETE;
  292. SGPropertyNode_ptr _routeEditedSignal;
  293. SGPropertyNode_ptr _routeFinishedSignal;
  294. SGPropertyNode_ptr _desiredCourseNode;
  295. double _selectedCourse;
  296. double _desiredCourse;
  297. bool _dataValid;
  298. SGGeod _last_pos;
  299. bool _lastPosValid;
  300. double _last_speed_kts;
  301. double _last_true_track;
  302. double _last_vertical_speed;
  303. double _lastEWVelocity;
  304. double _lastNSVelocity;
  305. std::string _mode;
  306. GPSListener* _listener;
  307. Config _config;
  308. FGRouteMgr* _routeMgr;
  309. bool _ref_navaid_set;
  310. double _ref_navaid_elapsed;
  311. FGPositionedRef _ref_navaid;
  312. std::string _name;
  313. int _num;
  314. SGGeodProperty _position;
  315. SGGeod _wp0_position;
  316. SGGeod _indicated_pos;
  317. double _legDistanceNm;
  318. // scratch data
  319. SGGeod _scratchPos;
  320. SGPropertyNode_ptr _scratchNode;
  321. bool _scratchValid;
  322. // search data
  323. int _searchResultIndex;
  324. std::string _searchQuery;
  325. FGPositioned::Type _searchType;
  326. bool _searchExact;
  327. FGPositioned::List _searchResults;
  328. bool _searchIsRoute; ///< set if 'search' is actually the current route
  329. bool _searchHasNext; ///< is there a result after this one?
  330. bool _searchNames; ///< set if we're searching names instead of idents
  331. // turn data
  332. bool _computeTurnData; ///< do we need to update the turn data?
  333. bool _anticipateTurn; ///< are we anticipating the next turn or not?
  334. bool _inTurn; // is a turn in progress?
  335. bool _turnSequenced; // have we sequenced the new leg?
  336. double _turnAngle; // angle to turn through, in degrees
  337. double _turnStartBearing; // bearing of inbound leg
  338. double _turnRadius; // radius of turn in nm
  339. SGGeod _turnPt;
  340. SGGeod _turnCentre;
  341. std::auto_ptr<flightgear::WayptController> _wayptController;
  342. SGPropertyNode_ptr _realismSimpleGps; ///< should the GPS be simple or realistic?
  343. flightgear::WayptRef _prevWaypt;
  344. flightgear::WayptRef _currentWaypt;
  345. // autopilot drive properties
  346. SGPropertyNode_ptr _apDrivingFlag;
  347. SGPropertyNode_ptr _apTrueHeading;
  348. SGPropertyNode_ptr _apTargetAltitudeFt;
  349. SGPropertyNode_ptr _apAltitudeLock;
  350. std::vector<SGPropertyNode_ptr> _tiedNodes;
  351. };
  352. #endif // __INSTRUMENTS_GPS_HXX