Skip to content

Commit 41c7f68

Browse files
Parameter Descriptor Simplification (#2179)
Signed-off-by: CursedRock17 <mtglucas1@gmail.com> Co-authored-by: Alejandro Hernandez Cordero <ahcorde@gmail.com>
1 parent 02caec1 commit 41c7f68

5 files changed

Lines changed: 210 additions & 0 deletions

File tree

rclcpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ set(${PROJECT_NAME}_SRCS
103103
src/rclcpp/node_options.cpp
104104
src/rclcpp/parameter.cpp
105105
src/rclcpp/parameter_client.cpp
106+
src/rclcpp/parameter_descriptor_wrapper.cpp
106107
src/rclcpp/parameter_event_handler.cpp
107108
src/rclcpp/parameter_events_filter.cpp
108109
src/rclcpp/parameter_map.cpp
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2023 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef RCLCPP__PARAMETER_DESCRIPTOR_WRAPPER_HPP_
16+
#define RCLCPP__PARAMETER_DESCRIPTOR_WRAPPER_HPP_
17+
18+
// C++ Standard library includes
19+
#include <functional>
20+
#include <utility>
21+
#include <memory>
22+
#include <string>
23+
24+
// Additional ROS libraries needed
25+
#include "rcl_interfaces/msg/parameter_descriptor.hpp"
26+
27+
#include "rclcpp/node.hpp"
28+
#include "rclcpp/parameter_value.hpp"
29+
#include "rclcpp/node_interfaces/get_node_parameters_interface.hpp"
30+
#include "rclcpp/visibility_control.hpp"
31+
#include "node_interfaces/node_parameters_interface.hpp"
32+
33+
namespace rclcpp
34+
{
35+
36+
// Implements ParameterDesription class with builder design pattern
37+
class ParameterDescription
38+
{
39+
public:
40+
RCLCPP_SMART_PTR_DEFINITIONS(ParameterDescription)
41+
42+
// List of classes the builder manages
43+
RCLCPP_PUBLIC
44+
ParameterDescription();
45+
46+
// Our Main build methods which will construct the base class
47+
RCLCPP_PUBLIC rcl_interfaces::msg::ParameterDescriptor build() const;
48+
49+
// Builder Methods:
50+
// Describes the instances in a parameter_description object
51+
RCLCPP_PUBLIC ParameterDescription & set_name(const std::string & name);
52+
RCLCPP_PUBLIC ParameterDescription & set_type(std::uint8_t type);
53+
RCLCPP_PUBLIC ParameterDescription & set_description_text(const std::string & description);
54+
RCLCPP_PUBLIC ParameterDescription & set_additional_constraints(const std::string & constraints);
55+
RCLCPP_PUBLIC ParameterDescription & set_read_only(bool read_only);
56+
RCLCPP_PUBLIC ParameterDescription & set_dynamic_typing(bool dynamic_typing);
57+
RCLCPP_PUBLIC ParameterDescription & set_floating_point_description_range(
58+
float min, float max, float step);
59+
RCLCPP_PUBLIC ParameterDescription & set_integer_description_range(int min, int max, int step);
60+
61+
// Need the current node in order to begin the configuration state
62+
// for it via the declare_parameter function
63+
template<typename NodeT>
64+
ParameterDescription & declare_parameter(
65+
const rclcpp::ParameterValue & default_value,
66+
NodeT && node)
67+
{
68+
auto node_param = rclcpp::node_interfaces::get_node_parameters_interface(node);
69+
node_param->declare_parameter(
70+
parameter_descriptor.name, default_value,
71+
parameter_descriptor);
72+
return *this;
73+
}
74+
75+
private:
76+
// The main descriptor object we're meant to initialize and adjust
77+
rcl_interfaces::msg::ParameterDescriptor parameter_descriptor;
78+
};
79+
80+
} // namespace rclcpp
81+
82+
#endif // RCLCPP__PARAMETER_DESCRIPTOR_WRAPPER_HPP_

rclcpp/include/rclcpp/rclcpp.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@
175175
#include "rclcpp/parameter_event_handler.hpp"
176176
#include "rclcpp/parameter.hpp"
177177
#include "rclcpp/parameter_service.hpp"
178+
#include "rclcpp/parameter_descriptor_wrapper.hpp"
178179
#include "rclcpp/rate.hpp"
179180
#include "rclcpp/time.hpp"
180181
#include "rclcpp/utilities.hpp"
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright 2023 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "rclcpp/parameter_descriptor_wrapper.hpp"
16+
17+
namespace rclcpp
18+
{
19+
20+
ParameterDescription::ParameterDescription()
21+
{
22+
// Need to set this in the constructor, but it doesn't necessarily need to be used
23+
parameter_descriptor.type = rcl_interfaces::msg::ParameterType::PARAMETER_NOT_SET;
24+
}
25+
26+
rcl_interfaces::msg::ParameterDescriptor ParameterDescription::build() const
27+
{
28+
// Return some some sort message
29+
return parameter_descriptor;
30+
}
31+
32+
// Builder methods which set up the original class
33+
// They all follow the same format of initing the value given within the base class
34+
// then returning the current class
35+
ParameterDescription & ParameterDescription::set_name(const std::string & name)
36+
{
37+
parameter_descriptor.name = name;
38+
return *this;
39+
}
40+
41+
ParameterDescription & ParameterDescription::set_type(std::uint8_t type)
42+
{
43+
parameter_descriptor.type = type;
44+
return *this;
45+
}
46+
47+
ParameterDescription & ParameterDescription::set_description_text(const std::string & description)
48+
{
49+
parameter_descriptor.description = description;
50+
return *this;
51+
}
52+
53+
ParameterDescription & ParameterDescription::set_additional_constraints(
54+
const std::string & constraints)
55+
{
56+
parameter_descriptor.additional_constraints = constraints;
57+
return *this;
58+
}
59+
60+
ParameterDescription & ParameterDescription::set_read_only(bool read_only)
61+
{
62+
parameter_descriptor.read_only = read_only;
63+
return *this;
64+
}
65+
66+
ParameterDescription & ParameterDescription::set_dynamic_typing(bool dynamic_typing)
67+
{
68+
parameter_descriptor.dynamic_typing = dynamic_typing;
69+
return *this;
70+
}
71+
72+
// Here is the Specific range function for this parameter description
73+
ParameterDescription & ParameterDescription::set_floating_point_description_range(
74+
float min, float max, float step)
75+
{
76+
if (parameter_descriptor.type == rcl_interfaces::msg::ParameterType::PARAMETER_DOUBLE) {
77+
parameter_descriptor.floating_point_range.resize(1);
78+
parameter_descriptor.floating_point_range.at(0).from_value = min;
79+
parameter_descriptor.floating_point_range.at(0).to_value = max;
80+
parameter_descriptor.floating_point_range.at(0).step = step;
81+
}
82+
return *this;
83+
}
84+
85+
ParameterDescription & ParameterDescription::set_integer_description_range(
86+
int min, int max, int step)
87+
{
88+
if (parameter_descriptor.type == rcl_interfaces::msg::ParameterType::PARAMETER_INTEGER) {
89+
parameter_descriptor.integer_range.resize(1);
90+
parameter_descriptor.integer_range.at(0).from_value = min;
91+
parameter_descriptor.integer_range.at(0).to_value = max;
92+
parameter_descriptor.integer_range.at(0).step = step;
93+
}
94+
return *this;
95+
}
96+
97+
} // namespace rclcpp

rclcpp/test/rclcpp/test_parameter_service.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,32 @@ TEST_F(TestParameterService, describe_parameters) {
120120
EXPECT_EQ(0u, parameter_descs.size());
121121
}
122122
}
123+
124+
TEST_F(TestParameterService, parameter_descriptor) {
125+
{
126+
rclcpp::ParameterDescription param_description;
127+
rclcpp::ParameterValue param_value(1);
128+
129+
param_description.set_name("int_parameter");
130+
param_description.set_type(2);
131+
param_description.set_description_text("description");
132+
param_description.set_additional_constraints("constraints");
133+
param_description.set_read_only(false);
134+
param_description.set_integer_description_range(0, 10, 1);
135+
136+
auto param = param_description.build();
137+
param_description.declare_parameter(param_value, node);
138+
139+
EXPECT_EQ("int_parameter", param.name);
140+
EXPECT_EQ(rclcpp::ParameterType::PARAMETER_INTEGER, param.type);
141+
EXPECT_EQ("description", param.description);
142+
EXPECT_EQ("constraints", param.additional_constraints);
143+
EXPECT_EQ(0u, param.read_only);
144+
EXPECT_EQ(0u, param.dynamic_typing);
145+
EXPECT_EQ(0u, param.integer_range.at(0).from_value);
146+
EXPECT_EQ(10, param.integer_range.at(0).to_value);
147+
EXPECT_EQ(1, param.integer_range.at(0).step);
148+
149+
ASSERT_EQ(1, client->get_parameter("int_parameter", 0));
150+
}
151+
}

0 commit comments

Comments
 (0)