Apollo 11.0
自动驾驶开放平台
scenario.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 *****************************************************************************/
16
22
23#include <cxxabi.h>
24
30
31namespace apollo {
32namespace planning {
33
36 current_stage_(nullptr),
37 msg_(""),
38 injector_(nullptr),
39 config_path_(""),
40 config_dir_(""),
41 name_("") {}
42
43bool Scenario::Init(std::shared_ptr<DependencyInjector> injector,
44 const std::string& name) {
45 name_ = name;
46 injector_ = injector;
47 // set scenario_type in PlanningContext
48 auto* scenario = injector_->planning_context()
49 ->mutable_planning_status()
50 ->mutable_scenario();
51 scenario->Clear();
52 scenario->set_scenario_type(name_);
53
54 // Generate the default config path.
55 int status;
56 // Get the name of this class.
57 std::string class_name =
58 abi::__cxa_demangle(typeid(*this).name(), 0, 0, &status);
59
61 ->GetPluginClassHomePath<Scenario>(class_name);
62 config_dir_ += "/conf";
63 AINFO << "config_dir : " << config_dir_;
64 // Generate the default task config path from PluginManager.
66 ->GetPluginConfPath<Scenario>(class_name,
67 "conf/scenario_conf.pb.txt");
68
69 // Load the pipeline config.
70 std::string pipeline_config_path =
72 ->GetPluginConfPath<Scenario>(class_name, "conf/pipeline.pb.txt");
73 AINFO << "Load config path:" << pipeline_config_path;
74 // Load the pipeline of scenario.
75 if (!apollo::cyber::common::GetProtoFromFile(pipeline_config_path,
77 AERROR << "Load pipeline of " << name_ << " failed!";
78 return false;
79 }
80 for (const auto& stage : scenario_pipeline_config_.stage()) {
81 stage_pipeline_map_[stage.name()] = &stage;
82 }
83 return true;
84}
85
87 const common::TrajectoryPoint& planning_init_point, Frame* frame) {
88 if (current_stage_ == nullptr) {
91 if (nullptr == current_stage_) {
92 AERROR << "Create stage " << scenario_pipeline_config_.stage(0).name()
93 << " failed!";
95 return scenario_result_;
96 }
97 AINFO << "Create stage " << current_stage_->Name();
98 }
99 if (current_stage_->Name().empty()) {
101 return scenario_result_;
102 }
103 auto ret = current_stage_->Process(planning_init_point, frame);
105 switch (ret.GetStageStatus()) {
107 AERROR << "Stage '" << current_stage_->Name() << "' returns error";
109 break;
110 }
113 break;
114 }
116 auto next_stage = current_stage_->NextStage();
117 if (next_stage != current_stage_->Name()) {
118 AINFO << "switch stage from " << current_stage_->Name() << " to "
119 << next_stage;
120 if (next_stage.empty()) {
122 return scenario_result_;
123 }
124 if (stage_pipeline_map_.find(next_stage) == stage_pipeline_map_.end()) {
125 AERROR << "Failed to find config for stage: " << next_stage;
128 return scenario_result_;
129 }
131 if (current_stage_ == nullptr) {
132 AWARN << "Current stage is a null pointer.";
135 return scenario_result_;
136 }
137 }
138 if (current_stage_ != nullptr && !current_stage_->Name().empty()) {
141 } else {
143 }
144 break;
145 }
146 default: {
147 AWARN << "Unexpected Stage return value: "
148 << static_cast<int>(ret.GetStageStatus());
150 }
151 }
152 return scenario_result_;
153}
154
155std::shared_ptr<Stage> Scenario::CreateStage(
156 const StagePipeline& stage_pipeline) {
157 auto stage_ptr =
161 if (nullptr == stage_ptr ||
162 !stage_ptr->Init(stage_pipeline, injector_, config_dir_, GetContext())) {
163 AERROR << "Create stage " << stage_pipeline.name() << " of " << name_
164 << " failed!";
165 return nullptr;
166 }
167 return stage_ptr;
168}
169
170const std::string Scenario::GetStage() const {
171 return current_stage_ ? current_stage_->Name() : "";
172}
173
178
179} // namespace planning
180} // namespace apollo
std::string GetPluginConfPath(const std::string &class_name, const std::string &conf_name)
get plugin configuration file location
static PluginManager * Instance()
get singleton instance of PluginManager
std::string GetPluginClassHomePath(const std::string &class_name)
@bried get plugin description file location that class belongs to
std::shared_ptr< Base > CreateInstance(const std::string &derived_class)
create plugin instance of derived class based on Base
static std::string GetFullPlanningClassName(const std::string &class_name)
Given the class name of planning module, combine the namespace "apollo::planning::" with it to create...
Frame holds all data for one planning cycle.
Definition frame.h:62
const ScenarioResult & SetScenarioStatus(const ScenarioStatusType &scenario_status)
Set the scenario status.
const ScenarioResult & SetStageResult(const StageResult &stage_result)
Set the stage status.
std::shared_ptr< Stage > current_stage_
Definition scenario.h:110
virtual ScenarioResult Process(const common::TrajectoryPoint &planning_init_point, Frame *frame)
Definition scenario.cc:86
virtual bool Init(std::shared_ptr< DependencyInjector > injector, const std::string &name)
Definition scenario.cc:43
const std::string GetStage() const
Definition scenario.cc:170
virtual ScenarioContext * GetContext()=0
Get the scenario context.
void Reset()
Reset the scenario, used before entering the scenario.
Definition scenario.cc:174
std::unordered_map< std::string, const StagePipeline * > stage_pipeline_map_
Definition scenario.h:111
std::shared_ptr< Stage > CreateStage(const StagePipeline &stage_pipeline)
Each scenario should define its own stages object's creation scenario will call stage's Stage::Proces...
Definition scenario.cc:155
ScenarioResult scenario_result_
Definition scenario.h:109
std::shared_ptr< DependencyInjector > injector_
Definition scenario.h:113
ScenarioPipeline scenario_pipeline_config_
Definition scenario.h:118
Planning module main class.
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
#define AWARN
Definition log.h:43
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
class register implement
Definition arena_queue.h:37