Skip to content

Commit

Permalink
feat: add ROS2 support to Python runtime
Browse files Browse the repository at this point in the history
Enable linking and compilation of ROS2 interfaces based on the build configuration. This improves flexibility by allowing the Python runtime to interface with ROS2 when needed, enhancing interoperability and feature set.
  • Loading branch information
zhangyi committed Nov 12, 2024
1 parent ccc44ba commit bdf100d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 45 deletions.
9 changes: 7 additions & 2 deletions src/runtime/python_runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ target_sources(${CUR_TARGET_NAME} PRIVATE ${src})
# Set link libraries of target
target_link_libraries(
${CUR_TARGET_NAME}
PRIVATE $<LINK_LIBRARY:WHOLE_ARCHIVE,aimrt::runtime::core>
aimrt::interface::aimrt_module_ros2_interface)
PRIVATE $<LINK_LIBRARY:WHOLE_ARCHIVE,aimrt::runtime::core>)

# Link ros2 interface if building with ros2
if (AIMRT_BUILD_WITH_ROS2)
target_link_libraries(${CUR_TARGET_NAME} PRIVATE aimrt::interface::aimrt_module_ros2_interface)
target_compile_definitions(${CUR_TARGET_NAME} PRIVATE AIMRT_BUILD_WITH_ROS2)
endif()

# Set misc of target
set_target_properties(${CUR_TARGET_NAME} PROPERTIES OUTPUT_NAME "aimrt_python_runtime")
Expand Down
92 changes: 50 additions & 42 deletions src/runtime/python_runtime/export_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@

#pragma once

#include "pybind11/pybind11.h"

#include <utility>

#include "aimrt_module_cpp_interface/channel/channel_context.h"
#include "aimrt_module_cpp_interface/channel/channel_handle.h"

#include "python_runtime/export_pb_type_support.h"
#include "python_runtime/export_ros2_type_support.h"
#include "python_runtime/ros2_type_support_utils.h"

#include "pybind11/pybind11.h"
#ifdef AIMRT_BUILD_WITH_ROS2
#include "python_runtime/export_ros2_type_support.h"
#include "python_runtime/ros2_type_support_utils.h"
#endif

namespace aimrt::runtime::python_runtime {

Expand Down Expand Up @@ -63,15 +66,6 @@ inline bool PyRegisterPbPublishType(
return publisher_ref.RegisterPublishType(msg_type_support->NativeHandle());
}

inline bool PyRegisterRos2PublishType(
aimrt::channel::PublisherRef& publisher_ref,
const std::shared_ptr<const PyRos2TypeSupport>& py_ros2_type_support) {
static std::vector<std::shared_ptr<const PyRos2TypeSupport>> py_ros2_ts_vec;
py_ros2_ts_vec.emplace_back(py_ros2_type_support);

return publisher_ref.RegisterPublishType(py_ros2_type_support->NativeHandle());
}

inline void PyPublishPbMessageWithCtx(
aimrt::channel::PublisherRef& publisher_ref,
std::string_view msg_type,
Expand All @@ -80,36 +74,6 @@ inline void PyPublishPbMessageWithCtx(
publisher_ref.Publish(msg_type, ctx_ref, static_cast<const void*>(&msg_buf));
}

inline void PyPublishRos2MessageWithCtx(
aimrt::channel::PublisherRef& publisher_ref,
std::string_view msg_type,
const aimrt::channel::ContextRef& ctx_ref,
pybind11::object msg_obj) {
auto msg_ptr = convert_from_py(msg_obj);
if (!msg_ptr) {
throw py::error_already_set();
}

publisher_ref.Publish(msg_type, ctx_ref, static_cast<const void*>(msg_ptr.get()));
}

inline void ExportPublisherRef(pybind11::object m) {
using aimrt::channel::PublisherRef;

using PyPbTsPtr = std::shared_ptr<const PyPbTypeSupport>;
using PyRos2TsPtr = std::shared_ptr<const PyRos2TypeSupport>;

pybind11::class_<PublisherRef>(std::move(m), "PublisherRef")
.def(pybind11::init<>())
.def("__bool__", &PublisherRef::operator bool)
.def("RegisterPbPublishType", &PyRegisterPbPublishType)
.def("RegisterRos2PublishType", &PyRegisterRos2PublishType)
.def("PublishPbMessageWithCtx", &PyPublishPbMessageWithCtx)
.def("PublishRos2MessageWithCtx", &PyPublishRos2MessageWithCtx)
.def("GetTopic", &PublisherRef::GetTopic)
.def("MergeSubscribeContextToPublishContext", &PublisherRef::MergeSubscribeContextToPublishContext);
}

inline bool PySubscribePbMessageWithCtx(
aimrt::channel::SubscriberRef& subscriber_ref,
const std::shared_ptr<const PyPbTypeSupport>& msg_type_support,
Expand Down Expand Up @@ -140,6 +104,30 @@ inline bool PySubscribePbMessageWithCtx(
});
}

#ifdef AIMRT_BUILD_WITH_ROS2

inline bool PyRegisterRos2PublishType(
aimrt::channel::PublisherRef& publisher_ref,
const std::shared_ptr<const PyRos2TypeSupport>& py_ros2_type_support) {
static std::vector<std::shared_ptr<const PyRos2TypeSupport>> py_ros2_ts_vec;
py_ros2_ts_vec.emplace_back(py_ros2_type_support);

return publisher_ref.RegisterPublishType(py_ros2_type_support->NativeHandle());
}

inline void PyPublishRos2MessageWithCtx(
aimrt::channel::PublisherRef& publisher_ref,
std::string_view msg_type,
const aimrt::channel::ContextRef& ctx_ref,
pybind11::object msg_obj) {
auto msg_ptr = convert_from_py(msg_obj);
if (!msg_ptr) {
throw py::error_already_set();
}

publisher_ref.Publish(msg_type, ctx_ref, static_cast<const void*>(msg_ptr.get()));
}

inline bool PySubscribeRos2MessageWithCtx(
aimrt::channel::SubscriberRef& subscriber_ref,
const std::shared_ptr<const PyRos2TypeSupport>& py_ros2_type_support,
Expand Down Expand Up @@ -184,14 +172,34 @@ inline bool PySubscribeRos2MessageWithCtx(
});
}

#endif

inline void ExportPublisherRef(pybind11::object m) {
using aimrt::channel::PublisherRef;

pybind11::class_<PublisherRef>(std::move(m), "PublisherRef")
.def(pybind11::init<>())
.def("__bool__", &PublisherRef::operator bool)
.def("RegisterPbPublishType", &PyRegisterPbPublishType)
.def("PublishPbMessageWithCtx", &PyPublishPbMessageWithCtx)
#ifdef AIMRT_BUILD_WITH_ROS2
.def("PublishRos2MessageWithCtx", &PyPublishRos2MessageWithCtx)
.def("RegisterRos2PublishType", &PyRegisterRos2PublishType)
#endif
.def("GetTopic", &PublisherRef::GetTopic)
.def("MergeSubscribeContextToPublishContext", &PublisherRef::MergeSubscribeContextToPublishContext);
}

inline void ExportSubscriberRef(pybind11::object m) {
using aimrt::channel::SubscriberRef;

pybind11::class_<SubscriberRef>(std::move(m), "SubscriberRef")
.def(pybind11::init<>())
.def("__bool__", &SubscriberRef::operator bool)
.def("SubscribePbMessageWithCtx", &PySubscribePbMessageWithCtx)
#ifdef AIMRT_BUILD_WITH_ROS2
.def("SubscribeRos2MessageWithCtx", &PySubscribeRos2MessageWithCtx)
#endif
.def("GetTopic", &SubscriberRef::GetTopic);
}

Expand Down
7 changes: 6 additions & 1 deletion src/runtime/python_runtime/python_runtime_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
#include "python_runtime/export_module_base.h"
#include "python_runtime/export_parameter.h"
#include "python_runtime/export_pb_type_support.h"
#include "python_runtime/export_ros2_type_support.h"
#include "python_runtime/export_rpc.h"

#ifdef AIMRT_BUILD_WITH_ROS2
#include "python_runtime/export_ros2_type_support.h"
#endif

using namespace aimrt::runtime::python_runtime;

PYBIND11_MODULE(aimrt_python_runtime, m) {
Expand All @@ -26,7 +29,9 @@ PYBIND11_MODULE(aimrt_python_runtime, m) {

// type support
ExportPbTypeSupport(m);
#ifdef AIMRT_BUILD_WITH_ROS2
ExportRos2TypeSupport(m);
#endif

// core handle
ExportModuleInfo(m);
Expand Down

0 comments on commit bdf100d

Please sign in to comment.