Skip to content

Commit

Permalink
Merge pull request #6 from Fool-Stuck/feat/subscription_utils
Browse files Browse the repository at this point in the history
feat: add no callback subscription wrapper class
  • Loading branch information
RyodoTanaka authored Jun 30, 2024
2 parents 7067164 + 8e717db commit 1d83f22
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/wheel_stuck_utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.5)
project(wheel_stuck_utils)

if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

find_package(ament_cmake_auto REQUIRED)
ament_auto_find_build_dependencies()

# ament_auto_add_library(autoware_universe_utils SHARED
# src/source.cpp
# )

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
endif()

ament_auto_package()
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef WHEEL_STUCK_UTILS__ROS__NO_CALLBACK_SUBSCRIPTION_HPP_
#define WHEEL_STUCK_UTILS__ROS__NO_CALLBACK_SUBSCRIPTION_HPP_

#include <rclcpp/rclcpp.hpp>

namespace wheel_stuck_utils
{

template <typename TMessage>
class NoCallbackSubscription
{
private:
typename rclcpp::Subscription<TMessage>::SharedPtr subscription_;

public:
using SharedPtr = std::shared_ptr<NoCallbackSubscription<TMessage>>;
static SharedPtr create_subscription(
rclcpp::Node * node, const std::string & topic_name, const rclcpp::QoS & qos = rclcpp::QoS{1})
{
return std::make_shared<NoCallbackSubscription<TMessage>>(node, topic_name, qos);
}

explicit NoCallbackSubscription(
rclcpp::Node * node, const std::string & topic_name, const rclcpp::QoS & qos = rclcpp::QoS{1})
{
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;

subscription_ = node->create_subscription<TMessage>(
topic_name, qos,
[node]([[maybe_unused]] const typename TMessage::ConstSharedPtr msg) { assert(false); },
noexec_subscription_options);
}

typename TMessage::ConstSharedPtr getData()
{
auto data = std::make_shared<TMessage>();
rclcpp::MessageInfo message_info;
const bool is_received = subscription_->take(*data, message_info);
return is_received ? data : nullptr;
}
};

} // namespace wheel_stuck_utils

#endif
20 changes: 20 additions & 0 deletions src/wheel_stuck_utils/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>wheel_stuck_utils</name>
<version>0.1.0</version>
<description>The wheel_stuck_utils package</description>
<maintainer email="[email protected]">Akiro Harada</maintainer>

<license>Apache 2</license>

<buildtool_depend>ament_cmake_auto</buildtool_depend>

<depend>rclcpp</depend>

<test_depend>ament_lint_auto</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>

0 comments on commit 1d83f22

Please sign in to comment.