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

相关内容

热门资讯

龙虎榜揭秘!大牛股背后资金动向... 龙虎榜揭秘。 近期A股市场整体波动较为平稳,但不少个股波动剧烈,甚至连续涨停或连续跌停,近日的龙虎榜...
原创 全... 全球都在用美元? 中国偷偷搞了个大动作! 美元占全球支付50%时人民币干了啥? 你可能不知道的是,当...
我国银行理财市场规模突破33万... 银行业理财登记托管中心1月23日发布的《中国银行业理财市场年度报告(2025年)》显示,截至2025...
最高分红率35%!上市银行春节... 随着春节临近,上市银行2025年中期分红逐渐进入尾声。 1月23日,华夏银行、渝农商行迎来2025年...
蔡英丽医生:帕金森患者麻醉注意... 帕金森病是中老年人常见的神经系统退行性疾病,随着病情进展,不少患者可能需要接受各类手术,而麻醉环节的...
原创 利... 朋友们,今天A股发生了一件挺有意思的事:在地面光伏行业不少公司还在为亏损发愁的时候,一个叫做“太空光...
二游王战之局,鹰角先下一城 2026二游王战的启幕来了。1月22日,鹰角《明日方舟:终末地》(以下简称终末地)正式公测,和我们预...
大润发首次跨界合作蛋仔派对,以... 2026年春节前夕,高鑫零售旗下核心品牌大润发与国民级游戏IP《蛋仔派对》正式达成深度跨界合作,共同...
2025年基金4季报重仓股全扫... 随着基金2025年4季报基本披露完毕,记者注意到,截至去年底,基金的重仓股发生了比较明显的变化,有5...
昔日“疫苗之王”科兴控股大消息... 让科兴控股小股东忐忑两个多月的退市事项,迎来新进展。 1月22日晚间,科兴控股生物技术有限公司(以下...
视频丨“片场”成打卡新去处 解... 最近,多个因电影而火热的城市持续收获关注,并积极筹备春节文旅服务。在河南安阳,电影《封神2》对殷商文...
东方新能源汽车主题混合:202... AI基金东方新能源汽车主题混合(400015)披露2025年四季报,第四季度基金利润8586.66万...
数字人民币生态日趋成熟,服贸会... 2025年中国国际服务贸易交易会(以下简称“服贸会”)正在北京举行。作为服贸会九大专题之一,本届金融...
证监会进一步扩大期货市场开放品... 近日,证监会根据《境外交易者和境外经纪机构从事境内特定品种期货交易管理暂行办法》(证监会令第116号...
医保搭台、产业唱戏 多部委协同... 党的二十届四中全会对“十五五”时期扩大高水平对外开放作出了顶层设计和战略擘画,强调了与世界各国共享机...
合众财险2025年揽收保费7.... (图片来源:视觉中国) 蓝鲸新闻1月23日讯(记者 陈晓娟)今日,合众财产保险股份有限公司(下称“合...
仕佳光子:公司高度重视全体股东... 证券日报网讯 1月23日,仕佳光子在互动平台回答投资者提问时表示,公司高度重视全体股东的合法权益,始...
透视行业变革 共话ETF未来—... 1月23日下午,2026年第四届基金生态大会在杭举办。本届大会由同花顺(300033.SZ)与财闻传...
原创 3... 32岁程序员高先生在周六处理工作时突发不适,经抢救无效离世,这起事件引发了关于周末居家加班猝死能否认...