2013-08-29 02:34:34 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2011-01-09 21:55:45 -04:00
|
|
|
|
|
|
|
//
|
|
|
|
// NMEA parser, adapted by Michael Smith from TinyGPS v9:
|
|
|
|
//
|
|
|
|
// TinyGPS - a small GPS library for Arduino providing basic NMEA parsing
|
|
|
|
// Copyright (C) 2008-9 Mikal Hart
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
/// @file AP_GPS_NMEA.h
|
|
|
|
/// @brief NMEA protocol parser
|
|
|
|
///
|
|
|
|
/// This is a lightweight NMEA parser, derived originally from the
|
|
|
|
/// TinyGPS parser by Mikal Hart. It is frugal in its use of memory
|
|
|
|
/// and tries to avoid unnecessary arithmetic.
|
|
|
|
///
|
|
|
|
/// The parser handles GPGGA, GPRMC and GPVTG messages, and attempts to be
|
|
|
|
/// robust in the face of occasional corruption in the input stream. It
|
|
|
|
/// makes a basic effort to configure GPS' that are likely to be connected in
|
|
|
|
/// NMEA mode (SiRF, MediaTek and ublox) to emit the correct message
|
|
|
|
/// stream, but does not validate that the correct stream is being received.
|
|
|
|
/// In particular, a unit emitting just GPRMC will show as having a fix
|
|
|
|
/// even though no altitude data is being received.
|
|
|
|
///
|
|
|
|
/// GPVTG data is parsed, but as the message may not contain the the
|
|
|
|
/// qualifier field (this is common with e.g. older SiRF units) it is
|
|
|
|
/// not considered a source of fix-valid information.
|
|
|
|
///
|
2016-02-17 21:25:23 -04:00
|
|
|
#pragma once
|
2010-09-06 17:16:50 -03:00
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include "AP_GPS.h"
|
2016-04-13 14:20:05 -03:00
|
|
|
#include "GPS_Backend.h"
|
2011-01-09 21:55:45 -04:00
|
|
|
|
|
|
|
/// NMEA parser
|
|
|
|
///
|
2014-03-28 16:52:27 -03:00
|
|
|
class AP_GPS_NMEA : public AP_GPS_Backend
|
2011-01-09 21:55:45 -04:00
|
|
|
{
|
2016-05-19 14:17:11 -03:00
|
|
|
friend class AP_GPS_NMEA_Test;
|
|
|
|
|
2011-01-09 21:55:45 -04:00
|
|
|
public:
|
2014-03-28 16:52:27 -03:00
|
|
|
AP_GPS_NMEA(AP_GPS &_gps, AP_GPS::GPS_State &_state, AP_HAL::UARTDriver *_port);
|
2010-10-16 13:56:29 -03:00
|
|
|
|
2011-10-28 15:52:50 -03:00
|
|
|
/// Checks the serial receive buffer for characters,
|
|
|
|
/// attempts to parse NMEA data and updates internal state
|
|
|
|
/// accordingly.
|
2014-03-28 00:50:44 -03:00
|
|
|
bool read();
|
2010-10-16 13:56:29 -03:00
|
|
|
|
2014-03-28 16:52:27 -03:00
|
|
|
static bool _detect(struct NMEA_detect_state &state, uint8_t data);
|
2012-09-17 01:43:07 -03:00
|
|
|
|
2016-08-01 08:58:23 -03:00
|
|
|
const char *name() const override { return "NMEA"; }
|
|
|
|
|
2011-01-09 21:55:45 -04:00
|
|
|
private:
|
2011-10-28 15:52:50 -03:00
|
|
|
/// Coding for the GPS sentences that the parser handles
|
2012-08-21 23:19:51 -03:00
|
|
|
enum _sentence_types { //there are some more than 10 fields in some sentences , thus we have to increase these value.
|
2016-04-18 02:30:17 -03:00
|
|
|
_GPS_SENTENCE_RMC = 32,
|
|
|
|
_GPS_SENTENCE_GGA = 64,
|
|
|
|
_GPS_SENTENCE_VTG = 96,
|
2011-10-28 15:52:50 -03:00
|
|
|
_GPS_SENTENCE_OTHER = 0
|
2011-01-09 21:55:45 -04:00
|
|
|
};
|
2010-10-16 13:56:29 -03:00
|
|
|
|
2011-10-28 15:52:50 -03:00
|
|
|
/// Update the decode state machine with a new character
|
|
|
|
///
|
|
|
|
/// @param c The next character in the NMEA input stream
|
|
|
|
/// @returns True if processing the character has resulted in
|
|
|
|
/// an update to the GPS state
|
|
|
|
///
|
2012-08-21 23:19:51 -03:00
|
|
|
bool _decode(char c);
|
2011-10-28 15:52:50 -03:00
|
|
|
|
|
|
|
/// Return the numeric value of an ascii hex character
|
|
|
|
///
|
|
|
|
/// @param a The character to be converted
|
|
|
|
/// @returns The value of the character as a hex digit
|
|
|
|
///
|
2012-08-21 23:19:51 -03:00
|
|
|
int16_t _from_hex(char a);
|
2011-10-28 15:52:50 -03:00
|
|
|
|
2016-05-19 10:10:49 -03:00
|
|
|
/// Parses the @p as a NMEA-style decimal number with
|
|
|
|
/// up to 3 decimal digits.
|
2011-10-28 15:52:50 -03:00
|
|
|
///
|
2016-05-19 10:10:49 -03:00
|
|
|
/// @returns The value expressed by the string in @p,
|
2011-10-28 15:52:50 -03:00
|
|
|
/// multiplied by 100.
|
|
|
|
///
|
2016-05-19 10:10:49 -03:00
|
|
|
static int32_t _parse_decimal_100(const char *p);
|
2011-10-28 15:52:50 -03:00
|
|
|
|
|
|
|
/// Parses the current term as a NMEA-style degrees + minutes
|
|
|
|
/// value with up to four decimal digits.
|
|
|
|
///
|
2013-10-23 21:10:32 -03:00
|
|
|
/// This gives a theoretical resolution limit of around 1cm.
|
2011-10-28 15:52:50 -03:00
|
|
|
///
|
|
|
|
/// @returns The value expressed by the string in _term,
|
2013-10-23 21:10:32 -03:00
|
|
|
/// multiplied by 1e7.
|
2011-10-28 15:52:50 -03:00
|
|
|
///
|
2012-08-21 23:19:51 -03:00
|
|
|
uint32_t _parse_degrees();
|
2011-10-28 15:52:50 -03:00
|
|
|
|
|
|
|
/// Processes the current term when it has been deemed to be
|
|
|
|
/// complete.
|
|
|
|
///
|
|
|
|
/// Each GPS message is broken up into terms separated by commas.
|
|
|
|
/// Each term is then processed by this function as it is received.
|
|
|
|
///
|
|
|
|
/// @returns True if completing the term has resulted in
|
|
|
|
/// an update to the GPS state.
|
2012-08-21 23:19:51 -03:00
|
|
|
bool _term_complete();
|
2010-09-06 17:16:50 -03:00
|
|
|
|
2015-07-03 09:08:05 -03:00
|
|
|
/// return true if we have a new set of NMEA messages
|
|
|
|
bool _have_new_message(void);
|
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
uint8_t _parity; ///< NMEA message checksum accumulator
|
|
|
|
bool _is_checksum_term; ///< current term is the checksum
|
|
|
|
char _term[15]; ///< buffer for the current term within the current sentence
|
|
|
|
uint8_t _sentence_type; ///< the sentence type currently being processed
|
|
|
|
uint8_t _term_number; ///< term index within the current sentence
|
|
|
|
uint8_t _term_offset; ///< character offset with the term being received
|
|
|
|
bool _gps_data_good; ///< set when the sentence indicates data is good
|
2010-09-06 17:16:50 -03:00
|
|
|
|
2011-01-09 21:55:45 -04:00
|
|
|
// The result of parsing terms within a message is stored temporarily until
|
|
|
|
// the message is completely processed and the checksum validated.
|
|
|
|
// This avoids the need to buffer the entire message.
|
2012-08-21 23:19:51 -03:00
|
|
|
int32_t _new_time; ///< time parsed from a term
|
|
|
|
int32_t _new_date; ///< date parsed from a term
|
|
|
|
int32_t _new_latitude; ///< latitude parsed from a term
|
|
|
|
int32_t _new_longitude; ///< longitude parsed from a term
|
|
|
|
int32_t _new_altitude; ///< altitude parsed from a term
|
|
|
|
int32_t _new_speed; ///< speed parsed from a term
|
|
|
|
int32_t _new_course; ///< course parsed from a term
|
2014-04-01 17:49:29 -03:00
|
|
|
uint16_t _new_hdop; ///< HDOP parsed from a term
|
2012-08-21 23:19:51 -03:00
|
|
|
uint8_t _new_satellite_count; ///< satellite count parsed from a term
|
2010-09-06 17:16:50 -03:00
|
|
|
|
2016-04-18 02:30:17 -03:00
|
|
|
uint32_t _last_RMC_ms = 0;
|
|
|
|
uint32_t _last_GGA_ms = 0;
|
|
|
|
uint32_t _last_VTG_ms = 0;
|
2015-07-03 09:08:05 -03:00
|
|
|
|
2011-01-09 21:55:45 -04:00
|
|
|
/// @name Init strings
|
|
|
|
/// In ::init, an attempt is made to configure the GPS
|
|
|
|
/// unit to send just the messages that we are interested
|
|
|
|
/// in using these strings
|
|
|
|
//@{
|
2015-10-26 08:25:44 -03:00
|
|
|
static const char _SiRF_init_string[]; ///< init string for SiRF units
|
|
|
|
static const char _MTK_init_string[]; ///< init string for MediaTek units
|
|
|
|
static const char _ublox_init_string[]; ///< init string for ublox units
|
2011-01-09 21:55:45 -04:00
|
|
|
//@}
|
2010-09-06 17:16:50 -03:00
|
|
|
|
2015-10-26 08:25:44 -03:00
|
|
|
static const char _initialisation_blob[];
|
2010-09-06 17:16:50 -03:00
|
|
|
};
|