Apollo 10.0
自动驾驶开放平台
parser_cli.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 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// A command-line interface (CLI) tool of parser. It parses a binary log file.
18// It is supposed to be
19// used for verifying if the parser works properly.
20
21#include <fstream>
22#include <iostream>
23#include <memory>
24
25#include "modules/drivers/gnss/proto/config.pb.h"
26
27#include "cyber/cyber.h"
28#include "cyber/init.h"
32
33namespace apollo {
34namespace drivers {
35namespace gnss {
36
37constexpr size_t BUFFER_SIZE = 128;
38
39void ParseBin(const char* filename, DataParser* parser) {
40 std::ios::sync_with_stdio(false);
41 std::ifstream f(filename, std::ifstream::binary);
42 char b[BUFFER_SIZE];
43 while (f && cyber::OK()) {
44 f.read(b, BUFFER_SIZE);
45 std::string msg(reinterpret_cast<const char*>(b), f.gcount());
46 parser->ParseRawData(msg);
47 }
48}
49
50void ParseRecord(const char* filename, DataParser* parser) {
51 cyber::record::RecordReader reader(filename);
53 while (reader.ReadMessage(&message) && cyber::OK()) {
54 if (message.channel_name == "/apollo/sensor/gnss/raw_data") {
56 msg.ParseFromString(message.content);
57 parser->ParseRawData(msg.data());
58 }
59 std::this_thread::sleep_for(std::chrono::milliseconds(2));
60 }
61}
62
63void Parse(const char* filename, const char* file_type,
64 const std::shared_ptr<::apollo::cyber::Node>& node) {
65 std::string type = std::string(file_type);
66 config::Config config;
68 std::string("modules/drivers/gnss/conf/gnss_conf.pb.txt"),
69 &config)) {
70 std::cout << "Unable to load gnss conf file";
71 }
72 DataParser* parser = new DataParser(config, node);
73 parser->Init();
74 if (type == "bin") {
75 ParseBin(filename, parser);
76 } else if (type == "record") {
77 ParseRecord(filename, parser);
78 } else {
79 std::cout << "unknown file type";
80 }
81 delete parser;
82}
83
84} // namespace gnss
85} // namespace drivers
86} // namespace apollo
87
88int main(int argc, char** argv) {
89 if (argc < 3) {
90 std::cout << "Usage: " << argv[0] << " filename [record|bin]" << std::endl;
91 return 0;
92 }
93 ::apollo::cyber::Init("parser_cli");
94 std::shared_ptr<::apollo::cyber::Node> parser_node(
95 ::apollo::cyber::CreateNode("parser_cli"));
96 ::apollo::drivers::gnss::Parse(argv[1], argv[2], parser_node);
97 return 0;
98}
double f
bool ReadMessage(RecordMessage *message, uint64_t begin_time=0, uint64_t end_time=std::numeric_limits< uint64_t >::max())
Read one message from reader.
void ParseRawData(const std::string &msg)
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
bool Init(const char *binary_name, const std::string &dag_info)
Definition init.cc:98
bool OK()
Definition state.h:44
std::unique_ptr< Node > CreateNode(const std::string &node_name, const std::string &name_space)
Definition cyber.cc:33
constexpr size_t BUFFER_SIZE
void ParseRecord(const char *filename, DataParser *parser)
Definition parser_cli.cc:50
void Parse(const char *filename, const char *file_type, const std::shared_ptr<::apollo::cyber::Node > &node)
Definition parser_cli.cc:63
void ParseBin(const char *filename, DataParser *parser)
Definition parser_cli.cc:39
class register implement
Definition arena_queue.h:37
int main(int argc, char **argv)
Definition parser_cli.cc:88
Basic data struct of record message.
std::string content
The content of the message.
std::string channel_name
The channel name of the message.