無人駕駛筆記系列(一):如何使用 Cyber RT 來創建一個新的組件

如何使用 Cyber RT 來創建一個新的組件

寫在最前面的話:這部分全部來源apollo的源碼內的文檔。我只是搬運工,就是爲了查看起來方便。後續會添加一些註釋。

Apollo Cyber 運行時框架 (Apollo Cyber RT Framework) 是基於組件概念來構建的。每個組件都是 Cyber 框架的一個構建塊,它包括一個特定的算法模塊, 此算法模塊處理一組輸入數椐併產生一組輸出數椐。

要創建並啓動一個算法組件,需要通過以下 4 個步驟:

  • 初如化組件的文件結構
  • 實現組件類
  • 設置配置文件
  • 啓動組件

下面的例子展示瞭如何創建,編譯,運行一個組件,並觀察組件在屏幕上的輸出。 如果想更深入的探索 Apollo Cyber RT 框架,可以在這個目錄/apollo/cyber/examples/找到很多例子,這些例子詳細展示瞭如何使用 Cyber 框架的各種功能。

Note: 這些例子必須運行在 Apollo docker 環境, 且需要通過 Bazel 來編譯。

初始化組件文件結構

例如組件的根目錄爲/apollo/cyber/examples/common_component_example/需要創建以下文件:

  • Header file: common_component_example.h
  • Source file: common_component_example.cc
  • Build file: BUILD
  • DAG dependency file: common.dag
  • Launch file: common.launch

實現組件類

實現組件頭文件

如何實現common_component_example.h:

  • 繼承 Component 類
  • 定義自己的 InitProc 函數。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)

實現組件源文件

對於源文件 common_component_example.cc, InitProc 這兩個函數需要實現。

#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;
}

創建 BUILD 編譯文件

創建 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()

設置配置文件

配置 DAG 依賴文件

在 DAG 依賴配置文件 (例如 common.dag) 中配置下面的項:

  • Channel names: 輸入輸出數椐的 Channel 名字
  • Library path: 此組件最終編譯出的庫的名字
  • Class name: 此組件的入口類的名字
# Define all components 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"
            }
        }
    }
}

配置 launch 啓動文件

在 launch 啓動文件中 (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>

啓動這個組件

通過下面的命令來編譯組件:

bash /apollo/apollo.sh build

Note: 確定組件正常編譯成功

然後配置環境:

cd /apollo/cyber
source setup.bash

有兩種方法來啓動組件:

  • 使用 launch 文件來啓動 (推薦這種方式)
cyber_launch start /apollo/cyber/examples/common_component_example/common.launch
  • 使用 dag 文件來啓動
mainboard -d /apollo/cyber/examples/common_component_example/common.dag
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章