25#include "absl/strings/str_cat.h"
27#include "modules/common_msgs/routing_msgs/routing.pb.h"
59 : sequence_num_(sequence_num),
60 monitor_logger_buffer_(common::monitor::MonitorMessageItem::PLANNING) {}
66 : sequence_num_(sequence_num),
68 planning_start_point_(planning_start_point),
69 vehicle_state_(vehicle_state),
70 reference_line_provider_(reference_line_provider),
71 monitor_logger_buffer_(common::monitor::MonitorMessageItem::PLANNING) {}
80 return planning_start_point_;
84 return vehicle_state_;
88 if (FLAGS_use_navigation_mode) {
89 AERROR <<
"Rerouting not supported in navigation mode";
93 AERROR <<
"No previous routing available";
97 AERROR <<
"Invalid HD Map.";
102 rerouting->set_need_rerouting(
true);
103 auto *lane_follow_command = rerouting->mutable_lane_follow_command();
104 if (future_route_waypoints_.size() < 1) {
105 AERROR <<
"Failed to find future waypoints";
108 for (
size_t i = 0; i < future_route_waypoints_.size() - 1; i++) {
109 auto waypoint = lane_follow_command->add_way_point();
110 waypoint->set_x(future_route_waypoints_[i].pose().x());
111 waypoint->set_y(future_route_waypoints_[i].pose().y());
112 waypoint->set_heading(future_route_waypoints_[i].heading());
114 auto *end_pose = lane_follow_command->mutable_end_pose();
115 end_pose->set_x(future_route_waypoints_.back().pose().x());
116 end_pose->set_y(future_route_waypoints_.back().pose().y());
117 end_pose->set_heading(future_route_waypoints_.back().heading());
119 monitor_logger_buffer_.INFO(
"Planning send Rerouting request");
124 return reference_line_info_;
128 return &reference_line_info_;
132 const std::map<std::string, uint32_t> &id_to_priority) {
133 for (
const auto &pair : id_to_priority) {
134 const auto id = pair.first;
135 const auto priority = pair.second;
136 auto ref_line_info_itr =
137 std::find_if(reference_line_info_.begin(), reference_line_info_.end(),
139 return ref_line_info.Lanes().Id() == id;
141 if (ref_line_info_itr != reference_line_info_.end()) {
142 ref_line_info_itr->SetPriority(priority);
147bool Frame::CreateReferenceLineInfo(
148 const std::list<ReferenceLine> &reference_lines,
149 const std::list<hdmap::RouteSegments> &segments) {
150 reference_line_info_.clear();
151 if (reference_lines.empty()) {
154 auto ref_line_iter = reference_lines.begin();
155 auto segments_iter = segments.begin();
156 std::size_t ref_line_index = 0;
157 while (ref_line_iter != reference_lines.end()) {
158 if (segments_iter->StopForDestination()) {
159 is_near_destination_ =
true;
161 reference_line_info_.emplace_back(vehicle_state_, planning_start_point_,
162 *ref_line_iter, *segments_iter);
163 reference_line_info_.back().set_index(ref_line_index);
169 if (reference_line_info_.size() == 2) {
170 common::math::Vec2d xy_point(vehicle_state_.
x(), vehicle_state_.
y());
171 common::SLPoint first_sl;
172 if (!reference_line_info_.front().reference_line().XYToSL(xy_point,
176 common::SLPoint second_sl;
177 if (!reference_line_info_.back().reference_line().XYToSL(xy_point,
181 const double offset = first_sl.l() - second_sl.l();
182 reference_line_info_.front().SetOffsetToOtherReferenceLine(offset);
183 reference_line_info_.back().SetOffsetToOtherReferenceLine(-offset);
185 double target_speed = FLAGS_default_cruise_speed;
189 bool has_valid_reference_line =
false;
191 for (
auto iter = reference_line_info_.begin();
192 iter != reference_line_info_.end();) {
193 if (!iter->Init(
obstacles(), target_speed)) {
194 reference_line_info_.erase(iter++);
196 has_valid_reference_line =
true;
197 iter->set_index(ref_line_index);
198 AINFO <<
"get referenceline: index: " << iter->index()
199 <<
", id: " << iter->id() <<
", key: " << iter->key();
204 if (!has_valid_reference_line) {
205 AINFO <<
"No valid reference line";
216 const std::string &obstacle_id,
const double obstacle_s,
217 double stop_wall_width) {
219 AERROR <<
"reference_line_info nullptr";
224 const double box_center_s = obstacle_s + FLAGS_virtual_stop_wall_length / 2.0;
225 auto box_center = reference_line.GetReferencePoint(box_center_s);
226 double heading = reference_line.GetReferencePoint(obstacle_s).heading();
227 Box2d stop_wall_box{box_center, heading, FLAGS_virtual_stop_wall_length,
230 return CreateStaticVirtualObstacle(obstacle_id, stop_wall_box);
238 const std::string &lane_id,
239 const double lane_s) {
241 AERROR <<
"Invalid HD Map.";
245 if (
nullptr == reference_line_provider_) {
252 AERROR <<
"Failed to find lane[" << lane_id <<
"]";
256 double dest_lane_s = std::max(0.0, lane_s);
257 auto dest_point = lane->GetSmoothPoint(dest_lane_s);
259 double lane_left_width = 0.0;
260 double lane_right_width = 0.0;
261 lane->GetWidth(dest_lane_s, &lane_left_width, &lane_right_width);
263 Box2d stop_wall_box{{dest_point.x(), dest_point.y()},
264 lane->Heading(dest_lane_s),
265 FLAGS_virtual_stop_wall_length,
266 lane_left_width + lane_right_width};
268 return CreateStaticVirtualObstacle(obstacle_id, stop_wall_box);
276 const std::string &obstacle_id,
const double obstacle_start_s,
277 const double obstacle_end_s) {
279 AERROR <<
"reference_line_info nullptr";
287 sl_point.set_s(obstacle_start_s);
290 if (!reference_line.SLToXY(sl_point, &obstacle_start_xy)) {
291 AERROR <<
"Failed to get start_xy from sl: " << sl_point.DebugString();
296 sl_point.set_s(obstacle_end_s);
299 if (!reference_line.SLToXY(sl_point, &obstacle_end_xy)) {
300 AERROR <<
"Failed to get end_xy from sl: " << sl_point.DebugString();
304 double left_lane_width = 0.0;
305 double right_lane_width = 0.0;
306 if (!reference_line.GetLaneWidth(obstacle_start_s, &left_lane_width,
307 &right_lane_width)) {
308 AERROR <<
"Failed to get lane width at s[" << obstacle_start_s <<
"]";
314 left_lane_width + right_lane_width};
316 return CreateStaticVirtualObstacle(obstacle_id, obstacle_box);
319const Obstacle *Frame::CreateStaticVirtualObstacle(
const std::string &
id,
321 const auto *
object = obstacles_.
Find(
id);
323 AWARN <<
"obstacle " <<
id <<
" already exist.";
329 AERROR <<
"Failed to create virtual obstacle " << id;
336 const std::list<ReferenceLine> &reference_lines,
337 const std::list<hdmap::RouteSegments> &segments,
338 const std::vector<routing::LaneWaypoint> &future_route_waypoints,
341 auto status = InitFrameData(vehicle_state_provider, ego_info);
343 AERROR <<
"failed to init frame:" << status.ToString();
346 if (!CreateReferenceLineInfo(reference_lines, segments)) {
347 const std::string msg =
"Failed to init reference line info.";
349 return Status(ErrorCode::PLANNING_ERROR, msg);
351 future_route_waypoints_ = future_route_waypoints;
361 return InitFrameData(vehicle_state_provider, ego_info);
364Status Frame::InitFrameData(
368 CHECK_NOTNULL(hdmap_);
371 AERROR <<
"Adc init point is not set";
372 return Status(ErrorCode::PLANNING_ERROR,
"Adc init point is not set");
374 ADEBUG <<
"Enabled align prediction time ? : " << std::boolalpha
375 << FLAGS_align_prediction_time;
377 if (FLAGS_align_prediction_time) {
383 Obstacle::CreateObstacles(*local_view_.prediction_obstacles)) {
386 if (planning_start_point_.
v() < 1e-3) {
387 const auto *collision_obstacle = FindCollisionObstacle(ego_info);
388 if (collision_obstacle !=
nullptr) {
389 const std::string msg = absl::StrCat(
"Found collision with obstacle: ",
390 collision_obstacle->Id());
392 monitor_logger_buffer_.ERROR(msg);
393 return Status(ErrorCode::PLANNING_ERROR, msg);
399 ReadPadMsgDrivingAction();
404const Obstacle *Frame::FindCollisionObstacle(
const EgoInfo *ego_info)
const {
405 if (obstacles_.
Items().empty()) {
409 const auto &adc_polygon = Polygon2d(ego_info->ego_box());
410 for (
const auto &obstacle : obstacles_.Items()) {
411 if (obstacle->IsVirtual()) {
415 const auto &obstacle_polygon = obstacle->PerceptionPolygon();
416 if (obstacle_polygon.HasOverlap(adc_polygon)) {
426 return absl::StrCat(
"Frame: ", sequence_num_);
431 ADEBUG <<
"Skip record input into debug";
434 auto *planning_debug_data = debug->mutable_planning_data();
435 auto *adc_position = planning_debug_data->mutable_adc_position();
438 auto debug_chassis = planning_debug_data->mutable_chassis();
439 debug_chassis->CopyFrom(*local_view_.
chassis);
441 if (!FLAGS_use_navigation_mode) {
442 auto debug_routing = planning_debug_data->mutable_routing();
443 debug_routing->CopyFrom(
447 planning_debug_data->mutable_prediction_header()->CopyFrom(
460 if (!prediction_obstacles || !prediction_obstacles->has_header() ||
461 !prediction_obstacles->
header().has_timestamp_sec()) {
464 double prediction_header_time =
466 for (
auto &obstacle : *prediction_obstacles->mutable_prediction_obstacle()) {
467 for (
auto &trajectory : *obstacle.mutable_trajectory()) {
468 for (
auto &point : *trajectory.mutable_trajectory_point()) {
469 point.set_relative_time(prediction_header_time + point.relative_time() -
470 planning_start_time);
472 if (!trajectory.trajectory_point().empty() &&
473 trajectory.trajectory_point().begin()->relative_time() < 0) {
474 auto it = trajectory.trajectory_point().begin();
475 while (it != trajectory.trajectory_point().end() &&
476 it->relative_time() < 0) {
479 trajectory.mutable_trajectory_point()->erase(
480 trajectory.trajectory_point().begin(), it);
488void Frame::AddObstacle(
const Obstacle &obstacle) {
489 obstacles_.
Add(obstacle.
Id(), obstacle);
492void Frame::ReadTrafficLights() {
493 traffic_lights_.clear();
495 const auto traffic_light_detection = local_view_.
traffic_light;
496 if (traffic_light_detection ==
nullptr) {
501 if (delay > FLAGS_signal_expire_time_sec) {
502 ADEBUG <<
"traffic signals msg is expired, delay = " << delay
506 for (
const auto &traffic_light : traffic_light_detection->traffic_light()) {
507 traffic_lights_[traffic_light.id()] = &traffic_light;
512 const std::string &traffic_light_id)
const {
514 apollo::common::util::FindPtrOrNull(traffic_lights_, traffic_light_id);
515 if (result ==
nullptr) {
517 traffic_light.set_id(traffic_light_id);
519 traffic_light.set_confidence(0.0);
520 traffic_light.set_tracking_time(0.0);
521 return traffic_light;
526void Frame::ReadPadMsgDrivingAction() {
528 if (local_view_.
pad_msg->has_action()) {
529 pad_msg_driving_action_ = local_view_.
pad_msg->action();
534void Frame::ResetPadMsgDrivingAction() {
539 double min_cost = std::numeric_limits<double>::infinity();
540 drive_reference_line_info_ =
nullptr;
548 return drive_reference_line_info_;
559 return target_reference_line_info;
574 return drive_reference_line_info_;
578 return obstacles_.
Items();
A general class to denote the return status of an API call.
static Status OK()
generate a success status.
The class of vehicle state.
const VehicleState & vehicle_state() const
Rectangular (undirected) bounding box in 2-D.
The class of polygon in 2-D.
Implements a class of 2-dimensional vectors.
a singleton clock that can be used to get the current timestamp.
static double NowInSeconds()
gets the current time in second.
static const HDMap * BaseMapPtr()
LaneInfoConstPtr GetLaneById(const Id &id) const
Frame holds all data for one planning cycle.
const std::vector< const Obstacle * > obstacles() const
static void AlignPredictionTime(const double planning_start_time, prediction::PredictionObstacles *prediction_obstacles)
uint32_t SequenceNum() const
Frame(uint32_t sequence_num)
const common::VehicleState & vehicle_state() const
const Obstacle * CreateStaticObstacle(ReferenceLineInfo *const reference_line_info, const std::string &obstacle_id, const double obstacle_start_s, const double obstacle_end_s)
: create static virtual object with lane width,
common::Status Init(const common::VehicleStateProvider *vehicle_state_provider, const std::list< ReferenceLine > &reference_lines, const std::list< hdmap::RouteSegments > &segments, const std::vector< routing::LaneWaypoint > &future_route_waypoints, const EgoInfo *ego_info)
void UpdateReferenceLinePriority(const std::map< std::string, uint32_t > &id_to_priority)
Adjust reference line priority according to actual road conditions @id_to_priority lane id and refere...
const ReferenceLineInfo * FindDriveReferenceLineInfo()
perception::TrafficLight GetSignal(const std::string &traffic_light_id) const
std::string DebugString() const
Obstacle * Find(const std::string &id)
bool Rerouting(PlanningContext *planning_context)
void RecordInputDebug(planning_internal::Debug *debug)
const ReferenceLineInfo * FindFailedReferenceLineInfo()
common::Status InitForOpenSpace(const common::VehicleStateProvider *vehicle_state_provider, const EgoInfo *ego_info)
const ReferenceLineInfo * DriveReferenceLineInfo() const
const std::list< ReferenceLineInfo > & reference_line_info() const
const Obstacle * CreateStopObstacle(ReferenceLineInfo *const reference_line_info, const std::string &obstacle_id, const double obstacle_s, double stop_wall_width=4.0)
: create static virtual object with lane width, mainly used for virtual stop wall
std::list< ReferenceLineInfo > * mutable_reference_line_info()
const ReferenceLineInfo * FindTargetReferenceLineInfo()
const common::TrajectoryPoint & PlanningStartPoint() const
This is the class that associates an Obstacle with its path properties.
const std::string & Id() const
static std::unique_ptr< Obstacle > CreateStaticVirtualObstacles(const std::string &id, const common::math::Box2d &obstacle_box)
PlanningStatus * mutable_planning_status()
ReferenceLineInfo holds all data for one reference line.
The class of ReferenceLineProvider.
hdmap::LaneInfoConstPtr GetLaneById(const hdmap::Id &id) const
T * Add(const I id, const T &object)
std::vector< const T * > Items() const
Planning module main class.
apollo::hdmap::Id MakeMapId(const std::string &id)
create a Map ID given a string.
std::shared_ptr< const LaneInfo > LaneInfoConstPtr
bool IsVehicleStateValid(const VehicleState &vehicle_state)
Declaration of the class ReferenceLineProvider.
optional double timestamp
std::shared_ptr< PadMessage > pad_msg
std::shared_ptr< localization::LocalizationEstimate > localization_estimate
std::shared_ptr< PlanningCommand > planning_command
std::shared_ptr< prediction::PredictionObstacles > prediction_obstacles
std::shared_ptr< canbus::Chassis > chassis
std::shared_ptr< perception::TrafficLightDetection > traffic_light
optional apollo::common::Header header
LocalView contains all necessary data as planning input