赞
踩
apollo9.0的planning的代码解析
9.0的planning的文件结构
Planning模块的入口为
/modules/planning/planning_component/planning_component.h
/modules/planning/planning_component/planning_component.cc
模块的接口为 PlanningComponent类,该类不可被继承。
在Init函数中:
injector_ = std::make_shared<DependencyInjector>(); //构造智能指针
planning_base_ = std::make_unique<NaviPlanning>(injector_);
planning_base_ = std::make_unique<OnLanePlanning>(injector_); //构造出智能指针planning_base_ 类型为OnLanePlanning
将config参数传到OnLanePlanning的对象中:
planning_base_->Init(config_);
进行OnLanePlanning的init
Status OnLanePlanning::Init(const PlanningConfig& config) { if (!CheckPlanningConfig(config)) { return Status(ErrorCode::PLANNING_ERROR, "planning config error: " + config_.DebugString()); }// PlanningBase::Init(con加载配置文件fig_);//planning_context初始化 // clear planning history injector_->history()->Clear(); // clear planning status injector_->planning_context()->mutable_planning_status()->Clear(); // load map hdmap_ = HDMapUtil::BaseMapPtr(); ACHECK(hdmap_) << "Failed to load map"; // instantiate reference line provider const ReferenceLineConfig* reference_line_config = nullptr; if (config_.has_reference_line_config()) { reference_line_config = &config_.reference_line_config(); } reference_line_provider_ = std::make_unique<ReferenceLineProvider>( injector_->vehicle_state(), reference_line_config); reference_line_provider_->Start();//启动重要的函数,参考线相关的操作均由该函数开始 // dispatch planner LoadPlanner();//加载规划器 if (!planner_) { return Status( ErrorCode::PLANNING_ERROR, "planning is not initialized with config : " + config_.DebugString()); } if (config_.learning_mode() != PlanningConfig::NO_LEARNING) {//学习模型相关的 PlanningSemanticMapConfig renderer_config; ACHECK(apollo::cyber::common::GetProtoFromFile( FLAGS_planning_birdview_img_feature_renderer_config_file, &renderer_config)) << "Failed to load renderer config" << FLAGS_planning_birdview_img_feature_renderer_config_file; BirdviewImgFeatureRenderer::Instance()->Init(renderer_config); } traffic_decider_.Init(injector_);//decider初始化 start_time_ = Clock::NowInSeconds(); return planner_->Init(injector_, FLAGS_planner_config_path);//planner初始化 }
然后进行Proc主程序:
bool PlanningComponent::Proc(const std::shared_ptr<prediction::PredictionObstacles>& prediction_obstacles, const std::shared_ptr<canbus::Chassis>& chassis,const std::shared_ptr<localization::LocalizationEstimate>& localization_estimate) {
// check and process possible rerouting request /** *
检查是否需要重新规划线路。 * **/
// 步骤1
CheckRerouting();
再进行local_view_赋值,保存外部变量
需要注意的是9.0引入的planning_command
{
std::lock_guard<std::mutex> lock(mutex_);
if (!local_view_.planning_command ||
!common::util::IsProtoEqual(local_view_.planning_command->header(),
planning_command_.header())) {
local_view_.planning_command =
std::make_shared<PlanningCommand>(planning_command_);
}
}
切入规划器,修改轨迹相对时间
planning_base_->RunOnce(local_view_, &adc_trajectory_pb);
auto start_time = adc_trajectory_pb.header().timestamp_sec();
common::util::FillHeader(node_->Name(), &adc_trajectory_pb);
// modify trajectory relative time due to the timestamp change in header
const double dt = start_time - adc_trajectory_pb.header().timestamp_sec();
for (auto& p : *adc_trajectory_pb.mutable_trajectory_point()) {
p.set_relative_time(p.relative_time() + dt);
}
planning_writer_->Write(adc_trajectory_pb);
RunOnce函数在class OnLanePlanning 中被实例化
void OnLanePlanning::RunOnce(const LocalView& local_view, ADCTrajectory* const ptr_trajectory_pb) { // when rerouting, reference line might not be updated. In this case, planning // module maintains not-ready until be restarted. local_view_ = local_view; const double start_timestamp = Clock::NowInSeconds(); const double start_system_timestamp = std::chrono::duration<double>( std::chrono::system_clock::now().time_since_epoch()) .count(); // localization ADEBUG << "Get localization:" << local_view_.localization_estimate->DebugString(); // chassis ADEBUG << "Get chassis:" << local_view_.chassis->DebugString(); Status status = injector_->vehicle_state()->Update( *local_view_.localization_estimate, *local_view_.chassis); VehicleState vehicle_state = injector_->vehicle_state()->vehicle_state(); const double vehicle_state_timestamp = vehicle_state.timestamp(); DCHECK_GE(start_timestamp, vehicle_state_timestamp) << "start_timestamp is behind vehicle_state_timestamp by " << start_timestamp - vehicle_state_timestamp << " secs"; if (!status.ok() || !util::IsVehicleStateValid(vehicle_state)) { const std::string msg = "Update VehicleStateProvider failed " "or the vehicle state is out dated."; AERROR << msg; ptr_trajectory_pb->mutable_decision() ->mutable_main_decision() ->mutable_not_ready() ->set_reason(msg); status.Save(ptr_trajectory_pb->mutable_header()->mutable_status()); // TODO(all): integrate reverse gear ptr_trajectory_pb->set_gear(canbus::Chassis::GEAR_DRIVE); FillPlanningPb(start_timestamp, ptr_trajectory_pb); GenerateStopTrajectory(ptr_trajectory_pb); return; } if (start_timestamp + 1e-6 < vehicle_state_timestamp) { common::monitor::MonitorLogBuffer monitor_logger_buffer( common::monitor::MonitorMessageItem::PLANNING); monitor_logger_buffer.ERROR("ego system time is behind GPS time"); } if (start_timestamp - vehicle_state_timestamp < FLAGS_message_latency_threshold) { vehicle_state = AlignTimeStamp(vehicle_state, start_timestamp); } // Update reference line provider and reset scenario if new routing reference_line_provider_->UpdateVehicleState(vehicle_state); if (local_view_.planning_command->is_motion_command() && util::IsDifferentRouting(last_command_, *local_view_.planning_command)) { last_command_ = *local_view_.planning_command; AINFO << "new_command:" << last_command_.DebugString(); reference_line_provider_->Reset(); injector_->history()->Clear(); injector_->planning_context()->mutable_planning_status()->Clear(); reference_line_provider_->UpdatePlanningCommand( *(local_view_.planning_command)); planner_->Reset(frame_.get()); } // Get end lane way point. reference_line_provider_->GetEndLaneWayPoint(local_view_.end_lane_way_point); // planning is triggered by prediction data, but we can still use an estimated // cycle time for stitching const double planning_cycle_time = 1.0 / static_cast<double>(FLAGS_planning_loop_rate); std::string replan_reason; std::vector<TrajectoryPoint> stitching_trajectory = TrajectoryStitcher::ComputeStitchingTrajectory( *(local_view_.chassis), vehicle_state, start_timestamp, planning_cycle_time, FLAGS_trajectory_stitching_preserved_length, true, last_publishable_trajectory_.get(), &replan_reason); injector_->ego_info()->Update(stitching_trajectory.back(), vehicle_state); const uint32_t frame_num = static_cast<uint32_t>(seq_num_++); status = InitFrame(frame_num, stitching_trajectory.back(), vehicle_state); AINFO << "Planning start frame sequence id = [" << frame_num << "]"; if (status.ok()) { injector_->ego_info()->CalculateFrontObstacleClearDistance( frame_->obstacles()); } if (FLAGS_enable_record_debug) { frame_->RecordInputDebug(ptr_trajectory_pb->mutable_debug()); } ptr_trajectory_pb->mutable_latency_stats()->set_init_frame_time_ms( Clock::NowInSeconds() - start_timestamp); if (!status.ok()) { AERROR << status.ToString(); if (FLAGS_publish_estop) { // "estop" signal check in function "Control::ProduceControlCommand()" // estop_ = estop_ || local_view_.trajectory.estop().is_estop(); // we should add more information to ensure the estop being triggered. ADCTrajectory estop_trajectory; EStop* estop = estop_trajectory.mutable_estop(); estop->set_is_estop(true); estop->set_reason(status.error_message()); status.Save(estop_trajectory.mutable_header()->mutable_status()); ptr_trajectory_pb->CopyFrom(estop_trajectory); } else { ptr_trajectory_pb->mutable_decision() ->mutable_main_decision() ->mutable_not_ready() ->set_reason(status.ToString()); status.Save(ptr_trajectory_pb->mutable_header()->mutable_status()); GenerateStopTrajectory(ptr_trajectory_pb); } // TODO(all): integrate reverse gear ptr_trajectory_pb->set_gear(canbus::Chassis::GEAR_DRIVE); FillPlanningPb(start_timestamp, ptr_trajectory_pb); frame_->set_current_frame_planned_trajectory(*ptr_trajectory_pb); const uint32_t n = frame_->SequenceNum(); injector_->frame_history()->Add(n, std::move(frame_)); return; } for (auto& ref_line_info : *frame_->mutable_reference_line_info()) { auto traffic_status = traffic_decider_.Execute(frame_.get(), &ref_line_info); if (!traffic_status.ok() || !ref_line_info.IsDrivable()) { ref_line_info.SetDrivable(false); AWARN << "Reference line " << ref_line_info.Lanes().Id() << " traffic decider failed"; } } status = Plan(start_timestamp, stitching_trajectory, ptr_trajectory_pb); PrintCurves print_curve; for (const auto& p : ptr_trajectory_pb->trajectory_point()) { print_curve.AddPoint("trajxy", p.path_point().x(), p.path_point().y()); } print_curve.PrintToLog(); const auto end_system_timestamp = std::chrono::duration<double>( std::chrono::system_clock::now().time_since_epoch()) .count(); const auto time_diff_ms = (end_system_timestamp - start_system_timestamp) * 1000; ADEBUG << "total planning time spend: " << time_diff_ms << " ms."; ptr_trajectory_pb->mutable_latency_stats()->set_total_time_ms(time_diff_ms); ADEBUG << "Planning latency: " << ptr_trajectory_pb->latency_stats().DebugString(); if (!status.ok()) { status.Save(ptr_trajectory_pb->mutable_header()->mutable_status()); AERROR << "Planning failed:" << status.ToString(); if (FLAGS_publish_estop) { AERROR << "Planning failed and set estop"; // "estop" signal check in function "Control::ProduceControlCommand()" // estop_ = estop_ || local_view_.trajectory.estop().is_estop(); // we should add more information to ensure the estop being triggered. EStop* estop = ptr_trajectory_pb->mutable_estop(); estop->set_is_estop(true); estop->set_reason(status.error_message()); } } ptr_trajectory_pb->set_is_replan(stitching_trajectory.size() == 1); if (ptr_trajectory_pb->is_replan()) { ptr_trajectory_pb->set_replan_reason(replan_reason); } if (frame_->open_space_info().is_on_open_space_trajectory()) { FillPlanningPb(start_timestamp, ptr_trajectory_pb); ADEBUG << "Planning pb:" << ptr_trajectory_pb->header().DebugString(); frame_->set_current_frame_planned_trajectory(*ptr_trajectory_pb); } else { auto* ref_line_task = ptr_trajectory_pb->mutable_latency_stats()->add_task_stats(); ref_line_task->set_time_ms(reference_line_provider_->LastTimeDelay() * 1000.0); ref_line_task->set_name("ReferenceLineProvider"); // TODO(all): integrate reverse gear ptr_trajectory_pb->set_gear(canbus::Chassis::GEAR_DRIVE); FillPlanningPb(start_timestamp, ptr_trajectory_pb); ADEBUG << "Planning pb:" << ptr_trajectory_pb->header().DebugString(); frame_->set_current_frame_planned_trajectory(*ptr_trajectory_pb); if (FLAGS_enable_planning_smoother) { planning_smoother_.Smooth(injector_->frame_history(), frame_.get(), ptr_trajectory_pb); } } const uint32_t n = frame_->SequenceNum(); AINFO << "Planning end frame sequence id = [" << n << "]"; injector_->frame_history()->Add(n, std::move(frame_)); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。