allow non @topic_config support

This commit is contained in:
Xuwznln
2026-03-31 13:15:06 +08:00
parent ee63e95f50
commit 861a012747

View File

@@ -1256,9 +1256,8 @@ class BaseROS2DeviceNode(Node, Generic[T]):
return self._lab_logger return self._lab_logger
def create_ros_publisher(self, attr_name, msg_type, initial_period=5.0): def create_ros_publisher(self, attr_name, msg_type, initial_period=5.0):
"""创建ROS发布者,仅当方法/属性有 @topic_config 装饰器时才创建""" """创建ROS发布者。已在 status_types 中声明的属性直接创建;@topic_config 用于覆盖默认参数"""
# 检测 @topic_config 装饰器配置 topic_cfg = {}
topic_config = {}
driver_class = type(self.driver_instance) driver_class = type(self.driver_instance)
# 区分 @property 和普通方法两种情况 # 区分 @property 和普通方法两种情况
@@ -1267,23 +1266,17 @@ class BaseROS2DeviceNode(Node, Generic[T]):
) )
if is_prop: if is_prop:
# @property: 检测 fget 上的 @topic_config
class_attr = getattr(driver_class, attr_name) class_attr = getattr(driver_class, attr_name)
if class_attr.fget is not None: if class_attr.fget is not None:
topic_config = get_topic_config(class_attr.fget) topic_cfg = get_topic_config(class_attr.fget)
else: else:
# 普通方法: 直接检测 attr_name 方法上的 @topic_config
if hasattr(self.driver_instance, attr_name): if hasattr(self.driver_instance, attr_name):
method = getattr(self.driver_instance, attr_name) method = getattr(self.driver_instance, attr_name)
if callable(method): if callable(method):
topic_config = get_topic_config(method) topic_cfg = get_topic_config(method)
# 没有 @topic_config 装饰器则跳过发布
if not topic_config:
return
# 发布名称优先级: @topic_config(name=...) > get_ 前缀去除 > attr_name # 发布名称优先级: @topic_config(name=...) > get_ 前缀去除 > attr_name
cfg_name = topic_config.get("name") cfg_name = topic_cfg.get("name")
if cfg_name: if cfg_name:
publish_name = cfg_name publish_name = cfg_name
elif attr_name.startswith("get_"): elif attr_name.startswith("get_"):
@@ -1291,10 +1284,10 @@ class BaseROS2DeviceNode(Node, Generic[T]):
else: else:
publish_name = attr_name publish_name = attr_name
# 使用装饰器配置或默认值 # @topic_config 参数覆盖默认值
cfg_period = topic_config.get("period") cfg_period = topic_cfg.get("period")
cfg_print = topic_config.get("print_publish") cfg_print = topic_cfg.get("print_publish")
cfg_qos = topic_config.get("qos") cfg_qos = topic_cfg.get("qos")
period: float = cfg_period if cfg_period is not None else initial_period period: float = cfg_period if cfg_period is not None else initial_period
print_publish: bool = cfg_print if cfg_print is not None else self._print_publish print_publish: bool = cfg_print if cfg_print is not None else self._print_publish
qos: int = cfg_qos if cfg_qos is not None else 10 qos: int = cfg_qos if cfg_qos is not None else 10