自学内容网 自学内容网

【SY4进阶】Apollo10.0 Cyber参数实验

实验基础

  • Apollo10.0 源码
  • Docker
  • Vscode
  • Ubuntu20.04
  • 以及完成相关配置

具体可以参考一下这篇文章 icon-default.png?t=O83Ahttps://blog.csdn.net/weixin_60062799/article/details/145029669?spm=1001.2014.3001.5502

实验步骤

步骤一、建立目录

 

步骤二、编写文件

client.cc
#include "cyber/cyber.h"
#include "cyber/parameter/parameter_client.h"

using apollo::cyber::ParameterClient;
using apollo::cyber::Parameter;

int main(int argc, char* argv[])
{
    apollo::cyber::Init(argv[0]);

    // 创建节点
    std::shared_ptr<apollo::cyber::Node> param_client_node = apollo::cyber::CreateNode("parameter_client");

    // 创建参数服务端
    auto param_client = std::make_shared<ParameterClient>(param_client_node, "parameter"); // 后面第二个参数为服务端节点名称

    // 读取
    Parameter temp;
    param_client->GetParameter("type", &temp);
    AINFO << temp.Name() << ":" << temp.AsString();
    param_client->GetParameter("height", &temp);
    AINFO << temp.Name() << ":" << temp.AsDouble();
    param_client->GetParameter("age", &temp);
    AINFO << temp.Name() << ":" << temp.AsInt64();

    // 修改参数
    param_client->SetParameter(Parameter("age", 20));
    AINFO << "----------修改后的参数-------------";
    param_client->GetParameter("age", &temp);
    AINFO << temp.Name() << ":" << temp.AsInt64();

    // 获取所有参数
    std::vector<Parameter> params;
    param_client->ListParameters(&params);
    for (auto &&param : params)
    {
        // AINFO << param.Name() << ":" << param.TypeName();
        AINFO << param.DebugString();
    }

    apollo::cyber::WaitForShutdown();

    return 0;
}
parameter_service.cc 
#include "cyber/cyber.h"
#include "cyber/parameter/parameter_server.h"

using apollo::cyber::ParameterServer;
using apollo::cyber::Parameter;

int main(int argc, char* argv[])
{
    apollo::cyber::Init(argv[0]);

    // 创建节点
    std::shared_ptr<apollo::cyber::Node> param_service_node = apollo::cyber::CreateNode("parameter");

    // 创建参数服务端
    auto param_service = std::make_shared<ParameterServer>(param_service_node);

    // 设置参数
    param_service->SetParameter(Parameter("type", "Xiaomi"));
    param_service->SetParameter(Parameter("height", 2.00));
    param_service->SetParameter(Parameter("age", 32));

    // 获取参数
    Parameter temp;
    param_service->GetParameter("type", &temp);
    AINFO << temp.Name() << ":" << temp.AsString();
    param_service->GetParameter("height", &temp);
    AINFO << temp.Name() << ":" << temp.AsDouble();
    param_service->GetParameter("age", &temp);
    AINFO << temp.Name() << ":" << temp.AsInt64();

    // 获取所有参数
    std::vector<Parameter> params;
    param_service->ListParameters(&params);
    for (auto &&param : params)
    {
        AINFO << param.Name() << ":" << param.TypeName() << ":" << param.DebugString();
    }

    apollo::cyber::WaitForShutdown();

    return 0;
}
BUILD
load("//tools:apollo_package.bzl", "apollo_cc_library", "apollo_cc_binary", "apollo_package", "apollo_component")
load("//tools:cpplint.bzl", "cpplint")

package(default_visibility = ["//visibility:public"])

apollo_cc_binary(
    name = "param_client",
    srcs = ["client.cc"],
    deps = [
        "//cyber",
    ],
)
apollo_cc_binary(
    name = "param_server",
    srcs = ["parameter_service.cc"],
    deps = [
        "//cyber",
    ],
)

apollo_package()

cpplint()

步骤三、编译执行

编译文件
buildtool build -p cyber/demo_cc/parameter/
更改变量,使终端能够打印出消息
 export GLOG_alsologtostderr=1
执行

第一个终端执行

bazel-bin/cyber/demo_cc/parameter/param_client

第二个终端执行

 bazel-bin/cyber/demo_cc/parameter/param_server

执行结果


原文地址:https://blog.csdn.net/weixin_60062799/article/details/145215369

免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!