From 289eaaf679edf0f87e1d8deadfc9b4917e09be6e Mon Sep 17 00:00:00 2001 From: Clint Lombard Date: Mon, 30 Jan 2023 15:37:22 +0200 Subject: [PATCH] Add SubscriptionOptions wrapper Signed-off-by: Clint Lombard --- rclpy/CMakeLists.txt | 1 + rclpy/rclpy/node.py | 6 +++-- rclpy/rclpy/subscription_options.py | 18 +++++++++++++ rclpy/src/rclpy/_rclpy_pybind11.cpp | 2 ++ rclpy/src/rclpy/subscription.cpp | 9 +++++-- rclpy/src/rclpy/subscription.hpp | 5 ++-- rclpy/src/rclpy/subscription_options.cpp | 33 ++++++++++++++++++++++++ rclpy/src/rclpy/subscription_options.hpp | 29 +++++++++++++++++++++ 8 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 rclpy/rclpy/subscription_options.py create mode 100644 rclpy/src/rclpy/subscription_options.cpp create mode 100644 rclpy/src/rclpy/subscription_options.hpp diff --git a/rclpy/CMakeLists.txt b/rclpy/CMakeLists.txt index 305ac85f7..0b3e2a6f5 100644 --- a/rclpy/CMakeLists.txt +++ b/rclpy/CMakeLists.txt @@ -97,6 +97,7 @@ pybind11_add_module(_rclpy_pybind11 SHARED src/rclpy/service_info.cpp src/rclpy/signal_handler.cpp src/rclpy/subscription.cpp + src/rclpy/subscription_options.cpp src/rclpy/time_point.cpp src/rclpy/timer.cpp src/rclpy/utils.cpp diff --git a/rclpy/rclpy/node.py b/rclpy/rclpy/node.py index 95042e2ca..c528af93d 100644 --- a/rclpy/rclpy/node.py +++ b/rclpy/rclpy/node.py @@ -73,6 +73,7 @@ from rclpy.qos_overriding_options import QoSOverridingOptions from rclpy.service import Service from rclpy.subscription import Subscription +from rclpy.subscription_options import SubscriptionOptions from rclpy.time_source import TimeSource from rclpy.timer import Rate from rclpy.timer import Timer @@ -1508,7 +1509,8 @@ def create_subscription( callback_group: Optional[CallbackGroup] = None, event_callbacks: Optional[SubscriptionEventCallbacks] = None, qos_overriding_options: Optional[QoSOverridingOptions] = None, - raw: bool = False + raw: bool = False, + subscription_options: Optional[SubscriptionOptions] = None, ) -> Subscription: """ Create a new subscription. @@ -1553,7 +1555,7 @@ def create_subscription( try: with self.handle: subscription_object = _rclpy.Subscription( - self.handle, msg_type, topic, qos_profile.get_c_qos_profile()) + self.handle, msg_type, topic, qos_profile.get_c_qos_profile(), subscription_options) except ValueError: failed = True if failed: diff --git a/rclpy/rclpy/subscription_options.py b/rclpy/rclpy/subscription_options.py new file mode 100644 index 000000000..f0d2897a3 --- /dev/null +++ b/rclpy/rclpy/subscription_options.py @@ -0,0 +1,18 @@ +# Copyright 2016 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. +from rclpy.impl.implementation_singleton import rclpy_implementation as _rclpy + + +class SubscriptionOptions(_rclpy.rmw_subscription_options_t): + pass diff --git a/rclpy/src/rclpy/_rclpy_pybind11.cpp b/rclpy/src/rclpy/_rclpy_pybind11.cpp index 719e9a87d..81a9ad6d5 100644 --- a/rclpy/src/rclpy/_rclpy_pybind11.cpp +++ b/rclpy/src/rclpy/_rclpy_pybind11.cpp @@ -46,6 +46,7 @@ #include "service_info.hpp" #include "signal_handler.hpp" #include "subscription.hpp" +#include "subscription_options.hpp" #include "time_point.hpp" #include "timer.hpp" #include "utils.hpp" @@ -144,6 +145,7 @@ PYBIND11_MODULE(_rclpy_pybind11, m) { rclpy::define_guard_condition(m); rclpy::define_timer(m); rclpy::define_subscription(m); + rclpy::define_subscription_options(m); rclpy::define_time_point(m); rclpy::define_clock(m); rclpy::define_waitset(m); diff --git a/rclpy/src/rclpy/subscription.cpp b/rclpy/src/rclpy/subscription.cpp index 295092bb7..1d49a3c6e 100644 --- a/rclpy/src/rclpy/subscription.cpp +++ b/rclpy/src/rclpy/subscription.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -37,7 +38,7 @@ namespace rclpy { Subscription::Subscription( Node & node, py::object pymsg_type, std::string topic, - py::object pyqos_profile) + py::object pyqos_profile, py::object pysubscription_options) : node_(node) { auto msg_type = static_cast( @@ -52,6 +53,10 @@ Subscription::Subscription( subscription_ops.qos = pyqos_profile.cast(); } + if (!pysubscription_options.is_none()) { + subscription_ops.rmw_subscription_options = pysubscription_options.cast(); + } + rcl_subscription_ = std::shared_ptr( new rcl_subscription_t, [node](rcl_subscription_t * subscription) @@ -172,7 +177,7 @@ void define_subscription(py::object module) { py::class_>(module, "Subscription") - .def(py::init()) + .def(py::init()) .def_property_readonly( "pointer", [](const Subscription & subscription) { return reinterpret_cast(subscription.rcl_ptr()); diff --git a/rclpy/src/rclpy/subscription.hpp b/rclpy/src/rclpy/subscription.hpp index 305f1bf5e..912378b54 100644 --- a/rclpy/src/rclpy/subscription.hpp +++ b/rclpy/src/rclpy/subscription.hpp @@ -46,10 +46,11 @@ class Subscription : public Destroyable, public std::enable_shared_from_this rcl_subscription_; }; -/// Define a pybind11 wrapper for an rclpy::Service +/// Define a pybind11 wrapper for an rclpy::Subscription void define_subscription(py::object module); } // namespace rclpy diff --git a/rclpy/src/rclpy/subscription_options.cpp b/rclpy/src/rclpy/subscription_options.cpp new file mode 100644 index 000000000..2ebc6c95b --- /dev/null +++ b/rclpy/src/rclpy/subscription_options.cpp @@ -0,0 +1,33 @@ +// Copyright 2021 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 + +#include +#include + +#include "subscription_options.hpp" + +using pybind11::literals::operator""_a; + +namespace rclpy +{ +void +define_subscription_options(py::object module) +{ + py::class_(module, "rmw_subscription_options_t") + .def(py::init<>(&rmw_get_default_subscription_options)) + .def_readwrite("ignore_local_publications", &rmw_subscription_options_t::ignore_local_publications); +} +} // namespace rclpy diff --git a/rclpy/src/rclpy/subscription_options.hpp b/rclpy/src/rclpy/subscription_options.hpp new file mode 100644 index 000000000..5b407590b --- /dev/null +++ b/rclpy/src/rclpy/subscription_options.hpp @@ -0,0 +1,29 @@ +// Copyright 2021 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 RCLPY__SUBSCRIPTION_OPTIONS_HPP_ +#define RCLPY__SUBSCRIPTION_OPTIONS_HPP_ + +#include + +namespace py = pybind11; + +namespace rclpy +{ +/// Define a pybind11 wrapper for an rclpy::SubscriptionOptions +void define_subscription_options(py::object module); +} // namespace rclpy + + +#endif // RCLPY__SUBSCRIPTION_OPTIONS_HPP_