15 #include "rclcpp/parameter_service.hpp"
22 #include "rclcpp/logging.hpp"
24 #include "./parameter_service_names.hpp"
28 ParameterService::ParameterService(
29 const std::shared_ptr<rclcpp::node_interfaces::NodeBaseInterface> node_base,
30 const std::shared_ptr<rclcpp::node_interfaces::NodeServicesInterface> node_services,
34 const std::string node_name = node_base->get_name();
36 get_parameters_service_ = create_service<rcl_interfaces::srv::GetParameters>(
37 node_base, node_services,
38 node_name +
"/" + parameter_service_names::get_parameters,
40 const std::shared_ptr<rmw_request_id_t>,
41 const std::shared_ptr<rcl_interfaces::srv::GetParameters::Request> request,
42 std::shared_ptr<rcl_interfaces::srv::GetParameters::Response> response)
46 for (
const auto & param : parameters) {
47 response->values.push_back(param.get_value_message());
55 qos_profile,
nullptr);
57 get_parameter_types_service_ = create_service<rcl_interfaces::srv::GetParameterTypes>(
58 node_base, node_services,
59 node_name +
"/" + parameter_service_names::get_parameter_types,
61 const std::shared_ptr<rmw_request_id_t>,
62 const std::shared_ptr<rcl_interfaces::srv::GetParameterTypes::Request> request,
63 std::shared_ptr<rcl_interfaces::srv::GetParameterTypes::Response> response)
66 auto types = node_params->get_parameter_types(request->names);
68 types.cbegin(), types.cend(),
69 std::back_inserter(response->types), [](
const uint8_t & type) {
70 return static_cast<rclcpp::ParameterType>(type);
73 RCLCPP_DEBUG(
rclcpp::get_logger(
"rclcpp"),
"Failed to get parameter types: %s", ex.what());
76 qos_profile,
nullptr);
78 set_parameters_service_ = create_service<rcl_interfaces::srv::SetParameters>(
79 node_base, node_services,
80 node_name +
"/" + parameter_service_names::set_parameters,
82 const std::shared_ptr<rmw_request_id_t>,
83 const std::shared_ptr<rcl_interfaces::srv::SetParameters::Request> request,
84 std::shared_ptr<rcl_interfaces::srv::SetParameters::Response> response)
88 auto result = rcl_interfaces::msg::SetParametersResult();
89 for (
auto & p : request->parameters) {
95 result.successful =
false;
96 result.reason = ex.what();
98 response->results.push_back(result);
101 qos_profile,
nullptr);
103 set_parameters_atomically_service_ = create_service<rcl_interfaces::srv::SetParametersAtomically>(
104 node_base, node_services,
105 node_name +
"/" + parameter_service_names::set_parameters_atomically,
107 const std::shared_ptr<rmw_request_id_t>,
108 const std::shared_ptr<rcl_interfaces::srv::SetParametersAtomically::Request> request,
109 std::shared_ptr<rcl_interfaces::srv::SetParametersAtomically::Response> response)
111 std::vector<rclcpp::Parameter> pvariants;
113 request->parameters.cbegin(), request->parameters.cend(),
114 std::back_inserter(pvariants),
115 [](
const rcl_interfaces::msg::Parameter & p) {
116 return rclcpp::Parameter::from_parameter_msg(p);
120 response->result = result;
124 response->result.successful =
false;
125 response->result.reason =
"One or more parameters were not declared before setting";
128 qos_profile,
nullptr);
130 describe_parameters_service_ = create_service<rcl_interfaces::srv::DescribeParameters>(
131 node_base, node_services,
132 node_name +
"/" + parameter_service_names::describe_parameters,
134 const std::shared_ptr<rmw_request_id_t>,
135 const std::shared_ptr<rcl_interfaces::srv::DescribeParameters::Request> request,
136 std::shared_ptr<rcl_interfaces::srv::DescribeParameters::Response> response)
139 auto descriptors = node_params->describe_parameters(request->names);
140 response->descriptors = descriptors;
142 RCLCPP_DEBUG(
rclcpp::get_logger(
"rclcpp"),
"Failed to describe parameters: %s", ex.what());
145 qos_profile,
nullptr);
147 list_parameters_service_ = create_service<rcl_interfaces::srv::ListParameters>(
148 node_base, node_services,
149 node_name +
"/" + parameter_service_names::list_parameters,
151 const std::shared_ptr<rmw_request_id_t>,
152 const std::shared_ptr<rcl_interfaces::srv::ListParameters::Request> request,
153 std::shared_ptr<rcl_interfaces::srv::ListParameters::Response> response)
155 auto result = node_params->list_parameters(request->prefixes, request->depth);
156 response->result = result;
158 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.