Apollo 11.0
自动驾驶开放平台
client.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 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 *****************************************************************************/
17
18#include <string>
19#include <vector>
20
21#include <boost/algorithm/string.hpp>
22#include <boost/filesystem.hpp>
23
24#include "yaml-cpp/yaml.h"
25
26#include "cyber/cyber.h"
31
32namespace apollo {
33namespace hdmap {
34
36 YAML::Node node = YAML::LoadFile(FLAGS_client_conf_yaml);
37 std::string bin_path = boost::filesystem::current_path().string();
38 data_collect_time_flag_file_ =
39 bin_path + "/" + node["time_flag_file"].as<std::string>();
40 channel_checker_stop_flag_file_ =
41 bin_path + "/" +
42 node["channel_check"]["stop_flag_file"].as<std::string>();
43 AINFO << "bin_path: " << bin_path
44 << ", data_collect_time_flag_file_: " << data_collect_time_flag_file_
45 << ", channel_checker_stop_flag_file_: "
46 << channel_checker_stop_flag_file_;
47}
48
50 std::string stage = FLAGS_stage;
51 AINFO << "stage [" << stage << "]";
52 int ret = 0;
53 if ("record_check" == stage) {
54 ret = RecordCheckStage();
55 } else if ("static_align" == stage) {
56 ret = StaticAlignStage();
57 } else if ("eight_route" == stage) {
58 ret = EightRouteStage();
59 } else if ("data_collect" == stage) {
60 ret = DataCollectStage();
61 } else if ("loops_check" == stage) {
62 ret = LoopsCheckStage();
63 } else if ("clean" == stage) {
64 ret = CleanStage();
65 }
66 return ret;
67}
68
69int Client::RecordCheckStage() {
70 std::string cmd = FLAGS_cmd;
71 ChannelChecker channel_checker(channel_checker_stop_flag_file_);
72 AINFO << "cmd [" << cmd << "]";
73 if ("start" == cmd) {
74 std::string record_path = FLAGS_record_path;
75 AINFO << "record_path [" << record_path << "]";
76 if (!boost::filesystem::exists(record_path)) {
77 AERROR << "record_path does not exist";
78 return -1;
79 }
80 int ret = 0;
81 ret = channel_checker.SyncStart(record_path);
82 if (ret != 0) {
83 AERROR << "SyncStart channel chacker failed, record_path [" << record_path
84 << "]";
85 return -1;
86 }
87 } else {
88 int ret = 0;
89 ret = channel_checker.SyncStop();
90 if (ret != 0) {
91 AERROR << "SyncStop channel chacker failed";
92 return -1;
93 }
94 }
95 return 0;
96}
97
98int Client::StaticAlignStage() {
99 std::string cmd = FLAGS_cmd;
100 AINFO << "cmd [" << cmd << "]";
101 StaticAlign static_align;
102 if ("start" == cmd) {
103 int ret = 0;
104 ret = static_align.SyncStart();
105 if (ret != 0) {
106 AERROR << "SyncStart static align failed";
107 return -1;
108 } else {
109 AINFO << "Static aligh succeed";
110 fprintf(USER_STREAM,
111 "Static aligh succeed. Next, you may want to run: bash client.sh "
112 "-- stage eight_route\n");
113 }
114 } else {
115 int ret = 0;
116 ret = static_align.SyncStop();
117 if (ret != 0) {
118 AERROR << "SyncStop static align failed";
119 return -1;
120 }
121 }
122 return 0;
123}
124
125int Client::EightRouteStage() {
126 std::string cmd = FLAGS_cmd;
127 AINFO << "cmd [" << cmd << "]";
128 EightRoute eight_route;
129 if ("start" == cmd) {
130 int ret = 0;
131 ret = eight_route.SyncStart();
132 if (ret != 0) {
133 AERROR << "SyncStart static align failed";
134 return -1;
135 } else {
136 AINFO << "Eight route succeed";
137 fprintf(USER_STREAM,
138 "Eight route succeed. Next, you may want to run: bash client.sh "
139 "--stage data_collect\n");
140 }
141 } else {
142 int ret = 0;
143 ret = eight_route.SyncStop();
144 if (ret != 0) {
145 AERROR << "SyncStop static align failed";
146 return -1;
147 }
148 }
149 return 0;
150}
151
152int Client::DataCollectStage() {
153 std::string cmd = FLAGS_cmd;
154 AINFO << "cmd [" << cmd << "]";
155 std::vector<std::string> lines = GetFileLines(data_collect_time_flag_file_);
156 std::ofstream time_file_handler(data_collect_time_flag_file_);
157 double now = UnixNow();
158 if (cmd == "start") {
159 if (lines.empty()) {
160 time_file_handler << now << " start\n";
161 AINFO << "write [" << now << " start] to file "
162 << data_collect_time_flag_file_;
163 fprintf(USER_STREAM,
164 "Start success. At the end of the collection, you should run: "
165 "bash client.sh data_collect stop\n");
166 } else {
167 std::string& the_last_line = lines.back();
168 std::vector<std::string> s;
169 boost::split(s, the_last_line, boost::is_any_of(" ,\t\n"));
170 if (s[1] == "start") {
171 AINFO << "This progress has been already started, this command will be "
172 "ignored";
173 fprintf(USER_STREAM,
174 "This progress has been already started, this command will be "
175 "ignored\n");
176 } else {
177 time_file_handler << now << " start\n";
178 AINFO << "write [" << now << " start] to file "
179 << data_collect_time_flag_file_;
180 fprintf(USER_STREAM,
181 "Start success. At the end of the collection, you should run: "
182 "bash client.sh --stage data_collect --cmd stop\n");
183 }
184 }
185 } else if (cmd == "stop") {
186 if (lines.empty()) {
187 AINFO << "Start first, this command will be ignored";
188 } else {
189 std::string& the_last_line = lines.back();
190 std::vector<std::string> s;
191 boost::split(s, the_last_line, boost::is_any_of(" ,\t\n"));
192 if (s[1] == "start") {
193 time_file_handler << now << " stop\n";
194 AINFO << "write [" << now << " stop] to file "
195 << data_collect_time_flag_file_;
196 fprintf(USER_STREAM,
197 "Stop success. Next you may want to run: bash client.sh "
198 "loops_check start\n");
199 } else {
200 AINFO << "This progress has been already stopped, this command will be "
201 "ignored";
202 fprintf(USER_STREAM,
203 "This progress has been already stopped, this command will be "
204 "ignored\n");
205 }
206 }
207 } else {
208 AINFO << "Error command, expected command are [start|stop], current "
209 "command is ["
210 << cmd << "]";
211 fprintf(USER_STREAM,
212 "Error command, expected command are [start|stop], current command "
213 "is [%s]\n",
214 cmd.c_str());
215 }
216 return 0;
217}
218
219int Client::LoopsCheckStage() {
220 LoopsChecker loops_checker(data_collect_time_flag_file_);
221 bool reached = false;
222 int ret = loops_checker.SyncStart(&reached);
223 if (ret != 0) {
224 AINFO << "loops_check failed";
225 return -1;
226 }
227 if (reached) {
228 AINFO << "loops meet requirements";
229 fprintf(USER_STREAM,
230 "Loops meet requirements. Next you may want to run: bash client.sh "
231 "--stage eight_route\n");
232 } else {
233 AINFO << "loops do not meet requirements";
234 fprintf(USER_STREAM,
235 "Next you may need to run: bash client.sh --stage data_collect\n");
236 }
237 return 0;
238}
239
240int Client::CleanStage() {
241 if (boost::filesystem::exists(data_collect_time_flag_file_)) {
242 boost::filesystem::remove(data_collect_time_flag_file_);
243 AINFO << "removed " << data_collect_time_flag_file_;
244 }
245 if (boost::filesystem::exists(channel_checker_stop_flag_file_)) {
246 boost::filesystem::remove(channel_checker_stop_flag_file_);
247 AINFO << "removed " << channel_checker_stop_flag_file_;
248 }
249 fprintf(USER_STREAM, "clean done\n");
250 return 0;
251}
252} // namespace hdmap
253} // namespace apollo
#define USER_STREAM
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
double UnixNow()
std::vector< std::string > GetFileLines(const std::string &path)
class register implement
Definition arena_queue.h:37