Skip to content

Commit efef248

Browse files
authored
Add try_update_params method (#260)
* Improve method * fix tests * add try_update_params method
1 parent b032766 commit efef248

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

example/test/example_test_gtest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ TEST_F(ExampleTest, check_parameters) {
6464
ASSERT_EQ(params_.ft_sensor.filter_coefficient, 0.1);
6565
}
6666

67+
TEST_F(ExampleTest, try_update_params) {
68+
ASSERT_FALSE(param_listener_->try_update_params(params_));
69+
70+
const rclcpp ::Parameter new_param("interpolation_mode", "linear");
71+
example_test_node_->set_parameter(new_param);
72+
ASSERT_TRUE(param_listener_->try_update_params(params_));
73+
ASSERT_EQ(params_.interpolation_mode, "linear");
74+
}
75+
6776
TEST_F(ExampleTest, try_get_params) {
6877
ASSERT_TRUE(param_listener_->try_get_params(params_));
6978

generate_parameter_library_py/generate_parameter_library_py/jinja_templates/cpp/parameter_library_header

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,30 @@ struct StackParams {
124124
return params_;
125125
}
126126

127+
/**
128+
* @brief Tries to update the parsed Params object
129+
* @param params_in The Params object to update
130+
* @return true if the Params object was updated, false if it was already up to date or the mutex could not be locked
131+
* @note This function tries to lock the mutex without blocking, so it can be used in a RT loop
132+
*/
133+
bool try_update_params(Params & params_in) const {
134+
std::unique_lock<std::mutex> lock(mutex_, std::try_to_lock);
135+
if (lock.owns_lock()) {
136+
if (const bool is_old = params_in.__stamp != params_.__stamp; is_old) {
137+
params_in = params_;
138+
return true;
139+
}
140+
}
141+
return false;
142+
}
143+
144+
/**
145+
* @brief Tries to get the current Params object
146+
* @param params_in The Params object to fill with the current parameters
147+
* @return true if mutex can be locked, false if mutex could not be locked
148+
* @note The parameters are only filled, when the mutex can be locked and the params timestamp is different
149+
* @note This function tries to lock the mutex without blocking, so it can be used in a RT loop
150+
*/
127151
bool try_get_params(Params & params_in) const {
128152
if (mutex_.try_lock()) {
129153
if (const bool is_old = params_in.__stamp != params_.__stamp; is_old) {

0 commit comments

Comments
 (0)