Apollo 11.0
自动驾驶开放平台
decision_data.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
18
20
21namespace apollo {
22namespace planning {
23
24// this sanity check will move to the very beginning of planning
25bool DecisionData::IsValidTrajectoryPoint(
26 const common::TrajectoryPoint& point) {
27 return !((!point.has_path_point()) || std::isnan(point.path_point().x()) ||
28 std::isnan(point.path_point().y()) ||
29 std::isnan(point.path_point().z()) ||
30 std::isnan(point.path_point().kappa()) ||
31 std::isnan(point.path_point().s()) ||
32 std::isnan(point.path_point().dkappa()) ||
33 std::isnan(point.path_point().ddkappa()) || std::isnan(point.v()) ||
34 std::isnan(point.a()) || std::isnan(point.relative_time()));
35}
36
37bool DecisionData::IsValidTrajectory(const prediction::Trajectory& trajectory) {
38 for (const auto& point : trajectory.trajectory_point()) {
39 if (!IsValidTrajectoryPoint(point)) {
40 AERROR << " TrajectoryPoint: " << trajectory.ShortDebugString()
41 << " is NOT valid.";
42 return false;
43 }
44 }
45 return true;
46}
47
49 const prediction::PredictionObstacles& prediction_obstacles,
50 const ReferenceLine& reference_line)
51 : reference_line_(reference_line) {
52 for (const auto& prediction_obstacle :
53 prediction_obstacles.prediction_obstacle()) {
54 const std::string perception_id =
55 std::to_string(prediction_obstacle.perception_obstacle().id());
56 if (prediction_obstacle.trajectory().empty()) {
57 obstacles_.emplace_back(new Obstacle(
58 perception_id, prediction_obstacle.perception_obstacle()));
59 all_obstacle_.emplace_back(obstacles_.back().get());
60 practical_obstacle_.emplace_back(obstacles_.back().get());
61 static_obstacle_.emplace_back(obstacles_.back().get());
62 obstacle_map_[perception_id] = obstacles_.back().get();
63 continue;
64 }
65 int trajectory_index = 0;
66 for (const auto& trajectory : prediction_obstacle.trajectory()) {
67 if (!IsValidTrajectory(trajectory)) {
68 AERROR << "obj:" << perception_id;
69 continue;
70 }
71 const std::string obstacle_id =
72 absl::StrCat(perception_id, "_", trajectory_index);
73 obstacles_.emplace_back(new Obstacle(
74 obstacle_id, prediction_obstacle.perception_obstacle(), trajectory));
75 all_obstacle_.emplace_back(obstacles_.back().get());
76 practical_obstacle_.emplace_back(obstacles_.back().get());
77 dynamic_obstacle_.emplace_back(obstacles_.back().get());
78 obstacle_map_[obstacle_id] = obstacles_.back().get();
79 ++trajectory_index;
80 }
81 }
82}
83
84Obstacle* DecisionData::GetObstacleById(const std::string& id) {
85 std::lock_guard<std::mutex> lock(mutex_);
86 return common::util::FindPtrOrNull(obstacle_map_, id);
87}
88
89std::vector<Obstacle*> DecisionData::GetObstacleByType(
90 const VirtualObjectType& type) {
91 std::lock_guard<std::mutex> lock(transaction_mutex_);
92
93 std::unordered_set<std::string> ids = GetObstacleIdByType(type);
94 if (ids.empty()) {
95 return std::vector<Obstacle*>();
96 }
97 std::vector<Obstacle*> ret;
98 for (const std::string& id : ids) {
99 ret.emplace_back(GetObstacleById(id));
100 if (ret.back() == nullptr) {
101 AERROR << "Ignore. can't find obstacle by id: " << id;
102 ret.pop_back();
103 }
104 }
105 return ret;
106}
107
108std::unordered_set<std::string> DecisionData::GetObstacleIdByType(
109 const VirtualObjectType& type) {
110 std::lock_guard<std::mutex> lock(mutex_);
111 return common::util::FindWithDefault(virtual_obstacle_id_map_, type, {});
112}
113
114const std::vector<Obstacle*>& DecisionData::GetStaticObstacle() const {
115 return static_obstacle_;
116}
117
118const std::vector<Obstacle*>& DecisionData::GetDynamicObstacle() const {
119 return dynamic_obstacle_;
120}
121
122const std::vector<Obstacle*>& DecisionData::GetVirtualObstacle() const {
123 return virtual_obstacle_;
124}
125
126const std::vector<Obstacle*>& DecisionData::GetPracticalObstacle() const {
127 return practical_obstacle_;
128}
129
130const std::vector<Obstacle*>& DecisionData::GetAllObstacle() const {
131 return all_obstacle_;
132}
133
135 const VirtualObjectType& type,
136 std::string* const id) {
137 // should build different box by type;
138 common::SLPoint sl_point;
139 if (!reference_line_.XYToSL(point, &sl_point)) {
140 return false;
141 }
142 double obstacle_s = sl_point.s();
143 const double box_center_s = obstacle_s + FLAGS_virtual_stop_wall_length / 2.0;
144 auto box_center = reference_line_.GetReferencePoint(box_center_s);
145 double heading = reference_line_.GetReferencePoint(obstacle_s).heading();
146 double lane_left_width = 0.0;
147 double lane_right_width = 0.0;
148 reference_line_.GetLaneWidth(obstacle_s, &lane_left_width, &lane_right_width);
149 common::math::Box2d box(box_center, heading, FLAGS_virtual_stop_wall_length,
150 lane_left_width + lane_right_width);
151 return CreateVirtualObstacle(box, type, id);
152}
153
154bool DecisionData::CreateVirtualObstacle(const double point_s,
155 const VirtualObjectType& type,
156 std::string* const id) {
157 // should build different box by type;
158 const double box_center_s = point_s + FLAGS_virtual_stop_wall_length / 2.0;
159 auto box_center = reference_line_.GetReferencePoint(box_center_s);
160 double heading = reference_line_.GetReferencePoint(point_s).heading();
161 double lane_left_width = 0.0;
162 double lane_right_width = 0.0;
163 reference_line_.GetLaneWidth(point_s, &lane_left_width, &lane_right_width);
164 common::math::Box2d box(box_center, heading, FLAGS_virtual_stop_wall_length,
165 lane_left_width + lane_right_width);
166 return CreateVirtualObstacle(box, type, id);
167}
168
170 const common::math::Box2d& obstacle_box, const VirtualObjectType& type,
171 std::string* const id) {
172 std::lock_guard<std::mutex> transaction_lock(transaction_mutex_);
173 std::lock_guard<std::mutex> lock(mutex_);
174
175 perception::PerceptionObstacle perception_obstacle;
176 // TODO(All) please chagne is_virtual in obstacle
177 perception_obstacle.set_id(virtual_obstacle_.size() + 1);
178 perception_obstacle.mutable_position()->set_x(obstacle_box.center().x());
179 perception_obstacle.mutable_position()->set_y(obstacle_box.center().y());
180 perception_obstacle.set_theta(obstacle_box.heading());
181 perception_obstacle.mutable_velocity()->set_x(0);
182 perception_obstacle.mutable_velocity()->set_y(0);
183 perception_obstacle.set_length(obstacle_box.length());
184 perception_obstacle.set_width(obstacle_box.width());
185 perception_obstacle.set_height(FLAGS_virtual_stop_wall_height);
186 perception_obstacle.set_type(
188 perception_obstacle.set_tracking_time(1.0);
189
190 std::vector<common::math::Vec2d> corner_points;
191 obstacle_box.GetAllCorners(&corner_points);
192 for (const auto& corner_point : corner_points) {
193 auto* point = perception_obstacle.add_polygon_point();
194 point->set_x(corner_point.x());
195 point->set_y(corner_point.y());
196 }
197 *id = std::to_string(perception_obstacle.id());
198 obstacles_.emplace_back(new Obstacle(*id, perception_obstacle));
199 all_obstacle_.emplace_back(obstacles_.back().get());
200 virtual_obstacle_.emplace_back(obstacles_.back().get());
201 // would be changed if some virtual type is not static one
202 static_obstacle_.emplace_back(obstacles_.back().get());
203
204 virtual_obstacle_id_map_[type].insert(*id);
205 obstacle_map_[*id] = obstacles_.back().get();
206 return true;
207}
208
209} // namespace planning
210} // namespace apollo
Rectangular (undirected) bounding box in 2-D.
Definition box2d.h:52
void GetAllCorners(std::vector< Vec2d > *const corners) const
Getter of the corners of the box
Definition box2d.cc:140
double width() const
Getter of the width
Definition box2d.h:130
const Vec2d & center() const
Getter of the center of the box
Definition box2d.h:106
double length() const
Getter of the length
Definition box2d.h:124
double heading() const
Getter of the heading
Definition box2d.h:148
double y() const
Getter for y component
Definition vec2d.h:57
double x() const
Getter for x component
Definition vec2d.h:54
double heading() const
Definition path.h:118
Obstacle * GetObstacleById(const std::string &id)
const std::vector< Obstacle * > & GetStaticObstacle() const
const std::vector< Obstacle * > & GetDynamicObstacle() const
const std::vector< Obstacle * > & GetPracticalObstacle() const
bool CreateVirtualObstacle(const ReferencePoint &point, const VirtualObjectType &type, std::string *const id)
const std::vector< Obstacle * > & GetVirtualObstacle() const
std::unordered_set< std::string > GetObstacleIdByType(const VirtualObjectType &type)
DecisionData(const prediction::PredictionObstacles &prediction_obstacles, const ReferenceLine &reference_line)
std::vector< Obstacle * > GetObstacleByType(const VirtualObjectType &type)
const std::vector< Obstacle * > & GetAllObstacle() const
This is the class that associates an Obstacle with its path properties.
Definition obstacle.h:62
ReferencePoint GetReferencePoint(const double s) const
bool XYToSL(const common::math::Vec2d &xy_point, common::SLPoint *const sl_point, double warm_start_s=-1.0) const
Transvert Cartesian coordinates to Frenet.
bool GetLaneWidth(const double s, double *const lane_left_width, double *const lane_right_width) const
Planning module main class.
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37