Skip to content

Add try_update_params method #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions example/test/example_test_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> 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) {
Expand Down
Loading