# 10. 组件 Component Component 是推荐的 **算法模块形态**:`.so` + DAG,支持 **1–4 路** 输入(`Component`)。 | 本文 §10 | 相关文档 | |----------|----------| | 算法模块 | [§0 指南](00_guide.md) · [§9 启动 Launch](09_launch.md) · [§3 通道 Channel](03_channel.md) · [§12 调度 Scheduler](12_scheduler.md) | --- ## 10.1 执行流程 框架在 DataVisitor 多路输入齐备时调度 `Proc()`。完整数据流见 [§1.4](01_architecture.md#14-component-数据流)。
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`): ```cpp #include "autolink/class_loader/class_loader.hpp" #include "autolink/component/component.hpp" #include "examples.pb.h" class CommonComponentSample : public Component { public: bool Init() override; bool Proc(const std::shared_ptr& msg0, const std::shared_ptr& msg1) override; }; AUTOLINK_REGISTER_COMPONENT(CommonComponentSample) ``` **实现**(`common_component_example.cpp`): ```cpp bool CommonComponentSample::Init() { AINFO << "Commontest component init"; return true; } bool CommonComponentSample::Proc(const std::shared_ptr& msg0, const std::shared_ptr& msg1) { AINFO << "Start common component Proc [" << msg0->msg_id() << "] [" << msg1->msg_id() << "]"; return true; } ``` **DAG**(`common.dag`)— 两个 `readers` 对应 `msg0`、`msg1`: ```text components { class_name : "CommonComponentSample" config { name : "common" readers { channel: "/autolink/prediction" } readers { channel: "/autolink/test" } } } ``` 仅当 **两路 channel 均有新消息** 且对齐后,`Proc` 才被调用(同步多传感器融合的典型模式)。 --- ## 10.3 模板参数 | 基类 | Proc 签名 | |------|-----------| | `Component` | `Proc(const shared_ptr&)` | | `Component` | `Proc(..., ...)` 两参 | | 至多 | `Component` 四路 | `component.hpp` 注释:勿手动调用 `Init`/`Proc`,由框架调度。 --- ## 10.4 Writer 输出 在 `Init()` 中通过 `node_` 创建 Writer(`TimerComponent` 示例同理): ```cpp bool TimerComponentSample::Init() { driver_writer_ = node_->CreateWriter("/carstatus/channel"); return true; } ``` `Proc` 内只 `Write`,勿重复 `CreateWriter`。 --- ## 10.5 ClassLoader `common_component_example/CMakeLists.txt` 将示例编译为 `libcommon_component_example.so`,安装到 `AUTOLINK_LIB_PATH`。mainboard 流程: ```cpp class_loader_manager_.LoadLibrary(load_path); auto component = class_loader_manager_.CreateClassObj(class_name); component->Initialize(conf); ``` 见 `module_controller.cpp`。 --- ## 10.6 运行示例 ```bash export AUTOLINK_PATH=... export AUTOLINK_LIB_PATH=.../libcommon_component_example.so 所在目录 mainboard -d .../common.dag ``` `Proc` 需要两路输入:用两个 talker 或改为单路 `Component` 测试。TimerComponent 无此依赖,见 [§11 时间 Time / Rate / Timer](11_timer.md)。 --- ## 10.7 Binary 对比 | | Binary | Component | |---|--------|-----------| | Node 创建 | `CreateNode` in `main` | 框架注入 `node_` | | 触发方式 | 自建循环 / Reader 回调 | DataVisitor 同步多路 | | 配置变更 | 重编译 | 改 DAG | | 部署 | 单 binary | `.so` + mainboard | --- **导航**:[← §9 启动 Launch](09_launch.md) · [§0 指南](00_guide.md) · [§11 时间 Time / Rate / Timer →](11_timer.md)