Apollo Cyber RT框架
Apollo Cyber RT框架文档
快速开始
入门
Apollo Cyber RT框架是基于组件的概念构建的。作为Apollo Cyber RT框架的基本构建块,每个组件都包含一个特定的算法模块,该模块处理一组数据输入并生成一组输出。
为了成功创建和启动新组件,需要执行四个基本步骤:
- 设置组件文件结构
- 实现组件类
- 设置配置文件
- 启动组件
下面的示例演示如何创建一个简单的组件,然后在屏幕上生成,运行和观看最终输出。如果您想了解有关Apollo Cyber RT的更多信息,您可以在directory目录下找到几个示例,展示如何使用框架的不同功能/apollo/cyber/examples/
。
注意:该示例必须在apollo docker环境中运行,并使用Bazel进行编译。
1、设置组件文件结构
请创建以下文件,假定位于目录下/apollo/cyber/examples/common_component_example/
:
- 头文件:common_component_example.h
- 源文件:common_component_example.cc
- 构建文件:BUILD
- DAG依赖项文件:common.dag
- 启动文件:common.launch
2、实现组件类
实现组件的头文件
实施common_component_example.h
:
- 继承Component类
- 定义你自己Init和Proc功能。Proc函数需要指定其输入数据类型
- 通过使用将您的组件类注册为全局组件 CYBER_REGISTER_COMPONENT
#include <memory>
#include "cyber/class_loader/class_loader.h"
#include "cyber/component/component.h"
#include "cyber/examples/proto/examples.pb.h"
using apollo::cyber::examples::proto::Driver;
using apollo::cyber::Component;
using apollo::cyber::ComponentBase;
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;
};
CYBER_REGISTER_COMPONENT(CommonComponentSample)
Proc类似于回调函数;
实现示例组件的源文件
对于common_component_example.cc
,都必须同时实现Init和Proc功能。
#include "cyber/examples/common_component_example/common_component_example.h"
#include "cyber/class_loader/class_loader.h"
#include "cyber/component/component.h"
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;
}
为示例组件创建构建文件
创建bazel BUILD文件。
load("//tools:cpplint.bzl", "cpplint")
package(default_visibility = ["//visibility:public"])
cc_binary(
name = "libcommon_component_example.so",
deps = [":common_component_example_lib"],
linkopts = ["-shared"],
linkstatic = False,
)
cc_library(
name = "common_component_example_lib",
srcs = [
"common_component_example.cc",
],
hdrs = [
"common_component_example.h",
],
deps = [
"//cyber",
"//cyber/examples/proto:examples_cc_proto",
],
)
cpplint()
3、设置配置文件
配置DAG依赖文件
要配置DAG依赖文件(common.dag),请指定以下各项:
- 通道名称:用于数据输入和输出
- 库路径:从组件类构建的库
- 类名:组件的类名
# Define all coms in DAG streaming.
component_config {
component_library : "/apollo/bazel-bin/cyber/examples/common_component_example/libcommon_component_example.so"
components {
class_name : "CommonComponentSample"
config {
name : "common"
readers {
channel: "/apollo/prediction"
}
readers {
channel: "/apollo/test"
}
}
}
}
配置启动文件
要配置启动(common.launch)文件,请指定以下各项:
- 组件名称
- 您在上一步中刚刚创建的dag文件。
- 组件在其中运行的进程的名称
<cyber>
<component>
<name>common</name>
<dag_conf>/apollo/cyber/examples/common_component_example/common.dag</dag_conf>
<process_name>common</process_name>
</component>
</cyber>
4、启动组件
通过运行以下命令来构建组件:
bash /apollo/apollo.sh build
注意:确保示例组件构建良好
然后配置环境:
cd /apollo/cyber
source setup.bash
有两种启动组件的方法:
与启动文件一起启动(推荐)
cyber_launch start /apollo/cyber/examples/common_component_example/common.launch
使用DAG文件启动
mainboard -d /apollo/cyber/examples/common_component_example/common.dag
参考案例:
https://github.com/gruminions/apollo/blob/record/cyber/examples/talker.cc