10. 组件 Component

Component 是推荐的 算法模块形态.so + DAG,支持 1–4 路 输入(Component<M0,…,M3>)。

本文 §10

相关文档

算法模块

§0 指南 · §9 启动 Launch · §3 通道 Channel · §12 调度 Scheduler


10.1 执行流程

框架在 DataVisitor 多路输入齐备时调度 Proc()。完整数据流见 §1.4

Component DAG → Initialize → Init / Proc → Writer AUTOLINK_REGISTER_COMPONENT 供 mainboard 按 class_name 实例化
DAG
Initialize 框架注册 Reader
DataVisitor
Proc() 用户 Init() 在 Initialize 内
Writer
钩子调用方
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;
}

DAGcommon.dag)— 两个 readers 对应 msg0msg1

components {
    class_name : "CommonComponentSample"
    config {
        name : "common"
        readers { channel: "/autolink/prediction" }
        readers { channel: "/autolink/test" }
    }
}

仅当 两路 channel 均有新消息 且对齐后,Proc 才被调用(同步多传感器融合的典型模式)。


10.3 模板参数

基类

Proc 签名

Component<M0>

Proc(const shared_ptr<M0>&)

Component<M0,M1>

Proc(..., ...) 两参

至多

Component<M0,M1,M2,M3> 四路

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 创建

CreateNode in main

框架注入 node_

触发方式

自建循环 / Reader 回调

DataVisitor 同步多路

配置变更

重编译

改 DAG

部署

单 binary

.so + mainboard


导航← §9 启动 Launch · §0 指南 · §11 时间 Time / Rate / Timer →