main logic of the planning module, runs periodically triggered by timer.
233 {
234
235
238 const double start_system_timestamp =
239 std::chrono::duration<double>(
240 std::chrono::system_clock::now().time_since_epoch())
241 .count();
242
243
244 ADEBUG <<
"Get localization:"
246
247
249
252
253 VehicleState vehicle_state =
injector_->vehicle_state()->vehicle_state();
254 const double vehicle_state_timestamp = vehicle_state.timestamp();
255 DCHECK_GE(start_timestamp, vehicle_state_timestamp)
256 << "start_timestamp is behind vehicle_state_timestamp by "
257 << start_timestamp - vehicle_state_timestamp << " secs";
258
260 const std::string msg =
261 "Update VehicleStateProvider failed "
262 "or the vehicle state is out dated.";
264 ptr_trajectory_pb->mutable_decision()
265 ->mutable_main_decision()
266 ->mutable_not_ready()
267 ->set_reason(msg);
268 status.Save(ptr_trajectory_pb->mutable_header()->mutable_status());
269
272 GenerateStopTrajectory(ptr_trajectory_pb);
273 return;
274 }
275
276 if (start_timestamp + 1e-6 < vehicle_state_timestamp) {
277 common::monitor::MonitorLogBuffer monitor_logger_buffer(
279 monitor_logger_buffer.ERROR("ego system time is behind GPS time");
280 }
281
282 if (start_timestamp - vehicle_state_timestamp <
283 FLAGS_message_latency_threshold) {
284 vehicle_state = AlignTimeStamp(vehicle_state, start_timestamp);
285 }
286
287
288 reference_line_provider_->UpdateVehicleState(vehicle_state);
292
293 reference_line_provider_->Reset();
295 injector_->planning_context()->mutable_planning_status()->Clear();
296 reference_line_provider_->UpdatePlanningCommand(
299 }
300
302
303
304
305 const double planning_cycle_time =
306 1.0 / static_cast<double>(FLAGS_planning_loop_rate);
307
308 std::string replan_reason;
309 std::vector<TrajectoryPoint> stitching_trajectory =
312 planning_cycle_time, FLAGS_trajectory_stitching_preserved_length,
315
316 injector_->ego_info()->Update(stitching_trajectory.back(), vehicle_state);
317 const uint32_t frame_num =
static_cast<uint32_t
>(
seq_num_++);
318 AINFO <<
"Planning start frame sequence id = [" << frame_num <<
"]";
319 status = InitFrame(frame_num, stitching_trajectory.back(), vehicle_state);
320 if (status.ok()) {
321 injector_->ego_info()->CalculateFrontObstacleClearDistance(
323 injector_->ego_info()->CalculateCurrentRouteInfo(
324 reference_line_provider_.get());
325 }
326
327 if (FLAGS_enable_record_debug) {
328 frame_->RecordInputDebug(ptr_trajectory_pb->mutable_debug());
329 }
330 ptr_trajectory_pb->mutable_latency_stats()->set_init_frame_time_ms(
332
333 if (!status.ok()) {
334 AERROR << status.ToString();
335 if (FLAGS_publish_estop) {
336
337
338
339 ADCTrajectory estop_trajectory;
340 EStop* estop = estop_trajectory.mutable_estop();
341 estop->set_is_estop(true);
342 estop->set_reason(status.error_message());
343 status.Save(estop_trajectory.mutable_header()->mutable_status());
344 ptr_trajectory_pb->CopyFrom(estop_trajectory);
345 } else {
346 ptr_trajectory_pb->mutable_decision()
347 ->mutable_main_decision()
348 ->mutable_not_ready()
349 ->set_reason(status.ToString());
350 status.Save(ptr_trajectory_pb->mutable_header()->mutable_status());
351 GenerateStopTrajectory(ptr_trajectory_pb);
352 }
353
356 frame_->set_current_frame_planned_trajectory(*ptr_trajectory_pb);
357 const uint32_t n =
frame_->SequenceNum();
359 return;
360 }
361
362 for (
auto& ref_line_info : *
frame_->mutable_reference_line_info()) {
363 auto traffic_status =
365 if (!traffic_status.ok() || !ref_line_info.IsDrivable()) {
366 ref_line_info.SetDrivable(false);
367 AWARN <<
"Reference line " << ref_line_info.Lanes().Id()
368 << " traffic decider failed";
369 }
370 }
371
372 status =
Plan(start_timestamp, stitching_trajectory, ptr_trajectory_pb);
373
374
375 PrintCurves trajectory_print_curve;
376 for (const auto& p : ptr_trajectory_pb->trajectory_point()) {
377 trajectory_print_curve.AddPoint("trajxy", p.path_point().x(),
378 p.path_point().y());
379 }
380 trajectory_print_curve.PrintToLog();
381
382
383 for (
const auto& obstacle :
frame_->obstacles()) {
384 obstacle->PrintPolygonCurve();
385 }
386
387 PrintBox print_box("ego_box");
388 print_box.AddAdcBox(vehicle_state.x(), vehicle_state.y(),
389 vehicle_state.heading(), true);
390 print_box.PrintToLog();
391
392 const auto end_system_timestamp =
393 std::chrono::duration<double>(
394 std::chrono::system_clock::now().time_since_epoch())
395 .count();
396 const auto time_diff_ms =
397 (end_system_timestamp - start_system_timestamp) * 1000;
398 ADEBUG <<
"total planning time spend: " << time_diff_ms <<
" ms.";
399
400 ptr_trajectory_pb->mutable_latency_stats()->set_total_time_ms(time_diff_ms);
401 ADEBUG <<
"Planning latency: "
402 << ptr_trajectory_pb->latency_stats().DebugString();
403
404 if (!status.ok()) {
405 status.Save(ptr_trajectory_pb->mutable_header()->mutable_status());
406 AERROR <<
"Planning failed:" << status.ToString();
407 if (FLAGS_publish_estop) {
408 AERROR <<
"Planning failed and set estop";
409
410
411
412 EStop* estop = ptr_trajectory_pb->mutable_estop();
413 estop->set_is_estop(true);
414 estop->set_reason(status.error_message());
415 }
416 }
417
418 ptr_trajectory_pb->set_is_replan(stitching_trajectory.size() == 1);
419 if (ptr_trajectory_pb->is_replan()) {
420 ptr_trajectory_pb->set_replan_reason(replan_reason);
421 }
422
423 if (
frame_->open_space_info().is_on_open_space_trajectory()) {
425 ADEBUG <<
"Planning pb:" << ptr_trajectory_pb->header().DebugString();
426 frame_->set_current_frame_planned_trajectory(*ptr_trajectory_pb);
427 } else {
428 auto* ref_line_task =
429 ptr_trajectory_pb->mutable_latency_stats()->add_task_stats();
430 ref_line_task->set_time_ms(reference_line_provider_->LastTimeDelay() *
431 1000.0);
432 ref_line_task->set_name("ReferenceLineProvider");
433
435 ADEBUG <<
"Planning pb:" << ptr_trajectory_pb->header().DebugString();
436
437 frame_->set_current_frame_planned_trajectory(*ptr_trajectory_pb);
438 if (FLAGS_enable_planning_smoother) {
440 ptr_trajectory_pb);
441 }
442 }
443
444 const auto end_planning_perf_timestamp =
445 std::chrono::duration<double>(
446 std::chrono::system_clock::now().time_since_epoch())
447 .count();
448 const auto plnning_perf_ms =
449 (end_planning_perf_timestamp - start_system_timestamp) * 1000;
450 AINFO <<
"Planning Perf: planning name [" <<
Name() <<
"], "
451 << plnning_perf_ms << " ms.";
452 AINFO <<
"Planning end frame sequence id = [" << frame_num <<
"]";
454}
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