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

相关内容

热门资讯

蜻蜓订购APP贵金属订购非法期...   蜻蜓订购APP以"投资平台"名义对外宣传,主打贵金属期货(订购)交易。该平台一方面强调自身"正规...
悟空订购APP非法期货交易欺骗...   近期,有投资者反映,“悟空订购APP”等平台以白银、贵金属订购为名,宣传所谓“高收益”“低门槛”...
小赢淘金APP期货投资骗局,平...   “小赢淘金”之流,最擅长的就是用“注册送现金”、“充值返券”的糖衣炮弹来引流。别天真地以为那是福...
八方淘金APP非法期货交易欺骗...   警惕!八方生财、每日淘金、小赢淘金、悟空订购、蜻蜓订购、斗金订购等APP均涉嫌非法期货交易!  ...
智易购APP贵金属订购交易骗局...   很多人看到“3元起投”“充值送福利”“操作简单”“随时提现”这类宣传,很容易把智易购APP当成普...
喜玛银庄APP商品订购投资骗局...   喜玛银庄APP白银、铂金“现货订购”骗局警示:无资质平台涉嫌非法期货交易,投资者爆仓后如何维权?...
吉鑫乐享APP炒贵金属订购交易...   近期,多名投资者反映在“吉鑫乐享APP”进行贵金属投资时遭遇严重亏损,甚至血本无归。看似正规的“...
易掘金APP虚假期货投资骗局,...   “易掘金”的APP它打着“高收益、灵活投资”的旗号,宣称通过“商品订购”就能轻松赚钱,吸引了不少...
在大吉订购APP订购交易亏损被...   近期,不少投资者反映在“大吉订购App”上投资白银、铂金等商品遭遇严重亏损。该平台宣称提供现货订...
铂银壹号APP商品订购交易亏损...   许多中小投资者误以为网上的“商品现货订购”是合规合法的理财渠道,殊不知这背后隐藏着巨大的合规风险...
兴鑫淘金APP贵金属订购交易亏...   白银、贵金属或其他商品类投资,除国家批准的正规期货交易场所及持牌期货公司外,市面上大量第三方交易...
铂银掘金APP无资质的违规平台...   铂银掘金等平台的投资者,往往是被“注册送现金”“充值返现券”“首单免亏”等广告话术吸引,进而下载...
八方淘金APP订购交易不合规,...   “八方淘金APP”“铂银掘金APP”“铂银壹号APP”等软件被指以“现货订购”为名,实际采用高杠...
在指尖订购APP订购商品亏损被...   指尖订购APP这类平台,表面是“订购交易”,实则是披着电商外衣的非法期货陷阱。绝大多数投资者被蒙...
在犀贝易购APP做商品订购交易...   市面上打着“现货投资”“贵金属交易”旗号的APP并不少,但真正合规、受监管的平台并不多。犀贝易购...
易达淘金APP虚假商品订购交易...   易达淘金APP存在严重的虚假宣传与违规交易行为。该平台打着“商品订购”的旗号,实则通过不合规的交...
大浪淘金APP白银订购亏损被骗...   大浪淘金APP最隐蔽的套路,在于其完全封闭的内盘交易系统。表面上,它宣称与国际贵金属市场接轨;实...
一品好购APP上订购白银出现亏...   一品好购APP原名“一品订购”,运营方为上海宝智未来信息技术有限公司。近期不少投资者反映在该平台...
九州订购APP贵金属订购投资骗...   九州订购APP正通过抖.音、快手、今日头条等平台大量投放投资广告,用“5元起投”“充值返现”“0...
坚锅淘金APP交易软件并不正规...   当前,市面上大量所谓的“现货商城订购”交易软件,早已偏离了正常的实物商品买卖,演变为高杠杆、高手...