heading2.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 
33 
34 #include <boost/make_shared.hpp>
35 
36 namespace novatel_gps_driver
37 {
38  const std::string Heading2Parser::MESSAGE_NAME = "HEADING2";
39 
41  {
42  return MESSAGE_ID;
43  }
44 
45  const std::string Heading2Parser::GetMessageName() const
46  {
47  return MESSAGE_NAME;
48  }
49 
50  novatel_gps_msgs::NovatelHeading2Ptr Heading2Parser::ParseBinary(const BinaryMessage& bin_msg) noexcept(false)
51  {
52  if (bin_msg.data_.size() != BINARY_LENGTH)
53  {
54  std::stringstream error;
55  error << "Unexpected HEADING2 message length: " << bin_msg.data_.size();
56  throw ParseException(error.str());
57  }
58  novatel_gps_msgs::NovatelHeading2Ptr ros_msg =
59  boost::make_shared<novatel_gps_msgs::NovatelHeading2>();
60  HeaderParser header_parser;
61  ros_msg->novatel_msg_header = header_parser.ParseBinary(bin_msg);
62  ros_msg->novatel_msg_header.message_name = MESSAGE_NAME;
63 
64  uint16_t solution_status = ParseUInt16(&bin_msg.data_[0]);
65  if (solution_status > MAX_SOLUTION_STATUS)
66  {
67  std::stringstream error;
68  error << "Unknown solution status: " << solution_status;
69  throw ParseException(error.str());
70  }
71  ros_msg->solution_status = SOLUTION_STATUSES[solution_status];
72 
73  uint16_t pos_type = ParseUInt16(&bin_msg.data_[4]);
74  if (pos_type > MAX_POSITION_TYPE)
75  {
76  std::stringstream error;
77  error << "Unknown position type: " << pos_type;
78  throw ParseException(error.str());
79  }
80  ros_msg->position_type = POSITION_TYPES[pos_type];
81 
82  ros_msg->baseline_length = ParseFloat(&bin_msg.data_[8]);
83 
84  ros_msg->heading = ParseFloat(&bin_msg.data_[12]);
85  ros_msg->pitch = ParseFloat(&bin_msg.data_[16]);
86 
87  // Bytes 20-23 reserved
88 
89  ros_msg->heading_sigma = ParseFloat(&bin_msg.data_[24]);
90  ros_msg->pitch_sigma = ParseFloat(&bin_msg.data_[28]);
91 
92  ros_msg->rover_station_id.resize(4);
93  std::copy(&bin_msg.data_[32], &bin_msg.data_[36], &ros_msg->rover_station_id[0]);
94 
95  ros_msg->master_station_id.resize(4);
96  std::copy(&bin_msg.data_[36], &bin_msg.data_[40], &ros_msg->master_station_id[0]);
97 
98  ros_msg->num_satellites_tracked = bin_msg.data_[40];
99  ros_msg->num_satellites_used_in_solution = bin_msg.data_[41];
100  ros_msg->num_satellites_above_elevation_mask_angle = bin_msg.data_[42];
101  ros_msg->num_satellites_above_elevation_mask_angle_l2 = bin_msg.data_[43];
102 
103  ros_msg->solution_source = SolutionSourceToMsgEnum(bin_msg.data_[44]);
104 
105  GetExtendedSolutionStatusMessage(bin_msg.data_[45],
106  ros_msg->extended_solution_status);
107 
108  // Byte 46 is reserved
109 
110  GetSignalsUsed(bin_msg.data_[47], ros_msg->signal_mask);
111 
112  return ros_msg;
113  }
114 
115  novatel_gps_msgs::NovatelHeading2Ptr Heading2Parser::ParseAscii(const NovatelSentence& sentence) noexcept(false)
116  {
117  novatel_gps_msgs::NovatelHeading2Ptr ros_msg =
118  boost::make_shared<novatel_gps_msgs::NovatelHeading2>();
119  HeaderParser h_parser;
120  ros_msg->novatel_msg_header = h_parser.ParseAscii(sentence);
121 
122  if (sentence.body.size() != ASCII_LENGTH)
123  {
124  std::stringstream error;
125  error << "Unexpected number of HEADING2 message fields: " << sentence.body.size();
126  throw ParseException(error.str());
127  }
128 
129  bool valid = true;
130 
131  ros_msg->solution_status = sentence.body[0];
132  ros_msg->position_type = sentence.body[1];
133 
134  valid = valid && ParseFloat(sentence.body[2], ros_msg->baseline_length);
135 
136  valid = valid && ParseFloat(sentence.body[3], ros_msg->heading);
137  valid = valid && ParseFloat(sentence.body[4], ros_msg->pitch);
138 
139  // Skip reserved field
140 
141  valid = valid && ParseFloat(sentence.body[6], ros_msg->heading_sigma);
142  valid = valid && ParseFloat(sentence.body[7], ros_msg->pitch_sigma);
143 
144  ros_msg->rover_station_id = sentence.body[8];
145 
146  ros_msg->master_station_id = sentence.body[9];
147 
148  valid = valid && ParseUInt8(sentence.body[10], ros_msg->num_satellites_tracked);
149  valid = valid && ParseUInt8(sentence.body[11], ros_msg->num_satellites_used_in_solution);
150  valid = valid && ParseUInt8(sentence.body[12], ros_msg->num_satellites_above_elevation_mask_angle);
151  valid = valid && ParseUInt8(sentence.body[13], ros_msg->num_satellites_above_elevation_mask_angle_l2);
152 
153  uint32_t solution_source = 0;
154  valid = valid && ParseUInt32(sentence.body[14], solution_source, 16);
155  ros_msg->solution_source = SolutionSourceToMsgEnum((uint8_t)solution_source);
156 
157  uint32_t extended_solution_status = 0;
158  valid = valid && ParseUInt32(sentence.body[15], extended_solution_status, 16);
160  extended_solution_status, ros_msg->extended_solution_status);
161 
162  // Skip reserved field
163 
164  uint32_t signal_mask = 0;
165  valid = valid && ParseUInt32(sentence.body[17], signal_mask, 16);
166  GetSignalsUsed(signal_mask, ros_msg->signal_mask);
167 
168  if (!valid)
169  {
170  throw ParseException("Invalid field in HEADING2 message");
171  }
172 
173  return ros_msg;
174  }
175 
176  uint8_t Heading2Parser::SolutionSourceToMsgEnum(uint8_t source_mask) noexcept(false)
177  {
178  uint8_t source_bits = (source_mask & 0x0Cu) >> 2u;
179  switch (source_bits)
180  {
181  case 0:
182  return novatel_gps_msgs::NovatelHeading2::SOURCE_PRIMARY_ANTENNA;
183  case 1:
184  return novatel_gps_msgs::NovatelHeading2::SOURCE_SECONDARY_ANTENNA;
185  default:
186  throw ParseException("HEADING2 Solution Source could not be parsed due to unknown source");
187  }
188  }
189 }
uint16_t ParseUInt16(const uint8_t *buffer)
Converts a buffer containing 2 bytes into an unsigned 16-bit int.
uint8_t SolutionSourceToMsgEnum(uint8_t source_mask) noexcept(false)
Definition: heading2.cpp:176
bool ParseUInt8(const std::string &string, uint8_t &value, int32_t base=10)
Parses a string containing an integer number into a uint16_t.
novatel_gps_msgs::NovatelHeading2Ptr ParseBinary(const BinaryMessage &bin_msg) noexcept(false) override
Converts bin_msg into a ROS message pointer and returns it.
Definition: heading2.cpp:50
const std::string GetMessageName() const override
Definition: heading2.cpp:45
static constexpr uint16_t MESSAGE_ID
Definition: heading2.h:51
static constexpr size_t BINARY_LENGTH
Definition: heading2.h:52
const size_t MAX_POSITION_TYPE
Definition: parsing_utils.h:56
novatel_gps_msgs::NovatelMessageHeader ParseAscii(const NovatelSentence &sentence) noexcept(false) override
Converts sentence into a ROS message pointer and returns it.
Definition: header.cpp:103
static constexpr size_t ASCII_LENGTH
Definition: heading2.h:53
const std::string POSITION_TYPES[]
Definition: parsing_utils.h:57
uint32_t ParseUInt32(const uint8_t *buffer)
Converts a buffer containing 4 bytes into an unsigned 32-bit int.
float ParseFloat(const uint8_t *buffer)
Converts a buffer containing 4 bytes into a float.
const std::string SOLUTION_STATUSES[]
Definition: parsing_utils.h:50
void GetExtendedSolutionStatusMessage(uint32_t status, novatel_gps_msgs::NovatelExtendedSolutionStatus &msg)
static const std::string MESSAGE_NAME
Definition: heading2.h:54
void GetSignalsUsed(uint32_t mask, novatel_gps_msgs::NovatelSignalMask &msg)
novatel_gps_msgs::NovatelMessageHeader ParseBinary(const BinaryMessage &bin_msg) noexcept(false) override
Converts bin_msg into a ROS message pointer and returns it.
Definition: header.cpp:44
const size_t MAX_SOLUTION_STATUS
Definition: parsing_utils.h:49
uint32_t GetMessageId() const override
Definition: heading2.cpp:40
novatel_gps_msgs::NovatelHeading2Ptr ParseAscii(const NovatelSentence &sentence) noexcept(false) override
Converts sentence into a ROS message pointer and returns it.
Definition: heading2.cpp:115


novatel_gps_driver
Author(s):
autogenerated on Thu Jul 16 2020 03:17:30