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,
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());
56 qos_profile,
nullptr);
58 get_parameter_types_service_ = create_service<rcl_interfaces::srv::GetParameterTypes>(
59 node_base, node_services,
60 node_name +
"/" + parameter_service_names::get_parameter_types,
62 const std::shared_ptr<rmw_request_id_t> &,
63 const std::shared_ptr<rcl_interfaces::srv::GetParameterTypes::Request> & request,
64 std::shared_ptr<rcl_interfaces::srv::GetParameterTypes::Response> response)
67 auto types = node_params->get_parameter_types(request->names);
69 types.cbegin(), types.cend(),
70 std::back_inserter(response->types), [](
const uint8_t & type) {
71 return static_cast<rclcpp::ParameterType>(type);
74 RCLCPP_WARN(
rclcpp::get_logger(
"rclcpp"),
"Failed to get parameter types: %s", ex.what());
77 qos_profile,
nullptr);
79 set_parameters_service_ = create_service<rcl_interfaces::srv::SetParameters>(
80 node_base, node_services,
81 node_name +
"/" + parameter_service_names::set_parameters,
83 const std::shared_ptr<rmw_request_id_t> &,
84 const std::shared_ptr<rcl_interfaces::srv::SetParameters::Request> & request,
85 std::shared_ptr<rcl_interfaces::srv::SetParameters::Response> response)
89 auto result = rcl_interfaces::msg::SetParametersResult();
90 for (
auto & p : request->parameters) {
96 result.successful =
false;
97 result.reason = ex.what();
98 }
catch (
const std::runtime_error & ex) {
100 result.successful =
false;
101 result.reason = ex.what();
103 response->results.push_back(result);
106 qos_profile,
nullptr);
108 set_parameters_atomically_service_ = create_service<rcl_interfaces::srv::SetParametersAtomically>(
109 node_base, node_services,
110 node_name +
"/" + parameter_service_names::set_parameters_atomically,
112 const std::shared_ptr<rmw_request_id_t> &,
113 const std::shared_ptr<rcl_interfaces::srv::SetParametersAtomically::Request> & request,
114 std::shared_ptr<rcl_interfaces::srv::SetParametersAtomically::Response> response)
117 std::vector<rclcpp::Parameter> pvariants;
119 request->parameters.cbegin(), request->parameters.cend(),
120 std::back_inserter(pvariants),
121 [](
const rcl_interfaces::msg::Parameter & p) {
122 return rclcpp::Parameter::from_parameter_msg(p);
125 response->result = result;
129 response->result.successful =
false;
130 response->result.reason =
"One or more parameters were not declared before setting";
131 }
catch (
const std::runtime_error & ex) {
134 response->result.successful =
false;
135 response->result.reason = ex.what();
138 qos_profile,
nullptr);
140 describe_parameters_service_ = create_service<rcl_interfaces::srv::DescribeParameters>(
141 node_base, node_services,
142 node_name +
"/" + parameter_service_names::describe_parameters,
144 const std::shared_ptr<rmw_request_id_t> &,
145 const std::shared_ptr<rcl_interfaces::srv::DescribeParameters::Request> & request,
146 std::shared_ptr<rcl_interfaces::srv::DescribeParameters::Response> response)
149 auto descriptors = node_params->describe_parameters(request->names);
150 response->descriptors = descriptors;
152 RCLCPP_WARN(
rclcpp::get_logger(
"rclcpp"),
"Failed to describe parameters: %s", ex.what());
155 qos_profile,
nullptr);
157 list_parameters_service_ = create_service<rcl_interfaces::srv::ListParameters>(
158 node_base, node_services,
159 node_name +
"/" + parameter_service_names::list_parameters,
161 const std::shared_ptr<rmw_request_id_t> &,
162 const std::shared_ptr<rcl_interfaces::srv::ListParameters::Request> & request,
163 std::shared_ptr<rcl_interfaces::srv::ListParameters::Response> response)
165 auto result = node_params->list_parameters(request->prefixes, request->depth);
166 response->result = result;
168 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.
Encapsulation of Quality of Service settings.
Thrown if parameter is not declared, e.g. either set or get was called without first declaring.
Thrown when an uninitialized parameter is accessed.
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.