【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

相关文章

Python中logging模块用法示例总结

《Python中logging模块用法示例总结》在Python中logging模块是一个强大的日志记录工具,它允许用户将程序运行期间产生的日志信息输出到控制台或者写入到文件中,:本文主要介绍Pyt... 目录前言一. 基本使用1. 五种日志等级2.  设置报告等级3. 自定义格式4. C语言风格的格式化方法

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

使用Spring Cache本地缓存示例代码

《使用SpringCache本地缓存示例代码》缓存是提高应用程序性能的重要手段,通过将频繁访问的数据存储在内存中,可以减少数据库访问次数,从而加速数据读取,:本文主要介绍使用SpringCac... 目录一、Spring Cache简介核心特点:二、基础配置1. 添加依赖2. 启用缓存3. 缓存配置方案方案

基于Redis自动过期的流处理暂停机制

《基于Redis自动过期的流处理暂停机制》基于Redis自动过期的流处理暂停机制是一种高效、可靠且易于实现的解决方案,防止延时过大的数据影响实时处理自动恢复处理,以避免积压的数据影响实时性,下面就来详... 目录核心思路代码实现1. 初始化Redis连接和键前缀2. 接收数据时检查暂停状态3. 检测到延时过

MySQL的配置文件详解及实例代码

《MySQL的配置文件详解及实例代码》MySQL的配置文件是服务器运行的重要组成部分,用于设置服务器操作的各种参数,下面:本文主要介绍MySQL配置文件的相关资料,文中通过代码介绍的非常详细,需要... 目录前言一、配置文件结构1.[mysqld]2.[client]3.[mysql]4.[mysqldum

Python多线程实现大文件快速下载的代码实现

《Python多线程实现大文件快速下载的代码实现》在互联网时代,文件下载是日常操作之一,尤其是大文件,然而,网络条件不稳定或带宽有限时,下载速度会变得很慢,本文将介绍如何使用Python实现多线程下载... 目录引言一、多线程下载原理二、python实现多线程下载代码说明:三、实战案例四、注意事项五、总结引

IDEA与MyEclipse代码量统计方式

《IDEA与MyEclipse代码量统计方式》文章介绍在项目中不安装第三方工具统计代码行数的方法,分别说明MyEclipse通过正则搜索(排除空行和注释)及IDEA使用Statistic插件或调整搜索... 目录项目场景MyEclipse代码量统计IDEA代码量统计总结项目场景在项目中,有时候我们需要统计