From 470d7283e4671965b625e49417338d2e365802b7 Mon Sep 17 00:00:00 2001 From: zhangshixiang <554662886@qq.com> Date: Mon, 19 Jan 2026 15:15:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B6=88=E6=81=AF=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- unilabos/ros/msgs/message_converter.py | 28 +++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/unilabos/ros/msgs/message_converter.py b/unilabos/ros/msgs/message_converter.py index 632d5e1..1946bfb 100644 --- a/unilabos/ros/msgs/message_converter.py +++ b/unilabos/ros/msgs/message_converter.py @@ -374,9 +374,35 @@ def convert_to_ros_msg(ros_msg_type: Union[Type, Any], obj: Any) -> Any: setattr(ros_msg, key, []) # FIXME elif "array.array" in str(type(attr)): if attr.typecode == "f" or attr.typecode == "d": + # 如果是单个值,转换为列表 + if value is None: + value = [] + elif not isinstance(value, Iterable) or isinstance(value, (str, bytes)): + value = [value] setattr(ros_msg, key, [float(i) for i in value]) else: - setattr(ros_msg, key, value) + # 对于整数数组,需要确保是序列且每个值在有效范围内 + if value is None: + value = [] + elif not isinstance(value, Iterable) or isinstance(value, (str, bytes)): + # 如果是单个值,转换为列表 + value = [value] + # 确保每个整数值在有效范围内(-2147483648 到 2147483647) + converted_value = [] + for i in value: + if i is None: + continue # 跳过 None 值 + if isinstance(i, (int, float)): + int_val = int(i) + # 确保在 int32 范围内 + if int_val < -2147483648: + int_val = -2147483648 + elif int_val > 2147483647: + int_val = 2147483647 + converted_value.append(int_val) + else: + converted_value.append(i) + setattr(ros_msg, key, converted_value) else: nested_ros_msg = convert_to_ros_msg(type(attr)(), value) setattr(ros_msg, key, nested_ros_msg)