15 #include "rclcpp/parameter_service.hpp"
23 #include "rclcpp/logging.hpp"
25 #include "./parameter_service_names.hpp"
29 ParameterService::ParameterService(
30 const std::shared_ptr<rclcpp::node_interfaces::NodeBaseInterface> node_base,
31 const std::shared_ptr<rclcpp::node_interfaces::NodeServicesInterface> node_services,
33 const rmw_qos_profile_t & qos_profile)
35 const std::string node_name = node_base->get_name();
37 get_parameters_service_ = create_service<rcl_interfaces::srv::GetParameters>(
38 node_base, node_services,
39 node_name +
"/" + parameter_service_names::get_parameters,
41 const std::shared_ptr<rmw_request_id_t>,
42 const std::shared_ptr<rcl_interfaces::srv::GetParameters::Request> request,
43 std::shared_ptr<rcl_interfaces::srv::GetParameters::Response> response)
47 for (
const auto & param : parameters) {
48 response->values.push_back(param.get_value_message());
54 qos_profile,
nullptr);
56 get_parameter_types_service_ = create_service<rcl_interfaces::srv::GetParameterTypes>(
57 node_base, node_services,
58 node_name +
"/" + parameter_service_names::get_parameter_types,
60 const std::shared_ptr<rmw_request_id_t>,
61 const std::shared_ptr<rcl_interfaces::srv::GetParameterTypes::Request> request,
62 std::shared_ptr<rcl_interfaces::srv::GetParameterTypes::Response> response)
65 auto types = node_params->get_parameter_types(request->names);
67 types.cbegin(), types.cend(),
68 std::back_inserter(response->types), [](
const uint8_t & type) {
69 return static_cast<rclcpp::ParameterType>(type);
72 RCLCPP_WARN(
rclcpp::get_logger(
"rclcpp"),
"Failed to get parameter types: %s", ex.what());
75 qos_profile,
nullptr);
77 set_parameters_service_ = create_service<rcl_interfaces::srv::SetParameters>(
78 node_base, node_services,
79 node_name +
"/" + parameter_service_names::set_parameters,
81 const std::shared_ptr<rmw_request_id_t>,
82 const std::shared_ptr<rcl_interfaces::srv::SetParameters::Request> request,
83 std::shared_ptr<rcl_interfaces::srv::SetParameters::Response> response)
87 auto result = rcl_interfaces::msg::SetParametersResult();
88 for (
auto & p : request->parameters) {
94 result.successful =
false;
95 result.reason = ex.what();
96 }
catch (
const std::runtime_error & ex) {
98 result.successful =
false;
99 result.reason = ex.what();
101 response->results.push_back(result);
104 qos_profile,
nullptr);
106 set_parameters_atomically_service_ = create_service<rcl_interfaces::srv::SetParametersAtomically>(
107 node_base, node_services,
108 node_name +
"/" + parameter_service_names::set_parameters_atomically,
110 const std::shared_ptr<rmw_request_id_t>,
111 const std::shared_ptr<rcl_interfaces::srv::SetParametersAtomically::Request> request,
112 std::shared_ptr<rcl_interfaces::srv::SetParametersAtomically::Response> response)
115 std::vector<rclcpp::Parameter> pvariants;
117 request->parameters.cbegin(), request->parameters.cend(),
118 std::back_inserter(pvariants),
119 [](
const rcl_interfaces::msg::Parameter & p) {
120 return rclcpp::Parameter::from_parameter_msg(p);
123 response->result = result;
127 response->result.successful =
false;
128 response->result.reason =
"One or more parameters were not declared before setting";
129 }
catch (
const std::runtime_error & ex) {
132 response->result.successful =
false;
133 response->result.reason = ex.what();
136 qos_profile,
nullptr);
138 describe_parameters_service_ = create_service<rcl_interfaces::srv::DescribeParameters>(
139 node_base, node_services,
140 node_name +
"/" + parameter_service_names::describe_parameters,
142 const std::shared_ptr<rmw_request_id_t>,
143 const std::shared_ptr<rcl_interfaces::srv::DescribeParameters::Request> request,
144 std::shared_ptr<rcl_interfaces::srv::DescribeParameters::Response> response)
147 auto descriptors = node_params->describe_parameters(request->names);
148 response->descriptors = descriptors;
150 RCLCPP_WARN(
rclcpp::get_logger(
"rclcpp"),
"Failed to describe parameters: %s", ex.what());
153 qos_profile,
nullptr);
155 list_parameters_service_ = create_service<rcl_interfaces::srv::ListParameters>(
156 node_base, node_services,
157 node_name +
"/" + parameter_service_names::list_parameters,
159 const std::shared_ptr<rmw_request_id_t>,
160 const std::shared_ptr<rcl_interfaces::srv::ListParameters::Request> request,
161 std::shared_ptr<rcl_interfaces::srv::ListParameters::Response> response)
163 auto result = node_params->list_parameters(request->prefixes, request->depth);
164 response->result = result;
166 qos_profile,
nullptr);
static RCLCPP_PUBLIC Parameter from_parameter_msg(const rcl_interfaces::msg::Parameter ¶meter)
Convert a parameter message in a Parameter class object.
Thrown if parameter is not declared, e.g. either set or get was called without first declaring.
Pure virtual interface class for the NodeParameters part of the Node API.
virtual RCLCPP_PUBLIC std::vector< rclcpp::Parameter > get_parameters(const std::vector< std::string > &names) const =0
Get descriptions of parameters given their names.
virtual RCLCPP_PUBLIC rcl_interfaces::msg::SetParametersResult set_parameters_atomically(const std::vector< rclcpp::Parameter > ¶meters)=0
Set one or more parameters, all at once.
RCLCPP_PUBLIC Logger get_logger(const std::string &name)
Return a named logger.