Skip to content

Commit 91337dc

Browse files
committed
erge remote-tracking branch 'origin/rolling' into asoragna/events-executor
2 parents 6811dfa + e3d9d81 commit 91337dc

7 files changed

Lines changed: 72 additions & 39 deletions

File tree

rclcpp/include/rclcpp/generic_subscription.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class GenericSubscription : public rclcpp::SubscriptionBase
8484
options.to_rcl_subscription_options(qos),
8585
options.event_callbacks,
8686
options.use_default_callbacks,
87-
SubscriptionType::SERIALIZED_MESSAGE),
87+
DeliveredMessageKind::SERIALIZED_MESSAGE),
8888
callback_(callback),
8989
ts_lib_(ts_lib)
9090
{}

rclcpp/include/rclcpp/subscription.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class Subscription : public SubscriptionBase
144144
// NOTE(methylDragon): Passing these args separately is necessary for event binding
145145
options.event_callbacks,
146146
options.use_default_callbacks,
147-
callback.is_serialized_message_callback() ? SubscriptionType::SERIALIZED_MESSAGE : SubscriptionType::ROS_MESSAGE), // NOLINT
147+
callback.is_serialized_message_callback() ? DeliveredMessageKind::SERIALIZED_MESSAGE : DeliveredMessageKind::ROS_MESSAGE), // NOLINT
148148
any_callback_(callback),
149149
options_(options),
150150
message_memory_strategy_(message_memory_strategy)

rclcpp/include/rclcpp/subscription_base.hpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,25 @@ namespace experimental
6363
class IntraProcessManager;
6464
} // namespace experimental
6565

66-
enum class SubscriptionType : uint8_t
66+
/// The kind of message that the subscription delivers in its callback, used by the executor
67+
/**
68+
* This enum needs to exist because the callback handle is not accessible to the executor's scope.
69+
*
70+
* "Kind" is used since what is being delivered is a category of messages, for example, there are
71+
* different ROS message types that can be delivered, but they're all ROS messages.
72+
*
73+
* As a concrete example, all of the following callbacks will be considered ROS_MESSAGE for
74+
* DeliveredMessageKind:
75+
* - void callback(const std_msgs::msg::String &)
76+
* - void callback(const std::string &) // type adaption
77+
* - void callback(std::unique_ptr<std_msgs::msg::String>)
78+
*/
79+
enum class DeliveredMessageKind : uint8_t
6780
{
68-
INVALID = 0, // The subscription type is most likely uninitialized
69-
ROS_MESSAGE = 1, // take message as ROS message and handle as ROS message
70-
SERIALIZED_MESSAGE = 2, // take message as serialized and handle as serialized
71-
DYNAMIC_MESSAGE_DIRECT = 3, // take message as DynamicMessage and handle as DynamicMessage
72-
DYNAMIC_MESSAGE_FROM_SERIALIZED = 4 // take message as serialized and handle as DynamicMessage
81+
INVALID = 0,
82+
ROS_MESSAGE = 1, // The subscription delivers a ROS message to its callback
83+
SERIALIZED_MESSAGE = 2, // The subscription delivers a serialized message to its callback
84+
DYNAMIC_MESSAGE = 3, // The subscription delivers a dynamic message to its callback
7385
};
7486

7587
/// Virtual base class for subscriptions. This pattern allows us to iterate over different template
@@ -88,7 +100,8 @@ class SubscriptionBase : public std::enable_shared_from_this<SubscriptionBase>
88100
* \param[in] type_support_handle rosidl type support struct, for the Message type of the topic.
89101
* \param[in] topic_name Name of the topic to subscribe to.
90102
* \param[in] subscription_options Options for the subscription.
91-
* \param[in] subscription_type Enum flag to change how the message will be received and delivered
103+
* \param[in] delivered_message_kind Enum flag to change how the message will be received and
104+
* delivered
92105
*/
93106
RCLCPP_PUBLIC
94107
SubscriptionBase(
@@ -98,7 +111,7 @@ class SubscriptionBase : public std::enable_shared_from_this<SubscriptionBase>
98111
const rcl_subscription_options_t & subscription_options,
99112
const SubscriptionEventCallbacks & event_callbacks,
100113
bool use_default_callbacks,
101-
SubscriptionType subscription_type = SubscriptionType::ROS_MESSAGE);
114+
DeliveredMessageKind delivered_message_kind = DeliveredMessageKind::ROS_MESSAGE);
102115

103116
/// Destructor.
104117
RCLCPP_PUBLIC
@@ -249,10 +262,10 @@ class SubscriptionBase : public std::enable_shared_from_this<SubscriptionBase>
249262

250263
/// Return the type of the subscription.
251264
/**
252-
* \return `SubscriptionType`, which adjusts how messages are received and delivered.
265+
* \return `DeliveredMessageKind`, which adjusts how messages are received and delivered.
253266
*/
254267
RCLCPP_PUBLIC
255-
SubscriptionType
268+
DeliveredMessageKind
256269
get_subscription_type() const;
257270

258271
/// Get matching publisher count.
@@ -650,7 +663,7 @@ class SubscriptionBase : public std::enable_shared_from_this<SubscriptionBase>
650663
RCLCPP_DISABLE_COPY(SubscriptionBase)
651664

652665
rosidl_message_type_support_t type_support_;
653-
SubscriptionType subscription_type_;
666+
DeliveredMessageKind delivered_message_type_;
654667

655668
std::atomic<bool> subscription_in_use_by_wait_set_{false};
656669
std::atomic<bool> intra_process_subscription_waitable_in_use_by_wait_set_{false};

rclcpp/src/rclcpp/executor.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,8 @@ Executor::execute_subscription(rclcpp::SubscriptionBase::SharedPtr subscription)
601601
message_info.get_rmw_message_info().from_intra_process = false;
602602

603603
switch (subscription->get_subscription_type()) {
604-
// Take ROS message
605-
case rclcpp::SubscriptionType::ROS_MESSAGE:
604+
// Deliver ROS message
605+
case rclcpp::DeliveredMessageKind::ROS_MESSAGE:
606606
{
607607
if (subscription->can_loan_messages()) {
608608
// This is the case where a loaned message is taken from the middleware via
@@ -655,8 +655,8 @@ Executor::execute_subscription(rclcpp::SubscriptionBase::SharedPtr subscription)
655655
break;
656656
}
657657

658-
// Take serialized message
659-
case rclcpp::SubscriptionType::SERIALIZED_MESSAGE:
658+
// Deliver serialized message
659+
case rclcpp::DeliveredMessageKind::SERIALIZED_MESSAGE:
660660
{
661661
// This is the case where a copy of the serialized message is taken from
662662
// the middleware via inter-process communication.
@@ -675,21 +675,15 @@ Executor::execute_subscription(rclcpp::SubscriptionBase::SharedPtr subscription)
675675
}
676676

677677
// DYNAMIC SUBSCRIPTION ========================================================================
678-
// Take dynamic message directly from the middleware
679-
case rclcpp::SubscriptionType::DYNAMIC_MESSAGE_DIRECT:
680-
{
681-
throw std::runtime_error("Unimplemented");
682-
}
683-
684-
// Take serialized and then convert to dynamic message
685-
case rclcpp::SubscriptionType::DYNAMIC_MESSAGE_FROM_SERIALIZED:
678+
// Deliver dynamic message
679+
case rclcpp::DeliveredMessageKind::DYNAMIC_MESSAGE:
686680
{
687681
throw std::runtime_error("Unimplemented");
688682
}
689683

690684
default:
691685
{
692-
throw std::runtime_error("Subscription type is not supported");
686+
throw std::runtime_error("Delivered message kind is not supported");
693687
}
694688
}
695689
return;

rclcpp/src/rclcpp/subscription_base.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,16 @@ SubscriptionBase::SubscriptionBase(
4444
const rcl_subscription_options_t & subscription_options,
4545
const SubscriptionEventCallbacks & event_callbacks,
4646
bool use_default_callbacks,
47-
SubscriptionType subscription_type)
47+
DeliveredMessageKind delivered_message_kind)
4848
: node_base_(node_base),
4949
node_handle_(node_base_->get_shared_rcl_node_handle()),
5050
node_logger_(rclcpp::get_node_logger(node_handle_.get())),
5151
use_intra_process_(false),
5252
intra_process_subscription_id_(0),
5353
event_callbacks_(event_callbacks),
5454
type_support_(type_support_handle),
55-
subscription_type_(subscription_type)
55+
delivered_message_type_(delivered_message_kind)
5656
{
57-
if (!rmw_feature_supported(RMW_MIDDLEWARE_CAN_TAKE_DYNAMIC_MESSAGE) &&
58-
subscription_type == rclcpp::SubscriptionType::DYNAMIC_MESSAGE_DIRECT)
59-
{
60-
throw std::runtime_error(
61-
"Cannot set subscription to take dynamic message directly, feature not supported in rmw"
62-
);
63-
}
64-
6557
auto custom_deletor = [node_handle = this->node_handle_](rcl_subscription_t * rcl_subs)
6658
{
6759
if (rcl_subscription_fini(rcl_subs, node_handle.get()) != RCL_RET_OK) {
@@ -269,13 +261,13 @@ SubscriptionBase::get_message_type_support_handle() const
269261
bool
270262
SubscriptionBase::is_serialized() const
271263
{
272-
return subscription_type_ == rclcpp::SubscriptionType::SERIALIZED_MESSAGE;
264+
return delivered_message_type_ == rclcpp::DeliveredMessageKind::SERIALIZED_MESSAGE;
273265
}
274266

275-
rclcpp::SubscriptionType
267+
rclcpp::DeliveredMessageKind
276268
SubscriptionBase::get_subscription_type() const
277269
{
278-
return subscription_type_;
270+
return delivered_message_type_;
279271
}
280272

281273
size_t

rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_publisher.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ class LifecyclePublisher : public SimpleManagedEntity,
9191
rclcpp::Publisher<MessageT, Alloc>::publish(msg);
9292
}
9393

94+
/// LifecyclePublisher publish function
95+
/**
96+
* The publish function checks whether the communication
97+
* was enabled or disabled and forwards the message
98+
* to the actual rclcpp Publisher base class
99+
*/
100+
virtual void
101+
publish(
102+
rclcpp::LoanedMessage<typename rclcpp::Publisher<MessageT,
103+
Alloc>::ROSMessageType, Alloc> && loaned_msg)
104+
{
105+
if (!this->is_activated()) {
106+
log_publisher_not_enabled();
107+
return;
108+
}
109+
rclcpp::Publisher<MessageT, Alloc>::publish(std::move(loaned_msg));
110+
}
111+
94112
void
95113
on_activate() override
96114
{

rclcpp_lifecycle/test/test_lifecycle_publisher.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ TEST_P(TestLifecyclePublisher, publish_managed_by_node) {
129129
auto msg_ptr = std::make_unique<test_msgs::msg::Empty>();
130130
EXPECT_NO_THROW(node_->publisher()->publish(std::move(msg_ptr)));
131131
}
132+
{
133+
auto loaned_msg = node_->publisher()->borrow_loaned_message();
134+
EXPECT_NO_THROW(node_->publisher()->publish(std::move(loaned_msg)));
135+
}
132136
node_->trigger_transition(
133137
rclcpp_lifecycle::Transition(Transition::TRANSITION_DEACTIVATE), ret);
134138
ASSERT_EQ(success, ret);
@@ -143,6 +147,10 @@ TEST_P(TestLifecyclePublisher, publish_managed_by_node) {
143147
auto msg_ptr = std::make_unique<test_msgs::msg::Empty>();
144148
EXPECT_NO_THROW(node_->publisher()->publish(std::move(msg_ptr)));
145149
}
150+
{
151+
auto loaned_msg = node_->publisher()->borrow_loaned_message();
152+
EXPECT_NO_THROW(node_->publisher()->publish(std::move(loaned_msg)));
153+
}
146154
}
147155

148156
TEST_P(TestLifecyclePublisher, publish) {
@@ -157,6 +165,10 @@ TEST_P(TestLifecyclePublisher, publish) {
157165
auto msg_ptr = std::make_unique<test_msgs::msg::Empty>();
158166
EXPECT_NO_THROW(node_->publisher()->publish(std::move(msg_ptr)));
159167
}
168+
{
169+
auto loaned_msg = node_->publisher()->borrow_loaned_message();
170+
EXPECT_NO_THROW(node_->publisher()->publish(std::move(loaned_msg)));
171+
}
160172
node_->publisher()->on_activate();
161173
EXPECT_TRUE(node_->publisher()->is_activated());
162174
{
@@ -167,6 +179,10 @@ TEST_P(TestLifecyclePublisher, publish) {
167179
auto msg_ptr = std::make_unique<test_msgs::msg::Empty>();
168180
EXPECT_NO_THROW(node_->publisher()->publish(std::move(msg_ptr)));
169181
}
182+
{
183+
auto loaned_msg = node_->publisher()->borrow_loaned_message();
184+
EXPECT_NO_THROW(node_->publisher()->publish(std::move(loaned_msg)));
185+
}
170186
}
171187

172188
INSTANTIATE_TEST_SUITE_P(

0 commit comments

Comments
 (0)