gprmc.cpp
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2017, Southwest Research Institute® (SwRI®)
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //     * Redistributions of source code must retain the above copyright
00009 //       notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above copyright
00011 //       notice, this list of conditions and the following disclaimer in the
00012 //       documentation and/or other materials provided with the distribution.
00013 //     * Neither the name of Southwest Research Institute® (SwRI®) nor the
00014 //       names of its contributors may be used to endorse or promote products
00015 //       derived from this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 // ARE DISCLAIMED. IN NO EVENT SHALL SOUTHWEST RESEARCH INSTITUTE BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //
00028 // *****************************************************************************
00029 
00030 #include <novatel_gps_driver/parsers/gprmc.h>
00031 #include <boost/make_shared.hpp>
00032 #include <swri_string_util/string_util.h>
00033 
00034 const std::string novatel_gps_driver::GprmcParser::MESSAGE_NAME = "GPRMC";
00035 
00036 uint32_t novatel_gps_driver::GprmcParser::GetMessageId() const
00037 {
00038   return 0;
00039 }
00040 
00041 const std::string novatel_gps_driver::GprmcParser::GetMessageName() const
00042 {
00043   return MESSAGE_NAME;
00044 }
00045 
00046 novatel_gps_msgs::GprmcPtr novatel_gps_driver::GprmcParser::ParseAscii(const novatel_gps_driver::NmeaSentence& sentence) throw(ParseException)
00047 {
00048   // Check the length first; should be 13 elements long for OEM6 & 7,
00049   // but only 12 elements for OEM4.
00050   const size_t EXPECTED_LEN_OEM6 = 13;
00051   const size_t EXPECTED_LEN_OEM4 = 12;
00052 
00053   if (sentence.body.size() != EXPECTED_LEN_OEM4 &&
00054       sentence.body.size() != EXPECTED_LEN_OEM6)
00055   {
00056     std::stringstream error;
00057     error << "Expected GPRMC lengths = "
00058           << EXPECTED_LEN_OEM4 << " (for OEM4), "
00059           << EXPECTED_LEN_OEM6 << " (for OEM6), "
00060           << "actual length = " << sentence.body.size();
00061     throw ParseException(error.str());
00062   }
00063 
00064   bool success = true;
00065   novatel_gps_msgs::GprmcPtr msg = boost::make_shared<novatel_gps_msgs::Gprmc>();
00066   msg->message_id = sentence.body[0];
00067 
00068   if (sentence.body[1].empty() || sentence.body[1] == "0")
00069   {
00070     msg->utc_seconds = 0;
00071   }
00072   else
00073   {
00074     double utc_float;
00075     if (swri_string_util::ToDouble(sentence.body[1], utc_float))
00076     {
00077       msg->utc_seconds = UtcFloatToSeconds(utc_float);
00078     }
00079     else
00080     {
00081       throw ParseException("Error parsing UTC seconds in GPRMC log.");
00082     }
00083   }
00084 
00085   msg->position_status = sentence.body[2];
00086   // Check to see whether this message is listed as valid
00087   success &= (sentence.body[2].compare("A") == 0);
00088   success &= !(sentence.body[3].empty() || sentence.body[5].empty());
00089 
00090   bool valid = true;
00091 
00092   double latitude = 0.0;
00093   valid = valid && ParseDouble(sentence.body[3], latitude);
00094   msg->lat = ConvertDmsToDegrees(latitude);
00095 
00096   double longitude = 0.0;
00097   valid = valid && ParseDouble(sentence.body[5], longitude);
00098   msg->lon = ConvertDmsToDegrees(longitude);
00099 
00100   msg->lat_dir = sentence.body[4];
00101   msg->lon_dir = sentence.body[6];
00102 
00103   valid = valid && ParseFloat(sentence.body[7], msg->speed);
00104   msg->speed *= KNOTS_TO_MPS;
00105 
00106   valid = valid && ParseFloat(sentence.body[8], msg->track);
00107 
00108   std::string date_str = sentence.body[9];
00109   if (!date_str.empty())
00110   {
00111     msg->date = std::string("20") + date_str.substr(4, 2) +
00112                 std::string("-") + date_str.substr(2, 2) +
00113                 std::string("-") + date_str.substr(0, 2);
00114   }
00115   valid = valid && ParseFloat(sentence.body[10], msg->mag_var);
00116   msg->mag_var_direction = sentence.body[11];
00117   if (sentence.body.size() == EXPECTED_LEN_OEM6) {
00118     msg->mode_indicator = sentence.body[12];
00119   }
00120 
00121   if (!valid)
00122   {
00123     was_last_gps_valid_ = false;
00124     throw ParseException("Error parsing GPRMC message.");
00125   }
00126 
00127   was_last_gps_valid_ = success;
00128 
00129   return msg;
00130 }
00131 
00132 bool novatel_gps_driver::GprmcParser::WasLastGpsValid() const
00133 {
00134   return was_last_gps_valid_;
00135 }


novatel_gps_driver
Author(s):
autogenerated on Wed Jul 3 2019 19:40:37