Apollo 应用与源码分析:CyberRT-服务通信与参数服务器
admin
2024-02-06 19:01:52
0

目录

服务通信

基本概念

定义请求、反馈消息结构

创建服务端和客户端

BUILD

参数服务器

支持的参数类型

创建参数对象

构造函数

对应样例

接口和数据读取

API

样例

创建参数服务器

API

参数服务器设置参数

API

参数服务器获取参数

API

获取参数列表

API

创建参数客户端

API

参数服务器样例


服务通信

基本概念

在自动驾驶系统中,有许多场景需要更多的模块通信,而不仅仅是发送或接收消息。服务是节点之间通信的另一种方式。与通道不同,服务实现双向通信,例如节点通过发送请求获得响应。

定义请求、反馈消息结构

在cyberRT中通信使用的是protobuf

syntax = "proto2";
package apollo.cyber.examples.proto;
message Driver {optional string content = 1;optional uint64 msg_id = 2;optional uint64 timestamp = 3;
};

创建服务端和客户端

#include "cyber/cyber.h"
#include "cyber/examples/proto/examples.pb.h"using apollo::cyber::examples::proto::Driver;int main(int argc, char* argv[]) {apollo::cyber::Init(argv[0]);std::shared_ptr node(apollo::cyber::CreateNode("start_node"));// 创建一个服务,其中包括了一个回掉函数auto server = node->CreateService("test_server", [](const std::shared_ptr& request,std::shared_ptr& response) {AINFO << "server: I am driver server";static uint64_t id = 0;++id;response->set_msg_id(id);response->set_timestamp(0);});// 创建一个客户端auto client = node->CreateClient("test_server");auto driver_msg = std::make_shared();driver_msg->set_msg_id(0);driver_msg->set_timestamp(0);while (apollo::cyber::OK()) {// 发送请求auto res = client->SendRequest(driver_msg);if (res != nullptr) {AINFO << "client: response: " << res->ShortDebugString();} else {AINFO << "client: service may not ready.";}sleep(1);}apollo::cyber::WaitForShutdown();return 0;
}

BUILD

cc_binary(name = "service",srcs = [ "service.cc", ],deps = ["//cyber","//cyber/examples/proto:examples_cc_proto",],
)

参数服务器

支持的参数类型

在apollo 中所有的参数都是apollo::cyber::Parameter 对象

Parameter type                             | C++  type   | protobuf data type 
:————-                                     | :————-      | :————– 
apollo::cyber::proto::ParamType::INT       | int64_t     | int64 
apollo::cyber::proto::ParamType::DOUBLE    | double      | double 
apollo::cyber::proto::ParamType::BOOL      | bool        | bool 
apollo::cyber::proto::ParamType::STRING    | std::string | string 
apollo::cyber::proto::ParamType::PROTOBUF  | std::string | string 
apollo::cyber::proto::ParamType::NOT_SET   | -           | -

创建参数对象

构造函数

Parameter();  // Name is empty, type is NOT_SET
explicit Parameter(const Parameter& parameter);
explicit Parameter(const std::string& name);  // type为NOT_SET
Parameter(const std::string& name, const bool bool_value);
Parameter(const std::string& name, const int int_value);
Parameter(const std::string& name, const int64_t int_value);
Parameter(const std::string& name, const float double_value);
Parameter(const std::string& name, const double double_value);
Parameter(const std::string& name, const std::string& string_value);
Parameter(const std::string& name, const char* string_value);
Parameter(const std::string& name, const std::string& msg_str,const std::string& full_name, const std::string& proto_desc);
Parameter(const std::string& name, const google::protobuf::Message& msg);

对应样例

Parameter a("int", 10);
Parameter b("bool", true);
Parameter c("double", 0.1);
Parameter d("string", "cyber");
Parameter e("string", std::string("cyber"));
// proto message Chatter
Chatter chatter;
Parameter f("chatter", chatter);
std::string msg_str("");
chatter.SerializeToString(&msg_str);
std::string msg_desc("");
ProtobufFactory::GetDescriptorString(chatter, &msg_desc);
Parameter g("chatter", msg_str, Chatter::descriptor()->full_name(), msg_desc);

接口和数据读取

API

inline ParamType type() const;
inline std::string TypeName() const;
inline std::string Descriptor() const;
inline const std::string Name() const;
inline bool AsBool() const;
inline int64_t AsInt64() const;
inline double AsDouble() const;
inline const std::string AsString() const;
std::string DebugString() const;
template 
typename std::enable_if::value, Type>::type
value() const;
template 
typename std::enable_if::value && !std::is_same::value, Type>::type
value() const;
template 
typename std::enable_if::value, Type>::type
value() const;
template 
typename std::enable_if::value, const std::string&>::type
value() const;
template 
typename std::enable_if::value, bool>::type
value() const;

样例

Parameter a("int", 10);
a.Name();  // return int
a.Type();  // return apollo::cyber::proto::ParamType::INT
a.TypeName();  // return string: INT
a.DebugString();  // return string: {name: "int", type: "INT", value: 10}
int x = a.AsInt64();  // x = 10
x = a.value();  // x = 10
x = a.AsString();  // Undefined behavior, error log prompt
f.TypeName();  // return string: chatter
auto chatter = f.value();

创建参数服务器

API

/*** @brief Construct a new ParameterService object** @param node shared_ptr of the node handler*/
explicit ParameterService(const std::shared_ptr& node);

参数服务器设置参数

API

/*** @brief Set the Parameter object** @param parameter parameter to be set*/
void SetParameter(const Parameter& parameter);

参数服务器获取参数

API

/*** @brief Get the Parameter object** @param param_name* @param parameter the pointer to store* @return true* @return false call service fail or timeout*/
bool GetParameter(const std::string& param_name, Parameter* parameter);

获取参数列表

API

/*** @brief Get all the Parameter objects** @param parameters pointer of vector to store all the parameters* @return true* @return false call service fail or timeout*/
bool ListParameters(std::vector* parameters);

创建参数客户端

API

/*** @brief Construct a new ParameterClient object** @param node shared_ptr of the node handler* @param service_node_name node name which provide a param services*/
ParameterClient(const std::shared_ptr& node, const std::string& service_node_name);

参数服务器样例

#include "cyber/cyber.h"
#include "cyber/parameter/parameter_client.h"
#include "cyber/parameter/parameter_server.h"using apollo::cyber::Parameter;
using apollo::cyber::ParameterServer;
using apollo::cyber::ParameterClient;int main(int argc, char** argv) {apollo::cyber::Init(*argv);std::shared_ptr node =apollo::cyber::CreateNode("parameter");auto param_server = std::make_shared(node);auto param_client = std::make_shared(node, "parameter");param_server->SetParameter(Parameter("int", 1));Parameter parameter;param_server->GetParameter("int", ¶meter);AINFO << "int: " << parameter.AsInt64();param_client->SetParameter(Parameter("string", "test"));param_client->GetParameter("string", ¶meter);AINFO << "string: " << parameter.AsString();param_client->GetParameter("int", ¶meter);AINFO << "int: " << parameter.AsInt64();return 0;
}

相关内容

热门资讯

消息称百度旗下昆仑芯瞄准500... 6 月 29 日消息,据《The Information》昨日援引知情人士消息,百度旗下 AI 芯片...
打造夏日消费新场景 第35届北... 北京商报讯(记者 翟枫瑞)6月29日消息,第35届北京国际燕京啤酒文化节新闻发布会在京举行。本届啤酒...
社保基金持仓数据出炉,一季度增... 最近各大上市公司一季度财报都公开了,咱们国家社保基金的持仓数据也全部曝光。目前社保拿着比亚迪价值44...
36氪首发 | 海思、中兴团队... 作者 | 乔钰杰 编辑 | 袁斯来 硬氪获悉,广州宸思通讯科技有限公司(以下简称“宸思科技”)近日完...
两天蒸发47亿市值!一纸税务通... 一纸税务通知书,能让一家百亿龙头两天蒸发47亿市值。 6月22日,北大荒(600598.SH)公告称...
SK海力士将投资1100万亿韩... SK集团会长崔泰源6月29日在韩国“三大重大计划”发布会上宣布,公司将投资1100万亿韩元扩大半导体...
两只A股,终止上市! 两家A股公司,即将摘牌。 6月29日,退市沪科(600608.SH)公告称,上海证券交易所将在202...
原创 M... 一家成立近十年的自动驾驶公司,在IPO时吸引了14家基石投资者认购近一半的发行股份,其中不乏奔驰、比...
基金忠言|国寿安保滤镜碎,三年... 图片来源:视觉中国 蓝鲸新闻6月29日讯(记者 祁和忠)保险系基金公司国寿安保总经理换人了。 6月2...
三星电机计划加码玻璃基板!相关... 6月29日,玻璃基板概念股午后有所回升, 华工科技(000988.SZ)逼近涨停, 彩虹股份(600...
拉萨海关持续壮大外贸经营主体 ...   新华网拉萨6月28日电(记者蒋梦辰)近日,记者从拉萨海关获悉,今年前5个月,西藏有进出口实绩的外...
机构:二季报临近,医药生物板块... 6月29日,华源证券发布了一篇医药生物行业的研究报告,报告指出,业绩期临近,产业链景气度有望再次迎来...
每日收评科创50放量涨超4.5... 财联社6月29日讯,三大指数全线收红,创业板指探底回升,科创50指数大涨4.61%。沪深两市成交额3...
6月多地土拍结构性升温:深圳单... 进入2026年6月,不少城市核心区地块集中诞生高溢价宗地,热度突出的城市包含深圳、杭州、长沙。 其中...
业绩炸裂!盛达资源半年预盈3.... 6月29日,贵金属矿山龙头盛达资源(000603.SZ)发布 2026 年半年度业绩预告,上半年业绩...
A股午后拉升三大股指收涨:半导... A股三大股指6月29日开盘涨跌互现。早盘沪强深弱,创指一度跌超2%。半导体午后拉升,带动两市上涨,沪...
原创 空... 前言 大家好,我是老金。 这几天,两幅极度割裂的画面放在一起,把我看笑了。 一边是在持续的热浪下,欧...
澳大利亚审慎监管局拟放宽银行风... 澳大利亚审慎监管局(APRA)6月29日就修改 银行信用风险资本设定公开征求意见,旨在加大信贷投放以...
全民炒股,急踩刹车!韩国股市突... 屈红燕/证券时报网 全民狂欢、交易高度拥挤、杠杆资金猛增、新入市投资者表现激进、大型IPO吸金等现象...