【Apollo自动驾驶-从理论到代码】cyber/component模块

2023-10-11 18:20

本文主要是介绍【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特点及须知

  1. Component最多可以处理四个通道消息。
  2. 消息的类型在component创建时指定。
  3. Component继承于ComponentBase,用户定义的component可以通过继承Component类,实现Init() & Proc()函数,这些函数将会被CyberRT调度。
  4. 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模块的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/189964

相关文章

Spring Boot 3.4.3 基于 Spring WebFlux 实现 SSE 功能(代码示例)

《SpringBoot3.4.3基于SpringWebFlux实现SSE功能(代码示例)》SpringBoot3.4.3结合SpringWebFlux实现SSE功能,为实时数据推送提供... 目录1. SSE 简介1.1 什么是 SSE?1.2 SSE 的优点1.3 适用场景2. Spring WebFlu

java之Objects.nonNull用法代码解读

《java之Objects.nonNull用法代码解读》:本文主要介绍java之Objects.nonNull用法代码,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录Java之Objects.nonwww.chinasem.cnNull用法代码Objects.nonN

SpringBoot中封装Cors自动配置方式

《SpringBoot中封装Cors自动配置方式》:本文主要介绍SpringBoot中封装Cors自动配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot封装Cors自动配置背景实现步骤1. 创建 GlobalCorsProperties

idea中创建新类时自动添加注释的实现

《idea中创建新类时自动添加注释的实现》在每次使用idea创建一个新类时,过了一段时间发现看不懂这个类是用来干嘛的,为了解决这个问题,我们可以设置在创建一个新类时自动添加注释,帮助我们理解这个类的用... 目录前言:详细操作:步骤一:点击上方的 文件(File),点击&nbmyHIgsp;设置(Setti

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

python+opencv处理颜色之将目标颜色转换实例代码

《python+opencv处理颜色之将目标颜色转换实例代码》OpenCV是一个的跨平台计算机视觉库,可以运行在Linux、Windows和MacOS操作系统上,:本文主要介绍python+ope... 目录下面是代码+ 效果 + 解释转HSV: 关于颜色总是要转HSV的掩膜再标注总结 目标:将红色的部分滤

在C#中调用Python代码的两种实现方式

《在C#中调用Python代码的两种实现方式》:本文主要介绍在C#中调用Python代码的两种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#调用python代码的方式1. 使用 Python.NET2. 使用外部进程调用 Python 脚本总结C#调

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

Python使用date模块进行日期处理的终极指南

《Python使用date模块进行日期处理的终极指南》在处理与时间相关的数据时,Python的date模块是开发者最趁手的工具之一,本文将用通俗的语言,结合真实案例,带您掌握date模块的六大核心功能... 目录引言一、date模块的核心功能1.1 日期表示1.2 日期计算1.3 日期比较二、六大常用方法详

Java中&和&&以及|和||的区别、应用场景和代码示例

《Java中&和&&以及|和||的区别、应用场景和代码示例》:本文主要介绍Java中的逻辑运算符&、&&、|和||的区别,包括它们在布尔和整数类型上的应用,文中通过代码介绍的非常详细,需要的朋友可... 目录前言1. & 和 &&代码示例2. | 和 ||代码示例3. 为什么要使用 & 和 | 而不是总是使