-
Notifications
You must be signed in to change notification settings - Fork 329
/
parameter_events.cpp
95 lines (78 loc) · 3.07 KB
/
parameter_events.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2015 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <chrono>
#include <memory>
#include <sstream>
#include "rclcpp/rclcpp.hpp"
using namespace std::chrono_literals;
void on_parameter_event(
const rcl_interfaces::msg::ParameterEvent::SharedPtr event, rclcpp::Logger logger)
{
// TODO(wjwwood): The message should have an operator<<, which would replace all of this.
std::stringstream ss;
ss << "\nParameter event:\n new parameters:";
for (auto & new_parameter : event->new_parameters) {
ss << "\n " << new_parameter.name;
}
ss << "\n changed parameters:";
for (auto & changed_parameter : event->changed_parameters) {
ss << "\n " << changed_parameter.name;
}
ss << "\n deleted parameters:";
for (auto & deleted_parameter : event->deleted_parameters) {
ss << "\n " << deleted_parameter.name;
}
ss << "\n";
RCLCPP_INFO(logger, ss.str().c_str())
}
int main(int argc, char ** argv)
{
// Force flush of the stdout buffer.
setvbuf(stdout, NULL, _IONBF, BUFSIZ);
rclcpp::init(argc, argv);
auto node = rclcpp::Node::make_shared("parameter_events");
// TODO(esteve): Make the parameter service automatically start with the node.
auto parameter_service = std::make_shared<rclcpp::ParameterService>(node);
auto parameters_client = std::make_shared<rclcpp::SyncParametersClient>(node);
while (!parameters_client->wait_for_service(1s)) {
if (!rclcpp::ok()) {
RCLCPP_ERROR(node->get_logger(), "Interrupted while waiting for the service. Exiting.")
return 0;
}
RCLCPP_INFO(node->get_logger(), "service not available, waiting again...")
}
// Setup callback for changes to parameters.
auto sub = parameters_client->on_parameter_event(
[node](const rcl_interfaces::msg::ParameterEvent::SharedPtr event) -> void
{
on_parameter_event(event, node->get_logger());
});
// Set several differnet types of parameters.
auto set_parameters_results = parameters_client->set_parameters({
rclcpp::parameter::ParameterVariant("foo", 2),
rclcpp::parameter::ParameterVariant("bar", "hello"),
rclcpp::parameter::ParameterVariant("baz", 1.45),
rclcpp::parameter::ParameterVariant("foobar", true),
});
// Change the value of some of them.
set_parameters_results = parameters_client->set_parameters({
rclcpp::parameter::ParameterVariant("foo", 3),
rclcpp::parameter::ParameterVariant("bar", "world"),
});
// TODO(wjwwood): Create and use delete_parameter
rclcpp::sleep_for(100ms);
rclcpp::spin_some(node);
rclcpp::shutdown();
return 0;
}