Apollo 10.0
自动驾驶开放平台
forsense_text_parser.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2024 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 std::string GPYJ = "$GPYJ";
38 size_t GPYJ_SIZE = 24;
39};
40
42 public:
43 explicit ForsenseTextParser(const config::Config &config)
44 : ForsenseBaseParser(config) {}
45 bool PrepareMessage() override;
46
47 private:
48 void PrepareMessageGPYJ(const std::vector<std::string> &fields);
49
50 ForsenseProtocol protocol_;
51 std::string input_str_;
52};
53
55 return new ForsenseTextParser(config);
56}
57
59 ADEBUG << "Forsense ASCII is: " << data_;
60
61 // message may be truncated, just save the last message
62 const uint8_t *data_start = data_end_;
63 for (; data_start != data_; --data_start) {
64 if (*data_start == '$') {
65 break;
66 }
67 }
68 if (data_start != data_) {
69 AWARN << "Forsense message has been truncated: " << data_;
70 }
71
72 if (*data_start != '$') {
73 input_str_.append(reinterpret_cast<const char *>(data_start),
74 std::distance(data_start, data_end_));
75 } else {
76 input_str_.assign(reinterpret_cast<const char *>(data_start),
77 std::distance(data_start, data_end_));
78 }
79
80 if (*(data_end_ - 1) != 0x0A) {
81 AWARN << "Forsense ASCII message is not complete: " << data_start;
82 return false;
83 }
84
85 std::vector<std::string> fields;
86 std::stringstream ss(input_str_);
87 for (std::string field; std::getline(ss, field, ',');) {
88 fields.push_back(field);
89 }
90 if (fields.empty()) {
91 return false;
92 }
93 if (fields[0] == protocol_.GPYJ) {
94 if (fields.size() < protocol_.GPYJ_SIZE) {
95 AERROR << "GPYJ message format error: " << data_start;
96 return false;
97 }
98 PrepareMessageGPYJ(fields);
99 return true;
100 }
101 return false;
102}
103
104void ForsenseTextParser::PrepareMessageGPYJ(
105 const std::vector<std::string> &fields) {
106 decode_message_.messageID = fields[0];
107 decode_message_.GPSWeek = std::stoi(fields[1]);
108 decode_message_.GPSTime = std::stod(fields[2]);
111 decode_message_.Heading = std::stod(fields[3]);
112 decode_message_.Pitch = std::stod(fields[4]);
113 decode_message_.Roll = std::stod(fields[5]);
114 decode_message_.GyroX = std::stod(fields[6]);
115 decode_message_.GyroY = std::stod(fields[7]);
116 decode_message_.GyroZ = std::stod(fields[8]);
117 decode_message_.AccX = std::stod(fields[9]);
118 decode_message_.AccY = std::stod(fields[10]);
119 decode_message_.AccZ = std::stod(fields[11]);
120 decode_message_.Latitude = std::stod(fields[12].empty() ? "0" : fields[12]);
121 decode_message_.Longitude = std::stod(fields[13].empty() ? "0" : fields[13]);
122 decode_message_.Altitude = std::stod(fields[14].empty() ? "0" : fields[14]);
123 decode_message_.Ve = std::stod(fields[15]);
124 decode_message_.Vn = std::stod(fields[16]);
125 decode_message_.Vu = std::stod(fields[17]);
126 decode_message_.V = std::stod(fields[18]);
127 decode_message_.NSV1 = std::stoi(fields[19]);
128 decode_message_.NSV2 = std::stoi(fields[20]);
129 int status = std::stoi(fields[21]);
130 PrepareMessageStatus(status % 10, status / 10);
131 decode_message_.Age = std::stod(fields[22]);
132 decode_message_.WarningCs = fields[23];
133}
134
135
136
137} // namespace gnss
138} // namespace drivers
139} // namespace apollo
void PrepareMessageStatus(const uint8_t &system_state, const uint8_t &satellite_status)
ForsenseTextParser(const config::Config &config)
static Parser * CreateForsenseText(const config::Config &config)
const uint8_t * data_
Definition parser.h:138
const uint8_t * data_end_
Definition parser.h:139
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AWARN
Definition log.h:43
constexpr int SECONDS_PER_WEEK
class register implement
Definition arena_queue.h:37