Skip to content

Commit c89a2b1

Browse files
authored
subordinate node consistent behavior and update docstring. (#2822)
* subordinate node consistent behavior and update docstring. Signed-off-by: Tomoya Fujita <[email protected]> * add subnode_parameter_operation test. Signed-off-by: Tomoya Fujita <[email protected]> * typo fixes. Signed-off-by: Tomoya Fujita <[email protected]> --------- Signed-off-by: Tomoya Fujita <[email protected]>
1 parent 1a28206 commit c89a2b1

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

rclcpp/include/rclcpp/node.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,10 @@ class Node : public std::enable_shared_from_this<Node>
15591559
* which has been created using an existing instance of this class, but which
15601560
* has an additional sub-namespace (short for subordinate namespace)
15611561
* associated with it.
1562+
* A subordinate node and an instance of this class share all the node interfaces
1563+
* such as `rclcpp::node_interfaces::NodeParameterInterface`.
1564+
* Subordinate nodes are primarily used to organize namespaces and provide a
1565+
* hierarchical structure, but they are not meant to be completely independent nodes.
15621566
* The sub-namespace will extend the node's namespace for the purpose of
15631567
* creating additional entities, such as Publishers, Subscriptions, Service
15641568
* Clients and Servers, and so on.

rclcpp/include/rclcpp/node_impl.hpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,9 @@ template<typename ParameterT>
323323
bool
324324
Node::get_parameter(const std::string & name, ParameterT & parameter) const
325325
{
326-
std::string sub_name = extend_name_with_sub_namespace(name, this->get_sub_namespace());
327-
328326
rclcpp::Parameter parameter_variant;
329327

330-
bool result = get_parameter(sub_name, parameter_variant);
328+
bool result = get_parameter(name, parameter_variant);
331329
if (result) {
332330
parameter = static_cast<ParameterT>(parameter_variant.get_value<ParameterT>());
333331
}
@@ -342,9 +340,7 @@ Node::get_parameter_or(
342340
ParameterT & parameter,
343341
const ParameterT & alternative_value) const
344342
{
345-
std::string sub_name = extend_name_with_sub_namespace(name, this->get_sub_namespace());
346-
347-
bool got_parameter = get_parameter(sub_name, parameter);
343+
bool got_parameter = get_parameter(name, parameter);
348344
if (!got_parameter) {
349345
parameter = alternative_value;
350346
}

rclcpp/src/rclcpp/node.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ Node::create_generic_client(
689689
node_base_,
690690
node_graph_,
691691
node_services_,
692-
service_name,
692+
extend_name_with_sub_namespace(service_name, this->get_sub_namespace()),
693693
service_type,
694694
qos,
695695
group);

rclcpp/test/rclcpp/test_generic_client.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ TEST_F(TestGenericClient, wait_for_service) {
221221
TEST_F(TestGenericClientSub, construction_and_destruction) {
222222
{
223223
auto client = subnode->create_generic_client("test_service", "test_msgs/srv/Empty");
224-
EXPECT_STREQ(client->get_service_name(), "/ns/test_service");
224+
EXPECT_STREQ(client->get_service_name(), "/ns/sub_ns/test_service");
225225
}
226226

227227
{

rclcpp/test/rclcpp/test_node.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,39 @@ TEST_F(TestNode, subnode_get_name_and_namespace) {
306306
}, rclcpp::exceptions::NameValidationError);
307307
}
308308
}
309+
310+
TEST_F(TestNode, subnode_parameter_operation) {
311+
auto node = std::make_shared<rclcpp::Node>("my_node", "ns");
312+
auto subnode = node->create_sub_node("sub_ns");
313+
314+
auto value = subnode->declare_parameter("param", 5);
315+
EXPECT_EQ(value, 5);
316+
// node and sub-node shares NodeParameterInterface, so expecting the exception.
317+
EXPECT_THROW(
318+
node->declare_parameter("param", 0),
319+
rclcpp::exceptions::ParameterAlreadyDeclaredException);
320+
rclcpp::Parameter param;
321+
322+
node->get_parameter("param", param);
323+
EXPECT_EQ(param.get_value<int>(), 5);
324+
subnode->get_parameter("param", param);
325+
EXPECT_EQ(param.get_value<int>(), 5);
326+
327+
int param_int;
328+
node->get_parameter("param", param_int);
329+
EXPECT_EQ(param_int, 5);
330+
subnode->get_parameter("param", param_int);
331+
EXPECT_EQ(param_int, 5);
332+
333+
EXPECT_EQ(node->get_parameter_or("param", 333), 5);
334+
EXPECT_EQ(subnode->get_parameter_or("param", 666), 5);
335+
336+
node->get_parameter_or("param", param_int, 333);
337+
EXPECT_EQ(param_int, 5);
338+
subnode->get_parameter_or("param", param_int, 666);
339+
EXPECT_EQ(param_int, 5);
340+
}
341+
309342
/*
310343
Testing node construction and destruction.
311344
*/

0 commit comments

Comments
 (0)