main logic of the planning module, runs periodically triggered by timer.
232 {
233
234
237 const double start_system_timestamp =
238 std::chrono::duration<double>(
239 std::chrono::system_clock::now().time_since_epoch())
240 .count();
241
242
243 ADEBUG <<
"Get localization:"
245
246
248
251
252 VehicleState vehicle_state =
injector_->vehicle_state()->vehicle_state();
253 const double vehicle_state_timestamp = vehicle_state.timestamp();
254 DCHECK_GE(start_timestamp, vehicle_state_timestamp)
255 << "start_timestamp is behind vehicle_state_timestamp by "
256 << start_timestamp - vehicle_state_timestamp << " secs";
257
259 const std::string msg =
260 "Update VehicleStateProvider failed "
261 "or the vehicle state is out dated.";
263 ptr_trajectory_pb->mutable_decision()
264 ->mutable_main_decision()
265 ->mutable_not_ready()
266 ->set_reason(msg);
267 status.Save(ptr_trajectory_pb->mutable_header()->mutable_status());
268
271 GenerateStopTrajectory(ptr_trajectory_pb);
272 return;
273 }
274
275 if (start_timestamp + 1e-6 < vehicle_state_timestamp) {
276 common::monitor::MonitorLogBuffer monitor_logger_buffer(
278 monitor_logger_buffer.ERROR("ego system time is behind GPS time");
279 }
280
281 if (start_timestamp - vehicle_state_timestamp <
282 FLAGS_message_latency_threshold) {
283 vehicle_state = AlignTimeStamp(vehicle_state, start_timestamp);
284 }
285
286
287 reference_line_provider_->UpdateVehicleState(vehicle_state);
291
292 reference_line_provider_->Reset();
294 injector_->planning_context()->mutable_planning_status()->Clear();
295 reference_line_provider_->UpdatePlanningCommand(
298 }
299
301
302
303
304 const double planning_cycle_time =
305 1.0 / static_cast<double>(FLAGS_planning_loop_rate);
306
307 std::string replan_reason;
308 std::vector<TrajectoryPoint> stitching_trajectory =
311 planning_cycle_time, FLAGS_trajectory_stitching_preserved_length,
314
315 injector_->ego_info()->Update(stitching_trajectory.back(), vehicle_state);
316 const uint32_t frame_num =
static_cast<uint32_t
>(
seq_num_++);
317 AINFO <<
"Planning start frame sequence id = [" << frame_num <<
"]";
318 status = InitFrame(frame_num, stitching_trajectory.back(), vehicle_state);
319 if (status.ok()) {
320 injector_->ego_info()->CalculateFrontObstacleClearDistance(
322 injector_->ego_info()->CalculateCurrentRouteInfo(
323 reference_line_provider_.get());
324 }
325
326 if (FLAGS_enable_record_debug) {
327 frame_->RecordInputDebug(ptr_trajectory_pb->mutable_debug());
328 }
329 ptr_trajectory_pb->mutable_latency_stats()->set_init_frame_time_ms(
331
332 if (!status.ok()) {
333 AERROR << status.ToString();
334 if (FLAGS_publish_estop) {
335
336
337
338 ADCTrajectory estop_trajectory;
339 EStop* estop = estop_trajectory.mutable_estop();
340 estop->set_is_estop(true);
341 estop->set_reason(status.error_message());
342 status.Save(estop_trajectory.mutable_header()->mutable_status());
343 ptr_trajectory_pb->CopyFrom(estop_trajectory);
344 } else {
345 ptr_trajectory_pb->mutable_decision()
346 ->mutable_main_decision()
347 ->mutable_not_ready()
348 ->set_reason(status.ToString());
349 status.Save(ptr_trajectory_pb->mutable_header()->mutable_status());
350 GenerateStopTrajectory(ptr_trajectory_pb);
351 }
352
355 frame_->set_current_frame_planned_trajectory(*ptr_trajectory_pb);
356 const uint32_t n =
frame_->SequenceNum();
358 return;
359 }
360
361 for (
auto& ref_line_info : *
frame_->mutable_reference_line_info()) {
362 auto traffic_status =
364 if (!traffic_status.ok() || !ref_line_info.IsDrivable()) {
365 ref_line_info.SetDrivable(false);
366 AWARN <<
"Reference line " << ref_line_info.Lanes().Id()
367 << " traffic decider failed";
368 }
369 }
370
371 status =
Plan(start_timestamp, stitching_trajectory, ptr_trajectory_pb);
372
373
374 PrintCurves trajectory_print_curve;
375 for (const auto& p : ptr_trajectory_pb->trajectory_point()) {
376 trajectory_print_curve.AddPoint("trajxy", p.path_point().x(),
377 p.path_point().y());
378 }
379 trajectory_print_curve.PrintToLog();
380
381
382 for (
const auto& obstacle :
frame_->obstacles()) {
383 obstacle->PrintPolygonCurve();
384 }
385
386 PrintBox print_box("ego_box");
387 print_box.AddAdcBox(vehicle_state.x(), vehicle_state.y(),
388 vehicle_state.heading(), true);
389 print_box.PrintToLog();
390
391 const auto end_system_timestamp =
392 std::chrono::duration<double>(
393 std::chrono::system_clock::now().time_since_epoch())
394 .count();
395 const auto time_diff_ms =
396 (end_system_timestamp - start_system_timestamp) * 1000;
397 ADEBUG <<
"total planning time spend: " << time_diff_ms <<
" ms.";
398
399 ptr_trajectory_pb->mutable_latency_stats()->set_total_time_ms(time_diff_ms);
400 ADEBUG <<
"Planning latency: "
401 << ptr_trajectory_pb->latency_stats().DebugString();
402
403 if (!status.ok()) {
404 status.Save(ptr_trajectory_pb->mutable_header()->mutable_status());
405 AERROR <<
"Planning failed:" << status.ToString();
406 if (FLAGS_publish_estop) {
407 AERROR <<
"Planning failed and set estop";
408
409
410
411 EStop* estop = ptr_trajectory_pb->mutable_estop();
412 estop->set_is_estop(true);
413 estop->set_reason(status.error_message());
414 }
415 }
416
417 ptr_trajectory_pb->set_is_replan(stitching_trajectory.size() == 1);
418 if (ptr_trajectory_pb->is_replan()) {
419 ptr_trajectory_pb->set_replan_reason(replan_reason);
420 }
421
422 if (
frame_->open_space_info().is_on_open_space_trajectory()) {
424 ADEBUG <<
"Planning pb:" << ptr_trajectory_pb->header().DebugString();
425 frame_->set_current_frame_planned_trajectory(*ptr_trajectory_pb);
426 } else {
427 auto* ref_line_task =
428 ptr_trajectory_pb->mutable_latency_stats()->add_task_stats();
429 ref_line_task->set_time_ms(reference_line_provider_->LastTimeDelay() *
430 1000.0);
431 ref_line_task->set_name("ReferenceLineProvider");
432
434 ADEBUG <<
"Planning pb:" << ptr_trajectory_pb->header().DebugString();
435
436 frame_->set_current_frame_planned_trajectory(*ptr_trajectory_pb);
437 if (FLAGS_enable_planning_smoother) {
439 ptr_trajectory_pb);
440 }
441 }
442
443 const auto end_planning_perf_timestamp =
444 std::chrono::duration<double>(
445 std::chrono::system_clock::now().time_since_epoch())
446 .count();
447 const auto plnning_perf_ms =
448 (end_planning_perf_timestamp - start_system_timestamp) * 1000;
449 AINFO <<
"Planning Perf: planning name [" <<
Name() <<
"], "
450 << plnning_perf_ms << " ms.";
451 AINFO <<
"Planning end frame sequence id = [" << frame_num <<
"]";
453}
common::Status Plan(const double current_time_stamp, const std::vector< common::TrajectoryPoint > &stitching_trajectory, ADCTrajectory *const trajectory) override
Plan the trajectory given current vehicle state
std::string Name() const override
Planning name.
virtual void FillPlanningPb(const double timestamp, ADCTrajectory *const trajectory_pb)
apollo::common::Status Smooth(const FrameHistory *frame_history, const Frame *current_frame, ADCTrajectory *const current_trajectory_pb)
apollo::common::Status Execute(Frame *frame, ReferenceLineInfo *reference_line_info)
static std::vector< common::TrajectoryPoint > ComputeStitchingTrajectory(const canbus::Chassis &vehicle_chassis, const common::VehicleState &vehicle_state, const double current_timestamp, const double planning_cycle_time, const size_t preserved_points_num, const bool replan_by_offset, const PublishableTrajectory *prev_trajectory, std::string *replan_reason, const control::ControlInteractiveMsg &control_interactive_msg)
bool IsVehicleStateValid(const VehicleState &vehicle_state)
bool IsDifferentRouting(const PlanningCommand &first, const PlanningCommand &second)
std::shared_ptr< routing::LaneWaypoint > end_lane_way_point
std::shared_ptr< localization::LocalizationEstimate > localization_estimate
std::shared_ptr< PlanningCommand > planning_command
std::shared_ptr< control::ControlInteractiveMsg > control_interactive_msg
std::shared_ptr< canbus::Chassis > chassis
LocalView contains all necessary data as planning input