-
Notifications
You must be signed in to change notification settings - Fork 429
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
Add LifecycleSubscription #2715
Open
Amronos
wants to merge
1
commit into
ros2:rolling
Choose a base branch
from
Amronos:lifecycle-subscription
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.
+275
−16
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 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
90 changes: 90 additions & 0 deletions
90
rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_subscription.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2024 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_SUBSCRIPTION_HPP_ | ||
#define RCLCPP_LIFECYCLE__LIFECYCLE_SUBSCRIPTION_HPP_ | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include "rclcpp/node_interfaces/node_base_interface.hpp" | ||
#include "rclcpp/subscription.hpp" | ||
#include "rclcpp/subscription_options.hpp" | ||
#include "rclcpp/qos.hpp" | ||
|
||
#include "rclcpp_lifecycle/managed_entity.hpp" | ||
|
||
|
||
namespace rclcpp_lifecycle | ||
{ | ||
|
||
/// @brief Child class of rclcpp::Subscription that adds lifecycle functionality. | ||
/** | ||
* This class is a child class of rclcpp::Subscription that adds a lifecycle | ||
* check to the callback function. If the node is in an inactive state, the | ||
* callback will not be called. | ||
*/ | ||
template< | ||
typename MessageT, | ||
typename AllocatorT = std::allocator<void>, | ||
typename SubscribedT = typename rclcpp::TypeAdapter<MessageT>::custom_type, | ||
typename ROSMessageT = typename rclcpp::TypeAdapter<MessageT>::ros_message_type, | ||
typename MessageMemoryStrategyT = rclcpp::message_memory_strategy::MessageMemoryStrategy< | ||
ROSMessageT, | ||
AllocatorT | ||
>> | ||
class LifecycleSubscription : public SimpleManagedEntity, | ||
public rclcpp::Subscription<MessageT, AllocatorT> | ||
{ | ||
private: | ||
using SubscriptionTopicStatisticsSharedPtr = | ||
std::shared_ptr<rclcpp::topic_statistics::SubscriptionTopicStatistics>; | ||
|
||
public: | ||
LifecycleSubscription( | ||
rclcpp::node_interfaces::NodeBaseInterface * node_base, | ||
const rosidl_message_type_support_t & type_support_handle, | ||
const std::string & topic_name, | ||
const rclcpp::QoS & qos, | ||
rclcpp::AnySubscriptionCallback<MessageT, AllocatorT> callback, | ||
const rclcpp::SubscriptionOptionsWithAllocator<AllocatorT> & options, | ||
typename MessageMemoryStrategyT::SharedPtr message_memory_strategy, | ||
SubscriptionTopicStatisticsSharedPtr subscription_topic_statistics = nullptr) | ||
: rclcpp::Subscription<MessageT>( | ||
node_base, | ||
type_support_handle, | ||
topic_name, | ||
qos, | ||
callback, | ||
options, | ||
message_memory_strategy, | ||
subscription_topic_statistics) | ||
{ | ||
} | ||
|
||
/// TODO: Hold onto the data that arrives before activation, and deliver that on activation. | ||
/// Check if we need to handle the message, and execute the callback if we do. | ||
void handle_message( | ||
std::shared_ptr<void> & message, const rclcpp::MessageInfo & message_info) override | ||
{ | ||
if (!this->is_activated()) { | ||
return; | ||
} | ||
rclcpp::Subscription<MessageT, AllocatorT>::handle_message(message, message_info); | ||
} | ||
}; | ||
|
||
} // namespace rclcpp_lifecycle | ||
|
||
#endif // RCLCPP_LIFECYCLE__LIFECYCLE_SUBSCRIPTION_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
// Copyright 2024 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 <memory> | ||
#include <string> | ||
|
||
#include "lifecycle_msgs/msg/state.hpp" | ||
#include "lifecycle_msgs/msg/transition.hpp" | ||
|
||
#include "test_msgs/msg/empty.hpp" | ||
|
||
#include "rclcpp_lifecycle/lifecycle_node.hpp" | ||
#include "rclcpp_lifecycle/lifecycle_subscription.hpp" | ||
|
||
using lifecycle_msgs::msg::State; | ||
using lifecycle_msgs::msg::Transition; | ||
|
||
class TestDefaultStateMachine : public ::testing::Test | ||
{ | ||
protected: | ||
static void SetUpTestCase() | ||
{ | ||
rclcpp::init(0, nullptr); | ||
} | ||
static void TearDownTestCase() | ||
{ | ||
rclcpp::shutdown(); | ||
} | ||
}; | ||
|
||
/// We want to test everything for both the wall and generic timer. | ||
enum class TimerType | ||
{ | ||
WALL_TIMER, | ||
GENERIC_TIMER, | ||
}; | ||
|
||
class EmptyLifecycleNode : public rclcpp_lifecycle::LifecycleNode | ||
{ | ||
public: | ||
explicit EmptyLifecycleNode(const std::string & node_name, const TimerType & timer_type) | ||
: rclcpp_lifecycle::LifecycleNode(node_name) | ||
{ | ||
// For coverage this is being added here | ||
switch (timer_type) { | ||
case TimerType::WALL_TIMER: | ||
{ | ||
auto timer = create_wall_timer(std::chrono::seconds(1), []() {}); | ||
add_timer_handle(timer); | ||
break; | ||
} | ||
case TimerType::GENERIC_TIMER: | ||
{ | ||
auto timer = create_timer(std::chrono::seconds(1), []() {}); | ||
add_timer_handle(timer); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
void empty_callback(const test_msgs::msg::Empty msg) {} | ||
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. i think one of the most important test case is missing here.
|
||
}; | ||
|
||
class TestLifecycleSubscription : public ::testing::TestWithParam<TimerType> | ||
{ | ||
public: | ||
void SetUp() | ||
{ | ||
rclcpp::init(0, nullptr); | ||
} | ||
|
||
void TearDown() | ||
{ | ||
rclcpp::shutdown(); | ||
} | ||
}; | ||
|
||
TEST_P(TestLifecycleSubscription, subscribe_managed_by_node) { | ||
auto node = std::make_shared<EmptyLifecycleNode>("node", GetParam()); | ||
|
||
std::shared_ptr<rclcpp_lifecycle::LifecycleSubscription<test_msgs::msg::Empty>> subscription = | ||
node->create_subscription<test_msgs::msg::Empty>(std::string("topic"), rclcpp::QoS(10), | ||
bind(&EmptyLifecycleNode::empty_callback, node, std::placeholders::_1)); | ||
|
||
// transition via LifecycleNode | ||
auto success = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS; | ||
auto reset_key = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::ERROR; | ||
auto ret = reset_key; | ||
|
||
EXPECT_EQ(State::PRIMARY_STATE_UNCONFIGURED, node->get_current_state().id()); | ||
node->trigger_transition( | ||
rclcpp_lifecycle::Transition(Transition::TRANSITION_CONFIGURE), ret); | ||
ASSERT_EQ(success, ret); | ||
ret = reset_key; | ||
node->trigger_transition( | ||
rclcpp_lifecycle::Transition(Transition::TRANSITION_ACTIVATE), ret); | ||
ASSERT_EQ(success, ret); | ||
ret = reset_key; | ||
EXPECT_TRUE(subscription->is_activated()); | ||
{ | ||
std::shared_ptr<void> msg_ptr(new test_msgs::msg::Empty()); | ||
EXPECT_NO_THROW(subscription->handle_message(msg_ptr, rclcpp::MessageInfo())); | ||
} | ||
node->trigger_transition( | ||
rclcpp_lifecycle::Transition(Transition::TRANSITION_DEACTIVATE), ret); | ||
ASSERT_EQ(success, ret); | ||
ret = reset_key; | ||
(void)ret; // Just to make clang happy | ||
EXPECT_FALSE(subscription->is_activated()); | ||
{ | ||
std::shared_ptr<void> msg_ptr(new test_msgs::msg::Empty()); | ||
EXPECT_NO_THROW(subscription->handle_message(msg_ptr, rclcpp::MessageInfo())); | ||
} | ||
} | ||
|
||
TEST_P(TestLifecycleSubscription, subscribe) { | ||
auto node = std::make_shared<EmptyLifecycleNode>("node", GetParam()); | ||
|
||
std::shared_ptr<rclcpp_lifecycle::LifecycleSubscription<test_msgs::msg::Empty>> subscription = | ||
node->create_subscription<test_msgs::msg::Empty>(std::string("topic"), rclcpp::QoS(10), | ||
bind(&EmptyLifecycleNode::empty_callback, node, std::placeholders::_1)); | ||
|
||
// transition via LifecycleSubscription | ||
subscription->on_deactivate(); | ||
EXPECT_FALSE(subscription->is_activated()); | ||
{ | ||
std::shared_ptr<void> msg_ptr(new test_msgs::msg::Empty()); | ||
EXPECT_NO_THROW(subscription->handle_message(msg_ptr, rclcpp::MessageInfo())); | ||
} | ||
|
||
subscription->on_activate(); | ||
EXPECT_TRUE(subscription->is_activated()); | ||
{ | ||
std::shared_ptr<void> msg_ptr(new test_msgs::msg::Empty()); | ||
EXPECT_NO_THROW(subscription->handle_message(msg_ptr, rclcpp::MessageInfo())); | ||
} | ||
} | ||
|
||
INSTANTIATE_TEST_SUITE_P( | ||
PerTimerType, TestLifecycleSubscription, | ||
::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"); | ||
} | ||
); |
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.
besides this, i think subscription should not be discovered in the graph if that is in
unconfigure
state. if that comes toinactive
state (means it is configured), subscription will be discovered in the ROS 2 network. and then hold onto the data in the rmw message queue not to take them out and dispose as described here.for doing that, probably we need to have state control (configured, inactive) in SubscriberBase class and underlying implementations, and then
on_configure
callback, we can configure the subscription to be discovered. (on_activate
, it can take the data out of the queue.) i am not sure how exactly we want to design this at this moment, but we would want to add some comments here too. (this feature is similar withLazySubscription
concept.)note that this requirement should also go to
LifecyclePublisher
as well.what do you think?