Apollo 11.0
自动驾驶开放平台
free_space_scenario.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 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
22
23#include <vector>
24
29
30namespace apollo {
31namespace planning {
35
36bool FreeSpaceScenario::Init(std::shared_ptr<DependencyInjector> injector, const std::string& name) {
37 // hook: Apollo License Verification: v_apollo_park
38 if (init_) {
39 return true;
40 }
41
42 if (!Scenario::Init(injector, name)) {
43 AERROR << "failed to init scenario" << Name();
44 return false;
45 }
46
47 if (!Scenario::LoadConfig<ScenarioFreeSpaceConfig>(&context_.scenario_config)) {
48 AERROR << "fail to get config of scenario" << Name();
49 return false;
50 }
52 CHECK_NOTNULL(hdmap_);
53 init_ = true;
54 return true;
55}
56
57bool FreeSpaceScenario::IsTransferable(const Scenario* const other_scenario, const Frame& frame) {
58 const auto& planning_command = frame.local_view().planning_command;
59 if (!planning_command->has_custom_command()) {
60 return false;
61 }
62 if (!planning_command->custom_command().Is<FreeSpaceCommand>()) {
63 return false;
64 }
65 context_.free_space_command.Clear();
66 if (!planning_command->custom_command().UnpackTo(&context_.free_space_command)) {
67 AERROR << "Free Space Command unpack error" << planning_command->DebugString();
68 return false;
69 }
70 const auto& vehicle_state = frame.vehicle_state();
71 const auto& vehicle_param = common::VehicleConfigHelper::Instance()->GetConfig().vehicle_param();
72 double adc_speed = vehicle_state.linear_velocity();
73 const double max_adc_stop_speed = vehicle_param.max_abs_speed_when_stopped();
74 if (std::fabs(adc_speed) > max_adc_stop_speed) {
75 AINFO << "adc not stopped" << adc_speed;
76 return false;
77 }
78 apollo::common::math::Polygon2d ego_box(injector_->ego_info()->ego_box());
79 Vec2d vec_to_center(
80 (vehicle_param.front_edge_to_center() - vehicle_param.back_edge_to_center()) / 2.0,
81 (vehicle_param.left_edge_to_center() - vehicle_param.right_edge_to_center()) / 2.0);
82 const auto& end_pose = context_.free_space_command.parking_spot_pose();
83 Vec2d position(end_pose.x(), end_pose.y());
84 Vec2d center(position + vec_to_center.rotate(end_pose.heading()));
85 Polygon2d end_box(Box2d(center, end_pose.heading(), vehicle_param.length(), vehicle_param.width()));
86 PrintCurves print_curves;
87 print_curves.AddPoint("ego_box", ego_box.points());
88 print_curves.AddPoint("end_box", end_box.points());
89 if (!context_.free_space_command.has_drivable_roi()) {
90 AERROR << "drivable_roi is needed!";
91 return false;
92 }
93 const auto& roi_polygon = context_.free_space_command.drivable_roi();
94 std::vector<Vec2d> polygon_points;
95 for (const auto& point : roi_polygon.point()) {
96 polygon_points.emplace_back(point.x(), point.y());
97 }
98 print_curves.AddPoint("free_space_roi", polygon_points);
99 Polygon2d polygon_roi(polygon_points);
100 if (OpenSpaceRoiUtil::IsPolygonClockwise(polygon_points)) {
101 AERROR << "drivable_roi should be counter-clockwise!";
102 print_curves.PrintToLog();
103 return false;
104 }
105 if (!polygon_roi.Contains(ego_box) || !polygon_roi.Contains(end_box)) {
106 AERROR << "ego box or end box is out of roi";
107 print_curves.PrintToLog();
108 return false;
109 }
110 for (const auto& roi_polygon : context_.free_space_command.non_drivable_roi()) {
111 std::vector<Vec2d> polygon_points;
112 for (const auto& point : roi_polygon.point()) {
113 polygon_points.emplace_back(point.x(), point.y());
114 }
115 print_curves.AddPoint("free_space_roi", polygon_points);
116 Polygon2d polygon_roi(polygon_points);
117 if (!OpenSpaceRoiUtil::IsPolygonClockwise(polygon_points)) {
118 AERROR << "non_drivable_roi should be clockwise!";
119 print_curves.PrintToLog();
120 return false;
121 }
122 if (polygon_roi.HasOverlap(ego_box) || polygon_roi.HasOverlap(end_box)) {
123 AERROR << "ego box or end box has collision with obs";
124 print_curves.PrintToLog();
125 return false;
126 }
127 }
128 print_curves.PrintToLog();
129 return true;
130} // namespace planning
131
132} // namespace planning
133} // namespace apollo
Rectangular (undirected) bounding box in 2-D.
Definition box2d.h:52
The class of polygon in 2-D.
Definition polygon2d.h:42
bool HasOverlap(const LineSegment2d &line_segment) const
Check if a line segment has overlap with this polygon.
Definition polygon2d.cc:377
bool Contains(const LineSegment2d &line_segment) const
Check if the polygon contains a line segment.
Definition polygon2d.cc:173
const std::vector< Vec2d > & points() const
Get the vertices of the polygon.
Definition polygon2d.h:65
Implements a class of 2-dimensional vectors.
Definition vec2d.h:42
Vec2d rotate(const double angle) const
rotate the vector by angle.
Definition vec2d.cc:65
static const HDMap * BaseMapPtr()
Frame holds all data for one planning cycle.
Definition frame.h:62
const LocalView & local_view() const
Definition frame.h:163
const common::VehicleState & vehicle_state() const
Definition frame.cc:83
bool IsTransferable(const Scenario *const other_scenario, const Frame &frame) override
Each scenario should define its own transfer condition, i.e., when it should allow to transfer from o...
bool Init(std::shared_ptr< DependencyInjector > injector, const std::string &name) override
static bool IsPolygonClockwise(const std::vector< Vec2d > &polygon)
void AddPoint(std::string key, double x, double y)
add point to curve key
virtual bool Init(std::shared_ptr< DependencyInjector > injector, const std::string &name)
Definition scenario.cc:43
std::shared_ptr< DependencyInjector > injector_
Definition scenario.h:113
const std::string & Name() const
Definition scenario.h:98
Planning module main class.
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37
apollo::external_command::FreeSpaceCommand free_space_command
ScenarioFreeSpaceConfig scenario_config
std::shared_ptr< PlanningCommand > planning_command
Definition local_view.h:48