Skip to content

Commit e25da92

Browse files
Forse ROS2 parameters to be static to meet RCLCPP API changes (#2239)
* Forse ROS2 parameters to be static to meet RCLCPP API changes * Fix comments * Move all common CostmapFilter parameter declarations into try-catch block
1 parent b91f1cc commit e25da92

File tree

16 files changed

+96
-66
lines changed

16 files changed

+96
-66
lines changed

nav2_behavior_tree/include/nav2_behavior_tree/bt_action_server_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ BtActionServer<ActionT>::BtActionServer(
4949
clock_ = node->get_clock();
5050

5151
// Declare this node's parameters
52-
node->declare_parameter("default_bt_xml_filename");
52+
node->declare_parameter("default_bt_xml_filename", rclcpp::PARAMETER_STRING);
5353
node->declare_parameter("enable_groot_monitoring", true);
5454
node->declare_parameter("groot_zmq_publisher_port", 1666);
5555
node->declare_parameter("groot_zmq_server_port", 1667);

nav2_costmap_2d/include/nav2_costmap_2d/layer.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ class Layer // TODO(mjeronimo): public nav2_util::LifecycleHelperInterface
128128
const std::string & param_name,
129129
const rclcpp::ParameterValue & value);
130130
void declareParameter(
131-
const std::string & param_name);
131+
const std::string & param_name,
132+
const rclcpp::ParameterType & param_type);
132133
bool hasParameter(const std::string & param_name);
133134
std::string getFullName(const std::string & param_name);
134135

nav2_costmap_2d/plugins/costmap_filters/costmap_filter.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,22 @@ void CostmapFilter::onInitialize()
6060
throw std::runtime_error{"Failed to lock node"};
6161
}
6262

63-
// Declare common for all costmap filters parameters
64-
declareParameter("enabled", rclcpp::ParameterValue(true));
65-
declareParameter("filter_info_topic");
66-
declareParameter("transform_tolerance", rclcpp::ParameterValue(0.1));
67-
68-
// Get parameters
69-
node->get_parameter(name_ + "." + "enabled", enabled_);
7063
try {
64+
// Declare common for all costmap filters parameters
65+
declareParameter("enabled", rclcpp::ParameterValue(true));
66+
declareParameter("filter_info_topic", rclcpp::PARAMETER_STRING);
67+
declareParameter("transform_tolerance", rclcpp::ParameterValue(0.1));
68+
69+
// Get parameters
70+
node->get_parameter(name_ + "." + "enabled", enabled_);
7171
filter_info_topic_ = node->get_parameter(name_ + "." + "filter_info_topic").as_string();
72-
} catch (rclcpp::exceptions::ParameterNotDeclaredException & ex) {
73-
RCLCPP_ERROR(logger_, "filter_info_topic parameter is not set");
72+
double transform_tolerance;
73+
node->get_parameter(name_ + "." + "transform_tolerance", transform_tolerance);
74+
transform_tolerance_ = tf2::durationFromSec(transform_tolerance);
75+
} catch (const std::exception & ex) {
76+
RCLCPP_ERROR(logger_, "Parameter problem: %s", ex.what());
7477
throw ex;
7578
}
76-
double transform_tolerance;
77-
node->get_parameter(name_ + "." + "transform_tolerance", transform_tolerance);
78-
transform_tolerance_ = tf2::durationFromSec(transform_tolerance);
7979
}
8080

8181
void CostmapFilter::activate()

nav2_costmap_2d/src/layer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,16 @@ Layer::declareParameter(
9191

9292
void
9393
Layer::declareParameter(
94-
const std::string & param_name)
94+
const std::string & param_name,
95+
const rclcpp::ParameterType & param_type)
9596
{
9697
auto node = node_.lock();
9798
if (!node) {
9899
throw std::runtime_error{"Failed to lock node"};
99100
}
100101
local_params_.insert(param_name);
101102
nav2_util::declare_parameter_if_not_declared(
102-
node, getFullName(param_name));
103+
node, getFullName(param_name), param_type);
103104
}
104105

105106
bool

nav2_costmap_2d/test/unit/declare_parameter_test.cpp

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class LayerWrapper : public nav2_costmap_2d::Layer
3636
bool isClearable() {return false;}
3737
};
3838

39-
TEST(DeclareParameter, useValidParameter2Args)
39+
TEST(DeclareParameter, useValidParameter)
4040
{
4141
LayerWrapper layer;
4242
nav2_util::LifecycleNode::SharedPtr node =
@@ -55,7 +55,7 @@ TEST(DeclareParameter, useValidParameter2Args)
5555
}
5656
}
5757

58-
TEST(DeclareParameter, useValidParameter1Arg)
58+
TEST(DeclareParameter, useInvalidParameter)
5959
{
6060
LayerWrapper layer;
6161
nav2_util::LifecycleNode::SharedPtr node =
@@ -65,31 +65,10 @@ TEST(DeclareParameter, useValidParameter1Arg)
6565

6666
layer.initialize(&layers, "test_layer", &tf, node, nullptr, nullptr);
6767

68-
layer.declareParameter("test2");
6968
try {
70-
node->set_parameter(rclcpp::Parameter("test_layer.test2", "test_val2"));
71-
std::string val = node->get_parameter("test_layer.test2").as_string();
72-
EXPECT_EQ(val, "test_val2");
73-
} catch (rclcpp::exceptions::ParameterNotDeclaredException & ex) {
74-
FAIL() << "test_layer.test2 parameter is not set";
75-
}
76-
}
77-
78-
TEST(DeclareParameter, useInvalidParameter1Arg)
79-
{
80-
LayerWrapper layer;
81-
nav2_util::LifecycleNode::SharedPtr node =
82-
std::make_shared<nav2_util::LifecycleNode>("test_node");
83-
tf2_ros::Buffer tf(node->get_clock());
84-
nav2_costmap_2d::LayeredCostmap layers("frame", false, false);
85-
86-
layer.initialize(&layers, "test_layer", &tf, node, nullptr, nullptr);
87-
88-
layer.declareParameter("test3");
89-
try {
90-
std::string val = node->get_parameter("test_layer.test3").as_string();
91-
FAIL() << "Incorrectly handling test_layer.test3 parameters which was not set";
92-
} catch (rclcpp::exceptions::ParameterNotDeclaredException & ex) {
69+
layer.declareParameter("test2", rclcpp::PARAMETER_STRING);
70+
FAIL() << "Incorrectly handling test_layer.test2 parameter which was not set";
71+
} catch (rclcpp::exceptions::NoParameterOverrideProvided & ex) {
9372
SUCCEED();
9473
}
9574
}

nav2_dwb_controller/dwb_core/src/dwb_local_planner.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ void DWBLocalPlanner::configure(
7676
costmap_ros_ = costmap_ros;
7777
tf_ = tf;
7878
dwb_plugin_name_ = name;
79-
declare_parameter_if_not_declared(node, dwb_plugin_name_ + ".critics");
80-
declare_parameter_if_not_declared(node, dwb_plugin_name_ + ".default_critic_namespaces");
79+
declare_parameter_if_not_declared(
80+
node, dwb_plugin_name_ + ".critics",
81+
rclcpp::PARAMETER_STRING_ARRAY);
82+
declare_parameter_if_not_declared(
83+
node, dwb_plugin_name_ + ".default_critic_namespaces",
84+
rclcpp::ParameterValue(std::vector<std::string>()));
8185
declare_parameter_if_not_declared(
8286
node, dwb_plugin_name_ + ".prune_plan",
8387
rclcpp::ParameterValue(true));

nav2_dwb_controller/dwb_plugins/src/limited_accel_generator.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,20 @@ void LimitedAccelGenerator::initialize(
5151
plugin_name_ = plugin_name;
5252
StandardTrajectoryGenerator::initialize(nh, plugin_name_);
5353

54-
nav2_util::declare_parameter_if_not_declared(nh, plugin_name + ".sim_period");
55-
56-
if (nh->get_parameter(plugin_name + ".sim_period", acceleration_time_)) {
57-
} else {
54+
try {
55+
nav2_util::declare_parameter_if_not_declared(
56+
nh, plugin_name + ".sim_period", rclcpp::PARAMETER_DOUBLE);
57+
if (!nh->get_parameter(plugin_name + ".sim_period", acceleration_time_)) {
58+
// This actually should never appear, since declare_parameter_if_not_declared()
59+
// completed w/o exceptions guarantee that static parameter will be initialized
60+
// with some value. However for reliability we should also process the case
61+
// when get_parameter() will return a failure for some other reasons.
62+
throw std::runtime_error("Failed to get 'sim_period' value");
63+
}
64+
} catch (std::exception &) {
65+
RCLCPP_WARN(
66+
rclcpp::get_logger("LimitedAccelGenerator"),
67+
"'sim_period' parameter is not set for %s", plugin_name.c_str());
5868
double controller_frequency = nav_2d_utils::searchAndGetParam(
5969
nh, "controller_frequency", 20.0);
6070
if (controller_frequency > 0) {

nav2_lifecycle_manager/src/lifecycle_manager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ LifecycleManager::LifecycleManager()
3838

3939
// The list of names is parameterized, allowing this module to be used with a different set
4040
// of nodes
41-
declare_parameter("node_names");
41+
declare_parameter("node_names", rclcpp::PARAMETER_STRING_ARRAY);
4242
declare_parameter("autostart", rclcpp::ParameterValue(false));
4343
declare_parameter("bond_timeout", 4.0);
4444

nav2_map_server/src/map_server/map_server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ MapServer::MapServer()
6868
RCLCPP_INFO(get_logger(), "Creating");
6969

7070
// Declare the node parameters
71-
declare_parameter("yaml_filename");
71+
declare_parameter("yaml_filename", rclcpp::PARAMETER_STRING);
7272
declare_parameter("topic_name", "map");
7373
declare_parameter("frame_id", "map");
7474
}

nav2_util/include/nav2_util/node_utils.hpp

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,20 @@ std::string time_to_string(size_t len);
8080
rclcpp::NodeOptions
8181
get_node_options_default(bool allow_undeclared = true, bool declare_initial_params = true);
8282

83+
/// Declares static ROS2 parameter and sets it to a given value if it was not already declared
84+
/* Declares static ROS2 parameter and sets it to a given value
85+
* if it was not already declared.
86+
*
87+
* \param[in] node A node in which given parameter to be declared
88+
* \param[in] param_name The name of parameter
89+
* \param[in] default_value Parameter value to initialize with
90+
* \param[in] parameter_descriptor Parameter descriptor (optional)
91+
*/
8392
template<typename NodeT>
8493
void declare_parameter_if_not_declared(
8594
NodeT node,
8695
const std::string & param_name,
87-
const rclcpp::ParameterValue & default_value = rclcpp::ParameterValue(),
96+
const rclcpp::ParameterValue & default_value,
8897
const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor =
8998
rcl_interfaces::msg::ParameterDescriptor())
9099
{
@@ -93,15 +102,54 @@ void declare_parameter_if_not_declared(
93102
}
94103
}
95104

105+
/// Declares static ROS2 parameter with given type if it was not already declared
106+
/* Declares static ROS2 parameter with given type if it was not already declared.
107+
* NOTE: The parameter should be set via input param-file
108+
* or throught a command-line. Otherwise according to the RCLCPP API,
109+
* NoParameterOverrideProvided exception will be thrown by declare_parameter().
110+
*
111+
* \param[in] node A node in which given parameter to be declared
112+
* \param[in] param_type The type of parameter
113+
* \param[in] default_value Parameter value to initialize with
114+
* \param[in] parameter_descriptor Parameter descriptor (optional)
115+
*/
116+
template<typename NodeT>
117+
void declare_parameter_if_not_declared(
118+
NodeT node,
119+
const std::string & param_name,
120+
const rclcpp::ParameterType & param_type,
121+
const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor =
122+
rcl_interfaces::msg::ParameterDescriptor())
123+
{
124+
if (!node->has_parameter(param_name)) {
125+
node->declare_parameter(param_name, param_type, parameter_descriptor);
126+
}
127+
}
128+
129+
/// Gets the type of plugin for the selected node and its plugin
130+
/**
131+
* Gets the type of plugin for the selected node and its plugin.
132+
* Actually seeks for the value of "<plugin_name>.plugin" parameter.
133+
*
134+
* \param[in] node Selected node
135+
* \param[in] plugin_name The name of plugin the type of which is being searched for
136+
* \return A string containing the type of plugin (the value of "<plugin_name>.plugin" parameter)
137+
*/
96138
template<typename NodeT>
97139
std::string get_plugin_type_param(
98140
NodeT node,
99141
const std::string & plugin_name)
100142
{
101-
declare_parameter_if_not_declared(node, plugin_name + ".plugin");
143+
try {
144+
declare_parameter_if_not_declared(node, plugin_name + ".plugin", rclcpp::PARAMETER_STRING);
145+
} catch (rclcpp::exceptions::NoParameterOverrideProvided & ex) {
146+
RCLCPP_FATAL(node->get_logger(), "'plugin' param not defined for %s", plugin_name.c_str());
147+
exit(-1);
148+
}
102149
std::string plugin_type;
103150
if (!node->get_parameter(plugin_name + ".plugin", plugin_type)) {
104-
RCLCPP_FATAL(node->get_logger(), "'plugin' param not defined for %s", plugin_name.c_str());
151+
RCLCPP_FATAL(
152+
node->get_logger(), "Can not get 'plugin' param value for %s", plugin_name.c_str());
105153
exit(-1);
106154
}
107155
return plugin_type;

0 commit comments

Comments
 (0)