@@ -80,11 +80,20 @@ std::string time_to_string(size_t len);
8080rclcpp::NodeOptions
8181get_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+ */
8392template <typename NodeT>
8493void 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+ */
96138template <typename NodeT>
97139std::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