Apollo 10.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() 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
47 BroadGnssTextProtocol protocol_;
48 std::string input_str_;
49};
50
52 return new BroadGnssTextParser(config);
53}
54
56 ADEBUG << "BroadGnss ASCII is: " << data_;
57
58 // message may be truncated, just save the last message
59 const uint8_t *data_start = data_end_;
60 for (; data_start != data_; --data_start) {
61 if (*data_start == '$') {
62 break;
63 }
64 }
65 if (data_start != data_) {
66 AINFO << "BroadGnss message has been truncated: " << data_;
67 }
68
69 if (*data_start != '$') {
70 input_str_.append(reinterpret_cast<const char *>(data_start),
71 std::distance(data_start, data_end_));
72 } else {
73 input_str_.assign(reinterpret_cast<const char *>(data_start),
74 std::distance(data_start, data_end_));
75 }
76
77 if (*(data_end_ - 1) != 0x0A) {
78 AINFO << "BroadGnss ASCII message is not complete: " << data_start;
79 return false;
80 } else {
81 ADEBUG << "BroadGnss ASCII message is complete";
82 }
83
84 // find cs pos
85 std::stringstream ss(input_str_.substr(0, input_str_.rfind('*')));
86 std::vector<std::string> fields;
87 for (std::string field; std::getline(ss, field, ',');) {
88 fields.push_back(field);
89 }
90 if (fields.empty()) {
91 return false;
92 }
93 auto valid_fields = [&]() -> bool {
94 for (size_t i = 1; i < fields.size(); ++i) {
95 if (fields[i].empty()) {
96 fields[i] = "0";
97 } else if (fields[i].find_first_not_of("0123456789.- ") !=
98 std::string::npos) {
99 AERROR << "BroadGnss ASCII message field error: " << fields[i]
100 << ", input str: " << input_str_;
101 return false;
102 }
103 }
104 return true;
105 };
106 if (fields[0] == protocol_.GIAVP) {
107 if (fields.size() != protocol_.GIAVP_SIZE) {
108 AERROR << "BroadGnss GIAVP message format error: " << data_;
109 return false;
110 }
111 if (!valid_fields()) {
112 return false;
113 }
114 PrepareMessageGIAVP(fields);
115 return true;
116 } else if (fields[0] == protocol_.GPINS) {
117 if (fields.size() != protocol_.GPINS_SIZE) {
118 AERROR << "BroadGnss GPINS message format error:" << data_;
119 return false;
120 }
121 if (!valid_fields()) {
122 return false;
123 }
124 PrepareMessageGPINS(fields);
125 return true;
126 }
127 return false;
128}
129
130void BroadGnssTextParser::PrepareMessageCommon(
131 const std::vector<std::string> &fields) {
133 std::stoi(fields[1]) * SECONDS_PER_WEEK + std::stod(fields[2]);
134 broadgnss_message_.heading = std::stod(fields[3]);
135 broadgnss_message_.pitch = std::stod(fields[4]);
136 broadgnss_message_.roll = std::stod(fields[5]);
137 broadgnss_message_.latitude = std::stod(fields[6]);
138 broadgnss_message_.longitude = std::stod(fields[7]);
139 broadgnss_message_.altitude = std::stod(fields[8]);
140 broadgnss_message_.ve = std::stod(fields[9]);
141 broadgnss_message_.vn = std::stod(fields[10]);
142 broadgnss_message_.vu = std::stod(fields[11]);
143 broadgnss_message_.satellites_num = std::stoi(fields[13]);
144}
145
146void BroadGnssTextParser::PrepareMessageGIAVP(
147 const std::vector<std::string> &fields) {
148 PrepareMessageCommon(fields);
149 int solution_type = std::stoi(fields[15]);
150 int solution_status = 0;
151 if (solution_type >= 4) {
152 solution_status = 4;
153 }
154 PrepareMessageStatus(solution_status, solution_type);
155 broadgnss_message_.acc_x = std::stod(fields[18]);
156 broadgnss_message_.acc_y = std::stod(fields[19]);
157 broadgnss_message_.acc_z = std::stod(fields[20]);
158 broadgnss_message_.gyro_x = std::stod(fields[21]) * DEG_TO_RAD;
159 broadgnss_message_.gyro_y = std::stod(fields[22]) * DEG_TO_RAD;
160 broadgnss_message_.gyro_z = std::stod(fields[23]) * DEG_TO_RAD;
161}
162
163void BroadGnssTextParser::PrepareMessageGPINS(
164 const std::vector<std::string> &fields) {
165 PrepareMessageCommon(fields);
166 PrepareMessageStatus(std::stoi(fields[17]), std::stoi(fields[15]));
167 broadgnss_message_.acc_x = std::stod(fields[21]);
168 broadgnss_message_.acc_y = std::stod(fields[22]);
169 broadgnss_message_.acc_z = std::stod(fields[23]);
170 broadgnss_message_.gyro_x = std::stod(fields[24]) * DEG_TO_RAD;
171 broadgnss_message_.gyro_y = std::stod(fields[25]) * DEG_TO_RAD;
172 broadgnss_message_.gyro_z = std::stod(fields[26]) * DEG_TO_RAD;
173}
174
175} // namespace gnss
176} // namespace drivers
177} // namespace apollo
void PrepareMessageStatus(const uint8_t &solution_status, const uint8_t &solution_type)
BroadGnssTextParser(const config::Config &config)
const uint8_t * data_
Definition parser.h:138
const uint8_t * data_end_
Definition parser.h:139
static Parser * CreateBroadGnssText(const config::Config &config)
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
constexpr double DEG_TO_RAD
constexpr int SECONDS_PER_WEEK
class register implement
Definition arena_queue.h:37