Cyber-定时器组件

 

/*
    定时器组件
    需求:
        周期性的执行某种操作, 比如周期性的写channel
    准备:
        在demo_cc目录下新建文件夹:component_timer,并在component_timer下新建BUILD文件
    流程:
        1.自定义类继承Timer Component类,并重写其Init()与Proc()函数;
        2.编写dag和launch文件
        3.编写BUILD
        4.编译并执行
*/
////////////////////////////.h文件//////////////////////////////
// 在component_timer目录下新建c++文件timer_cpt.h

#include "cyber/component/component.h"
#include "cyber/component/timer_component.h"

using apollo::cyber::Component;
using apollo::cyber::TimerComponent;

class MyTimer : public TimerComponent{
public:
    bool Init() override;
    bool Proc() override;
private:
    int seq;
}
CYBER_REGISTER_COMPONENT(MyTimer)
/////////////////////////////////////////////////////////

////////////////////////////.cpp文件//////////////////////////////
// 在component_timer目录下新建c++文件timer_cpt.cpp

#include "cyber/component_timer/timer_cpt.h"

bool MyTimer::Init()
{
    seq = 0;
    AINFO << "init...";
    return true;
}

bool MyTimer::Proc()
{
    ++seq;
    AINFO << " ------------ " << seq;
    return true;
}
/////////////////////////////////////////////////////////////////

//////////////////////////BUILD///////////////////////////////////////
package(default_visibility = ["//visibility:public"])
load("//tools/install:install.bzl", "install") // build不会自动导入install,需要手动导入

cc_library(
    name = "timer_cpt_cc_lib",
    srcs = ["timer_cpt.cc"],
    hdrs = ["timer_cpt.h"],
    deps = [
            "//cyber"
    ]
)

cc_binary(
    name = "timer_cpt_cc_bin.so",
    linkshared = True,
    linkstatic = False,
    deps = [":timer_cpt_cc_lib"]
)

filegroup(
    name = "conf",
    // 将dag文件和launch文件打包
    srcs = [
            ":cpt.dag",
            ":cpt.launch"
    ]
)

// 安装dag和launch
install(
    name = "install",
    data = [
            ":conf"
    ],
    runtime_dest = "cyber/demo_cc/component_timer"  // 当前包相对路径
    targets = [

        ":timer_cpt_cc_bin.so" // 组件的动态库
    ]
)


/////////////////////////////////////////////////////////////////

////////////////////////////dag文件/////////////////////////////////////
// 在component_timer目录下新建cpt.dag文件
module_config {
    // 动态库路径
    module_library : "/apollo/bezal-bin/cyber/demo_cc/component_timer/timer_cpt_cc_bin.so"
    timer_components {
        class_name : "MyTimer" // 类名
        config {
            // 该配置文件名称,自定义
            name : "my_timer"
            interval: 10  // 循环间隔10ms
        }
    }
}
/////////////////////////////////////////////////////////////////

///////////////////////////launch文件//////////////////////////////////////
// 在component_common02目录下新建cpt.launch文件
<cyber>
    <module>
        <name>my_timer/name>
        // 指向dag文件的绝对路径
        <dag_conf>/apollo/bezal-bin/cyber/demo_cc/component_timer/cpt.dag</dag_conf>
        <process_name>my_timer</process_name>
    </module>
</cyber>
/////////////////////////////////////////////////////////////////

 

posted @ 2023-09-18 13:44  WTSRUVF  阅读(36)  评论(0编辑  收藏  举报