From 759d203f82abcaebba816bcda6b372cf53c01a45 Mon Sep 17 00:00:00 2001 From: Dong Li Date: Wed, 2 Aug 2017 00:01:15 -0700 Subject: [PATCH] move ObjectTable out of DataCenter * prepare for further refactor ObjectTable and DataCenter --- modules/planning/common/BUILD | 19 +---- modules/planning/common/data_center.cc | 84 ------------------- modules/planning/common/data_center.h | 28 +------ modules/planning/common/frame.cc | 3 + modules/planning/common/frame.h | 8 ++ modules/planning/common/indexed_list.h | 6 +- modules/planning/common/object_table.h | 40 --------- modules/planning/optimizer/dp_st_speed/BUILD | 1 + .../optimizer/dp_st_speed/dp_st_graph.cc | 10 +-- .../optimizer/dp_st_speed/dp_st_graph.h | 9 +- .../dp_st_speed/dp_st_speed_optimizer.cc | 5 +- 11 files changed, 38 insertions(+), 175 deletions(-) delete mode 100644 modules/planning/common/data_center.cc delete mode 100644 modules/planning/common/object_table.h diff --git a/modules/planning/common/BUILD b/modules/planning/common/BUILD index 543c39f264..c65db05e36 100644 --- a/modules/planning/common/BUILD +++ b/modules/planning/common/BUILD @@ -23,14 +23,9 @@ cc_library( ) cc_library( - name = "object_table", + name = "indexed_queue", hdrs = [ - "object_table.h", - ], - deps = [ - ":indexed_list", - ":obstacle", - ":planning_gflags", + "indexed_queue.h", ], ) @@ -106,6 +101,7 @@ cc_library( "frame.h", ], deps = [ + ":indexed_list", "//modules/common:log", "//modules/common/adapters:adapter_manager", "//modules/common/vehicle_state", @@ -146,20 +142,13 @@ cc_library( cc_library( name = "data_center", - srcs = [ - "data_center.cc", - ], hdrs = [ "data_center.h", ], deps = [ ":frame", - ":object_table", - ":planning_gflags", + ":indexed_queue", "//modules/common", - "//modules/common/adapters:adapter_manager", - "//modules/common/status", - "//modules/map/hdmap", ], ) diff --git a/modules/planning/common/data_center.cc b/modules/planning/common/data_center.cc deleted file mode 100644 index b4b9d39b7a..0000000000 --- a/modules/planning/common/data_center.cc +++ /dev/null @@ -1,84 +0,0 @@ -/****************************************************************************** - * Copyright 2017 The Apollo Authors. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *****************************************************************************/ - -/** - * @file: - **/ - -#include "modules/planning/common/data_center.h" - -#include -#include -#include - -#include "modules/map/proto/map_id.pb.h" - -#include "google/protobuf/text_format.h" -#include "modules/common/adapters/adapter_manager.h" -#include "modules/common/log.h" -#include "modules/common/util/util.h" -#include "modules/planning/common/planning_gflags.h" - -namespace apollo { -namespace planning { - -using apollo::common::Status; -using apollo::common::math::Vec2d; -using apollo::common::adapter::AdapterManager; - -DataCenter::DataCenter() { - _object_table.reset(new ObjectTable()); - - AINFO << "Data Center is ready!"; -} - -Frame *DataCenter::frame(const uint32_t sequence_num) const { - std::unordered_map>::const_iterator it = - _frames.find(sequence_num); - if (it != _frames.end()) { - return it->second.get(); - } - return nullptr; -} - -void DataCenter::save_frame() { - _sequence_queue.push_back(_frame->sequence_num()); - _frames[_frame->sequence_num()] = std::move(_frame); - if (_sequence_queue.size() > - static_cast(FLAGS_max_history_result)) { - _frames.erase(_sequence_queue.front()); - _sequence_queue.pop_front(); - } -} - -const Frame *DataCenter::last_frame() const { - if (_sequence_queue.empty()) { - return nullptr; - } - uint32_t sequence_num = _sequence_queue.back(); - return _frames.find(sequence_num)->second.get(); -} - -ObjectTable *DataCenter::mutable_object_table() const { - return _object_table.get(); -} - -const ObjectTable &DataCenter::object_table() const { - return *(_object_table.get()); -} - -} // namespace planning -} // namespace apollo diff --git a/modules/planning/common/data_center.h b/modules/planning/common/data_center.h index 1addfc7ca0..7ae81f67e1 100644 --- a/modules/planning/common/data_center.h +++ b/modules/planning/common/data_center.h @@ -26,37 +26,13 @@ #include #include "modules/common/macro.h" -#include "modules/common/status/status.h" -#include "modules/map/hdmap/hdmap.h" -#include "modules/map/pnc_map/pnc_map.h" #include "modules/planning/common/frame.h" -#include "modules/planning/common/object_table.h" +#include "modules/planning/common/indexed_queue.h" namespace apollo { namespace planning { -class DataCenter { - public: - ~DataCenter() = default; - Frame *frame(const uint32_t sequence_num) const; - - public: - void save_frame(); - - const Frame *last_frame() const; - - const ObjectTable &object_table() const; - ObjectTable *mutable_object_table() const; - - private: - std::unordered_map> _frames; - std::list _sequence_queue; - std::unique_ptr _frame = nullptr; - std::unique_ptr _object_table = nullptr; - - private: - DECLARE_SINGLETON(DataCenter); -}; +using DataCenter = IndexedQueue; } // namespace planning } // namespace apollo diff --git a/modules/planning/common/frame.cc b/modules/planning/common/frame.cc index 5eaafc4594..92e6d58c56 100644 --- a/modules/planning/common/frame.cc +++ b/modules/planning/common/frame.cc @@ -176,6 +176,9 @@ bool Frame::SmoothReferenceLine() { return true; } +const ObstacleTable &Frame::GetObstacleTable() const { return obstacle_table_; } +ObstacleTable *Frame::MutableObstacleTable() { return &obstacle_table_; } + std::string Frame::DebugString() const { return "Frame: " + std::to_string(sequence_num_); } diff --git a/modules/planning/common/frame.h b/modules/planning/common/frame.h index 2db2d154e8..05f605996d 100644 --- a/modules/planning/common/frame.h +++ b/modules/planning/common/frame.h @@ -30,12 +30,16 @@ #include "modules/map/hdmap/hdmap.h" #include "modules/map/pnc_map/pnc_map.h" #include "modules/map/proto/routing.pb.h" +#include "modules/planning/common/indexed_list.h" +#include "modules/planning/common/obstacle.h" #include "modules/planning/common/planning_data.h" #include "modules/prediction/proto/prediction_obstacle.pb.h" namespace apollo { namespace planning { +using ObstacleTable = IndexedList; + class Frame { public: Frame(const uint32_t sequence_num); @@ -56,6 +60,9 @@ class Frame { PlanningData *mutable_planning_data(); std::string DebugString() const; + const ObstacleTable &GetObstacleTable() const; + ObstacleTable *MutableObstacleTable(); + void set_computed_trajectory(const PublishableTrajectory &trajectory); const PublishableTrajectory &computed_trajectory() const; @@ -70,6 +77,7 @@ class Frame { hdmap::RoutingResult routing_result_; prediction::PredictionObstacles prediction_; + ObstacleTable obstacle_table_; uint32_t sequence_num_ = 0; hdmap::Path hdmap_path_; localization::Pose init_pose_; diff --git a/modules/planning/common/indexed_list.h b/modules/planning/common/indexed_list.h index 0c1c3073ac..8d9604f194 100644 --- a/modules/planning/common/indexed_list.h +++ b/modules/planning/common/indexed_list.h @@ -31,9 +31,13 @@ namespace planning { template class IndexedList { public: - void Add(const I id, std::unique_ptr ptr) { + bool Add(const I id, std::unique_ptr ptr) { + if (Find(id)) { + return false; + } _object_list.push_back(ptr.get()); _object_dict[id] = std::move(ptr); + return true; } T* Find(const I id) { auto iter = _object_dict.find(id); diff --git a/modules/planning/common/object_table.h b/modules/planning/common/object_table.h deleted file mode 100644 index a8a1a3dae7..0000000000 --- a/modules/planning/common/object_table.h +++ /dev/null @@ -1,40 +0,0 @@ -/****************************************************************************** - * Copyright 2017 The Apollo Authors. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *****************************************************************************/ - -/** - * @file object_table.h - **/ - -#ifndef MODULES_PLANNING_COMMON_OBJECT_TABLE_H -#define MODULES_PLANNING_COMMON_OBJECT_TABLE_H - -#include -#include -#include - -#include "modules/planning/common/indexed_list.h" -#include "modules/planning/common/obstacle.h" -#include "modules/planning/common/planning_gflags.h" - -namespace apollo { -namespace planning { - -using ObjectTable = IndexedList; - -} // namespace planning -} // namespace apollo - -#endif // MODULES_PLANNING_COMMON_OBJECT_TABLE_H diff --git a/modules/planning/optimizer/dp_st_speed/BUILD b/modules/planning/optimizer/dp_st_speed/BUILD index ded42f1ab4..81313ff062 100644 --- a/modules/planning/optimizer/dp_st_speed/BUILD +++ b/modules/planning/optimizer/dp_st_speed/BUILD @@ -32,6 +32,7 @@ cc_library( "//modules/common/configs/proto:vehicle_config_proto", "//modules/common/proto:common_proto", "//modules/common/proto:pnc_point_proto", + "//modules/common/status", "//modules/planning/common:data_center", "//modules/planning/common:decision_data", "//modules/planning/common/speed:speed_data", diff --git a/modules/planning/optimizer/dp_st_speed/dp_st_graph.cc b/modules/planning/optimizer/dp_st_speed/dp_st_graph.cc index 0a5203dc56..40117c4f3d 100644 --- a/modules/planning/optimizer/dp_st_speed/dp_st_graph.cc +++ b/modules/planning/optimizer/dp_st_speed/dp_st_graph.cc @@ -41,7 +41,7 @@ DpStGraph::DpStGraph(const DpStSpeedConfig& dp_config) Status DpStGraph::Search(const StGraphData& st_graph_data, DecisionData* const decision_data, - SpeedData* const speed_data) { + SpeedData* const speed_data, ObstacleTable* table) { init_point_ = st_graph_data.init_point(); if (st_graph_data.path_data_length() < @@ -69,7 +69,7 @@ Status DpStGraph::Search(const StGraphData& st_graph_data, return Status(ErrorCode::PLANNING_ERROR, msg); } - if (!get_object_decision(st_graph_data, *speed_data).ok()) { + if (!get_object_decision(st_graph_data, *speed_data, table).ok()) { const std::string msg = "Get object decision by speed profile failed."; AERROR << msg; return Status(ErrorCode::PLANNING_ERROR, msg); @@ -283,7 +283,8 @@ Status DpStGraph::retrieve_speed_profile(SpeedData* const speed_data) const { } Status DpStGraph::get_object_decision(const StGraphData& st_graph_data, - const SpeedData& speed_profile) const { + const SpeedData& speed_profile, + ObstacleTable* obstacles) const { if (speed_profile.speed_vector().size() < 2) { const std::string msg = "dp_st_graph failed to get speed profile."; AERROR << msg; @@ -299,8 +300,7 @@ Status DpStGraph::get_object_decision(const StGraphData& st_graph_data, boundary_it != obs_boundaries.end(); ++boundary_it) { CHECK_EQ(boundary_it->points().size(), 4); - Obstacle* object_ptr = - DataCenter::instance()->mutable_object_table()->Find(boundary_it->id()); + Obstacle* object_ptr = obstacles->Find(boundary_it->id()); if (!object_ptr) { AERROR << "Failed to find object " << boundary_it->id(); return Status(ErrorCode::PLANNING_ERROR, diff --git a/modules/planning/optimizer/dp_st_speed/dp_st_graph.h b/modules/planning/optimizer/dp_st_speed/dp_st_graph.h index ba1c192bbc..fa3f235203 100644 --- a/modules/planning/optimizer/dp_st_speed/dp_st_graph.h +++ b/modules/planning/optimizer/dp_st_speed/dp_st_graph.h @@ -29,6 +29,7 @@ #include "modules/common/status/status.h" #include "modules/planning/common/decision_data.h" +#include "modules/planning/common/frame.h" #include "modules/planning/common/speed/speed_data.h" #include "modules/planning/common/speed/st_point.h" #include "modules/planning/optimizer/dp_st_speed/dp_st_cost.h" @@ -43,7 +44,8 @@ class DpStGraph { apollo::common::Status Search(const StGraphData& st_graph_data, DecisionData* const decision_data, - SpeedData* const speed_data); + SpeedData* const speed_data, + ObstacleTable* table); private: apollo::common::Status InitCostTable(); @@ -53,8 +55,9 @@ class DpStGraph { apollo::common::Status retrieve_speed_profile( SpeedData* const speed_data) const; - apollo::common::Status get_object_decision( - const StGraphData& st_graph_data, const SpeedData& speed_profile) const; + apollo::common::Status get_object_decision(const StGraphData& st_graph_data, + const SpeedData& speed_profile, + ObstacleTable* obstacles) const; apollo::common::Status CalculateTotalCost(const StGraphData& st_graph_data); void CalculateCostAt(const StGraphData& st_graph_data, const uint32_t r, diff --git a/modules/planning/optimizer/dp_st_speed/dp_st_speed_optimizer.cc b/modules/planning/optimizer/dp_st_speed/dp_st_speed_optimizer.cc index 4d4d5c84d8..cc355da2a3 100644 --- a/modules/planning/optimizer/dp_st_speed/dp_st_speed_optimizer.cc +++ b/modules/planning/optimizer/dp_st_speed/dp_st_speed_optimizer.cc @@ -101,7 +101,10 @@ Status DpStSpeedOptimizer::Process(const PathData& path_data, StGraphData st_graph_data(boundaries, init_point, speed_limit, path_length); DpStGraph st_graph(dp_st_speed_config_); - if (!st_graph.Search(st_graph_data, decision_data, speed_data).ok()) { + if (!st_graph + .Search(st_graph_data, decision_data, speed_data, + frame_->MutableObstacleTable()) + .ok()) { const std::string msg = "Failed to search graph with dynamic programming."; AERROR << msg; return Status(ErrorCode::PLANNING_ERROR, msg); -- GitLab