Apollo 11.0
自动驾驶开放平台
extricate_stage.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
22
23#include <string>
24#include <vector>
28
29namespace apollo {
30namespace planning {
31
35#define AINFO AERROR
37 const StagePipeline& config,
38 const std::shared_ptr<DependencyInjector>& injector,
39 const std::string& config_dir,
40 void* context) {
41 CHECK_NOTNULL(context);
42 bool ret = Stage::Init(config, injector, config_dir, context);
43 if (!ret) {
44 AERROR << Name() << "init failed!";
45 return false;
46 }
47 scenario_config_.CopyFrom(GetContextAs<SquareContext>()->scenario_config);
48 return ret;
49}
50
52 AINFO << "In ExtricateStage";
53 apollo::common::VehicleState vehicle_state = injector_->vehicle_state()->vehicle_state();
54 if (vehicle_state.gear() != canbus::Chassis::GEAR_REVERSE) {
57 static constexpr double gear_shift_max_t = 5.0;
58 static constexpr double gear_shift_unit_t = 0.1;
59 for (double t = 0.0; t < gear_shift_max_t; t += gear_shift_unit_t) {
60 TrajectoryPoint point;
61 point.mutable_path_point()->set_x(frame->vehicle_state().x());
62 point.mutable_path_point()->set_y(frame->vehicle_state().y());
63 point.mutable_path_point()->set_theta(frame->vehicle_state().heading());
64 point.mutable_path_point()->set_s(0.0);
65 point.mutable_path_point()->set_kappa(0);
66 point.set_relative_time(t);
67 point.set_v(0.0);
68 point.set_a(0.0);
69 traj_pair->first.emplace_back(point);
70 }
71 traj_pair->second = apollo::canbus::Chassis::GEAR_REVERSE;
72 AINFO << "GENERATE REVERSE TRAJECTORY";
73 need_to_stop_ = false;
75 }
76 AINFO << "gear is reverse";
77 auto& reference_line_info = frame->mutable_reference_line_info()->back();
78 const auto& reference_line = reference_line_info.reference_line();
79 double adc_start_s = reference_line_info.AdcSlBoundary().start_s();
80 // GenerateStraightReversePath(reference_line_info.mutable_path_data(),
81 // planning_init_point);
82 if (stop_point_ == nullptr) {
84 auto ref_point = reference_line.GetReferencePoint(adc_start_s - 10.0);
85 stop_point_ = std::make_shared<common::PathPoint>(ref_point.ToPathPoint(0.0));
86 }
87 AINFO << "need to stop";
88 common::SLPoint sl_point;
89 reference_line.XYToSL(*stop_point_, &sl_point);
90 std::vector<std::string> wait_for_obstacles;
92 "reverse_stop",
93 sl_point.s(),
94 0.1,
95 StopReasonCode::STOP_REASON_STOP_SIGN,
96 wait_for_obstacles,
97 "reverse stop",
98 frame,
99 &(frame->mutable_reference_line_info()->front()));
100 StageResult result = ExecuteTaskOnReferenceLine(planning_init_point, frame);
101 const auto& candidate_paths = reference_line_info.GetCandidatePathData();
102 bool has_regular_path = false;
103 for (const auto& path_data : candidate_paths) {
104 if (path_data.path_label().find("regular") != std::string::npos && path_data.blocking_obstacle_id().empty()) {
105 AINFO << "find regular path: " << path_data.path_label();
106 has_regular_path = true;
107 break;
108 }
109 }
110 if (has_regular_path) {
111 success_path_times_++;
112 } else {
113 success_path_times_ = 0;
114 need_to_stop_ = false;
115 }
116 if (success_path_times_ > 10 && !need_to_stop_) {
117 AINFO << "find square path change stage";
118 need_to_stop_ = true;
119 *stop_point_ = reference_line_info.mutable_path_data()->discretized_path().Evaluate(5.0);
120 AINFO << "stop_point_:" << stop_point_->x() << "," << stop_point_->y();
121 }
122 if (success_path_times_ > 10 && std::fabs(vehicle_state.linear_velocity()) < 0.1) {
123 AINFO << "already stopped change stage" << vehicle_state.linear_velocity() << " " << vehicle_state.x() << ", "
124 << vehicle_state.y();
125 return FinishStage();
126 }
128}
129
130StageResult ExtricateStage::FinishStage() {
131 next_stage_ = "SQUARE_LANE_FOLLOW_STAGE";
132 success_path_times_ = 0;
133 need_to_stop_ = false;
135}
136
138 auto obstacles_by_frame = frame->GetObstacleList();
139 common::math::Polygon2d adc_polygon(adc_box);
140 for (const auto& obstacle : obstacles_by_frame->Items()) {
141 if (adc_polygon.HasOverlap(obstacle->PerceptionPolygon())) {
142 return true;
143 }
144 }
145 return false;
146}
147
149 DiscretizedPath path;
150 std::vector<common::PathPoint> path_points;
151 static constexpr double kReversePathLength = 10.0;
152 static constexpr double kReversePathUnit = 0.5;
153 for (double s = 0; s <= kReversePathLength; s += kReversePathUnit) {
154 common::PathPoint point;
155 point.set_x(init_point.path_point().x() - s * std::cos(init_point.path_point().theta()));
156 point.set_y(init_point.path_point().y() - s * std::sin(init_point.path_point().theta()));
157 point.set_theta(init_point.path_point().theta());
158 point.set_s(s);
159 point.set_kappa(0.0);
160 point.set_dkappa(0.0);
161 point.set_ddkappa(0.0);
162 path.emplace_back(point);
163 }
164 path_data->SetDiscretizedPath(path);
165 path_data->set_path_label("reverse_straight_path");
166 path_data->set_is_reverse_path(true);
167 return true;
168}
169
170} // namespace planning
171} // namespace apollo
A general class to denote the return status of an API call.
Definition status.h:43
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
Implements a class of 2-dimensional vectors.
Definition vec2d.h:42
StageResult Process(const common::TrajectoryPoint &planning_init_point, Frame *frame) override
Each stage does its business logic inside Process function.
bool GenerateStraightReversePath(PathData *path_data, const common::TrajectoryPoint &init_point)
bool HasCollision(Frame *frame, const common::math::Box2d &adc_box)
bool Init(const StagePipeline &config, const std::shared_ptr< DependencyInjector > &injector, const std::string &config_dir, void *context)
Frame holds all data for one planning cycle.
Definition frame.h:62
const common::VehicleState & vehicle_state() const
Definition frame.cc:83
OpenSpaceInfo * mutable_open_space_info()
Definition frame.h:169
ThreadSafeIndexedObstacles * GetObstacleList()
Definition frame.h:165
std::list< ReferenceLineInfo > * mutable_reference_line_info()
Definition frame.cc:127
void set_is_on_open_space_trajectory(const bool flag)
std::pair< PublishableTrajectory, canbus::Chassis::GearPosition > * mutable_publishable_trajectory_data()
bool SetDiscretizedPath(DiscretizedPath path)
Definition path_data.cc:43
void set_is_reverse_path(bool is_reverse_path)
Definition path_data.h:114
void set_path_label(const std::string &label)
Definition path_data.cc:262
const StageResult & SetStageStatus(const StageStatusType &stage_status)
Set the stage status.
std::string next_stage_
Definition stage.h:89
virtual bool Init(const StagePipeline &config, const std::shared_ptr< DependencyInjector > &injector, const std::string &config_dir, void *context)
Definition stage.cc:43
std::shared_ptr< DependencyInjector > injector_
Definition stage.h:91
StageResult ExecuteTaskOnReferenceLine(const common::TrajectoryPoint &planning_start_point, Frame *frame)
Definition stage.cc:101
const std::string & Name() const
Definition stage.cc:99
Planning module main class.
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
int BuildStopDecision(const std::string &stop_wall_id, const double stop_line_s, const double stop_distance, const StopReasonCode &stop_reason_code, const std::vector< std::string > &wait_for_obstacles, const std::string &decision_tag, Frame *const frame, ReferenceLineInfo *const reference_line_info, double stop_wall_width)
Definition common.cc:28
class register implement
Definition arena_queue.h:37
optional apollo::canbus::Chassis::GearPosition gear