Skip to content

Commit ec0500b

Browse files
committed
Pass parameter values to node constructor
1 parent d298fa4 commit ec0500b

File tree

9 files changed

+93
-3
lines changed

9 files changed

+93
-3
lines changed

rclcpp/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ if(BUILD_TESTING)
205205
)
206206
target_link_libraries(test_node_global_args ${PROJECT_NAME})
207207
endif()
208+
ament_add_gtest(test_node_initial_parameters test/test_node_initial_parameters.cpp)
209+
if(TARGET test_node_initial_parameters)
210+
target_link_libraries(test_node_initial_parameters ${PROJECT_NAME})
211+
endif()
208212
ament_add_gtest(test_parameter_events_filter test/test_parameter_events_filter.cpp)
209213
if(TARGET test_parameter_events_filter)
210214
target_include_directories(test_parameter_events_filter PUBLIC

rclcpp/include/rclcpp/node.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class Node : public std::enable_shared_from_this<Node>
9696
const std::string & namespace_,
9797
rclcpp::Context::SharedPtr context,
9898
const std::vector<std::string> & arguments,
99+
const std::vector<Parameter> & initial_values,
99100
bool use_global_arguments = true,
100101
bool use_intra_process_comms = false,
101102
bool start_parameter_services = true);

rclcpp/include/rclcpp/node_interfaces/node_parameters.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class NodeParameters : public NodeParametersInterface
5151
const node_interfaces::NodeBaseInterface::SharedPtr node_base,
5252
const node_interfaces::NodeTopicsInterface::SharedPtr node_topics,
5353
const node_interfaces::NodeServicesInterface::SharedPtr node_services,
54+
const std::vector<Parameter> & initial_values,
5455
bool use_intra_process,
5556
bool start_parameter_services);
5657

rclcpp/src/rclcpp/node.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Node::Node(
4444
namespace_,
4545
rclcpp::contexts::default_context::get_global_default_context(),
4646
{},
47+
{},
4748
true,
4849
use_intra_process_comms,
4950
true)
@@ -54,6 +55,7 @@ Node::Node(
5455
const std::string & namespace_,
5556
rclcpp::Context::SharedPtr context,
5657
const std::vector<std::string> & arguments,
58+
const std::vector<Parameter> & initial_values,
5759
bool use_global_arguments,
5860
bool use_intra_process_comms,
5961
bool start_parameter_services)
@@ -68,6 +70,7 @@ Node::Node(
6870
node_base_,
6971
node_topics_,
7072
node_services_,
73+
initial_values,
7174
use_intra_process_comms,
7275
start_parameter_services
7376
)),

rclcpp/src/rclcpp/node_interfaces/node_parameters.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ NodeParameters::NodeParameters(
3131
const rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base,
3232
const rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics,
3333
const rclcpp::node_interfaces::NodeServicesInterface::SharedPtr node_services,
34+
const std::vector<Parameter> & initial_values,
3435
bool use_intra_process,
3536
bool start_parameter_services)
3637
{
@@ -50,6 +51,15 @@ NodeParameters::NodeParameters(
5051
rmw_qos_profile_parameter_events,
5152
use_intra_process,
5253
allocator);
54+
55+
// TODO(sloretz) store initial values and use them when a parameter is created
56+
// Set initial parameter values
57+
if (!initial_values.empty()) {
58+
rcl_interfaces::msg::SetParametersResult result = set_parameters_atomically(initial_values);
59+
if (!result.successful) {
60+
throw std::runtime_error("Failed to set initial parameters");
61+
}
62+
}
5363
}
5464

5565
NodeParameters::~NodeParameters()

rclcpp/test/test_node_global_args.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,30 @@ class TestNodeWithGlobalArgs : public ::testing::Test
3535
TEST_F(TestNodeWithGlobalArgs, local_arguments_before_global) {
3636
auto context = rclcpp::contexts::default_context::get_global_default_context();
3737
const std::vector<std::string> arguments = {"__node:=local_arguments_test"};
38+
const std::vector<rclcpp::Parameter> initial_values = {};
3839
const bool use_global_arguments = true;
3940
const bool use_intra_process = false;
4041
auto node = rclcpp::Node::make_shared(
41-
"orig_name", "", context, arguments, use_global_arguments, use_intra_process);
42+
"orig_name", "", context, arguments, initial_values, use_global_arguments, use_intra_process);
4243
EXPECT_STREQ("local_arguments_test", node->get_name());
4344
}
4445

4546
TEST_F(TestNodeWithGlobalArgs, use_or_ignore_global_arguments) {
4647
auto context = rclcpp::contexts::default_context::get_global_default_context();
4748
const std::vector<std::string> arguments = {};
49+
const std::vector<rclcpp::Parameter> initial_values = {};
4850
const bool use_intra_process = false;
4951

5052
{ // Don't use global args
5153
const bool use_global_arguments = false;
5254
auto node = rclcpp::Node::make_shared(
53-
"orig_name", "", context, arguments, use_global_arguments, use_intra_process);
55+
"orig_name", "", context, arguments, initial_values, use_global_arguments, use_intra_process);
5456
EXPECT_STREQ("orig_name", node->get_name());
5557
}
5658
{ // Do use global args
5759
const bool use_global_arguments = true;
5860
auto node = rclcpp::Node::make_shared(
59-
"orig_name", "", context, arguments, use_global_arguments, use_intra_process);
61+
"orig_name", "", context, arguments, initial_values, use_global_arguments, use_intra_process);
6062
EXPECT_STREQ("global_node_name", node->get_name());
6163
}
6264
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2018 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 <gtest/gtest.h>
16+
17+
#include <string>
18+
#include <memory>
19+
#include <vector>
20+
21+
#include "rclcpp/node.hpp"
22+
#include "rclcpp/rclcpp.hpp"
23+
24+
class TestNodeWithInitialValues : public ::testing::Test
25+
{
26+
protected:
27+
static void SetUpTestCase()
28+
{
29+
rclcpp::init(0, NULL);
30+
}
31+
};
32+
33+
TEST_F(TestNodeWithInitialValues, no_initial_values) {
34+
auto context = rclcpp::contexts::default_context::get_global_default_context();
35+
const std::vector<std::string> arguments = {};
36+
const std::vector<rclcpp::Parameter> initial_values = {};
37+
const bool use_global_arguments = false;
38+
const bool use_intra_process = false;
39+
auto node = rclcpp::Node::make_shared(
40+
"node_name", "", context, arguments, initial_values, use_global_arguments, use_intra_process);
41+
auto list_params_result = node->list_parameters({}, 0);
42+
EXPECT_EQ(0u, list_params_result.names.size());
43+
}
44+
45+
TEST_F(TestNodeWithInitialValues, many_initial_values) {
46+
auto context = rclcpp::contexts::default_context::get_global_default_context();
47+
const std::vector<std::string> arguments = {};
48+
const std::vector<rclcpp::Parameter> initial_values = {
49+
rclcpp::Parameter("foo", true),
50+
rclcpp::Parameter("bar", "hello world"),
51+
rclcpp::Parameter("baz", std::vector<double>{3.14, 2.718})
52+
};
53+
const bool use_global_arguments = false;
54+
const bool use_intra_process = false;
55+
auto node = rclcpp::Node::make_shared(
56+
"node_name", "", context, arguments, initial_values, use_global_arguments, use_intra_process);
57+
auto list_params_result = node->list_parameters({}, 0);
58+
EXPECT_EQ(3u, list_params_result.names.size());
59+
EXPECT_TRUE(node->get_parameter("foo").get_value<bool>());
60+
EXPECT_STREQ("hello world", node->get_parameter("bar").get_value<std::string>().c_str());
61+
std::vector<double> double_array = node->get_parameter("baz").get_value<std::vector<double>>();
62+
ASSERT_EQ(2u, double_array.size());
63+
EXPECT_DOUBLE_EQ(3.14, double_array.at(0));
64+
EXPECT_DOUBLE_EQ(2.718, double_array.at(1));
65+
}

rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_node.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class LifecycleNode : public node_interfaces::LifecycleNodeInterface,
100100
const std::string & namespace_,
101101
rclcpp::Context::SharedPtr context,
102102
const std::vector<std::string> & arguments,
103+
const std::vector<rclcpp::Parameter> & initial_values,
103104
bool use_global_arguments = true,
104105
bool use_intra_process_comms = false,
105106
bool start_parameter_services = true);

rclcpp_lifecycle/src/lifecycle_node.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ LifecycleNode::LifecycleNode(
5151
namespace_,
5252
rclcpp::contexts::default_context::get_global_default_context(),
5353
{},
54+
{},
5455
true,
5556
use_intra_process_comms,
5657
true)
@@ -61,6 +62,7 @@ LifecycleNode::LifecycleNode(
6162
const std::string & namespace_,
6263
rclcpp::Context::SharedPtr context,
6364
const std::vector<std::string> & arguments,
65+
const std::vector<rclcpp::Parameter> & initial_values,
6466
bool use_global_arguments,
6567
bool use_intra_process_comms,
6668
bool start_parameter_services)
@@ -75,6 +77,7 @@ LifecycleNode::LifecycleNode(
7577
node_base_,
7678
node_topics_,
7779
node_services_,
80+
initial_values,
7881
use_intra_process_comms,
7982
start_parameter_services
8083
)),

0 commit comments

Comments
 (0)