forked from autowarefoundation/autoware.universe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tier4_autoware_utils, obstacle_cruise): change to read topic by …
…polling (autowarefoundation#6702) change to read topic by polling Signed-off-by: Yuki Takagi <[email protected]>
- Loading branch information
1 parent
d4ec8d3
commit c0fcd75
Showing
3 changed files
with
93 additions
and
32 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
common/tier4_autoware_utils/include/tier4_autoware_utils/ros/polling_subscriber.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,65 @@ | ||
// Copyright 2024 TIER IV, 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 TIER4_AUTOWARE_UTILS__ROS__POLLING_SUBSCRIBER_HPP_ | ||
#define TIER4_AUTOWARE_UTILS__ROS__POLLING_SUBSCRIBER_HPP_ | ||
|
||
#include <rclcpp/rclcpp.hpp> | ||
|
||
#include <string> | ||
|
||
namespace tier4_autoware_utils | ||
{ | ||
|
||
template <typename T> | ||
class InterProcessPollingSubscriber | ||
{ | ||
private: | ||
typename rclcpp::Subscription<T>::SharedPtr subscriber_; | ||
std::optional<T> data_; | ||
|
||
public: | ||
explicit InterProcessPollingSubscriber(rclcpp::Node * node, const std::string & topic_name) | ||
{ | ||
auto noexec_callback_group = | ||
node->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive, false); | ||
auto noexec_subscription_options = rclcpp::SubscriptionOptions(); | ||
noexec_subscription_options.callback_group = noexec_callback_group; | ||
|
||
subscriber_ = node->create_subscription<T>( | ||
topic_name, rclcpp::QoS{1}, [node](const typename T::ConstSharedPtr msg) { assert(false); }, | ||
noexec_subscription_options); | ||
}; | ||
bool updateLatestData() | ||
{ | ||
rclcpp::MessageInfo message_info; | ||
T tmp; | ||
// The queue size (QoS) must be 1 to get the last message data. | ||
if (subscriber_->take(tmp, message_info)) { | ||
data_ = tmp; | ||
} | ||
return data_.has_value(); | ||
}; | ||
const T & getData() const | ||
{ | ||
if (!data_.has_value()) { | ||
throw std::runtime_error("Bad_optional_access in class InterProcessPollingSubscriber"); | ||
} | ||
return data_.value(); | ||
}; | ||
}; | ||
|
||
} // namespace tier4_autoware_utils | ||
|
||
#endif // TIER4_AUTOWARE_UTILS__ROS__POLLING_SUBSCRIBER_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
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