Apollo 10.0
自动驾驶开放平台
planning_component.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 *****************************************************************************/
17
18#include "cyber/common/file.h"
30namespace apollo {
31namespace planning {
32
36
43
45 injector_ = std::make_shared<DependencyInjector>();
46
47 if (FLAGS_use_navigation_mode) {
48 planning_base_ = std::make_unique<NaviPlanning>(injector_);
49 } else {
50 planning_base_ = std::make_unique<OnLanePlanning>(injector_);
51 }
52
54 << "failed to load planning config file "
56
57 if (FLAGS_planning_offline_learning ||
59 if (!message_process_.Init(config_, injector_)) {
60 AERROR << "failed to init MessageProcess";
61 return false;
62 }
63 }
64
65 planning_base_->Init(config_);
66
67 planning_command_reader_ = node_->CreateReader<PlanningCommand>(
69 [this](const std::shared_ptr<PlanningCommand>& planning_command) {
70 AINFO << "Received planning data: run planning callback."
71 << planning_command->header().DebugString();
72 std::lock_guard<std::mutex> lock(mutex_);
73 planning_command_.CopyFrom(*planning_command);
74 });
75
76 traffic_light_reader_ = node_->CreateReader<TrafficLightDetection>(
78 [this](const std::shared_ptr<TrafficLightDetection>& traffic_light) {
79 ADEBUG << "Received traffic light data: run traffic light callback.";
80 std::lock_guard<std::mutex> lock(mutex_);
81 traffic_light_.CopyFrom(*traffic_light);
82 });
83
84 pad_msg_reader_ = node_->CreateReader<PadMessage>(
86 [this](const std::shared_ptr<PadMessage>& pad_msg) {
87 ADEBUG << "Received pad data: run pad callback.";
88 std::lock_guard<std::mutex> lock(mutex_);
89 pad_msg_.CopyFrom(*pad_msg);
90 });
91
92 story_telling_reader_ = node_->CreateReader<Stories>(
94 [this](const std::shared_ptr<Stories>& stories) {
95 ADEBUG << "Received story_telling data: run story_telling callback.";
96 std::lock_guard<std::mutex> lock(mutex_);
97 stories_.CopyFrom(*stories);
98 });
99
100 control_interactive_reader_ = node_->CreateReader<ControlInteractiveMsg>(
102 [this](const std::shared_ptr<ControlInteractiveMsg>&
103 control_interactive_msg) {
104 ADEBUG << "Received story_telling data: run story_telling callback.";
105 std::lock_guard<std::mutex> lock(mutex_);
106 control_interactive_msg_.CopyFrom(*control_interactive_msg);
107 });
108
109 if (FLAGS_use_navigation_mode) {
110 relative_map_reader_ = node_->CreateReader<MapMsg>(
112 [this](const std::shared_ptr<MapMsg>& map_message) {
113 ADEBUG << "Received relative map data: run relative map callback.";
114 std::lock_guard<std::mutex> lock(mutex_);
115 relative_map_.CopyFrom(*map_message);
116 });
117 }
118 planning_writer_ = node_->CreateWriter<ADCTrajectory>(
120
121 rerouting_client_ =
125 planning_learning_data_writer_ = node_->CreateWriter<PlanningLearningData>(
127 command_status_writer_ = node_->CreateWriter<external_command::CommandStatus>(
128 FLAGS_planning_command_status);
129 return true;
130}
131
133 const std::shared_ptr<prediction::PredictionObstacles>&
134 prediction_obstacles,
135 const std::shared_ptr<canbus::Chassis>& chassis,
136 const std::shared_ptr<localization::LocalizationEstimate>&
137 localization_estimate) {
138 ACHECK(prediction_obstacles != nullptr);
139
140 // check and process possible rerouting request
141 CheckRerouting();
142
143 // process fused input data
144 local_view_.prediction_obstacles = prediction_obstacles;
145 local_view_.chassis = chassis;
146 local_view_.localization_estimate = localization_estimate;
147 {
148 std::lock_guard<std::mutex> lock(mutex_);
149 if (!local_view_.planning_command ||
150 !common::util::IsProtoEqual(local_view_.planning_command->header(),
151 planning_command_.header())) {
152 local_view_.planning_command =
153 std::make_shared<PlanningCommand>(planning_command_);
154 }
155 }
156 {
157 std::lock_guard<std::mutex> lock(mutex_);
158 local_view_.traffic_light =
159 std::make_shared<TrafficLightDetection>(traffic_light_);
160 local_view_.relative_map = std::make_shared<MapMsg>(relative_map_);
161 }
162 {
163 std::lock_guard<std::mutex> lock(mutex_);
164 if (!local_view_.pad_msg ||
165 !common::util::IsProtoEqual(local_view_.pad_msg->header(),
166 pad_msg_.header())) {
167 // Check if "CLEAR_PLANNING" PadMessage is received and process.
168 if (pad_msg_.action() == PadMessage::CLEAR_PLANNING) {
169 local_view_.planning_command = nullptr;
170 planning_command_.Clear();
171 }
172 local_view_.pad_msg = std::make_shared<PadMessage>(pad_msg_);
173 }
174 }
175 {
176 std::lock_guard<std::mutex> lock(mutex_);
177 local_view_.stories = std::make_shared<Stories>(stories_);
178 }
179 {
180 std::lock_guard<std::mutex> lock(mutex_);
181 if (!local_view_.control_interactive_msg ||
183 local_view_.control_interactive_msg->header(),
184 control_interactive_msg_.header())) {
185 local_view_.control_interactive_msg =
186 std::make_shared<ControlInteractiveMsg>(control_interactive_msg_);
187 }
188 }
189
190 if (!CheckInput()) {
191 AINFO << "Input check failed";
192 return false;
193 }
194
196 // data process for online training
197 message_process_.OnChassis(*local_view_.chassis);
198 message_process_.OnPrediction(*local_view_.prediction_obstacles);
199 if (local_view_.planning_command->has_lane_follow_command()) {
200 message_process_.OnRoutingResponse(
201 local_view_.planning_command->lane_follow_command());
202 }
203 message_process_.OnStoryTelling(*local_view_.stories);
204 message_process_.OnTrafficLightDetection(*local_view_.traffic_light);
205 message_process_.OnLocalization(*local_view_.localization_estimate);
206 }
207
208 // publish learning data frame for RL test
209 if (config_.learning_mode() == PlanningConfig::RL_TEST) {
210 PlanningLearningData planning_learning_data;
211 LearningDataFrame* learning_data_frame =
212 injector_->learning_based_data()->GetLatestLearningDataFrame();
213 if (learning_data_frame) {
214 planning_learning_data.mutable_learning_data_frame()->CopyFrom(
215 *learning_data_frame);
216 common::util::FillHeader(node_->Name(), &planning_learning_data);
217 planning_learning_data_writer_->Write(planning_learning_data);
218 } else {
219 AERROR << "fail to generate learning data frame";
220 return false;
221 }
222 return true;
223 }
224
225 ADCTrajectory adc_trajectory_pb;
226 planning_base_->RunOnce(local_view_, &adc_trajectory_pb);
227 auto start_time = adc_trajectory_pb.header().timestamp_sec();
228 common::util::FillHeader(node_->Name(), &adc_trajectory_pb);
229
230 SetLocation(&adc_trajectory_pb);
231 // modify trajectory relative time due to the timestamp change in header
232 const double dt = start_time - adc_trajectory_pb.header().timestamp_sec();
233 for (auto& p : *adc_trajectory_pb.mutable_trajectory_point()) {
234 p.set_relative_time(p.relative_time() + dt);
235 }
236 planning_writer_->Write(adc_trajectory_pb);
237
238 // Send command execution feedback.
239 // Error occured while executing the command.
240 external_command::CommandStatus command_status;
241 common::util::FillHeader(node_->Name(), &command_status);
242 if (nullptr != local_view_.planning_command) {
243 command_status.set_command_id(local_view_.planning_command->command_id());
244 }
245
246 ADCTrajectory::TrajectoryType current_trajectory_type =
247 adc_trajectory_pb.trajectory_type();
248 if (adc_trajectory_pb.header().status().error_code() !=
250 command_status.set_status(external_command::CommandStatusType::ERROR);
251 command_status.set_message(adc_trajectory_pb.header().status().msg());
252 } else if (planning_base_->IsPlanningFinished(current_trajectory_type)) {
253 AINFO << "Set the external_command: FINISHED";
254 command_status.set_status(external_command::CommandStatusType::FINISHED);
255 } else {
256 AINFO << "Set the external_command: RUNNING";
257 command_status.set_status(external_command::CommandStatusType::RUNNING);
258 }
259 command_status_writer_->Write(command_status);
260
261 // record in history
262 auto* history = injector_->history();
263 history->Add(adc_trajectory_pb);
264
265 return true;
266}
267
268void PlanningComponent::CheckRerouting() {
269 auto* rerouting = injector_->planning_context()
270 ->mutable_planning_status()
271 ->mutable_rerouting();
272 if (!rerouting->need_rerouting()) {
273 return;
274 }
275 common::util::FillHeader(node_->Name(),
276 rerouting->mutable_lane_follow_command());
277 auto lane_follow_command_ptr =
278 std::make_shared<apollo::external_command::LaneFollowCommand>(
279 rerouting->lane_follow_command());
280 rerouting_client_->SendRequest(lane_follow_command_ptr);
281 rerouting->set_need_rerouting(false);
282}
283
284bool PlanningComponent::CheckInput() {
285 ADCTrajectory trajectory_pb;
286
287 SetLocation(&trajectory_pb);
288 auto* not_ready = trajectory_pb.mutable_decision()
289 ->mutable_main_decision()
290 ->mutable_not_ready();
291
292 if (local_view_.localization_estimate == nullptr) {
293 not_ready->set_reason("localization not ready");
294 } else if (local_view_.chassis == nullptr) {
295 not_ready->set_reason("chassis not ready");
296 } else if (HDMapUtil::BaseMapPtr() == nullptr) {
297 not_ready->set_reason("map not ready");
298 } else {
299 // nothing
300 }
301
302 if (FLAGS_use_navigation_mode) {
303 if (!local_view_.relative_map->has_header()) {
304 not_ready->set_reason("relative map not ready");
305 }
306 } else {
307 if (!local_view_.planning_command ||
308 !local_view_.planning_command->has_header()) {
309 not_ready->set_reason("planning_command not ready");
310 }
311 }
312
313 if (not_ready->has_reason()) {
314 AINFO << not_ready->reason() << "; skip the planning cycle.";
315 common::util::FillHeader(node_->Name(), &trajectory_pb);
316 planning_writer_->Write(trajectory_pb);
317 return false;
318 }
319 return true;
320}
321
322void PlanningComponent::SetLocation(ADCTrajectory* const ptr_trajectory_pb) {
323 auto p = ptr_trajectory_pb->mutable_location_pose();
324 p->mutable_vehice_location()->set_x(
325 local_view_.localization_estimate->pose().position().x());
326 p->mutable_vehice_location()->set_y(
327 local_view_.localization_estimate->pose().position().y());
328 const Vec2d& adc_position = {
329 local_view_.localization_estimate->pose().position().x(),
330 local_view_.localization_estimate->pose().position().y()};
331 Vec2d left_point, right_point;
332 if (planning_base_->GenerateWidthOfLane(adc_position, left_point,
333 right_point)) {
334 p->mutable_left_lane_boundary_point()->set_x(left_point.x());
335 p->mutable_left_lane_boundary_point()->set_y(left_point.y());
336 p->mutable_right_lane_boundary_point()->set_x(right_point.x());
337 p->mutable_right_lane_boundary_point()->set_y(right_point.y());
338 }
339}
340
341} // namespace planning
342} // namespace apollo
Implements a class of 2-dimensional vectors.
Definition vec2d.h:42
bool GetProtoConfig(T *config) const
const std::string & ConfigFilePath() const
std::shared_ptr< Node > node_
static const HDMap * BaseMapPtr()
bool Init(const PlanningConfig &planning_config)
void OnPrediction(const apollo::prediction::PredictionObstacles &prediction_obstacles)
void OnStoryTelling(const apollo::storytelling::Stories &stories)
void OnRoutingResponse(const apollo::routing::RoutingResponse &routing_response)
void OnLocalization(const apollo::localization::LocalizationEstimate &le)
void OnTrafficLightDetection(const apollo::perception::TrafficLightDetection &traffic_light_detection)
void OnChassis(const apollo::canbus::Chassis &chassis)
bool Proc(const std::shared_ptr< prediction::PredictionObstacles > &prediction_obstacles, const std::shared_ptr< canbus::Chassis > &chassis, const std::shared_ptr< localization::LocalizationEstimate > &localization_estimate) override
Planning module main class.
#define ACHECK(cond)
Definition log.h:80
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
Some string util functions.
Some util functions.
bool IsProtoEqual(const ProtoA &a, const ProtoB &b)
Definition util.h:47
class register implement
Definition arena_queue.h:37
optional double timestamp_sec
Definition header.proto:9
optional StatusPb status
Definition header.proto:30
optional ErrorCode error_code
optional TrajectoryType trajectory_type
optional apollo::common::Header header
std::shared_ptr< PadMessage > pad_msg
Definition local_view.h:46
std::shared_ptr< localization::LocalizationEstimate > localization_estimate
Definition local_view.h:43
std::shared_ptr< PlanningCommand > planning_command
Definition local_view.h:48
std::shared_ptr< relative_map::MapMsg > relative_map
Definition local_view.h:45
std::shared_ptr< control::ControlInteractiveMsg > control_interactive_msg
Definition local_view.h:50
std::shared_ptr< prediction::PredictionObstacles > prediction_obstacles
Definition local_view.h:41
std::shared_ptr< storytelling::Stories > stories
Definition local_view.h:47
std::shared_ptr< canbus::Chassis > chassis
Definition local_view.h:42
std::shared_ptr< perception::TrafficLightDetection > traffic_light
Definition local_view.h:44
optional apollo::common::Header header
Definition pad_msg.proto:8
optional DrivingAction action
Definition pad_msg.proto:26
optional apollo::common::Header header
optional PlanningLearningMode learning_mode
optional string traffic_light_detection_topic
Defines the Vec2d class.