Skip to content

Commit 6c06a29

Browse files
alsorawjwwood
andauthored
add take_data_by_entity_id API to waitable (#1892)
* add take_data_by_entity_id API to waitable Signed-off-by: Alberto Soragna <alberto.soragna@gmail.com> * use size_t for entity id Signed-off-by: William Woodall <william@osrfoundation.org> Co-authored-by: William Woodall <william@osrfoundation.org>
1 parent 03fa731 commit 6c06a29

8 files changed

Lines changed: 99 additions & 4 deletions

File tree

rclcpp/include/rclcpp/experimental/subscription_intra_process_base.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class SubscriptionIntraProcessBase : public rclcpp::Waitable
3838
public:
3939
RCLCPP_SMART_PTR_ALIASES_ONLY(SubscriptionIntraProcessBase)
4040

41-
enum class EntityType
41+
enum class EntityType : std::size_t
4242
{
4343
Subscription,
4444
};
@@ -68,6 +68,13 @@ class SubscriptionIntraProcessBase : public rclcpp::Waitable
6868
std::shared_ptr<void>
6969
take_data() override = 0;
7070

71+
std::shared_ptr<void>
72+
take_data_by_entity_id(size_t id) override
73+
{
74+
(void)id;
75+
return take_data();
76+
}
77+
7178
void
7279
execute(std::shared_ptr<void> & data) override = 0;
7380

rclcpp/include/rclcpp/qos_event.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class UnsupportedEventTypeException : public exceptions::RCLErrorBase, public st
8989
class QOSEventHandlerBase : public Waitable
9090
{
9191
public:
92-
enum class EntityType
92+
enum class EntityType : std::size_t
9393
{
9494
Event,
9595
};
@@ -259,6 +259,13 @@ class QOSEventHandler : public QOSEventHandlerBase
259259
return std::static_pointer_cast<void>(std::make_shared<EventCallbackInfoT>(callback_info));
260260
}
261261

262+
std::shared_ptr<void>
263+
take_data_by_entity_id(size_t id) override
264+
{
265+
(void)id;
266+
return take_data();
267+
}
268+
262269
/// Execute any entities of the Waitable that are ready.
263270
void
264271
execute(std::shared_ptr<void> & data) override

rclcpp/include/rclcpp/waitable.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,24 @@ class Waitable
160160
std::shared_ptr<void>
161161
take_data() = 0;
162162

163+
/// Take the data so that it can be consumed with `execute`.
164+
/**
165+
* This function allows to specify an entity ID to take the data from.
166+
* Entity IDs are identifiers that can be defined by waitable-derived
167+
* classes that are composed of several distinct entities.
168+
* The main use-case is in conjunction with the listener APIs.
169+
*
170+
* \param[in] id the id of the entity from which to take
171+
* \returns the type-erased data taken from entity specified
172+
*
173+
* \sa rclcpp::Waitable::take_data
174+
* \sa rclcpp::Waitable::set_on_ready_callback
175+
*/
176+
RCLCPP_PUBLIC
177+
virtual
178+
std::shared_ptr<void>
179+
take_data_by_entity_id(size_t id);
180+
163181
/// Execute data that is passed in.
164182
/**
165183
* Before calling this method, the Waitable should be added to a wait set,

rclcpp/src/rclcpp/waitable.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ Waitable::get_number_of_ready_guard_conditions()
5454
return 0u;
5555
}
5656

57+
std::shared_ptr<void>
58+
Waitable::take_data_by_entity_id(size_t id)
59+
{
60+
(void)id;
61+
throw std::runtime_error(
62+
"Custom waitables should override take_data_by_entity_id "
63+
"if they want to use it.");
64+
}
65+
5766
bool
5867
Waitable::exchange_in_use_by_wait_set_state(bool in_use_state)
5968
{

rclcpp_action/include/rclcpp_action/client.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class ClientBase : public rclcpp::Waitable
6666
virtual ~ClientBase();
6767

6868
/// Enum to identify entities belonging to the action client
69-
enum class EntityType
69+
enum class EntityType : std::size_t
7070
{
7171
GoalClient,
7272
ResultClient,
@@ -134,6 +134,11 @@ class ClientBase : public rclcpp::Waitable
134134
std::shared_ptr<void>
135135
take_data() override;
136136

137+
/// \internal
138+
RCLCPP_ACTION_PUBLIC
139+
std::shared_ptr<void>
140+
take_data_by_entity_id(size_t id) override;
141+
137142
/// \internal
138143
RCLCPP_ACTION_PUBLIC
139144
void

rclcpp_action/include/rclcpp_action/server.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class ServerBase : public rclcpp::Waitable
7272
{
7373
public:
7474
/// Enum to identify entities belonging to the action server
75-
enum class EntityType
75+
enum class EntityType : std::size_t
7676
{
7777
GoalService,
7878
ResultService,
@@ -131,6 +131,10 @@ class ServerBase : public rclcpp::Waitable
131131
std::shared_ptr<void>
132132
take_data() override;
133133

134+
RCLCPP_ACTION_PUBLIC
135+
std::shared_ptr<void>
136+
take_data_by_entity_id(size_t id) override;
137+
134138
/// Act on entities in the wait set which are ready to be acted upon.
135139
/// \internal
136140
RCLCPP_ACTION_PUBLIC

rclcpp_action/src/client.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,31 @@ ClientBase::take_data()
589589
}
590590
}
591591

592+
std::shared_ptr<void>
593+
ClientBase::take_data_by_entity_id(size_t id)
594+
{
595+
// Mark as ready the entity from which we want to take data
596+
switch (static_cast<EntityType>(id)) {
597+
case EntityType::GoalClient:
598+
pimpl_->is_goal_response_ready = true;
599+
break;
600+
case EntityType::ResultClient:
601+
pimpl_->is_result_response_ready = true;
602+
break;
603+
case EntityType::CancelClient:
604+
pimpl_->is_cancel_response_ready = true;
605+
break;
606+
case EntityType::FeedbackSubscription:
607+
pimpl_->is_feedback_ready = true;
608+
break;
609+
case EntityType::StatusSubscription:
610+
pimpl_->is_status_ready = true;
611+
break;
612+
}
613+
614+
return take_data();
615+
}
616+
592617
void
593618
ClientBase::execute(std::shared_ptr<void> & data)
594619
{

rclcpp_action/src/server.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,25 @@ ServerBase::take_data()
268268
}
269269
}
270270

271+
std::shared_ptr<void>
272+
ServerBase::take_data_by_entity_id(size_t id)
273+
{
274+
// Mark as ready the entity from which we want to take data
275+
switch (static_cast<EntityType>(id)) {
276+
case EntityType::GoalService:
277+
pimpl_->goal_request_ready_ = true;
278+
break;
279+
case EntityType::ResultService:
280+
pimpl_->result_request_ready_ = true;
281+
break;
282+
case EntityType::CancelService:
283+
pimpl_->cancel_request_ready_ = true;
284+
break;
285+
}
286+
287+
return take_data();
288+
}
289+
271290
void
272291
ServerBase::execute(std::shared_ptr<void> & data)
273292
{
@@ -398,6 +417,7 @@ ServerBase::execute_cancel_request_received(std::shared_ptr<void> & data)
398417
}
399418
auto request = std::get<1>(*shared_ptr);
400419
auto request_header = std::get<2>(*shared_ptr);
420+
pimpl_->cancel_request_ready_ = false;
401421

402422
// Convert c++ message to C message
403423
rcl_action_cancel_request_t cancel_request = rcl_action_get_zero_initialized_cancel_request();

0 commit comments

Comments
 (0)