写在前面。网上关于Cyber RT中自定义组件的有很多,其中大部分直接翻译apollo官方github教程——如何使用 Cyber RT 来创建一个新的组件,并未对其中的个个语句给出详细的解释。在次前提下,整理网上资料和翻阅源代码,自学整理,记录在此。2019.11.10
/*common_component_example.h*/
// std
#include
// cyber
#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;
/*需要继承Component类,并实现Init和Proc成员函数
若继承自Component<>,则CommonComponentSample中没有继承Proc函数,无需实现*/
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宏定义把组件类注册成全局可用*/
CYBER_REGISTER_COMPONENT(CommonComponentSample)
/*common_component_example.cc*/
#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() {
/*apollo采用google::LogMessage记录日志文件,并保存在apollo/data/log/mainborad.INFO文件中*/
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;
}
# 从apollo/tools/cpplint.bzl文件中加载cpplint
load("//tools:cpplint.bzl", "cpplint")
# default_visibility指定了这个包的默认可见规则。可见的情况下才能被其他package调用。
package(default_visibility = ["//visibility:public"])
# 构建二进制文件
cc_binary(
# 构建二进制文件的名字
name = "libcommon_component_example.so",
#
linkopts = ["-shared"],
#
linkstatic = False,
# 引用当前包中的目标
deps = [":common_component_example_lib"],
)
# 构建库
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",
],
)
# apollo自定义的方法,必须添加到末尾
# 原型为cpplint(data=None, extra_srcs=None)
# 可以传入额外的配置(data)和源文件(extra_srcs)
# 参见:/apollo/apollo/tools/cpplint.bzl 53行
cpplint()
# Define all coms in DAG streaming.
# 与官方教程(component_config)不同此处为:module_config
# 参见:/apollo/cyber/mainboard/module_controller.cc 129行
module_config {
# 模块路径
module_library : "/apollo/bazel-bin/cyber/examples/common_component_example/libcommon_component_example.so"
# 组件
components {
# 类名
class_name : "CommonComponentSample"
# 配置
config {
# 组件的名字
name : "common"
# 接收消息的通道,因为在头文件中自定义的类继承自Component,
# 模板中含有两个量,因此此处*必须*要有两个readers
# 参见源码:/apollo/apollo/cyber/component/component.h 237行
readers {
channel: "/apollo/prediction"
}
# 接收消息的通道
readers {
channel: "/apollo/test"
}
}
}
}
<cyber>
<module>
<name>commonname>
<dag_conf>/apollo/cyber/examples/common_component_example/common.dagdag_conf>
<process_name>commonprocess_name>
module>
cyber>
## 编译
# 如果已经完成apollo的编译,建议使用第二条
# 仅编译所有文件夹中的内容
bash /apollo/apollo.sh build
# 仅编译cyber文件夹中的内容
bash /apollo/apollo.sh build_cyber
cyber_launch start /apollo/cyber/examples/common_component_example/common.launch
如何使用 Cyber RT 来创建一个新的组件
Bazel学习笔记