10. 组件 Component
Component 是推荐的 算法模块形态:.so + DAG,支持 1–4 路 输入(Component<M0,…,M3>)。
本文 §10 |
相关文档 |
|---|---|
算法模块 |
10.1 执行流程
框架在 DataVisitor 多路输入齐备时调度 Proc()。完整数据流见 §1.4。
AUTOLINK_REGISTER_COMPONENT 供 mainboard 按 class_name 实例化
| 钩子 | 调用方 |
|---|---|
Initialize(config) | 框架:创建 Node、注入 Reader、注册调度 |
Init() / Proc() | 用户:一次性初始化 / 每帧业务逻辑 |
10.2 CommonComponentSample
头文件(common_component_example.hpp):
#include "autolink/class_loader/class_loader.hpp"
#include "autolink/component/component.hpp"
#include "examples.pb.h"
class CommonComponentSample : public Component<Driver, Driver> {
public:
bool Init() override;
bool Proc(const std::shared_ptr<Driver>& msg0,
const std::shared_ptr<Driver>& msg1) override;
};
AUTOLINK_REGISTER_COMPONENT(CommonComponentSample)
实现(common_component_example.cpp):
bool CommonComponentSample::Init() {
AINFO << "Commontest component init";
return true;
}
bool CommonComponentSample::Proc(const std::shared_ptr<Driver>& msg0,
const std::shared_ptr<Driver>& msg1) {
AINFO << "Start common component Proc [" << msg0->msg_id() << "] ["
<< msg1->msg_id() << "]";
return true;
}
DAG(common.dag)— 两个 readers 对应 msg0、msg1:
components {
class_name : "CommonComponentSample"
config {
name : "common"
readers { channel: "/autolink/prediction" }
readers { channel: "/autolink/test" }
}
}
仅当 两路 channel 均有新消息 且对齐后,Proc 才被调用(同步多传感器融合的典型模式)。
10.3 模板参数
基类 |
Proc 签名 |
|---|---|
|
|
|
|
至多 |
|
component.hpp 注释:勿手动调用 Init/Proc,由框架调度。
10.4 Writer 输出
在 Init() 中通过 node_ 创建 Writer(TimerComponent 示例同理):
bool TimerComponentSample::Init() {
driver_writer_ = node_->CreateWriter<Driver>("/carstatus/channel");
return true;
}
Proc 内只 Write,勿重复 CreateWriter。
10.5 ClassLoader
common_component_example/CMakeLists.txt 将示例编译为 libcommon_component_example.so,安装到 AUTOLINK_LIB_PATH。mainboard 流程:
class_loader_manager_.LoadLibrary(load_path);
auto component = class_loader_manager_.CreateClassObj<ComponentBase>(class_name);
component->Initialize(conf);
见 module_controller.cpp。
10.6 运行示例
export AUTOLINK_PATH=...
export AUTOLINK_LIB_PATH=.../libcommon_component_example.so 所在目录
mainboard -d .../common.dag
Proc 需要两路输入:用两个 talker 或改为单路 Component<Driver> 测试。TimerComponent 无此依赖,见 §11 时间 Time / Rate / Timer。
10.7 Binary 对比
Binary |
Component |
|
|---|---|---|
Node 创建 |
|
框架注入 |
触发方式 |
自建循环 / Reader 回调 |
DataVisitor 同步多路 |
配置变更 |
重编译 |
改 DAG |
部署 |
单 binary |
|