Skip to content

Commit

Permalink
Improved parameter_handler usage when re-configure
Browse files Browse the repository at this point in the history
  • Loading branch information
guihomework committed Mar 9, 2023
1 parent 9cee59e commit 528d4ea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
31 changes: 17 additions & 14 deletions include/control_filters/gravity_compensation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,24 @@ bool GravityCompensation<T>::configure()
logger_.reset(
new rclcpp::Logger(this->logging_interface_->get_logger().get_child(this->filter_name_)));

// Initialize the parameters
try
// Initialize the parameter_listener once
if (!parameter_handler_)
{
parameter_handler_ =
std::make_shared<gravity_compensation_filter::ParamListener>(this->params_interface_);
}
catch (rclcpp::exceptions::ParameterUninitializedException & ex) {
RCLCPP_ERROR((*logger_), "GravityCompensation filter cannot be configured: %s", ex.what());
parameter_handler_.reset();
return false;
}
catch (rclcpp::exceptions::InvalidParameterValueException & ex) {
RCLCPP_ERROR((*logger_), "GravityCompensation filter cannot be configured: %s", ex.what());
parameter_handler_.reset();
return false;
try
{
parameter_handler_ =
std::make_shared<gravity_compensation_filter::ParamListener>(this->params_interface_);
}
catch (rclcpp::exceptions::ParameterUninitializedException & ex) {
RCLCPP_ERROR((*logger_), "GravityCompensation filter cannot be configured: %s", ex.what());
parameter_handler_.reset();
return false;
}
catch (rclcpp::exceptions::InvalidParameterValueException & ex) {
RCLCPP_ERROR((*logger_), "GravityCompensation filter cannot be configured: %s", ex.what());
parameter_handler_.reset();
return false;
}
}
parameters_ = parameter_handler_->get_params();
compute_internal_params();
Expand Down
10 changes: 8 additions & 2 deletions test/control_filters/test_gravity_compensation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ TEST_F(GravityCompensationTest, TestGravityCompensationMissingParameters)
*/
}

TEST_F(GravityCompensationTest, TestGravityCompensationInvalidParameters)
TEST_F(GravityCompensationTest, TestGravityCompensationParameters)
{
std::shared_ptr<filters::FilterBase<geometry_msgs::msg::WrenchStamped>> filter_ =
std::make_shared<control_filters::GravityCompensation<geometry_msgs::msg::WrenchStamped>>();
Expand All @@ -49,7 +49,13 @@ TEST_F(GravityCompensationTest, TestGravityCompensationInvalidParameters)
node_->get_node_logging_interface(), node_->get_node_parameters_interface()));

node_->set_parameter(rclcpp::Parameter("CoG.pos", std::vector<double>({0.0, 0.0, 0.0})));
// all parameters correctly set
// all parameters correctly set AND second call to yet unconfigured filter
ASSERT_TRUE(filter_->configure("", "TestGravityCompensationFilter",
node_->get_node_logging_interface(), node_->get_node_parameters_interface()));

// change a parameter
node_->set_parameter(rclcpp::Parameter("CoG.pos", std::vector<double>({0.0, 0.0, 0.2})));
// accept second call to configure with valid parameters to already configured filter
ASSERT_TRUE(filter_->configure("", "TestGravityCompensationFilter",
node_->get_node_logging_interface(), node_->get_node_parameters_interface()));
}
Expand Down

0 comments on commit 528d4ea

Please sign in to comment.