Apollo Cyber RT自定义组件

写在前面。网上关于Cyber RT中自定义组件的有很多,其中大部分直接翻译apollo官方github教程——如何使用 Cyber RT 来创建一个新的组件,并未对其中的个个语句给出详细的解释。在次前提下,整理网上资料和翻阅源代码,自学整理,记录在此。2019.11.10

文件结构

  • 程序头文件
  • 程序源文件
  • BUILD bazel编译所需
  • DAG 依赖文件
  • Launch文件

头文件

/*common_component_example.h*/
// std
#include <memory>
// 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;
}

BUILD编译文件

# 从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()

DAG

# 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<Driver, Driver>,
            # 模板中含有两个量,因此此处*必须*要有两个readers
            # 参见源码:/apollo/apollo/cyber/component/component.h 237行
            readers {
                channel: "/apollo/prediction"
            }
            # 接收消息的通道
            readers {
                channel: "/apollo/test"
            }
        }
    }
}

Launch

<cyber>
    <module>
        <name>common</name>
        <dag_conf>/apollo/cyber/examples/common_component_example/common.dag</dag_conf>
        <process_name>common</process_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学习笔记

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值