Skip to content

Commit

Permalink
feat: update message types to use RosTestMsg for better compatibility
Browse files Browse the repository at this point in the history
Register new message type RosTestMsg in both publisher and subscriber applications. Update data format for publishing events to improve clarity and consistency in message handling. Ensure proper setup in the shell scripts for localization configuration.
  • Loading branch information
zhangyi committed Nov 12, 2024
1 parent b9a0016 commit 6d75862
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
15 changes: 8 additions & 7 deletions src/examples/py/ros2_chn/examples_py_ros2_chn_publisher_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import aimrt_py
import yaml
from std_msgs.msg import String
from example_ros2.msg import RosTestMsg


def main():
Expand Down Expand Up @@ -37,7 +37,7 @@ def main():
publisher = module_handle.GetChannelHandle().GetPublisher(topic_name)
assert publisher, f"Get publisher for topic '{topic_name}' failed."

aimrt_py.RegisterPublishType(publisher, String)
aimrt_py.RegisterPublishType(publisher, RosTestMsg)

# Start
thread = threading.Thread(target=aimrt_core.Start)
Expand All @@ -46,24 +46,25 @@ def main():
# Sleep for seconds
time.sleep(1)

msg = String()
msg = RosTestMsg()
msg.num = 1000

# Publish event
msg.data = "Publish without ctx or serialization_type"
msg.data = [bytes([x]) for x in [1, 2, 3, 4]]
aimrt_py.Publish(publisher, msg)
aimrt_py.info(module_handle.GetLogger(),
f"Publish new ros2 message, data: {msg.data}")

# Publish event with ros2 serialization
msg.data = "Publish with ros2 serialization"
msg.data = [bytes([x]) for x in [5, 6, 7, 8]]
aimrt_py.Publish(publisher, "ros2", msg)
aimrt_py.info(module_handle.GetLogger(),
f"Publish new ros2 message, data: {msg.data}")

# Publish event with context
ctx = aimrt_py.Context()
ctx.SetMetaValue("key1", "value1")
msg.data = "Publish with context"
msg.data = [bytes([x]) for x in [9, 10, 11, 12]]
aimrt_py.Publish(publisher, ctx, msg)
aimrt_py.info(module_handle.GetLogger(),
f"Publish new ros2 message, data: {msg.data}")
Expand All @@ -73,7 +74,7 @@ def main():
ctx_ref = aimrt_py.ContextRef(ctx)
ctx_ref.SetMetaValue("key2", "value2")
ctx_ref.SetSerializationType("ros2")
msg.data = "Publish with context ref"
msg.data = [bytes([x]) for x in [13, 14, 15, 16]]
aimrt_py.Publish(publisher, ctx_ref, msg)
aimrt_py.info(module_handle.GetLogger(),
f"Publish new ros2 message, data: {msg.data}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import aimrt_py
import yaml
from std_msgs.msg import String
from example_ros2.msg import RosTestMsg

global_aimrt_core = None

Expand Down Expand Up @@ -58,13 +58,13 @@ def main():

count = 0

def EventHandle(ctx_ref: aimrt_py.ContextRef, msg: String):
def EventHandle(ctx_ref: aimrt_py.ContextRef, msg: RosTestMsg):
nonlocal count
count += 1
aimrt_py.info(module_handle.GetLogger(),
f"Get new ros2 message, ctx: {ctx_ref.ToString()}, data: {msg.data}, count: {count}")

aimrt_py.Subscribe(subscriber, String, EventHandle)
aimrt_py.Subscribe(subscriber, RosTestMsg, EventHandle)

# Start
thread = threading.Thread(target=aimrt_core.Start)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash

export AIMRT_PLUGIN_DIR=$(pip show aimrt_py | grep Location | awk '{print $2}')/aimrt_py
source $AIMRT_PLUGIN_DIR/share/example_ros2/local_setup.bash

python3 ./examples_py_ros2_chn_publisher_app.py --cfg_file_path=./cfg/examples_py_ros2_chn_http_pub_cfg.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash

export AIMRT_PLUGIN_DIR=$(pip show aimrt_py | grep Location | awk '{print $2}')/aimrt_py
source $AIMRT_PLUGIN_DIR/share/example_ros2/local_setup.bash

python3 ./examples_py_ros2_chn_subscriber_app.py --cfg_file_path=./cfg/examples_py_ros2_chn_http_sub_cfg.yaml
6 changes: 3 additions & 3 deletions src/runtime/python_runtime/aimrt_py_chn.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ def _CreateContextRef(ctx_or_type, default_serialization_type: str) -> aimrt_pyt


def _GetRos2MessageTypeName(msg_type: Ros2MsgType) -> str:
return "ros2:" + ".".join([msg_type.__module__, msg_type.__name__])
module_parts = msg_type.__module__.split('.')
module_name = '/'.join(module_parts[:-1])
return "ros2:" + "/".join([module_name, msg_type.__name__])


def _GetPbMessageTypeName(msg: google.protobuf.message.Message | google._upb._message.MessageMeta) -> str:
Expand All @@ -74,13 +76,11 @@ def RegisterPublishType(publisher: aimrt_python_runtime.PublisherRef,
bool: True if success, False otherwise
"""
if isinstance(msg_type, google._upb._message.MessageMeta):
print(f"_GetPbMessageTypeName(msg_type): {_GetPbMessageTypeName(msg_type)}")
py_pb_ts = aimrt_python_runtime.PyPbTypeSupport()
py_pb_ts.SetTypeName(_GetPbMessageTypeName(msg_type))
py_pb_ts.SetSerializationTypesSupportedList(["pb", "json"])
return publisher.RegisterPbPublishType(py_pb_ts)
elif check_is_valid_ros2_msg_type(msg_type):
print(f"_GetRos2MessageTypeName(msg_type): {_GetRos2MessageTypeName(msg_type)}")
py_ros2_ts = aimrt_python_runtime.PyRos2TypeSupport(msg_type)
py_ros2_ts.SetTypeName(_GetRos2MessageTypeName(msg_type))
py_ros2_ts.SetSerializationTypesSupportedList(["ros2"])
Expand Down

0 comments on commit 6d75862

Please sign in to comment.