Apollo 11.0
自动驾驶开放平台
broadgnss_text_parser.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17// An parser for decoding binary messages from a NovAtel receiver. The following
18// messages must be
19// logged in order for this parser to work properly.
20//
21#include <cmath>
22#include <limits>
23#include <memory>
24#include <sstream>
25#include <string>
26#include <vector>
27
28#include "cyber/cyber.h"
31
32namespace apollo {
33namespace drivers {
34namespace gnss {
35
37 public:
38 explicit BroadGnssTextParser(const config::Config &config)
39 : BroadGnssBaseParser(config) {}
40 bool PrepareMessage(const uint8_t &channel) override;
41
42 private:
43 void PrepareMessageCommon(const std::vector<std::string> &fields);
44 void PrepareMessageGIAVP(const std::vector<std::string> &fields);
45 void PrepareMessageGPINS(const std::vector<std::string> &fields);
46 void PrepareMessageBESTPOSA(const std::vector<std::string> &fields);
47 void PrepareMessageHEADINGA(const std::vector<std::string> &fields);
48
49 std::string input_str_;
50 ASCIIParser ascii_parser_;
51};
52
54 return new BroadGnssTextParser(config);
55}
56
57bool BroadGnssTextParser::PrepareMessage(const uint8_t &channel) {
58 ADEBUG << "BroadGnss ASCII is: " << data_;
59
60 ascii_parser_.Add(data_, data_end_, channel);
61 auto &message = ascii_parser_.GetMessage();
62 if (message.empty()) {
63 return false;
64 }
65
66 std::stringstream ss(message.substr(0, message.rfind('*')));
67 std::vector<std::string> fields;
68 for (std::string field; std::getline(ss, field, ',');) {
69 fields.push_back(field);
70 }
71 if (fields.empty()) {
72 return false;
73 }
74 auto valid_fields = [&]() -> bool {
75 for (size_t i = 1; i < fields.size(); ++i) {
76 if (fields[i].empty()) {
77 fields[i] = "0";
78 } else if (fields[i].find_first_not_of("0123456789.- ") !=
79 std::string::npos ||
80 fields[i].find_first_of("0123456789") == std::string::npos) {
81 AERROR << "BroadGnss ASCII message field error: " << fields[i]
82 << ", input str: " << message;
83 return false;
84 }
85 }
86 return true;
87 };
89 if (fields[0] == GIAVP_MESSAGE_ID) {
90 if (fields.size() != GIAVP_MESSAGE_SIZE) {
91 AERROR << "BroadGnss GIAVP message format error: " << message;
92 return false;
93 }
94 if (!valid_fields()) {
95 return false;
96 }
97 PrepareMessageGIAVP(fields);
98 return true;
99 } else if (fields[0] == GPINS_MESSAGE_ID) {
100 if (fields.size() != GPINS_MESSAGE_SIZE) {
101 AERROR << "BroadGnss GPINS message format error:" << message;
102 return false;
103 }
104 if (!valid_fields()) {
105 return false;
106 }
107 PrepareMessageGPINS(fields);
108 return true;
109 } else if (fields[0] == BESTPOSA_MESSAGE_ID) {
110 if (fields.size() != BESTPOSA_MESSAGE_SIZE) {
111 AERROR << "BroadGnss BESTPOSA message format error: " << message;
112 return false;
113 }
114 PrepareMessageBESTPOSA(fields);
115 return true;
116 } else if (fields[0] == HEADINGA_MESSAGE_ID) {
117 if (fields.size() != HEADINGA_MESSAGE_SIZE) {
118 AERROR << "BroadGnss HEADINGA message format error: " << message;
119 return false;
120 }
121 PrepareMessageHEADINGA(fields);
122 return true;
123 }
124 return false;
125}
126
127void BroadGnssTextParser::PrepareMessageCommon(
128 const std::vector<std::string> &fields) {
130 std::stoi(fields[1]) * SECONDS_PER_WEEK + std::stod(fields[2]);
131 broadgnss_message_.heading = std::stod(fields[3]);
132 broadgnss_message_.pitch = std::stod(fields[4]);
133 broadgnss_message_.roll = std::stod(fields[5]);
134 broadgnss_message_.latitude = std::stod(fields[6]);
135 broadgnss_message_.longitude = std::stod(fields[7]);
136 broadgnss_message_.altitude = std::stod(fields[8]);
137 broadgnss_message_.ve = std::stod(fields[9]);
138 broadgnss_message_.vn = std::stod(fields[10]);
139 broadgnss_message_.vu = std::stod(fields[11]);
140 broadgnss_message_.satellites_num = std::stoi(fields[13]);
141}
142
143void BroadGnssTextParser::PrepareMessageGIAVP(
144 const std::vector<std::string> &fields) {
145 PrepareMessageCommon(fields);
146 int solution_type = std::stoi(fields[15]);
147 int solution_status = 0;
148 if (solution_type >= 4) {
149 solution_status = 4;
150 }
151 PrepareMessageStatus(solution_status, solution_type);
152 broadgnss_message_.acc_x = std::stod(fields[18]);
153 broadgnss_message_.acc_y = std::stod(fields[19]);
154 broadgnss_message_.acc_z = std::stod(fields[20]);
155 broadgnss_message_.gyro_x = std::stod(fields[21]) * DEG_TO_RAD;
156 broadgnss_message_.gyro_y = std::stod(fields[22]) * DEG_TO_RAD;
157 broadgnss_message_.gyro_z = std::stod(fields[23]) * DEG_TO_RAD;
158}
159
160void BroadGnssTextParser::PrepareMessageGPINS(
161 const std::vector<std::string> &fields) {
162 PrepareMessageCommon(fields);
163 PrepareMessageStatus(std::stoi(fields[17]), std::stoi(fields[15]));
164 broadgnss_message_.acc_x = std::stod(fields[21]);
165 broadgnss_message_.acc_y = std::stod(fields[22]);
166 broadgnss_message_.acc_z = std::stod(fields[23]);
167 broadgnss_message_.gyro_x = std::stod(fields[24]) * DEG_TO_RAD;
168 broadgnss_message_.gyro_y = std::stod(fields[25]) * DEG_TO_RAD;
169 broadgnss_message_.gyro_z = std::stod(fields[26]) * DEG_TO_RAD;
170}
171
172void BroadGnssTextParser::PrepareMessageBESTPOSA(
173 const std::vector<std::string> &fields) {
174 broadgnss_message_.best_latitude = std::stod(fields[11]);
175 broadgnss_message_.best_longitude = std::stod(fields[12]);
176 broadgnss_message_.best_altitude = std::stof(fields[13]);
177 broadgnss_message_.undulation = std::stof(fields[14]);
178 broadgnss_message_.lat_std = std::stof(fields[16]);
179 broadgnss_message_.lon_std = std::stof(fields[17]);
180 broadgnss_message_.alti_std = std::stof(fields[18]);
181 broadgnss_message_.differential_age = std::stof(fields[20]);
182 broadgnss_message_.solution_age = std::stof(fields[21]);
183}
184
185void BroadGnssTextParser::PrepareMessageHEADINGA(
186 const std::vector<std::string> &fields) {
187 broadgnss_message_.gps_heading = std::stof(fields[12]);
188 broadgnss_message_.gps_pitch = std::stof(fields[13]);
189 broadgnss_message_.yaw_std = std::stof(fields[15]);
190 broadgnss_message_.pitch_std = std::stof(fields[16]);
191}
192
193} // namespace gnss
194} // namespace drivers
195} // namespace apollo
void Add(const uint8_t *data_start, const uint8_t *data_end, const uint8_t channel=0)
void PrepareMessageStatus(const uint8_t &solution_status, const uint8_t &solution_type)
BroadGnssTextParser(const config::Config &config)
bool PrepareMessage(const uint8_t &channel) override
const uint8_t * data_
Definition parser.h:145
const uint8_t * data_end_
Definition parser.h:146
static Parser * CreateBroadGnssText(const config::Config &config)
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
constexpr uint32_t GIAVP_MESSAGE_SIZE
constexpr uint32_t BESTPOSA_MESSAGE_SIZE
constexpr char GPINS_MESSAGE_ID[]
constexpr double DEG_TO_RAD
constexpr uint32_t HEADINGA_MESSAGE_SIZE
constexpr char BESTPOSA_MESSAGE_ID[]
constexpr uint32_t GPINS_MESSAGE_SIZE
constexpr int SECONDS_PER_WEEK
constexpr char GIAVP_MESSAGE_ID[]
constexpr char HEADINGA_MESSAGE_ID[]
class register implement
Definition arena_queue.h:37