Apollo 应用与源码分析:CyberRT-组件(component)
创始人
2024-02-10 05:03:47
0

目录

概念

二进制与组件

基本的使用方法

使用组件的优势

Dag文件

样例

参数含义

样例

Common_component_example

Timer_component_example

构建与运行

注意

Apollo Guardian 样例分析

guardian_component.h

guardian_component.cc

dag

build

launch


概念

组件是cyberrt提供的用于构建应用程序模块的基类。

每个特定的应用程序模块都可以继承Component类,并定义自己的Init和Proc函数,以便将其加载到Cyber框架中。

二进制与组件

将Cyber RT框架用于应用程序有两个选项:

  • 基于二进制文件:应用程序单独编译成二进制文件,通过创建自己的Reader和Writer与其他网络模块通信。
  • 基于组件:应用程序被编译成共享库。通过继承component类并编写相应的dag描述文件,Cyber RT框架将动态加载和运行应用程序。

基本的使用方法

主要是需要继承component类,然后重写两个函数: init和proc

  • 组件的Init()函数类似于对算法进行一些初始化的主要函数。
  • 组件的Proc()函数的工作原理类似于框架在消息到达时调用的Reader回调函数。


使用组件的优势

  • 组件可以通过启动文件加载到不同的进程中,并且部署是灵活的。
  • 组件可以通过修改dag文件而不重新编译来更改收到的通道名称。
  • 组件支持接收多种类型的数据。
  • 组件支持提供多种融合策略。

Dag文件

样例

# Define all coms in DAG streaming.
module_config {module_library : "lib/libperception_component.so"components {class_name : "PerceptionComponent"config {name : "perception"readers {channel: "perception/channel_name"}}}timer_components {class_name : "DriverComponent"config {name : "driver"interval : 100}}
}

参数含义

  • module_library:如果您想加载.so库,根目录是网络的工作目录(setup.bash的同一目录)
  • components & timer_component:选择需要加载的基本组件类类型。
  • class_name:要加载的组件类的名称
  • name:加载的class_name作为加载示例的标识符
  • readers:当前组件收到的数据,支持1-3个数据通道。

样例

Common_component_example

#include #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 Commontestcomponent : public Component {public:bool Init() override;bool Proc(const std::shared_ptr& msg0,const std::shared_ptr& msg1) override;
};
CYBER_REGISTER_COMPONENT(Commontestcomponent)  // 注册到反射框架中
#include "cyber/examples/common_component_smaple/common_component_example.h"#include "cyber/class_loader/class_loader.h"
#include "cyber/component/component.h"bool Commontestcomponent::Init() {AINFO << "Commontest component init";return true;
}bool Commontestcomponent::Proc(const std::shared_ptr& msg0,const std::shared_ptr& msg1) {AINFO << "Start commontest component Proc [" << msg0->msg_id() << "] ["<< msg1->msg_id() << "]";return true;
} 

Timer_component_example

#include #include "cyber/class_loader/class_loader.h"
#include "cyber/component/component.h"
#include "cyber/component/timer_component.h"
#include "cyber/examples/proto/examples.pb.h"using apollo::cyber::examples::proto::Driver;
using apollo::cyber::Component;
using apollo::cyber::ComponentBase;
using apollo::cyber::TimerComponent;
using apollo::cyber::Writer;class TimertestComponent : public TimerComponent {public:bool Init() override;bool Proc() override;private:std::shared_ptr> driver_writer_ = nullptr;
};
CYBER_REGISTER_COMPONENT(TimertestComponent)
#include "cyber/examples/timer_component_example/timer_component_example.h"#include "cyber/class_loader/class_loader.h"
#include "cyber/component/component.h"
#include "cyber/examples/proto/examples.pb.h"bool TimertestComponent::Init() {driver_writer_ = node_->CreateWriter("/carstatus/channel");return true;
}bool TimertestComponent::Proc() {static int i = 0;auto out_msg = std::make_shared();out_msg->set_msg_id(i++);driver_writer_->Write(out_msg);AINFO << "timertestcomponent: Write drivermsg->"<< out_msg->ShortDebugString();return true;
}

构建与运行

  • Build: bazel build cyber/examples/timer_component_smaple/…
  • Run: mainboard -d cyber/examples/timer_component_smaple/timer.dag

注意

  • 需要注册组件才能通过SharedLibrary加载类。注册界面如下所示:
CYBER_REGISTER_COMPONENT(DriverComponent) 

如果您在注册时使用命名空间,则在dag文件中定义命名空间时,您还需要添加命名空间。

  • Component和TimerComponent的配置不同,请注意不要将两者混淆。

Apollo Guardian 样例分析

guardian 相对比较简单,可以用于分析component的使用

guardian_component.h

/******************************************************************************* Copyright 2018 The Apollo Authors. All Rights Reserved.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*****************************************************************************//*** @file*/#pragma once#include #include "cyber/common/macros.h"
#include "cyber/component/timer_component.h"
#include "cyber/cyber.h"#include "modules/canbus/proto/chassis.pb.h"
#include "modules/control/proto/control_cmd.pb.h"
#include "modules/guardian/proto/guardian.pb.h"
#include "modules/guardian/proto/guardian_conf.pb.h"
#include "modules/monitor/proto/system_status.pb.h"/*** @namespace apollo::guardian* @brief apollo::guardian*/
namespace apollo {
namespace guardian {class GuardianComponent : public apollo::cyber::TimerComponent {public:bool Init() override;bool Proc() override;private:void PassThroughControlCommand();void TriggerSafetyMode();apollo::guardian::GuardianConf guardian_conf_;apollo::canbus::Chassis chassis_;apollo::monitor::SystemStatus system_status_;apollo::control::ControlCommand control_cmd_;apollo::guardian::GuardianCommand guardian_cmd_;double last_status_received_s_{};std::shared_ptr>chassis_reader_;std::shared_ptr>control_cmd_reader_;std::shared_ptr>system_status_reader_;std::shared_ptr>guardian_writer_;std::mutex mutex_;
};CYBER_REGISTER_COMPONENT(GuardianComponent)}  // namespace guardian
}  // namespace apollo

guardian_component.cc

/******************************************************************************* Copyright 2018 The Apollo Authors. All Rights Reserved.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*****************************************************************************/
#include "modules/guardian/guardian_component.h"#include "cyber/common/log.h"
#include "modules/common/adapters/adapter_gflags.h"
#include "modules/common/util/message_util.h"using Time = ::apollo::cyber::Time;namespace apollo {
namespace guardian {using apollo::canbus::Chassis;
using apollo::control::ControlCommand;
using apollo::monitor::SystemStatus;bool GuardianComponent::Init() {if (!GetProtoConfig(&guardian_conf_)) {AERROR << "Unable to load canbus conf file: " << ConfigFilePath();return false;}chassis_reader_ = node_->CreateReader(FLAGS_chassis_topic, [this](const std::shared_ptr& chassis) {ADEBUG << "Received chassis data: run chassis callback.";std::lock_guard lock(mutex_);chassis_.CopyFrom(*chassis);});control_cmd_reader_ = node_->CreateReader(FLAGS_control_command_topic,[this](const std::shared_ptr& cmd) {ADEBUG << "Received control data: run control callback.";std::lock_guard lock(mutex_);control_cmd_.CopyFrom(*cmd);});system_status_reader_ = node_->CreateReader(FLAGS_system_status_topic,[this](const std::shared_ptr& status) {ADEBUG << "Received system status data: run system status callback.";std::lock_guard lock(mutex_);last_status_received_s_ = Time::Now().ToSecond();system_status_.CopyFrom(*status);});guardian_writer_ = node_->CreateWriter(FLAGS_guardian_topic);return true;
}bool GuardianComponent::Proc() {constexpr double kSecondsTillTimeout(2.5);bool safety_mode_triggered = false;if (guardian_conf_.guardian_enable()) {std::lock_guard lock(mutex_);if (Time::Now().ToSecond() - last_status_received_s_ >kSecondsTillTimeout) {safety_mode_triggered = true;}safety_mode_triggered =safety_mode_triggered || system_status_.has_safety_mode_trigger_time();}if (safety_mode_triggered) {ADEBUG << "Safety mode triggered, enable safety mode";TriggerSafetyMode();} else {ADEBUG << "Safety mode not triggered, bypass control command";PassThroughControlCommand();}common::util::FillHeader(node_->Name(), &guardian_cmd_);guardian_writer_->Write(guardian_cmd_);return true;
}void GuardianComponent::PassThroughControlCommand() {std::lock_guard lock(mutex_);guardian_cmd_.mutable_control_command()->CopyFrom(control_cmd_);
}void GuardianComponent::TriggerSafetyMode() {AINFO << "Safety state triggered, with system safety mode trigger time : "<< system_status_.safety_mode_trigger_time();std::lock_guard lock(mutex_);bool sensor_malfunction = false, obstacle_detected = false;if (!chassis_.surround().sonar_enabled() ||chassis_.surround().sonar_fault()) {AINFO << "Ultrasonic sensor not enabled for faulted, will do emergency ""stop!";sensor_malfunction = true;} else {// TODO(QiL) : Load for configfor (int i = 0; i < chassis_.surround().sonar_range_size(); ++i) {if ((chassis_.surround().sonar_range(i) > 0.0 &&chassis_.surround().sonar_range(i) < 2.5) ||chassis_.surround().sonar_range(i) > 30) {AINFO << "Object detected or ultrasonic sensor fault output, will do ""emergency stop!";obstacle_detected = true;}}}guardian_cmd_.mutable_control_command()->set_throttle(0.0);guardian_cmd_.mutable_control_command()->set_steering_target(0.0);guardian_cmd_.mutable_control_command()->set_steering_rate(25.0);guardian_cmd_.mutable_control_command()->set_is_in_safe_mode(true);// TODO(QiL) : Remove this one once hardware re-alignment is done.sensor_malfunction = false;obstacle_detected = false;AINFO << "Temporarily ignore the ultrasonic sensor output during hardware ""re-alignment!";if (system_status_.require_emergency_stop() || sensor_malfunction ||obstacle_detected) {AINFO << "Emergency stop triggered! with system status from monitor as : "<< system_status_.require_emergency_stop();guardian_cmd_.mutable_control_command()->set_brake(guardian_conf_.guardian_cmd_emergency_stop_percentage());} else {AINFO << "Soft stop triggered! with system status from monitor as : "<< system_status_.require_emergency_stop();guardian_cmd_.mutable_control_command()->set_brake(guardian_conf_.guardian_cmd_soft_stop_percentage());}
}}  // namespace guardian
}  // namespace apollo

dag

module_config {module_library : "/apollo/bazel-bin/modules/guardian/libguardian_component.so",timer_components {class_name : "GuardianComponent"config {name: "guardian"config_file_path: "/apollo/modules/guardian/conf/guardian_conf.pb.txt"interval: 10}}
}

build

load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
load("//tools/install:install.bzl", "install")
load("//tools:cpplint.bzl", "cpplint")package(default_visibility = ["//visibility:public"])GUARDIAN_COPTS = ['-DMODULE_NAME=\\"guardian\\"']cc_library(name = "guardian_component_lib",srcs = ["guardian_component.cc"],hdrs = ["guardian_component.h"],copts = GUARDIAN_COPTS,deps = ["//cyber","//modules/canbus/proto:chassis_cc_proto","//modules/common/adapters:adapter_gflags","//modules/common/util:message_util","//modules/control/proto:control_cmd_cc_proto","//modules/guardian/proto:guardian_cc_proto","//modules/guardian/proto:guardian_conf_cc_proto","//modules/monitor/proto:system_status_cc_proto",],
)cc_binary(name = "libguardian_component.so",linkshared = True,linkstatic = False,deps = [":guardian_component_lib",],
)install(name = "install",data = [":runtime_data",],targets = [":libguardian_component.so",],deps = ["//cyber:install",],
)filegroup(name = "runtime_data",srcs = glob(["conf/*.txt","dag/*.dag","launch/*.launch",]),
)cpplint()

launch

guardian/apollo/modules/guardian/dag/guardian.dagguardian

相关内容

热门资讯

以色列加密交易平台eToro美...   以色列股票和加密货币交易平台eToro集团周二表示,计划在美国进行规模更大的首次公开募股(IPO...
两位家居业“大佬”先后被调查留...   继居然智家董事长汪林朋之后,又一家居龙头企业创始人被留置。  5月13日晚间,A股上市公司美凯龙...
重磅数据公布!特朗普再怼鲍威尔...   当地时间5月13日,美股三大指数收盘涨跌不一。纳指上涨301.74点,涨幅1.61%;标普500...
第九届上海合作组织成员国教育部...   新华社乌鲁木齐5月13日电(记者贾钊、张瑜)第九届上海合作组织成员国教育部长会议13日在新疆乌鲁...
安徽新增两处国家林木种质资源库   记者从省林业局获悉,近日,国家林业和草原局公布第四批国家林木种质资源库,我省铜陵市国有林场泡桐国...
众安在线前4个月原保险保费收入... .ct_hqimg {margin: 10px 0;} .hqimg_wrapper {text-a...
分析师:英伟达或将很快取代苹果...   英伟达周二上涨5.6%后,其市值现已回升至3.1万亿美元以上。该公司股价目前已较4月份低点上涨3...
在传统与当代的经纬间“织锦” 图为昂桑的作品《藏女与花》。 图为昂桑的作品《十二生肖》。 图/文 本报记者 格桑伦珠 在拉萨...
完善知识产权制度 促进人工智能... 转自:千龙网今年全国知识产权宣传周的主题是“知识产权与人工智能”‌,涉及知识产权与人工智能技术相互支...
美国佛蒙特州暂停电动汽车销售要...   美国佛蒙特州州长菲尔·斯科特(Phil Scott)周二暂停了该州对乘用车、中型和重型卡车的电动...
浙江宣传:“预制式”调研不是真... 转自:北京日报客户端说起“预制菜”,想必很多人都不陌生。“预制菜”有着一样的配方、一样的味道,好处是...
俄乌代表团将同日抵达土耳其,泽... 俄乌会谈双方代表团将于5月15日抵达土耳其。乌克兰总统泽连斯基强调,他已准备在土耳其与俄罗斯总统普京...
发扬党的优良传统 恪尽职守努... 本报拉萨5月13日讯(记者 刘文涛 张黎黎 张尚华)5月12日,自治区党委书记王君正在拉萨与新任职干...
一季度日赚1亿多,京东集团CE... 转自:财联社《科创板日报》5月14日讯(记者 徐赐豪)进入外卖市场后,京东备受瞩目的一季报新鲜出炉。...
银河证券:看好AI+重铸电子行... 人民财讯5月14日电,银河证券研报指出,梳理电子行业一季度,认为AI基础设施建设所带动的相关硬件如A...
“读懂中国式现代化与现代化成果...   本报北京5月13日电  (记者张晓东)近日,中国科学院中国现代化研究中心联合人民出版社、北京大学...
石榴花开香喷喷 山歌唱响苗乡乐   “万紫千红大地春,石榴花开香喷喷。山歌唱响苗乡乐,清亲飞歌庆太平……”春耕时节的湖南省城步苗族自...
代表委员关注这件事|王绍南代表... 本报讯(记者吴贻伙)“农村基层治理不仅仅是乡村振兴的重要内容,更是国家治理体系的重要组成部分。”近日...
我省优化完善部分货车通行费优惠... 转自:新华日报近日,省交通运输厅、省发展改革委、省财政厅联合印发《关于优化完善我省部分货车车辆通行费...
国内核聚变项目建设加速 机构称... 媒体报道,日本政府近日宣布,将参与为设计耐用性较高且安全的核聚变反应堆、正在欧洲推进的建材试验计划。...