-
Notifications
You must be signed in to change notification settings - Fork 99
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
Fix bug in rate_limiter filter and add more tests #237
Open
christophfroehlich
wants to merge
7
commits into
ros2-master
Choose a base branch
from
test/rate_limiter
base: ros2-master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+320
−28
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bb56bda
Fix copyright claim
christophfroehlich 0eda373
Add custom validator excluding NaN values and update parameter descri…
christophfroehlich b4cfba4
Add tests
christophfroehlich 9e702a4
Fix bug in memory old outputs
christophfroehlich e68482c
Pass validators to CMake file
christophfroehlich e578b0c
Fix templated class member variable types
christophfroehlich d984267
Add compute test and properly initialize internal state
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 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 12 steps to reach 0 (first and second derivative limits) | ||
for (int i = 0; i < 11; 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is infinite a better default value here? Or Nan is better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in principle you are right, and it would already work setting it + or -infinite. However, NaN is the default value of the diff_drive_controller. And the logic of "copying the positive value if negative is not given" would not work because infinite is a valid limit 🤔