Apollo 11.0
自动驾驶开放平台
planning_test_base.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
18
19#include "modules/common_msgs/chassis_msgs/chassis.pb.h"
20#include "modules/common_msgs/localization_msgs/localization.pb.h"
21#include "modules/common_msgs/perception_msgs/traffic_light_detection.pb.h"
22#include "modules/common_msgs/prediction_msgs/prediction_obstacle.pb.h"
23#include "modules/common_msgs/routing_msgs/routing.pb.h"
24
25#include "cyber/common/file.h"
26#include "cyber/common/log.h"
29
30namespace apollo {
31namespace planning {
32
39
40DEFINE_string(test_data_dir, "", "the test data folder");
41DEFINE_bool(test_update_golden_log, false,
42 "true to update decision golden log file.");
43DEFINE_string(test_routing_response_file, "", "The routing file used in test");
44DEFINE_string(test_localization_file, "", "The localization test file");
45DEFINE_string(test_chassis_file, "", "The chassis test file");
46DEFINE_string(test_planning_config_file, "", "planning config file for test");
47DEFINE_string(test_prediction_file, "", "The prediction module test file");
48DEFINE_string(test_traffic_light_file, "", "The traffic light test file");
49DEFINE_string(test_relative_map_file, "", "The relative map test file");
50DEFINE_string(test_previous_planning_file, "",
51 "The previous planning test file");
52
54 FLAGS_use_multi_thread_to_add_obstacles = false;
55 FLAGS_traffic_rule_config_filename =
56 "/apollo/modules/planning/planning_component/conf/"
57 "traffic_rule_config.pb.txt";
58 FLAGS_smoother_config_filename =
59 "/apollo/modules/planning/planning_component/conf/"
60 "qp_spline_smoother_config.pb.txt";
61 FLAGS_map_dir = "/apollo/modules/planning/planning_base/testdata";
62 FLAGS_test_localization_file = "";
63 FLAGS_test_chassis_file = "";
64 FLAGS_test_routing_response_file = "";
65 FLAGS_test_planning_config_file =
66 "/apollo/modules/planning/planning_component/conf/planning_config.pb.txt";
67 FLAGS_test_previous_planning_file = "";
68 FLAGS_test_prediction_file = "";
69 FLAGS_align_prediction_time = false;
70 FLAGS_enable_reference_line_provider_thread = false;
71 // FLAGS_enable_trajectory_check is temporarily disabled, otherwise EMPlanner
72 // and LatticePlanner can't pass the unit test.
73 FLAGS_enable_trajectory_check = false;
74}
75
77 // chassis
78 Chassis chassis;
79 if (FLAGS_test_chassis_file.empty()) {
80 AERROR << "Requires FLAGS_test_chassis_file to be set";
81 return false;
82 }
84 FLAGS_test_data_dir + "/" + FLAGS_test_chassis_file, &chassis)) {
85 AERROR << "failed to load file: " << FLAGS_test_chassis_file;
86 return false;
87 }
88 // localization
89 if (FLAGS_test_localization_file.empty()) {
90 AERROR << "Requires FLAGS_test_localization_file to be set";
91 return false;
92 }
93 LocalizationEstimate localization;
95 FLAGS_test_data_dir + "/" + FLAGS_test_localization_file,
96 &localization)) {
97 AERROR << "failed to load file: " << FLAGS_test_localization_file;
98 return false;
99 }
102
103 // prediction
104 if (FLAGS_test_prediction_file.empty()) {
105 AERROR << "Requires FLAGS_test_prediction_file to be set";
106 return false;
107 }
108 PredictionObstacles prediction;
110 FLAGS_test_data_dir + "/" + FLAGS_test_prediction_file,
111 &prediction)) {
112 AERROR << "failed to load file: " << FLAGS_test_prediction_file;
113 return false;
114 }
115 // routing_response
116 if (FLAGS_test_routing_response_file.empty()) {
117 AERROR << "Requires FLAGS_test_routing_response_file";
118 return false;
119 }
120 RoutingResponse routing_response;
122 FLAGS_test_data_dir + "/" + FLAGS_test_routing_response_file,
123 &routing_response)) {
124 AERROR << "failed to load file: " << FLAGS_test_routing_response_file;
125 return false;
126 }
127 // traffic_light_detection
128 // optional
129 TrafficLightDetection traffic_light_detection;
131 FLAGS_test_data_dir + "/" + FLAGS_test_traffic_light_file,
132 &traffic_light_detection)) {
133 AERROR << "failed to load file: " << FLAGS_test_traffic_light_file;
134 return false;
135 }
136
138 std::make_shared<PredictionObstacles>(prediction);
139 local_view_.chassis = std::make_shared<Chassis>(chassis);
141 std::make_shared<LocalizationEstimate>(localization);
143 std::make_shared<TrafficLightDetection>(traffic_light_detection);
144
145 AINFO << "Successfully feed proto files.";
146 return true;
147}
148
150 injector_ = std::make_shared<DependencyInjector>();
151
152 if (FLAGS_use_navigation_mode) {
153 // TODO(all)
154 // planning_ = std::unique_ptr<PlanningBase>(new NaviPlanning());
155 } else {
156 planning_ = std::unique_ptr<PlanningBase>(new OnLanePlanning(injector_));
157 }
158
159 ACHECK(FeedTestData()) << "Failed to feed test data";
160
161 ACHECK(cyber::common::GetProtoFromFile(FLAGS_test_planning_config_file,
162 &config_))
163 << "failed to load planning config file "
164 << FLAGS_test_planning_config_file;
165
166 ACHECK(planning_->Init(config_).ok()) << "Failed to init planning module";
167}
168
170 ACHECK(FeedTestData()) << "Failed to feed test data";
171}
172
174 bool no_trajectory_point) {
175 origin->clear_latency_stats();
176 origin->clear_debug();
177 // origin->mutable_header()->clear_radar_timestamp();
178 // origin->mutable_header()->clear_lidar_timestamp();
179 // origin->mutable_header()->clear_timestamp_sec();
180 // origin->mutable_header()->clear_camera_timestamp();
181 // origin->mutable_header()->clear_sequence_num();
182
183 if (no_trajectory_point) {
184 origin->clear_total_path_length();
185 origin->clear_total_path_time();
186 origin->clear_trajectory_point();
187 }
188}
189
190bool PlanningTestBase::RunPlanning(const std::string& test_case_name,
191 int case_num, bool no_trajectory_point) {
192 const std::string golden_result_file =
193 absl::StrCat("result_", test_case_name, "_", case_num, ".pb.txt");
194
195 std::string full_golden_path = FLAGS_test_data_dir + "/" + golden_result_file;
196
197 ADCTrajectory adc_trajectory_pb;
198 planning_->RunOnce(local_view_, &adc_trajectory_pb);
199
200 if (!IsValidTrajectory(adc_trajectory_pb)) {
201 AERROR << "Fail to pass trajectory check.";
202 return false;
203 }
204
205 adc_trajectory_ = adc_trajectory_pb;
206 TrimPlanning(&adc_trajectory_, no_trajectory_point);
207 if (FLAGS_test_update_golden_log) {
208 AINFO << "The golden file is regenerated:" << full_golden_path;
210 } else {
211 ADCTrajectory golden_result;
212 bool load_success =
213 cyber::common::GetProtoFromASCIIFile(full_golden_path, &golden_result);
214 TrimPlanning(&golden_result, no_trajectory_point);
215 if (!load_success ||
217 char tmp_fname[100] = "/tmp/XXXXXX";
218 int fd = mkstemp(tmp_fname);
219 if (fd < 0) {
220 AERROR << "Failed to create temporary file: " << tmp_fname;
221 return false;
222 }
224 AERROR << "Failed to write to file: " << tmp_fname;
225 }
226 AERROR << "found error\ndiff -y " << tmp_fname << " " << full_golden_path;
227 AERROR << "to override error\nmv " << tmp_fname << " "
228 << full_golden_path;
229 AERROR << "to visualize\n/usr/bin/python "
230 "modules/tools/plot_trace/plot_planning_result.py "
231 << tmp_fname << " " << full_golden_path;
232 return false;
233 }
234 }
235 return true;
236}
237
239 for (int i = 0; i < trajectory.trajectory_point_size(); ++i) {
240 const auto& point = trajectory.trajectory_point(i);
241
242 const double kMaxAccelThreshold =
243 FLAGS_longitudinal_acceleration_upper_bound;
244 const double kMinAccelThreshold =
245 FLAGS_longitudinal_acceleration_lower_bound;
246 if (point.a() > kMaxAccelThreshold || point.a() < kMinAccelThreshold) {
247 AERROR << "Invalid trajectory point because accel out of range: "
248 << point.DebugString();
249 return false;
250 }
251
252 if (!point.has_path_point()) {
253 AERROR << "Invalid trajectory point because NO path_point in "
254 "trajectory_point: "
255 << point.DebugString();
256 return false;
257 }
258
259 if (i > 0) {
260 const double kPathSEpsilon = 1e-3;
261 const auto& last_point = trajectory.trajectory_point(i - 1);
262 if (point.path_point().s() + kPathSEpsilon <
263 last_point.path_point().s()) {
264 AERROR << "Invalid trajectory point because s value error. last point: "
265 << last_point.DebugString()
266 << ", curr point: " << point.DebugString();
267 return false;
268 }
269 }
270 }
271 return true;
272}
273
274} // namespace planning
275} // namespace apollo
a singleton clock that can be used to get the current timestamp.
Definition clock.h:39
static void SetNowInSeconds(const double seconds)
This is for mock clock mode only.
Definition clock.h:84
static void SetMode(ClockMode mode)
Definition clock.cc:58
void TrimPlanning(ADCTrajectory *origin, bool no_trajectory_point)
bool RunPlanning(const std::string &test_case_name, int case_num, bool no_trajectory_point)
Execute the planning code.
bool IsValidTrajectory(const ADCTrajectory &trajectory)
std::shared_ptr< DependencyInjector > injector_
std::unique_ptr< PlanningBase > planning_
Planning module main class.
#define ACHECK(cond)
Definition log.h:80
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
bool IsProtoEqual(const ProtoA &a, const ProtoB &b)
Definition util.h:47
bool GetProtoFromASCIIFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as ascii representation of protobufs,...
Definition file.cc:89
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 SetProtoToASCIIFile(const google::protobuf::Message &message, int file_descriptor)
Definition file.cc:43
DEFINE_bool(test_update_golden_log, false, "true to update decision golden log file.")
DEFINE_string(test_data_dir, "", "the test data folder")
class register implement
Definition arena_queue.h:37
optional double timestamp_sec
Definition header.proto:9
optional apollo::common::Header header
repeated apollo::common::TrajectoryPoint trajectory_point
std::shared_ptr< localization::LocalizationEstimate > localization_estimate
Definition local_view.h:43
std::shared_ptr< prediction::PredictionObstacles > prediction_obstacles
Definition local_view.h:41
std::shared_ptr< canbus::Chassis > chassis
Definition local_view.h:42
std::shared_ptr< perception::TrafficLightDetection > traffic_light
Definition local_view.h:44