diff --git a/example/test/example_test_gtest.cpp b/example/test/example_test_gtest.cpp index 40e06e14..fcae688c 100644 --- a/example/test/example_test_gtest.cpp +++ b/example/test/example_test_gtest.cpp @@ -64,6 +64,15 @@ TEST_F(ExampleTest, check_parameters) { ASSERT_EQ(params_.ft_sensor.filter_coefficient, 0.1); } +TEST_F(ExampleTest, try_update_params) { + ASSERT_FALSE(param_listener_->try_update_params(params_)); + + const rclcpp ::Parameter new_param("interpolation_mode", "linear"); + example_test_node_->set_parameter(new_param); + ASSERT_TRUE(param_listener_->try_update_params(params_)); + ASSERT_EQ(params_.interpolation_mode, "linear"); +} + TEST_F(ExampleTest, try_get_params) { ASSERT_TRUE(param_listener_->try_get_params(params_)); diff --git a/generate_parameter_library_py/generate_parameter_library_py/jinja_templates/cpp/parameter_library_header b/generate_parameter_library_py/generate_parameter_library_py/jinja_templates/cpp/parameter_library_header index 179d5060..29562e65 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/jinja_templates/cpp/parameter_library_header +++ b/generate_parameter_library_py/generate_parameter_library_py/jinja_templates/cpp/parameter_library_header @@ -124,6 +124,30 @@ struct StackParams { return params_; } + /** + * @brief Tries to update the parsed Params object + * @param params_in The Params object to update + * @return true if the Params object was updated, false if it was already up to date or the mutex could not be locked + * @note This function tries to lock the mutex without blocking, so it can be used in a RT loop + */ + bool try_update_params(Params & params_in) const { + std::unique_lock lock(mutex_, std::try_to_lock); + if (lock.owns_lock()) { + if (const bool is_old = params_in.__stamp != params_.__stamp; is_old) { + params_in = params_; + return true; + } + } + return false; + } + + /** + * @brief Tries to get the current Params object + * @param params_in The Params object to fill with the current parameters + * @return true if mutex can be locked, false if mutex could not be locked + * @note The parameters are only filled, when the mutex can be locked and the params timestamp is different + * @note This function tries to lock the mutex without blocking, so it can be used in a RT loop + */ bool try_get_params(Params & params_in) const { if (mutex_.try_lock()) { if (const bool is_old = params_in.__stamp != params_.__stamp; is_old) {