gprmc.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2017, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL SOUTHWEST RESEARCH INSTITUTE BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 
31 #include <boost/make_shared.hpp>
33 
34 const std::string novatel_gps_driver::GprmcParser::MESSAGE_NAME = "GPRMC";
35 
37 {
38  return 0;
39 }
40 
42 {
43  return MESSAGE_NAME;
44 }
45 
47 {
48  // Check the length first; should be 13 elements long for OEM6 & 7,
49  // but only 12 elements for OEM4.
50  const size_t EXPECTED_LEN_OEM6 = 13;
51  const size_t EXPECTED_LEN_OEM4 = 12;
52 
53  if (sentence.body.size() != EXPECTED_LEN_OEM4 &&
54  sentence.body.size() != EXPECTED_LEN_OEM6)
55  {
56  std::stringstream error;
57  error << "Expected GPRMC lengths = "
58  << EXPECTED_LEN_OEM4 << " (for OEM4), "
59  << EXPECTED_LEN_OEM6 << " (for OEM6), "
60  << "actual length = " << sentence.body.size();
61  throw ParseException(error.str());
62  }
63 
64  bool success = true;
65  novatel_gps_msgs::GprmcPtr msg = boost::make_shared<novatel_gps_msgs::Gprmc>();
66  msg->message_id = sentence.body[0];
67 
68  if (sentence.body[1].empty() || sentence.body[1] == "0")
69  {
70  msg->utc_seconds = 0;
71  }
72  else
73  {
74  double utc_float;
75  if (swri_string_util::ToDouble(sentence.body[1], utc_float))
76  {
77  msg->utc_seconds = UtcFloatToSeconds(utc_float);
78  }
79  else
80  {
81  throw ParseException("Error parsing UTC seconds in GPRMC log.");
82  }
83  }
84 
85  msg->position_status = sentence.body[2];
86  // Check to see whether this message is listed as valid
87  success &= (sentence.body[2].compare("A") == 0);
88  success &= !(sentence.body[3].empty() || sentence.body[5].empty());
89 
90  bool valid = true;
91 
92  double latitude = 0.0;
93  valid = valid && ParseDouble(sentence.body[3], latitude);
94  msg->lat = ConvertDmsToDegrees(latitude);
95 
96  double longitude = 0.0;
97  valid = valid && ParseDouble(sentence.body[5], longitude);
98  msg->lon = ConvertDmsToDegrees(longitude);
99 
100  msg->lat_dir = sentence.body[4];
101  msg->lon_dir = sentence.body[6];
102 
103  valid = valid && ParseFloat(sentence.body[7], msg->speed);
104  msg->speed *= KNOTS_TO_MPS;
105 
106  valid = valid && ParseFloat(sentence.body[8], msg->track);
107 
108  std::string date_str = sentence.body[9];
109  if (!date_str.empty())
110  {
111  msg->date = std::string("20") + date_str.substr(4, 2) +
112  std::string("-") + date_str.substr(2, 2) +
113  std::string("-") + date_str.substr(0, 2);
114  }
115  valid = valid && ParseFloat(sentence.body[10], msg->mag_var);
116  msg->mag_var_direction = sentence.body[11];
117  if (sentence.body.size() == EXPECTED_LEN_OEM6) {
118  msg->mode_indicator = sentence.body[12];
119  }
120 
121  if (!valid)
122  {
123  was_last_gps_valid_ = false;
124  throw ParseException("Error parsing GPRMC message.");
125  }
126 
127  was_last_gps_valid_ = success;
128 
129  return msg;
130 }
131 
133 {
134  return was_last_gps_valid_;
135 }
msg
static constexpr double KNOTS_TO_MPS
Definition: gprmc.h:54
bool WasLastGpsValid() const
Definition: gprmc.cpp:132
const std::string GetMessageName() const override
Definition: gprmc.cpp:41
double ParseDouble(const uint8_t *buffer)
Converts a buffer containing 8 bytes into a double.
novatel_gps_msgs::GprmcPtr ParseAscii(const NmeaSentence &sentence) override
Converts sentence into a ROS message pointer and returns it.
Definition: gprmc.cpp:46
float ParseFloat(const uint8_t *buffer)
Converts a buffer containing 4 bytes into a float.
uint32_t GetMessageId() const override
Definition: gprmc.cpp:36
static const std::string MESSAGE_NAME
Definition: gprmc.h:53
double UtcFloatToSeconds(double utc_float)
double ConvertDmsToDegrees(double dms)
bool ToDouble(const std::string &string, double &value)


novatel_gps_driver
Author(s):
autogenerated on Wed Jul 3 2019 19:36:46