From 1471129a51e050c01e7dc2ca0c8fdf01626c2a89 Mon Sep 17 00:00:00 2001 From: Nico Zantopp Date: Wed, 20 Nov 2024 12:06:19 +0100 Subject: [PATCH 1/5] Set default parameter if the parameter is not defined in params.yml. Solves the problem that the parameter is not set if it is not defined in the parameter file. --- .../{{ package_name }}/{{ node_name }}.py.jinja | 1 + 1 file changed, 1 insertion(+) diff --git a/templates/ros2_python_pkg/{{ package_name }}/{{ package_name }}/{{ node_name }}.py.jinja b/templates/ros2_python_pkg/{{ package_name }}/{{ package_name }}/{{ node_name }}.py.jinja index 784b2ef..55df51c 100644 --- a/templates/ros2_python_pkg/{{ package_name }}/{{ package_name }}/{{ node_name }}.py.jinja +++ b/templates/ros2_python_pkg/{{ package_name }}/{{ package_name }}/{{ node_name }}.py.jinja @@ -108,6 +108,7 @@ class {{ node_class_name }}(Node): else: self.get_logger().warn(f"Missing parameter '{name}', using default value: {default}") param = default + self.set_parameters([rclpy.Parameter(name=name, value=param)]) # add parameter to auto-reconfigurable parameters if add_to_auto_reconfigurable_params: From be8ac451391744a105cf7fa839064cbd68a025c0 Mon Sep 17 00:00:00 2001 From: Nico Zantopp Date: Wed, 20 Nov 2024 16:15:59 +0100 Subject: [PATCH 2/5] Add default parameter setting in declareAndLoadParameter method to fix load of default param value --- .../{{ package_name }}/src/{{ node_name }}.cpp.jinja | 1 + 1 file changed, 1 insertion(+) diff --git a/templates/ros2_cpp_pkg/{{ package_name }}/src/{{ node_name }}.cpp.jinja b/templates/ros2_cpp_pkg/{{ package_name }}/src/{{ node_name }}.cpp.jinja index 02ee49d..18fe64b 100644 --- a/templates/ros2_cpp_pkg/{{ package_name }}/src/{{ node_name }}.cpp.jinja +++ b/templates/ros2_cpp_pkg/{{ package_name }}/src/{{ node_name }}.cpp.jinja @@ -126,6 +126,7 @@ void {{ node_class_name }}::declareAndLoadParameter(const std::string& name, for (const auto& element : param) ss << element << (&element != ¶m.back() ? ", " : "]"); } else { ss << param; + this->set_parameters({rclcpp::Parameter(name, param)}); } RCLCPP_WARN_STREAM(this->get_logger(), ss.str()); } From e3ab12c8438735d84543b22d1324f88a5fe71e1d Mon Sep 17 00:00:00 2001 From: Nico Zantopp Date: Wed, 20 Nov 2024 16:17:12 +0100 Subject: [PATCH 3/5] Update samples with set default parametet --- samples/ros2_cpp_all_pkg/src/ros2_cpp_node.cpp | 1 + samples/ros2_cpp_component_pkg/src/ros2_cpp_node.cpp | 1 + samples/ros2_cpp_lifecycle_pkg/src/ros2_cpp_node.cpp | 1 + samples/ros2_cpp_pkg/src/ros2_cpp_node.cpp | 1 + .../ros2_python_all_pkg/ros2_python_all_pkg/ros2_python_node.py | 1 + samples/ros2_python_pkg/ros2_python_pkg/ros2_python_node.py | 1 + 6 files changed, 6 insertions(+) diff --git a/samples/ros2_cpp_all_pkg/src/ros2_cpp_node.cpp b/samples/ros2_cpp_all_pkg/src/ros2_cpp_node.cpp index c6fb53d..3c0f800 100644 --- a/samples/ros2_cpp_all_pkg/src/ros2_cpp_node.cpp +++ b/samples/ros2_cpp_all_pkg/src/ros2_cpp_node.cpp @@ -102,6 +102,7 @@ void Ros2CppNode::declareAndLoadParameter(const std::string& name, for (const auto& element : param) ss << element << (&element != ¶m.back() ? ", " : "]"); } else { ss << param; + this->set_parameters({rclcpp::Parameter(name, param)}); } RCLCPP_WARN_STREAM(this->get_logger(), ss.str()); } diff --git a/samples/ros2_cpp_component_pkg/src/ros2_cpp_node.cpp b/samples/ros2_cpp_component_pkg/src/ros2_cpp_node.cpp index 9a59593..8864566 100644 --- a/samples/ros2_cpp_component_pkg/src/ros2_cpp_node.cpp +++ b/samples/ros2_cpp_component_pkg/src/ros2_cpp_node.cpp @@ -95,6 +95,7 @@ void Ros2CppNode::declareAndLoadParameter(const std::string& name, for (const auto& element : param) ss << element << (&element != ¶m.back() ? ", " : "]"); } else { ss << param; + this->set_parameters({rclcpp::Parameter(name, param)}); } RCLCPP_WARN_STREAM(this->get_logger(), ss.str()); } diff --git a/samples/ros2_cpp_lifecycle_pkg/src/ros2_cpp_node.cpp b/samples/ros2_cpp_lifecycle_pkg/src/ros2_cpp_node.cpp index f0bdd4e..da67d28 100644 --- a/samples/ros2_cpp_lifecycle_pkg/src/ros2_cpp_node.cpp +++ b/samples/ros2_cpp_lifecycle_pkg/src/ros2_cpp_node.cpp @@ -97,6 +97,7 @@ void Ros2CppNode::declareAndLoadParameter(const std::string& name, for (const auto& element : param) ss << element << (&element != ¶m.back() ? ", " : "]"); } else { ss << param; + this->set_parameters({rclcpp::Parameter(name, param)}); } RCLCPP_WARN_STREAM(this->get_logger(), ss.str()); } diff --git a/samples/ros2_cpp_pkg/src/ros2_cpp_node.cpp b/samples/ros2_cpp_pkg/src/ros2_cpp_node.cpp index 15ae866..ddfc005 100644 --- a/samples/ros2_cpp_pkg/src/ros2_cpp_node.cpp +++ b/samples/ros2_cpp_pkg/src/ros2_cpp_node.cpp @@ -92,6 +92,7 @@ void Ros2CppNode::declareAndLoadParameter(const std::string& name, for (const auto& element : param) ss << element << (&element != ¶m.back() ? ", " : "]"); } else { ss << param; + this->set_parameters({rclcpp::Parameter(name, param)}); } RCLCPP_WARN_STREAM(this->get_logger(), ss.str()); } diff --git a/samples/ros2_python_all_pkg/ros2_python_all_pkg/ros2_python_node.py b/samples/ros2_python_all_pkg/ros2_python_all_pkg/ros2_python_node.py index f5b1016..e21ff10 100644 --- a/samples/ros2_python_all_pkg/ros2_python_all_pkg/ros2_python_node.py +++ b/samples/ros2_python_all_pkg/ros2_python_all_pkg/ros2_python_node.py @@ -91,6 +91,7 @@ def declareAndLoadParameter(self, else: self.get_logger().warn(f"Missing parameter '{name}', using default value: {default}") param = default + self.set_parameters([rclpy.Parameter(name=name, value=param)]) # add parameter to auto-reconfigurable parameters if add_to_auto_reconfigurable_params: diff --git a/samples/ros2_python_pkg/ros2_python_pkg/ros2_python_node.py b/samples/ros2_python_pkg/ros2_python_pkg/ros2_python_node.py index 664bd43..ad58f33 100644 --- a/samples/ros2_python_pkg/ros2_python_pkg/ros2_python_node.py +++ b/samples/ros2_python_pkg/ros2_python_pkg/ros2_python_node.py @@ -87,6 +87,7 @@ def declareAndLoadParameter(self, else: self.get_logger().warn(f"Missing parameter '{name}', using default value: {default}") param = default + self.set_parameters([rclpy.Parameter(name=name, value=param)]) # add parameter to auto-reconfigurable parameters if add_to_auto_reconfigurable_params: From 727d794ad557af8a339eb39e95e42a572b9fbeb0 Mon Sep 17 00:00:00 2001 From: Nico Zantopp Date: Wed, 20 Nov 2024 17:19:10 +0100 Subject: [PATCH 4/5] Modify set default to fix bug with vector --- .../{{ package_name }}/src/{{ node_name }}.cpp.jinja | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/ros2_cpp_pkg/{{ package_name }}/src/{{ node_name }}.cpp.jinja b/templates/ros2_cpp_pkg/{{ package_name }}/src/{{ node_name }}.cpp.jinja index 18fe64b..6ad60d9 100644 --- a/templates/ros2_cpp_pkg/{{ package_name }}/src/{{ node_name }}.cpp.jinja +++ b/templates/ros2_cpp_pkg/{{ package_name }}/src/{{ node_name }}.cpp.jinja @@ -126,9 +126,9 @@ void {{ node_class_name }}::declareAndLoadParameter(const std::string& name, for (const auto& element : param) ss << element << (&element != ¶m.back() ? ", " : "]"); } else { ss << param; - this->set_parameters({rclcpp::Parameter(name, param)}); } RCLCPP_WARN_STREAM(this->get_logger(), ss.str()); + this->set_parameters({rclcpp::Parameter(name, rclcpp::ParameterValue(param))}); } } From 9d219bed189f3d21b5b4334589ed9c1b5e263959 Mon Sep 17 00:00:00 2001 From: Nico Zantopp Date: Wed, 20 Nov 2024 17:20:11 +0100 Subject: [PATCH 5/5] Updated samples to work with vector --- samples/ros2_cpp_all_pkg/src/ros2_cpp_node.cpp | 2 +- samples/ros2_cpp_component_pkg/src/ros2_cpp_node.cpp | 2 +- samples/ros2_cpp_lifecycle_pkg/src/ros2_cpp_node.cpp | 2 +- samples/ros2_cpp_pkg/src/ros2_cpp_node.cpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/samples/ros2_cpp_all_pkg/src/ros2_cpp_node.cpp b/samples/ros2_cpp_all_pkg/src/ros2_cpp_node.cpp index 3c0f800..c9997ab 100644 --- a/samples/ros2_cpp_all_pkg/src/ros2_cpp_node.cpp +++ b/samples/ros2_cpp_all_pkg/src/ros2_cpp_node.cpp @@ -102,9 +102,9 @@ void Ros2CppNode::declareAndLoadParameter(const std::string& name, for (const auto& element : param) ss << element << (&element != ¶m.back() ? ", " : "]"); } else { ss << param; - this->set_parameters({rclcpp::Parameter(name, param)}); } RCLCPP_WARN_STREAM(this->get_logger(), ss.str()); + this->set_parameters({rclcpp::Parameter(name, rclcpp::ParameterValue(param))}); } } diff --git a/samples/ros2_cpp_component_pkg/src/ros2_cpp_node.cpp b/samples/ros2_cpp_component_pkg/src/ros2_cpp_node.cpp index 8864566..f652eab 100644 --- a/samples/ros2_cpp_component_pkg/src/ros2_cpp_node.cpp +++ b/samples/ros2_cpp_component_pkg/src/ros2_cpp_node.cpp @@ -95,9 +95,9 @@ void Ros2CppNode::declareAndLoadParameter(const std::string& name, for (const auto& element : param) ss << element << (&element != ¶m.back() ? ", " : "]"); } else { ss << param; - this->set_parameters({rclcpp::Parameter(name, param)}); } RCLCPP_WARN_STREAM(this->get_logger(), ss.str()); + this->set_parameters({rclcpp::Parameter(name, rclcpp::ParameterValue(param))}); } } diff --git a/samples/ros2_cpp_lifecycle_pkg/src/ros2_cpp_node.cpp b/samples/ros2_cpp_lifecycle_pkg/src/ros2_cpp_node.cpp index da67d28..ab5311b 100644 --- a/samples/ros2_cpp_lifecycle_pkg/src/ros2_cpp_node.cpp +++ b/samples/ros2_cpp_lifecycle_pkg/src/ros2_cpp_node.cpp @@ -97,9 +97,9 @@ void Ros2CppNode::declareAndLoadParameter(const std::string& name, for (const auto& element : param) ss << element << (&element != ¶m.back() ? ", " : "]"); } else { ss << param; - this->set_parameters({rclcpp::Parameter(name, param)}); } RCLCPP_WARN_STREAM(this->get_logger(), ss.str()); + this->set_parameters({rclcpp::Parameter(name, rclcpp::ParameterValue(param))}); } } diff --git a/samples/ros2_cpp_pkg/src/ros2_cpp_node.cpp b/samples/ros2_cpp_pkg/src/ros2_cpp_node.cpp index ddfc005..c700c4e 100644 --- a/samples/ros2_cpp_pkg/src/ros2_cpp_node.cpp +++ b/samples/ros2_cpp_pkg/src/ros2_cpp_node.cpp @@ -92,9 +92,9 @@ void Ros2CppNode::declareAndLoadParameter(const std::string& name, for (const auto& element : param) ss << element << (&element != ¶m.back() ? ", " : "]"); } else { ss << param; - this->set_parameters({rclcpp::Parameter(name, param)}); } RCLCPP_WARN_STREAM(this->get_logger(), ss.str()); + this->set_parameters({rclcpp::Parameter(name, rclcpp::ParameterValue(param))}); } }