-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bug in rate_limiter filter and add more tests (#237)
- Loading branch information
1 parent
3a3e36e
commit 93955a8
Showing
9 changed files
with
320 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2024 AIT - Austrian Institute of Technology GmbH | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef CONTROL_FILTERS__CUSTOM_VALIDATORS_HPP_ | ||
#define CONTROL_FILTERS__CUSTOM_VALIDATORS_HPP_ | ||
|
||
#include <fmt/core.h> | ||
|
||
#include <string> | ||
|
||
#include <rclcpp/rclcpp.hpp> | ||
#include <rsl/parameter_validators.hpp> | ||
#include <tl_expected/expected.hpp> | ||
|
||
namespace control_filters { | ||
|
||
/** | ||
* @brief gt_eq, but check only if the value is not NaN | ||
*/ | ||
template <typename T> | ||
tl::expected<void, std::string> gt_eq_or_nan( | ||
rclcpp::Parameter const& parameter, T expected_value) { | ||
auto param_value = parameter.as_double(); | ||
if (!std::isnan(param_value)) { | ||
// check only if the value is not NaN | ||
return rsl::gt_eq<T>(parameter, expected_value); | ||
} | ||
return {}; | ||
} | ||
|
||
/** | ||
* @brief lt_eq, but check only if the value is not NaN | ||
*/ | ||
template <typename T> | ||
tl::expected<void, std::string> lt_eq_or_nan( | ||
rclcpp::Parameter const& parameter, T expected_value) { | ||
auto param_value = parameter.as_double(); | ||
if (!std::isnan(param_value)) { | ||
// check only if the value is not NaN | ||
return rsl::lt_eq<T>(parameter, expected_value); | ||
} | ||
return {}; | ||
} | ||
|
||
} // namespace control_filters | ||
|
||
#endif // CONTROL_FILTERS__CUSTOM_VALIDATORS_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2024 AIT - Austrian Institute of Technology GmbH | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "test_rate_limiter.hpp" | ||
|
||
TEST_F(RateLimiterTest, TestRateLimiterAllParameters) | ||
{ | ||
std::shared_ptr<filters::FilterBase<double>> filter_ = | ||
std::make_shared<control_filters::RateLimiter<double>>(); | ||
|
||
// should allow configuration and find parameters in sensor_filter_chain param namespace | ||
ASSERT_TRUE(filter_->configure("", "TestRateLimiter", | ||
node_->get_node_logging_interface(), node_->get_node_parameters_interface())); | ||
|
||
// change a parameter | ||
node_->set_parameter(rclcpp::Parameter("sampling_interval", 0.5)); | ||
// accept second call to configure with valid parameters to already configured filter | ||
ASSERT_TRUE(filter_->configure("", "TestRateLimiter", | ||
node_->get_node_logging_interface(), node_->get_node_parameters_interface())); | ||
} | ||
|
||
|
||
TEST_F(RateLimiterTest, TestRateLimiterMissingParameter) | ||
{ | ||
std::shared_ptr<filters::FilterBase<double>> filter_ = | ||
std::make_shared<control_filters::RateLimiter<double>>(); | ||
|
||
// should deny configuration as sampling_interval is missing | ||
ASSERT_FALSE(filter_->configure("", "TestRateLimiter", | ||
node_->get_node_logging_interface(), node_->get_node_parameters_interface())); | ||
} | ||
|
||
TEST_F(RateLimiterTest, TestRateLimiterInvalidThenFixedParameter) | ||
{ | ||
std::shared_ptr<filters::FilterBase<double>> filter_ = | ||
std::make_shared<control_filters::RateLimiter<double>>(); | ||
|
||
// should deny configuration as sampling_interval is invalid | ||
ASSERT_FALSE(filter_->configure("", "TestRateLimiter", | ||
node_->get_node_logging_interface(), node_->get_node_parameters_interface())); | ||
|
||
// fix the param | ||
node_->set_parameter(rclcpp::Parameter("sampling_interval", 1.0)); | ||
// should allow configuration and pass second call to unconfigured filter | ||
ASSERT_TRUE(filter_->configure("", "TestRateLimiter", | ||
node_->get_node_logging_interface(), node_->get_node_parameters_interface())); | ||
} | ||
|
||
TEST_F(RateLimiterTest, TestRateLimiterThrowsUnconfigured) | ||
{ | ||
std::shared_ptr<filters::FilterBase<double>> filter_ = | ||
std::make_shared<control_filters::RateLimiter<double>>(); | ||
double in, out; | ||
ASSERT_THROW(filter_->update(in, out), std::runtime_error); | ||
} | ||
|
||
TEST_F(RateLimiterTest, TestRateLimiterCompute) | ||
{ | ||
std::shared_ptr<filters::FilterBase<double>> filter_ = | ||
std::make_shared<control_filters::RateLimiter<double>>(); | ||
|
||
ASSERT_TRUE(filter_->configure("", "TestRateLimiter", | ||
node_->get_node_logging_interface(), node_->get_node_parameters_interface())); | ||
|
||
double in = 10.0, out; | ||
for (int i = 0; i < 10; i++) | ||
{ | ||
ASSERT_NO_THROW(filter_->update(in, out)); | ||
// no change | ||
EXPECT_THAT(out, ::testing::DoubleEq(in)); | ||
} | ||
in = 0.0; | ||
// takes 14 steps to reach 0 (first (10.0/1.0) plus second derivative limits) | ||
for (int i = 0; i < 14; i++) | ||
{ | ||
ASSERT_NO_THROW(filter_->update(in, out)); | ||
EXPECT_THAT(out, ::testing::Not(::testing::DoubleNear(in, 1e-6))) << "i=" << i; | ||
} | ||
ASSERT_NO_THROW(filter_->update(in, out)); | ||
EXPECT_THAT(out, ::testing::DoubleNear(in, 1e-6)); | ||
} | ||
|
||
int main(int argc, char ** argv) | ||
{ | ||
::testing::InitGoogleTest(&argc, argv); | ||
rclcpp::init(argc, argv); | ||
int result = RUN_ALL_TESTS(); | ||
rclcpp::shutdown(); | ||
return result; | ||
} |
Oops, something went wrong.