ROSaic
Public Member Functions | Static Public Attributes | Private Attributes
GprmcParser Class Reference

Derived class for parsing RMC messages. More...

#include <gprmc.hpp>

Inheritance diagram for GprmcParser:
Inheritance graph
[legend]
Collaboration diagram for GprmcParser:
Collaboration graph
[legend]

Public Member Functions

 GprmcParser ()
 Constructor of the class GprmcParser. More...
 
const std::string getMessageID () const override
 Returns the ASCII message ID, here "$GPRMC". More...
 
rosaic::GprmcPtr parseASCII (const NMEASentence &sentence) noexcept(false) override
 Parses one RMC message. More...
 
bool wasLastGPRMCValid () const
 Tells us whether the last RMC message was valid/usable or not. More...
 
- Public Member Functions inherited from BaseParser< rosaic::GprmcPtr >
 BaseParser ()=default
 Default constructor of the class BaseParser. More...
 
virtual ~BaseParser ()=default
 Default destructor of the class BaseParser. More...
 
rosaic::GprmcPtr parseBinary (const SBFStructT &bin_msg) noexcept(false)
 Converts bin_msg into a ROS message pointer (e.g. nmea_msgs::GpggaPtr) and returns it. More...
 

Static Public Attributes

static const std::string MESSAGE_ID = "$GPRMC"
 Declares the string MESSAGE_ID. More...
 
static constexpr double KNOTS_TO_MPS = 0.5144444
 

Private Attributes

bool was_last_gprmc_valid_
 Declares a boolean representing whether or not the last GPRMC message was valid. More...
 

Detailed Description

Derived class for parsing RMC messages.

Date
28/09/20

Definition at line 83 of file gprmc.hpp.

Constructor & Destructor Documentation

◆ GprmcParser()

GprmcParser::GprmcParser ( )
inline

Constructor of the class GprmcParser.

Definition at line 90 of file gprmc.hpp.

References getMessageID(), parseASCII(), and wasLastGPRMCValid().

bool was_last_gprmc_valid_
Declares a boolean representing whether or not the last GPRMC message was valid.
Definition: gprmc.hpp:126
Here is the call graph for this function:

Member Function Documentation

◆ getMessageID()

const std::string GprmcParser::getMessageID ( ) const
overridevirtual

Returns the ASCII message ID, here "$GPRMC".

Returns
The message ID

Implements BaseParser< rosaic::GprmcPtr >.

Definition at line 41 of file gprmc.cpp.

References MESSAGE_ID.

Referenced by GprmcParser().

42 {
44 }
static const std::string MESSAGE_ID
Declares the string MESSAGE_ID.
Definition: gprmc.hpp:116
Here is the caller graph for this function:

◆ parseASCII()

rosaic::GprmcPtr GprmcParser::parseASCII ( const NMEASentence sentence)
overridevirtualnoexcept

Parses one RMC message.

Parameters
[in]sentenceThe RMC message to be parsed
Returns
A ROS message pointer of ROS type rosaic::GprmcPtr

Caution: Due to the occurrence of the throw keyword, this method ParseASCII should be called within a try / catch framework... Note: This method is called from within the read() method of the RxMessage class by including the checksum part in the argument "sentence" here, though the checksum is never parsed: It would be sentence.get_body()[13] if anybody ever needs it. The status character can be 'A' (for Active) or 'V' (for Void), signaling whether the GPS was active when the positioning was made. If it is void, the GPS could not make a good positioning and you should thus ignore it. This usually occurs when the GPS is still searching for satellites. WasLastGPRMCValid() will return false in this case.

Reimplemented from BaseParser< rosaic::GprmcPtr >.

Definition at line 54 of file gprmc.cpp.

References parsing_utilities::convertDMSToDegrees(), parsing_utilities::convertUTCDoubleToSeconds(), parsing_utilities::convertUTCtoUnix(), g_frame_id, g_use_gnss_time, KNOTS_TO_MPS, parsing_utilities::parseDouble(), parsing_utilities::parseFloat(), string_utilities::toDouble(), and was_last_gprmc_valid_.

Referenced by GprmcParser(), and io_comm_rx::RxMessage::read().

55 {
56 
57  // Checking the length first, it should be between 13 and 14 elements
58  const size_t LEN_MIN = 13;
59  const size_t LEN_MAX = 14;
60 
61  if (sentence.get_body().size() > LEN_MAX ||
62  sentence.get_body().size() < LEN_MIN)
63  {
64  std::stringstream error;
65  error << "Expected GPRMC length is between " << LEN_MIN << " and "
66  << LEN_MAX << ". The actual length is " << sentence.get_body().size();
67  throw ParseException(error.str());
68  }
69 
70  rosaic::GprmcPtr msg = boost::make_shared<rosaic::Gprmc>();
71 
72  msg->header.frame_id = g_frame_id;
73 
74  msg->message_id = sentence.get_body()[0];
75 
76  if (sentence.get_body()[1].empty() || sentence.get_body()[1] == "0")
77  {
78  msg->utc_seconds = 0;
79  }
80  else
81  {
82  double utc_double;
83  if (string_utilities::toDouble(sentence.get_body()[1], utc_double))
84  {
85  msg->utc_seconds = parsing_utilities::convertUTCDoubleToSeconds(utc_double);
86  if(g_use_gnss_time)
87  {
88  // The Header's Unix Epoch time stamp
89  time_t unix_time_seconds = parsing_utilities::convertUTCtoUnix(utc_double);
90  // The following assumes that there are two digits after the decimal point in utc_double, i.e. in the NMEA UTC time.
91  uint32_t unix_time_nanoseconds = (static_cast<uint32_t>(utc_double*100)%100)*10000;
92  msg->header.stamp.sec = unix_time_seconds;
93  msg->header.stamp.nsec = unix_time_nanoseconds;
94  }
95  else
96  {
97  ros::Time time_obj;
98  time_obj = ros::Time::now();
99  msg->header.stamp.sec = time_obj.sec;
100  msg->header.stamp.nsec = time_obj.nsec;
101  }
102  }
103  else
104  {
105  throw ParseException("Error parsing UTC seconds in GPRMC"); // E.g. if one of the fields of the NMEA UTC string is empty
106  }
107  }
108  bool valid = true;
109  bool to_be_ignored = false;
110 
111  msg->position_status = sentence.get_body()[2];
112  // Check to see whether this message should be ignored
113  to_be_ignored &= !(sentence.get_body()[2].compare("A") == 0); // 0 : if both strings are equal.
114  to_be_ignored &= (sentence.get_body()[3].empty() || sentence.get_body()[5].empty());
115 
116  double latitude = 0.0;
117  valid = valid && parsing_utilities::parseDouble(sentence.get_body()[3], latitude);
118  msg->lat = parsing_utilities::convertDMSToDegrees(latitude);
119 
120  double longitude = 0.0;
121  valid = valid && parsing_utilities::parseDouble(sentence.get_body()[5], longitude);
122  msg->lon = parsing_utilities::convertDMSToDegrees(longitude);
123 
124  msg->lat_dir = sentence.get_body()[4];
125  msg->lon_dir = sentence.get_body()[6];
126 
127  valid = valid && parsing_utilities::parseFloat(sentence.get_body()[7], msg->speed);
128  msg->speed *= KNOTS_TO_MPS;
129 
130  valid = valid && parsing_utilities::parseFloat(sentence.get_body()[8], msg->track);
131 
132  std::string date_str = sentence.get_body()[9];
133  if (!date_str.empty())
134  {
135  msg->date = std::string("20") + date_str.substr(4, 2) +
136  std::string("-") + date_str.substr(2, 2) +
137  std::string("-") + date_str.substr(0, 2);
138  }
139  valid = valid && parsing_utilities::parseFloat(sentence.get_body()[10], msg->mag_var);
140  msg->mag_var_direction = sentence.get_body()[11];
141  if (sentence.get_body().size() == LEN_MAX)
142  {
143  msg->mode_indicator = sentence.get_body()[12];
144  }
145 
146  if (!valid)
147  {
148  was_last_gprmc_valid_ = false;
149  throw ParseException("Error parsing GPRMC message.");
150  }
151 
152  was_last_gprmc_valid_ = !to_be_ignored;
153 
154  return msg;
155 }
std::string g_frame_id
The frame ID used in the header of every published ROS message.
bool toDouble(const std::string &string, double &value)
Interprets the contents of "string" as a floating point number of type double It stores the "string"&#39;...
float parseFloat(const uint8_t *buffer)
Converts a 4-byte-buffer into a float.
std::vector< std::string > get_body() const
double convertUTCDoubleToSeconds(double utc_double)
Converts UTC time from the without-colon-delimiter format to the number-of-seconds-since-midnight for...
static constexpr double KNOTS_TO_MPS
Definition: gprmc.hpp:120
double convertDMSToDegrees(double dms)
Converts latitude or longitude from the DMS notation (in the without-colon-delimiter format)...
bool was_last_gprmc_valid_
Declares a boolean representing whether or not the last GPRMC message was valid.
Definition: gprmc.hpp:126
time_t convertUTCtoUnix(double utc_double)
Converts UTC time from the without-colon-delimiter format to Unix Epoch time (a number-of-seconds-sin...
Class to declare error message format when parsing, derived from the public class "std::runtime_error...
bool g_use_gnss_time
double parseDouble(const uint8_t *buffer)
Converts an 8-byte-buffer into a double.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ wasLastGPRMCValid()

bool GprmcParser::wasLastGPRMCValid ( ) const

Tells us whether the last RMC message was valid/usable or not.

Returns
True if last RMC message was valid, false if not

E.g. if the status is V=Void, then this function will also return false.

Definition at line 158 of file gprmc.cpp.

References was_last_gprmc_valid_.

Referenced by GprmcParser().

159 {
160  return was_last_gprmc_valid_;
161 }
bool was_last_gprmc_valid_
Declares a boolean representing whether or not the last GPRMC message was valid.
Definition: gprmc.hpp:126
Here is the caller graph for this function:

Field Documentation

◆ KNOTS_TO_MPS

constexpr double GprmcParser::KNOTS_TO_MPS = 0.5144444
static

1 kt = 0.51444444444 mps (meters per second) Note that constexpr is needed for in-class initialization of static data members.

Definition at line 120 of file gprmc.hpp.

Referenced by parseASCII().

◆ MESSAGE_ID

const std::string GprmcParser::MESSAGE_ID = "$GPRMC"
static

Declares the string MESSAGE_ID.

Definition at line 116 of file gprmc.hpp.

Referenced by getMessageID().

◆ was_last_gprmc_valid_

bool GprmcParser::was_last_gprmc_valid_
private

Declares a boolean representing whether or not the last GPRMC message was valid.

Definition at line 126 of file gprmc.hpp.

Referenced by parseASCII(), and wasLastGPRMCValid().


The documentation for this class was generated from the following files: