Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: sparate ros2 dependency in aimrt_py #117

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: streamline ROS2 import structure and enhance CMake configur…
…ation

- Removed direct import of aimrt_python_runtime_ros2 in favor of aliasing for clarity in aimrt_py_chn.py.
- Updated CMakeLists.txt to conditionally add dependencies for ROS2 components, improving build flexibility.
- Adjusted ROS2 service generation script to utilize the aliased import for better readability and maintainability.
  • Loading branch information
zhangyi1357 committed Dec 3, 2024
commit be39f7120be160935be489cd44f0b3bd2e5de382
15 changes: 9 additions & 6 deletions src/runtime/python_runtime/aimrt_py_chn.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import google.protobuf
import google.protobuf.message

from . import aimrt_python_runtime, aimrt_python_runtime_ros2
from . import aimrt_python_runtime
from .check_ros2_type import check_is_valid_ros2_msg_type

Ros2MsgType = TypeVar("Ros2MsgType")
Expand Down Expand Up @@ -81,10 +81,11 @@ def RegisterPublishType(publisher: aimrt_python_runtime.PublisherRef,
py_pb_ts.SetSerializationTypesSupportedList(["pb", "json"])
return publisher.PbRegisterPublishType(py_pb_ts)
elif check_is_valid_ros2_msg_type(msg_type):
py_ros2_ts = aimrt_python_runtime_ros2.PyRos2TypeSupport(msg_type)
from . import aimrt_python_runtime_ros2 as aimrt_py_ros2
py_ros2_ts = aimrt_py_ros2.PyRos2TypeSupport(msg_type)
py_ros2_ts.SetTypeName(GetRos2MessageTypeName(msg_type))
py_ros2_ts.SetSerializationTypesSupportedList(["ros2"])
return aimrt_python_runtime_ros2.Ros2RegisterPublishType(publisher, py_ros2_ts)
return aimrt_py_ros2.Ros2RegisterPublishType(publisher, py_ros2_ts)
else:
raise TypeError(f"Invalid message type: {type(msg_type)}")

Expand Down Expand Up @@ -140,8 +141,9 @@ def Publish(publisher: aimrt_python_runtime.PublisherRef, second, third=None):
serialized_msg = _SerializeProtobufMessage(msg, ctx_ref.GetSerializationType())
publisher.PbPublishWithCtx(GetPbMessageTypeName(msg.__class__), ctx_ref, serialized_msg)
elif message_type == "ros2":
from . import aimrt_python_runtime_ros2 as aimrt_py_ros2
ctx_ref = _CreateContextRef(ctx_or_type, default_serialization_type="ros2")
aimrt_python_runtime_ros2.Ros2PublishWithCtx(publisher, GetRos2MessageTypeName(msg.__class__), ctx_ref, msg)
aimrt_py_ros2.Ros2PublishWithCtx(publisher, GetRos2MessageTypeName(msg.__class__), ctx_ref, msg)


def Subscribe(subscriber: aimrt_python_runtime.SubscriberRef,
Expand Down Expand Up @@ -187,7 +189,8 @@ def handle_callback(ctx_ref: aimrt_python_runtime.ContextRef, msg_buf: bytes):
subscriber.PbSubscribeWithCtx(py_pb_ts, handle_callback)

elif check_is_valid_ros2_msg_type(msg_type):
py_ros2_ts = aimrt_python_runtime_ros2.PyRos2TypeSupport(msg_type)
from . import aimrt_python_runtime_ros2 as aimrt_py_ros2
py_ros2_ts = aimrt_py_ros2.PyRos2TypeSupport(msg_type)
py_ros2_ts.SetTypeName(GetRos2MessageTypeName(msg_type))
py_ros2_ts.SetSerializationTypesSupportedList(["ros2"])

Expand All @@ -200,7 +203,7 @@ def ros2_callback_wrapper(ctx_ref: aimrt_python_runtime.ContextRef, msg):
except Exception as e:
print(f"AimRT channel handle get exception, {e}", file=sys.stderr)

aimrt_python_runtime_ros2.Ros2SubscribeWithCtx(subscriber, py_ros2_ts, msg_type, ros2_callback_wrapper)
aimrt_py_ros2.Ros2SubscribeWithCtx(subscriber, py_ros2_ts, msg_type, ros2_callback_wrapper)

else:
raise TypeError(f"Invalid message type: {type(msg_type)}, expected Protobuf or ROS2 message type")
4 changes: 4 additions & 0 deletions src/tools/package_aimrt_py/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ add_custom_target(
DEPENDS copy_python_runtime
WORKING_DIRECTORY ${AIMRT_PY_PKG_DIR})

if(AIMRT_BUILD_WITH_ROS2)
add_dependencies(create_python_pkg copy_ros2_python_runtime)
endif()

if(AIMRT_BUILD_WITH_ROS2 AND AIMRT_BUILD_ROS2_PLUGIN)
add_dependencies(create_python_pkg copy_ros2_plugin_proto_files)
endif()
Expand Down
15 changes: 8 additions & 7 deletions src/tools/ros2_py_gen_aimrt_py_rpc/ros2_py_gen_aimrt_py_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from typing import overload

import aimrt_py
import aimrt_py.aimrt_python_runtime_ros2 as aimrt_py_ros2
import {{package_name}}.srv

if {{package_name}}.srv.{{service_name}}._TYPE_SUPPORT is None:
Expand All @@ -23,18 +24,18 @@ def __init__(self):
super().__init__("ros2", "{{package_name}}/srv")

# {{service_name}}
{{service_name}}_req_aimrt_ts = aimrt_py.PyRos2TypeSupport({{package_name}}.srv.{{service_name}}.Request)
{{service_name}}_req_aimrt_ts = aimrt_py_ros2.PyRos2TypeSupport({{package_name}}.srv.{{service_name}}.Request)
{{service_name}}_req_aimrt_ts.SetTypeName(aimrt_py.GetRos2MessageTypeName({{package_name}}.srv.{{service_name}}.Request))
{{service_name}}_req_aimrt_ts.SetSerializationTypesSupportedList(["ros2"])

{{service_name}}_rsp_aimrt_ts = aimrt_py.PyRos2TypeSupport({{package_name}}.srv.{{service_name}}.Response)
{{service_name}}_rsp_aimrt_ts = aimrt_py_ros2.PyRos2TypeSupport({{package_name}}.srv.{{service_name}}.Response)
{{service_name}}_rsp_aimrt_ts.SetTypeName(aimrt_py.GetRos2MessageTypeName({{package_name}}.srv.{{service_name}}.Response))
{{service_name}}_rsp_aimrt_ts.SetSerializationTypesSupportedList(["ros2"])

if not aimrt_py.check_is_valid_srv_type({{package_name}}.srv.{{service_name}}):
raise RuntimeError("The service type provided is not valid")

aimrt_py.Ros2RegisterServiceFunc(
aimrt_py_ros2.Ros2RegisterServiceFunc(
self,
"{{service_name}}",
{{package_name}}.srv.{{service_name}},
Expand Down Expand Up @@ -92,7 +93,7 @@ def {{service_name}}(self, *args) -> tuple[aimrt_py.RpcStatus, {{package_name}}.
ctx_ref = aimrt_py.RpcContextRef(real_ctx)
ctx_ref.SetSerializationType("ros2")

status, rsp = aimrt_py.Ros2Invoke(
status, rsp = aimrt_py_ros2.Ros2Invoke(
self.rpc_handle_ref,
"ros2:/{{package_name}}/srv/{{service_name}}",
ctx_ref,
Expand All @@ -105,15 +106,15 @@ def {{service_name}}(self, *args) -> tuple[aimrt_py.RpcStatus, {{package_name}}.
@staticmethod
def RegisterClientFunc(rpc_handle):
# {{service_name}}
{{service_name}}_req_aimrt_ts = aimrt_py.PyRos2TypeSupport({{package_name}}.srv.{{service_name}}.Request)
{{service_name}}_req_aimrt_ts = aimrt_py_ros2.PyRos2TypeSupport({{package_name}}.srv.{{service_name}}.Request)
{{service_name}}_req_aimrt_ts.SetTypeName(aimrt_py.GetRos2MessageTypeName({{package_name}}.srv.{{service_name}}.Request))
{{service_name}}_req_aimrt_ts.SetSerializationTypesSupportedList(["ros2"])

{{service_name}}_rsp_aimrt_ts = aimrt_py.PyRos2TypeSupport({{package_name}}.srv.{{service_name}}.Response)
{{service_name}}_rsp_aimrt_ts = aimrt_py_ros2.PyRos2TypeSupport({{package_name}}.srv.{{service_name}}.Response)
{{service_name}}_rsp_aimrt_ts.SetTypeName(aimrt_py.GetRos2MessageTypeName({{package_name}}.srv.{{service_name}}.Response))
{{service_name}}_rsp_aimrt_ts.SetSerializationTypesSupportedList(["ros2"])

return aimrt_py.Ros2RegisterClientFunc(
return aimrt_py_ros2.Ros2RegisterClientFunc(
rpc_handle,
"ros2:/{{package_name}}/srv/{{service_name}}",
{{package_name}}.srv.{{service_name}},
Expand Down