Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the set_parameters_callback example program. #608

Merged
merged 1 commit into from
Mar 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions demo_nodes_cpp/src/parameters/set_parameters_callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,23 @@ class SetParametersCallback : public rclcpp::Node
auto on_set_parameter_callback =
[this](std::vector<rclcpp::Parameter> parameters) {
rcl_interfaces::msg::SetParametersResult result;
result.successful = true;

for (const auto & param : parameters) {
if (param.get_name() == "param1") {
result.successful = true;
result.reason = "success param1";
}
if (param.get_name() == "param2") {
result.successful = true;
result.reason = "success param2";
// Arbitrarily reject updates setting param1 > 5.0
if (param.get_value<double>() > 5.0) {
result.successful = false;
result.reason = "cannot set param1 > 5.0";
break;
}
} else if (param.get_name() == "param2") {
// Arbitrarily reject updates setting param2 < -5.0
if (param.get_value<double>() < -5.0) {
result.successful = false;
result.reason = "cannot set param2 < -5.0";
break;
}
}
}

Expand Down Expand Up @@ -95,6 +104,13 @@ class SetParametersCallback : public rclcpp::Node
on_set_parameter_callback);
post_set_parameters_callback_handle_ = this->add_post_set_parameters_callback(
post_set_parameter_callback);

printf("This node shows off parameter callbacks.");
printf("To do that, it exhibits the following behavior:\n");
printf(" * Two parameters of type double are declared on the node, param1 and param2\n");
printf(" * param1 cannot be set to a value > 5.0\n");
printf(" * param2 cannot be set to a value < -5.0\n");
printf(" * any time param1 is set, param2 is automatically set to 4.0\n");
}

private:
Expand Down