-
Notifications
You must be signed in to change notification settings - Fork 108
Fix bug in rate_limiter filter and add more tests #237
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e28b3c9
Fix copyright claim
christophfroehlich d47ac7d
Add custom validator excluding NaN values and update parameter descri…
christophfroehlich d37ff0a
Add tests
christophfroehlich e1f9b28
Fix bug in memory old outputs
christophfroehlich 8123f1f
Pass validators to CMake file
christophfroehlich 4b8e49f
Fix templated class member variable types
christophfroehlich c6336cd
Add compute test and properly initialize internal state
christophfroehlich 361dd0c
Update test criterion
christophfroehlich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.