-
Notifications
You must be signed in to change notification settings - Fork 564
add LifecycleTimer #3263
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
Open
armaho
wants to merge
3
commits into
ros2:rolling
Choose a base branch
from
armaho:lifecycle_timer
base: rolling
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.
Open
add LifecycleTimer #3263
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
90 changes: 90 additions & 0 deletions
90
rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_timer.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // Copyright 2026 Open Source Robotics Foundation, Inc. | ||
| // | ||
| // 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 RCLCPP_LIFECYCLE__LIFECYCLE_TIMER_HPP_ | ||
| #define RCLCPP_LIFECYCLE__LIFECYCLE_TIMER_HPP_ | ||
|
|
||
| #include <chrono> | ||
| #include <memory> | ||
| #include <utility> | ||
|
|
||
| #include "rclcpp/timer.hpp" | ||
|
|
||
| #include "rclcpp_lifecycle/managed_entity.hpp" | ||
|
|
||
| namespace rclcpp_lifecycle | ||
| { | ||
|
|
||
| template<typename FunctorT> | ||
| class LifecycleGenericTimer : public SimpleManagedEntity, | ||
| public rclcpp::GenericTimer<FunctorT> { | ||
| public: | ||
| RCLCPP_SMART_PTR_DEFINITIONS(LifecycleGenericTimer) | ||
|
|
||
| /// Default constructor. | ||
| /** | ||
| * \param[in] clock The clock providing the current time. | ||
| * \param[in] period The interval at which the timer fires. | ||
| * \param[in] callback User-specified callback function. | ||
| * \param[in] context custom context to be used. | ||
| */ | ||
| explicit LifecycleGenericTimer( | ||
| rclcpp::Clock::SharedPtr clock, | ||
| std::chrono::nanoseconds period, | ||
| FunctorT && callback, | ||
| rclcpp::Context::SharedPtr context) | ||
| : rclcpp::GenericTimer<FunctorT>(clock, period, std::move(callback), | ||
| context, false) {} | ||
|
|
||
| void on_activate() override | ||
| { | ||
| SimpleManagedEntity::on_activate(); | ||
| rclcpp::GenericTimer<FunctorT>::reset(); | ||
| } | ||
|
|
||
| void on_deactivate() override | ||
| { | ||
| SimpleManagedEntity::on_deactivate(); | ||
| rclcpp::GenericTimer<FunctorT>::cancel(); | ||
| } | ||
|
|
||
| protected: | ||
| RCLCPP_DISABLE_COPY(LifecycleGenericTimer) | ||
| }; | ||
|
|
||
| template<typename FunctorT> | ||
| class LifecycleWallTimer : public LifecycleGenericTimer<FunctorT> { | ||
| public: | ||
| RCLCPP_SMART_PTR_DEFINITIONS(LifecycleWallTimer) | ||
|
|
||
| /// Default constructor. | ||
| /** | ||
| * \param period The interval at which the timer fires | ||
| * \param callback The callback function to execute every interval | ||
| * \param context node context | ||
| */ | ||
| LifecycleWallTimer( | ||
| std::chrono::nanoseconds period, FunctorT && callback, | ||
| rclcpp::Context::SharedPtr context) | ||
| : LifecycleGenericTimer<FunctorT>( | ||
| std::make_shared<rclcpp::Clock>(RCL_STEADY_TIME), period, | ||
| std::move(callback), context) {} | ||
|
|
||
| protected: | ||
| RCLCPP_DISABLE_COPY(LifecycleWallTimer) | ||
| }; | ||
|
|
||
| } // namespace rclcpp_lifecycle | ||
|
|
||
| #endif // RCLCPP_LIFECYCLE__LIFECYCLE_TIMER_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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| // Copyright 2026 Open Source Robotics Foundation, Inc. | ||
| // | ||
| // 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 <gtest/gtest.h> | ||
| #include <chrono> | ||
|
|
||
| #include "rclcpp/executors/single_threaded_executor.hpp" | ||
|
|
||
| #include "lifecycle_msgs/msg/state.hpp" | ||
| #include "lifecycle_msgs/msg/transition.hpp" | ||
|
|
||
| #include "rclcpp_lifecycle/lifecycle_node.hpp" | ||
|
|
||
| using lifecycle_msgs::msg::State; | ||
| using lifecycle_msgs::msg::Transition; | ||
|
|
||
| using namespace std::chrono_literals; | ||
|
|
||
| enum class TimerType | ||
| { | ||
| WALL_TIMER, | ||
| GENERIC_TIMER, | ||
| }; | ||
|
|
||
| class TestLifecycleTimer : public ::testing::TestWithParam<TimerType> { | ||
| protected: | ||
| static void SetUpTestCase() {rclcpp::init(0, nullptr);} | ||
| static void TearDownTestCase() {rclcpp::shutdown();} | ||
| }; | ||
|
|
||
| TEST_P(TestLifecycleTimer, timer_becomes_activated_and_deactivated_with_node) { | ||
| std::atomic_bool is_executed = false; | ||
| auto node = std::make_shared<rclcpp_lifecycle::LifecycleNode>("node"); | ||
|
|
||
| ASSERT_EQ(State::PRIMARY_STATE_UNCONFIGURED, node->get_current_state().id()); | ||
|
|
||
| auto success = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface:: | ||
| CallbackReturn::SUCCESS; | ||
| auto error = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface:: | ||
| CallbackReturn::ERROR; | ||
| auto ret = error; | ||
|
|
||
| node->trigger_transition( | ||
| rclcpp_lifecycle::Transition(Transition::TRANSITION_CONFIGURE), ret); | ||
| ASSERT_EQ(success, ret) << "node transition failed"; | ||
| ret = error; | ||
|
|
||
| TimerType timer_type = GetParam(); | ||
| std::function<void()> callback = [&]() {is_executed = true;}; | ||
| rclcpp_lifecycle::LifecycleGenericTimer<std::function<void()>>::SharedPtr | ||
| timer; | ||
| switch (timer_type) { | ||
| case TimerType::WALL_TIMER: { | ||
| timer = node->create_lifecycle_wall_timer(100ms, callback); | ||
| break; | ||
| } | ||
| case TimerType::GENERIC_TIMER: { | ||
| timer = node->create_lifecycle_timer(100ms, callback); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| auto exec = std::make_shared<rclcpp::executors::SingleThreadedExecutor>(); | ||
| exec->add_node(node->get_node_base_interface()); | ||
|
|
||
| auto spinner = std::thread([&]() {exec->spin();}); | ||
|
|
||
| auto cleanup = rcpputils::make_scope_exit([&]() { | ||
| exec->cancel(); | ||
| spinner.join(); | ||
| }); | ||
|
|
||
| // Timer should not be executed until the node is activated | ||
| ASSERT_FALSE(timer->is_activated()) | ||
| << "managed timer is active while its node is not"; | ||
| std::this_thread::sleep_for(500ms); | ||
| ASSERT_FALSE(is_executed) | ||
| << "managed timer was executed while node was inactive"; | ||
|
|
||
| node->trigger_transition( | ||
| rclcpp_lifecycle::Transition(Transition::TRANSITION_ACTIVATE), ret); | ||
| ASSERT_EQ(success, ret) << "node transition failed"; | ||
| ret = error; | ||
|
|
||
| // Now, the timer should be activated and its callback should be executed | ||
| ASSERT_TRUE(timer->is_activated()) | ||
| << "managed timer should be activated with the node"; | ||
| std::this_thread::sleep_for(500ms); | ||
| ASSERT_TRUE(is_executed) | ||
| << "managed timer was not executed while the node was active"; | ||
|
|
||
| node->trigger_transition( | ||
| rclcpp_lifecycle::Transition(Transition::TRANSITION_DEACTIVATE), ret); | ||
| ASSERT_EQ(success, ret) << "node transition failed"; | ||
| ret = error; | ||
| is_executed = false; | ||
|
|
||
| // Timer should not be executed after deactivation | ||
| ASSERT_FALSE(timer->is_activated()) | ||
| << "managed timer is active while its node is not"; | ||
| std::this_thread::sleep_for(500ms); | ||
| ASSERT_FALSE(is_executed) | ||
| << "managed timer was executed while node was inactive"; | ||
|
|
||
| node->trigger_transition( | ||
| rclcpp_lifecycle::Transition(Transition::TRANSITION_INACTIVE_SHUTDOWN), | ||
| ret); | ||
| ASSERT_EQ(success, ret) << "node transition failed"; | ||
| } | ||
|
|
||
| INSTANTIATE_TEST_SUITE_P( | ||
| PerTimerType, TestLifecycleTimer, | ||
| ::testing::Values(TimerType::WALL_TIMER, TimerType::GENERIC_TIMER), | ||
| [](const ::testing::TestParamInfo<TimerType> & info) -> std::string { | ||
| switch (info.param) { | ||
| case TimerType::WALL_TIMER: | ||
| return std::string("wall_timer"); | ||
| case TimerType::GENERIC_TIMER: | ||
| return std::string("generic_timer"); | ||
| default: | ||
| break; | ||
| } | ||
| return std::string("unknown"); | ||
| }); | ||
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.