Skip to content

Instantly share code, notes, and snippets.

@wjwwood
Created March 13, 2016 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wjwwood/5667039b95a682e3034b to your computer and use it in GitHub Desktop.
Save wjwwood/5667039b95a682e3034b to your computer and use it in GitHub Desktop.
#include <iostream>
#include "rclcpp/rclcpp.hpp"
#include <memory>
using rclcpp::parameter_client::AsyncParametersClient;
rclcpp::node::Node::SharedPtr node;
void spin()
{
// rclcpp::WallRate loop_rate(30);
// while (rclcpp::ok()) {
// rclcpp::spin_some(node);
// loop_rate.sleep();
// }
rclcpp::spin(node);
}
int main(int argc, char **argv) {
rclcpp::init(argc, argv);
node = rclcpp::node::Node::make_shared("parameter_server_test");
std::thread spinThread(&spin);
// Extra scope so the first client will be deleted afterwards
{
std::cout << "Creating first client" << std::endl;
auto parameters_client = std::make_shared<AsyncParametersClient>(node, "ParameterServer");
auto results = parameters_client->set_parameters({
rclcpp::parameter::ParameterVariant("DemoRobot", 2),
rclcpp::parameter::ParameterVariant("bar", "hello"),
rclcpp::parameter::ParameterVariant("baz", 1.45),
rclcpp::parameter::ParameterVariant("foobar", true),
});
// Wait for the results.
if (results.wait_for(5_s) != std::future_status::ready)
{
printf("set_parameters service call failed. Exiting example.\n");
return -1;
}
}
std::cout << "Creating second client" << std::endl;
auto parameters_client_2 = std::make_shared<AsyncParametersClient>(node, "ParameterServer");
// Get a few of the parameters just set.
auto parameters = parameters_client_2->get_parameters({"DemoRobot", "baz"});
// Here comes the segfault
if (parameters.wait_for(5_s) != std::future_status::ready)
{
printf("get_parameters service call failed. Exiting example.\n");
return -1;
}
for (auto & parameter : parameters.get()) {
std::cout << "Parameter name: " << parameter.get_name() << std::endl;
std::cout << "Parameter value (" << parameter.get_type_name() << "): " <<
parameter.value_to_string() << std::endl;
}
std::string input = "";
while (input != "exit")
{
std::getline (std::cin,input);
// Simply wait
}
// Must join a thread you start, it will end when !rclcpp::ok().
spinThread.join();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment