【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通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

Go语言代码格式化的技巧分享

《Go语言代码格式化的技巧分享》在Go语言的开发过程中,代码格式化是一个看似细微却至关重要的环节,良好的代码格式化不仅能提升代码的可读性,还能促进团队协作,减少因代码风格差异引发的问题,Go在代码格式... 目录一、Go 语言代码格式化的重要性二、Go 语言代码格式化工具:gofmt 与 go fmt(一)

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.