Apollo  6.0
Open source self driving car software
stage.h
Go to the documentation of this file.
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 
21 #pragma once
22 
23 #include <map>
24 #include <memory>
25 #include <string>
26 #include <vector>
27 
31 #include "modules/planning/proto/planning_config.pb.h"
33 
34 namespace apollo {
35 namespace planning {
36 namespace scenario {
37 
38 class Stage {
39  public:
40  enum StageStatus {
41  ERROR = 1,
42  READY = 2,
43  RUNNING = 3,
44  FINISHED = 4,
45  };
46 
47  Stage(const ScenarioConfig::StageConfig& config,
48  const std::shared_ptr<DependencyInjector>& injector);
49 
50  virtual ~Stage() = default;
51 
52  const ScenarioConfig::StageConfig& config() const { return config_; }
53 
54  ScenarioConfig::StageType stage_type() const { return config_.stage_type(); }
55 
61  virtual StageStatus Process(
62  const common::TrajectoryPoint& planning_init_point, Frame* frame) = 0;
63 
68  const std::vector<Task*>& TaskList() const { return task_list_; }
69 
70  const std::string& Name() const;
71 
72  template <typename T>
73  T* GetContextAs() {
74  return static_cast<T*>(context_);
75  }
76 
77  void SetContext(void* context) { context_ = context; }
78 
79  Task* FindTask(TaskConfig::TaskType task_type) const;
80 
81  ScenarioConfig::StageType NextStage() const { return next_stage_; }
82 
83  protected:
85  const common::TrajectoryPoint& planning_start_point, Frame* frame);
86 
88  const common::TrajectoryPoint& planning_start_point, Frame* frame);
89 
90  bool ExecuteTaskOnOpenSpace(Frame* frame);
91 
93 
94  void RecordDebugInfo(ReferenceLineInfo* reference_line_info,
95  const std::string& name, const double time_diff_ms);
96 
97  protected:
98  std::map<TaskConfig::TaskType, std::unique_ptr<Task>> tasks_;
99  std::vector<Task*> task_list_;
100  ScenarioConfig::StageConfig config_;
101  ScenarioConfig::StageType next_stage_;
102  void* context_ = nullptr;
103  std::string name_;
104  std::shared_ptr<DependencyInjector> injector_;
105 };
106 
107 #define DECLARE_STAGE(NAME, CONTEXT) \
108  class NAME : public Stage { \
109  public: \
110  explicit NAME(const ScenarioConfig::StageConfig& config) \
111  : Stage(config) {} \
112  Stage::StageStatus Process( \
113  const common::TrajectoryPoint& planning_init_point, \
114  Frame* frame) override; \
115  CONTEXT* GetContext() { return GetContextAs<CONTEXT>(); } \
116  }
117 
118 } // namespace scenario
119 } // namespace planning
120 } // namespace apollo
void SetContext(void *context)
Definition: stage.h:77
std::shared_ptr< DependencyInjector > injector_
Definition: stage.h:104
std::map< TaskConfig::TaskType, std::unique_ptr< Task > > tasks_
Definition: stage.h:98
std::string name_
Definition: stage.h:103
ScenarioConfig::StageType next_stage_
Definition: stage.h:101
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
ScenarioConfig::StageType NextStage() const
Definition: stage.h:81
bool ExecuteTaskOnOpenSpace(Frame *frame)
Planning module main class. It processes GPS and IMU as input, to generate planning info...
const ScenarioConfig::StageConfig & config() const
Definition: stage.h:52
Frame holds all data for one planning cycle.
Definition: frame.h:61
ReferenceLineInfo holds all data for one reference line.
Definition: reference_line_info.h:54
bool ExecuteTaskOnReferenceLine(const common::TrajectoryPoint &planning_start_point, Frame *frame)
ScenarioConfig::StageConfig config_
Definition: stage.h:100
ScenarioConfig::StageType stage_type() const
Definition: stage.h:54
std::vector< Task * > task_list_
Definition: stage.h:99
Task * FindTask(TaskConfig::TaskType task_type) const
StageStatus
Definition: stage.h:40
Defines the Factory class.
virtual Stage::StageStatus FinishScenario()
const std::vector< Task * > & TaskList() const
The sequence of tasks inside the stage. These tasks usually will be executed in order.
Definition: stage.h:68
T * GetContextAs()
Definition: stage.h:73
const std::string & Name() const
Definition: task.h:35
void RecordDebugInfo(ReferenceLineInfo *reference_line_info, const std::string &name, const double time_diff_ms)
bool ExecuteTaskOnReferenceLineForOnlineLearning(const common::TrajectoryPoint &planning_start_point, Frame *frame)
Stage(const ScenarioConfig::StageConfig &config, const std::shared_ptr< DependencyInjector > &injector)
Definition: stage.h:38
void * context_
Definition: stage.h:102
virtual StageStatus Process(const common::TrajectoryPoint &planning_init_point, Frame *frame)=0
Each stage does its business logic inside Process function. If the stage want to transit to a differe...