AP_GPS_NMEA.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. //
  14. // NMEA parser, adapted by Michael Smith from TinyGPS v9:
  15. //
  16. // TinyGPS - a small GPS library for Arduino providing basic NMEA parsing
  17. // Copyright (C) 2008-9 Mikal Hart
  18. // All rights reserved.
  19. //
  20. //
  21. /// @file AP_GPS_NMEA.h
  22. /// @brief NMEA protocol parser
  23. ///
  24. /// This is a lightweight NMEA parser, derived originally from the
  25. /// TinyGPS parser by Mikal Hart. It is frugal in its use of memory
  26. /// and tries to avoid unnecessary arithmetic.
  27. ///
  28. /// The parser handles GPGGA, GPRMC and GPVTG messages, and attempts to be
  29. /// robust in the face of occasional corruption in the input stream. It
  30. /// makes a basic effort to configure GPS' that are likely to be connected in
  31. /// NMEA mode (SiRF, MediaTek and ublox) to emit the correct message
  32. /// stream, but does not validate that the correct stream is being received.
  33. /// In particular, a unit emitting just GPRMC will show as having a fix
  34. /// even though no altitude data is being received.
  35. ///
  36. /// GPVTG data is parsed, but as the message may not contain the the
  37. /// qualifier field (this is common with e.g. older SiRF units) it is
  38. /// not considered a source of fix-valid information.
  39. ///
  40. #pragma once
  41. #include "AP_GPS.h"
  42. #include "GPS_Backend.h"
  43. /// NMEA parser
  44. ///
  45. class AP_GPS_NMEA : public AP_GPS_Backend
  46. {
  47. friend class AP_GPS_NMEA_Test;
  48. public:
  49. using AP_GPS_Backend::AP_GPS_Backend;
  50. /// Checks the serial receive buffer for characters,
  51. /// attempts to parse NMEA data and updates internal state
  52. /// accordingly.
  53. bool read() override;
  54. static bool _detect(struct NMEA_detect_state &state, uint8_t data);
  55. const char *name() const override { return "NMEA"; }
  56. private:
  57. /// Coding for the GPS sentences that the parser handles
  58. enum _sentence_types { //there are some more than 10 fields in some sentences , thus we have to increase these value.
  59. _GPS_SENTENCE_RMC = 32,
  60. _GPS_SENTENCE_GGA = 64,
  61. _GPS_SENTENCE_VTG = 96,
  62. _GPS_SENTENCE_HDT = 128,
  63. _GPS_SENTENCE_OTHER = 0
  64. };
  65. /// Update the decode state machine with a new character
  66. ///
  67. /// @param c The next character in the NMEA input stream
  68. /// @returns True if processing the character has resulted in
  69. /// an update to the GPS state
  70. ///
  71. bool _decode(char c);
  72. /// Parses the @p as a NMEA-style decimal number with
  73. /// up to 3 decimal digits.
  74. ///
  75. /// @returns The value expressed by the string in @p,
  76. /// multiplied by 100.
  77. ///
  78. static int32_t _parse_decimal_100(const char *p);
  79. /// Parses the current term as a NMEA-style degrees + minutes
  80. /// value with up to four decimal digits.
  81. ///
  82. /// This gives a theoretical resolution limit of around 1cm.
  83. ///
  84. /// @returns The value expressed by the string in _term,
  85. /// multiplied by 1e7.
  86. ///
  87. uint32_t _parse_degrees();
  88. /// Processes the current term when it has been deemed to be
  89. /// complete.
  90. ///
  91. /// Each GPS message is broken up into terms separated by commas.
  92. /// Each term is then processed by this function as it is received.
  93. ///
  94. /// @returns True if completing the term has resulted in
  95. /// an update to the GPS state.
  96. bool _term_complete();
  97. /// return true if we have a new set of NMEA messages
  98. bool _have_new_message(void);
  99. uint8_t _parity; ///< NMEA message checksum accumulator
  100. bool _is_checksum_term; ///< current term is the checksum
  101. char _term[15]; ///< buffer for the current term within the current sentence
  102. uint8_t _sentence_type; ///< the sentence type currently being processed
  103. uint8_t _term_number; ///< term index within the current sentence
  104. uint8_t _term_offset; ///< character offset with the term being received
  105. uint16_t _sentence_length;
  106. bool _gps_data_good; ///< set when the sentence indicates data is good
  107. // The result of parsing terms within a message is stored temporarily until
  108. // the message is completely processed and the checksum validated.
  109. // This avoids the need to buffer the entire message.
  110. int32_t _new_time; ///< time parsed from a term
  111. int32_t _new_date; ///< date parsed from a term
  112. int32_t _new_latitude; ///< latitude parsed from a term
  113. int32_t _new_longitude; ///< longitude parsed from a term
  114. int32_t _new_altitude; ///< altitude parsed from a term
  115. int32_t _new_speed; ///< speed parsed from a term
  116. int32_t _new_course; ///< course parsed from a term
  117. float _new_gps_yaw; ///< yaw parsed from a term
  118. uint16_t _new_hdop; ///< HDOP parsed from a term
  119. uint8_t _new_satellite_count; ///< satellite count parsed from a term
  120. uint8_t _new_quality_indicator; ///< GPS quality indicator parsed from a term
  121. uint32_t _last_RMC_ms = 0;
  122. uint32_t _last_GGA_ms = 0;
  123. uint32_t _last_VTG_ms = 0;
  124. uint32_t _last_HDT_ms = 0;
  125. /// @name Init strings
  126. /// In ::init, an attempt is made to configure the GPS
  127. /// unit to send just the messages that we are interested
  128. /// in using these strings
  129. //@{
  130. static const char _SiRF_init_string[]; ///< init string for SiRF units
  131. static const char _MTK_init_string[]; ///< init string for MediaTek units
  132. static const char _ublox_init_string[]; ///< init string for ublox units
  133. //@}
  134. static const char _initialisation_blob[];
  135. };
  136. #define AP_GPS_NMEA_HEMISPHERE_INIT_STRING \
  137. "$JATT,NMEAHE,0\r\n" /* Prefix of GP on the HDT message */ \
  138. "$JASC,GPGGA,5\r\n" /* GGA at 5Hz */ \
  139. "$JASC,GPRMC,5\r\n" /* RMC at 5Hz */ \
  140. "$JASC,GPVTG,5\r\n" /* VTG at 5Hz */ \
  141. "$JASC,GPHDT,5\r\n" /* HDT at 5Hz */ \
  142. "$JMODE,SBASR,YES\r\n" /* Enable SBAS */