本文主要是介绍【Apollo自动驾驶-从理论到代码】cyber/component模块,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/* 作者水平有限,欢迎批评指正,内容持续完善中!!*/
Apollo Cyber Component
- 主要文件
- 类图
- 处理流程
- Component特点及须知
- 代码详解
- component_base.h
- component.h
主要文件
文件名 | 描述 | 作用 |
---|---|---|
component_base.h | 组件代码基类 | |
component.h | 常规组件基类 | |
timer_component.h | 定时器组件基类 | |
timer_component.cc | ||
类图
处理流程
Component特点及须知
- Component最多可以处理四个通道消息。
- 消息的类型在component创建时指定。
- Component继承于ComponentBase,用户定义的component可以通过继承Component类,实现Init() & Proc()函数,这些函数将会被CyberRT调度。
- Init() & Proc()需要被重载,但是不能被调用。是由CyberRT框架自动调度。
模板类和模板函数的写法如下:
模板类定义:
template <typename M0 = NullType, typename M1 = NullType,typename M2 = NullType, typename M3 = NullType>
class Component : public ComponentBase {}template <>
class Component<NullType, NullType, NullType, NullType> : public ComponentBase {}template <typename M0>
class Component<M0, NullType, NullType, NullType> : public ComponentBase {}template <typename M0, typename M1>
class Component<M0, M1, NullType, NullType> : public ComponentBase {}template <typename M0, typename M1, typename M2>
class Component<M0, M1, M2, NullType> : public ComponentBase {}
Initialize模板函数:
inline bool Component<NullType, NullType, NullType>::Initialize(const ComponentConfig& config){}template <typename M0>
bool Component<M0, NullType, NullType, NullType>::Initialize(const ComponentConfig& config) {}template <typename M0, typename M1>
bool Component<M0, M1, NullType, NullType>::Initialize(const ComponentConfig& config) {}template <typename M0, typename M1, typename M2>
bool Component<M0, M1, M2, NullType>::Initialize(const ComponentConfig& config) {template <typename M0, typename M1, typename M2, typename M3>
bool Component<M0, M1, M2, M3>::Initialize(const ComponentConfig& config) {
Process模板函数:
template <typename M0>
bool Component<M0, NullType, NullType, NullType>::Process(const std::shared_ptr<M0>& msg) {}template <typename M0, typename M1>
bool Component<M0, M1, NullType, NullType>::Process(const std::shared_ptr<M0>& msg0, const std::shared_ptr<M1>& msg1) {}template <typename M0, typename M1, typename M2>
bool Component<M0, M1, M2, NullType>::Process(const std::shared_ptr<M0>& msg0,const std::shared_ptr<M1>& msg1,const std::shared_ptr<M2>& msg2) {}template <typename M0, typename M1, typename M2, typename M3>
bool Component<M0, M1, M2, M3>::Process(const std::shared_ptr<M0>& msg0,const std::shared_ptr<M1>& msg1,const std::shared_ptr<M2>& msg2,const std::shared_ptr<M3>& msg3) {}
上面代码罗列出来的目的,是为了更清楚的展示不同消息数情况下的函数原型。方便大家后续创建自定义消息数量Component的理解。考虑到代码的雷同,将只会对一个消息数的代码进行介绍。
代码详解
component_base.h
namespace apollo {
namespace cyber {using apollo::cyber::proto::ComponentConfig;
using apollo::cyber::proto::TimerComponentConfig;// enable_shared_from_this 是一个以其派生类为模板类型参数的基类模板,继承它,派生类的this指针就能变成一个 shared_ptr。
class ComponentBase : public std::enable_shared_from_this<ComponentBase> {public:template <typename M>using Reader = cyber::Reader<M>;virtual ~ComponentBase() {}// 虚函数,由子类重写,分别为Component类和TimerComponent类。virtual bool Initialize(const ComponentConfig& config) { return false; }virtual bool Initialize(const TimerComponentConfig& config) { return false; }virtual void Shutdown() {if (is_shutdown_.exchange(true)) {return;}// 释放资源Clear();for (auto& reader : readers_) {reader->Shutdown();}scheduler::Instance()->RemoveTask(node_->Name());}template <typename T>bool GetProtoConfig(T* config) const {return common::GetProtoFromFile(config_file_path_, config);}protected:virtual bool Init() = 0;virtual void Clear() { return; }const std::string& ConfigFilePath() const { return config_file_path_; }//加载Component配置文件void LoadConfigFiles(const ComponentConfig& config) {if (!config.config_file_path().empty()) {if (config.config_file_path()[0] != '/') {config_file_path_ = common::GetAbsolutePath(common::WorkRoot(),config.config_file_path());} else {config_file_path_ = config.config_file_path();}}if (!config.flag_file_path().empty()) {std::string flag_file_path = config.flag_file_path();if (flag_file_path[0] != '/') {flag_file_path =common::GetAbsolutePath(common::WorkRoot(), flag_file_path);}google::SetCommandLineOption("flagfile", flag_file_path.c_str());}}//加载TimerComponent配置文件void LoadConfigFiles(const TimerComponentConfig& config) {if (!config.config_file_path().empty()) {if (config.config_file_path()[0] != '/') {config_file_path_ = common::GetAbsolutePath(common::WorkRoot(),config.config_file_path());} else {config_file_path_ = config.config_file_path();}}if (!config.flag_file_path().empty()) {std::string flag_file_path = config.flag_file_path();if (flag_file_path[0] != '/') {flag_file_path =common::GetAbsolutePath(common::WorkRoot(), flag_file_path);}google::SetCommandLineOption("flagfile", flag_file_path.c_str());}}std::atomic<bool> is_shutdown_ = {false};//指向Node的智能指针std::shared_ptr<Node> node_ = nullptr;std::string config_file_path_ = "";std::vector<std::shared_ptr<ReaderBase>> readers_;
};} // namespace cyber
} // namespace apollo
component.h
对于该文件,只提取部分代码进行介绍。
template <typename M0>
bool Component<M0, NullType, NullType, NullType>::Initialize(const ComponentConfig& config) {node_.reset(new Node(config.name()));// 使用父类的LoadConfigFiles(config);if (config.readers_size() < 1) {AERROR << "Invalid config file: too few readers.";return false;}// 子类的初始化函数,由用户实现if (!Init()) {AERROR << "Component Init() failed.";return false;}bool is_reality_mode = GlobalData::Instance()->IsRealityMode();// 初始化Reader配置,主要包括通道名,qos策略,队列大小ReaderConfig reader_cfg;reader_cfg.channel_name = config.readers(0).channel();reader_cfg.qos_profile.CopyFrom(config.readers(0).qos_profile());reader_cfg.pending_queue_size = config.readers(0).pending_queue_size();// 获取指向派生类对象的weak指针,如果对象存在则初始化消息,否则打印错误std::weak_ptr<Component<M0>> self =std::dynamic_pointer_cast<Component<M0>>(shared_from_this());auto func = [self](const std::shared_ptr<M0>& msg) {auto ptr = self.lock();if (ptr) {ptr->Process(msg);} else {AERROR << "Component object has been destroyed.";}};// Reader对象指针std::shared_ptr<Reader<M0>> reader = nullptr;// 根据是否为仿真模式,创建不同的Reader对象if (cyber_likely(is_reality_mode)) {reader = node_->CreateReader<M0>(reader_cfg);} else {reader = node_->CreateReader<M0>(reader_cfg, func);}if (reader == nullptr) {AERROR << "Component create reader failed.";return false;}// 将reader放入当前Component的readers_队列中,该队列的大小为0-4。readers_.emplace_back(std::move(reader));// 如果为仿真模式则直接退出if (cyber_unlikely(!is_reality_mode)) {return true;}// 通过协程工厂创建携程,将协程与调度器进行绑定。data::VisitorConfig conf = {readers_[0]->ChannelId(),readers_[0]->PendingQueueSize()};auto dv = std::make_shared<data::DataVisitor<M0>>(conf);croutine::RoutineFactory factory =croutine::CreateRoutineFactory<M0>(func, dv);auto sched = scheduler::Instance();return sched->CreateTask(factory, node_->Name());
}
这篇关于【Apollo自动驾驶-从理论到代码】cyber/component模块的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!