-
Notifications
You must be signed in to change notification settings - Fork 357
Add composition::LifecycleTalker component #746
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
SuperJappie08
wants to merge
3
commits into
ros2:rolling
Choose a base branch
from
SuperJappie08:rolling-lifecycle-component
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
Changes from all commits
Commits
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
66 changes: 66 additions & 0 deletions
66
composition/include/composition/lifecycle_talker_component.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,66 @@ | ||
| // Copyright 2025 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 COMPOSITION__LIFECYCLE_TALKER_COMPONENT_HPP_ | ||
| #define COMPOSITION__LIFECYCLE_TALKER_COMPONENT_HPP_ | ||
|
|
||
| #include "composition/visibility_control.h" | ||
|
|
||
| #include "rclcpp/rclcpp.hpp" | ||
|
|
||
| #include "rclcpp_lifecycle/lifecycle_node.hpp" | ||
| #include "rclcpp_lifecycle/lifecycle_publisher.hpp" | ||
|
|
||
| #include "example_interfaces/msg/string.hpp" | ||
|
|
||
| namespace composition | ||
| { | ||
|
|
||
| class LifecycleTalker : public rclcpp_lifecycle::LifecycleNode { | ||
| public: | ||
| COMPOSITION_PUBLIC | ||
| explicit LifecycleTalker(const rclcpp::NodeOptions & options); | ||
|
|
||
| protected: | ||
| void publish(); | ||
|
|
||
| rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn on_configure( | ||
| const rclcpp_lifecycle::State & state) override; | ||
|
|
||
| rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn on_cleanup( | ||
| const rclcpp_lifecycle::State & state) override; | ||
|
|
||
| rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn on_shutdown( | ||
| const rclcpp_lifecycle::State & state) override; | ||
|
|
||
| private: | ||
| size_t count_; | ||
|
|
||
| // We hold an instance of a lifecycle publisher. This lifecycle publisher | ||
| // can be activated or deactivated regarding on which state the lifecycle node | ||
| // is in. | ||
| // By default, a lifecycle publisher is inactive by creation and has to be | ||
| // activated to publish messages into the ROS world. | ||
| rclcpp_lifecycle::LifecyclePublisher<example_interfaces::msg::String>::SharedPtr pub_; | ||
|
|
||
| // We hold an instance of a timer which periodically triggers the publish function. | ||
| // As for the beta version, this is a regular timer. In a future version, a | ||
| // lifecycle timer will be created which obeys the same lifecycle management as the | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would definitely appreciate lifecycle timer contribution |
||
| // lifecycle publisher. | ||
| rclcpp::TimerBase::SharedPtr timer_; | ||
| }; | ||
|
|
||
| } // namespace composition | ||
|
|
||
| #endif // COMPOSITION__LIFECYCLE_TALKER_COMPONENT_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,107 @@ | ||
| // Copyright 2025 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 "composition/lifecycle_talker_component.hpp" | ||
|
|
||
| #include <chrono> | ||
| #include <iostream> | ||
| #include <memory> | ||
| #include <utility> | ||
|
|
||
| #include "rclcpp/rclcpp.hpp" | ||
| #include "example_interfaces/msg/string.hpp" | ||
|
|
||
| using namespace std::chrono_literals; | ||
|
|
||
| namespace composition | ||
| { | ||
|
|
||
| LifecycleTalker::LifecycleTalker(const rclcpp::NodeOptions & options) | ||
| : LifecycleNode("lifecycle_talker", options), count_(0) | ||
| { | ||
| } | ||
|
|
||
| /// Callback for walltimer in order to publish the message. | ||
| /** | ||
| * Callback for walltimer. This function gets invoked by the timer | ||
| * and executes the publishing. | ||
| * For this demo, we ask the node for its current state. If the | ||
| * lifecycle publisher is not activate, we still invoke publish, but | ||
| * the communication is blocked so that no messages is actually transferred. | ||
| */ | ||
| void LifecycleTalker::publish() | ||
| { | ||
| auto msg = std::make_unique<example_interfaces::msg::String>(); | ||
| msg->data = "Lifecycle Hello World: " + std::to_string(++count_); | ||
|
|
||
| // Print the current state for demo purposes | ||
| RCLCPP_INFO_EXPRESSION(this->get_logger(), pub_->is_activated(), | ||
| "Lifecycle Publisher is active. Publishing: '%s'", msg->data.c_str()); | ||
| RCLCPP_INFO_EXPRESSION(this->get_logger(), !pub_->is_activated(), | ||
| "Lifecycle Publisher is inactive. Not publishing: '%s'", msg->data.c_str()); | ||
| std::flush(std::cout); | ||
|
|
||
| // We independently from the current state call publish on the lifecycle publisher. | ||
| // Only if the publisher is in an active state, the message transfer is | ||
| // enabled and the message actually published. | ||
| // Put the message into a queue to be processed by the middleware. | ||
| // This call is non-blocking. | ||
| pub_->publish(std::move(msg)); | ||
| } | ||
|
|
||
| rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn | ||
| LifecycleTalker::on_configure(const rclcpp_lifecycle::State &) | ||
| { | ||
| pub_ = create_publisher<example_interfaces::msg::String>("lc_chatter", 10); | ||
| timer_ = create_wall_timer(1s, [this](){return this->publish();}); | ||
|
|
||
| RCLCPP_INFO(get_logger(), "on_configure() is called."); | ||
|
|
||
| return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS; | ||
| } | ||
|
|
||
| rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn | ||
| LifecycleTalker::on_cleanup(const rclcpp_lifecycle::State &) | ||
| { | ||
| // In our cleanup phase, we release the shared pointers to the timer and publisher. | ||
| // These entities are no longer available and our node is "clean". | ||
| timer_.reset(); | ||
| pub_.reset(); | ||
|
|
||
| RCLCPP_INFO(get_logger(), "on_cleanup() is called."); | ||
|
|
||
| return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS; | ||
| } | ||
|
|
||
| rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn | ||
| LifecycleTalker::on_shutdown(const rclcpp_lifecycle::State & state) | ||
| { | ||
| // In our cleanup phase, we release the shared pointers to the timer and publisher. | ||
| // These entities are no longer available and our node is "clean". | ||
| timer_.reset(); | ||
| pub_.reset(); | ||
|
|
||
| RCLCPP_INFO(get_logger(), "on_shutdown() is called from state %s.", state.label().c_str()); | ||
|
|
||
| return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS; | ||
| } | ||
|
|
||
| } // namespace composition | ||
|
|
||
| #include "rclcpp_components/register_node_macro.hpp" | ||
|
|
||
| // Register the component with class_loader. | ||
| // This acts as a sort of entry point, allowing the component to be discoverable when its library | ||
| // is being loaded into a running process. | ||
| RCLCPP_COMPONENTS_REGISTER_NODE(composition::LifecycleTalker) |
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,69 @@ | ||
| # Copyright 2025 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. | ||
|
|
||
| import unittest | ||
|
|
||
| from launch import LaunchDescription | ||
| from launch.actions import ExecuteProcess | ||
|
|
||
| import launch_testing | ||
| import launch_testing.actions | ||
| import launch_testing.asserts | ||
| import launch_testing_ros | ||
|
|
||
|
|
||
| def generate_test_description(): | ||
| launch_description = LaunchDescription() | ||
| process_under_test = ExecuteProcess( | ||
| cmd=['@API_COMPOSITION_EXECUTABLE@'], | ||
| name='test_api_composition', | ||
| output='screen' | ||
| ) | ||
| launch_description.add_action(process_under_test) | ||
| launch_description.add_action( | ||
| ExecuteProcess( | ||
| cmd=[ | ||
| '@API_COMPOSITION_CLI_EXECUTABLE@', | ||
| 'composition', 'composition::LifecycleTalker' | ||
| ], | ||
| name='load_talker_component' | ||
| ) | ||
| ) | ||
| launch_description.add_action( | ||
| ExecuteProcess( | ||
| cmd=[ | ||
| '@API_COMPOSITION_CLI_EXECUTABLE@', | ||
| 'composition', 'composition::Listener' | ||
| ], | ||
| name='load_listener_component' | ||
| ) | ||
| ) | ||
| launch_description.add_action( | ||
| launch_testing.actions.ReadyToTest() | ||
| ) | ||
| return launch_description, locals() | ||
|
|
||
|
|
||
| class TestComposition(unittest.TestCase): | ||
|
|
||
| def test_api_pubsub_composition(self, proc_output, process_under_test): | ||
| """Test process' output against expectations.""" | ||
| output_filter = launch_testing_ros.tools.basic_output_filter( | ||
| filtered_rmw_implementation='@rmw_implementation@' | ||
| ) | ||
| proc_output.assertWaitFor( | ||
| expected_output=launch_testing.tools.expected_output_from_file( | ||
| path='@EXPECTED_OUTPUT_PUBSUB@' | ||
| ), process=process_under_test, output_filter=output_filter, timeout=10 | ||
| ) |
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
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.
Should we exercise the other transitions
on_activate, etc?Uh oh!
There was an error while loading. Please reload this page.
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.
I'm unsure if I understand correctly, but do you mean 'Should we add the other transitions?'.
I haven't added them right now, as I intended this to be a minimal example for creating lifecycle components.
It isn't a full copy of
lifecycle'sLifecycleTalkeras I wanted to keep this simple for testing (for example in ros2/launch_ros#479), because currently there is no suitable lifecycle component to test with.I'm open to adding these extra transitions, but we should probably consider the 'difficulty order' or complexity of both lifecycle nodes and node components. (Thinking about 'the (fictitious) ROS 2 textbook chapter ordering'.)
For example, I would imagine most users (or at least beginners) would first learn about lifecycle nodes before node components.
This would make it safe to assume they are familiar with lifecycle nodes before looking at this, which would greatly limit the benefit of these nearly empty transitions while introducing more 'clutter'.
(Having written this, I'm second-guessing the ordering of my ROS 2 journey.)