mirror of
https://github.com/deepmodeling/Uni-Lab-OS
synced 2026-03-24 11:24:19 +00:00
Compare commits
76 Commits
aeeb36d075
...
feature/or
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b61c818f7f | ||
|
|
f2c0bec02c | ||
|
|
47a29a0c2f | ||
|
|
e0394bf414 | ||
|
|
975a56415a | ||
|
|
cadbe87e3f | ||
|
|
b993c1f590 | ||
|
|
e0fae94c10 | ||
|
|
b5cd181ac1 | ||
|
|
5c047beb83 | ||
|
|
b40c087143 | ||
|
|
7f1cc3b2a5 | ||
|
|
3f160c2049 | ||
|
|
a54e7c0f23 | ||
|
|
e5015cd5e0 | ||
|
|
9c6f7c7505 | ||
|
|
514373c164 | ||
|
|
fcea02585a | ||
|
|
07cf690897 | ||
|
|
cfea27460a | ||
|
|
b7d3e980a9 | ||
|
|
f9ed6cb3fb | ||
|
|
699a0b3ce7 | ||
|
|
cf3a20ae79 | ||
|
|
cdf0652020 | ||
|
|
60073ff139 | ||
|
|
a9053b822f | ||
|
|
d238c2ab8b | ||
|
|
9a7d5c7c82 | ||
|
|
4f7d431c0b | ||
|
|
341a1b537c | ||
|
|
957fb41a6f | ||
|
|
26271bcab8 | ||
|
|
e4e4bfbe20 | ||
|
|
84a8223173 | ||
|
|
e8d1263488 | ||
|
|
380b39100d | ||
|
|
64c748d921 | ||
|
|
15ff0e9d30 | ||
|
|
f8a52860ad | ||
|
|
e30c01d54e | ||
|
|
56eb7e2ab4 | ||
|
|
23ce145f74 | ||
|
|
b0da149252 | ||
|
|
07c9e6f0fe | ||
|
|
ccec6b9d77 | ||
|
|
dadfdf3d8d | ||
|
|
37ec49f318 | ||
|
|
6bf57f18c1 | ||
|
|
400bb073d4 | ||
|
|
3f63c36505 | ||
|
|
0ae94f7f3c | ||
|
|
7eacae6442 | ||
|
|
f7d2cb4b9e | ||
|
|
bf980d7248 | ||
|
|
27c0544bfc | ||
|
|
d48e77c9ae | ||
|
|
e70a5bea66 | ||
|
|
467d75dc03 | ||
|
|
9feeb0c430 | ||
|
|
b2f26ffb28 | ||
|
|
4b0d1553e9 | ||
|
|
67ddee2ab2 | ||
|
|
1bcdad9448 | ||
|
|
039c96fe01 | ||
|
|
e1555d10a0 | ||
|
|
f2a96b2041 | ||
|
|
329349639e | ||
|
|
e4cc111523 | ||
|
|
d245ceef1b | ||
|
|
6db7fbd721 | ||
|
|
ab05b858e1 | ||
|
|
43e4c71a8e | ||
|
|
c4a3be1498 | ||
|
|
e11070315d | ||
|
|
50ebcad9d7 |
62
.conda/base/recipe.yaml
Normal file
62
.conda/base/recipe.yaml
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
# unilabos: Production package (depends on unilabos-env + pip unilabos)
|
||||||
|
# For production deployment
|
||||||
|
|
||||||
|
package:
|
||||||
|
name: unilabos
|
||||||
|
version: 0.10.18
|
||||||
|
|
||||||
|
source:
|
||||||
|
path: ../../unilabos
|
||||||
|
target_directory: unilabos
|
||||||
|
|
||||||
|
build:
|
||||||
|
python:
|
||||||
|
entry_points:
|
||||||
|
- unilab = unilabos.app.main:main
|
||||||
|
script:
|
||||||
|
- set PIP_NO_INDEX=
|
||||||
|
- if: win
|
||||||
|
then:
|
||||||
|
- copy %RECIPE_DIR%\..\..\MANIFEST.in %SRC_DIR%
|
||||||
|
- copy %RECIPE_DIR%\..\..\setup.cfg %SRC_DIR%
|
||||||
|
- copy %RECIPE_DIR%\..\..\setup.py %SRC_DIR%
|
||||||
|
- pip install %SRC_DIR%
|
||||||
|
- if: unix
|
||||||
|
then:
|
||||||
|
- cp $RECIPE_DIR/../../MANIFEST.in $SRC_DIR
|
||||||
|
- cp $RECIPE_DIR/../../setup.cfg $SRC_DIR
|
||||||
|
- cp $RECIPE_DIR/../../setup.py $SRC_DIR
|
||||||
|
- pip install $SRC_DIR
|
||||||
|
|
||||||
|
requirements:
|
||||||
|
host:
|
||||||
|
- python ==3.11.14
|
||||||
|
- pip
|
||||||
|
- setuptools
|
||||||
|
- zstd
|
||||||
|
- zstandard
|
||||||
|
run:
|
||||||
|
- zstd
|
||||||
|
- zstandard
|
||||||
|
- networkx
|
||||||
|
- typing_extensions
|
||||||
|
- websockets
|
||||||
|
- pint
|
||||||
|
- fastapi
|
||||||
|
- jinja2
|
||||||
|
- requests
|
||||||
|
- uvicorn
|
||||||
|
- if: not osx
|
||||||
|
then:
|
||||||
|
- opcua
|
||||||
|
- pyserial
|
||||||
|
- pandas
|
||||||
|
- pymodbus
|
||||||
|
- matplotlib
|
||||||
|
- pylibftdi
|
||||||
|
- uni-lab::unilabos-env ==0.10.18
|
||||||
|
|
||||||
|
about:
|
||||||
|
repository: https://github.com/deepmodeling/Uni-Lab-OS
|
||||||
|
license: GPL-3.0-only
|
||||||
|
description: "UniLabOS - Production package with minimal ROS2 dependencies"
|
||||||
39
.conda/environment/recipe.yaml
Normal file
39
.conda/environment/recipe.yaml
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
# unilabos-env: conda environment dependencies (ROS2 + conda packages)
|
||||||
|
|
||||||
|
package:
|
||||||
|
name: unilabos-env
|
||||||
|
version: 0.10.18
|
||||||
|
|
||||||
|
build:
|
||||||
|
noarch: generic
|
||||||
|
|
||||||
|
requirements:
|
||||||
|
run:
|
||||||
|
# Python
|
||||||
|
- zstd
|
||||||
|
- zstandard
|
||||||
|
- conda-forge::python ==3.11.14
|
||||||
|
- conda-forge::opencv
|
||||||
|
# ROS2 dependencies (from ci-check.yml)
|
||||||
|
- robostack-staging::ros-humble-ros-core
|
||||||
|
- robostack-staging::ros-humble-action-msgs
|
||||||
|
- robostack-staging::ros-humble-std-msgs
|
||||||
|
- robostack-staging::ros-humble-geometry-msgs
|
||||||
|
- robostack-staging::ros-humble-control-msgs
|
||||||
|
- robostack-staging::ros-humble-nav2-msgs
|
||||||
|
- robostack-staging::ros-humble-cv-bridge
|
||||||
|
- robostack-staging::ros-humble-vision-opencv
|
||||||
|
- robostack-staging::ros-humble-tf-transformations
|
||||||
|
- robostack-staging::ros-humble-moveit-msgs
|
||||||
|
- robostack-staging::ros-humble-tf2-ros
|
||||||
|
- robostack-staging::ros-humble-tf2-ros-py
|
||||||
|
- conda-forge::transforms3d
|
||||||
|
- conda-forge::uv
|
||||||
|
|
||||||
|
# UniLabOS custom messages
|
||||||
|
- uni-lab::ros-humble-unilabos-msgs
|
||||||
|
|
||||||
|
about:
|
||||||
|
repository: https://github.com/deepmodeling/Uni-Lab-OS
|
||||||
|
license: GPL-3.0-only
|
||||||
|
description: "UniLabOS Environment - ROS2 and conda dependencies"
|
||||||
42
.conda/full/recipe.yaml
Normal file
42
.conda/full/recipe.yaml
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
# unilabos-full: Full package with all features
|
||||||
|
# Depends on unilabos + complete ROS2 desktop + dev tools
|
||||||
|
|
||||||
|
package:
|
||||||
|
name: unilabos-full
|
||||||
|
version: 0.10.18
|
||||||
|
|
||||||
|
build:
|
||||||
|
noarch: generic
|
||||||
|
|
||||||
|
requirements:
|
||||||
|
run:
|
||||||
|
# Base unilabos package (includes unilabos-env)
|
||||||
|
- uni-lab::unilabos ==0.10.18
|
||||||
|
# Documentation tools
|
||||||
|
- sphinx
|
||||||
|
- sphinx_rtd_theme
|
||||||
|
# Web UI
|
||||||
|
- gradio
|
||||||
|
- flask
|
||||||
|
# Interactive development
|
||||||
|
- ipython
|
||||||
|
- jupyter
|
||||||
|
- jupyros
|
||||||
|
- colcon-common-extensions
|
||||||
|
# ROS2 full desktop (includes rviz2, gazebo, etc.)
|
||||||
|
- robostack-staging::ros-humble-desktop-full
|
||||||
|
# Navigation and motion control
|
||||||
|
- ros-humble-navigation2
|
||||||
|
- ros-humble-ros2-control
|
||||||
|
- ros-humble-robot-state-publisher
|
||||||
|
- ros-humble-joint-state-publisher
|
||||||
|
# MoveIt motion planning
|
||||||
|
- ros-humble-moveit
|
||||||
|
- ros-humble-moveit-servo
|
||||||
|
# Simulation
|
||||||
|
- ros-humble-simulation
|
||||||
|
|
||||||
|
about:
|
||||||
|
repository: https://github.com/deepmodeling/Uni-Lab-OS
|
||||||
|
license: GPL-3.0-only
|
||||||
|
description: "UniLabOS Full - Complete package with ROS2 Desktop, MoveIt, Navigation2, Gazebo, Jupyter"
|
||||||
@@ -1,91 +0,0 @@
|
|||||||
package:
|
|
||||||
name: unilabos
|
|
||||||
version: 0.10.15
|
|
||||||
|
|
||||||
source:
|
|
||||||
path: ../unilabos
|
|
||||||
target_directory: unilabos
|
|
||||||
|
|
||||||
build:
|
|
||||||
python:
|
|
||||||
entry_points:
|
|
||||||
- unilab = unilabos.app.main:main
|
|
||||||
script:
|
|
||||||
- set PIP_NO_INDEX=
|
|
||||||
- if: win
|
|
||||||
then:
|
|
||||||
- copy %RECIPE_DIR%\..\MANIFEST.in %SRC_DIR%
|
|
||||||
- copy %RECIPE_DIR%\..\setup.cfg %SRC_DIR%
|
|
||||||
- copy %RECIPE_DIR%\..\setup.py %SRC_DIR%
|
|
||||||
- call %PYTHON% -m pip install %SRC_DIR%
|
|
||||||
- if: unix
|
|
||||||
then:
|
|
||||||
- cp $RECIPE_DIR/../MANIFEST.in $SRC_DIR
|
|
||||||
- cp $RECIPE_DIR/../setup.cfg $SRC_DIR
|
|
||||||
- cp $RECIPE_DIR/../setup.py $SRC_DIR
|
|
||||||
- $PYTHON -m pip install $SRC_DIR
|
|
||||||
|
|
||||||
requirements:
|
|
||||||
host:
|
|
||||||
- python ==3.11.11
|
|
||||||
- pip
|
|
||||||
- setuptools
|
|
||||||
- zstd
|
|
||||||
- zstandard
|
|
||||||
run:
|
|
||||||
- conda-forge::python ==3.11.11
|
|
||||||
- compilers
|
|
||||||
- cmake
|
|
||||||
- zstd
|
|
||||||
- zstandard
|
|
||||||
- ninja
|
|
||||||
- if: unix
|
|
||||||
then:
|
|
||||||
- make
|
|
||||||
- sphinx
|
|
||||||
- sphinx_rtd_theme
|
|
||||||
- numpy
|
|
||||||
- scipy
|
|
||||||
- pandas
|
|
||||||
- networkx
|
|
||||||
- matplotlib
|
|
||||||
- pint
|
|
||||||
- pyserial
|
|
||||||
- pyusb
|
|
||||||
- pylibftdi
|
|
||||||
- pymodbus
|
|
||||||
- python-can
|
|
||||||
- pyvisa
|
|
||||||
- opencv
|
|
||||||
- pydantic
|
|
||||||
- fastapi
|
|
||||||
- uvicorn
|
|
||||||
- gradio
|
|
||||||
- flask
|
|
||||||
- websockets
|
|
||||||
- ipython
|
|
||||||
- jupyter
|
|
||||||
- jupyros
|
|
||||||
- colcon-common-extensions
|
|
||||||
- robostack-staging::ros-humble-desktop-full
|
|
||||||
- robostack-staging::ros-humble-control-msgs
|
|
||||||
- robostack-staging::ros-humble-sensor-msgs
|
|
||||||
- robostack-staging::ros-humble-trajectory-msgs
|
|
||||||
- ros-humble-navigation2
|
|
||||||
- ros-humble-ros2-control
|
|
||||||
- ros-humble-robot-state-publisher
|
|
||||||
- ros-humble-joint-state-publisher
|
|
||||||
- ros-humble-rosbridge-server
|
|
||||||
- ros-humble-cv-bridge
|
|
||||||
- ros-humble-tf2
|
|
||||||
- ros-humble-moveit
|
|
||||||
- ros-humble-moveit-servo
|
|
||||||
- ros-humble-simulation
|
|
||||||
- ros-humble-tf-transformations
|
|
||||||
- transforms3d
|
|
||||||
- uni-lab::ros-humble-unilabos-msgs
|
|
||||||
|
|
||||||
about:
|
|
||||||
repository: https://github.com/deepmodeling/Uni-Lab-OS
|
|
||||||
license: GPL-3.0-only
|
|
||||||
description: "Uni-Lab-OS"
|
|
||||||
328
.cursor/rules/device-drivers.mdc
Normal file
328
.cursor/rules/device-drivers.mdc
Normal file
@@ -0,0 +1,328 @@
|
|||||||
|
---
|
||||||
|
description: 设备驱动开发规范
|
||||||
|
globs: ["unilabos/devices/**/*.py"]
|
||||||
|
---
|
||||||
|
|
||||||
|
# 设备驱动开发规范
|
||||||
|
|
||||||
|
## 目录结构
|
||||||
|
|
||||||
|
```
|
||||||
|
unilabos/devices/
|
||||||
|
├── virtual/ # 虚拟设备(用于测试)
|
||||||
|
│ ├── virtual_stirrer.py
|
||||||
|
│ └── virtual_centrifuge.py
|
||||||
|
├── liquid_handling/ # 液体处理设备
|
||||||
|
├── balance/ # 天平设备
|
||||||
|
├── hplc/ # HPLC设备
|
||||||
|
├── pump_and_valve/ # 泵和阀门
|
||||||
|
├── temperature/ # 温度控制设备
|
||||||
|
├── workstation/ # 工作站(组合设备)
|
||||||
|
└── ...
|
||||||
|
```
|
||||||
|
|
||||||
|
## 设备类完整模板
|
||||||
|
|
||||||
|
```python
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
import time as time_module
|
||||||
|
from typing import Dict, Any, Optional
|
||||||
|
|
||||||
|
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
|
||||||
|
|
||||||
|
|
||||||
|
class MyDevice:
|
||||||
|
"""
|
||||||
|
设备类描述
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
device_id: 设备唯一标识
|
||||||
|
config: 设备配置字典
|
||||||
|
data: 设备状态数据
|
||||||
|
"""
|
||||||
|
|
||||||
|
_ros_node: BaseROS2DeviceNode
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
device_id: str = None,
|
||||||
|
config: Dict[str, Any] = None,
|
||||||
|
**kwargs
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
初始化设备
|
||||||
|
|
||||||
|
Args:
|
||||||
|
device_id: 设备ID
|
||||||
|
config: 配置字典
|
||||||
|
**kwargs: 其他参数
|
||||||
|
"""
|
||||||
|
# 兼容不同调用方式
|
||||||
|
if device_id is None and 'id' in kwargs:
|
||||||
|
device_id = kwargs.pop('id')
|
||||||
|
if config is None and 'config' in kwargs:
|
||||||
|
config = kwargs.pop('config')
|
||||||
|
|
||||||
|
self.device_id = device_id or "unknown_device"
|
||||||
|
self.config = config or {}
|
||||||
|
self.data = {}
|
||||||
|
|
||||||
|
# 从config读取参数
|
||||||
|
self.port = self.config.get('port') or kwargs.get('port', 'COM1')
|
||||||
|
self._max_value = self.config.get('max_value', 1000.0)
|
||||||
|
|
||||||
|
# 初始化日志
|
||||||
|
self.logger = logging.getLogger(f"MyDevice.{self.device_id}")
|
||||||
|
|
||||||
|
self.logger.info(f"设备 {self.device_id} 已创建")
|
||||||
|
|
||||||
|
def post_init(self, ros_node: BaseROS2DeviceNode):
|
||||||
|
"""
|
||||||
|
ROS节点注入 - 在ROS节点创建后调用
|
||||||
|
|
||||||
|
Args:
|
||||||
|
ros_node: ROS2设备节点实例
|
||||||
|
"""
|
||||||
|
self._ros_node = ros_node
|
||||||
|
|
||||||
|
async def initialize(self) -> bool:
|
||||||
|
"""
|
||||||
|
初始化设备 - 连接硬件、设置初始状态
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: 初始化是否成功
|
||||||
|
"""
|
||||||
|
self.logger.info(f"初始化设备 {self.device_id}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 执行硬件初始化
|
||||||
|
# await self._connect_hardware()
|
||||||
|
|
||||||
|
# 设置初始状态
|
||||||
|
self.data.update({
|
||||||
|
"status": "待机",
|
||||||
|
"is_running": False,
|
||||||
|
"current_value": 0.0,
|
||||||
|
})
|
||||||
|
|
||||||
|
self.logger.info(f"设备 {self.device_id} 初始化完成")
|
||||||
|
return True
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error(f"初始化失败: {e}")
|
||||||
|
self.data["status"] = f"错误: {e}"
|
||||||
|
return False
|
||||||
|
|
||||||
|
async def cleanup(self) -> bool:
|
||||||
|
"""
|
||||||
|
清理设备 - 断开连接、释放资源
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: 清理是否成功
|
||||||
|
"""
|
||||||
|
self.logger.info(f"清理设备 {self.device_id}")
|
||||||
|
|
||||||
|
self.data.update({
|
||||||
|
"status": "离线",
|
||||||
|
"is_running": False,
|
||||||
|
})
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
# ==================== 设备动作 ====================
|
||||||
|
|
||||||
|
async def execute_action(
|
||||||
|
self,
|
||||||
|
param1: float,
|
||||||
|
param2: str = "",
|
||||||
|
**kwargs
|
||||||
|
) -> bool:
|
||||||
|
"""
|
||||||
|
执行设备动作
|
||||||
|
|
||||||
|
Args:
|
||||||
|
param1: 参数1
|
||||||
|
param2: 参数2(可选)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: 动作是否成功
|
||||||
|
"""
|
||||||
|
# 类型转换和验证
|
||||||
|
try:
|
||||||
|
param1 = float(param1)
|
||||||
|
except (ValueError, TypeError) as e:
|
||||||
|
self.logger.error(f"参数类型错误: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
# 参数验证
|
||||||
|
if param1 > self._max_value:
|
||||||
|
self.logger.error(f"参数超出范围: {param1} > {self._max_value}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
self.logger.info(f"执行动作: param1={param1}, param2={param2}")
|
||||||
|
|
||||||
|
# 更新状态
|
||||||
|
self.data.update({
|
||||||
|
"status": "运行中",
|
||||||
|
"is_running": True,
|
||||||
|
})
|
||||||
|
|
||||||
|
# 执行动作(带进度反馈)
|
||||||
|
duration = 10.0 # 秒
|
||||||
|
start_time = time_module.time()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
elapsed = time_module.time() - start_time
|
||||||
|
remaining = max(0, duration - elapsed)
|
||||||
|
progress = min(100, (elapsed / duration) * 100)
|
||||||
|
|
||||||
|
self.data.update({
|
||||||
|
"status": f"运行中: {progress:.0f}%",
|
||||||
|
"remaining_time": remaining,
|
||||||
|
})
|
||||||
|
|
||||||
|
if remaining <= 0:
|
||||||
|
break
|
||||||
|
|
||||||
|
await self._ros_node.sleep(1.0)
|
||||||
|
|
||||||
|
# 完成
|
||||||
|
self.data.update({
|
||||||
|
"status": "完成",
|
||||||
|
"is_running": False,
|
||||||
|
})
|
||||||
|
|
||||||
|
self.logger.info("动作执行完成")
|
||||||
|
return True
|
||||||
|
|
||||||
|
# ==================== 状态属性 ====================
|
||||||
|
|
||||||
|
@property
|
||||||
|
def status(self) -> str:
|
||||||
|
"""设备状态 - 自动发布为ROS Topic"""
|
||||||
|
return self.data.get("status", "未知")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_running(self) -> bool:
|
||||||
|
"""是否正在运行"""
|
||||||
|
return self.data.get("is_running", False)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def current_value(self) -> float:
|
||||||
|
"""当前值"""
|
||||||
|
return self.data.get("current_value", 0.0)
|
||||||
|
|
||||||
|
# ==================== 辅助方法 ====================
|
||||||
|
|
||||||
|
def get_device_info(self) -> Dict[str, Any]:
|
||||||
|
"""获取设备信息"""
|
||||||
|
return {
|
||||||
|
"device_id": self.device_id,
|
||||||
|
"status": self.status,
|
||||||
|
"is_running": self.is_running,
|
||||||
|
"current_value": self.current_value,
|
||||||
|
}
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return f"MyDevice({self.device_id}: {self.status})"
|
||||||
|
```
|
||||||
|
|
||||||
|
## 关键规则
|
||||||
|
|
||||||
|
### 1. 参数处理
|
||||||
|
|
||||||
|
所有动作方法的参数都可能以字符串形式传入,必须进行类型转换:
|
||||||
|
|
||||||
|
```python
|
||||||
|
async def my_action(self, value: float, **kwargs) -> bool:
|
||||||
|
# 始终进行类型转换
|
||||||
|
try:
|
||||||
|
value = float(value)
|
||||||
|
except (ValueError, TypeError) as e:
|
||||||
|
self.logger.error(f"参数类型错误: {e}")
|
||||||
|
return False
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. vessel 参数处理
|
||||||
|
|
||||||
|
vessel 参数可能是字符串ID或字典:
|
||||||
|
|
||||||
|
```python
|
||||||
|
def extract_vessel_id(vessel: Union[str, dict]) -> str:
|
||||||
|
if isinstance(vessel, dict):
|
||||||
|
return vessel.get("id", "")
|
||||||
|
return str(vessel) if vessel else ""
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. 状态更新
|
||||||
|
|
||||||
|
使用 `self.data` 字典存储状态,属性读取状态:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 更新状态
|
||||||
|
self.data["status"] = "运行中"
|
||||||
|
self.data["current_speed"] = 300.0
|
||||||
|
|
||||||
|
# 读取状态(通过属性)
|
||||||
|
@property
|
||||||
|
def status(self) -> str:
|
||||||
|
return self.data.get("status", "待机")
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. 异步等待
|
||||||
|
|
||||||
|
使用 ROS 节点的 sleep 方法:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 正确
|
||||||
|
await self._ros_node.sleep(1.0)
|
||||||
|
|
||||||
|
# 避免(除非在纯 Python 测试环境)
|
||||||
|
await asyncio.sleep(1.0)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5. 进度反馈
|
||||||
|
|
||||||
|
长时间运行的操作需要提供进度反馈:
|
||||||
|
|
||||||
|
```python
|
||||||
|
while remaining > 0:
|
||||||
|
progress = (elapsed / total_time) * 100
|
||||||
|
self.data["status"] = f"运行中: {progress:.0f}%"
|
||||||
|
self.data["remaining_time"] = remaining
|
||||||
|
|
||||||
|
await self._ros_node.sleep(1.0)
|
||||||
|
```
|
||||||
|
|
||||||
|
## 虚拟设备
|
||||||
|
|
||||||
|
虚拟设备用于测试和演示,放在 `unilabos/devices/virtual/` 目录:
|
||||||
|
|
||||||
|
- 类名以 `Virtual` 开头
|
||||||
|
- 文件名以 `virtual_` 开头
|
||||||
|
- 模拟真实设备的行为和时序
|
||||||
|
- 使用表情符号增强日志可读性(可选)
|
||||||
|
|
||||||
|
## 工作站设备
|
||||||
|
|
||||||
|
工作站是组合多个设备的复杂设备:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.devices.workstation.workstation_base import WorkstationBase
|
||||||
|
|
||||||
|
class MyWorkstation(WorkstationBase):
|
||||||
|
"""组合工作站"""
|
||||||
|
|
||||||
|
async def execute_workflow(self, workflow: Dict[str, Any]) -> bool:
|
||||||
|
"""执行工作流"""
|
||||||
|
pass
|
||||||
|
```
|
||||||
|
|
||||||
|
## 设备注册
|
||||||
|
|
||||||
|
设备类开发完成后,需要在注册表中注册:
|
||||||
|
|
||||||
|
1. 创建/编辑 `unilabos/registry/devices/my_category.yaml`
|
||||||
|
2. 添加设备配置(参考 `virtual_device.yaml`)
|
||||||
|
3. 运行 `--complete_registry` 自动生成 schema
|
||||||
240
.cursor/rules/protocol-development.mdc
Normal file
240
.cursor/rules/protocol-development.mdc
Normal file
@@ -0,0 +1,240 @@
|
|||||||
|
---
|
||||||
|
description: 协议编译器开发规范
|
||||||
|
globs: ["unilabos/compile/**/*.py"]
|
||||||
|
---
|
||||||
|
|
||||||
|
# 协议编译器开发规范
|
||||||
|
|
||||||
|
## 概述
|
||||||
|
|
||||||
|
协议编译器负责将高级实验操作(如 Stir、Add、Filter)编译为设备可执行的动作序列。
|
||||||
|
|
||||||
|
## 文件命名
|
||||||
|
|
||||||
|
- 位置: `unilabos/compile/`
|
||||||
|
- 命名: `{operation}_protocol.py`
|
||||||
|
- 示例: `stir_protocol.py`, `add_protocol.py`, `filter_protocol.py`
|
||||||
|
|
||||||
|
## 协议函数模板
|
||||||
|
|
||||||
|
```python
|
||||||
|
from typing import List, Dict, Any, Union
|
||||||
|
import networkx as nx
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from .utils.unit_parser import parse_time_input
|
||||||
|
from .utils.vessel_parser import extract_vessel_id
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def generate_{operation}_protocol(
|
||||||
|
G: nx.DiGraph,
|
||||||
|
vessel: Union[str, dict],
|
||||||
|
param1: Union[str, float] = "0",
|
||||||
|
param2: float = 0.0,
|
||||||
|
**kwargs
|
||||||
|
) -> List[Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
生成{操作}协议序列
|
||||||
|
|
||||||
|
Args:
|
||||||
|
G: 物理拓扑图 (NetworkX DiGraph)
|
||||||
|
vessel: 容器ID或Resource字典
|
||||||
|
param1: 参数1(支持字符串单位,如 "5 min")
|
||||||
|
param2: 参数2
|
||||||
|
**kwargs: 其他参数
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Dict]: 动作序列
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: 参数无效时
|
||||||
|
"""
|
||||||
|
# 1. 提取 vessel_id
|
||||||
|
vessel_id = extract_vessel_id(vessel)
|
||||||
|
|
||||||
|
# 2. 验证参数
|
||||||
|
if not vessel_id:
|
||||||
|
raise ValueError("vessel 参数不能为空")
|
||||||
|
|
||||||
|
if vessel_id not in G.nodes():
|
||||||
|
raise ValueError(f"容器 '{vessel_id}' 不存在于系统中")
|
||||||
|
|
||||||
|
# 3. 解析参数(支持单位)
|
||||||
|
parsed_param1 = parse_time_input(param1) # "5 min" -> 300.0
|
||||||
|
|
||||||
|
# 4. 查找设备
|
||||||
|
device_id = find_connected_device(G, vessel_id, device_type="my_device")
|
||||||
|
|
||||||
|
# 5. 生成动作序列
|
||||||
|
action_sequence = []
|
||||||
|
|
||||||
|
action = {
|
||||||
|
"device_id": device_id,
|
||||||
|
"action_name": "my_action",
|
||||||
|
"action_kwargs": {
|
||||||
|
"vessel": {"id": vessel_id}, # 始终使用字典格式
|
||||||
|
"param1": float(parsed_param1),
|
||||||
|
"param2": float(param2),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
action_sequence.append(action)
|
||||||
|
|
||||||
|
logger.info(f"生成协议: {len(action_sequence)} 个动作")
|
||||||
|
return action_sequence
|
||||||
|
|
||||||
|
|
||||||
|
def find_connected_device(
|
||||||
|
G: nx.DiGraph,
|
||||||
|
vessel_id: str,
|
||||||
|
device_type: str = ""
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
查找与容器相连的设备
|
||||||
|
|
||||||
|
Args:
|
||||||
|
G: 拓扑图
|
||||||
|
vessel_id: 容器ID
|
||||||
|
device_type: 设备类型关键字
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: 设备ID
|
||||||
|
"""
|
||||||
|
# 查找所有匹配类型的设备
|
||||||
|
device_nodes = []
|
||||||
|
for node in G.nodes():
|
||||||
|
node_class = G.nodes[node].get('class', '') or ''
|
||||||
|
if device_type.lower() in node_class.lower():
|
||||||
|
device_nodes.append(node)
|
||||||
|
|
||||||
|
# 检查连接
|
||||||
|
if vessel_id and device_nodes:
|
||||||
|
for device in device_nodes:
|
||||||
|
if G.has_edge(device, vessel_id) or G.has_edge(vessel_id, device):
|
||||||
|
return device
|
||||||
|
|
||||||
|
# 返回第一个可用设备
|
||||||
|
if device_nodes:
|
||||||
|
return device_nodes[0]
|
||||||
|
|
||||||
|
# 默认设备
|
||||||
|
return f"{device_type}_1"
|
||||||
|
```
|
||||||
|
|
||||||
|
## 关键规则
|
||||||
|
|
||||||
|
### 1. vessel 参数处理
|
||||||
|
|
||||||
|
vessel 参数可能是字符串或字典,需要统一处理:
|
||||||
|
|
||||||
|
```python
|
||||||
|
def extract_vessel_id(vessel: Union[str, dict]) -> str:
|
||||||
|
"""提取vessel_id"""
|
||||||
|
if isinstance(vessel, dict):
|
||||||
|
# 可能是 {"id": "xxx"} 或完整 Resource 对象
|
||||||
|
return vessel.get("id", list(vessel.values())[0].get("id", ""))
|
||||||
|
return str(vessel) if vessel else ""
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. action_kwargs 中的 vessel
|
||||||
|
|
||||||
|
始终使用 `{"id": vessel_id}` 格式传递 vessel:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 正确
|
||||||
|
"action_kwargs": {
|
||||||
|
"vessel": {"id": vessel_id}, # 字符串ID包装为字典
|
||||||
|
}
|
||||||
|
|
||||||
|
# 避免
|
||||||
|
"action_kwargs": {
|
||||||
|
"vessel": vessel_resource, # 不要传递完整 Resource 对象
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. 单位解析
|
||||||
|
|
||||||
|
使用 `parse_time_input` 解析时间参数:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from .utils.unit_parser import parse_time_input
|
||||||
|
|
||||||
|
# 支持格式: "5 min", "1 h", "300", "1.5 hours"
|
||||||
|
time_seconds = parse_time_input("5 min") # -> 300.0
|
||||||
|
time_seconds = parse_time_input(120) # -> 120.0
|
||||||
|
time_seconds = parse_time_input("1 h") # -> 3600.0
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. 参数验证
|
||||||
|
|
||||||
|
所有参数必须进行验证和类型转换:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 验证范围
|
||||||
|
if speed < 10.0 or speed > 1500.0:
|
||||||
|
logger.warning(f"速度 {speed} 超出范围,修正为 300")
|
||||||
|
speed = 300.0
|
||||||
|
|
||||||
|
# 类型转换
|
||||||
|
param = float(param) if not isinstance(param, (int, float)) else param
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5. 日志记录
|
||||||
|
|
||||||
|
使用项目日志记录器:
|
||||||
|
|
||||||
|
```python
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
def generate_protocol(...):
|
||||||
|
logger.info(f"开始生成协议...")
|
||||||
|
logger.debug(f"参数: vessel={vessel_id}, time={time}")
|
||||||
|
logger.warning(f"参数修正: {old_value} -> {new_value}")
|
||||||
|
```
|
||||||
|
|
||||||
|
## 便捷函数
|
||||||
|
|
||||||
|
为常用操作提供便捷函数:
|
||||||
|
|
||||||
|
```python
|
||||||
|
def stir_briefly(G: nx.DiGraph, vessel: Union[str, dict],
|
||||||
|
speed: float = 300.0) -> List[Dict[str, Any]]:
|
||||||
|
"""短时间搅拌(30秒)"""
|
||||||
|
return generate_stir_protocol(G, vessel, time="30", stir_speed=speed)
|
||||||
|
|
||||||
|
def stir_vigorously(G: nx.DiGraph, vessel: Union[str, dict],
|
||||||
|
time: str = "5 min") -> List[Dict[str, Any]]:
|
||||||
|
"""剧烈搅拌"""
|
||||||
|
return generate_stir_protocol(G, vessel, time=time, stir_speed=800.0)
|
||||||
|
```
|
||||||
|
|
||||||
|
## 测试函数
|
||||||
|
|
||||||
|
每个协议文件应包含测试函数:
|
||||||
|
|
||||||
|
```python
|
||||||
|
def test_{operation}_protocol():
|
||||||
|
"""测试协议生成"""
|
||||||
|
# 测试参数处理
|
||||||
|
vessel_dict = {"id": "flask_1", "name": "反应瓶1"}
|
||||||
|
vessel_id = extract_vessel_id(vessel_dict)
|
||||||
|
assert vessel_id == "flask_1"
|
||||||
|
|
||||||
|
# 测试单位解析
|
||||||
|
time_s = parse_time_input("5 min")
|
||||||
|
assert time_s == 300.0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_{operation}_protocol()
|
||||||
|
```
|
||||||
|
|
||||||
|
## 现有协议参考
|
||||||
|
|
||||||
|
- `stir_protocol.py` - 搅拌操作
|
||||||
|
- `add_protocol.py` - 添加物料
|
||||||
|
- `filter_protocol.py` - 过滤操作
|
||||||
|
- `heatchill_protocol.py` - 加热/冷却
|
||||||
|
- `separate_protocol.py` - 分离操作
|
||||||
|
- `evaporate_protocol.py` - 蒸发操作
|
||||||
319
.cursor/rules/registry-config.mdc
Normal file
319
.cursor/rules/registry-config.mdc
Normal file
@@ -0,0 +1,319 @@
|
|||||||
|
---
|
||||||
|
description: 注册表配置规范 (YAML)
|
||||||
|
globs: ["unilabos/registry/**/*.yaml"]
|
||||||
|
---
|
||||||
|
|
||||||
|
# 注册表配置规范
|
||||||
|
|
||||||
|
## 概述
|
||||||
|
|
||||||
|
注册表使用 YAML 格式定义设备和资源类型,是 Uni-Lab-OS 的核心配置系统。
|
||||||
|
|
||||||
|
## 目录结构
|
||||||
|
|
||||||
|
```
|
||||||
|
unilabos/registry/
|
||||||
|
├── devices/ # 设备类型注册
|
||||||
|
│ ├── virtual_device.yaml
|
||||||
|
│ ├── liquid_handler.yaml
|
||||||
|
│ └── ...
|
||||||
|
├── device_comms/ # 通信设备配置
|
||||||
|
│ ├── communication_devices.yaml
|
||||||
|
│ └── modbus_ioboard.yaml
|
||||||
|
└── resources/ # 资源类型注册
|
||||||
|
├── bioyond/
|
||||||
|
├── organic/
|
||||||
|
├── opentrons/
|
||||||
|
└── ...
|
||||||
|
```
|
||||||
|
|
||||||
|
## 设备注册表格式
|
||||||
|
|
||||||
|
### 基本结构
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
device_type_id:
|
||||||
|
# 基本信息
|
||||||
|
description: "设备描述"
|
||||||
|
version: "1.0.0"
|
||||||
|
category:
|
||||||
|
- category_name
|
||||||
|
icon: "icon_device.webp"
|
||||||
|
|
||||||
|
# 类配置
|
||||||
|
class:
|
||||||
|
module: "unilabos.devices.my_module:MyClass"
|
||||||
|
type: python
|
||||||
|
|
||||||
|
# 状态类型(属性 -> ROS消息类型)
|
||||||
|
status_types:
|
||||||
|
status: String
|
||||||
|
temperature: Float64
|
||||||
|
is_running: Bool
|
||||||
|
|
||||||
|
# 动作映射
|
||||||
|
action_value_mappings:
|
||||||
|
action_name:
|
||||||
|
type: UniLabJsonCommand # 或 UniLabJsonCommandAsync
|
||||||
|
goal: {}
|
||||||
|
feedback: {}
|
||||||
|
result: {}
|
||||||
|
schema: {...}
|
||||||
|
handles: {}
|
||||||
|
```
|
||||||
|
|
||||||
|
### action_value_mappings 详细格式
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
action_value_mappings:
|
||||||
|
# 同步动作
|
||||||
|
my_sync_action:
|
||||||
|
type: UniLabJsonCommand
|
||||||
|
goal:
|
||||||
|
param1: param1
|
||||||
|
param2: param2
|
||||||
|
feedback: {}
|
||||||
|
result:
|
||||||
|
success: success
|
||||||
|
message: message
|
||||||
|
goal_default:
|
||||||
|
param1: 0.0
|
||||||
|
param2: ""
|
||||||
|
handles: {}
|
||||||
|
placeholder_keys:
|
||||||
|
device_param: unilabos_devices # 设备选择器
|
||||||
|
resource_param: unilabos_resources # 资源选择器
|
||||||
|
schema:
|
||||||
|
title: "动作名称参数"
|
||||||
|
description: "动作描述"
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
goal:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
param1:
|
||||||
|
type: number
|
||||||
|
param2:
|
||||||
|
type: string
|
||||||
|
required:
|
||||||
|
- param1
|
||||||
|
feedback: {}
|
||||||
|
result:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
success:
|
||||||
|
type: boolean
|
||||||
|
message:
|
||||||
|
type: string
|
||||||
|
required:
|
||||||
|
- goal
|
||||||
|
|
||||||
|
# 异步动作
|
||||||
|
my_async_action:
|
||||||
|
type: UniLabJsonCommandAsync
|
||||||
|
goal: {}
|
||||||
|
feedback:
|
||||||
|
progress: progress
|
||||||
|
current_status: status
|
||||||
|
result:
|
||||||
|
success: success
|
||||||
|
schema: {...}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 自动生成的动作
|
||||||
|
|
||||||
|
以 `auto-` 开头的动作由系统自动生成:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
action_value_mappings:
|
||||||
|
auto-initialize:
|
||||||
|
type: UniLabJsonCommandAsync
|
||||||
|
goal: {}
|
||||||
|
feedback: {}
|
||||||
|
result: {}
|
||||||
|
schema: {...}
|
||||||
|
|
||||||
|
auto-cleanup:
|
||||||
|
type: UniLabJsonCommandAsync
|
||||||
|
goal: {}
|
||||||
|
feedback: {}
|
||||||
|
result: {}
|
||||||
|
schema: {...}
|
||||||
|
```
|
||||||
|
|
||||||
|
### handles 配置
|
||||||
|
|
||||||
|
用于工作流编辑器中的数据流连接:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
handles:
|
||||||
|
input:
|
||||||
|
- handler_key: "input_resource"
|
||||||
|
data_type: "resource"
|
||||||
|
label: "输入资源"
|
||||||
|
data_source: "handle"
|
||||||
|
data_key: "resources"
|
||||||
|
output:
|
||||||
|
- handler_key: "output_labware"
|
||||||
|
data_type: "resource"
|
||||||
|
label: "输出器皿"
|
||||||
|
data_source: "executor"
|
||||||
|
data_key: "created_resource.@flatten"
|
||||||
|
```
|
||||||
|
|
||||||
|
## 资源注册表格式
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
resource_type_id:
|
||||||
|
description: "资源描述"
|
||||||
|
version: "1.0.0"
|
||||||
|
category:
|
||||||
|
- category_name
|
||||||
|
icon: ""
|
||||||
|
handles: []
|
||||||
|
init_param_schema: {}
|
||||||
|
|
||||||
|
class:
|
||||||
|
module: "unilabos.resources.my_module:MyResource"
|
||||||
|
type: pylabrobot # 或 python
|
||||||
|
```
|
||||||
|
|
||||||
|
### PyLabRobot 资源示例
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
BIOYOND_Electrolyte_6VialCarrier:
|
||||||
|
category:
|
||||||
|
- bottle_carriers
|
||||||
|
- bioyond
|
||||||
|
class:
|
||||||
|
module: "unilabos.resources.bioyond.bottle_carriers:BIOYOND_Electrolyte_6VialCarrier"
|
||||||
|
type: pylabrobot
|
||||||
|
version: "1.0.0"
|
||||||
|
```
|
||||||
|
|
||||||
|
## 状态类型映射
|
||||||
|
|
||||||
|
Python 类型到 ROS 消息类型的映射:
|
||||||
|
|
||||||
|
| Python 类型 | ROS 消息类型 |
|
||||||
|
|------------|-------------|
|
||||||
|
| `str` | `String` |
|
||||||
|
| `bool` | `Bool` |
|
||||||
|
| `int` | `Int64` |
|
||||||
|
| `float` | `Float64` |
|
||||||
|
| `list` | `String` (序列化) |
|
||||||
|
| `dict` | `String` (序列化) |
|
||||||
|
|
||||||
|
## 自动完善注册表
|
||||||
|
|
||||||
|
使用 `--complete_registry` 参数自动生成 schema:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python -m unilabos.app.main --complete_registry
|
||||||
|
```
|
||||||
|
|
||||||
|
这会:
|
||||||
|
1. 扫描设备类的方法签名
|
||||||
|
2. 自动生成 `auto-` 前缀的动作
|
||||||
|
3. 生成 JSON Schema
|
||||||
|
4. 更新 YAML 文件
|
||||||
|
|
||||||
|
## 验证规则
|
||||||
|
|
||||||
|
1. **device_type_id** 必须唯一
|
||||||
|
2. **module** 路径必须正确可导入
|
||||||
|
3. **status_types** 的类型必须是有效的 ROS 消息类型
|
||||||
|
4. **schema** 必须是有效的 JSON Schema
|
||||||
|
|
||||||
|
## 示例:完整设备配置
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
virtual_stirrer:
|
||||||
|
category:
|
||||||
|
- virtual_device
|
||||||
|
description: "虚拟搅拌器设备"
|
||||||
|
version: "1.0.0"
|
||||||
|
icon: "icon_stirrer.webp"
|
||||||
|
handles: []
|
||||||
|
init_param_schema: {}
|
||||||
|
|
||||||
|
class:
|
||||||
|
module: "unilabos.devices.virtual.virtual_stirrer:VirtualStirrer"
|
||||||
|
type: python
|
||||||
|
|
||||||
|
status_types:
|
||||||
|
status: String
|
||||||
|
operation_mode: String
|
||||||
|
current_speed: Float64
|
||||||
|
is_stirring: Bool
|
||||||
|
remaining_time: Float64
|
||||||
|
|
||||||
|
action_value_mappings:
|
||||||
|
auto-initialize:
|
||||||
|
type: UniLabJsonCommandAsync
|
||||||
|
goal: {}
|
||||||
|
feedback: {}
|
||||||
|
result: {}
|
||||||
|
schema:
|
||||||
|
title: "initialize参数"
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
goal:
|
||||||
|
type: object
|
||||||
|
properties: {}
|
||||||
|
feedback: {}
|
||||||
|
result: {}
|
||||||
|
required:
|
||||||
|
- goal
|
||||||
|
|
||||||
|
stir:
|
||||||
|
type: UniLabJsonCommandAsync
|
||||||
|
goal:
|
||||||
|
stir_time: stir_time
|
||||||
|
stir_speed: stir_speed
|
||||||
|
settling_time: settling_time
|
||||||
|
feedback:
|
||||||
|
current_speed: current_speed
|
||||||
|
remaining_time: remaining_time
|
||||||
|
result:
|
||||||
|
success: success
|
||||||
|
goal_default:
|
||||||
|
stir_time: 60.0
|
||||||
|
stir_speed: 300.0
|
||||||
|
settling_time: 30.0
|
||||||
|
handles: {}
|
||||||
|
schema:
|
||||||
|
title: "stir参数"
|
||||||
|
description: "搅拌操作"
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
goal:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
stir_time:
|
||||||
|
type: number
|
||||||
|
description: "搅拌时间(秒)"
|
||||||
|
stir_speed:
|
||||||
|
type: number
|
||||||
|
description: "搅拌速度(RPM)"
|
||||||
|
settling_time:
|
||||||
|
type: number
|
||||||
|
description: "沉降时间(秒)"
|
||||||
|
required:
|
||||||
|
- stir_time
|
||||||
|
- stir_speed
|
||||||
|
feedback:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
current_speed:
|
||||||
|
type: number
|
||||||
|
remaining_time:
|
||||||
|
type: number
|
||||||
|
result:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
success:
|
||||||
|
type: boolean
|
||||||
|
required:
|
||||||
|
- goal
|
||||||
|
```
|
||||||
233
.cursor/rules/ros-integration.mdc
Normal file
233
.cursor/rules/ros-integration.mdc
Normal file
@@ -0,0 +1,233 @@
|
|||||||
|
---
|
||||||
|
description: ROS 2 集成开发规范
|
||||||
|
globs: ["unilabos/ros/**/*.py", "**/*_node.py"]
|
||||||
|
---
|
||||||
|
|
||||||
|
# ROS 2 集成开发规范
|
||||||
|
|
||||||
|
## 概述
|
||||||
|
|
||||||
|
Uni-Lab-OS 使用 ROS 2 作为设备通信中间件,基于 rclpy 实现。
|
||||||
|
|
||||||
|
## 核心组件
|
||||||
|
|
||||||
|
### BaseROS2DeviceNode
|
||||||
|
|
||||||
|
设备节点基类,提供:
|
||||||
|
- ROS Topic 自动发布(状态属性)
|
||||||
|
- Action Server 自动创建(设备动作)
|
||||||
|
- 资源管理服务
|
||||||
|
- 异步任务调度
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
|
||||||
|
```
|
||||||
|
|
||||||
|
### 消息转换器
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.ros.msgs.message_converter import (
|
||||||
|
convert_to_ros_msg,
|
||||||
|
convert_from_ros_msg_with_mapping,
|
||||||
|
msg_converter_manager,
|
||||||
|
ros_action_to_json_schema,
|
||||||
|
ros_message_to_json_schema,
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
## 设备与 ROS 集成
|
||||||
|
|
||||||
|
### post_init 方法
|
||||||
|
|
||||||
|
设备类必须实现 `post_init` 方法接收 ROS 节点:
|
||||||
|
|
||||||
|
```python
|
||||||
|
class MyDevice:
|
||||||
|
_ros_node: BaseROS2DeviceNode
|
||||||
|
|
||||||
|
def post_init(self, ros_node: BaseROS2DeviceNode):
|
||||||
|
"""ROS节点注入"""
|
||||||
|
self._ros_node = ros_node
|
||||||
|
```
|
||||||
|
|
||||||
|
### 状态属性发布
|
||||||
|
|
||||||
|
设备的 `@property` 属性会自动发布为 ROS Topic:
|
||||||
|
|
||||||
|
```python
|
||||||
|
class MyDevice:
|
||||||
|
@property
|
||||||
|
def temperature(self) -> float:
|
||||||
|
return self._temperature
|
||||||
|
|
||||||
|
# 自动发布到 /{namespace}/temperature Topic
|
||||||
|
```
|
||||||
|
|
||||||
|
### Topic 配置装饰器
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.utils.decorator import topic_config
|
||||||
|
|
||||||
|
class MyDevice:
|
||||||
|
@property
|
||||||
|
@topic_config(period=1.0, print_publish=False, qos=10)
|
||||||
|
def fast_data(self) -> float:
|
||||||
|
"""高频数据 - 每秒发布一次"""
|
||||||
|
return self._fast_data
|
||||||
|
|
||||||
|
@property
|
||||||
|
@topic_config(period=5.0)
|
||||||
|
def slow_data(self) -> str:
|
||||||
|
"""低频数据 - 每5秒发布一次"""
|
||||||
|
return self._slow_data
|
||||||
|
```
|
||||||
|
|
||||||
|
### 订阅装饰器
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.utils.decorator import subscribe
|
||||||
|
|
||||||
|
class MyDevice:
|
||||||
|
@subscribe(topic="/external/sensor_data", qos=10)
|
||||||
|
def on_sensor_data(self, msg):
|
||||||
|
"""订阅外部Topic"""
|
||||||
|
self._sensor_value = msg.data
|
||||||
|
```
|
||||||
|
|
||||||
|
## 异步操作
|
||||||
|
|
||||||
|
### 使用 ROS 节点睡眠
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 推荐:使用ROS节点的睡眠方法
|
||||||
|
await self._ros_node.sleep(1.0)
|
||||||
|
|
||||||
|
# 不推荐:直接使用asyncio(可能导致回调阻塞)
|
||||||
|
await asyncio.sleep(1.0)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 获取事件循环
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.ros.x.rclpyx import get_event_loop
|
||||||
|
|
||||||
|
loop = get_event_loop()
|
||||||
|
```
|
||||||
|
|
||||||
|
## 消息类型
|
||||||
|
|
||||||
|
### unilabos_msgs 包
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos_msgs.msg import Resource
|
||||||
|
from unilabos_msgs.srv import (
|
||||||
|
ResourceAdd,
|
||||||
|
ResourceDelete,
|
||||||
|
ResourceUpdate,
|
||||||
|
ResourceList,
|
||||||
|
SerialCommand,
|
||||||
|
)
|
||||||
|
from unilabos_msgs.action import SendCmd
|
||||||
|
```
|
||||||
|
|
||||||
|
### Resource 消息结构
|
||||||
|
|
||||||
|
```python
|
||||||
|
Resource:
|
||||||
|
id: str
|
||||||
|
name: str
|
||||||
|
category: str
|
||||||
|
type: str
|
||||||
|
parent: str
|
||||||
|
children: List[str]
|
||||||
|
config: str # JSON字符串
|
||||||
|
data: str # JSON字符串
|
||||||
|
sample_id: str
|
||||||
|
pose: Pose
|
||||||
|
```
|
||||||
|
|
||||||
|
## 日志适配器
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.utils.log import info, debug, warning, error, trace
|
||||||
|
|
||||||
|
class MyDevice:
|
||||||
|
def __init__(self):
|
||||||
|
# 创建设备专属日志器
|
||||||
|
self.logger = logging.getLogger(f"MyDevice.{self.device_id}")
|
||||||
|
```
|
||||||
|
|
||||||
|
ROSLoggerAdapter 同时向自定义日志和 ROS 日志发送消息。
|
||||||
|
|
||||||
|
## Action Server
|
||||||
|
|
||||||
|
设备动作自动创建为 ROS Action Server:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
# 在注册表中配置
|
||||||
|
action_value_mappings:
|
||||||
|
my_action:
|
||||||
|
type: UniLabJsonCommandAsync # 异步Action
|
||||||
|
goal: {...}
|
||||||
|
feedback: {...}
|
||||||
|
result: {...}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Action 类型
|
||||||
|
|
||||||
|
- **UniLabJsonCommand**: 同步动作
|
||||||
|
- **UniLabJsonCommandAsync**: 异步动作(支持feedback)
|
||||||
|
|
||||||
|
## 服务客户端
|
||||||
|
|
||||||
|
```python
|
||||||
|
from rclpy.client import Client
|
||||||
|
|
||||||
|
# 调用其他节点的服务
|
||||||
|
response = await self._ros_node.call_service(
|
||||||
|
service_name="/other_node/service",
|
||||||
|
request=MyServiceRequest(...)
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
## 命名空间
|
||||||
|
|
||||||
|
设备节点使用命名空间隔离:
|
||||||
|
|
||||||
|
```
|
||||||
|
/{device_id}/ # 设备命名空间
|
||||||
|
/{device_id}/status # 状态Topic
|
||||||
|
/{device_id}/temperature # 温度Topic
|
||||||
|
/{device_id}/my_action # 动作Server
|
||||||
|
```
|
||||||
|
|
||||||
|
## 调试
|
||||||
|
|
||||||
|
### 查看 Topic
|
||||||
|
|
||||||
|
```bash
|
||||||
|
ros2 topic list
|
||||||
|
ros2 topic echo /{device_id}/status
|
||||||
|
```
|
||||||
|
|
||||||
|
### 查看 Action
|
||||||
|
|
||||||
|
```bash
|
||||||
|
ros2 action list
|
||||||
|
ros2 action info /{device_id}/my_action
|
||||||
|
```
|
||||||
|
|
||||||
|
### 查看 Service
|
||||||
|
|
||||||
|
```bash
|
||||||
|
ros2 service list
|
||||||
|
ros2 service call /{device_id}/resource_list unilabos_msgs/srv/ResourceList
|
||||||
|
```
|
||||||
|
|
||||||
|
## 最佳实践
|
||||||
|
|
||||||
|
1. **状态属性命名**: 使用蛇形命名法(snake_case)
|
||||||
|
2. **Topic 频率**: 根据数据变化频率调整,避免过高频率
|
||||||
|
3. **Action 反馈**: 长时间操作提供进度反馈
|
||||||
|
4. **错误处理**: 使用 try-except 捕获并记录错误
|
||||||
|
5. **资源清理**: 在 cleanup 方法中正确清理资源
|
||||||
357
.cursor/rules/testing-patterns.mdc
Normal file
357
.cursor/rules/testing-patterns.mdc
Normal file
@@ -0,0 +1,357 @@
|
|||||||
|
---
|
||||||
|
description: 测试开发规范
|
||||||
|
globs: ["tests/**/*.py", "**/test_*.py"]
|
||||||
|
---
|
||||||
|
|
||||||
|
# 测试开发规范
|
||||||
|
|
||||||
|
## 目录结构
|
||||||
|
|
||||||
|
```
|
||||||
|
tests/
|
||||||
|
├── __init__.py
|
||||||
|
├── devices/ # 设备测试
|
||||||
|
│ └── liquid_handling/
|
||||||
|
│ └── test_transfer_liquid.py
|
||||||
|
├── resources/ # 资源测试
|
||||||
|
│ ├── test_bottle_carrier.py
|
||||||
|
│ └── test_resourcetreeset.py
|
||||||
|
├── ros/ # ROS消息测试
|
||||||
|
│ └── msgs/
|
||||||
|
│ ├── test_basic.py
|
||||||
|
│ ├── test_conversion.py
|
||||||
|
│ └── test_mapping.py
|
||||||
|
└── workflow/ # 工作流测试
|
||||||
|
└── merge_workflow.py
|
||||||
|
```
|
||||||
|
|
||||||
|
## 测试框架
|
||||||
|
|
||||||
|
使用 pytest 作为测试框架:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 运行所有测试
|
||||||
|
pytest tests/
|
||||||
|
|
||||||
|
# 运行特定测试文件
|
||||||
|
pytest tests/resources/test_bottle_carrier.py
|
||||||
|
|
||||||
|
# 运行特定测试函数
|
||||||
|
pytest tests/resources/test_bottle_carrier.py::test_bottle_carrier
|
||||||
|
|
||||||
|
# 显示详细输出
|
||||||
|
pytest -v tests/
|
||||||
|
|
||||||
|
# 显示打印输出
|
||||||
|
pytest -s tests/
|
||||||
|
```
|
||||||
|
|
||||||
|
## 测试文件模板
|
||||||
|
|
||||||
|
```python
|
||||||
|
import pytest
|
||||||
|
from typing import List, Dict, Any
|
||||||
|
|
||||||
|
# 导入被测试的模块
|
||||||
|
from unilabos.resources.bioyond.bottle_carriers import (
|
||||||
|
BIOYOND_Electrolyte_6VialCarrier,
|
||||||
|
)
|
||||||
|
from unilabos.resources.bioyond.bottles import (
|
||||||
|
BIOYOND_PolymerStation_Solid_Vial,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestBottleCarrier:
|
||||||
|
"""BottleCarrier 测试类"""
|
||||||
|
|
||||||
|
def setup_method(self):
|
||||||
|
"""每个测试方法前执行"""
|
||||||
|
self.carrier = BIOYOND_Electrolyte_6VialCarrier("test_carrier")
|
||||||
|
|
||||||
|
def teardown_method(self):
|
||||||
|
"""每个测试方法后执行"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_carrier_creation(self):
|
||||||
|
"""测试载架创建"""
|
||||||
|
assert self.carrier.name == "test_carrier"
|
||||||
|
assert len(self.carrier.sites) == 6
|
||||||
|
|
||||||
|
def test_bottle_placement(self):
|
||||||
|
"""测试瓶子放置"""
|
||||||
|
bottle = BIOYOND_PolymerStation_Solid_Vial("test_bottle")
|
||||||
|
# 测试逻辑...
|
||||||
|
assert bottle.name == "test_bottle"
|
||||||
|
|
||||||
|
|
||||||
|
def test_standalone_function():
|
||||||
|
"""独立测试函数"""
|
||||||
|
result = some_function()
|
||||||
|
assert result is True
|
||||||
|
|
||||||
|
|
||||||
|
# 参数化测试
|
||||||
|
@pytest.mark.parametrize("input,expected", [
|
||||||
|
("5 min", 300.0),
|
||||||
|
("1 h", 3600.0),
|
||||||
|
("120", 120.0),
|
||||||
|
(60, 60.0),
|
||||||
|
])
|
||||||
|
def test_time_parsing(input, expected):
|
||||||
|
"""测试时间解析"""
|
||||||
|
from unilabos.compile.utils.unit_parser import parse_time_input
|
||||||
|
assert parse_time_input(input) == expected
|
||||||
|
|
||||||
|
|
||||||
|
# 异常测试
|
||||||
|
def test_invalid_input_raises_error():
|
||||||
|
"""测试无效输入抛出异常"""
|
||||||
|
with pytest.raises(ValueError) as exc_info:
|
||||||
|
invalid_function("bad_input")
|
||||||
|
assert "invalid" in str(exc_info.value).lower()
|
||||||
|
|
||||||
|
|
||||||
|
# 跳过条件测试
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
not os.environ.get("ROS_DISTRO"),
|
||||||
|
reason="需要ROS环境"
|
||||||
|
)
|
||||||
|
def test_ros_feature():
|
||||||
|
"""需要ROS环境的测试"""
|
||||||
|
pass
|
||||||
|
```
|
||||||
|
|
||||||
|
## 设备测试
|
||||||
|
|
||||||
|
### 虚拟设备测试
|
||||||
|
|
||||||
|
```python
|
||||||
|
import pytest
|
||||||
|
import asyncio
|
||||||
|
from unittest.mock import MagicMock, AsyncMock
|
||||||
|
|
||||||
|
from unilabos.devices.virtual.virtual_stirrer import VirtualStirrer
|
||||||
|
|
||||||
|
|
||||||
|
class TestVirtualStirrer:
|
||||||
|
"""VirtualStirrer 测试"""
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def stirrer(self):
|
||||||
|
"""创建测试用搅拌器"""
|
||||||
|
device = VirtualStirrer(
|
||||||
|
device_id="test_stirrer",
|
||||||
|
config={"max_speed": 1500.0, "min_speed": 50.0}
|
||||||
|
)
|
||||||
|
|
||||||
|
# Mock ROS节点
|
||||||
|
mock_node = MagicMock()
|
||||||
|
mock_node.sleep = AsyncMock(return_value=None)
|
||||||
|
device.post_init(mock_node)
|
||||||
|
|
||||||
|
return device
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_initialize(self, stirrer):
|
||||||
|
"""测试初始化"""
|
||||||
|
result = await stirrer.initialize()
|
||||||
|
assert result is True
|
||||||
|
assert stirrer.status == "待机中"
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_stir_action(self, stirrer):
|
||||||
|
"""测试搅拌动作"""
|
||||||
|
await stirrer.initialize()
|
||||||
|
|
||||||
|
result = await stirrer.stir(
|
||||||
|
stir_time=5.0,
|
||||||
|
stir_speed=300.0,
|
||||||
|
settling_time=2.0
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result is True
|
||||||
|
assert stirrer.operation_mode == "Completed"
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_stir_invalid_speed(self, stirrer):
|
||||||
|
"""测试无效速度"""
|
||||||
|
await stirrer.initialize()
|
||||||
|
|
||||||
|
# 速度超出范围
|
||||||
|
result = await stirrer.stir(
|
||||||
|
stir_time=5.0,
|
||||||
|
stir_speed=2000.0, # 超过max_speed
|
||||||
|
settling_time=0.0
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result is False
|
||||||
|
assert "错误" in stirrer.status
|
||||||
|
```
|
||||||
|
|
||||||
|
### 异步测试配置
|
||||||
|
|
||||||
|
```python
|
||||||
|
# conftest.py
|
||||||
|
import pytest
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="session")
|
||||||
|
def event_loop():
|
||||||
|
"""创建事件循环"""
|
||||||
|
loop = asyncio.get_event_loop_policy().new_event_loop()
|
||||||
|
yield loop
|
||||||
|
loop.close()
|
||||||
|
```
|
||||||
|
|
||||||
|
## 资源测试
|
||||||
|
|
||||||
|
```python
|
||||||
|
import pytest
|
||||||
|
from unilabos.resources.resource_tracker import (
|
||||||
|
ResourceTreeSet,
|
||||||
|
ResourceTreeInstance,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_resource_tree_creation():
|
||||||
|
"""测试资源树创建"""
|
||||||
|
tree_set = ResourceTreeSet()
|
||||||
|
|
||||||
|
# 添加资源
|
||||||
|
resource = {"id": "res_1", "name": "Resource 1"}
|
||||||
|
tree_set.add_resource(resource)
|
||||||
|
|
||||||
|
# 验证
|
||||||
|
assert len(tree_set.all_nodes) == 1
|
||||||
|
assert tree_set.get_resource("res_1") is not None
|
||||||
|
|
||||||
|
|
||||||
|
def test_resource_tree_merge():
|
||||||
|
"""测试资源树合并"""
|
||||||
|
local_set = ResourceTreeSet()
|
||||||
|
remote_set = ResourceTreeSet()
|
||||||
|
|
||||||
|
# 设置数据...
|
||||||
|
|
||||||
|
local_set.merge_remote_resources(remote_set)
|
||||||
|
|
||||||
|
# 验证合并结果...
|
||||||
|
```
|
||||||
|
|
||||||
|
## ROS 消息测试
|
||||||
|
|
||||||
|
```python
|
||||||
|
import pytest
|
||||||
|
from unilabos.ros.msgs.message_converter import (
|
||||||
|
convert_to_ros_msg,
|
||||||
|
convert_from_ros_msg_with_mapping,
|
||||||
|
msg_converter_manager,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_message_conversion():
|
||||||
|
"""测试消息转换"""
|
||||||
|
# Python -> ROS
|
||||||
|
python_data = {"id": "test", "value": 42}
|
||||||
|
ros_msg = convert_to_ros_msg(python_data, MyMsgType)
|
||||||
|
|
||||||
|
assert ros_msg.id == "test"
|
||||||
|
assert ros_msg.value == 42
|
||||||
|
|
||||||
|
# ROS -> Python
|
||||||
|
result = convert_from_ros_msg_with_mapping(ros_msg, mapping)
|
||||||
|
assert result["id"] == "test"
|
||||||
|
```
|
||||||
|
|
||||||
|
## 协议测试
|
||||||
|
|
||||||
|
```python
|
||||||
|
import pytest
|
||||||
|
import networkx as nx
|
||||||
|
from unilabos.compile.stir_protocol import (
|
||||||
|
generate_stir_protocol,
|
||||||
|
extract_vessel_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def topology_graph():
|
||||||
|
"""创建测试拓扑图"""
|
||||||
|
G = nx.DiGraph()
|
||||||
|
G.add_node("flask_1", **{"class": "flask"})
|
||||||
|
G.add_node("stirrer_1", **{"class": "virtual_stirrer"})
|
||||||
|
G.add_edge("stirrer_1", "flask_1")
|
||||||
|
return G
|
||||||
|
|
||||||
|
|
||||||
|
def test_generate_stir_protocol(topology_graph):
|
||||||
|
"""测试搅拌协议生成"""
|
||||||
|
actions = generate_stir_protocol(
|
||||||
|
G=topology_graph,
|
||||||
|
vessel="flask_1",
|
||||||
|
time="5 min",
|
||||||
|
stir_speed=300.0
|
||||||
|
)
|
||||||
|
|
||||||
|
assert len(actions) == 1
|
||||||
|
assert actions[0]["device_id"] == "stirrer_1"
|
||||||
|
assert actions[0]["action_name"] == "stir"
|
||||||
|
|
||||||
|
|
||||||
|
def test_extract_vessel_id():
|
||||||
|
"""测试vessel_id提取"""
|
||||||
|
# 字典格式
|
||||||
|
assert extract_vessel_id({"id": "flask_1"}) == "flask_1"
|
||||||
|
|
||||||
|
# 字符串格式
|
||||||
|
assert extract_vessel_id("flask_2") == "flask_2"
|
||||||
|
|
||||||
|
# 空值
|
||||||
|
assert extract_vessel_id("") == ""
|
||||||
|
```
|
||||||
|
|
||||||
|
## 测试标记
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 慢速测试
|
||||||
|
@pytest.mark.slow
|
||||||
|
def test_long_running():
|
||||||
|
pass
|
||||||
|
|
||||||
|
# 需要网络
|
||||||
|
@pytest.mark.network
|
||||||
|
def test_network_call():
|
||||||
|
pass
|
||||||
|
|
||||||
|
# 需要ROS
|
||||||
|
@pytest.mark.ros
|
||||||
|
def test_ros_feature():
|
||||||
|
pass
|
||||||
|
```
|
||||||
|
|
||||||
|
运行特定标记的测试:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pytest -m "not slow" # 排除慢速测试
|
||||||
|
pytest -m ros # 仅ROS测试
|
||||||
|
```
|
||||||
|
|
||||||
|
## 覆盖率
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 生成覆盖率报告
|
||||||
|
pytest --cov=unilabos tests/
|
||||||
|
|
||||||
|
# HTML报告
|
||||||
|
pytest --cov=unilabos --cov-report=html tests/
|
||||||
|
```
|
||||||
|
|
||||||
|
## 最佳实践
|
||||||
|
|
||||||
|
1. **测试命名**: `test_{功能}_{场景}_{预期结果}`
|
||||||
|
2. **独立性**: 每个测试独立运行,不依赖其他测试
|
||||||
|
3. **Mock外部依赖**: 使用 unittest.mock 模拟外部服务
|
||||||
|
4. **参数化**: 使用 `@pytest.mark.parametrize` 减少重复代码
|
||||||
|
5. **fixtures**: 使用 fixtures 共享测试设置
|
||||||
|
6. **断言清晰**: 每个断言只验证一件事
|
||||||
353
.cursor/rules/unilabos-project.mdc
Normal file
353
.cursor/rules/unilabos-project.mdc
Normal file
@@ -0,0 +1,353 @@
|
|||||||
|
---
|
||||||
|
description: Uni-Lab-OS 实验室自动化平台开发规范 - 核心规则
|
||||||
|
globs: ["**/*.py", "**/*.yaml", "**/*.json"]
|
||||||
|
---
|
||||||
|
|
||||||
|
# Uni-Lab-OS 项目开发规范
|
||||||
|
|
||||||
|
## 项目概述
|
||||||
|
|
||||||
|
Uni-Lab-OS 是一个实验室自动化操作系统,用于连接和控制各种实验设备,实现实验工作流的自动化和标准化。
|
||||||
|
|
||||||
|
## 技术栈
|
||||||
|
|
||||||
|
- **Python 3.11** - 核心开发语言
|
||||||
|
- **ROS 2** - 设备通信中间件 (rclpy)
|
||||||
|
- **Conda/Mamba** - 包管理 (robostack-staging, conda-forge)
|
||||||
|
- **FastAPI** - Web API 服务
|
||||||
|
- **WebSocket** - 实时通信
|
||||||
|
- **NetworkX** - 拓扑图管理
|
||||||
|
- **YAML** - 配置和注册表定义
|
||||||
|
- **PyLabRobot** - 实验室自动化库集成
|
||||||
|
- **pytest** - 测试框架
|
||||||
|
- **asyncio** - 异步编程
|
||||||
|
|
||||||
|
## 项目结构
|
||||||
|
|
||||||
|
```
|
||||||
|
unilabos/
|
||||||
|
├── app/ # 应用入口、Web服务、后端
|
||||||
|
├── compile/ # 协议编译器 (stir, add, filter 等)
|
||||||
|
├── config/ # 配置管理
|
||||||
|
├── devices/ # 设备驱动 (真实/虚拟)
|
||||||
|
├── device_comms/ # 设备通信协议
|
||||||
|
├── device_mesh/ # 3D网格和可视化
|
||||||
|
├── registry/ # 设备和资源类型注册表 (YAML)
|
||||||
|
├── resources/ # 资源定义
|
||||||
|
├── ros/ # ROS 2 集成
|
||||||
|
├── utils/ # 工具函数
|
||||||
|
└── workflow/ # 工作流管理
|
||||||
|
```
|
||||||
|
|
||||||
|
## 代码规范
|
||||||
|
|
||||||
|
### Python 风格
|
||||||
|
|
||||||
|
1. **类型注解**:所有函数必须使用类型注解
|
||||||
|
```python
|
||||||
|
def transfer_liquid(
|
||||||
|
source: str,
|
||||||
|
destination: str,
|
||||||
|
volume: float,
|
||||||
|
**kwargs
|
||||||
|
) -> List[Dict[str, Any]]:
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Docstring**:使用 Google 风格的文档字符串
|
||||||
|
```python
|
||||||
|
def initialize(self) -> bool:
|
||||||
|
"""
|
||||||
|
初始化设备
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: 初始化是否成功
|
||||||
|
"""
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **导入顺序**:
|
||||||
|
- 标准库
|
||||||
|
- 第三方库
|
||||||
|
- ROS 相关 (rclpy, unilabos_msgs)
|
||||||
|
- 项目内部模块
|
||||||
|
|
||||||
|
### 异步编程
|
||||||
|
|
||||||
|
1. 设备操作方法使用 `async def`
|
||||||
|
2. 使用 `await self._ros_node.sleep()` 而非 `asyncio.sleep()`
|
||||||
|
3. 长时间运行操作需提供进度反馈
|
||||||
|
|
||||||
|
```python
|
||||||
|
async def stir(self, stir_time: float, stir_speed: float, **kwargs) -> bool:
|
||||||
|
"""执行搅拌操作"""
|
||||||
|
start_time = time_module.time()
|
||||||
|
while True:
|
||||||
|
elapsed = time_module.time() - start_time
|
||||||
|
remaining = max(0, stir_time - elapsed)
|
||||||
|
|
||||||
|
self.data.update({
|
||||||
|
"remaining_time": remaining,
|
||||||
|
"status": f"搅拌中: {stir_speed} RPM"
|
||||||
|
})
|
||||||
|
|
||||||
|
if remaining <= 0:
|
||||||
|
break
|
||||||
|
await self._ros_node.sleep(1.0)
|
||||||
|
return True
|
||||||
|
```
|
||||||
|
|
||||||
|
### 日志规范
|
||||||
|
|
||||||
|
使用项目自定义日志系统:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.utils.log import logger, info, debug, warning, error, trace
|
||||||
|
|
||||||
|
# 在设备类中使用
|
||||||
|
self.logger = logging.getLogger(f"DeviceName.{self.device_id}")
|
||||||
|
self.logger.info("设备初始化完成")
|
||||||
|
```
|
||||||
|
|
||||||
|
## 设备驱动开发
|
||||||
|
|
||||||
|
### 设备类结构
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
|
||||||
|
|
||||||
|
class MyDevice:
|
||||||
|
"""设备驱动类"""
|
||||||
|
|
||||||
|
_ros_node: BaseROS2DeviceNode
|
||||||
|
|
||||||
|
def __init__(self, device_id: str = None, config: Dict[str, Any] = None, **kwargs):
|
||||||
|
self.device_id = device_id or "unknown_device"
|
||||||
|
self.config = config or {}
|
||||||
|
self.data = {} # 设备状态数据
|
||||||
|
|
||||||
|
def post_init(self, ros_node: BaseROS2DeviceNode):
|
||||||
|
"""ROS节点注入"""
|
||||||
|
self._ros_node = ros_node
|
||||||
|
|
||||||
|
async def initialize(self) -> bool:
|
||||||
|
"""初始化设备"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def cleanup(self) -> bool:
|
||||||
|
"""清理设备"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
# 状态属性 - 自动发布为 ROS Topic
|
||||||
|
@property
|
||||||
|
def status(self) -> str:
|
||||||
|
return self.data.get("status", "待机")
|
||||||
|
```
|
||||||
|
|
||||||
|
### 状态属性装饰器
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.utils.decorator import topic_config
|
||||||
|
|
||||||
|
class MyDevice:
|
||||||
|
@property
|
||||||
|
@topic_config(period=1.0, qos=10) # 每秒发布一次
|
||||||
|
def temperature(self) -> float:
|
||||||
|
return self._temperature
|
||||||
|
```
|
||||||
|
|
||||||
|
### 虚拟设备
|
||||||
|
|
||||||
|
虚拟设备放置在 `unilabos/devices/virtual/` 目录下,命名为 `virtual_*.py`
|
||||||
|
|
||||||
|
## 注册表配置
|
||||||
|
|
||||||
|
### 设备注册表 (YAML)
|
||||||
|
|
||||||
|
位置: `unilabos/registry/devices/*.yaml`
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
my_device_type:
|
||||||
|
category:
|
||||||
|
- my_category
|
||||||
|
description: "设备描述"
|
||||||
|
version: "1.0.0"
|
||||||
|
class:
|
||||||
|
module: "unilabos.devices.my_device:MyDevice"
|
||||||
|
type: python
|
||||||
|
status_types:
|
||||||
|
status: String
|
||||||
|
temperature: Float64
|
||||||
|
action_value_mappings:
|
||||||
|
auto-initialize:
|
||||||
|
type: UniLabJsonCommandAsync
|
||||||
|
goal: {}
|
||||||
|
feedback: {}
|
||||||
|
result: {}
|
||||||
|
schema: {...}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 资源注册表 (YAML)
|
||||||
|
|
||||||
|
位置: `unilabos/registry/resources/**/*.yaml`
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
my_container:
|
||||||
|
category:
|
||||||
|
- container
|
||||||
|
class:
|
||||||
|
module: "unilabos.resources.my_resource:MyContainer"
|
||||||
|
type: pylabrobot
|
||||||
|
version: "1.0.0"
|
||||||
|
```
|
||||||
|
|
||||||
|
## 协议编译器
|
||||||
|
|
||||||
|
位置: `unilabos/compile/*_protocol.py`
|
||||||
|
|
||||||
|
### 协议生成函数模板
|
||||||
|
|
||||||
|
```python
|
||||||
|
from typing import List, Dict, Any, Union
|
||||||
|
import networkx as nx
|
||||||
|
|
||||||
|
def generate_my_protocol(
|
||||||
|
G: nx.DiGraph,
|
||||||
|
vessel: Union[str, dict],
|
||||||
|
param1: float = 0.0,
|
||||||
|
**kwargs
|
||||||
|
) -> List[Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
生成操作协议序列
|
||||||
|
|
||||||
|
Args:
|
||||||
|
G: 物理拓扑图
|
||||||
|
vessel: 容器ID或字典
|
||||||
|
param1: 参数1
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Dict]: 动作序列
|
||||||
|
"""
|
||||||
|
# 提取vessel_id
|
||||||
|
vessel_id = vessel if isinstance(vessel, str) else vessel.get("id", "")
|
||||||
|
|
||||||
|
# 查找设备
|
||||||
|
device_id = find_connected_device(G, vessel_id)
|
||||||
|
|
||||||
|
# 生成动作
|
||||||
|
action_sequence = [{
|
||||||
|
"device_id": device_id,
|
||||||
|
"action_name": "my_action",
|
||||||
|
"action_kwargs": {
|
||||||
|
"vessel": {"id": vessel_id},
|
||||||
|
"param1": float(param1)
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
|
||||||
|
return action_sequence
|
||||||
|
```
|
||||||
|
|
||||||
|
## 测试规范
|
||||||
|
|
||||||
|
### 测试文件位置
|
||||||
|
|
||||||
|
- 单元测试: `tests/` 目录
|
||||||
|
- 设备测试: `tests/devices/`
|
||||||
|
- 资源测试: `tests/resources/`
|
||||||
|
- ROS消息测试: `tests/ros/msgs/`
|
||||||
|
|
||||||
|
### 测试命名
|
||||||
|
|
||||||
|
```python
|
||||||
|
# tests/devices/my_device/test_my_device.py
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
def test_device_initialization():
|
||||||
|
"""测试设备初始化"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_device_action():
|
||||||
|
"""测试设备动作"""
|
||||||
|
pass
|
||||||
|
```
|
||||||
|
|
||||||
|
## 错误处理
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.utils.exception import UniLabException
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = await device.execute_action()
|
||||||
|
except ValueError as e:
|
||||||
|
self.logger.error(f"参数错误: {e}")
|
||||||
|
self.data["status"] = "错误: 参数无效"
|
||||||
|
return False
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error(f"执行失败: {e}")
|
||||||
|
raise
|
||||||
|
```
|
||||||
|
|
||||||
|
## 配置管理
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.config.config import BasicConfig, HTTPConfig
|
||||||
|
|
||||||
|
# 读取配置
|
||||||
|
port = BasicConfig.port
|
||||||
|
is_host = BasicConfig.is_host_mode
|
||||||
|
|
||||||
|
# 配置文件: local_config.py
|
||||||
|
```
|
||||||
|
|
||||||
|
## 常用工具
|
||||||
|
|
||||||
|
### 单例模式
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.utils.decorator import singleton
|
||||||
|
|
||||||
|
@singleton
|
||||||
|
class MyManager:
|
||||||
|
pass
|
||||||
|
```
|
||||||
|
|
||||||
|
### 类型检查
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.utils.type_check import NoAliasDumper
|
||||||
|
|
||||||
|
yaml.dump(data, f, Dumper=NoAliasDumper)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 导入管理
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.utils.import_manager import get_class
|
||||||
|
|
||||||
|
device_class = get_class("unilabos.devices.my_device:MyDevice")
|
||||||
|
```
|
||||||
|
|
||||||
|
## Git 提交规范
|
||||||
|
|
||||||
|
提交信息格式:
|
||||||
|
```
|
||||||
|
<type>(<scope>): <subject>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
```
|
||||||
|
|
||||||
|
类型:
|
||||||
|
- `feat`: 新功能
|
||||||
|
- `fix`: 修复bug
|
||||||
|
- `docs`: 文档更新
|
||||||
|
- `refactor`: 重构
|
||||||
|
- `test`: 测试相关
|
||||||
|
- `chore`: 构建/工具相关
|
||||||
|
|
||||||
|
示例:
|
||||||
|
```
|
||||||
|
feat(devices): 添加虚拟搅拌器设备
|
||||||
|
|
||||||
|
- 实现VirtualStirrer类
|
||||||
|
- 支持定时搅拌和持续搅拌模式
|
||||||
|
- 添加速度验证逻辑
|
||||||
|
```
|
||||||
24
.cursor/skills/add-device/SKILL.md
Normal file
24
.cursor/skills/add-device/SKILL.md
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
---
|
||||||
|
name: add-device
|
||||||
|
description: Guide for adding new devices to Uni-Lab-OS (接入新设备). Walks through device category selection (thing model), communication protocol, command protocol collection, driver creation, registry YAML, and graph file setup. Use when the user wants to add/integrate a new device, create a device driver, write a device class, configure device registry, or mentions 接入设备/添加设备/设备驱动/物模型.
|
||||||
|
---
|
||||||
|
|
||||||
|
# 添加新设备到 Uni-Lab-OS
|
||||||
|
|
||||||
|
**第一步:** 使用 Read 工具读取 `docs/ai_guides/add_device.md`,获取完整的设备接入指南并严格遵循。
|
||||||
|
|
||||||
|
该指南包含:
|
||||||
|
- 8 步完整流程(设备类别、通信协议、指令收集、接口对齐、驱动创建、注册表、图文件、验证)
|
||||||
|
- 所有物模型代码模板(注射泵、电磁阀、蠕动泵、温控、电机等)
|
||||||
|
- 通信协议代码片段(Serial、Modbus、TCP、HTTP、OPC UA)
|
||||||
|
- 现有设备接口快照(用于第四步对齐,包含参数名、status_types、方法签名)
|
||||||
|
- 常见错误检查清单
|
||||||
|
|
||||||
|
**Cursor 工具映射:**
|
||||||
|
|
||||||
|
| 指南中的操作 | Cursor 中使用的工具 |
|
||||||
|
|---|---|
|
||||||
|
| 向用户确认设备类别、协议等信息 | 使用 AskQuestion 工具 |
|
||||||
|
| 搜索已有设备注册表 | 使用 Grep 在 `unilabos/registry/devices/` 中搜索 |
|
||||||
|
| 读取用户提供的协议文档/SDK 代码 | 使用 Read 工具 |
|
||||||
|
| 第四步对齐:查找同类设备接口 | 优先使用 Grep 搜索仓库中的最新注册表;指南中的「现有设备接口快照」作为兜底参考 |
|
||||||
323
.cursor/skills/add-protocol/SKILL.md
Normal file
323
.cursor/skills/add-protocol/SKILL.md
Normal file
@@ -0,0 +1,323 @@
|
|||||||
|
---
|
||||||
|
name: add-protocol
|
||||||
|
description: Guide for adding new experiment protocols to Uni-Lab-OS (添加新实验操作协议). Walks through ROS Action definition, Pydantic model creation, protocol generator implementation, and registration. Use when the user wants to add a new protocol, create a compile function, implement an experiment operation, or mentions 协议/protocol/编译/compile/实验操作.
|
||||||
|
---
|
||||||
|
|
||||||
|
# 添加新实验操作协议(Protocol)
|
||||||
|
|
||||||
|
Protocol 是对实验有意义的完整动作(如泵转移、过滤、溶解),需要多设备协同。`compile/` 中的生成函数根据设备连接图将抽象操作"编译"为设备指令序列。
|
||||||
|
|
||||||
|
添加一个 Protocol 需修改 **6 个文件**,按以下流程执行。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第一步:确认协议信息
|
||||||
|
|
||||||
|
向用户确认:
|
||||||
|
|
||||||
|
| 信息 | 示例 |
|
||||||
|
|------|------|
|
||||||
|
| 协议英文名 | `MyNewProtocol` |
|
||||||
|
| 操作描述 | 将固体样品研磨至目标粒径 |
|
||||||
|
| Goal 参数(必需 + 可选) | `vessel: dict`, `time: float = 300.0` |
|
||||||
|
| Result 字段 | `success: bool`, `message: str` |
|
||||||
|
| 需要哪些设备协同 | 研磨器、搅拌器 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第二步:创建 ROS Action 定义
|
||||||
|
|
||||||
|
路径:`unilabos_msgs/action/<ActionName>.action`
|
||||||
|
|
||||||
|
三段式结构(Goal / Result / Feedback),用 `---` 分隔:
|
||||||
|
|
||||||
|
```
|
||||||
|
# Goal
|
||||||
|
Resource vessel
|
||||||
|
float64 time
|
||||||
|
string mode
|
||||||
|
---
|
||||||
|
# Result
|
||||||
|
bool success
|
||||||
|
string return_info
|
||||||
|
---
|
||||||
|
# Feedback
|
||||||
|
string status
|
||||||
|
string current_device
|
||||||
|
builtin_interfaces/Duration time_spent
|
||||||
|
builtin_interfaces/Duration time_remaining
|
||||||
|
```
|
||||||
|
|
||||||
|
**类型映射:**
|
||||||
|
|
||||||
|
| Python 类型 | ROS 类型 | 说明 |
|
||||||
|
|------------|----------|------|
|
||||||
|
| `dict` | `Resource` | 容器/设备引用,自定义消息类型 |
|
||||||
|
| `float` | `float64` | |
|
||||||
|
| `int` | `int32` | |
|
||||||
|
| `str` | `string` | |
|
||||||
|
| `bool` | `bool` | |
|
||||||
|
|
||||||
|
> `Resource` 是 `unilabos_msgs/msg/Resource.msg` 中定义的自定义消息类型。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第三步:注册 Action 到 CMakeLists
|
||||||
|
|
||||||
|
在 `unilabos_msgs/CMakeLists.txt` 的 `set(action_files ...)` 块中添加:
|
||||||
|
|
||||||
|
```cmake
|
||||||
|
"action/MyNewAction.action"
|
||||||
|
```
|
||||||
|
|
||||||
|
> 调试时需编译:`cd unilabos_msgs && colcon build && source ./install/local_setup.sh && cd ..`
|
||||||
|
> PR 合并后 CI/CD 自动发布,`mamba update ros-humble-unilabos-msgs` 即可。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第四步:创建 Pydantic 模型
|
||||||
|
|
||||||
|
在 `unilabos/messages/__init__.py` 中添加(位于 `# Start Protocols` 和 `# End Protocols` 之间):
|
||||||
|
|
||||||
|
```python
|
||||||
|
class MyNewProtocol(BaseModel):
|
||||||
|
# === 必需参数 ===
|
||||||
|
vessel: dict = Field(..., description="目标容器")
|
||||||
|
|
||||||
|
# === 可选参数 ===
|
||||||
|
time: float = Field(300.0, description="操作时间 (秒)")
|
||||||
|
mode: str = Field("default", description="操作模式")
|
||||||
|
|
||||||
|
def model_post_init(self, __context):
|
||||||
|
"""参数验证和修正"""
|
||||||
|
if self.time <= 0:
|
||||||
|
self.time = 300.0
|
||||||
|
```
|
||||||
|
|
||||||
|
**规则:**
|
||||||
|
- 参数名必须与 `.action` 文件中 Goal 字段完全一致
|
||||||
|
- `dict` 类型对应 `.action` 中的 `Resource`
|
||||||
|
- 将类名加入文件末尾的 `__all__` 列表
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第五步:实现协议生成函数
|
||||||
|
|
||||||
|
路径:`unilabos/compile/<protocol_name>_protocol.py`
|
||||||
|
|
||||||
|
```python
|
||||||
|
import networkx as nx
|
||||||
|
from typing import List, Dict, Any
|
||||||
|
|
||||||
|
|
||||||
|
def generate_my_new_protocol(
|
||||||
|
G: nx.DiGraph,
|
||||||
|
vessel: dict,
|
||||||
|
time: float = 300.0,
|
||||||
|
mode: str = "default",
|
||||||
|
**kwargs,
|
||||||
|
) -> List[Dict[str, Any]]:
|
||||||
|
"""将 MyNewProtocol 编译为设备动作序列。
|
||||||
|
|
||||||
|
Args:
|
||||||
|
G: 设备连接图(NetworkX),节点为设备/容器,边为物理连接
|
||||||
|
vessel: 目标容器 {"id": "reactor_1"}
|
||||||
|
time: 操作时间(秒)
|
||||||
|
mode: 操作模式
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
动作列表,每个元素为:
|
||||||
|
- dict: 单步动作
|
||||||
|
- list[dict]: 并行动作
|
||||||
|
"""
|
||||||
|
from unilabos.compile.utils.vessel_parser import get_vessel
|
||||||
|
|
||||||
|
vessel_id, vessel_data = get_vessel(vessel)
|
||||||
|
actions = []
|
||||||
|
|
||||||
|
# 查找相关设备(通过图的连接关系)
|
||||||
|
# 生成动作序列
|
||||||
|
actions.append({
|
||||||
|
"device_id": "target_device_id",
|
||||||
|
"action_name": "some_action",
|
||||||
|
"action_kwargs": {"param": "value"}
|
||||||
|
})
|
||||||
|
|
||||||
|
# 等待
|
||||||
|
actions.append({
|
||||||
|
"action_name": "wait",
|
||||||
|
"action_kwargs": {"time": time}
|
||||||
|
})
|
||||||
|
|
||||||
|
return actions
|
||||||
|
```
|
||||||
|
|
||||||
|
### 动作字典格式
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 单步动作(发给子设备)
|
||||||
|
{"device_id": "pump_1", "action_name": "set_position", "action_kwargs": {"position": 10.0}}
|
||||||
|
|
||||||
|
# 发给工作站自身
|
||||||
|
{"device_id": "self", "action_name": "my_action", "action_kwargs": {...}}
|
||||||
|
|
||||||
|
# 等待
|
||||||
|
{"action_name": "wait", "action_kwargs": {"time": 5.0}}
|
||||||
|
|
||||||
|
# 并行动作(列表嵌套)
|
||||||
|
[
|
||||||
|
{"device_id": "pump_1", "action_name": "set_position", "action_kwargs": {"position": 10.0}},
|
||||||
|
{"device_id": "stirrer_1", "action_name": "start_stir", "action_kwargs": {"stir_speed": 300}}
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
### 关于 `vessel` 参数类型
|
||||||
|
|
||||||
|
现有协议的 `vessel` 参数类型不统一:
|
||||||
|
- 新协议趋势:使用 `dict`(如 `{"id": "reactor_1"}`)
|
||||||
|
- 旧协议:使用 `str`(如 `"reactor_1"`)
|
||||||
|
- 兼容写法:`Union[str, dict]`
|
||||||
|
|
||||||
|
**建议新协议统一使用 `dict` 类型**,通过 `get_vessel()` 兼容两种输入。
|
||||||
|
|
||||||
|
### 公共工具函数(`unilabos/compile/utils/`)
|
||||||
|
|
||||||
|
| 函数 | 用途 |
|
||||||
|
|------|------|
|
||||||
|
| `get_vessel(vessel)` | 解析容器参数为 `(vessel_id, vessel_data)`,兼容 dict 和 str |
|
||||||
|
| `find_solvent_vessel(G, solvent)` | 根据溶剂名查找容器(精确→命名规则→模糊→液体类型) |
|
||||||
|
| `find_reagent_vessel(G, reagent)` | 根据试剂名查找容器(支持固体和液体) |
|
||||||
|
| `find_connected_stirrer(G, vessel)` | 查找与容器相连的搅拌器 |
|
||||||
|
| `find_solid_dispenser(G)` | 查找固体加样器 |
|
||||||
|
|
||||||
|
### 协议内专属查找函数
|
||||||
|
|
||||||
|
许多协议在自己的文件内定义了专属的 `find_*` 函数(不在 `utils/` 中)。编写新协议时,优先复用 `utils/` 中的公共函数;如需特殊查找逻辑,在协议文件内部定义即可:
|
||||||
|
|
||||||
|
```python
|
||||||
|
def find_my_special_device(G: nx.DiGraph, vessel: str) -> str:
|
||||||
|
"""查找与容器相关的特殊设备"""
|
||||||
|
for node in G.nodes():
|
||||||
|
if 'my_device_type' in G.nodes[node].get('class', '').lower():
|
||||||
|
return node
|
||||||
|
raise ValueError("未找到特殊设备")
|
||||||
|
```
|
||||||
|
|
||||||
|
### 复用已有协议
|
||||||
|
|
||||||
|
复杂协议通常组合已有协议:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.compile.pump_protocol import generate_pump_protocol_with_rinsing
|
||||||
|
|
||||||
|
actions.extend(generate_pump_protocol_with_rinsing(
|
||||||
|
G, from_vessel=solvent_vessel, to_vessel=vessel, volume=volume
|
||||||
|
))
|
||||||
|
```
|
||||||
|
|
||||||
|
### 图查询模式
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 查找与容器相连的特定类型设备
|
||||||
|
for neighbor in G.neighbors(vessel_id):
|
||||||
|
node_data = G.nodes[neighbor]
|
||||||
|
if "heater" in node_data.get("class", ""):
|
||||||
|
heater_id = neighbor
|
||||||
|
break
|
||||||
|
|
||||||
|
# 查找最短路径(泵转移)
|
||||||
|
path = nx.shortest_path(G, source=from_vessel_id, target=to_vessel_id)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第六步:注册协议生成函数
|
||||||
|
|
||||||
|
在 `unilabos/compile/__init__.py` 中:
|
||||||
|
|
||||||
|
1. 顶部添加导入:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from .my_new_protocol import generate_my_new_protocol
|
||||||
|
```
|
||||||
|
|
||||||
|
2. 在 `action_protocol_generators` 字典中添加映射:
|
||||||
|
|
||||||
|
```python
|
||||||
|
action_protocol_generators = {
|
||||||
|
# ... 已有协议
|
||||||
|
MyNewProtocol: generate_my_new_protocol,
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第七步:配置图文件
|
||||||
|
|
||||||
|
在工作站的图文件中,将协议名加入 `protocol_type`:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "my_station",
|
||||||
|
"class": "workstation",
|
||||||
|
"config": {
|
||||||
|
"protocol_type": ["PumpTransferProtocol", "MyNewProtocol"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第八步:验证
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 1. 模块可导入
|
||||||
|
python -c "from unilabos.messages import MyNewProtocol; print(MyNewProtocol.model_fields)"
|
||||||
|
|
||||||
|
# 2. 生成函数可导入
|
||||||
|
python -c "from unilabos.compile import action_protocol_generators; print(list(action_protocol_generators.keys()))"
|
||||||
|
|
||||||
|
# 3. 启动测试(可选)
|
||||||
|
unilab -g <graph>.json --complete_registry
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 工作流清单
|
||||||
|
|
||||||
|
```
|
||||||
|
协议接入进度:
|
||||||
|
- [ ] 1. 确认协议名、参数、涉及设备
|
||||||
|
- [ ] 2. 创建 .action 文件 (unilabos_msgs/action/<Name>.action)
|
||||||
|
- [ ] 3. 注册到 CMakeLists.txt
|
||||||
|
- [ ] 4. 创建 Pydantic 模型 (unilabos/messages/__init__.py) + 更新 __all__
|
||||||
|
- [ ] 5. 实现生成函数 (unilabos/compile/<name>_protocol.py)
|
||||||
|
- [ ] 6. 注册到 compile/__init__.py
|
||||||
|
- [ ] 7. 配置图文件 protocol_type
|
||||||
|
- [ ] 8. 验证
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 高级模式
|
||||||
|
|
||||||
|
实现复杂协议时,详见 [reference.md](reference.md):协议运行时数据流、mock graph 测试模式、单位解析工具(`unit_parser.py`)、复杂协议组合模式(以 dissolve 为例)。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 现有协议速查
|
||||||
|
|
||||||
|
| 协议 | Pydantic 类 | 生成函数 | 核心参数 |
|
||||||
|
|------|-------------|---------|---------|
|
||||||
|
| 泵转移 | `PumpTransferProtocol` | `generate_pump_protocol_with_rinsing` | `from_vessel, to_vessel, volume` |
|
||||||
|
| 简单转移 | `TransferProtocol` | `generate_pump_protocol` | `from_vessel, to_vessel, volume` |
|
||||||
|
| 加样 | `AddProtocol` | `generate_add_protocol` | `vessel, reagent, volume` |
|
||||||
|
| 过滤 | `FilterProtocol` | `generate_filter_protocol` | `vessel, filtrate_vessel` |
|
||||||
|
| 溶解 | `DissolveProtocol` | `generate_dissolve_protocol` | `vessel, solvent, volume` |
|
||||||
|
| 加热/冷却 | `HeatChillProtocol` | `generate_heat_chill_protocol` | `vessel, temp, time` |
|
||||||
|
| 搅拌 | `StirProtocol` | `generate_stir_protocol` | `vessel, time` |
|
||||||
|
| 分离 | `SeparateProtocol` | `generate_separate_protocol` | `from_vessel, separation_vessel, solvent` |
|
||||||
|
| 蒸发 | `EvaporateProtocol` | `generate_evaporate_protocol` | `vessel, pressure, temp, time` |
|
||||||
|
| 清洗 | `CleanProtocol` | `generate_clean_protocol` | `vessel, solvent, volume` |
|
||||||
|
| 离心 | `CentrifugeProtocol` | `generate_centrifuge_protocol` | `vessel, speed, time` |
|
||||||
|
| 抽气充气 | `EvacuateAndRefillProtocol` | `generate_evacuateandrefill_protocol` | `vessel, gas` |
|
||||||
207
.cursor/skills/add-protocol/reference.md
Normal file
207
.cursor/skills/add-protocol/reference.md
Normal file
@@ -0,0 +1,207 @@
|
|||||||
|
# 协议高级参考
|
||||||
|
|
||||||
|
本文件是 SKILL.md 的补充,包含协议运行时数据流、测试模式、单位解析工具和复杂协议组合模式。Agent 在需要实现这些功能时按需阅读。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 协议运行时数据流
|
||||||
|
|
||||||
|
从图文件到协议执行的完整链路:
|
||||||
|
|
||||||
|
```
|
||||||
|
实验图 JSON
|
||||||
|
↓ graphio.read_node_link_json()
|
||||||
|
physical_setup_graph (NetworkX DiGraph)
|
||||||
|
↓ ROS2WorkstationNode._setup_protocol_names(protocol_type)
|
||||||
|
为每个 protocol_name 创建 ActionServer
|
||||||
|
↓ 收到 Action Goal
|
||||||
|
_create_protocol_execute_callback()
|
||||||
|
↓ convert_from_ros_msg_with_mapping(goal, mapping)
|
||||||
|
protocol_kwargs (Python dict)
|
||||||
|
↓ 向 Host 查询 Resource 类型参数的当前状态
|
||||||
|
protocol_kwargs 更新(vessel 带上 children、data 等)
|
||||||
|
↓ protocol_steps_generator(G=physical_setup_graph, **protocol_kwargs)
|
||||||
|
List[Dict] 动作序列
|
||||||
|
↓ 逐步 execute_single_action / 并行 create_task
|
||||||
|
子设备 ActionClient 执行
|
||||||
|
```
|
||||||
|
|
||||||
|
### `_setup_protocol_names` 核心逻辑
|
||||||
|
|
||||||
|
```python
|
||||||
|
def _setup_protocol_names(self, protocol_type):
|
||||||
|
if isinstance(protocol_type, str):
|
||||||
|
self.protocol_names = [p.strip() for p in protocol_type.split(",")]
|
||||||
|
else:
|
||||||
|
self.protocol_names = protocol_type
|
||||||
|
self.protocol_action_mappings = {}
|
||||||
|
for protocol_name in self.protocol_names:
|
||||||
|
protocol_type = globals()[protocol_name] # 从 messages 模块取 Pydantic 类
|
||||||
|
self.protocol_action_mappings[protocol_name] = get_action_type(protocol_type)
|
||||||
|
```
|
||||||
|
|
||||||
|
### `_create_protocol_execute_callback` 关键步骤
|
||||||
|
|
||||||
|
1. `convert_from_ros_msg_with_mapping(goal, action_value_mapping["goal"])` — ROS Goal → Python dict
|
||||||
|
2. 对 `Resource` 类型字段,通过 `resource_get` Service 查询 Host 的最新资源状态
|
||||||
|
3. `protocol_steps_generator(G=physical_setup_graph, **protocol_kwargs)` — 调用编译函数
|
||||||
|
4. 遍历 steps:`dict` 串行执行,`list` 并行执行
|
||||||
|
5. `execute_single_action` 通过 `_action_clients[device_id]` 向子设备发送 Action Goal
|
||||||
|
6. 执行完毕后通过 `resource_update` Service 更新资源状态
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 测试模式
|
||||||
|
|
||||||
|
### 2.1 协议文件内测试函数
|
||||||
|
|
||||||
|
许多协议文件末尾有 `test_*` 函数,主要测试参数解析工具:
|
||||||
|
|
||||||
|
```python
|
||||||
|
def test_dissolve_protocol():
|
||||||
|
"""测试溶解协议的各种参数解析"""
|
||||||
|
volumes = ["10 mL", "?", 10.0, "1 L", "500 μL"]
|
||||||
|
for vol in volumes:
|
||||||
|
result = parse_volume_input(vol)
|
||||||
|
print(f"体积解析: {vol} → {result}mL")
|
||||||
|
|
||||||
|
masses = ["2.9 g", "?", 2.5, "500 mg"]
|
||||||
|
for mass in masses:
|
||||||
|
result = parse_mass_input(mass)
|
||||||
|
print(f"质量解析: {mass} → {result}g")
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2.2 使用 mock graph 测试协议生成器
|
||||||
|
|
||||||
|
推荐的端到端测试模式:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import pytest
|
||||||
|
import networkx as nx
|
||||||
|
from unilabos.compile.stir_protocol import generate_stir_protocol
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def topology_graph():
|
||||||
|
"""创建测试拓扑图"""
|
||||||
|
G = nx.DiGraph()
|
||||||
|
G.add_node("flask_1", **{"class": "flask", "type": "container"})
|
||||||
|
G.add_node("stirrer_1", **{"class": "virtual_stirrer", "type": "device"})
|
||||||
|
G.add_edge("stirrer_1", "flask_1")
|
||||||
|
return G
|
||||||
|
|
||||||
|
|
||||||
|
def test_generate_stir_protocol(topology_graph):
|
||||||
|
"""测试搅拌协议生成"""
|
||||||
|
actions = generate_stir_protocol(
|
||||||
|
G=topology_graph,
|
||||||
|
vessel="flask_1",
|
||||||
|
time="5 min",
|
||||||
|
stir_speed=300.0
|
||||||
|
)
|
||||||
|
assert len(actions) >= 1
|
||||||
|
assert actions[0]["device_id"] == "stirrer_1"
|
||||||
|
```
|
||||||
|
|
||||||
|
**要点:**
|
||||||
|
- 用 `nx.DiGraph()` 构建最小拓扑
|
||||||
|
- `add_node(id, **attrs)` 设置 `class`、`type`、`data` 等
|
||||||
|
- `add_edge(src, dst)` 建立物理连接
|
||||||
|
- 协议内的 `find_*` 函数依赖这些节点和边
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 单位解析工具
|
||||||
|
|
||||||
|
路径:`unilabos/compile/utils/unit_parser.py`
|
||||||
|
|
||||||
|
| 函数 | 输入 | 返回 | 默认值 |
|
||||||
|
|------|------|------|--------|
|
||||||
|
| `parse_volume_input(input, default_unit)` | `"100 mL"`, `"2.5 L"`, `"500 μL"`, `10.0`, `"?"` | mL (float) | 50.0 |
|
||||||
|
| `parse_mass_input(input)` | `"19.3 g"`, `"500 mg"`, `2.5`, `"?"` | g (float) | 1.0 |
|
||||||
|
| `parse_time_input(input)` | `"30 min"`, `"1 h"`, `"300"`, `60.0`, `"?"` | 秒 (float) | 60.0 |
|
||||||
|
|
||||||
|
支持的单位:
|
||||||
|
|
||||||
|
- **体积**: mL, L, μL/uL, milliliter, liter, microliter
|
||||||
|
- **质量**: g, mg, kg, gram, milligram, kilogram
|
||||||
|
- **时间**: s/sec/second, min/minute, h/hr/hour, d/day
|
||||||
|
|
||||||
|
特殊值 `"?"`、`"unknown"`、`"tbd"` 返回默认值。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 复杂协议组合模式
|
||||||
|
|
||||||
|
以 `dissolve_protocol` 为例,展示如何组合多个子操作:
|
||||||
|
|
||||||
|
### 整体流程
|
||||||
|
|
||||||
|
```
|
||||||
|
1. 解析参数 (parse_volume_input, parse_mass_input, parse_time_input)
|
||||||
|
2. 设备发现 (find_connected_heatchill, find_connected_stirrer, find_solid_dispenser)
|
||||||
|
3. 判断溶解类型 (液体 vs 固体)
|
||||||
|
4. 组合动作序列:
|
||||||
|
a. heat_chill_start / start_stir (启动加热/搅拌)
|
||||||
|
b. wait (等待温度稳定)
|
||||||
|
c. pump_protocol_with_rinsing (液体转移, 通过 extend 拼接)
|
||||||
|
或 add_solid (固体加样)
|
||||||
|
d. heat_chill / stir / wait (溶解等待)
|
||||||
|
e. heat_chill_stop (停止加热)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 关键代码模式
|
||||||
|
|
||||||
|
**设备发现 → 条件组合:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
heatchill_id = find_connected_heatchill(G, vessel_id)
|
||||||
|
stirrer_id = find_connected_stirrer(G, vessel_id)
|
||||||
|
solid_dispenser_id = find_solid_dispenser(G)
|
||||||
|
|
||||||
|
actions = []
|
||||||
|
|
||||||
|
# 启动阶段
|
||||||
|
if heatchill_id and temp > 25.0:
|
||||||
|
actions.append({
|
||||||
|
"device_id": heatchill_id,
|
||||||
|
"action_name": "heat_chill_start",
|
||||||
|
"action_kwargs": {"vessel": {"id": vessel_id}, "temp": temp}
|
||||||
|
})
|
||||||
|
actions.append({"action_name": "wait", "action_kwargs": {"time": 30}})
|
||||||
|
elif stirrer_id:
|
||||||
|
actions.append({
|
||||||
|
"device_id": stirrer_id,
|
||||||
|
"action_name": "start_stir",
|
||||||
|
"action_kwargs": {"vessel": {"id": vessel_id}, "stir_speed": stir_speed}
|
||||||
|
})
|
||||||
|
|
||||||
|
# 转移阶段(复用已有协议)
|
||||||
|
pump_actions = generate_pump_protocol_with_rinsing(
|
||||||
|
G=G, from_vessel=solvent_vessel, to_vessel=vessel_id, volume=volume
|
||||||
|
)
|
||||||
|
actions.extend(pump_actions)
|
||||||
|
|
||||||
|
# 等待阶段
|
||||||
|
if heatchill_id:
|
||||||
|
actions.append({
|
||||||
|
"device_id": heatchill_id,
|
||||||
|
"action_name": "heat_chill",
|
||||||
|
"action_kwargs": {"vessel": {"id": vessel_id}, "temp": temp, "time": time}
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
actions.append({"action_name": "wait", "action_kwargs": {"time": time}})
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 关键路径
|
||||||
|
|
||||||
|
| 内容 | 路径 |
|
||||||
|
|------|------|
|
||||||
|
| 协议执行回调 | `unilabos/ros/nodes/presets/workstation.py` |
|
||||||
|
| ROS 消息映射 | `unilabos/ros/msgs/message_converter.py` |
|
||||||
|
| 物理拓扑图 | `unilabos/resources/graphio.py` (`physical_setup_graph`) |
|
||||||
|
| 单位解析 | `unilabos/compile/utils/unit_parser.py` |
|
||||||
|
| 容器解析 | `unilabos/compile/utils/vessel_parser.py` |
|
||||||
|
| 溶解协议(组合示例) | `unilabos/compile/dissolve_protocol.py` |
|
||||||
371
.cursor/skills/add-resource/SKILL.md
Normal file
371
.cursor/skills/add-resource/SKILL.md
Normal file
@@ -0,0 +1,371 @@
|
|||||||
|
---
|
||||||
|
name: add-resource
|
||||||
|
description: Guide for adding new resources (materials, bottles, carriers, decks, warehouses) to Uni-Lab-OS (添加新物料/资源). Covers Bottle, Carrier, Deck, WareHouse definitions and registry YAML. Use when the user wants to add resources, define materials, create a deck layout, add bottles/carriers/plates, or mentions 物料/资源/resource/bottle/carrier/deck/plate/warehouse.
|
||||||
|
---
|
||||||
|
|
||||||
|
# 添加新物料资源
|
||||||
|
|
||||||
|
Uni-Lab-OS 的资源体系基于 PyLabRobot,通过扩展实现 Bottle、Carrier、WareHouse、Deck 等实验室物料管理。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第一步:确认资源类型
|
||||||
|
|
||||||
|
向用户确认需要添加的资源类型:
|
||||||
|
|
||||||
|
| 类型 | 基类 | 用途 | 示例 |
|
||||||
|
|------|------|------|------|
|
||||||
|
| **Bottle** | `Well` (PyLabRobot) | 单个容器(瓶、小瓶、烧杯、反应器) | 试剂瓶、粉末瓶 |
|
||||||
|
| **BottleCarrier** | `ItemizedCarrier` | 多槽位载架(放多个 Bottle) | 6 位试剂架、枪头盒 |
|
||||||
|
| **WareHouse** | `ItemizedCarrier` | 堆栈/仓库(放多个 Carrier) | 4x4 堆栈 |
|
||||||
|
| **Deck** | `Deck` (PyLabRobot) | 工作站台面(放多个 WareHouse) | 反应站 Deck |
|
||||||
|
|
||||||
|
**层级关系:** `Deck` → `WareHouse` → `BottleCarrier` → `Bottle`
|
||||||
|
|
||||||
|
还需确认:
|
||||||
|
- 资源所属的项目/场景(如 bioyond、battery、通用)
|
||||||
|
- 尺寸参数(直径、高度、最大容积等)
|
||||||
|
- 布局参数(行列数、间距等)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第二步:创建资源定义
|
||||||
|
|
||||||
|
### 文件位置
|
||||||
|
|
||||||
|
```
|
||||||
|
unilabos/resources/
|
||||||
|
├── <project>/ # 按项目分组
|
||||||
|
│ ├── bottles.py # Bottle 工厂函数
|
||||||
|
│ ├── bottle_carriers.py # Carrier 工厂函数
|
||||||
|
│ ├── warehouses.py # WareHouse 工厂函数
|
||||||
|
│ └── decks.py # Deck 类定义
|
||||||
|
├── itemized_carrier.py # Bottle, BottleCarrier, ItemizedCarrier 基类
|
||||||
|
├── warehouse.py # WareHouse 基类
|
||||||
|
└── container.py # 通用容器
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2A. 添加 Bottle(工厂函数)
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.resources.itemized_carrier import Bottle
|
||||||
|
|
||||||
|
|
||||||
|
def My_Reagent_Bottle(
|
||||||
|
name: str,
|
||||||
|
diameter: float = 70.0, # 瓶体直径 (mm)
|
||||||
|
height: float = 120.0, # 瓶体高度 (mm)
|
||||||
|
max_volume: float = 500000.0, # 最大容积 (μL)
|
||||||
|
barcode: str = None,
|
||||||
|
) -> Bottle:
|
||||||
|
"""创建试剂瓶"""
|
||||||
|
return Bottle(
|
||||||
|
name=name,
|
||||||
|
diameter=diameter,
|
||||||
|
height=height,
|
||||||
|
max_volume=max_volume,
|
||||||
|
barcode=barcode,
|
||||||
|
model="My_Reagent_Bottle", # 唯一标识,用于注册表和物料映射
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Bottle 参数:**
|
||||||
|
- `name`: 实例名称(运行时唯一)
|
||||||
|
- `diameter`: 瓶体直径 (mm)
|
||||||
|
- `height`: 瓶体高度 (mm)
|
||||||
|
- `max_volume`: 最大容积 (**μL**,注意单位!500mL = 500000)
|
||||||
|
- `barcode`: 条形码(可选)
|
||||||
|
- `model`: 模型标识,与注册表 key 一致
|
||||||
|
|
||||||
|
### 2B. 添加 BottleCarrier(工厂函数)
|
||||||
|
|
||||||
|
```python
|
||||||
|
from pylabrobot.resources import ResourceHolder
|
||||||
|
from pylabrobot.resources.carrier import create_ordered_items_2d
|
||||||
|
|
||||||
|
from unilabos.resources.itemized_carrier import BottleCarrier
|
||||||
|
|
||||||
|
|
||||||
|
def My_6SlotCarrier(name: str) -> BottleCarrier:
|
||||||
|
"""创建 3x2 六槽位载架"""
|
||||||
|
sites = create_ordered_items_2d(
|
||||||
|
klass=ResourceHolder,
|
||||||
|
num_items_x=3, # 列数
|
||||||
|
num_items_y=2, # 行数
|
||||||
|
dx=10.0, # X 起始偏移
|
||||||
|
dy=10.0, # Y 起始偏移
|
||||||
|
dz=5.0, # Z 偏移
|
||||||
|
item_dx=42.0, # X 间距
|
||||||
|
item_dy=35.0, # Y 间距
|
||||||
|
size_x=20.0, # 槽位宽
|
||||||
|
size_y=20.0, # 槽位深
|
||||||
|
size_z=50.0, # 槽位高
|
||||||
|
)
|
||||||
|
carrier = BottleCarrier(
|
||||||
|
name=name,
|
||||||
|
size_x=146.0, # 载架总宽
|
||||||
|
size_y=80.0, # 载架总深
|
||||||
|
size_z=55.0, # 载架总高
|
||||||
|
sites=sites,
|
||||||
|
model="My_6SlotCarrier",
|
||||||
|
)
|
||||||
|
carrier.num_items_x = 3
|
||||||
|
carrier.num_items_y = 2
|
||||||
|
carrier.num_items_z = 1
|
||||||
|
|
||||||
|
# 预装 Bottle(可选)
|
||||||
|
ordering = ["A01", "A02", "A03", "B01", "B02", "B03"]
|
||||||
|
for i in range(6):
|
||||||
|
carrier[i] = My_Reagent_Bottle(f"{ordering[i]}")
|
||||||
|
|
||||||
|
return carrier
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2C. 添加 WareHouse(工厂函数)
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.resources.warehouse import warehouse_factory
|
||||||
|
|
||||||
|
|
||||||
|
def my_warehouse_4x4(name: str) -> "WareHouse":
|
||||||
|
"""创建 4行x4列 堆栈仓库"""
|
||||||
|
return warehouse_factory(
|
||||||
|
name=name,
|
||||||
|
num_items_x=4, # 列数
|
||||||
|
num_items_y=4, # 行数
|
||||||
|
num_items_z=1, # 层数(通常为 1)
|
||||||
|
dx=137.0, # X 起始偏移
|
||||||
|
dy=96.0, # Y 起始偏移
|
||||||
|
dz=120.0, # Z 起始偏移
|
||||||
|
item_dx=137.0, # X 间距
|
||||||
|
item_dy=125.0, # Y 间距
|
||||||
|
item_dz=10.0, # Z 间距(多层时用)
|
||||||
|
resource_size_x=127.0, # 槽位宽
|
||||||
|
resource_size_y=85.0, # 槽位深
|
||||||
|
resource_size_z=100.0, # 槽位高
|
||||||
|
model="my_warehouse_4x4",
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
**`warehouse_factory` 参数说明:**
|
||||||
|
|
||||||
|
| 参数 | 说明 |
|
||||||
|
|------|------|
|
||||||
|
| `num_items_x/y/z` | 列数/行数/层数 |
|
||||||
|
| `dx, dy, dz` | 第一个槽位的起始坐标偏移 |
|
||||||
|
| `item_dx, item_dy, item_dz` | 相邻槽位间距 |
|
||||||
|
| `resource_size_x/y/z` | 单个槽位的物理尺寸 |
|
||||||
|
| `col_offset` | 列命名偏移(如设 4 则从 A05 开始) |
|
||||||
|
| `row_offset` | 行命名偏移(如设 5 则从 F 行开始) |
|
||||||
|
| `layout` | 排序方式:`"col-major"`(列优先,默认)/ `"row-major"`(行优先) |
|
||||||
|
| `removed_positions` | 要移除的位置索引列表 |
|
||||||
|
|
||||||
|
自动生成 `ResourceHolder` 槽位,命名规则为 `A01, B01, C01, D01, A02, ...`(列优先)或 `A01, A02, A03, A04, B01, ...`(行优先)。
|
||||||
|
|
||||||
|
### 2D. 添加 Deck(类定义)
|
||||||
|
|
||||||
|
```python
|
||||||
|
from pylabrobot.resources import Deck, Coordinate
|
||||||
|
|
||||||
|
|
||||||
|
class MyStation_Deck(Deck):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
name: str = "MyStation_Deck",
|
||||||
|
size_x: float = 2700.0,
|
||||||
|
size_y: float = 1080.0,
|
||||||
|
size_z: float = 1500.0,
|
||||||
|
category: str = "deck",
|
||||||
|
setup: bool = False,
|
||||||
|
) -> None:
|
||||||
|
super().__init__(name=name, size_x=size_x, size_y=size_y, size_z=size_z)
|
||||||
|
if setup:
|
||||||
|
self.setup()
|
||||||
|
|
||||||
|
def setup(self) -> None:
|
||||||
|
self.warehouses = {
|
||||||
|
"仓库A": my_warehouse_4x4("仓库A"),
|
||||||
|
"仓库B": my_warehouse_4x4("仓库B"),
|
||||||
|
}
|
||||||
|
self.warehouse_locations = {
|
||||||
|
"仓库A": Coordinate(-200.0, 400.0, 0.0),
|
||||||
|
"仓库B": Coordinate(2350.0, 400.0, 0.0),
|
||||||
|
}
|
||||||
|
for wh_name, wh in self.warehouses.items():
|
||||||
|
self.assign_child_resource(wh, location=self.warehouse_locations[wh_name])
|
||||||
|
```
|
||||||
|
|
||||||
|
**Deck 要点:**
|
||||||
|
- 继承 `pylabrobot.resources.Deck`
|
||||||
|
- `setup()` 创建 WareHouse 并通过 `assign_child_resource` 放置到指定坐标
|
||||||
|
- `setup` 参数控制是否在构造时自动调用 `setup()`(图文件中通过 `config.setup: true` 触发)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第三步:创建注册表 YAML
|
||||||
|
|
||||||
|
路径:`unilabos/registry/resources/<project>/<type>.yaml`
|
||||||
|
|
||||||
|
### Bottle 注册
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
My_Reagent_Bottle:
|
||||||
|
category:
|
||||||
|
- bottles
|
||||||
|
class:
|
||||||
|
module: unilabos.resources.my_project.bottles:My_Reagent_Bottle
|
||||||
|
type: pylabrobot
|
||||||
|
description: 我的试剂瓶
|
||||||
|
handles: []
|
||||||
|
icon: ''
|
||||||
|
init_param_schema: {}
|
||||||
|
version: 1.0.0
|
||||||
|
```
|
||||||
|
|
||||||
|
### Carrier 注册
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
My_6SlotCarrier:
|
||||||
|
category:
|
||||||
|
- bottle_carriers
|
||||||
|
class:
|
||||||
|
module: unilabos.resources.my_project.bottle_carriers:My_6SlotCarrier
|
||||||
|
type: pylabrobot
|
||||||
|
handles: []
|
||||||
|
icon: ''
|
||||||
|
init_param_schema: {}
|
||||||
|
version: 1.0.0
|
||||||
|
```
|
||||||
|
|
||||||
|
### Deck 注册
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
MyStation_Deck:
|
||||||
|
category:
|
||||||
|
- deck
|
||||||
|
class:
|
||||||
|
module: unilabos.resources.my_project.decks:MyStation_Deck
|
||||||
|
type: pylabrobot
|
||||||
|
description: 我的工作站 Deck
|
||||||
|
handles: []
|
||||||
|
icon: ''
|
||||||
|
init_param_schema: {}
|
||||||
|
registry_type: resource
|
||||||
|
version: 1.0.0
|
||||||
|
```
|
||||||
|
|
||||||
|
**注册表规则:**
|
||||||
|
- `class.module` 格式为 `python.module.path:ClassName_or_FunctionName`
|
||||||
|
- `class.type` 固定为 `pylabrobot`
|
||||||
|
- Key(如 `My_Reagent_Bottle`)必须与工厂函数名 / 类名一致
|
||||||
|
- `category` 按类型标注(`bottles`, `bottle_carriers`, `deck` 等)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第四步:在图文件中引用
|
||||||
|
|
||||||
|
### Deck 在工作站中的引用
|
||||||
|
|
||||||
|
工作站节点通过 `deck` 字段引用,Deck 作为子节点:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "my_station",
|
||||||
|
"children": ["my_deck"],
|
||||||
|
"deck": {
|
||||||
|
"data": {
|
||||||
|
"_resource_child_name": "my_deck",
|
||||||
|
"_resource_type": "unilabos.resources.my_project.decks:MyStation_Deck"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "my_deck",
|
||||||
|
"parent": "my_station",
|
||||||
|
"type": "deck",
|
||||||
|
"class": "MyStation_Deck",
|
||||||
|
"config": {"type": "MyStation_Deck", "setup": true}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 物料类型映射(外部系统对接时)
|
||||||
|
|
||||||
|
如果工作站需要与外部系统同步物料,在 config 中配置 `material_type_mappings`:
|
||||||
|
|
||||||
|
```json
|
||||||
|
"material_type_mappings": {
|
||||||
|
"My_Reagent_Bottle": ["试剂瓶", "external-type-uuid"],
|
||||||
|
"My_6SlotCarrier": ["六槽载架", "external-type-uuid"]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第五步:注册 PLR 扩展(如需要)
|
||||||
|
|
||||||
|
如果添加了新的 Deck 类,需要在 `unilabos/resources/plr_additional_res_reg.py` 中导入,使 `find_subclass` 能发现它:
|
||||||
|
|
||||||
|
```python
|
||||||
|
def register():
|
||||||
|
from unilabos.resources.my_project.decks import MyStation_Deck
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第六步:验证
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 1. 资源可导入
|
||||||
|
python -c "from unilabos.resources.my_project.bottles import My_Reagent_Bottle; print(My_Reagent_Bottle('test'))"
|
||||||
|
|
||||||
|
# 2. Deck 可创建
|
||||||
|
python -c "
|
||||||
|
from unilabos.resources.my_project.decks import MyStation_Deck
|
||||||
|
d = MyStation_Deck('test', setup=True)
|
||||||
|
print(d.children)
|
||||||
|
"
|
||||||
|
|
||||||
|
# 3. 启动测试
|
||||||
|
unilab -g <graph>.json --complete_registry
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 工作流清单
|
||||||
|
|
||||||
|
```
|
||||||
|
资源接入进度:
|
||||||
|
- [ ] 1. 确定资源类型(Bottle / Carrier / WareHouse / Deck)
|
||||||
|
- [ ] 2. 创建资源定义(工厂函数/类)
|
||||||
|
- [ ] 3. 创建注册表 YAML (unilabos/registry/resources/<project>/<type>.yaml)
|
||||||
|
- [ ] 4. 在图文件中引用(如需要)
|
||||||
|
- [ ] 5. 注册 PLR 扩展(Deck 类需要)
|
||||||
|
- [ ] 6. 验证
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 高级模式
|
||||||
|
|
||||||
|
实现复杂资源系统时,详见 [reference.md](reference.md):类继承体系完整图、序列化/反序列化流程、Bioyond 物料双向同步、非瓶类资源(ElectrodeSheet / Magazine)、仓库工厂 layout 模式。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 现有资源参考
|
||||||
|
|
||||||
|
| 项目 | Bottles | Carriers | WareHouses | Decks |
|
||||||
|
|------|---------|----------|------------|-------|
|
||||||
|
| bioyond | `bioyond/bottles.py` | `bioyond/bottle_carriers.py` | `bioyond/warehouses.py`, `YB_warehouses.py` | `bioyond/decks.py` |
|
||||||
|
| battery | — | `battery/bottle_carriers.py` | — | — |
|
||||||
|
| 通用 | — | — | `warehouse.py` | — |
|
||||||
|
|
||||||
|
### 关键路径
|
||||||
|
|
||||||
|
| 内容 | 路径 |
|
||||||
|
|------|------|
|
||||||
|
| Bottle/Carrier 基类 | `unilabos/resources/itemized_carrier.py` |
|
||||||
|
| WareHouse 基类 + 工厂 | `unilabos/resources/warehouse.py` |
|
||||||
|
| PLR 注册 | `unilabos/resources/plr_additional_res_reg.py` |
|
||||||
|
| 资源注册表 | `unilabos/registry/resources/` |
|
||||||
|
| 图文件加载 | `unilabos/resources/graphio.py` |
|
||||||
|
| 资源跟踪器 | `unilabos/resources/resource_tracker.py` |
|
||||||
292
.cursor/skills/add-resource/reference.md
Normal file
292
.cursor/skills/add-resource/reference.md
Normal file
@@ -0,0 +1,292 @@
|
|||||||
|
# 资源高级参考
|
||||||
|
|
||||||
|
本文件是 SKILL.md 的补充,包含类继承体系、序列化/反序列化、Bioyond 物料同步、非瓶类资源和仓库工厂模式。Agent 在需要实现这些功能时按需阅读。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 类继承体系
|
||||||
|
|
||||||
|
```
|
||||||
|
PyLabRobot
|
||||||
|
├── Resource (PLR 基类)
|
||||||
|
│ ├── Well
|
||||||
|
│ │ └── Bottle (unilabos) → 瓶/小瓶/烧杯/反应器
|
||||||
|
│ ├── Deck
|
||||||
|
│ │ └── 自定义 Deck 类 (unilabos) → 工作站台面
|
||||||
|
│ ├── ResourceHolder → 槽位占位符
|
||||||
|
│ └── Container
|
||||||
|
│ └── Battery (unilabos) → 组装好的电池
|
||||||
|
│
|
||||||
|
├── ItemizedCarrier (unilabos, 继承 Resource)
|
||||||
|
│ ├── BottleCarrier (unilabos) → 瓶载架
|
||||||
|
│ └── WareHouse (unilabos) → 堆栈仓库
|
||||||
|
│
|
||||||
|
├── ItemizedResource (PLR)
|
||||||
|
│ └── MagazineHolder (unilabos) → 子弹夹载架
|
||||||
|
│
|
||||||
|
└── ResourceStack (PLR)
|
||||||
|
└── Magazine (unilabos) → 子弹夹洞位
|
||||||
|
```
|
||||||
|
|
||||||
|
### Bottle 类细节
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Bottle(Well):
|
||||||
|
def __init__(self, name, diameter, height, max_volume,
|
||||||
|
size_x=0.0, size_y=0.0, size_z=0.0,
|
||||||
|
barcode=None, category="container", model=None, **kwargs):
|
||||||
|
super().__init__(
|
||||||
|
name=name,
|
||||||
|
size_x=diameter, # PLR 用 diameter 作为 size_x/size_y
|
||||||
|
size_y=diameter,
|
||||||
|
size_z=height, # PLR 用 height 作为 size_z
|
||||||
|
max_volume=max_volume,
|
||||||
|
category=category,
|
||||||
|
model=model,
|
||||||
|
bottom_type="flat",
|
||||||
|
cross_section_type="circle"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
注意 `size_x = size_y = diameter`,`size_z = height`。
|
||||||
|
|
||||||
|
### ItemizedCarrier 核心方法
|
||||||
|
|
||||||
|
| 方法 | 说明 |
|
||||||
|
|------|------|
|
||||||
|
| `__getitem__(identifier)` | 通过索引或 Excel 标识(如 `"A01"`)访问槽位 |
|
||||||
|
| `__setitem__(identifier, resource)` | 向槽位放入资源 |
|
||||||
|
| `get_child_identifier(child)` | 获取子资源的标识符 |
|
||||||
|
| `capacity` | 总槽位数 |
|
||||||
|
| `sites` | 所有槽位字典 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 序列化与反序列化
|
||||||
|
|
||||||
|
### PLR ↔ UniLab 转换
|
||||||
|
|
||||||
|
| 函数 | 位置 | 方向 |
|
||||||
|
|------|------|------|
|
||||||
|
| `ResourceTreeSet.from_plr_resources(resources)` | `resource_tracker.py` | PLR → UniLab |
|
||||||
|
| `ResourceTreeSet.to_plr_resources()` | `resource_tracker.py` | UniLab → PLR |
|
||||||
|
|
||||||
|
### `from_plr_resources` 流程
|
||||||
|
|
||||||
|
```
|
||||||
|
PLR Resource
|
||||||
|
↓ build_uuid_mapping (递归生成 UUID)
|
||||||
|
↓ resource.serialize() → dict
|
||||||
|
↓ resource.serialize_all_state() → states
|
||||||
|
↓ resource_plr_inner (递归构建 ResourceDictInstance)
|
||||||
|
ResourceTreeSet
|
||||||
|
```
|
||||||
|
|
||||||
|
关键:每个 PLR 资源通过 `unilabos_uuid` 属性携带 UUID,`unilabos_extra` 携带扩展数据(如 `class` 名)。
|
||||||
|
|
||||||
|
### `to_plr_resources` 流程
|
||||||
|
|
||||||
|
```
|
||||||
|
ResourceTreeSet
|
||||||
|
↓ collect_node_data (收集 UUID、状态、扩展数据)
|
||||||
|
↓ node_to_plr_dict (转为 PLR 字典格式)
|
||||||
|
↓ find_subclass(type_name, PLRResource) (查找 PLR 子类)
|
||||||
|
↓ sub_cls.deserialize(plr_dict) (反序列化)
|
||||||
|
↓ loop_set_uuid, loop_set_extra (递归设置 UUID 和扩展)
|
||||||
|
PLR Resource
|
||||||
|
```
|
||||||
|
|
||||||
|
### Bottle 序列化
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Bottle(Well):
|
||||||
|
def serialize(self) -> dict:
|
||||||
|
data = super().serialize()
|
||||||
|
return {**data, "diameter": self.diameter, "height": self.height}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def deserialize(cls, data: dict, allow_marshal=False):
|
||||||
|
barcode_data = data.pop("barcode", None)
|
||||||
|
instance = super().deserialize(data, allow_marshal=allow_marshal)
|
||||||
|
if barcode_data and isinstance(barcode_data, str):
|
||||||
|
instance.barcode = barcode_data
|
||||||
|
return instance
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. Bioyond 物料同步
|
||||||
|
|
||||||
|
### 双向转换函数
|
||||||
|
|
||||||
|
| 函数 | 位置 | 方向 |
|
||||||
|
|------|------|------|
|
||||||
|
| `resource_bioyond_to_plr(materials, type_mapping, deck)` | `graphio.py` | Bioyond → PLR |
|
||||||
|
| `resource_plr_to_bioyond(resources, type_mapping, warehouse_mapping)` | `graphio.py` | PLR → Bioyond |
|
||||||
|
|
||||||
|
### `resource_bioyond_to_plr` 流程
|
||||||
|
|
||||||
|
```
|
||||||
|
Bioyond 物料列表
|
||||||
|
↓ reverse_type_mapping: {typeName → (model, UUID)}
|
||||||
|
↓ 对每个物料:
|
||||||
|
typeName → 查映射 → model (如 "BIOYOND_PolymerStation_Reactor")
|
||||||
|
initialize_resource({"name": unique_name, "class": model})
|
||||||
|
↓ 设置 unilabos_extra (material_bioyond_id, material_bioyond_name 等)
|
||||||
|
↓ 处理 detail (子物料/坐标)
|
||||||
|
↓ 按 locationName 放入 deck.warehouses 对应槽位
|
||||||
|
PLR 资源列表
|
||||||
|
```
|
||||||
|
|
||||||
|
### `resource_plr_to_bioyond` 流程
|
||||||
|
|
||||||
|
```
|
||||||
|
PLR 资源列表
|
||||||
|
↓ 遍历每个资源:
|
||||||
|
载架(capacity > 1): 生成 details 子物料 + 坐标
|
||||||
|
单瓶: 直接映射
|
||||||
|
↓ type_mapping 查找 typeId
|
||||||
|
↓ warehouse_mapping 查找位置 UUID
|
||||||
|
↓ 组装 Bioyond 格式 (name, typeName, typeId, quantity, Parameters, locations)
|
||||||
|
Bioyond 物料列表
|
||||||
|
```
|
||||||
|
|
||||||
|
### BioyondResourceSynchronizer
|
||||||
|
|
||||||
|
工作站通过 `ResourceSynchronizer` 自动同步物料:
|
||||||
|
|
||||||
|
```python
|
||||||
|
class BioyondResourceSynchronizer(ResourceSynchronizer):
|
||||||
|
def sync_from_external(self) -> bool:
|
||||||
|
all_data = []
|
||||||
|
all_data.extend(api_client.stock_material('{"typeMode": 0}')) # 耗材
|
||||||
|
all_data.extend(api_client.stock_material('{"typeMode": 1}')) # 样品
|
||||||
|
all_data.extend(api_client.stock_material('{"typeMode": 2}')) # 试剂
|
||||||
|
unilab_resources = resource_bioyond_to_plr(
|
||||||
|
all_data,
|
||||||
|
type_mapping=self.workstation.bioyond_config["material_type_mappings"],
|
||||||
|
deck=self.workstation.deck
|
||||||
|
)
|
||||||
|
# 更新 deck 上的资源
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 非瓶类资源
|
||||||
|
|
||||||
|
### ElectrodeSheet(极片)
|
||||||
|
|
||||||
|
路径:`unilabos/resources/battery/electrode_sheet.py`
|
||||||
|
|
||||||
|
```python
|
||||||
|
class ElectrodeSheet(ResourcePLR):
|
||||||
|
"""片状材料(极片、隔膜、弹片、垫片等)"""
|
||||||
|
_unilabos_state = {
|
||||||
|
"diameter": 0.0,
|
||||||
|
"thickness": 0.0,
|
||||||
|
"mass": 0.0,
|
||||||
|
"material_type": "",
|
||||||
|
"color": "",
|
||||||
|
"info": "",
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
工厂函数:`PositiveCan`, `PositiveElectrode`, `NegativeCan`, `NegativeElectrode`, `SpringWasher`, `FlatWasher`, `AluminumFoil`
|
||||||
|
|
||||||
|
### Battery(电池)
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Battery(Container):
|
||||||
|
"""组装好的电池"""
|
||||||
|
_unilabos_state = {
|
||||||
|
"color": "",
|
||||||
|
"electrolyte_name": "",
|
||||||
|
"open_circuit_voltage": 0.0,
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Magazine / MagazineHolder(子弹夹)
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Magazine(ResourceStack):
|
||||||
|
"""子弹夹洞位,可堆叠 ElectrodeSheet"""
|
||||||
|
# direction, max_sheets
|
||||||
|
|
||||||
|
class MagazineHolder(ItemizedResource):
|
||||||
|
"""多洞位子弹夹"""
|
||||||
|
# hole_diameter, hole_depth, max_sheets_per_hole
|
||||||
|
```
|
||||||
|
|
||||||
|
工厂函数 `magazine_factory()` 用 `create_homogeneous_resources` 生成洞位,可选预填 `ElectrodeSheet` 或 `Battery`。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 仓库工厂模式参考
|
||||||
|
|
||||||
|
### 实际 warehouse 工厂函数示例
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 行优先 4x4 仓库
|
||||||
|
def bioyond_warehouse_1x4x4(name: str) -> WareHouse:
|
||||||
|
return warehouse_factory(
|
||||||
|
name=name,
|
||||||
|
num_items_x=4, num_items_y=4, num_items_z=1,
|
||||||
|
dx=10.0, dy=10.0, dz=10.0,
|
||||||
|
item_dx=147.0, item_dy=106.0, item_dz=130.0,
|
||||||
|
layout="row-major", # A01,A02,A03,A04, B01,...
|
||||||
|
)
|
||||||
|
|
||||||
|
# 右侧 4x4 仓库(列名偏移)
|
||||||
|
def bioyond_warehouse_1x4x4_right(name: str) -> WareHouse:
|
||||||
|
return warehouse_factory(
|
||||||
|
name=name,
|
||||||
|
num_items_x=4, num_items_y=4, num_items_z=1,
|
||||||
|
dx=10.0, dy=10.0, dz=10.0,
|
||||||
|
item_dx=147.0, item_dy=106.0, item_dz=130.0,
|
||||||
|
col_offset=4, # A05,A06,A07,A08
|
||||||
|
layout="row-major",
|
||||||
|
)
|
||||||
|
|
||||||
|
# 竖向仓库(站内试剂存放)
|
||||||
|
def bioyond_warehouse_reagent_storage(name: str) -> WareHouse:
|
||||||
|
return warehouse_factory(
|
||||||
|
name=name,
|
||||||
|
num_items_x=1, num_items_y=2, num_items_z=1,
|
||||||
|
dx=10.0, dy=10.0, dz=10.0,
|
||||||
|
item_dx=147.0, item_dy=106.0, item_dz=130.0,
|
||||||
|
layout="vertical-col-major",
|
||||||
|
)
|
||||||
|
|
||||||
|
# 行偏移(F 行开始)
|
||||||
|
def bioyond_warehouse_5x3x1(name: str, row_offset: int = 0) -> WareHouse:
|
||||||
|
return warehouse_factory(
|
||||||
|
name=name,
|
||||||
|
num_items_x=3, num_items_y=5, num_items_z=1,
|
||||||
|
dx=10.0, dy=10.0, dz=10.0,
|
||||||
|
item_dx=159.0, item_dy=183.0, item_dz=130.0,
|
||||||
|
row_offset=row_offset, # 0→A行起,5→F行起
|
||||||
|
layout="row-major",
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
### layout 类型说明
|
||||||
|
|
||||||
|
| layout | 命名顺序 | 适用场景 |
|
||||||
|
|--------|---------|---------|
|
||||||
|
| `col-major` (默认) | A01,B01,C01,D01, A02,B02,... | 列优先,标准堆栈 |
|
||||||
|
| `row-major` | A01,A02,A03,A04, B01,B02,... | 行优先,Bioyond 前端展示 |
|
||||||
|
| `vertical-col-major` | 竖向排列,标签从底部开始 | 竖向仓库(试剂存放、测密度) |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. 关键路径
|
||||||
|
|
||||||
|
| 内容 | 路径 |
|
||||||
|
|------|------|
|
||||||
|
| Bottle/Carrier 基类 | `unilabos/resources/itemized_carrier.py` |
|
||||||
|
| WareHouse 类 + 工厂 | `unilabos/resources/warehouse.py` |
|
||||||
|
| ResourceTreeSet 转换 | `unilabos/resources/resource_tracker.py` |
|
||||||
|
| Bioyond 物料转换 | `unilabos/resources/graphio.py` |
|
||||||
|
| Bioyond 仓库定义 | `unilabos/resources/bioyond/warehouses.py` |
|
||||||
|
| 电池资源 | `unilabos/resources/battery/` |
|
||||||
|
| PLR 注册 | `unilabos/resources/plr_additional_res_reg.py` |
|
||||||
500
.cursor/skills/add-workstation/SKILL.md
Normal file
500
.cursor/skills/add-workstation/SKILL.md
Normal file
@@ -0,0 +1,500 @@
|
|||||||
|
---
|
||||||
|
name: add-workstation
|
||||||
|
description: Guide for adding new workstations to Uni-Lab-OS (接入新工作站). Walks through workstation type selection, sub-device composition, external system integration, driver creation, registry YAML, deck setup, and graph file configuration. Use when the user wants to add/integrate a new workstation, create a workstation driver, configure a station with sub-devices, set up deck and materials, or mentions 工作站/工站/station/workstation.
|
||||||
|
---
|
||||||
|
|
||||||
|
# Uni-Lab-OS 工作站接入指南
|
||||||
|
|
||||||
|
工作站(workstation)是组合多个子设备的大型设备,拥有独立的物料管理系统(PLR Deck)和工作流引擎。本指南覆盖从需求分析到验证的全流程。
|
||||||
|
|
||||||
|
> **前置知识**:工作站接入基于 `docs/ai_guides/add_device.md` 的通用设备接入框架,但有显著差异。阅读本指南前无需先读通用指南。
|
||||||
|
|
||||||
|
## 第一步:确定工作站类型
|
||||||
|
|
||||||
|
向用户确认以下信息:
|
||||||
|
|
||||||
|
**Q1: 工作站的业务场景?**
|
||||||
|
|
||||||
|
| 类型 | 基类 | 适用场景 | 示例 |
|
||||||
|
|------|------|----------|------|
|
||||||
|
| **Protocol 工作站** | `ProtocolNode` | 标准化学操作协议(过滤、转移、加热等) | FilterProtocolStation |
|
||||||
|
| **外部系统工作站** | `WorkstationBase` | 与外部 LIMS/MES 系统对接,有专属 API | BioyondStation |
|
||||||
|
| **硬件控制工作站** | `WorkstationBase` | 直接控制 PLC/硬件,无外部系统 | CoinCellAssembly |
|
||||||
|
|
||||||
|
**Q2: 工作站英文名称?**(如 `my_reaction_station`)
|
||||||
|
|
||||||
|
**Q3: 与外部系统的交互方式?**
|
||||||
|
|
||||||
|
| 方式 | 适用场景 | 需要的配置 |
|
||||||
|
|------|----------|-----------|
|
||||||
|
| 无外部系统 | Protocol 工作站、纯硬件控制 | 无 |
|
||||||
|
| HTTP API | LIMS/MES 系统(如 Bioyond) | `api_host`, `api_key` |
|
||||||
|
| Modbus TCP | PLC 控制 | `address`, `port` |
|
||||||
|
| OPC UA | 工业设备 | `url` |
|
||||||
|
|
||||||
|
**Q4: 子设备组成?**
|
||||||
|
- 列出所有子设备(如反应器、泵、阀、传感器等)
|
||||||
|
- 哪些是已有设备类型?哪些需要新增?
|
||||||
|
- 子设备之间的硬件代理关系(如泵通过串口设备通信)
|
||||||
|
|
||||||
|
**Q5: 物料管理需求?**
|
||||||
|
- 是否需要 Deck(物料面板)?
|
||||||
|
- 物料类型(plate、tip_rack、bottle 等)
|
||||||
|
- 是否需要与外部物料系统同步?
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第二步:理解工作站架构
|
||||||
|
|
||||||
|
工作站与普通设备的核心差异:
|
||||||
|
|
||||||
|
| 维度 | 普通设备 | 工作站 |
|
||||||
|
|------|---------|--------|
|
||||||
|
| 基类 | 无(纯 Python 类) | `WorkstationBase` 或 `ProtocolNode` |
|
||||||
|
| ROS 节点 | `BaseROS2DeviceNode` | `ROS2WorkstationNode` |
|
||||||
|
| 状态管理 | `self.data` 字典 | 通常不用 `self.data`,用 `@property` 直接访问 |
|
||||||
|
| 子设备 | 无 | `children` 列表,通过 `self._children` 访问 |
|
||||||
|
| 物料 | 无 | `self.deck`(PLR Deck) |
|
||||||
|
| 图文件角色 | `parent: null` 或 `parent: "<station>"` | `parent: null`,含 `children` 和 `deck` |
|
||||||
|
|
||||||
|
### 继承体系
|
||||||
|
|
||||||
|
`WorkstationBase` (ABC) → `ProtocolNode` (通用协议) / `BioyondWorkstation` (→ ReactionStation, DispensingStation) / `CoinCellAssemblyWorkstation` (硬件控制)
|
||||||
|
|
||||||
|
### ROS 层
|
||||||
|
|
||||||
|
`ROS2WorkstationNode` 额外负责:初始化 children 子设备节点、为子设备创建 ActionClient、配置硬件代理、为 protocol_type 创建协议 ActionServer。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第三步:创建驱动文件
|
||||||
|
|
||||||
|
文件路径:`unilabos/devices/workstation/<station_name>/<station_name>.py`
|
||||||
|
|
||||||
|
### 模板 A:基于外部系统的工作站
|
||||||
|
|
||||||
|
适用于与 LIMS/MES 等外部系统对接的场景。
|
||||||
|
|
||||||
|
```python
|
||||||
|
import logging
|
||||||
|
from typing import Dict, Any, Optional, List
|
||||||
|
from pylabrobot.resources import Deck
|
||||||
|
|
||||||
|
from unilabos.devices.workstation.workstation_base import WorkstationBase
|
||||||
|
|
||||||
|
try:
|
||||||
|
from unilabos.ros.nodes.presets.workstation import ROS2WorkstationNode
|
||||||
|
except ImportError:
|
||||||
|
ROS2WorkstationNode = None
|
||||||
|
|
||||||
|
|
||||||
|
class MyWorkstation(WorkstationBase):
|
||||||
|
"""工作站描述"""
|
||||||
|
|
||||||
|
_ros_node: "ROS2WorkstationNode"
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
config: dict = None,
|
||||||
|
deck: Optional[Deck] = None,
|
||||||
|
protocol_type: list = None,
|
||||||
|
**kwargs,
|
||||||
|
):
|
||||||
|
super().__init__(deck=deck, **kwargs)
|
||||||
|
self.config = config or {}
|
||||||
|
self.logger = logging.getLogger(f"MyWorkstation")
|
||||||
|
|
||||||
|
# 外部系统连接配置
|
||||||
|
self.api_host = self.config.get("api_host", "")
|
||||||
|
self.api_key = self.config.get("api_key", "")
|
||||||
|
|
||||||
|
# 工作站业务状态(不同于 self.data 模式)
|
||||||
|
self._status = "Idle"
|
||||||
|
|
||||||
|
def post_init(self, ros_node: "ROS2WorkstationNode") -> None:
|
||||||
|
super().post_init(ros_node)
|
||||||
|
# 在这里启动后台服务、连接监控等
|
||||||
|
|
||||||
|
# ============ 子设备访问 ============
|
||||||
|
|
||||||
|
def _get_child_device(self, device_id: str):
|
||||||
|
"""通过 ID 获取子设备节点"""
|
||||||
|
return self._children.get(device_id)
|
||||||
|
|
||||||
|
# ============ 动作方法 ============
|
||||||
|
|
||||||
|
async def scheduler_start(self, **kwargs) -> Dict[str, Any]:
|
||||||
|
"""启动调度器"""
|
||||||
|
return {"success": True}
|
||||||
|
|
||||||
|
async def create_order(self, json_str: str, **kwargs) -> Dict[str, Any]:
|
||||||
|
"""创建工单"""
|
||||||
|
return {"success": True}
|
||||||
|
|
||||||
|
# ============ 属性 ============
|
||||||
|
|
||||||
|
@property
|
||||||
|
def workflow_sequence(self) -> str:
|
||||||
|
return "[]"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def material_info(self) -> str:
|
||||||
|
return "{}"
|
||||||
|
```
|
||||||
|
|
||||||
|
### 模板 B:基于硬件控制的工作站
|
||||||
|
|
||||||
|
适用于直接与 PLC/硬件通信的场景。
|
||||||
|
|
||||||
|
```python
|
||||||
|
import logging
|
||||||
|
from typing import Dict, Any, Optional
|
||||||
|
from pylabrobot.resources import Deck
|
||||||
|
|
||||||
|
from unilabos.devices.workstation.workstation_base import WorkstationBase
|
||||||
|
|
||||||
|
try:
|
||||||
|
from unilabos.ros.nodes.presets.workstation import ROS2WorkstationNode
|
||||||
|
except ImportError:
|
||||||
|
ROS2WorkstationNode = None
|
||||||
|
|
||||||
|
|
||||||
|
class MyHardwareWorkstation(WorkstationBase):
|
||||||
|
"""硬件控制工作站"""
|
||||||
|
|
||||||
|
_ros_node: "ROS2WorkstationNode"
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
config: dict = None,
|
||||||
|
deck: Optional[Deck] = None,
|
||||||
|
address: str = "192.168.1.100",
|
||||||
|
port: str = "502",
|
||||||
|
debug_mode: bool = False,
|
||||||
|
*args,
|
||||||
|
**kwargs,
|
||||||
|
):
|
||||||
|
super().__init__(deck=deck, *args, **kwargs)
|
||||||
|
self.config = config or {}
|
||||||
|
self.address = address
|
||||||
|
self.port = int(port)
|
||||||
|
self.debug_mode = debug_mode
|
||||||
|
self.logger = logging.getLogger("MyHardwareWorkstation")
|
||||||
|
|
||||||
|
# 初始化通信客户端
|
||||||
|
if not debug_mode:
|
||||||
|
from unilabos.device_comms.modbus_plc.client import ModbusTcpClient
|
||||||
|
self.client = ModbusTcpClient(host=self.address, port=self.port)
|
||||||
|
else:
|
||||||
|
self.client = None
|
||||||
|
|
||||||
|
def post_init(self, ros_node: "ROS2WorkstationNode") -> None:
|
||||||
|
super().post_init(ros_node)
|
||||||
|
|
||||||
|
# ============ 硬件读写 ============
|
||||||
|
|
||||||
|
def _read_register(self, name: str):
|
||||||
|
"""读取 Modbus 寄存器"""
|
||||||
|
if self.debug_mode:
|
||||||
|
return 0
|
||||||
|
# 实际读取逻辑
|
||||||
|
pass
|
||||||
|
|
||||||
|
# ============ 动作方法 ============
|
||||||
|
|
||||||
|
async def start_process(self, **kwargs) -> Dict[str, Any]:
|
||||||
|
"""启动加工流程"""
|
||||||
|
return {"success": True}
|
||||||
|
|
||||||
|
async def stop_process(self, **kwargs) -> Dict[str, Any]:
|
||||||
|
"""停止加工流程"""
|
||||||
|
return {"success": True}
|
||||||
|
|
||||||
|
# ============ 属性(从硬件实时读取)============
|
||||||
|
|
||||||
|
@property
|
||||||
|
def sys_status(self) -> str:
|
||||||
|
return str(self._read_register("SYS_STATUS"))
|
||||||
|
```
|
||||||
|
|
||||||
|
### 模板 C:Protocol 工作站
|
||||||
|
|
||||||
|
适用于标准化学操作协议的场景,直接使用 `ProtocolNode`。
|
||||||
|
|
||||||
|
```python
|
||||||
|
from typing import List, Optional
|
||||||
|
from pylabrobot.resources import Resource as PLRResource
|
||||||
|
|
||||||
|
from unilabos.devices.workstation.workstation_base import ProtocolNode
|
||||||
|
|
||||||
|
|
||||||
|
class MyProtocolStation(ProtocolNode):
|
||||||
|
"""Protocol 工作站 — 使用标准化学操作协议"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
protocol_type: List[str],
|
||||||
|
deck: Optional[PLRResource] = None,
|
||||||
|
*args,
|
||||||
|
**kwargs,
|
||||||
|
):
|
||||||
|
super().__init__(protocol_type=protocol_type, deck=deck, *args, **kwargs)
|
||||||
|
```
|
||||||
|
|
||||||
|
> Protocol 工作站通常不需要自定义驱动类,直接使用 `ProtocolNode` 并在注册表和图文件中配置 `protocol_type` 即可。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第四步:创建子设备驱动(如需要)
|
||||||
|
|
||||||
|
工作站的子设备本身是独立设备。按 `docs/ai_guides/add_device.md` 的标准流程创建。
|
||||||
|
|
||||||
|
子设备的关键约束:
|
||||||
|
- 在图文件中 `parent` 指向工作站 ID
|
||||||
|
- 图文件中在工作站的 `children` 数组里列出
|
||||||
|
- 如需硬件代理,在子设备的 `config.hardware_interface.name` 指向通信设备 ID
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第五步:创建注册表 YAML
|
||||||
|
|
||||||
|
路径:`unilabos/registry/devices/<station_name>.yaml`
|
||||||
|
|
||||||
|
### 最小配置
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
my_workstation:
|
||||||
|
category:
|
||||||
|
- workstation
|
||||||
|
class:
|
||||||
|
module: unilabos.devices.workstation.my_station.my_station:MyWorkstation
|
||||||
|
type: python
|
||||||
|
```
|
||||||
|
|
||||||
|
启动时 `--complete_registry` 自动补全 `status_types` 和 `action_value_mappings`。
|
||||||
|
|
||||||
|
### 完整配置参考
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
my_workstation:
|
||||||
|
description: "我的工作站"
|
||||||
|
version: "1.0.0"
|
||||||
|
category:
|
||||||
|
- workstation
|
||||||
|
- my_category
|
||||||
|
class:
|
||||||
|
module: unilabos.devices.workstation.my_station.my_station:MyWorkstation
|
||||||
|
type: python
|
||||||
|
status_types:
|
||||||
|
workflow_sequence: String
|
||||||
|
material_info: String
|
||||||
|
action_value_mappings:
|
||||||
|
scheduler_start:
|
||||||
|
type: UniLabJsonCommandAsync
|
||||||
|
goal: {}
|
||||||
|
result:
|
||||||
|
success: success
|
||||||
|
create_order:
|
||||||
|
type: UniLabJsonCommandAsync
|
||||||
|
goal:
|
||||||
|
json_str: json_str
|
||||||
|
result:
|
||||||
|
success: success
|
||||||
|
init_param_schema:
|
||||||
|
config:
|
||||||
|
type: object
|
||||||
|
deck:
|
||||||
|
type: object
|
||||||
|
protocol_type:
|
||||||
|
type: array
|
||||||
|
```
|
||||||
|
|
||||||
|
### 子设备注册表
|
||||||
|
|
||||||
|
子设备有独立的注册表文件,需要在 `category` 中包含工作站标识:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
my_reactor:
|
||||||
|
category:
|
||||||
|
- reactor
|
||||||
|
- my_workstation
|
||||||
|
class:
|
||||||
|
module: unilabos.devices.workstation.my_station.my_reactor:MyReactor
|
||||||
|
type: python
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第六步:配置 Deck 资源(如需要)
|
||||||
|
|
||||||
|
如果工作站有物料管理需求,需要定义 Deck 类。
|
||||||
|
|
||||||
|
### 使用已有 Deck 类
|
||||||
|
|
||||||
|
查看 `unilabos/resources/` 目录下是否有适用的 Deck 类。
|
||||||
|
|
||||||
|
### 创建自定义 Deck
|
||||||
|
|
||||||
|
在 `unilabos/resources/<category>/decks.py` 中定义:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from pylabrobot.resources import Deck
|
||||||
|
from pylabrobot.resources.coordinate import Coordinate
|
||||||
|
|
||||||
|
|
||||||
|
def MyStation_Deck(name: str = "MyStation_Deck") -> Deck:
|
||||||
|
deck = Deck(name=name, size_x=2700.0, size_y=1080.0, size_z=1500.0)
|
||||||
|
# 在 deck 上定义子资源位置(carrier、plate 等)
|
||||||
|
return deck
|
||||||
|
```
|
||||||
|
|
||||||
|
在 `unilabos/resources/<category>/` 下注册或通过注册表引用。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第七步:配置图文件
|
||||||
|
|
||||||
|
图文件路径:`unilabos/test/experiments/<station_name>.json`
|
||||||
|
|
||||||
|
### 完整结构
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"nodes": [
|
||||||
|
{
|
||||||
|
"id": "my_station",
|
||||||
|
"name": "my_station",
|
||||||
|
"children": ["my_deck", "sub_device_1", "sub_device_2"],
|
||||||
|
"parent": null,
|
||||||
|
"type": "device",
|
||||||
|
"class": "my_workstation",
|
||||||
|
"position": {"x": 0, "y": 0, "z": 0},
|
||||||
|
"config": {
|
||||||
|
"api_host": "http://192.168.1.100:8080",
|
||||||
|
"api_key": "YOUR_KEY"
|
||||||
|
},
|
||||||
|
"deck": {
|
||||||
|
"data": {
|
||||||
|
"_resource_child_name": "my_deck",
|
||||||
|
"_resource_type": "unilabos.resources.my_module.decks:MyStation_Deck"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"size_x": 2700.0,
|
||||||
|
"size_y": 1080.0,
|
||||||
|
"size_z": 1500.0,
|
||||||
|
"protocol_type": [],
|
||||||
|
"data": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "my_deck",
|
||||||
|
"name": "my_deck",
|
||||||
|
"children": [],
|
||||||
|
"parent": "my_station",
|
||||||
|
"type": "deck",
|
||||||
|
"class": "MyStation_Deck",
|
||||||
|
"position": {"x": 0, "y": 0, "z": 0},
|
||||||
|
"config": {
|
||||||
|
"type": "MyStation_Deck",
|
||||||
|
"setup": true,
|
||||||
|
"rotation": {"x": 0, "y": 0, "z": 0, "type": "Rotation"}
|
||||||
|
},
|
||||||
|
"data": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "sub_device_1",
|
||||||
|
"name": "sub_device_1",
|
||||||
|
"children": [],
|
||||||
|
"parent": "my_station",
|
||||||
|
"type": "device",
|
||||||
|
"class": "sub_device_registry_name",
|
||||||
|
"position": {"x": 100, "y": 0, "z": 0},
|
||||||
|
"config": {},
|
||||||
|
"data": {}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 图文件规则
|
||||||
|
|
||||||
|
| 字段 | 说明 |
|
||||||
|
|------|------|
|
||||||
|
| `id` | 节点唯一标识,与 `children` 数组中的引用一致 |
|
||||||
|
| `children` | 包含 deck ID 和所有子设备 ID |
|
||||||
|
| `parent` | 工作站节点为 `null`;子设备/deck 指向工作站 ID |
|
||||||
|
| `type` | 工作站和子设备为 `"device"`;deck 为 `"deck"` |
|
||||||
|
| `class` | 对应注册表中的设备名 |
|
||||||
|
| `deck.data._resource_child_name` | 必须与 deck 节点的 `id` 一致 |
|
||||||
|
| `deck.data._resource_type` | Deck 工厂函数的完整 Python 路径 |
|
||||||
|
| `protocol_type` | Protocol 工作站填入协议名列表;否则为 `[]` |
|
||||||
|
| `config` | 传入驱动 `__init__` 的 `config` 参数 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第八步:验证
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 1. 模块可导入
|
||||||
|
python -c "from unilabos.devices.workstation.<name>.<name> import <ClassName>"
|
||||||
|
|
||||||
|
# 2. 注册表补全
|
||||||
|
unilab -g <graph>.json --complete_registry
|
||||||
|
|
||||||
|
# 3. 启动测试
|
||||||
|
unilab -g <graph>.json
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 高级模式
|
||||||
|
|
||||||
|
实现外部系统对接型工作站时,详见 [reference.md](reference.md):RPC 客户端、HTTP 回调服务、连接监控、Config 结构模式(material_type_mappings / warehouse_mapping / workflow_mappings)、ResourceSynchronizer、update_resource、工作流序列、站间物料转移、post_init 完整模式。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 关键规则
|
||||||
|
|
||||||
|
1. **`__init__` 必须接受 `deck` 和 `**kwargs`** — `WorkstationBase.__init__` 需要 `deck` 参数
|
||||||
|
2. **通过 `self._children` 访问子设备** — 不要自行维护子设备引用
|
||||||
|
3. **`post_init` 中启动后台服务** — 不要在 `__init__` 中启动网络连接
|
||||||
|
4. **异步方法使用 `await self._ros_node.sleep()`** — 禁止 `time.sleep()` 和 `asyncio.sleep()`
|
||||||
|
5. **子设备在图文件中声明** — 不在驱动代码中创建子设备实例
|
||||||
|
6. **`deck` 配置中的 `_resource_child_name` 必须与 deck 节点 ID 一致**
|
||||||
|
7. **Protocol 工作站优先使用 `ProtocolNode`** — 不需要自定义类
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 工作流清单
|
||||||
|
|
||||||
|
```
|
||||||
|
工作站接入进度:
|
||||||
|
- [ ] 1. 确定工作站类型(Protocol / 外部系统 / 硬件控制)
|
||||||
|
- [ ] 2. 确认子设备组成和物料需求
|
||||||
|
- [ ] 3. 创建工作站驱动 unilabos/devices/workstation/<name>/<name>.py
|
||||||
|
- [ ] 4. 创建子设备驱动(如需要,按 add_device.md 流程)
|
||||||
|
- [ ] 5. 创建注册表 unilabos/registry/devices/<name>.yaml
|
||||||
|
- [ ] 6. 创建/选择 Deck 资源类(如需要)
|
||||||
|
- [ ] 7. 配置图文件 unilabos/test/experiments/<name>.json
|
||||||
|
- [ ] 8. 验证:可导入 + 注册表补全 + 启动测试
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 现有工作站参考
|
||||||
|
|
||||||
|
| 工作站 | 注册表名 | 驱动类 | 类型 |
|
||||||
|
|--------|----------|--------|------|
|
||||||
|
| Protocol 通用 | `workstation` | `ProtocolNode` | Protocol |
|
||||||
|
| Bioyond 反应站 | `reaction_station.bioyond` | `BioyondReactionStation` | 外部系统 |
|
||||||
|
| Bioyond 配液站 | `bioyond_dispensing_station` | `BioyondDispensingStation` | 外部系统 |
|
||||||
|
| 纽扣电池组装 | `coincellassemblyworkstation_device` | `CoinCellAssemblyWorkstation` | 硬件控制 |
|
||||||
|
|
||||||
|
### 参考文件路径
|
||||||
|
|
||||||
|
- 基类: `unilabos/devices/workstation/workstation_base.py`
|
||||||
|
- Bioyond 基类: `unilabos/devices/workstation/bioyond_studio/station.py`
|
||||||
|
- 反应站: `unilabos/devices/workstation/bioyond_studio/reaction_station/reaction_station.py`
|
||||||
|
- 配液站: `unilabos/devices/workstation/bioyond_studio/dispensing_station/dispensing_station.py`
|
||||||
|
- 纽扣电池: `unilabos/devices/workstation/coin_cell_assembly/coin_cell_assembly.py`
|
||||||
|
- ROS 节点: `unilabos/ros/nodes/presets/workstation.py`
|
||||||
|
- 图文件: `unilabos/test/experiments/reaction_station_bioyond.json`, `dispensing_station_bioyond.json`
|
||||||
371
.cursor/skills/add-workstation/reference.md
Normal file
371
.cursor/skills/add-workstation/reference.md
Normal file
@@ -0,0 +1,371 @@
|
|||||||
|
# 工作站高级模式参考
|
||||||
|
|
||||||
|
本文件是 SKILL.md 的补充,包含外部系统集成、物料同步、配置结构等高级模式。
|
||||||
|
Agent 在需要实现这些功能时按需阅读。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 外部系统集成模式
|
||||||
|
|
||||||
|
### 1.1 RPC 客户端
|
||||||
|
|
||||||
|
与外部 LIMS/MES 系统通信的标准模式。继承 `BaseRequest`,所有接口统一用 POST。
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.device_comms.rpc import BaseRequest
|
||||||
|
|
||||||
|
|
||||||
|
class MySystemRPC(BaseRequest):
|
||||||
|
"""外部系统 RPC 客户端"""
|
||||||
|
|
||||||
|
def __init__(self, host: str, api_key: str):
|
||||||
|
super().__init__(host)
|
||||||
|
self.api_key = api_key
|
||||||
|
|
||||||
|
def _request(self, endpoint: str, data: dict = None) -> dict:
|
||||||
|
return self.post(
|
||||||
|
url=f"{self.host}/api/{endpoint}",
|
||||||
|
params={
|
||||||
|
"apiKey": self.api_key,
|
||||||
|
"requestTime": self.get_current_time_iso8601(),
|
||||||
|
"data": data or {},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
def query_status(self) -> dict:
|
||||||
|
return self._request("status/query")
|
||||||
|
|
||||||
|
def create_order(self, order_data: dict) -> dict:
|
||||||
|
return self._request("order/create", order_data)
|
||||||
|
```
|
||||||
|
|
||||||
|
参考:`unilabos/devices/workstation/bioyond_studio/bioyond_rpc.py`(`BioyondV1RPC`)
|
||||||
|
|
||||||
|
### 1.2 HTTP 回调服务
|
||||||
|
|
||||||
|
接收外部系统报送的标准模式。使用 `WorkstationHTTPService`,在 `post_init` 中启动。
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.devices.workstation.workstation_http_service import WorkstationHTTPService
|
||||||
|
|
||||||
|
|
||||||
|
class MyWorkstation(WorkstationBase):
|
||||||
|
def __init__(self, config=None, deck=None, **kwargs):
|
||||||
|
super().__init__(deck=deck, **kwargs)
|
||||||
|
self.config = config or {}
|
||||||
|
http_cfg = self.config.get("http_service_config", {})
|
||||||
|
self._http_service_config = {
|
||||||
|
"host": http_cfg.get("http_service_host", "127.0.0.1"),
|
||||||
|
"port": http_cfg.get("http_service_port", 8080),
|
||||||
|
}
|
||||||
|
self.http_service = None
|
||||||
|
|
||||||
|
def post_init(self, ros_node):
|
||||||
|
super().post_init(ros_node)
|
||||||
|
self.http_service = WorkstationHTTPService(
|
||||||
|
workstation_instance=self,
|
||||||
|
host=self._http_service_config["host"],
|
||||||
|
port=self._http_service_config["port"],
|
||||||
|
)
|
||||||
|
self.http_service.start()
|
||||||
|
```
|
||||||
|
|
||||||
|
**HTTP 服务路由**(固定端点,由 `WorkstationHTTPHandler` 自动分发):
|
||||||
|
|
||||||
|
| 端点 | 调用的工作站方法 |
|
||||||
|
|------|-----------------|
|
||||||
|
| `/report/step_finish` | `process_step_finish_report(report_request)` |
|
||||||
|
| `/report/sample_finish` | `process_sample_finish_report(report_request)` |
|
||||||
|
| `/report/order_finish` | `process_order_finish_report(report_request, used_materials)` |
|
||||||
|
| `/report/material_change` | `process_material_change_report(report_data)` |
|
||||||
|
| `/report/error_handling` | `handle_external_error(error_data)` |
|
||||||
|
|
||||||
|
实现对应方法即可接收回调:
|
||||||
|
|
||||||
|
```python
|
||||||
|
def process_step_finish_report(self, report_request) -> Dict[str, Any]:
|
||||||
|
"""处理步骤完成报告"""
|
||||||
|
step_name = report_request.data.get("stepName")
|
||||||
|
return {"success": True, "message": f"步骤 {step_name} 已处理"}
|
||||||
|
|
||||||
|
def process_order_finish_report(self, report_request, used_materials) -> Dict[str, Any]:
|
||||||
|
"""处理订单完成报告"""
|
||||||
|
order_code = report_request.data.get("orderCode")
|
||||||
|
return {"success": True}
|
||||||
|
```
|
||||||
|
|
||||||
|
参考:`unilabos/devices/workstation/workstation_http_service.py`
|
||||||
|
|
||||||
|
### 1.3 连接监控
|
||||||
|
|
||||||
|
独立线程周期性检测外部系统连接状态,状态变化时发布 ROS 事件。
|
||||||
|
|
||||||
|
```python
|
||||||
|
class ConnectionMonitor:
|
||||||
|
def __init__(self, workstation, check_interval=30):
|
||||||
|
self.workstation = workstation
|
||||||
|
self.check_interval = check_interval
|
||||||
|
self._running = False
|
||||||
|
self._thread = None
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
self._running = True
|
||||||
|
self._thread = threading.Thread(target=self._monitor_loop, daemon=True)
|
||||||
|
self._thread.start()
|
||||||
|
|
||||||
|
def _monitor_loop(self):
|
||||||
|
while self._running:
|
||||||
|
try:
|
||||||
|
# 调用外部系统接口检测连接
|
||||||
|
self.workstation.hardware_interface.ping()
|
||||||
|
status = "online"
|
||||||
|
except Exception:
|
||||||
|
status = "offline"
|
||||||
|
time.sleep(self.check_interval)
|
||||||
|
```
|
||||||
|
|
||||||
|
参考:`unilabos/devices/workstation/bioyond_studio/station.py`(`ConnectionMonitor`)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. Config 结构模式
|
||||||
|
|
||||||
|
工作站的 `config` 在图文件中定义,传入 `__init__`。以下是常见字段模式:
|
||||||
|
|
||||||
|
### 2.1 外部系统连接
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"api_host": "http://192.168.1.100:8080",
|
||||||
|
"api_key": "YOUR_API_KEY"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2.2 HTTP 回调服务
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"http_service_config": {
|
||||||
|
"http_service_host": "127.0.0.1",
|
||||||
|
"http_service_port": 8080
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2.3 物料类型映射
|
||||||
|
|
||||||
|
将 PLR 资源类名映射到外部系统的物料类型(名称 + UUID)。用于双向物料转换。
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"material_type_mappings": {
|
||||||
|
"PLR_ResourceClassName": ["外部系统显示名", "external-type-uuid"],
|
||||||
|
"BIOYOND_PolymerStation_Reactor": ["反应器", "3a14233b-902d-0d7b-..."]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2.4 仓库映射
|
||||||
|
|
||||||
|
将仓库名映射到外部系统的仓库 UUID 和库位 UUID。用于入库/出库操作。
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"warehouse_mapping": {
|
||||||
|
"仓库名": {
|
||||||
|
"uuid": "warehouse-uuid",
|
||||||
|
"site_uuids": {
|
||||||
|
"A01": "site-uuid-A01",
|
||||||
|
"A02": "site-uuid-A02"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2.5 工作流映射
|
||||||
|
|
||||||
|
将内部工作流名映射到外部系统的工作流 ID。
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"workflow_mappings": {
|
||||||
|
"internal_workflow_name": "external-workflow-uuid"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2.6 物料默认参数
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"material_default_parameters": {
|
||||||
|
"NMP": {
|
||||||
|
"unit": "毫升",
|
||||||
|
"density": "1.03",
|
||||||
|
"densityUnit": "g/mL",
|
||||||
|
"description": "N-甲基吡咯烷酮"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 资源同步机制
|
||||||
|
|
||||||
|
### 3.1 ResourceSynchronizer
|
||||||
|
|
||||||
|
抽象基类,用于与外部物料系统双向同步。定义在 `workstation_base.py`。
|
||||||
|
|
||||||
|
```python
|
||||||
|
from unilabos.devices.workstation.workstation_base import ResourceSynchronizer
|
||||||
|
|
||||||
|
|
||||||
|
class MyResourceSynchronizer(ResourceSynchronizer):
|
||||||
|
def __init__(self, workstation, api_client):
|
||||||
|
super().__init__(workstation)
|
||||||
|
self.api_client = api_client
|
||||||
|
|
||||||
|
def sync_from_external(self) -> bool:
|
||||||
|
"""从外部系统拉取物料到 deck"""
|
||||||
|
external_materials = self.api_client.list_materials()
|
||||||
|
for material in external_materials:
|
||||||
|
plr_resource = self._convert_to_plr(material)
|
||||||
|
self.workstation.deck.assign_child_resource(plr_resource, coordinate)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def sync_to_external(self, plr_resource) -> bool:
|
||||||
|
"""将 deck 中的物料变更推送到外部系统"""
|
||||||
|
external_data = self._convert_from_plr(plr_resource)
|
||||||
|
self.api_client.update_material(external_data)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def handle_external_change(self, change_info) -> bool:
|
||||||
|
"""处理外部系统推送的物料变更"""
|
||||||
|
return True
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3.2 update_resource — 上传资源树到云端
|
||||||
|
|
||||||
|
将 PLR Deck 序列化后通过 ROS 服务上传。典型使用场景:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 在 post_init 中上传初始 deck
|
||||||
|
from unilabos.ros.nodes.base_device_node import ROS2DeviceNode
|
||||||
|
|
||||||
|
ROS2DeviceNode.run_async_func(
|
||||||
|
self._ros_node.update_resource, True,
|
||||||
|
**{"resources": [self.deck]}
|
||||||
|
)
|
||||||
|
|
||||||
|
# 在动作方法中更新特定资源
|
||||||
|
ROS2DeviceNode.run_async_func(
|
||||||
|
self._ros_node.update_resource, True,
|
||||||
|
**{"resources": [updated_plate]}
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 工作流序列管理
|
||||||
|
|
||||||
|
工作站通过 `workflow_sequence` 属性管理任务队列(JSON 字符串形式)。
|
||||||
|
|
||||||
|
```python
|
||||||
|
class MyWorkstation(WorkstationBase):
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
super().__init__(**kwargs)
|
||||||
|
self._workflow_sequence = []
|
||||||
|
|
||||||
|
@property
|
||||||
|
def workflow_sequence(self) -> str:
|
||||||
|
"""返回 JSON 字符串,ROS 自动发布"""
|
||||||
|
import json
|
||||||
|
return json.dumps(self._workflow_sequence)
|
||||||
|
|
||||||
|
async def append_to_workflow_sequence(self, workflow_name: str) -> Dict[str, Any]:
|
||||||
|
"""添加工作流到队列"""
|
||||||
|
self._workflow_sequence.append({
|
||||||
|
"name": workflow_name,
|
||||||
|
"status": "pending",
|
||||||
|
"created_at": time.time(),
|
||||||
|
})
|
||||||
|
return {"success": True}
|
||||||
|
|
||||||
|
async def clear_workflows(self) -> Dict[str, Any]:
|
||||||
|
"""清空工作流队列"""
|
||||||
|
self._workflow_sequence = []
|
||||||
|
return {"success": True}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 站间物料转移
|
||||||
|
|
||||||
|
工作站之间转移物料的模式。通过 ROS ActionClient 调用目标站的动作。
|
||||||
|
|
||||||
|
```python
|
||||||
|
async def transfer_materials_to_another_station(
|
||||||
|
self,
|
||||||
|
target_device_id: str,
|
||||||
|
transfer_groups: list,
|
||||||
|
**kwargs,
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""将物料转移到另一个工作站"""
|
||||||
|
target_node = self._children.get(target_device_id)
|
||||||
|
if not target_node:
|
||||||
|
# 通过 ROS 节点查找非子设备的目标站
|
||||||
|
pass
|
||||||
|
|
||||||
|
for group in transfer_groups:
|
||||||
|
resource = self.find_resource_by_name(group["resource_name"])
|
||||||
|
# 从本站 deck 移除
|
||||||
|
resource.unassign()
|
||||||
|
# 调用目标站的接收方法
|
||||||
|
# ...
|
||||||
|
|
||||||
|
return {"success": True, "transferred": len(transfer_groups)}
|
||||||
|
```
|
||||||
|
|
||||||
|
参考:`BioyondDispensingStation.transfer_materials_to_reaction_station`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. post_init 完整模式
|
||||||
|
|
||||||
|
`post_init` 是工作站初始化的关键阶段,此时 ROS 节点和子设备已就绪。
|
||||||
|
|
||||||
|
```python
|
||||||
|
def post_init(self, ros_node):
|
||||||
|
super().post_init(ros_node)
|
||||||
|
|
||||||
|
# 1. 初始化外部系统客户端(此时 config 已可用)
|
||||||
|
self.rpc_client = MySystemRPC(
|
||||||
|
host=self.config.get("api_host"),
|
||||||
|
api_key=self.config.get("api_key"),
|
||||||
|
)
|
||||||
|
self.hardware_interface = self.rpc_client
|
||||||
|
|
||||||
|
# 2. 启动连接监控
|
||||||
|
self.connection_monitor = ConnectionMonitor(self)
|
||||||
|
self.connection_monitor.start()
|
||||||
|
|
||||||
|
# 3. 启动 HTTP 回调服务
|
||||||
|
if hasattr(self, '_http_service_config'):
|
||||||
|
self.http_service = WorkstationHTTPService(
|
||||||
|
workstation_instance=self,
|
||||||
|
host=self._http_service_config["host"],
|
||||||
|
port=self._http_service_config["port"],
|
||||||
|
)
|
||||||
|
self.http_service.start()
|
||||||
|
|
||||||
|
# 4. 上传 deck 到云端
|
||||||
|
ROS2DeviceNode.run_async_func(
|
||||||
|
self._ros_node.update_resource, True,
|
||||||
|
**{"resources": [self.deck]}
|
||||||
|
)
|
||||||
|
|
||||||
|
# 5. 初始化资源同步器(可选)
|
||||||
|
self.resource_synchronizer = MyResourceSynchronizer(self, self.rpc_client)
|
||||||
|
```
|
||||||
381
.cursor/skills/edit-experiment-graph/SKILL.md
Normal file
381
.cursor/skills/edit-experiment-graph/SKILL.md
Normal file
@@ -0,0 +1,381 @@
|
|||||||
|
---
|
||||||
|
name: edit-experiment-graph
|
||||||
|
description: Guide for creating and editing experiment graph files in Uni-Lab-OS (创建/编辑实验组态图). Covers node types, link types, parent-child relationships, deck configuration, and common graph patterns. Use when the user wants to create a graph file, edit an experiment configuration, set up device topology, or mentions 图文件/graph/组态/拓扑/实验图/experiment JSON.
|
||||||
|
---
|
||||||
|
|
||||||
|
# 创建/编辑实验组态图
|
||||||
|
|
||||||
|
实验图(Graph File)定义设备拓扑、物理连接和物料配置。系统启动时加载图文件,初始化所有设备和连接关系。
|
||||||
|
|
||||||
|
路径:`unilabos/test/experiments/<name>.json`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第一步:确认需求
|
||||||
|
|
||||||
|
向用户确认:
|
||||||
|
|
||||||
|
| 信息 | 说明 |
|
||||||
|
|------|------|
|
||||||
|
| 场景类型 | 单设备调试 / 多设备联调 / 工作站完整图 |
|
||||||
|
| 包含的设备 | 设备 ID、注册表 class 名、配置参数 |
|
||||||
|
| 连接关系 | 物理连接(管道)/ 通信连接(串口)/ 无连接 |
|
||||||
|
| 父子关系 | 是否有工作站包含子设备 |
|
||||||
|
| 物料需求 | 是否需要 Deck、容器、试剂瓶 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第二步:JSON 顶层结构
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"nodes": [],
|
||||||
|
"links": []
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> `links` 也可写作 `edges`,加载时两者等效。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第三步:定义 Nodes
|
||||||
|
|
||||||
|
### 节点字段
|
||||||
|
|
||||||
|
| 字段 | 类型 | 必需 | 默认值 | 说明 |
|
||||||
|
|------|------|------|--------|------|
|
||||||
|
| `id` | string | **是** | — | 节点唯一标识,links 和 children 中引用此值 |
|
||||||
|
| `class` | string | **是** | — | 对应注册表名(设备/资源 YAML 的 key),容器可为 `null` |
|
||||||
|
| `name` | string | 否 | 同 `id` | 显示名称,缺省时自动用 `id` |
|
||||||
|
| `type` | string | 否 | `"device"` | 节点类型(见下表),缺省时自动设为 `"device"` |
|
||||||
|
| `children` | string[] | 否 | `[]` | 子节点 ID 列表 |
|
||||||
|
| `parent` | string\|null | 否 | `null` | 父节点 ID,顶层设备为 `null` |
|
||||||
|
| `position` | object | 否 | `{x:0,y:0,z:0}` | 空间坐标 |
|
||||||
|
| `config` | object | 否 | `{}` | 传给驱动 `__init__` 的参数 |
|
||||||
|
| `data` | object | 否 | `{}` | 初始运行状态 |
|
||||||
|
| `size_x/y/z` | float | 否 | — | 节点物理尺寸(工作站节点常用) |
|
||||||
|
|
||||||
|
> 非标准字段(如 `api_host`)会自动移入 `config`。
|
||||||
|
|
||||||
|
### 节点类型
|
||||||
|
|
||||||
|
| `type` | 用途 | `class` 要求 |
|
||||||
|
|--------|------|-------------|
|
||||||
|
| `device` | 设备(默认) | 注册表中的设备名 |
|
||||||
|
| `deck` | 工作台面 | Deck 工厂函数/类名 |
|
||||||
|
| `container` | 容器(烧瓶、反应釜) | `null` 或具体容器类名 |
|
||||||
|
|
||||||
|
### 设备节点模板
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "my_device",
|
||||||
|
"name": "我的设备",
|
||||||
|
"children": [],
|
||||||
|
"parent": null,
|
||||||
|
"type": "device",
|
||||||
|
"class": "registry_device_name",
|
||||||
|
"position": {"x": 0, "y": 0, "z": 0},
|
||||||
|
"config": {
|
||||||
|
"port": "/dev/ttyUSB0",
|
||||||
|
"baudrate": 115200
|
||||||
|
},
|
||||||
|
"data": {
|
||||||
|
"status": "Idle"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 容器节点模板
|
||||||
|
|
||||||
|
容器用于协议系统中表示试剂瓶、反应釜等,`class` 通常为 `null`:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "flask_DMF",
|
||||||
|
"name": "DMF试剂瓶",
|
||||||
|
"children": [],
|
||||||
|
"parent": "my_station",
|
||||||
|
"type": "container",
|
||||||
|
"class": null,
|
||||||
|
"position": {"x": 200, "y": 500, "z": 0},
|
||||||
|
"config": {"max_volume": 1000.0},
|
||||||
|
"data": {
|
||||||
|
"liquid": [{"liquid_type": "DMF", "liquid_volume": 800.0}]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Deck 节点模板
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "my_deck",
|
||||||
|
"name": "my_deck",
|
||||||
|
"children": [],
|
||||||
|
"parent": "my_station",
|
||||||
|
"type": "deck",
|
||||||
|
"class": "MyStation_Deck",
|
||||||
|
"position": {"x": 0, "y": 0, "z": 0},
|
||||||
|
"config": {
|
||||||
|
"type": "MyStation_Deck",
|
||||||
|
"setup": true,
|
||||||
|
"rotation": {"x": 0, "y": 0, "z": 0, "type": "Rotation"}
|
||||||
|
},
|
||||||
|
"data": {}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第四步:定义 Links
|
||||||
|
|
||||||
|
### Link 字段
|
||||||
|
|
||||||
|
| 字段 | 类型 | 说明 |
|
||||||
|
|------|------|------|
|
||||||
|
| `source` | string | 源节点 ID |
|
||||||
|
| `target` | string | 目标节点 ID |
|
||||||
|
| `type` | string | `"physical"` / `"fluid"` / `"communication"` |
|
||||||
|
| `port` | object | 端口映射 `{source_id: "port_name", target_id: "port_name"}` |
|
||||||
|
|
||||||
|
### 物理/流体连接
|
||||||
|
|
||||||
|
设备间的管道连接,协议系统用此查找路径:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"source": "multiway_valve_1",
|
||||||
|
"target": "flask_DMF",
|
||||||
|
"type": "fluid",
|
||||||
|
"port": {
|
||||||
|
"multiway_valve_1": "2",
|
||||||
|
"flask_DMF": "outlet"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 通信连接
|
||||||
|
|
||||||
|
设备间的串口/IO 通信代理,加载时自动将端口信息写入目标设备 config:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"source": "pump_1",
|
||||||
|
"target": "serial_device",
|
||||||
|
"type": "communication",
|
||||||
|
"port": {
|
||||||
|
"pump_1": "port",
|
||||||
|
"serial_device": "port"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 第五步:父子关系与工作站配置
|
||||||
|
|
||||||
|
### 工作站 + 子设备
|
||||||
|
|
||||||
|
工作站节点的 `children` 列出所有子节点 ID,子节点的 `parent` 指向工作站:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "my_station",
|
||||||
|
"children": ["my_deck", "pump_1", "valve_1", "reactor_1"],
|
||||||
|
"parent": null,
|
||||||
|
"type": "device",
|
||||||
|
"class": "workstation",
|
||||||
|
"config": {
|
||||||
|
"protocol_type": ["PumpTransferProtocol", "CleanProtocol"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 工作站 + Deck 引用
|
||||||
|
|
||||||
|
工作站节点中通过 `deck` 字段引用 Deck:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "my_station",
|
||||||
|
"children": ["my_deck", "sub_device_1"],
|
||||||
|
"deck": {
|
||||||
|
"data": {
|
||||||
|
"_resource_child_name": "my_deck",
|
||||||
|
"_resource_type": "unilabos.resources.my_module.decks:MyDeck"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**关键约束:**
|
||||||
|
- `_resource_child_name` 必须与 Deck 节点的 `id` 一致
|
||||||
|
- `_resource_type` 为 Deck 类/工厂函数的完整 Python 路径
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 常见图模式
|
||||||
|
|
||||||
|
### 模式 A:单设备调试
|
||||||
|
|
||||||
|
最简形式,一个设备节点,无连接:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"nodes": [
|
||||||
|
{
|
||||||
|
"id": "my_device",
|
||||||
|
"name": "my_device",
|
||||||
|
"children": [],
|
||||||
|
"parent": null,
|
||||||
|
"type": "device",
|
||||||
|
"class": "motor.zdt_x42",
|
||||||
|
"position": {"x": 0, "y": 0, "z": 0},
|
||||||
|
"config": {"port": "/dev/ttyUSB0", "baudrate": 115200},
|
||||||
|
"data": {"status": "idle"}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"links": []
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 模式 B:Protocol 工作站(泵+阀+容器)
|
||||||
|
|
||||||
|
工作站配合泵、阀、容器和物理连接,用于协议编译:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"nodes": [
|
||||||
|
{
|
||||||
|
"id": "station", "name": "协议工作站",
|
||||||
|
"class": "workstation", "type": "device", "parent": null,
|
||||||
|
"children": ["pump", "valve", "flask_solvent", "reactor", "waste"],
|
||||||
|
"config": {"protocol_type": ["PumpTransferProtocol"]}
|
||||||
|
},
|
||||||
|
{"id": "pump", "name": "转移泵", "class": "virtual_transfer_pump",
|
||||||
|
"type": "device", "parent": "station",
|
||||||
|
"config": {"port": "VIRTUAL", "max_volume": 25.0},
|
||||||
|
"data": {"status": "Idle", "position": 0.0, "valve_position": "0"}},
|
||||||
|
{"id": "valve", "name": "多通阀", "class": "virtual_multiway_valve",
|
||||||
|
"type": "device", "parent": "station",
|
||||||
|
"config": {"port": "VIRTUAL", "positions": 8}},
|
||||||
|
{"id": "flask_solvent", "name": "溶剂瓶", "type": "container",
|
||||||
|
"class": null, "parent": "station",
|
||||||
|
"config": {"max_volume": 1000.0},
|
||||||
|
"data": {"liquid": [{"liquid_type": "DMF", "liquid_volume": 500}]}},
|
||||||
|
{"id": "reactor", "name": "反应器", "type": "container",
|
||||||
|
"class": null, "parent": "station"},
|
||||||
|
{"id": "waste", "name": "废液瓶", "type": "container",
|
||||||
|
"class": null, "parent": "station"}
|
||||||
|
],
|
||||||
|
"links": [
|
||||||
|
{"source": "pump", "target": "valve", "type": "fluid",
|
||||||
|
"port": {"pump": "transferpump", "valve": "transferpump"}},
|
||||||
|
{"source": "valve", "target": "flask_solvent", "type": "fluid",
|
||||||
|
"port": {"valve": "1", "flask_solvent": "outlet"}},
|
||||||
|
{"source": "valve", "target": "reactor", "type": "fluid",
|
||||||
|
"port": {"valve": "2", "reactor": "inlet"}},
|
||||||
|
{"source": "valve", "target": "waste", "type": "fluid",
|
||||||
|
"port": {"valve": "3", "waste": "inlet"}}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 模式 C:外部系统工作站 + Deck
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"nodes": [
|
||||||
|
{
|
||||||
|
"id": "bioyond_station", "class": "reaction_station.bioyond",
|
||||||
|
"parent": null, "children": ["bioyond_deck"],
|
||||||
|
"config": {
|
||||||
|
"api_host": "http://192.168.1.100:8080",
|
||||||
|
"api_key": "YOUR_KEY",
|
||||||
|
"material_type_mappings": {},
|
||||||
|
"warehouse_mapping": {}
|
||||||
|
},
|
||||||
|
"deck": {
|
||||||
|
"data": {
|
||||||
|
"_resource_child_name": "bioyond_deck",
|
||||||
|
"_resource_type": "unilabos.resources.bioyond.decks:BIOYOND_PolymerReactionStation_Deck"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "bioyond_deck", "class": "BIOYOND_PolymerReactionStation_Deck",
|
||||||
|
"parent": "bioyond_station", "type": "deck",
|
||||||
|
"config": {"type": "BIOYOND_PolymerReactionStation_Deck", "setup": true}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"links": []
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 模式 D:通信代理(串口设备)
|
||||||
|
|
||||||
|
泵通过串口设备通信,使用 `communication` 类型的 link。加载时系统会自动将串口端口信息写入泵的 `config`:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"nodes": [
|
||||||
|
{"id": "station", "name": "工作站", "type": "device",
|
||||||
|
"class": "workstation", "parent": null,
|
||||||
|
"children": ["serial_1", "pump_1"]},
|
||||||
|
{"id": "serial_1", "name": "串口", "type": "device",
|
||||||
|
"class": "serial", "parent": "station",
|
||||||
|
"config": {"port": "COM7", "baudrate": 9600}},
|
||||||
|
{"id": "pump_1", "name": "注射泵", "type": "device",
|
||||||
|
"class": "syringe_pump_with_valve.runze.SY03B-T08", "parent": "station"}
|
||||||
|
],
|
||||||
|
"links": [
|
||||||
|
{"source": "pump_1", "target": "serial_1", "type": "communication",
|
||||||
|
"port": {"pump_1": "port", "serial_1": "port"}}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 验证
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 启动测试
|
||||||
|
unilab -g unilabos/test/experiments/<name>.json --complete_registry
|
||||||
|
|
||||||
|
# 仅检查注册表
|
||||||
|
python -m unilabos --check_mode --skip_env_check
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 高级模式
|
||||||
|
|
||||||
|
处理复杂图文件时,详见 [reference.md](reference.md):ResourceDict 完整字段 schema、Pose 标准化规则、Handle 验证机制、GraphML 格式支持、外部系统工作站完整 config 结构。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 常见错误
|
||||||
|
|
||||||
|
| 错误 | 原因 | 修复 |
|
||||||
|
|------|------|------|
|
||||||
|
| `class` 找不到 | 注册表中无此设备名 | 在 `unilabos/registry/devices/` 或 `resources/` 中搜索正确名称 |
|
||||||
|
| children/parent 不一致 | 子节点 `parent` 与父节点 `children` 不匹配 | 确保双向一致 |
|
||||||
|
| `_resource_child_name` 不匹配 | Deck 引用名与 Deck 节点 `id` 不同 | 保持一致 |
|
||||||
|
| Link 端口错误 | `port` 中的 key 不是 source/target 的 `id` | key 必须是对应节点的 `id` |
|
||||||
|
| 重复 UUID | 多个节点有相同 `uuid` | 删除或修改 UUID |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 参考路径
|
||||||
|
|
||||||
|
| 内容 | 路径 |
|
||||||
|
|------|------|
|
||||||
|
| 图文件目录 | `unilabos/test/experiments/` |
|
||||||
|
| 协议测试站 | `unilabos/test/experiments/Protocol_Test_Station/` |
|
||||||
|
| 图加载代码 | `unilabos/resources/graphio.py` |
|
||||||
|
| 节点模型 | `unilabos/resources/resource_tracker.py` |
|
||||||
|
| 设备注册表 | `unilabos/registry/devices/` |
|
||||||
|
| 资源注册表 | `unilabos/registry/resources/` |
|
||||||
|
| 用户文档 | `docs/user_guide/graph_files.md` |
|
||||||
255
.cursor/skills/edit-experiment-graph/reference.md
Normal file
255
.cursor/skills/edit-experiment-graph/reference.md
Normal file
@@ -0,0 +1,255 @@
|
|||||||
|
# 实验图高级参考
|
||||||
|
|
||||||
|
本文件是 SKILL.md 的补充,包含 ResourceDict 完整 schema、Handle 验证、GraphML 格式、Pose 标准化规则和复杂图文件结构。Agent 在需要处理这些场景时按需阅读。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. ResourceDict 完整字段
|
||||||
|
|
||||||
|
`unilabos/resources/resource_tracker.py` 中定义的节点数据模型:
|
||||||
|
|
||||||
|
| 字段 | 类型 | 别名 | 说明 |
|
||||||
|
|------|------|------|------|
|
||||||
|
| `id` | `str` | — | 节点唯一标识 |
|
||||||
|
| `uuid` | `str` | — | 全局唯一标识 |
|
||||||
|
| `name` | `str` | — | 显示名称 |
|
||||||
|
| `description` | `str` | — | 描述(默认 `""` ) |
|
||||||
|
| `resource_schema` | `Dict[str, Any]` | `schema` | 资源 schema |
|
||||||
|
| `model` | `Dict[str, Any]` | — | 3D 模型信息 |
|
||||||
|
| `icon` | `str` | — | 图标(默认 `""` ) |
|
||||||
|
| `parent_uuid` | `Optional[str]` | — | 父节点 UUID |
|
||||||
|
| `parent` | `Optional[ResourceDict]` | — | 父节点引用(序列化时 exclude) |
|
||||||
|
| `type` | `Union[Literal["device"], str]` | — | 节点类型 |
|
||||||
|
| `klass` | `str` | `class` | 注册表类名 |
|
||||||
|
| `pose` | `ResourceDictPosition` | — | 位姿信息 |
|
||||||
|
| `config` | `Dict[str, Any]` | — | 配置参数 |
|
||||||
|
| `data` | `Dict[str, Any]` | — | 运行时数据 |
|
||||||
|
| `extra` | `Dict[str, Any]` | — | 扩展数据 |
|
||||||
|
|
||||||
|
### Pose 完整结构(ResourceDictPosition)
|
||||||
|
|
||||||
|
| 字段 | 类型 | 默认值 | 说明 |
|
||||||
|
|------|------|--------|------|
|
||||||
|
| `size` | `{width, height, depth}` | `{0,0,0}` | 节点尺寸 |
|
||||||
|
| `scale` | `{x, y, z}` | `{1,1,1}` | 缩放比例 |
|
||||||
|
| `layout` | `"2d"/"x-y"/"z-y"/"x-z"` | `"x-y"` | 布局方向 |
|
||||||
|
| `position` | `{x, y, z}` | `{0,0,0}` | 2D 位置 |
|
||||||
|
| `position3d` | `{x, y, z}` | `{0,0,0}` | 3D 位置 |
|
||||||
|
| `rotation` | `{x, y, z}` | `{0,0,0}` | 旋转角度 |
|
||||||
|
| `cross_section_type` | `"rectangle"/"circle"/"rounded_rectangle"` | `"rectangle"` | 横截面形状 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. Position / Pose 标准化规则
|
||||||
|
|
||||||
|
图文件中的 `position` 有多种写法,加载时自动标准化。
|
||||||
|
|
||||||
|
### 输入格式兼容
|
||||||
|
|
||||||
|
```json
|
||||||
|
// 格式 A: 直接 {x, y, z}(最常用)
|
||||||
|
"position": {"x": 100, "y": 200, "z": 0}
|
||||||
|
|
||||||
|
// 格式 B: 嵌套 position
|
||||||
|
"position": {"position": {"x": 100, "y": 200, "z": 0}}
|
||||||
|
|
||||||
|
// 格式 C: 使用 pose 字段
|
||||||
|
"pose": {"position": {"x": 100, "y": 200, "z": 0}}
|
||||||
|
|
||||||
|
// 格式 D: 顶层 x, y, z(无 position 字段)
|
||||||
|
"x": 100, "y": 200, "z": 0
|
||||||
|
```
|
||||||
|
|
||||||
|
### 标准化流程
|
||||||
|
|
||||||
|
1. **graphio.py `canonicalize_nodes_data`**:若 `position` 不是 dict,从节点顶层提取 `x/y/z` 填入 `pose.position`
|
||||||
|
2. **resource_tracker.py `get_resource_instance_from_dict`**:若 `position.x` 存在(旧格式),转为 `{"position": {"x":..., "y":..., "z":...}}`
|
||||||
|
3. `pose.size` 从 `config.size_x/size_y/size_z` 自动填充
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. Handle 验证
|
||||||
|
|
||||||
|
启动时系统验证 link 中的 `sourceHandle` / `targetHandle` 是否在注册表的 `handles` 中定义。
|
||||||
|
|
||||||
|
```python
|
||||||
|
# unilabos/app/main.py (约 449-481 行)
|
||||||
|
source_handler_keys = [
|
||||||
|
h["handler_key"] for h in materials[source_node.klass]["handles"]
|
||||||
|
if h["io_type"] == "source"
|
||||||
|
]
|
||||||
|
target_handler_keys = [
|
||||||
|
h["handler_key"] for h in materials[target_node.klass]["handles"]
|
||||||
|
if h["io_type"] == "target"
|
||||||
|
]
|
||||||
|
if source_handle not in source_handler_keys:
|
||||||
|
print_status(f"节点 {source_node.id} 的source端点 {source_handle} 不存在", "error")
|
||||||
|
resource_edge_info.pop(...) # 移除非法 link
|
||||||
|
```
|
||||||
|
|
||||||
|
**Handle 定义在注册表 YAML 中:**
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
my_device:
|
||||||
|
handles:
|
||||||
|
- handler_key: access
|
||||||
|
io_type: target
|
||||||
|
data_type: fluid
|
||||||
|
side: NORTH
|
||||||
|
label: access
|
||||||
|
```
|
||||||
|
|
||||||
|
> 大多数简单设备不定义 handles,此验证仅对有 `sourceHandle`/`targetHandle` 的 link 生效。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. GraphML 格式支持
|
||||||
|
|
||||||
|
除 JSON 外,系统也支持 GraphML 格式(`unilabos/resources/graphio.py::read_graphml`)。
|
||||||
|
|
||||||
|
### 与 JSON 的关键差异
|
||||||
|
|
||||||
|
| 特性 | JSON | GraphML |
|
||||||
|
|------|------|---------|
|
||||||
|
| 父子关系 | `parent`/`children` 字段 | `::` 分隔的节点 ID(如 `station::pump_1`) |
|
||||||
|
| 加载后 | 直接解析 | 先 `nx.read_graphml` 再转 JSON 格式 |
|
||||||
|
| 输出 | 不生成副本 | 自动生成等价的 `.json` 文件 |
|
||||||
|
|
||||||
|
### GraphML 转换流程
|
||||||
|
|
||||||
|
```
|
||||||
|
nx.read_graphml(file)
|
||||||
|
↓ 用 label 重映射节点名
|
||||||
|
↓ 从 "::" 推断 parent_relation
|
||||||
|
nx.relabel_nodes + nx.node_link_data
|
||||||
|
↓ canonicalize_nodes_data + canonicalize_links_ports
|
||||||
|
↓ 写出等价 JSON 文件
|
||||||
|
physical_setup_graph + handle_communications
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 复杂图文件结构示例
|
||||||
|
|
||||||
|
### 外部系统工作站完整 config
|
||||||
|
|
||||||
|
以 `reaction_station_bioyond.json` 为例,工作站 `config` 中的关键字段:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"config": {
|
||||||
|
"api_key": "DE9BDDA0",
|
||||||
|
"api_host": "http://172.21.103.36:45388",
|
||||||
|
|
||||||
|
"workflow_mappings": {
|
||||||
|
"scheduler_start": {"workflow": "start", "params": {}},
|
||||||
|
"create_order": {"workflow": "create_order", "params": {}}
|
||||||
|
},
|
||||||
|
|
||||||
|
"material_type_mappings": {
|
||||||
|
"BIOYOND_PolymerStation_Reactor": ["反应器", "type-uuid-here"],
|
||||||
|
"BIOYOND_PolymerStation_1BottleCarrier": ["试剂瓶", "type-uuid-here"]
|
||||||
|
},
|
||||||
|
|
||||||
|
"warehouse_mapping": {
|
||||||
|
"堆栈1左": {
|
||||||
|
"uuid": "warehouse-uuid-here",
|
||||||
|
"site_uuids": {
|
||||||
|
"A01": "site-uuid-1",
|
||||||
|
"A02": "site-uuid-2"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
"http_service_config": {
|
||||||
|
"enabled": true,
|
||||||
|
"host": "0.0.0.0",
|
||||||
|
"port": 45399,
|
||||||
|
"routes": ["/callback/workflow", "/callback/material"]
|
||||||
|
},
|
||||||
|
|
||||||
|
"deck": {
|
||||||
|
"data": {
|
||||||
|
"_resource_child_name": "Bioyond_Deck",
|
||||||
|
"_resource_type": "unilabos.resources.bioyond.decks:BIOYOND_PolymerReactionStation_Deck"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
"size_x": 2700.0,
|
||||||
|
"size_y": 1080.0,
|
||||||
|
"size_z": 2500.0,
|
||||||
|
"protocol_type": [],
|
||||||
|
"data": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 子设备 Reactor 节点
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "reactor_1",
|
||||||
|
"name": "reactor_1",
|
||||||
|
"parent": "reaction_station_bioyond",
|
||||||
|
"type": "device",
|
||||||
|
"class": "bioyond_reactor",
|
||||||
|
"position": {"x": 1150, "y": 300, "z": 0},
|
||||||
|
"config": {
|
||||||
|
"reactor_index": 0,
|
||||||
|
"bioyond_workflow_key": "reactor_1"
|
||||||
|
},
|
||||||
|
"data": {}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Deck 节点
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "Bioyond_Deck",
|
||||||
|
"name": "Bioyond_Deck",
|
||||||
|
"parent": "reaction_station_bioyond",
|
||||||
|
"type": "deck",
|
||||||
|
"class": "BIOYOND_PolymerReactionStation_Deck",
|
||||||
|
"position": {"x": 0, "y": 0, "z": 0},
|
||||||
|
"config": {
|
||||||
|
"type": "BIOYOND_PolymerReactionStation_Deck",
|
||||||
|
"setup": true,
|
||||||
|
"rotation": {"x": 0, "y": 0, "z": 0, "type": "Rotation"}
|
||||||
|
},
|
||||||
|
"data": {}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. Link 端口标准化
|
||||||
|
|
||||||
|
`graphio.py::canonicalize_links_ports` 处理 `port` 字段的多种格式:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 输入: 字符串格式 "(A,B)"
|
||||||
|
"port": "(pump_1, valve_1)"
|
||||||
|
# 输出: 字典格式
|
||||||
|
"port": {"source_id": "pump_1", "target_id": "valve_1"}
|
||||||
|
|
||||||
|
# 输入: 已是字典
|
||||||
|
"port": {"pump_1": "port", "serial_1": "port"}
|
||||||
|
# 保持不变
|
||||||
|
|
||||||
|
# 输入: 无 port 字段
|
||||||
|
# 自动补充空 port
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. 关键路径
|
||||||
|
|
||||||
|
| 内容 | 路径 |
|
||||||
|
|------|------|
|
||||||
|
| ResourceDict 模型 | `unilabos/resources/resource_tracker.py` |
|
||||||
|
| 图加载 + 标准化 | `unilabos/resources/graphio.py` |
|
||||||
|
| Handle 验证 | `unilabos/app/main.py` (449-481 行) |
|
||||||
|
| 反应站图文件 | `unilabos/test/experiments/reaction_station_bioyond.json` |
|
||||||
|
| 配液站图文件 | `unilabos/test/experiments/dispensing_station_bioyond.json` |
|
||||||
|
| 用户文档 | `docs/user_guide/graph_files.md` |
|
||||||
188
.cursorignore
Normal file
188
.cursorignore
Normal file
@@ -0,0 +1,188 @@
|
|||||||
|
# ============================================================
|
||||||
|
# Uni-Lab-OS Cursor Ignore 配置,控制 Cursor AI 的文件索引范围
|
||||||
|
# ============================================================
|
||||||
|
|
||||||
|
# ==================== 敏感配置文件 ====================
|
||||||
|
# 本地配置(可能包含密钥)
|
||||||
|
**/local_config.py
|
||||||
|
test_config.py
|
||||||
|
local_test*.py
|
||||||
|
|
||||||
|
# 环境变量和密钥
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
**/.certs/
|
||||||
|
*.pem
|
||||||
|
*.key
|
||||||
|
credentials.json
|
||||||
|
secrets.yaml
|
||||||
|
|
||||||
|
# ==================== 二进制和 3D 模型文件 ====================
|
||||||
|
# 3D 模型文件(无需索引)
|
||||||
|
*.stl
|
||||||
|
*.dae
|
||||||
|
*.glb
|
||||||
|
*.gltf
|
||||||
|
*.obj
|
||||||
|
*.fbx
|
||||||
|
*.blend
|
||||||
|
|
||||||
|
# URDF/Xacro 机器人描述文件(大型XML)
|
||||||
|
*.xacro
|
||||||
|
|
||||||
|
# 图片文件
|
||||||
|
*.png
|
||||||
|
*.jpg
|
||||||
|
*.jpeg
|
||||||
|
*.gif
|
||||||
|
*.webp
|
||||||
|
*.ico
|
||||||
|
*.svg
|
||||||
|
*.bmp
|
||||||
|
|
||||||
|
# 压缩包
|
||||||
|
*.zip
|
||||||
|
*.tar
|
||||||
|
*.tar.gz
|
||||||
|
*.tgz
|
||||||
|
*.bz2
|
||||||
|
*.rar
|
||||||
|
*.7z
|
||||||
|
|
||||||
|
# ==================== Python 生成文件 ====================
|
||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
|
*$py.class
|
||||||
|
*.so
|
||||||
|
*.pyd
|
||||||
|
*.egg
|
||||||
|
*.egg-info/
|
||||||
|
.eggs/
|
||||||
|
dist/
|
||||||
|
build/
|
||||||
|
*.manifest
|
||||||
|
*.spec
|
||||||
|
|
||||||
|
# ==================== IDE 和编辑器 ====================
|
||||||
|
.idea/
|
||||||
|
.vscode/
|
||||||
|
*.swp
|
||||||
|
*.swo
|
||||||
|
*~
|
||||||
|
.#*
|
||||||
|
|
||||||
|
# ==================== 测试和覆盖率 ====================
|
||||||
|
.pytest_cache/
|
||||||
|
.coverage
|
||||||
|
.coverage.*
|
||||||
|
htmlcov/
|
||||||
|
.tox/
|
||||||
|
.nox/
|
||||||
|
coverage.xml
|
||||||
|
*.cover
|
||||||
|
|
||||||
|
# ==================== 虚拟环境 ====================
|
||||||
|
.venv/
|
||||||
|
venv/
|
||||||
|
env/
|
||||||
|
ENV/
|
||||||
|
|
||||||
|
# ==================== ROS 2 生成文件 ====================
|
||||||
|
# ROS 构建目录
|
||||||
|
build/
|
||||||
|
install/
|
||||||
|
log/
|
||||||
|
logs/
|
||||||
|
devel/
|
||||||
|
|
||||||
|
# ROS 消息生成
|
||||||
|
msg_gen/
|
||||||
|
srv_gen/
|
||||||
|
msg/*Action.msg
|
||||||
|
msg/*ActionFeedback.msg
|
||||||
|
msg/*ActionGoal.msg
|
||||||
|
msg/*ActionResult.msg
|
||||||
|
msg/*Feedback.msg
|
||||||
|
msg/*Goal.msg
|
||||||
|
msg/*Result.msg
|
||||||
|
msg/_*.py
|
||||||
|
srv/_*.py
|
||||||
|
build_isolated/
|
||||||
|
devel_isolated/
|
||||||
|
|
||||||
|
# ROS 动态配置
|
||||||
|
*.cfgc
|
||||||
|
/cfg/cpp/
|
||||||
|
/cfg/*.py
|
||||||
|
|
||||||
|
# ==================== 项目特定目录 ====================
|
||||||
|
# 工作数据目录
|
||||||
|
unilabos_data/
|
||||||
|
|
||||||
|
# 临时和输出目录
|
||||||
|
temp/
|
||||||
|
output/
|
||||||
|
cursor_docs/
|
||||||
|
configs/
|
||||||
|
|
||||||
|
# 文档构建
|
||||||
|
docs/_build/
|
||||||
|
/site
|
||||||
|
|
||||||
|
# ==================== 大型数据文件 ====================
|
||||||
|
# 点云数据
|
||||||
|
*.pcd
|
||||||
|
|
||||||
|
# GraphML 图形文件
|
||||||
|
*.graphml
|
||||||
|
|
||||||
|
# 日志文件
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# 数据库
|
||||||
|
*.sqlite3
|
||||||
|
*.db
|
||||||
|
|
||||||
|
# Jupyter 检查点
|
||||||
|
.ipynb_checkpoints/
|
||||||
|
|
||||||
|
# ==================== 设备网格资源 ====================
|
||||||
|
# 3D 网格文件目录(包含大量 STL/DAE 文件)
|
||||||
|
unilabos/device_mesh/devices/**/*.stl
|
||||||
|
unilabos/device_mesh/devices/**/*.dae
|
||||||
|
unilabos/device_mesh/resources/**/*.stl
|
||||||
|
unilabos/device_mesh/resources/**/*.glb
|
||||||
|
unilabos/device_mesh/resources/**/*.xacro
|
||||||
|
|
||||||
|
# RViz 配置
|
||||||
|
*.rviz
|
||||||
|
|
||||||
|
# ==================== 系统文件 ====================
|
||||||
|
.DS_Store
|
||||||
|
Thumbs.db
|
||||||
|
desktop.ini
|
||||||
|
|
||||||
|
# ==================== 锁文件 ====================
|
||||||
|
poetry.lock
|
||||||
|
Pipfile.lock
|
||||||
|
pdm.lock
|
||||||
|
package-lock.json
|
||||||
|
yarn.lock
|
||||||
|
|
||||||
|
# ==================== 类型检查缓存 ====================
|
||||||
|
.mypy_cache/
|
||||||
|
.dmypy.json
|
||||||
|
.pytype/
|
||||||
|
.pyre/
|
||||||
|
pyrightconfig.json
|
||||||
|
|
||||||
|
# ==================== 其他 ====================
|
||||||
|
# Catkin
|
||||||
|
CATKIN_IGNORE
|
||||||
|
|
||||||
|
# Eclipse/Qt
|
||||||
|
.project
|
||||||
|
.cproject
|
||||||
|
CMakeLists.txt.user
|
||||||
|
*.user
|
||||||
|
qtcreator-*
|
||||||
11
.github/copilot-instructions.md
vendored
Normal file
11
.github/copilot-instructions.md
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
## 设备接入
|
||||||
|
|
||||||
|
当被要求添加设备驱动时,参考 `docs/ai_guides/add_device.md`。
|
||||||
|
该指南包含完整的模板和已有设备接口参考。
|
||||||
|
|
||||||
|
## 关键规则
|
||||||
|
|
||||||
|
- 动作方法的参数名是接口契约,不可重命名
|
||||||
|
- `status` 字符串必须与同类已有设备一致
|
||||||
|
- `self.data` 必须在 `__init__` 中预填充所有属性字段
|
||||||
|
- 异步方法中使用 `await self._ros_node.sleep()`,禁止 `time.sleep()`
|
||||||
77
.github/workflows/ci-check.yml
vendored
77
.github/workflows/ci-check.yml
vendored
@@ -8,14 +8,19 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
registry-check:
|
registry-check:
|
||||||
runs-on: ubuntu-latest
|
runs-on: windows-latest
|
||||||
|
|
||||||
|
env:
|
||||||
|
# Fix Unicode encoding issue on Windows runner (cp1252 -> utf-8)
|
||||||
|
PYTHONIOENCODING: utf-8
|
||||||
|
PYTHONUTF8: 1
|
||||||
|
|
||||||
defaults:
|
defaults:
|
||||||
run:
|
run:
|
||||||
shell: bash -l {0}
|
shell: cmd
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v6
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
@@ -27,73 +32,31 @@ jobs:
|
|||||||
channels: robostack-staging,conda-forge,uni-lab
|
channels: robostack-staging,conda-forge,uni-lab
|
||||||
channel-priority: flexible
|
channel-priority: flexible
|
||||||
activate-environment: check-env
|
activate-environment: check-env
|
||||||
auto-activate-base: false
|
|
||||||
auto-update-conda: false
|
auto-update-conda: false
|
||||||
show-channel-urls: true
|
show-channel-urls: true
|
||||||
|
|
||||||
- name: Install ROS dependencies and unilabos-msgs
|
- name: Install ROS dependencies, uv and unilabos-msgs
|
||||||
run: |
|
run: |
|
||||||
# Install all packages together for proper dependency resolution
|
echo Installing ROS dependencies...
|
||||||
# Use mamba for faster and more reliable solving
|
mamba install -n check-env conda-forge::uv conda-forge::opencv robostack-staging::ros-humble-ros-core robostack-staging::ros-humble-action-msgs robostack-staging::ros-humble-std-msgs robostack-staging::ros-humble-geometry-msgs robostack-staging::ros-humble-control-msgs robostack-staging::ros-humble-nav2-msgs uni-lab::ros-humble-unilabos-msgs robostack-staging::ros-humble-cv-bridge robostack-staging::ros-humble-vision-opencv robostack-staging::ros-humble-tf-transformations robostack-staging::ros-humble-moveit-msgs robostack-staging::ros-humble-tf2-ros robostack-staging::ros-humble-tf2-ros-py conda-forge::transforms3d -c robostack-staging -c conda-forge -c uni-lab -y
|
||||||
mamba install -n check-env \
|
|
||||||
python=3.11.14 \
|
|
||||||
robostack-staging::ros-humble-ros-core \
|
|
||||||
robostack-staging::ros-humble-action-msgs \
|
|
||||||
robostack-staging::ros-humble-std-msgs \
|
|
||||||
robostack-staging::ros-humble-geometry-msgs \
|
|
||||||
robostack-staging::ros-humble-control-msgs \
|
|
||||||
robostack-staging::ros-humble-nav2-msgs \
|
|
||||||
uni-lab::ros-humble-unilabos-msgs \
|
|
||||||
robostack-staging::ros-humble-cv-bridge \
|
|
||||||
robostack-staging::ros-humble-vision-opencv \
|
|
||||||
robostack-staging::ros-humble-tf-transformations \
|
|
||||||
robostack-staging::ros-humble-moveit-msgs \
|
|
||||||
robostack-staging::ros-humble-tf2-ros \
|
|
||||||
robostack-staging::ros-humble-tf2-ros-py \
|
|
||||||
conda-forge::transforms3d \
|
|
||||||
-c robostack-staging -c conda-forge -c uni-lab -y
|
|
||||||
|
|
||||||
- name: Install pip dependencies and unilabos
|
- name: Install pip dependencies and unilabos
|
||||||
run: |
|
run: |
|
||||||
# Activate the environment
|
call conda activate check-env
|
||||||
conda activate check-env
|
echo Installing pip dependencies...
|
||||||
|
uv pip install -r unilabos/utils/requirements.txt
|
||||||
# Core dependencies for devices
|
uv pip install pywinauto git+https://github.com/Xuwznln/pylabrobot.git
|
||||||
pip install uv
|
uv pip uninstall enum34 || echo enum34 not installed, skipping
|
||||||
uv pip install networkx \
|
uv pip install .
|
||||||
typing_extensions \
|
|
||||||
websockets \
|
|
||||||
msgcenterpy \
|
|
||||||
opentrons_shared_data \
|
|
||||||
pint \
|
|
||||||
fastapi \
|
|
||||||
jinja2 \
|
|
||||||
requests \
|
|
||||||
uvicorn \
|
|
||||||
git+https://github.com/Xuwznln/pylabrobot.git \
|
|
||||||
opencv-python \
|
|
||||||
pyautogui \
|
|
||||||
opcua \
|
|
||||||
pyserial \
|
|
||||||
pandas \
|
|
||||||
crcmod-plus \
|
|
||||||
pymodbus \
|
|
||||||
pywinauto_recorder \
|
|
||||||
matplotlib \
|
|
||||||
|
|
||||||
|
|
||||||
# PyLabRobot (custom fork)
|
|
||||||
pip install
|
|
||||||
|
|
||||||
# Install unilabos in editable mode
|
|
||||||
pip install -e .
|
|
||||||
|
|
||||||
- name: Run check mode (complete_registry)
|
- name: Run check mode (complete_registry)
|
||||||
run: |
|
run: |
|
||||||
conda activate check-env
|
call conda activate check-env
|
||||||
|
echo Running check mode...
|
||||||
python -m unilabos --check_mode --skip_env_check
|
python -m unilabos --check_mode --skip_env_check
|
||||||
|
|
||||||
- name: Check for uncommitted changes
|
- name: Check for uncommitted changes
|
||||||
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
if ! git diff --exit-code; then
|
if ! git diff --exit-code; then
|
||||||
echo "::error::检测到文件变化!请先在本地运行 'python -m unilabos --complete_registry' 并提交变更"
|
echo "::error::检测到文件变化!请先在本地运行 'python -m unilabos --complete_registry' 并提交变更"
|
||||||
|
|||||||
43
.github/workflows/conda-pack-build.yml
vendored
43
.github/workflows/conda-pack-build.yml
vendored
@@ -13,6 +13,11 @@ on:
|
|||||||
required: false
|
required: false
|
||||||
default: 'win-64'
|
default: 'win-64'
|
||||||
type: string
|
type: string
|
||||||
|
build_full:
|
||||||
|
description: '是否构建完整版 unilabos-full (默认构建轻量版 unilabos)'
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
type: boolean
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-conda-pack:
|
build-conda-pack:
|
||||||
@@ -57,7 +62,7 @@ jobs:
|
|||||||
echo "should_build=false" >> $GITHUB_OUTPUT
|
echo "should_build=false" >> $GITHUB_OUTPUT
|
||||||
fi
|
fi
|
||||||
|
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v6
|
||||||
if: steps.should_build.outputs.should_build == 'true'
|
if: steps.should_build.outputs.should_build == 'true'
|
||||||
with:
|
with:
|
||||||
ref: ${{ github.event.inputs.branch }}
|
ref: ${{ github.event.inputs.branch }}
|
||||||
@@ -69,7 +74,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
miniforge-version: latest
|
miniforge-version: latest
|
||||||
use-mamba: true
|
use-mamba: true
|
||||||
python-version: '3.11.11'
|
python-version: '3.11.14'
|
||||||
channels: conda-forge,robostack-staging,uni-lab,defaults
|
channels: conda-forge,robostack-staging,uni-lab,defaults
|
||||||
channel-priority: flexible
|
channel-priority: flexible
|
||||||
activate-environment: unilab
|
activate-environment: unilab
|
||||||
@@ -81,7 +86,14 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
echo Installing unilabos and dependencies to unilab environment...
|
echo Installing unilabos and dependencies to unilab environment...
|
||||||
echo Using mamba for faster and more reliable dependency resolution...
|
echo Using mamba for faster and more reliable dependency resolution...
|
||||||
mamba install -n unilab uni-lab::unilabos conda-pack -c uni-lab -c robostack-staging -c conda-forge -y
|
echo Build full: ${{ github.event.inputs.build_full }}
|
||||||
|
if "${{ github.event.inputs.build_full }}"=="true" (
|
||||||
|
echo Installing unilabos-full ^(complete package^)...
|
||||||
|
mamba install -n unilab uni-lab::unilabos-full conda-pack -c uni-lab -c robostack-staging -c conda-forge -y
|
||||||
|
) else (
|
||||||
|
echo Installing unilabos ^(minimal package^)...
|
||||||
|
mamba install -n unilab uni-lab::unilabos conda-pack -c uni-lab -c robostack-staging -c conda-forge -y
|
||||||
|
)
|
||||||
|
|
||||||
- name: Install conda-pack, unilabos and dependencies (Unix)
|
- name: Install conda-pack, unilabos and dependencies (Unix)
|
||||||
if: steps.should_build.outputs.should_build == 'true' && matrix.platform != 'win-64'
|
if: steps.should_build.outputs.should_build == 'true' && matrix.platform != 'win-64'
|
||||||
@@ -89,7 +101,14 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
echo "Installing unilabos and dependencies to unilab environment..."
|
echo "Installing unilabos and dependencies to unilab environment..."
|
||||||
echo "Using mamba for faster and more reliable dependency resolution..."
|
echo "Using mamba for faster and more reliable dependency resolution..."
|
||||||
mamba install -n unilab uni-lab::unilabos conda-pack -c uni-lab -c robostack-staging -c conda-forge -y
|
echo "Build full: ${{ github.event.inputs.build_full }}"
|
||||||
|
if [[ "${{ github.event.inputs.build_full }}" == "true" ]]; then
|
||||||
|
echo "Installing unilabos-full (complete package)..."
|
||||||
|
mamba install -n unilab uni-lab::unilabos-full conda-pack -c uni-lab -c robostack-staging -c conda-forge -y
|
||||||
|
else
|
||||||
|
echo "Installing unilabos (minimal package)..."
|
||||||
|
mamba install -n unilab uni-lab::unilabos conda-pack -c uni-lab -c robostack-staging -c conda-forge -y
|
||||||
|
fi
|
||||||
|
|
||||||
- name: Get latest ros-humble-unilabos-msgs version (Windows)
|
- name: Get latest ros-humble-unilabos-msgs version (Windows)
|
||||||
if: steps.should_build.outputs.should_build == 'true' && matrix.platform == 'win-64'
|
if: steps.should_build.outputs.should_build == 'true' && matrix.platform == 'win-64'
|
||||||
@@ -293,7 +312,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Upload distribution package
|
- name: Upload distribution package
|
||||||
if: steps.should_build.outputs.should_build == 'true'
|
if: steps.should_build.outputs.should_build == 'true'
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v6
|
||||||
with:
|
with:
|
||||||
name: unilab-pack-${{ matrix.platform }}-${{ github.event.inputs.branch }}
|
name: unilab-pack-${{ matrix.platform }}-${{ github.event.inputs.branch }}
|
||||||
path: dist-package/
|
path: dist-package/
|
||||||
@@ -308,7 +327,12 @@ jobs:
|
|||||||
echo ==========================================
|
echo ==========================================
|
||||||
echo Platform: ${{ matrix.platform }}
|
echo Platform: ${{ matrix.platform }}
|
||||||
echo Branch: ${{ github.event.inputs.branch }}
|
echo Branch: ${{ github.event.inputs.branch }}
|
||||||
echo Python version: 3.11.11
|
echo Python version: 3.11.14
|
||||||
|
if "${{ github.event.inputs.build_full }}"=="true" (
|
||||||
|
echo Package: unilabos-full ^(complete^)
|
||||||
|
) else (
|
||||||
|
echo Package: unilabos ^(minimal^)
|
||||||
|
)
|
||||||
echo.
|
echo.
|
||||||
echo Distribution package contents:
|
echo Distribution package contents:
|
||||||
dir dist-package
|
dir dist-package
|
||||||
@@ -328,7 +352,12 @@ jobs:
|
|||||||
echo "=========================================="
|
echo "=========================================="
|
||||||
echo "Platform: ${{ matrix.platform }}"
|
echo "Platform: ${{ matrix.platform }}"
|
||||||
echo "Branch: ${{ github.event.inputs.branch }}"
|
echo "Branch: ${{ github.event.inputs.branch }}"
|
||||||
echo "Python version: 3.11.11"
|
echo "Python version: 3.11.14"
|
||||||
|
if [[ "${{ github.event.inputs.build_full }}" == "true" ]]; then
|
||||||
|
echo "Package: unilabos-full (complete)"
|
||||||
|
else
|
||||||
|
echo "Package: unilabos (minimal)"
|
||||||
|
fi
|
||||||
echo ""
|
echo ""
|
||||||
echo "Distribution package contents:"
|
echo "Distribution package contents:"
|
||||||
ls -lh dist-package/
|
ls -lh dist-package/
|
||||||
|
|||||||
37
.github/workflows/deploy-docs.yml
vendored
37
.github/workflows/deploy-docs.yml
vendored
@@ -1,10 +1,12 @@
|
|||||||
name: Deploy Docs
|
name: Deploy Docs
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
# 在 CI Check 成功后自动触发(仅 main 分支)
|
||||||
branches: [main]
|
workflow_run:
|
||||||
pull_request:
|
workflows: ["CI Check"]
|
||||||
|
types: [completed]
|
||||||
branches: [main]
|
branches: [main]
|
||||||
|
# 手动触发
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
inputs:
|
inputs:
|
||||||
branch:
|
branch:
|
||||||
@@ -33,12 +35,19 @@ concurrency:
|
|||||||
jobs:
|
jobs:
|
||||||
# Build documentation
|
# Build documentation
|
||||||
build:
|
build:
|
||||||
|
# 只在以下情况运行:
|
||||||
|
# 1. workflow_run 触发且 CI Check 成功
|
||||||
|
# 2. 手动触发
|
||||||
|
if: |
|
||||||
|
github.event_name == 'workflow_dispatch' ||
|
||||||
|
(github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success')
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v6
|
||||||
with:
|
with:
|
||||||
ref: ${{ github.event.inputs.branch || github.ref }}
|
# workflow_run 时使用触发工作流的分支,手动触发时使用输入的分支
|
||||||
|
ref: ${{ github.event.workflow_run.head_branch || github.event.inputs.branch || github.ref }}
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: Setup Miniforge (with mamba)
|
- name: Setup Miniforge (with mamba)
|
||||||
@@ -46,7 +55,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
miniforge-version: latest
|
miniforge-version: latest
|
||||||
use-mamba: true
|
use-mamba: true
|
||||||
python-version: '3.11.11'
|
python-version: '3.11.14'
|
||||||
channels: conda-forge,robostack-staging,uni-lab,defaults
|
channels: conda-forge,robostack-staging,uni-lab,defaults
|
||||||
channel-priority: flexible
|
channel-priority: flexible
|
||||||
activate-environment: unilab
|
activate-environment: unilab
|
||||||
@@ -75,8 +84,10 @@ jobs:
|
|||||||
|
|
||||||
- name: Setup Pages
|
- name: Setup Pages
|
||||||
id: pages
|
id: pages
|
||||||
uses: actions/configure-pages@v4
|
uses: actions/configure-pages@v5
|
||||||
if: github.ref == 'refs/heads/main' || (github.event_name == 'workflow_dispatch' && github.event.inputs.deploy_to_pages == 'true')
|
if: |
|
||||||
|
github.event.workflow_run.head_branch == 'main' ||
|
||||||
|
(github.event_name == 'workflow_dispatch' && github.event.inputs.deploy_to_pages == 'true')
|
||||||
|
|
||||||
- name: Build Sphinx documentation
|
- name: Build Sphinx documentation
|
||||||
run: |
|
run: |
|
||||||
@@ -94,14 +105,18 @@ jobs:
|
|||||||
test -f docs/_build/html/index.html && echo "✓ index.html exists" || echo "✗ index.html missing"
|
test -f docs/_build/html/index.html && echo "✓ index.html exists" || echo "✗ index.html missing"
|
||||||
|
|
||||||
- name: Upload build artifacts
|
- name: Upload build artifacts
|
||||||
uses: actions/upload-pages-artifact@v3
|
uses: actions/upload-pages-artifact@v4
|
||||||
if: github.ref == 'refs/heads/main' || (github.event_name == 'workflow_dispatch' && github.event.inputs.deploy_to_pages == 'true')
|
if: |
|
||||||
|
github.event.workflow_run.head_branch == 'main' ||
|
||||||
|
(github.event_name == 'workflow_dispatch' && github.event.inputs.deploy_to_pages == 'true')
|
||||||
with:
|
with:
|
||||||
path: docs/_build/html
|
path: docs/_build/html
|
||||||
|
|
||||||
# Deploy to GitHub Pages
|
# Deploy to GitHub Pages
|
||||||
deploy:
|
deploy:
|
||||||
if: github.ref == 'refs/heads/main' || (github.event_name == 'workflow_dispatch' && github.event.inputs.deploy_to_pages == 'true')
|
if: |
|
||||||
|
github.event.workflow_run.head_branch == 'main' ||
|
||||||
|
(github.event_name == 'workflow_dispatch' && github.event.inputs.deploy_to_pages == 'true')
|
||||||
environment:
|
environment:
|
||||||
name: github-pages
|
name: github-pages
|
||||||
url: ${{ steps.deployment.outputs.page_url }}
|
url: ${{ steps.deployment.outputs.page_url }}
|
||||||
|
|||||||
46
.github/workflows/multi-platform-build.yml
vendored
46
.github/workflows/multi-platform-build.yml
vendored
@@ -1,11 +1,16 @@
|
|||||||
name: Multi-Platform Conda Build
|
name: Multi-Platform Conda Build
|
||||||
|
|
||||||
on:
|
on:
|
||||||
|
# 在 CI Check 工作流完成后触发(仅限 main/dev 分支)
|
||||||
|
workflow_run:
|
||||||
|
workflows: ["CI Check"]
|
||||||
|
types:
|
||||||
|
- completed
|
||||||
|
branches: [main, dev]
|
||||||
|
# 支持 tag 推送(不依赖 CI Check)
|
||||||
push:
|
push:
|
||||||
branches: [main, dev]
|
|
||||||
tags: ['v*']
|
tags: ['v*']
|
||||||
pull_request:
|
# 手动触发
|
||||||
branches: [main, dev]
|
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
inputs:
|
inputs:
|
||||||
platforms:
|
platforms:
|
||||||
@@ -17,9 +22,37 @@ on:
|
|||||||
required: false
|
required: false
|
||||||
default: false
|
default: false
|
||||||
type: boolean
|
type: boolean
|
||||||
|
skip_ci_check:
|
||||||
|
description: '跳过等待 CI Check (手动触发时可选)'
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
type: boolean
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
# 等待 CI Check 完成的 job (仅用于 workflow_run 触发)
|
||||||
|
wait-for-ci:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
if: github.event_name == 'workflow_run'
|
||||||
|
outputs:
|
||||||
|
should_continue: ${{ steps.check.outputs.should_continue }}
|
||||||
|
steps:
|
||||||
|
- name: Check CI status
|
||||||
|
id: check
|
||||||
|
run: |
|
||||||
|
if [[ "${{ github.event.workflow_run.conclusion }}" == "success" ]]; then
|
||||||
|
echo "should_continue=true" >> $GITHUB_OUTPUT
|
||||||
|
echo "CI Check passed, proceeding with build"
|
||||||
|
else
|
||||||
|
echo "should_continue=false" >> $GITHUB_OUTPUT
|
||||||
|
echo "CI Check did not succeed (status: ${{ github.event.workflow_run.conclusion }}), skipping build"
|
||||||
|
fi
|
||||||
|
|
||||||
build:
|
build:
|
||||||
|
needs: [wait-for-ci]
|
||||||
|
# 运行条件:workflow_run 触发且 CI 成功,或者其他触发方式
|
||||||
|
if: |
|
||||||
|
always() &&
|
||||||
|
(needs.wait-for-ci.result == 'skipped' || needs.wait-for-ci.outputs.should_continue == 'true')
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
@@ -44,8 +77,10 @@ jobs:
|
|||||||
shell: bash -l {0}
|
shell: bash -l {0}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v6
|
||||||
with:
|
with:
|
||||||
|
# 如果是 workflow_run 触发,使用触发 CI Check 的 commit
|
||||||
|
ref: ${{ github.event.workflow_run.head_sha || github.ref }}
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: Check if platform should be built
|
- name: Check if platform should be built
|
||||||
@@ -69,7 +104,6 @@ jobs:
|
|||||||
channels: conda-forge,robostack-staging,defaults
|
channels: conda-forge,robostack-staging,defaults
|
||||||
channel-priority: strict
|
channel-priority: strict
|
||||||
activate-environment: build-env
|
activate-environment: build-env
|
||||||
auto-activate-base: false
|
|
||||||
auto-update-conda: false
|
auto-update-conda: false
|
||||||
show-channel-urls: true
|
show-channel-urls: true
|
||||||
|
|
||||||
@@ -115,7 +149,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Upload conda package artifacts
|
- name: Upload conda package artifacts
|
||||||
if: steps.should_build.outputs.should_build == 'true'
|
if: steps.should_build.outputs.should_build == 'true'
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v6
|
||||||
with:
|
with:
|
||||||
name: conda-package-${{ matrix.platform }}
|
name: conda-package-${{ matrix.platform }}
|
||||||
path: conda-packages-temp
|
path: conda-packages-temp
|
||||||
|
|||||||
113
.github/workflows/unilabos-conda-build.yml
vendored
113
.github/workflows/unilabos-conda-build.yml
vendored
@@ -1,25 +1,62 @@
|
|||||||
name: UniLabOS Conda Build
|
name: UniLabOS Conda Build
|
||||||
|
|
||||||
on:
|
on:
|
||||||
|
# 在 CI Check 成功后自动触发
|
||||||
|
workflow_run:
|
||||||
|
workflows: ["CI Check"]
|
||||||
|
types: [completed]
|
||||||
|
branches: [main, dev]
|
||||||
|
# 标签推送时直接触发(发布版本)
|
||||||
push:
|
push:
|
||||||
branches: [main, dev]
|
|
||||||
tags: ['v*']
|
tags: ['v*']
|
||||||
pull_request:
|
# 手动触发
|
||||||
branches: [main, dev]
|
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
inputs:
|
inputs:
|
||||||
platforms:
|
platforms:
|
||||||
description: '选择构建平台 (逗号分隔): linux-64, osx-64, osx-arm64, win-64'
|
description: '选择构建平台 (逗号分隔): linux-64, osx-64, osx-arm64, win-64'
|
||||||
required: false
|
required: false
|
||||||
default: 'linux-64'
|
default: 'linux-64'
|
||||||
|
build_full:
|
||||||
|
description: '是否构建 unilabos-full 完整包 (默认只构建 unilabos 基础包)'
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
type: boolean
|
||||||
upload_to_anaconda:
|
upload_to_anaconda:
|
||||||
description: '是否上传到Anaconda.org'
|
description: '是否上传到Anaconda.org'
|
||||||
required: false
|
required: false
|
||||||
default: false
|
default: false
|
||||||
type: boolean
|
type: boolean
|
||||||
|
skip_ci_check:
|
||||||
|
description: '跳过等待 CI Check (手动触发时可选)'
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
type: boolean
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
# 等待 CI Check 完成的 job (仅用于 workflow_run 触发)
|
||||||
|
wait-for-ci:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
if: github.event_name == 'workflow_run'
|
||||||
|
outputs:
|
||||||
|
should_continue: ${{ steps.check.outputs.should_continue }}
|
||||||
|
steps:
|
||||||
|
- name: Check CI status
|
||||||
|
id: check
|
||||||
|
run: |
|
||||||
|
if [[ "${{ github.event.workflow_run.conclusion }}" == "success" ]]; then
|
||||||
|
echo "should_continue=true" >> $GITHUB_OUTPUT
|
||||||
|
echo "CI Check passed, proceeding with build"
|
||||||
|
else
|
||||||
|
echo "should_continue=false" >> $GITHUB_OUTPUT
|
||||||
|
echo "CI Check did not succeed (status: ${{ github.event.workflow_run.conclusion }}), skipping build"
|
||||||
|
fi
|
||||||
|
|
||||||
build:
|
build:
|
||||||
|
needs: [wait-for-ci]
|
||||||
|
# 运行条件:workflow_run 触发且 CI 成功,或者其他触发方式
|
||||||
|
if: |
|
||||||
|
always() &&
|
||||||
|
(needs.wait-for-ci.result == 'skipped' || needs.wait-for-ci.outputs.should_continue == 'true')
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
@@ -40,8 +77,10 @@ jobs:
|
|||||||
shell: bash -l {0}
|
shell: bash -l {0}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v6
|
||||||
with:
|
with:
|
||||||
|
# 如果是 workflow_run 触发,使用触发 CI Check 的 commit
|
||||||
|
ref: ${{ github.event.workflow_run.head_sha || github.ref }}
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: Check if platform should be built
|
- name: Check if platform should be built
|
||||||
@@ -65,7 +104,6 @@ jobs:
|
|||||||
channels: conda-forge,robostack-staging,uni-lab,defaults
|
channels: conda-forge,robostack-staging,uni-lab,defaults
|
||||||
channel-priority: strict
|
channel-priority: strict
|
||||||
activate-environment: build-env
|
activate-environment: build-env
|
||||||
auto-activate-base: false
|
|
||||||
auto-update-conda: false
|
auto-update-conda: false
|
||||||
show-channel-urls: true
|
show-channel-urls: true
|
||||||
|
|
||||||
@@ -81,12 +119,61 @@ jobs:
|
|||||||
conda list | grep -E "(rattler-build|anaconda-client)"
|
conda list | grep -E "(rattler-build|anaconda-client)"
|
||||||
echo "Platform: ${{ matrix.platform }}"
|
echo "Platform: ${{ matrix.platform }}"
|
||||||
echo "OS: ${{ matrix.os }}"
|
echo "OS: ${{ matrix.os }}"
|
||||||
echo "Building UniLabOS package"
|
echo "Build full package: ${{ github.event.inputs.build_full || 'false' }}"
|
||||||
|
echo "Building packages:"
|
||||||
|
echo " - unilabos-env (environment dependencies)"
|
||||||
|
echo " - unilabos (with pip package)"
|
||||||
|
if [[ "${{ github.event.inputs.build_full }}" == "true" ]]; then
|
||||||
|
echo " - unilabos-full (complete package)"
|
||||||
|
fi
|
||||||
|
|
||||||
- name: Build conda package
|
- name: Build unilabos-env (conda environment only, noarch)
|
||||||
if: steps.should_build.outputs.should_build == 'true'
|
if: steps.should_build.outputs.should_build == 'true'
|
||||||
run: |
|
run: |
|
||||||
rattler-build build -r .conda/recipe.yaml -c uni-lab -c robostack-staging -c conda-forge
|
echo "Building unilabos-env (conda environment dependencies)..."
|
||||||
|
rattler-build build -r .conda/environment/recipe.yaml -c uni-lab -c robostack-staging -c conda-forge
|
||||||
|
|
||||||
|
- name: Upload unilabos-env to Anaconda.org (if enabled)
|
||||||
|
if: steps.should_build.outputs.should_build == 'true' && github.event.inputs.upload_to_anaconda == 'true'
|
||||||
|
run: |
|
||||||
|
echo "Uploading unilabos-env to uni-lab organization..."
|
||||||
|
for package in $(find ./output -name "unilabos-env*.conda"); do
|
||||||
|
anaconda -t ${{ secrets.ANACONDA_API_TOKEN }} upload --user uni-lab --force "$package"
|
||||||
|
done
|
||||||
|
|
||||||
|
- name: Build unilabos (with pip package)
|
||||||
|
if: steps.should_build.outputs.should_build == 'true'
|
||||||
|
run: |
|
||||||
|
echo "Building unilabos package..."
|
||||||
|
# 如果已上传到 Anaconda,从 uni-lab channel 获取 unilabos-env;否则从本地 output 获取
|
||||||
|
rattler-build build -r .conda/base/recipe.yaml -c uni-lab -c robostack-staging -c conda-forge --channel ./output
|
||||||
|
|
||||||
|
- name: Upload unilabos to Anaconda.org (if enabled)
|
||||||
|
if: steps.should_build.outputs.should_build == 'true' && github.event.inputs.upload_to_anaconda == 'true'
|
||||||
|
run: |
|
||||||
|
echo "Uploading unilabos to uni-lab organization..."
|
||||||
|
for package in $(find ./output -name "unilabos-0*.conda" -o -name "unilabos-[0-9]*.conda"); do
|
||||||
|
anaconda -t ${{ secrets.ANACONDA_API_TOKEN }} upload --user uni-lab --force "$package"
|
||||||
|
done
|
||||||
|
|
||||||
|
- name: Build unilabos-full - Only when explicitly requested
|
||||||
|
if: |
|
||||||
|
steps.should_build.outputs.should_build == 'true' &&
|
||||||
|
github.event.inputs.build_full == 'true'
|
||||||
|
run: |
|
||||||
|
echo "Building unilabos-full package on ${{ matrix.platform }}..."
|
||||||
|
rattler-build build -r .conda/full/recipe.yaml -c uni-lab -c robostack-staging -c conda-forge --channel ./output
|
||||||
|
|
||||||
|
- name: Upload unilabos-full to Anaconda.org (if enabled)
|
||||||
|
if: |
|
||||||
|
steps.should_build.outputs.should_build == 'true' &&
|
||||||
|
github.event.inputs.build_full == 'true' &&
|
||||||
|
github.event.inputs.upload_to_anaconda == 'true'
|
||||||
|
run: |
|
||||||
|
echo "Uploading unilabos-full to uni-lab organization..."
|
||||||
|
for package in $(find ./output -name "unilabos-full*.conda"); do
|
||||||
|
anaconda -t ${{ secrets.ANACONDA_API_TOKEN }} upload --user uni-lab --force "$package"
|
||||||
|
done
|
||||||
|
|
||||||
- name: List built packages
|
- name: List built packages
|
||||||
if: steps.should_build.outputs.should_build == 'true'
|
if: steps.should_build.outputs.should_build == 'true'
|
||||||
@@ -108,17 +195,9 @@ jobs:
|
|||||||
|
|
||||||
- name: Upload conda package artifacts
|
- name: Upload conda package artifacts
|
||||||
if: steps.should_build.outputs.should_build == 'true'
|
if: steps.should_build.outputs.should_build == 'true'
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v6
|
||||||
with:
|
with:
|
||||||
name: conda-package-unilabos-${{ matrix.platform }}
|
name: conda-package-unilabos-${{ matrix.platform }}
|
||||||
path: conda-packages-temp
|
path: conda-packages-temp
|
||||||
if-no-files-found: warn
|
if-no-files-found: warn
|
||||||
retention-days: 30
|
retention-days: 30
|
||||||
|
|
||||||
- name: Upload to Anaconda.org (uni-lab organization)
|
|
||||||
if: github.event.inputs.upload_to_anaconda == 'true'
|
|
||||||
run: |
|
|
||||||
for package in $(find ./output -name "*.conda"); do
|
|
||||||
echo "Uploading $package to uni-lab organization..."
|
|
||||||
anaconda -t ${{ secrets.ANACONDA_API_TOKEN }} upload --user uni-lab --force "$package"
|
|
||||||
done
|
|
||||||
|
|||||||
21
AGENTS.md
Normal file
21
AGENTS.md
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
# Uni-Lab-OS AI Agent 指南
|
||||||
|
|
||||||
|
## 设备接入
|
||||||
|
|
||||||
|
当用户要求添加/接入新设备时,读取 `docs/ai_guides/add_device.md` 并按其流程执行。
|
||||||
|
该指南完全自包含,包含物模型模板、现有设备接口快照、常见错误和验证清单。
|
||||||
|
|
||||||
|
## 关键规则
|
||||||
|
|
||||||
|
- 动作方法的参数名是接口契约,不可重命名(如 `volume` 不能改为 `volume_ml`)
|
||||||
|
- `status` 字符串必须与同类已有设备一致(如 `"Idle"` 不能改为 `"就绪"`)
|
||||||
|
- `self.data` 必须在 `__init__` 中预填充所有属性字段
|
||||||
|
- 异步方法中使用 `await self._ros_node.sleep()`,禁止 `time.sleep()` 和 `asyncio.sleep()`
|
||||||
|
|
||||||
|
## 项目结构
|
||||||
|
|
||||||
|
- 设备驱动:`unilabos/devices/<category>/<device_name>.py`
|
||||||
|
- 设备注册表:`unilabos/registry/devices/<device_name>.yaml`
|
||||||
|
- 实验图文件:`unilabos/test/experiments/*.json`
|
||||||
|
- 人类开发文档:`docs/developer_guide/`
|
||||||
|
- AI 专用指南:`docs/ai_guides/`
|
||||||
14
CLAUDE.md
Normal file
14
CLAUDE.md
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
# Uni-Lab-OS
|
||||||
|
|
||||||
|
## 设备接入
|
||||||
|
|
||||||
|
读取 `docs/ai_guides/add_device.md` 获取完整的自包含指南。
|
||||||
|
如果可以访问仓库,优先搜索 `unilabos/registry/devices/` 获取最新设备接口;
|
||||||
|
否则使用指南中内联的「现有设备接口快照」。
|
||||||
|
|
||||||
|
## 关键规则
|
||||||
|
|
||||||
|
- 动作方法的参数名是接口契约,不可重命名(如 `volume` 不能改为 `volume_ml`)
|
||||||
|
- `status` 字符串必须与同类已有设备一致(如 `"Idle"` 不能改为 `"就绪"`)
|
||||||
|
- `self.data` 必须在 `__init__` 中预填充所有属性字段
|
||||||
|
- 异步方法中使用 `await self._ros_node.sleep()`,禁止 `time.sleep()` 和 `asyncio.sleep()`
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
recursive-include unilabos/test *
|
recursive-include unilabos/test *
|
||||||
|
recursive-include unilabos/utils *
|
||||||
recursive-include unilabos/registry *.yaml
|
recursive-include unilabos/registry *.yaml
|
||||||
recursive-include unilabos/app/web/static *
|
recursive-include unilabos/app/web/static *
|
||||||
recursive-include unilabos/app/web/templates *
|
recursive-include unilabos/app/web/templates *
|
||||||
|
|||||||
38
README.md
38
README.md
@@ -31,26 +31,46 @@ Detailed documentation can be found at:
|
|||||||
|
|
||||||
## Quick Start
|
## Quick Start
|
||||||
|
|
||||||
1. Setup Conda Environment
|
### 1. Setup Conda Environment
|
||||||
|
|
||||||
Uni-Lab-OS recommends using `mamba` for environment management:
|
Uni-Lab-OS recommends using `mamba` for environment management. Choose the package that fits your needs:
|
||||||
|
|
||||||
|
| Package | Use Case | Contents |
|
||||||
|
|---------|----------|----------|
|
||||||
|
| `unilabos` | **Recommended for most users** | Complete package, ready to use |
|
||||||
|
| `unilabos-env` | Developers (editable install) | Environment only, install unilabos via pip |
|
||||||
|
| `unilabos-full` | Simulation/Visualization | unilabos + ROS2 Desktop + Gazebo + MoveIt |
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Create new environment
|
# Create new environment
|
||||||
mamba create -n unilab python=3.11.11
|
mamba create -n unilab python=3.11.14
|
||||||
mamba activate unilab
|
mamba activate unilab
|
||||||
mamba install -n unilab uni-lab::unilabos -c robostack-staging -c conda-forge
|
|
||||||
|
# Option A: Standard installation (recommended for most users)
|
||||||
|
mamba install uni-lab::unilabos -c robostack-staging -c conda-forge
|
||||||
|
|
||||||
|
# Option B: For developers (editable mode development)
|
||||||
|
mamba install uni-lab::unilabos-env -c robostack-staging -c conda-forge
|
||||||
|
# Then install unilabos and dependencies:
|
||||||
|
git clone https://github.com/deepmodeling/Uni-Lab-OS.git && cd Uni-Lab-OS
|
||||||
|
pip install -e .
|
||||||
|
uv pip install -r unilabos/utils/requirements.txt
|
||||||
|
|
||||||
|
# Option C: Full installation (simulation/visualization)
|
||||||
|
mamba install uni-lab::unilabos-full -c robostack-staging -c conda-forge
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Install Dev Uni-Lab-OS
|
**When to use which?**
|
||||||
|
- **unilabos**: Standard installation for production deployment and general usage (recommended)
|
||||||
|
- **unilabos-env**: For developers who need `pip install -e .` editable mode, modify source code
|
||||||
|
- **unilabos-full**: For simulation (Gazebo), visualization (rviz2), and Jupyter notebooks
|
||||||
|
|
||||||
|
### 2. Clone Repository (Optional, for developers)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Clone the repository
|
# Clone the repository (only needed for development or examples)
|
||||||
git clone https://github.com/deepmodeling/Uni-Lab-OS.git
|
git clone https://github.com/deepmodeling/Uni-Lab-OS.git
|
||||||
cd Uni-Lab-OS
|
cd Uni-Lab-OS
|
||||||
|
|
||||||
# Install Uni-Lab-OS
|
|
||||||
pip install .
|
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Start Uni-Lab System
|
3. Start Uni-Lab System
|
||||||
|
|||||||
38
README_zh.md
38
README_zh.md
@@ -31,26 +31,46 @@ Uni-Lab-OS 是一个用于实验室自动化的综合平台,旨在连接和控
|
|||||||
|
|
||||||
## 快速开始
|
## 快速开始
|
||||||
|
|
||||||
1. 配置 Conda 环境
|
### 1. 配置 Conda 环境
|
||||||
|
|
||||||
Uni-Lab-OS 建议使用 `mamba` 管理环境。根据您的操作系统选择适当的环境文件:
|
Uni-Lab-OS 建议使用 `mamba` 管理环境。根据您的需求选择合适的安装包:
|
||||||
|
|
||||||
|
| 安装包 | 适用场景 | 包含内容 |
|
||||||
|
|--------|----------|----------|
|
||||||
|
| `unilabos` | **推荐大多数用户** | 完整安装包,开箱即用 |
|
||||||
|
| `unilabos-env` | 开发者(可编辑安装) | 仅环境依赖,通过 pip 安装 unilabos |
|
||||||
|
| `unilabos-full` | 仿真/可视化 | unilabos + ROS2 桌面版 + Gazebo + MoveIt |
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# 创建新环境
|
# 创建新环境
|
||||||
mamba create -n unilab python=3.11.11
|
mamba create -n unilab python=3.11.14
|
||||||
mamba activate unilab
|
mamba activate unilab
|
||||||
mamba install -n unilab uni-lab::unilabos -c robostack-staging -c conda-forge
|
|
||||||
|
# 方案 A:标准安装(推荐大多数用户)
|
||||||
|
mamba install uni-lab::unilabos -c robostack-staging -c conda-forge
|
||||||
|
|
||||||
|
# 方案 B:开发者环境(可编辑模式开发)
|
||||||
|
mamba install uni-lab::unilabos-env -c robostack-staging -c conda-forge
|
||||||
|
# 然后安装 unilabos 和依赖:
|
||||||
|
git clone https://github.com/deepmodeling/Uni-Lab-OS.git && cd Uni-Lab-OS
|
||||||
|
pip install -e .
|
||||||
|
uv pip install -r unilabos/utils/requirements.txt
|
||||||
|
|
||||||
|
# 方案 C:完整安装(仿真/可视化)
|
||||||
|
mamba install uni-lab::unilabos-full -c robostack-staging -c conda-forge
|
||||||
```
|
```
|
||||||
|
|
||||||
2. 安装开发版 Uni-Lab-OS:
|
**如何选择?**
|
||||||
|
- **unilabos**:标准安装,适用于生产部署和日常使用(推荐)
|
||||||
|
- **unilabos-env**:开发者使用,支持 `pip install -e .` 可编辑模式,可修改源代码
|
||||||
|
- **unilabos-full**:需要仿真(Gazebo)、可视化(rviz2)或 Jupyter Notebook
|
||||||
|
|
||||||
|
### 2. 克隆仓库(可选,供开发者使用)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# 克隆仓库
|
# 克隆仓库(仅开发或查看示例时需要)
|
||||||
git clone https://github.com/deepmodeling/Uni-Lab-OS.git
|
git clone https://github.com/deepmodeling/Uni-Lab-OS.git
|
||||||
cd Uni-Lab-OS
|
cd Uni-Lab-OS
|
||||||
|
|
||||||
# 安装 Uni-Lab-OS
|
|
||||||
pip install .
|
|
||||||
```
|
```
|
||||||
|
|
||||||
3. 启动 Uni-Lab 系统
|
3. 启动 Uni-Lab 系统
|
||||||
|
|||||||
1100
docs/ai_guides/add_device.md
Normal file
1100
docs/ai_guides/add_device.md
Normal file
File diff suppressed because it is too large
Load Diff
344
docs/ai_guides/agent_prompt_template.md
Normal file
344
docs/ai_guides/agent_prompt_template.md
Normal file
@@ -0,0 +1,344 @@
|
|||||||
|
# Uni-Lab-OS 设备接入 Agent — 提示词模板
|
||||||
|
|
||||||
|
> 本文件提供一套可直接复制使用的 Agent 系统提示词,以及各平台的配置说明。
|
||||||
|
> 提示词模板与 `add_device.md`(领域知识)配合使用,前者控制 Agent 行为,后者提供完整的技术细节。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 系统提示词模板
|
||||||
|
|
||||||
|
以下内容可直接作为系统提示词 / Instructions / Custom Instructions 使用。`{{...}}` 标记的变量根据平台替换。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 开始复制 ↓
|
||||||
|
|
||||||
|
```
|
||||||
|
你是 Uni-Lab-OS 设备接入专家。你的任务是帮助用户将新的实验室硬件设备接入 Uni-Lab-OS 系统。
|
||||||
|
|
||||||
|
你能做的事:
|
||||||
|
- 根据用户描述,生成完整的设备驱动代码(Python)、注册表(YAML)和实验图文件(JSON)
|
||||||
|
- 解读用户提供的通信协议文档、SDK 代码、或口述的指令格式
|
||||||
|
- 诊断已有驱动代码的接口对齐问题
|
||||||
|
|
||||||
|
你不能做的事:
|
||||||
|
- 凭空猜测硬件私有通信指令(必须从用户提供的资料中获取)
|
||||||
|
- 替代真实硬件联调测试
|
||||||
|
|
||||||
|
## 知识来源
|
||||||
|
|
||||||
|
{{KNOWLEDGE_LOADING}}
|
||||||
|
|
||||||
|
## 工作流程
|
||||||
|
|
||||||
|
当用户要求接入新设备时,严格按以下流程执行。每个暂停点必须等待用户确认后再继续。
|
||||||
|
|
||||||
|
### 阶段 1:设备画像(交互)
|
||||||
|
|
||||||
|
向用户收集以下三个信息,可以一次性提问:
|
||||||
|
|
||||||
|
1. **设备类别** — 属于以下哪一种?
|
||||||
|
- temperature(温控)、pump_and_valve(泵阀)、motor(电机)
|
||||||
|
- heaterstirrer(加热搅拌)、balance(天平)、sensor(传感器)
|
||||||
|
- liquid_handling(液体处理)、robot_arm(机械臂)、workstation(工作站)
|
||||||
|
- virtual(虚拟设备)、custom(自定义)
|
||||||
|
- 如果是 pump_and_valve,进一步确认子类型:注射泵 / 电磁阀 / 蠕动泵
|
||||||
|
|
||||||
|
2. **设备英文名称** — 用于文件名和类名(如 my_heater、runze_sy03b)
|
||||||
|
|
||||||
|
3. **通信协议** — Serial(RS232/RS485) / Modbus RTU / Modbus TCP / TCP Socket / HTTP API / OPC UA / 无通信(虚拟)
|
||||||
|
|
||||||
|
⏸️ **暂停:等待用户回答后继续**
|
||||||
|
|
||||||
|
### 阶段 2:指令协议收集(交互)
|
||||||
|
|
||||||
|
根据上一步确定的通信协议,引导用户提供指令信息:
|
||||||
|
|
||||||
|
- 如果用户有 **SDK/驱动代码**:请用户提供代码文件,你从中提取通信逻辑
|
||||||
|
- 如果用户有 **协议文档**:请用户提供文档(PDF/图片/文本),你从中解析指令格式
|
||||||
|
- 如果用户 **口头描述**:针对每个标准动作逐一确认硬件指令
|
||||||
|
- 如果是 **标准协议**(Modbus 寄存器表、SCPI):请用户提供寄存器/指令映射
|
||||||
|
- 如果是 **虚拟设备**:跳过此阶段
|
||||||
|
|
||||||
|
⏸️ **暂停:确认已获取足够的指令协议信息**
|
||||||
|
|
||||||
|
### 阶段 3:确认摘要
|
||||||
|
|
||||||
|
在开始生成代码前,向用户展示你的理解摘要:
|
||||||
|
|
||||||
|
```
|
||||||
|
设备接入摘要:
|
||||||
|
- 设备名称:<name>
|
||||||
|
- 设备类别:<category>(<subtype>)
|
||||||
|
- 通信协议:<protocol>
|
||||||
|
- 指令来源:<source>
|
||||||
|
- 将要实现的属性:<list>
|
||||||
|
- 将要实现的动作:<list>
|
||||||
|
- 同类已有设备:<existing>(将对齐其接口)
|
||||||
|
```
|
||||||
|
|
||||||
|
⏸️ **暂停:用户确认"没问题"后再生成代码**
|
||||||
|
|
||||||
|
### 阶段 4:自动生成(无需暂停)
|
||||||
|
|
||||||
|
按以下顺序自动执行:
|
||||||
|
|
||||||
|
1. **对齐同类设备接口**(指南第四步)
|
||||||
|
- 查阅指南中的「现有设备接口快照」或搜索仓库注册表
|
||||||
|
- 确保所有已有设备的 status_types 和动作方法都被覆盖
|
||||||
|
- 参数名必须完全一致
|
||||||
|
|
||||||
|
2. **生成驱动代码** — `unilabos/devices/<category>/<name>.py`
|
||||||
|
|
||||||
|
3. **生成注册表** — `unilabos/registry/devices/<name>.yaml`(最小配置)
|
||||||
|
|
||||||
|
4. **生成图文件** — `unilabos/test/experiments/graph_example_<name>.json`
|
||||||
|
|
||||||
|
### 阶段 5:验证输出
|
||||||
|
|
||||||
|
生成完成后,逐项检查对齐验证清单并展示结果:
|
||||||
|
|
||||||
|
```
|
||||||
|
对齐验证清单:
|
||||||
|
- [x] 所有动作方法的参数名与已有设备完全一致
|
||||||
|
- [x] status 属性返回的字符串值与已有设备一致
|
||||||
|
- [x] 已有设备的所有 status_types 字段都有对应 @property
|
||||||
|
- [x] 已有设备的所有非 auto- 前缀的 action 都有对应方法
|
||||||
|
- [x] self.data 在 __init__ 中已预填充所有属性字段的默认值
|
||||||
|
- [x] 串口/二进制协议的响应解析先定位帧起始标记
|
||||||
|
```
|
||||||
|
|
||||||
|
如果有未通过的项,主动修复后再展示。
|
||||||
|
|
||||||
|
## 硬约束(违反任何一条都会导致设备接入失败)
|
||||||
|
|
||||||
|
1. **禁止重命名参数** — 动作方法的参数名(如 volume、position、max_velocity)是接口契约,框架通过参数名分派调用。绝不能加后缀(如 volume_ml)、改名(如 speed_ml_s)。单位写在 docstring 中。
|
||||||
|
|
||||||
|
2. **status 字符串必须一致** — 如果同类已有设备用英文(如 "Idle" / "Busy"),新驱动必须用相同的字符串,不能改为中文(如 "就绪")。
|
||||||
|
|
||||||
|
3. **self.data 必须预填充** — 不能用空字典 {}。框架在 initialize() 之前就可能读取属性值。每个 @property 对应的键都必须在 __init__ 中有初始值。
|
||||||
|
|
||||||
|
4. **禁止跳过接口对齐** — 对齐同类设备接口是强制步骤。缺失的属性和动作会导致设备在工作流中不可互换。
|
||||||
|
|
||||||
|
5. **串口解析先找帧头** — RS-485 总线上响应前常有回声/噪声字节。必须先定位帧起始标记(如 /、0xFE),禁止用硬编码索引直接解析。
|
||||||
|
|
||||||
|
6. **异步等待用 _ros_node.sleep** — 在 async 方法中使用 await self._ros_node.sleep(),禁止 time.sleep()(阻塞事件循环)和 asyncio.sleep()。
|
||||||
|
|
||||||
|
7. **物理单位对外暴露** — 对外参数使用用户友好的物理单位(mL、°C、RPM),驱动内部负责转换到硬件原始值(步数、Hz、寄存器值)。
|
||||||
|
|
||||||
|
## 代码骨架参考
|
||||||
|
|
||||||
|
所有设备驱动遵循以下结构:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import logging
|
||||||
|
import time as time_module
|
||||||
|
from typing import Dict, Any
|
||||||
|
|
||||||
|
try:
|
||||||
|
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
|
||||||
|
except ImportError:
|
||||||
|
BaseROS2DeviceNode = None
|
||||||
|
|
||||||
|
class MyDevice:
|
||||||
|
_ros_node: "BaseROS2DeviceNode"
|
||||||
|
|
||||||
|
def __init__(self, device_id: str = None, config: Dict[str, Any] = None, **kwargs):
|
||||||
|
if device_id is None and 'id' in kwargs:
|
||||||
|
device_id = kwargs.pop('id')
|
||||||
|
if config is None and 'config' in kwargs:
|
||||||
|
config = kwargs.pop('config')
|
||||||
|
self.device_id = device_id or "unknown_device"
|
||||||
|
self.config = config or {}
|
||||||
|
self.logger = logging.getLogger(f"MyDevice.{self.device_id}")
|
||||||
|
self.data = {
|
||||||
|
"status": "Idle",
|
||||||
|
# 所有 @property 的键都必须在此预填充
|
||||||
|
}
|
||||||
|
|
||||||
|
def post_init(self, ros_node: "BaseROS2DeviceNode"):
|
||||||
|
self._ros_node = ros_node
|
||||||
|
|
||||||
|
async def initialize(self) -> bool:
|
||||||
|
self.data["status"] = "Idle"
|
||||||
|
return True
|
||||||
|
|
||||||
|
async def cleanup(self) -> bool:
|
||||||
|
self.data["status"] = "Offline"
|
||||||
|
return True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def status(self) -> str:
|
||||||
|
return self.data.get("status", "Idle")
|
||||||
|
```
|
||||||
|
|
||||||
|
## 注册表最小配置
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
my_device:
|
||||||
|
class:
|
||||||
|
module: unilabos.devices.<category>.<file>:MyDevice
|
||||||
|
type: python
|
||||||
|
```
|
||||||
|
|
||||||
|
启动时 --complete_registry 自动生成 status_types 和 action_value_mappings。
|
||||||
|
|
||||||
|
## 图文件模板
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"nodes": [
|
||||||
|
{
|
||||||
|
"id": "my_device_1",
|
||||||
|
"name": "设备名称",
|
||||||
|
"children": [],
|
||||||
|
"parent": null,
|
||||||
|
"type": "device",
|
||||||
|
"class": "my_device",
|
||||||
|
"position": {"x": 0, "y": 0, "z": 0},
|
||||||
|
"config": {},
|
||||||
|
"data": {}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 现有设备接口快照(对齐用)
|
||||||
|
|
||||||
|
对齐时参考以下已有设备接口。如果能联网,优先从 GitHub 获取最新版本:
|
||||||
|
https://github.com/dptech-corp/Uni-Lab-OS/tree/main/unilabos/registry/devices/
|
||||||
|
|
||||||
|
### pump_and_valve — 注射泵
|
||||||
|
|
||||||
|
已有设备:syringe_pump_with_valve.runze.SY03B-T06
|
||||||
|
|
||||||
|
属性:status(str, "Idle"/"Busy"), valve_position(str), position(float, mL), max_velocity(float, mL/s), mode(int), plunger_position(String), velocity_grade(String), velocity_init(String), velocity_end(String)
|
||||||
|
|
||||||
|
方法签名(参数名不可改):
|
||||||
|
- initialize()
|
||||||
|
- set_valve_position(position)
|
||||||
|
- set_position(position: float, max_velocity: float = None)
|
||||||
|
- pull_plunger(volume: float)
|
||||||
|
- push_plunger(volume: float)
|
||||||
|
- set_max_velocity(velocity: float)
|
||||||
|
- set_velocity_grade(velocity)
|
||||||
|
- stop_operation()
|
||||||
|
|
||||||
|
### pump_and_valve — 电磁阀
|
||||||
|
|
||||||
|
属性:status(str), valve_position(str)
|
||||||
|
方法:open(), close(), set_valve_position(position), is_open(), is_closed()
|
||||||
|
|
||||||
|
### temperature
|
||||||
|
|
||||||
|
属性:status(str), temp(float, °C), temp_target(float, °C), stir_speed(float, RPM), temp_warning(float, °C)
|
||||||
|
|
||||||
|
### motor
|
||||||
|
|
||||||
|
属性:status(str), position(int)
|
||||||
|
|
||||||
|
### sensor
|
||||||
|
|
||||||
|
属性:level(bool), rssi(int)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 结束复制 ↑
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## `{{KNOWLEDGE_LOADING}}` 变量替换
|
||||||
|
|
||||||
|
根据平台能力,将提示词中的 `{{KNOWLEDGE_LOADING}}` 替换为以下对应内容:
|
||||||
|
|
||||||
|
### 方案 A:有知识库(Custom GPT / Claude Project)
|
||||||
|
|
||||||
|
```
|
||||||
|
你的知识库中包含 add_device.md 文件,这是完整的设备接入指南。
|
||||||
|
执行工作流时,参考该文件获取物模型模板、通信协议代码片段、指令协议模式和常见错误检查清单。
|
||||||
|
本提示词中的「现有设备接口快照」和「硬约束」是从指南中提炼的关键内容,以确保即使知识库检索不完整也能正确工作。
|
||||||
|
```
|
||||||
|
|
||||||
|
### 方案 B:有联网能力
|
||||||
|
|
||||||
|
```
|
||||||
|
执行工作流前,从以下 URL 获取完整的设备接入指南:
|
||||||
|
https://raw.githubusercontent.com/dptech-corp/Uni-Lab-OS/main/docs/ai_guides/add_device.md
|
||||||
|
|
||||||
|
该指南包含物模型模板、通信协议代码片段、指令协议模式和常见错误检查清单。
|
||||||
|
如果无法访问 URL,使用本提示词中内联的「现有设备接口快照」和「代码骨架参考」作为兜底。
|
||||||
|
```
|
||||||
|
|
||||||
|
### 方案 C:无知识库、无联网
|
||||||
|
|
||||||
|
```
|
||||||
|
完整的设备接入指南需要用户在对话中提供。
|
||||||
|
如果用户未主动提供,请在阶段 1 开始前询问:
|
||||||
|
"请将 add_device.md 的内容粘贴到对话中,或上传该文件。如果没有该文件,我将使用内置的精简规则工作。"
|
||||||
|
|
||||||
|
本提示词已内联了最关键的内容(硬约束 + 代码骨架 + 接口快照),足以生成基本正确的驱动。
|
||||||
|
但完整指南包含更多物模型模板和通信协议代码片段,能显著提升生成质量。
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 各平台配置指南
|
||||||
|
|
||||||
|
### OpenAI Custom GPT
|
||||||
|
|
||||||
|
1. 进入 https://chat.openai.com/gpts/editor
|
||||||
|
2. **Name**:Uni-Lab-OS 设备接入助手
|
||||||
|
3. **Description**:帮助用户将实验室硬件设备接入 Uni-Lab-OS 系统,自动生成驱动代码、注册表和图文件。
|
||||||
|
4. **Instructions**:粘贴上方系统提示词,`{{KNOWLEDGE_LOADING}}` 替换为方案 A
|
||||||
|
5. **Knowledge**:上传 `docs/ai_guides/add_device.md`
|
||||||
|
6. **Capabilities**:开启 Code Interpreter(用于代码验证)
|
||||||
|
7. **Conversation starters**:
|
||||||
|
- "我要接入一个新的注射泵"
|
||||||
|
- "帮我把这个 SDK 包装成 UniLab 驱动"
|
||||||
|
- "检查我的设备驱动有没有接口问题"
|
||||||
|
|
||||||
|
### Claude Project
|
||||||
|
|
||||||
|
1. 创建新 Project
|
||||||
|
2. **Custom Instructions**:粘贴系统提示词,`{{KNOWLEDGE_LOADING}}` 替换为方案 A
|
||||||
|
3. **Project Knowledge**:上传 `docs/ai_guides/add_device.md`
|
||||||
|
|
||||||
|
### API Agent(LangChain / AutoGen / 自建框架)
|
||||||
|
|
||||||
|
```python
|
||||||
|
system_prompt = """
|
||||||
|
<粘贴完整系统提示词,{{KNOWLEDGE_LOADING}} 替换为方案 B>
|
||||||
|
"""
|
||||||
|
|
||||||
|
# 如果框架支持工具调用,可注册以下工具:
|
||||||
|
tools = [
|
||||||
|
{
|
||||||
|
"name": "fetch_device_guide",
|
||||||
|
"description": "获取最新的 Uni-Lab-OS 设备接入指南",
|
||||||
|
"url": "https://raw.githubusercontent.com/dptech-corp/Uni-Lab-OS/main/docs/ai_guides/add_device.md"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "fetch_registry",
|
||||||
|
"description": "获取最新的设备注册表",
|
||||||
|
"url": "https://raw.githubusercontent.com/dptech-corp/Uni-Lab-OS/main/unilabos/registry/devices/{category}.yaml"
|
||||||
|
},
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
### Cursor Agent Mode
|
||||||
|
|
||||||
|
无需使用本模板。Cursor 中使用已有的 `.cursor/skills/add-device/SKILL.md`,它会自动读取 `docs/ai_guides/add_device.md` 并利用 Cursor 的工具能力(Grep 搜索注册表、AskQuestion 收集信息等)。
|
||||||
|
|
||||||
|
### 纯网页对话(ChatGPT / Claude 无 Project)
|
||||||
|
|
||||||
|
1. 第一条消息粘贴系统提示词(`{{KNOWLEDGE_LOADING}}` 替换为方案 C)
|
||||||
|
2. 第二条消息上传或粘贴 `add_device.md`
|
||||||
|
3. 第三条消息开始描述设备
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 维护说明
|
||||||
|
|
||||||
|
- **硬约束更新**:如果 `add_device.md` 中新增了禁止事项或常见错误,需要同步更新本模板的「硬约束」部分
|
||||||
|
- **接口快照更新**:新增设备类别或已有设备接口变更时,需要同步更新本模板的「现有设备接口快照」部分
|
||||||
|
- **工作流调整**:如果接入流程发生变化(新增步骤、合并步骤),需要同步调整「工作流程」部分
|
||||||
|
- 本模板与 `add_device.md` 是**互补关系**:模板定义 Agent 行为,指南提供领域知识。两者独立维护
|
||||||
@@ -31,6 +31,14 @@
|
|||||||
|
|
||||||
详细的安装步骤请参考 [安装指南](installation.md)。
|
详细的安装步骤请参考 [安装指南](installation.md)。
|
||||||
|
|
||||||
|
**选择合适的安装包:**
|
||||||
|
|
||||||
|
| 安装包 | 适用场景 | 包含组件 |
|
||||||
|
|--------|----------|----------|
|
||||||
|
| `unilabos` | **推荐大多数用户**,生产部署 | 完整安装包,开箱即用 |
|
||||||
|
| `unilabos-env` | 开发者(可编辑安装) | 仅环境依赖,通过 pip 安装 unilabos |
|
||||||
|
| `unilabos-full` | 仿真/可视化 | unilabos + 完整 ROS2 桌面版 + Gazebo + MoveIt |
|
||||||
|
|
||||||
**关键步骤:**
|
**关键步骤:**
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -38,15 +46,30 @@
|
|||||||
# 下载 Miniforge: https://github.com/conda-forge/miniforge/releases
|
# 下载 Miniforge: https://github.com/conda-forge/miniforge/releases
|
||||||
|
|
||||||
# 2. 创建 Conda 环境
|
# 2. 创建 Conda 环境
|
||||||
mamba create -n unilab python=3.11.11
|
mamba create -n unilab python=3.11.14
|
||||||
|
|
||||||
# 3. 激活环境
|
# 3. 激活环境
|
||||||
mamba activate unilab
|
mamba activate unilab
|
||||||
|
|
||||||
# 4. 安装 Uni-Lab-OS
|
# 4. 安装 Uni-Lab-OS(选择其一)
|
||||||
|
|
||||||
|
# 方案 A:标准安装(推荐大多数用户)
|
||||||
mamba install uni-lab::unilabos -c robostack-staging -c conda-forge
|
mamba install uni-lab::unilabos -c robostack-staging -c conda-forge
|
||||||
|
|
||||||
|
# 方案 B:开发者环境(可编辑模式开发)
|
||||||
|
mamba install uni-lab::unilabos-env -c robostack-staging -c conda-forge
|
||||||
|
pip install -e /path/to/Uni-Lab-OS # 可编辑安装
|
||||||
|
uv pip install -r unilabos/utils/requirements.txt # 安装 pip 依赖
|
||||||
|
|
||||||
|
# 方案 C:完整版(仿真/可视化)
|
||||||
|
mamba install uni-lab::unilabos-full -c robostack-staging -c conda-forge
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**选择建议:**
|
||||||
|
- **日常使用/生产部署**:使用 `unilabos`(推荐),完整功能,开箱即用
|
||||||
|
- **开发者**:使用 `unilabos-env` + `pip install -e .` + `uv pip install -r unilabos/utils/requirements.txt`,代码修改立即生效
|
||||||
|
- **仿真/可视化**:使用 `unilabos-full`,含 Gazebo、rviz2、MoveIt
|
||||||
|
|
||||||
#### 1.2 验证安装
|
#### 1.2 验证安装
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -416,6 +439,9 @@ unilab --ak your_ak --sk your_sk -g test/experiments/mock_devices/mock_all.json
|
|||||||
1. 访问 Web 界面,进入"仪器耗材"模块
|
1. 访问 Web 界面,进入"仪器耗材"模块
|
||||||
2. 在"仪器设备"区域找到并添加上述设备
|
2. 在"仪器设备"区域找到并添加上述设备
|
||||||
3. 在"物料耗材"区域找到并添加容器
|
3. 在"物料耗材"区域找到并添加容器
|
||||||
|
4. 在workstation中配置protocol_type包含PumpTransferProtocol
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
@@ -426,8 +452,9 @@ unilab --ak your_ak --sk your_sk -g test/experiments/mock_devices/mock_all.json
|
|||||||
**操作步骤:**
|
**操作步骤:**
|
||||||
|
|
||||||
1. 将两个 `container` 拖拽到 `workstation` 中
|
1. 将两个 `container` 拖拽到 `workstation` 中
|
||||||
2. 将 `virtual_transfer_pump` 拖拽到 `workstation` 中
|
2. 将 `virtual_multiway_valve` 拖拽到 `workstation` 中
|
||||||
3. 在画布上连接它们(建立父子关系)
|
3. 将 `virtual_transfer_pump` 拖拽到 `workstation` 中
|
||||||
|
4. 在画布上连接它们(建立父子关系)
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
@@ -768,7 +795,43 @@ Waiting for host service...
|
|||||||
|
|
||||||
详细的设备驱动编写指南请参考 [添加设备驱动](../developer_guide/add_device.md)。
|
详细的设备驱动编写指南请参考 [添加设备驱动](../developer_guide/add_device.md)。
|
||||||
|
|
||||||
#### 9.1 为什么需要自定义设备?
|
#### 9.1 开发环境准备
|
||||||
|
|
||||||
|
**推荐使用 `unilabos-env` + `pip install -e .` + `uv pip install`** 进行设备开发:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 1. 创建环境并安装 unilabos-env(ROS2 + conda 依赖 + uv)
|
||||||
|
mamba create -n unilab python=3.11.14
|
||||||
|
conda activate unilab
|
||||||
|
mamba install uni-lab::unilabos-env -c robostack-staging -c conda-forge
|
||||||
|
|
||||||
|
# 2. 克隆代码
|
||||||
|
git clone https://github.com/deepmodeling/Uni-Lab-OS.git
|
||||||
|
cd Uni-Lab-OS
|
||||||
|
|
||||||
|
# 3. 以可编辑模式安装(推荐使用脚本,自动检测中文环境)
|
||||||
|
python scripts/dev_install.py
|
||||||
|
|
||||||
|
# 或手动安装:
|
||||||
|
pip install -e .
|
||||||
|
uv pip install -r unilabos/utils/requirements.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
**为什么使用这种方式?**
|
||||||
|
- `unilabos-env` 提供 ROS2 核心组件和 uv(通过 conda 安装,避免编译)
|
||||||
|
- `unilabos/utils/requirements.txt` 包含所有运行时需要的 pip 依赖
|
||||||
|
- `dev_install.py` 自动检测中文环境,中文系统自动使用清华镜像
|
||||||
|
- 使用 `uv` 替代 `pip`,安装速度更快
|
||||||
|
- 可编辑模式:代码修改**立即生效**,无需重新安装
|
||||||
|
|
||||||
|
**如果安装失败或速度太慢**,可以手动执行(使用清华镜像):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install -e . -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
|
||||||
|
uv pip install -r unilabos/utils/requirements.txt -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 9.2 为什么需要自定义设备?
|
||||||
|
|
||||||
Uni-Lab-OS 内置了常见设备,但您的实验室可能有特殊设备需要集成:
|
Uni-Lab-OS 内置了常见设备,但您的实验室可能有特殊设备需要集成:
|
||||||
|
|
||||||
@@ -777,7 +840,7 @@ Uni-Lab-OS 内置了常见设备,但您的实验室可能有特殊设备需要
|
|||||||
- 特殊的实验流程
|
- 特殊的实验流程
|
||||||
- 第三方设备集成
|
- 第三方设备集成
|
||||||
|
|
||||||
#### 9.2 创建 Python 包
|
#### 9.3 创建 Python 包
|
||||||
|
|
||||||
为了方便开发和管理,建议为您的实验室创建独立的 Python 包。
|
为了方便开发和管理,建议为您的实验室创建独立的 Python 包。
|
||||||
|
|
||||||
@@ -814,7 +877,7 @@ touch my_lab_devices/my_lab_devices/__init__.py
|
|||||||
touch my_lab_devices/my_lab_devices/devices/__init__.py
|
touch my_lab_devices/my_lab_devices/devices/__init__.py
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 9.3 创建 setup.py
|
#### 9.4 创建 setup.py
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# my_lab_devices/setup.py
|
# my_lab_devices/setup.py
|
||||||
@@ -845,7 +908,7 @@ setup(
|
|||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 9.4 开发安装
|
#### 9.5 开发安装
|
||||||
|
|
||||||
使用 `-e` 参数进行可编辑安装,这样代码修改后立即生效:
|
使用 `-e` 参数进行可编辑安装,这样代码修改后立即生效:
|
||||||
|
|
||||||
@@ -860,7 +923,7 @@ pip install -e . -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
|
|||||||
- 方便调试和测试
|
- 方便调试和测试
|
||||||
- 支持版本控制(git)
|
- 支持版本控制(git)
|
||||||
|
|
||||||
#### 9.5 编写设备驱动
|
#### 9.6 编写设备驱动
|
||||||
|
|
||||||
创建设备驱动文件:
|
创建设备驱动文件:
|
||||||
|
|
||||||
@@ -1001,7 +1064,7 @@ class MyPump:
|
|||||||
- **返回 Dict**:所有动作方法返回字典类型
|
- **返回 Dict**:所有动作方法返回字典类型
|
||||||
- **文档字符串**:详细说明参数和功能
|
- **文档字符串**:详细说明参数和功能
|
||||||
|
|
||||||
#### 9.6 测试设备驱动
|
#### 9.7 测试设备驱动
|
||||||
|
|
||||||
创建简单的测试脚本:
|
创建简单的测试脚本:
|
||||||
|
|
||||||
|
|||||||
BIN
docs/user_guide/image/add_protocol.png
Normal file
BIN
docs/user_guide/image/add_protocol.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 81 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 275 KiB After Width: | Height: | Size: 415 KiB |
@@ -13,15 +13,26 @@
|
|||||||
- 开发者需要 Git 和基本的 Python 开发知识
|
- 开发者需要 Git 和基本的 Python 开发知识
|
||||||
- 自定义 msgs 需要 GitHub 账号
|
- 自定义 msgs 需要 GitHub 账号
|
||||||
|
|
||||||
|
## 安装包选择
|
||||||
|
|
||||||
|
Uni-Lab-OS 提供三个安装包版本,根据您的需求选择:
|
||||||
|
|
||||||
|
| 安装包 | 适用场景 | 包含组件 | 磁盘占用 |
|
||||||
|
|--------|----------|----------|----------|
|
||||||
|
| **unilabos** | **推荐大多数用户**,生产部署 | 完整安装包,开箱即用 | ~2-3 GB |
|
||||||
|
| **unilabos-env** | 开发者环境(可编辑安装) | 仅环境依赖,通过 pip 安装 unilabos | ~2 GB |
|
||||||
|
| **unilabos-full** | 仿真可视化、完整功能体验 | unilabos + 完整 ROS2 桌面版 + Gazebo + MoveIt | ~8-10 GB |
|
||||||
|
|
||||||
## 安装方式选择
|
## 安装方式选择
|
||||||
|
|
||||||
根据您的使用场景,选择合适的安装方式:
|
根据您的使用场景,选择合适的安装方式:
|
||||||
|
|
||||||
| 安装方式 | 适用人群 | 特点 | 安装时间 |
|
| 安装方式 | 适用人群 | 推荐安装包 | 特点 | 安装时间 |
|
||||||
| ---------------------- | -------------------- | ------------------------------ | ---------------------------- |
|
| ---------------------- | -------------------- | ----------------- | ------------------------------ | ---------------------------- |
|
||||||
| **方式一:一键安装** | 实验室用户、快速体验 | 预打包环境,离线可用,无需配置 | 5-10 分钟 (网络良好的情况下) |
|
| **方式一:一键安装** | 快速体验、演示 | 预打包环境 | 离线可用,无需配置 | 5-10 分钟 (网络良好的情况下) |
|
||||||
| **方式二:手动安装** | 标准用户、生产环境 | 灵活配置,版本可控 | 10-20 分钟 |
|
| **方式二:手动安装** | **大多数用户** | `unilabos` | 完整功能,开箱即用 | 10-20 分钟 |
|
||||||
| **方式三:开发者安装** | 开发者、需要修改源码 | 可编辑模式,支持自定义 msgs | 20-30 分钟 |
|
| **方式三:开发者安装** | 开发者、需要修改源码 | `unilabos-env` | 可编辑模式,支持自定义开发 | 20-30 分钟 |
|
||||||
|
| **仿真/可视化** | 仿真测试、可视化调试 | `unilabos-full` | 含 Gazebo、rviz2、MoveIt | 30-60 分钟 |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -144,17 +155,38 @@ bash Miniforge3-$(uname)-$(uname -m).sh
|
|||||||
使用以下命令创建 Uni-Lab 专用环境:
|
使用以下命令创建 Uni-Lab 专用环境:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
mamba create -n unilab python=3.11.11 # 目前ros2组件依赖版本大多为3.11.11
|
mamba create -n unilab python=3.11.14 # 目前ros2组件依赖版本大多为3.11.14
|
||||||
mamba activate unilab
|
mamba activate unilab
|
||||||
mamba install -n unilab uni-lab::unilabos -c robostack-staging -c conda-forge
|
|
||||||
|
# 选择安装包(三选一):
|
||||||
|
|
||||||
|
# 方案 A:标准安装(推荐大多数用户)
|
||||||
|
mamba install uni-lab::unilabos -c robostack-staging -c conda-forge
|
||||||
|
|
||||||
|
# 方案 B:开发者环境(可编辑模式开发)
|
||||||
|
mamba install uni-lab::unilabos-env -c robostack-staging -c conda-forge
|
||||||
|
# 然后安装 unilabos 和 pip 依赖:
|
||||||
|
git clone https://github.com/deepmodeling/Uni-Lab-OS.git && cd Uni-Lab-OS
|
||||||
|
pip install -e .
|
||||||
|
uv pip install -r unilabos/utils/requirements.txt
|
||||||
|
|
||||||
|
# 方案 C:完整版(含仿真和可视化工具)
|
||||||
|
mamba install uni-lab::unilabos-full -c robostack-staging -c conda-forge
|
||||||
```
|
```
|
||||||
|
|
||||||
**参数说明**:
|
**参数说明**:
|
||||||
|
|
||||||
- `-n unilab`: 创建名为 "unilab" 的环境
|
- `-n unilab`: 创建名为 "unilab" 的环境
|
||||||
- `uni-lab::unilabos`: 从 uni-lab channel 安装 unilabos 包
|
- `uni-lab::unilabos`: 安装 unilabos 完整包,开箱即用(推荐)
|
||||||
|
- `uni-lab::unilabos-env`: 仅安装环境依赖,适合开发者使用 `pip install -e .`
|
||||||
|
- `uni-lab::unilabos-full`: 安装完整包(含 ROS2 Desktop、Gazebo、MoveIt 等)
|
||||||
- `-c robostack-staging -c conda-forge`: 添加额外的软件源
|
- `-c robostack-staging -c conda-forge`: 添加额外的软件源
|
||||||
|
|
||||||
|
**包选择建议**:
|
||||||
|
- **日常使用/生产部署**:安装 `unilabos`(推荐,完整功能,开箱即用)
|
||||||
|
- **开发者**:安装 `unilabos-env`,然后使用 `uv pip install -r unilabos/utils/requirements.txt` 安装依赖,再 `pip install -e .` 进行可编辑安装
|
||||||
|
- **仿真/可视化**:安装 `unilabos-full`(Gazebo、rviz2、MoveIt)
|
||||||
|
|
||||||
**如果遇到网络问题**,可以使用清华镜像源加速下载:
|
**如果遇到网络问题**,可以使用清华镜像源加速下载:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -163,8 +195,14 @@ mamba config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/m
|
|||||||
mamba config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
|
mamba config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
|
||||||
mamba config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
|
mamba config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
|
||||||
|
|
||||||
# 然后重新执行安装命令
|
# 然后重新执行安装命令(推荐标准安装)
|
||||||
mamba create -n unilab uni-lab::unilabos -c robostack-staging
|
mamba create -n unilab uni-lab::unilabos -c robostack-staging
|
||||||
|
|
||||||
|
# 或完整版(仿真/可视化)
|
||||||
|
mamba create -n unilab uni-lab::unilabos-full -c robostack-staging
|
||||||
|
|
||||||
|
# pip 安装时使用清华镜像(开发者安装时使用)
|
||||||
|
uv pip install -r unilabos/utils/requirements.txt -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
|
||||||
```
|
```
|
||||||
|
|
||||||
### 第三步:激活环境
|
### 第三步:激活环境
|
||||||
@@ -203,58 +241,87 @@ cd Uni-Lab-OS
|
|||||||
cd Uni-Lab-OS
|
cd Uni-Lab-OS
|
||||||
```
|
```
|
||||||
|
|
||||||
### 第二步:安装基础环境
|
### 第二步:安装开发环境(unilabos-env)
|
||||||
|
|
||||||
**推荐方式**:先通过**方式一(一键安装)**或**方式二(手动安装)**完成基础环境的安装,这将包含所有必需的依赖项(ROS2、msgs 等)。
|
**重要**:开发者请使用 `unilabos-env` 包,它专为开发者设计:
|
||||||
|
- 包含 ROS2 核心组件和消息包(ros-humble-ros-core、std-msgs、geometry-msgs 等)
|
||||||
#### 选项 A:通过一键安装(推荐)
|
- 包含 transforms3d、cv-bridge、tf2 等 conda 依赖
|
||||||
|
- 包含 `uv` 工具,用于快速安装 pip 依赖
|
||||||
参考上文"方式一:一键安装",完成基础环境的安装后,激活环境:
|
- **不包含** pip 依赖和 unilabos 包(由 `pip install -e .` 和 `uv pip install` 安装)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
# 创建并激活环境
|
||||||
|
mamba create -n unilab python=3.11.14
|
||||||
conda activate unilab
|
conda activate unilab
|
||||||
|
|
||||||
|
# 安装开发者环境包(ROS2 + conda 依赖 + uv)
|
||||||
|
mamba install uni-lab::unilabos-env -c robostack-staging -c conda-forge
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 选项 B:通过手动安装
|
### 第三步:安装 pip 依赖和可编辑模式安装
|
||||||
|
|
||||||
参考上文"方式二:手动安装",创建并安装环境:
|
克隆代码并安装依赖:
|
||||||
|
|
||||||
```bash
|
|
||||||
mamba create -n unilab python=3.11.11
|
|
||||||
conda activate unilab
|
|
||||||
mamba install -n unilab uni-lab::unilabos -c robostack-staging -c conda-forge
|
|
||||||
```
|
|
||||||
|
|
||||||
**说明**:这会安装包括 Python 3.11.11、ROS2 Humble、ros-humble-unilabos-msgs 和所有必需依赖
|
|
||||||
|
|
||||||
### 第三步:切换到开发版本
|
|
||||||
|
|
||||||
现在你已经有了一个完整可用的 Uni-Lab 环境,接下来将 unilabos 包切换为开发版本:
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# 确保环境已激活
|
# 确保环境已激活
|
||||||
conda activate unilab
|
conda activate unilab
|
||||||
|
|
||||||
# 卸载 pip 安装的 unilabos(保留所有 conda 依赖)
|
# 克隆仓库(如果还未克隆)
|
||||||
pip uninstall unilabos -y
|
git clone https://github.com/deepmodeling/Uni-Lab-OS.git
|
||||||
|
|
||||||
# 克隆 dev 分支(如果还未克隆)
|
|
||||||
cd /path/to/your/workspace
|
|
||||||
git clone -b dev https://github.com/deepmodeling/Uni-Lab-OS.git
|
|
||||||
# 或者如果已经克隆,切换到 dev 分支
|
|
||||||
cd Uni-Lab-OS
|
cd Uni-Lab-OS
|
||||||
|
|
||||||
|
# 切换到 dev 分支(可选)
|
||||||
git checkout dev
|
git checkout dev
|
||||||
git pull
|
git pull
|
||||||
|
|
||||||
# 以可编辑模式安装开发版 unilabos
|
|
||||||
pip install -e . -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**参数说明**:
|
**推荐:使用安装脚本**(自动检测中文环境,使用 uv 加速):
|
||||||
|
|
||||||
- `-e`: editable mode(可编辑模式),代码修改立即生效,无需重新安装
|
```bash
|
||||||
- `-i`: 使用清华镜像源加速下载
|
# 自动检测中文环境,如果是中文系统则使用清华镜像
|
||||||
- `pip uninstall unilabos`: 只卸载 pip 安装的 unilabos 包,不影响 conda 安装的其他依赖(如 ROS2、msgs 等)
|
python scripts/dev_install.py
|
||||||
|
|
||||||
|
# 或者手动指定:
|
||||||
|
python scripts/dev_install.py --china # 强制使用清华镜像
|
||||||
|
python scripts/dev_install.py --no-mirror # 强制使用 PyPI
|
||||||
|
python scripts/dev_install.py --skip-deps # 跳过 pip 依赖安装
|
||||||
|
python scripts/dev_install.py --use-pip # 使用 pip 而非 uv
|
||||||
|
```
|
||||||
|
|
||||||
|
**手动安装**(如果脚本安装失败或速度太慢):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 1. 安装 unilabos(可编辑模式)
|
||||||
|
pip install -e .
|
||||||
|
|
||||||
|
# 2. 使用 uv 安装 pip 依赖(推荐,速度更快)
|
||||||
|
uv pip install -r unilabos/utils/requirements.txt
|
||||||
|
|
||||||
|
# 国内用户使用清华镜像:
|
||||||
|
pip install -e . -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
|
||||||
|
uv pip install -r unilabos/utils/requirements.txt -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
|
||||||
|
```
|
||||||
|
|
||||||
|
**注意**:
|
||||||
|
- `uv` 已包含在 `unilabos-env` 中,无需单独安装
|
||||||
|
- `unilabos/utils/requirements.txt` 包含运行 unilabos 所需的所有 pip 依赖
|
||||||
|
- 部分特殊包(如 pylabrobot)会在运行时由 unilabos 自动检测并安装
|
||||||
|
|
||||||
|
**为什么使用可编辑模式?**
|
||||||
|
|
||||||
|
- `-e` (editable mode):代码修改**立即生效**,无需重新安装
|
||||||
|
- 适合开发调试:修改代码后直接运行测试
|
||||||
|
- 与 `unilabos-env` 配合:环境依赖由 conda 管理,unilabos 代码由 pip 管理
|
||||||
|
|
||||||
|
**验证安装**:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 检查 unilabos 版本
|
||||||
|
python -c "import unilabos; print(unilabos.__version__)"
|
||||||
|
|
||||||
|
# 检查安装位置(应该指向你的代码目录)
|
||||||
|
pip show unilabos | grep Location
|
||||||
|
```
|
||||||
|
|
||||||
### 第四步:安装或自定义 ros-humble-unilabos-msgs(可选)
|
### 第四步:安装或自定义 ros-humble-unilabos-msgs(可选)
|
||||||
|
|
||||||
@@ -464,7 +531,45 @@ cd $CONDA_PREFIX/envs/unilab
|
|||||||
|
|
||||||
### 问题 8: 环境很大,有办法减小吗?
|
### 问题 8: 环境很大,有办法减小吗?
|
||||||
|
|
||||||
**解决方案**: 预打包的环境包含所有依赖,通常较大(压缩后 2-5GB)。这是为了确保离线安装和完整功能。如果空间有限,考虑使用方式二手动安装,只安装需要的组件。
|
**解决方案**:
|
||||||
|
|
||||||
|
1. **使用 `unilabos` 标准版**(推荐大多数用户):
|
||||||
|
```bash
|
||||||
|
mamba install uni-lab::unilabos -c robostack-staging -c conda-forge
|
||||||
|
```
|
||||||
|
标准版包含完整功能,环境大小约 2-3GB(相比完整版的 8-10GB)。
|
||||||
|
|
||||||
|
2. **使用 `unilabos-env` 开发者版**(最小化):
|
||||||
|
```bash
|
||||||
|
mamba install uni-lab::unilabos-env -c robostack-staging -c conda-forge
|
||||||
|
# 然后手动安装依赖
|
||||||
|
pip install -e .
|
||||||
|
uv pip install -r unilabos/utils/requirements.txt
|
||||||
|
```
|
||||||
|
开发者版只包含环境依赖,体积最小约 2GB。
|
||||||
|
|
||||||
|
3. **按需安装额外组件**:
|
||||||
|
如果后续需要特定功能,可以单独安装:
|
||||||
|
```bash
|
||||||
|
# 需要 Jupyter
|
||||||
|
mamba install jupyter jupyros
|
||||||
|
|
||||||
|
# 需要可视化
|
||||||
|
mamba install matplotlib opencv
|
||||||
|
|
||||||
|
# 需要仿真(注意:这会安装大量依赖)
|
||||||
|
mamba install ros-humble-gazebo-ros
|
||||||
|
```
|
||||||
|
|
||||||
|
4. **预打包环境问题**:
|
||||||
|
预打包环境(方式一)包含所有依赖,通常较大(压缩后 2-5GB)。这是为了确保离线安装和完整功能。
|
||||||
|
|
||||||
|
**包选择建议**:
|
||||||
|
| 需求 | 推荐包 | 预估大小 |
|
||||||
|
|------|--------|----------|
|
||||||
|
| 日常使用/生产部署 | `unilabos` | ~2-3 GB |
|
||||||
|
| 开发调试(可编辑模式) | `unilabos-env` | ~2 GB |
|
||||||
|
| 仿真/可视化 | `unilabos-full` | ~8-10 GB |
|
||||||
|
|
||||||
### 问题 9: 如何更新到最新版本?
|
### 问题 9: 如何更新到最新版本?
|
||||||
|
|
||||||
@@ -511,6 +616,7 @@ mamba update ros-humble-unilabos-msgs -c uni-lab -c robostack-staging -c conda-f
|
|||||||
|
|
||||||
**提示**:
|
**提示**:
|
||||||
|
|
||||||
- 生产环境推荐使用方式二(手动安装)的稳定版本
|
- **大多数用户**推荐使用方式二(手动安装)的 `unilabos` 标准版
|
||||||
- 开发和测试推荐使用方式三(开发者安装)
|
- **开发者**推荐使用方式三(开发者安装),安装 `unilabos-env` 后使用 `uv pip install -r unilabos/utils/requirements.txt` 安装依赖
|
||||||
- 快速体验和演示推荐使用方式一(一键安装)
|
- **仿真/可视化**推荐安装 `unilabos-full` 完整版
|
||||||
|
- **快速体验和演示**推荐使用方式一(一键安装)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package:
|
package:
|
||||||
name: ros-humble-unilabos-msgs
|
name: ros-humble-unilabos-msgs
|
||||||
version: 0.10.16
|
version: 0.10.18
|
||||||
source:
|
source:
|
||||||
path: ../../unilabos_msgs
|
path: ../../unilabos_msgs
|
||||||
target_directory: src
|
target_directory: src
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package:
|
package:
|
||||||
name: unilabos
|
name: unilabos
|
||||||
version: "0.10.16"
|
version: "0.10.18"
|
||||||
|
|
||||||
source:
|
source:
|
||||||
path: ../..
|
path: ../..
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ Verification:
|
|||||||
-------------
|
-------------
|
||||||
|
|
||||||
The verify_installation.py script will check:
|
The verify_installation.py script will check:
|
||||||
- Python version (3.11.11)
|
- Python version (3.11.14)
|
||||||
- ROS2 rclpy installation
|
- ROS2 rclpy installation
|
||||||
- UniLabOS installation and dependencies
|
- UniLabOS installation and dependencies
|
||||||
|
|
||||||
@@ -104,7 +104,7 @@ Build Information:
|
|||||||
|
|
||||||
Branch: {branch}
|
Branch: {branch}
|
||||||
Platform: {platform}
|
Platform: {platform}
|
||||||
Python: 3.11.11
|
Python: 3.11.14
|
||||||
Date: {build_date}
|
Date: {build_date}
|
||||||
|
|
||||||
Troubleshooting:
|
Troubleshooting:
|
||||||
|
|||||||
214
scripts/dev_install.py
Normal file
214
scripts/dev_install.py
Normal file
@@ -0,0 +1,214 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Development installation script for UniLabOS.
|
||||||
|
Auto-detects Chinese locale and uses appropriate mirror.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
python scripts/dev_install.py
|
||||||
|
python scripts/dev_install.py --no-mirror # Force no mirror
|
||||||
|
python scripts/dev_install.py --china # Force China mirror
|
||||||
|
python scripts/dev_install.py --skip-deps # Skip pip dependencies installation
|
||||||
|
|
||||||
|
Flow:
|
||||||
|
1. pip install -e . (install unilabos in editable mode)
|
||||||
|
2. Detect Chinese locale
|
||||||
|
3. Use uv to install pip dependencies from requirements.txt
|
||||||
|
4. Special packages (like pylabrobot) are handled by environment_check.py at runtime
|
||||||
|
"""
|
||||||
|
|
||||||
|
import locale
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import argparse
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
# Tsinghua mirror URL
|
||||||
|
TSINGHUA_MIRROR = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"
|
||||||
|
|
||||||
|
|
||||||
|
def is_chinese_locale() -> bool:
|
||||||
|
"""
|
||||||
|
Detect if system is in Chinese locale.
|
||||||
|
Same logic as EnvironmentChecker._is_chinese_locale()
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
lang = locale.getdefaultlocale()[0]
|
||||||
|
if lang and ("zh" in lang.lower() or "chinese" in lang.lower()):
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def run_command(cmd: list, description: str, retry: int = 2) -> bool:
|
||||||
|
"""Run command with retry support."""
|
||||||
|
print(f"[INFO] {description}")
|
||||||
|
print(f"[CMD] {' '.join(cmd)}")
|
||||||
|
|
||||||
|
for attempt in range(retry + 1):
|
||||||
|
try:
|
||||||
|
result = subprocess.run(cmd, check=True, timeout=600)
|
||||||
|
print(f"[OK] {description}")
|
||||||
|
return True
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
if attempt < retry:
|
||||||
|
print(f"[WARN] Attempt {attempt + 1} failed, retrying...")
|
||||||
|
else:
|
||||||
|
print(f"[ERROR] {description} failed: {e}")
|
||||||
|
return False
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
print(f"[ERROR] {description} timed out")
|
||||||
|
return False
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def install_editable(project_root: Path, use_mirror: bool) -> bool:
|
||||||
|
"""Install unilabos in editable mode using pip."""
|
||||||
|
cmd = [sys.executable, "-m", "pip", "install", "-e", str(project_root)]
|
||||||
|
if use_mirror:
|
||||||
|
cmd.extend(["-i", TSINGHUA_MIRROR])
|
||||||
|
|
||||||
|
return run_command(cmd, "Installing unilabos in editable mode")
|
||||||
|
|
||||||
|
|
||||||
|
def install_requirements_uv(requirements_file: Path, use_mirror: bool) -> bool:
|
||||||
|
"""Install pip dependencies using uv (installed via conda-forge::uv)."""
|
||||||
|
cmd = ["uv", "pip", "install", "-r", str(requirements_file)]
|
||||||
|
if use_mirror:
|
||||||
|
cmd.extend(["-i", TSINGHUA_MIRROR])
|
||||||
|
|
||||||
|
return run_command(cmd, "Installing pip dependencies with uv", retry=2)
|
||||||
|
|
||||||
|
|
||||||
|
def install_requirements_pip(requirements_file: Path, use_mirror: bool) -> bool:
|
||||||
|
"""Fallback: Install pip dependencies using pip."""
|
||||||
|
cmd = [sys.executable, "-m", "pip", "install", "-r", str(requirements_file)]
|
||||||
|
if use_mirror:
|
||||||
|
cmd.extend(["-i", TSINGHUA_MIRROR])
|
||||||
|
|
||||||
|
return run_command(cmd, "Installing pip dependencies with pip", retry=2)
|
||||||
|
|
||||||
|
|
||||||
|
def check_uv_available() -> bool:
|
||||||
|
"""Check if uv is available (installed via conda-forge::uv)."""
|
||||||
|
try:
|
||||||
|
subprocess.run(["uv", "--version"], capture_output=True, check=True)
|
||||||
|
return True
|
||||||
|
except (subprocess.CalledProcessError, FileNotFoundError):
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser(description="Development installation script for UniLabOS")
|
||||||
|
parser.add_argument("--china", action="store_true", help="Force use China mirror (Tsinghua)")
|
||||||
|
parser.add_argument("--no-mirror", action="store_true", help="Force use default PyPI (no mirror)")
|
||||||
|
parser.add_argument(
|
||||||
|
"--skip-deps", action="store_true", help="Skip pip dependencies installation (only install unilabos)"
|
||||||
|
)
|
||||||
|
parser.add_argument("--use-pip", action="store_true", help="Use pip instead of uv for dependencies")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
# Determine project root
|
||||||
|
script_dir = Path(__file__).parent
|
||||||
|
project_root = script_dir.parent
|
||||||
|
requirements_file = project_root / "unilabos" / "utils" / "requirements.txt"
|
||||||
|
|
||||||
|
if not (project_root / "setup.py").exists():
|
||||||
|
print(f"[ERROR] setup.py not found in {project_root}")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
print("=" * 60)
|
||||||
|
print("UniLabOS Development Installation")
|
||||||
|
print("=" * 60)
|
||||||
|
print(f"Project root: {project_root}")
|
||||||
|
print()
|
||||||
|
|
||||||
|
# Determine mirror usage based on locale
|
||||||
|
if args.no_mirror:
|
||||||
|
use_mirror = False
|
||||||
|
print("[INFO] Mirror disabled by --no-mirror flag")
|
||||||
|
elif args.china:
|
||||||
|
use_mirror = True
|
||||||
|
print("[INFO] China mirror enabled by --china flag")
|
||||||
|
else:
|
||||||
|
use_mirror = is_chinese_locale()
|
||||||
|
if use_mirror:
|
||||||
|
print("[INFO] Chinese locale detected, using Tsinghua mirror")
|
||||||
|
else:
|
||||||
|
print("[INFO] Non-Chinese locale detected, using default PyPI")
|
||||||
|
|
||||||
|
print()
|
||||||
|
|
||||||
|
# Step 1: Install unilabos in editable mode
|
||||||
|
print("[STEP 1] Installing unilabos in editable mode...")
|
||||||
|
if not install_editable(project_root, use_mirror):
|
||||||
|
print("[ERROR] Failed to install unilabos")
|
||||||
|
print()
|
||||||
|
print("Manual fallback:")
|
||||||
|
if use_mirror:
|
||||||
|
print(f" pip install -e {project_root} -i {TSINGHUA_MIRROR}")
|
||||||
|
else:
|
||||||
|
print(f" pip install -e {project_root}")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
print()
|
||||||
|
|
||||||
|
# Step 2: Install pip dependencies
|
||||||
|
if args.skip_deps:
|
||||||
|
print("[INFO] Skipping pip dependencies installation (--skip-deps)")
|
||||||
|
else:
|
||||||
|
print("[STEP 2] Installing pip dependencies...")
|
||||||
|
|
||||||
|
if not requirements_file.exists():
|
||||||
|
print(f"[WARN] Requirements file not found: {requirements_file}")
|
||||||
|
print("[INFO] Skipping dependencies installation")
|
||||||
|
else:
|
||||||
|
# Try uv first (faster), fallback to pip
|
||||||
|
if args.use_pip:
|
||||||
|
print("[INFO] Using pip (--use-pip flag)")
|
||||||
|
success = install_requirements_pip(requirements_file, use_mirror)
|
||||||
|
elif check_uv_available():
|
||||||
|
print("[INFO] Using uv (installed via conda-forge::uv)")
|
||||||
|
success = install_requirements_uv(requirements_file, use_mirror)
|
||||||
|
if not success:
|
||||||
|
print("[WARN] uv failed, falling back to pip...")
|
||||||
|
success = install_requirements_pip(requirements_file, use_mirror)
|
||||||
|
else:
|
||||||
|
print("[WARN] uv not available (should be installed via: mamba install conda-forge::uv)")
|
||||||
|
print("[INFO] Falling back to pip...")
|
||||||
|
success = install_requirements_pip(requirements_file, use_mirror)
|
||||||
|
|
||||||
|
if not success:
|
||||||
|
print()
|
||||||
|
print("[WARN] Failed to install some dependencies automatically.")
|
||||||
|
print("You can manually install them:")
|
||||||
|
if use_mirror:
|
||||||
|
print(f" uv pip install -r {requirements_file} -i {TSINGHUA_MIRROR}")
|
||||||
|
print(" or:")
|
||||||
|
print(f" pip install -r {requirements_file} -i {TSINGHUA_MIRROR}")
|
||||||
|
else:
|
||||||
|
print(f" uv pip install -r {requirements_file}")
|
||||||
|
print(" or:")
|
||||||
|
print(f" pip install -r {requirements_file}")
|
||||||
|
|
||||||
|
print()
|
||||||
|
print("=" * 60)
|
||||||
|
print("Installation complete!")
|
||||||
|
print("=" * 60)
|
||||||
|
print()
|
||||||
|
print("Note: Some special packages (like pylabrobot) are installed")
|
||||||
|
print("automatically at runtime by unilabos if needed.")
|
||||||
|
print()
|
||||||
|
print("Verify installation:")
|
||||||
|
print(' python -c "import unilabos; print(unilabos.__version__)"')
|
||||||
|
print()
|
||||||
|
print("If you encounter issues, you can manually install dependencies:")
|
||||||
|
if use_mirror:
|
||||||
|
print(f" uv pip install -r unilabos/utils/requirements.txt -i {TSINGHUA_MIRROR}")
|
||||||
|
else:
|
||||||
|
print(" uv pip install -r unilabos/utils/requirements.txt")
|
||||||
|
print()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
2
setup.py
2
setup.py
@@ -4,7 +4,7 @@ package_name = 'unilabos'
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name=package_name,
|
name=package_name,
|
||||||
version='0.10.16',
|
version='0.10.18',
|
||||||
packages=find_packages(),
|
packages=find_packages(),
|
||||||
include_package_data=True,
|
include_package_data=True,
|
||||||
install_requires=['setuptools'],
|
install_requires=['setuptools'],
|
||||||
|
|||||||
213
tests/workflow/test.json
Normal file
213
tests/workflow/test.json
Normal file
@@ -0,0 +1,213 @@
|
|||||||
|
{
|
||||||
|
"workflow": [
|
||||||
|
{
|
||||||
|
"action": "transfer_liquid",
|
||||||
|
"action_args": {
|
||||||
|
"sources": "cell_lines",
|
||||||
|
"targets": "Liquid_1",
|
||||||
|
"asp_vol": 100.0,
|
||||||
|
"dis_vol": 74.75,
|
||||||
|
"asp_flow_rate": 94.0,
|
||||||
|
"dis_flow_rate": 95.5
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "transfer_liquid",
|
||||||
|
"action_args": {
|
||||||
|
"sources": "cell_lines",
|
||||||
|
"targets": "Liquid_2",
|
||||||
|
"asp_vol": 100.0,
|
||||||
|
"dis_vol": 74.75,
|
||||||
|
"asp_flow_rate": 94.0,
|
||||||
|
"dis_flow_rate": 95.5
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "transfer_liquid",
|
||||||
|
"action_args": {
|
||||||
|
"sources": "cell_lines",
|
||||||
|
"targets": "Liquid_3",
|
||||||
|
"asp_vol": 100.0,
|
||||||
|
"dis_vol": 74.75,
|
||||||
|
"asp_flow_rate": 94.0,
|
||||||
|
"dis_flow_rate": 95.5
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "transfer_liquid",
|
||||||
|
"action_args": {
|
||||||
|
"sources": "cell_lines_2",
|
||||||
|
"targets": "Liquid_4",
|
||||||
|
"asp_vol": 100.0,
|
||||||
|
"dis_vol": 74.75,
|
||||||
|
"asp_flow_rate": 94.0,
|
||||||
|
"dis_flow_rate": 95.5
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "transfer_liquid",
|
||||||
|
"action_args": {
|
||||||
|
"sources": "cell_lines_2",
|
||||||
|
"targets": "Liquid_5",
|
||||||
|
"asp_vol": 100.0,
|
||||||
|
"dis_vol": 74.75,
|
||||||
|
"asp_flow_rate": 94.0,
|
||||||
|
"dis_flow_rate": 95.5
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "transfer_liquid",
|
||||||
|
"action_args": {
|
||||||
|
"sources": "cell_lines_2",
|
||||||
|
"targets": "Liquid_6",
|
||||||
|
"asp_vol": 100.0,
|
||||||
|
"dis_vol": 74.75,
|
||||||
|
"asp_flow_rate": 94.0,
|
||||||
|
"dis_flow_rate": 95.5
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "transfer_liquid",
|
||||||
|
"action_args": {
|
||||||
|
"sources": "cell_lines_3",
|
||||||
|
"targets": "dest_set",
|
||||||
|
"asp_vol": 100.0,
|
||||||
|
"dis_vol": 74.75,
|
||||||
|
"asp_flow_rate": 94.0,
|
||||||
|
"dis_flow_rate": 95.5
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "transfer_liquid",
|
||||||
|
"action_args": {
|
||||||
|
"sources": "cell_lines_3",
|
||||||
|
"targets": "dest_set_2",
|
||||||
|
"asp_vol": 100.0,
|
||||||
|
"dis_vol": 74.75,
|
||||||
|
"asp_flow_rate": 94.0,
|
||||||
|
"dis_flow_rate": 95.5
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "transfer_liquid",
|
||||||
|
"action_args": {
|
||||||
|
"sources": "cell_lines_3",
|
||||||
|
"targets": "dest_set_3",
|
||||||
|
"asp_vol": 100.0,
|
||||||
|
"dis_vol": 74.75,
|
||||||
|
"asp_flow_rate": 94.0,
|
||||||
|
"dis_flow_rate": 95.5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"reagent": {
|
||||||
|
"Liquid_1": {
|
||||||
|
"slot": 1,
|
||||||
|
"well": [
|
||||||
|
"A4",
|
||||||
|
"A7",
|
||||||
|
"A10"
|
||||||
|
],
|
||||||
|
"labware": "rep 1"
|
||||||
|
},
|
||||||
|
"Liquid_4": {
|
||||||
|
"slot": 1,
|
||||||
|
"well": [
|
||||||
|
"A4",
|
||||||
|
"A7",
|
||||||
|
"A10"
|
||||||
|
],
|
||||||
|
"labware": "rep 1"
|
||||||
|
},
|
||||||
|
"dest_set": {
|
||||||
|
"slot": 1,
|
||||||
|
"well": [
|
||||||
|
"A4",
|
||||||
|
"A7",
|
||||||
|
"A10"
|
||||||
|
],
|
||||||
|
"labware": "rep 1"
|
||||||
|
},
|
||||||
|
"Liquid_2": {
|
||||||
|
"slot": 2,
|
||||||
|
"well": [
|
||||||
|
"A3",
|
||||||
|
"A5",
|
||||||
|
"A8"
|
||||||
|
],
|
||||||
|
"labware": "rep 2"
|
||||||
|
},
|
||||||
|
"Liquid_5": {
|
||||||
|
"slot": 2,
|
||||||
|
"well": [
|
||||||
|
"A3",
|
||||||
|
"A5",
|
||||||
|
"A8"
|
||||||
|
],
|
||||||
|
"labware": "rep 2"
|
||||||
|
},
|
||||||
|
"dest_set_2": {
|
||||||
|
"slot": 2,
|
||||||
|
"well": [
|
||||||
|
"A3",
|
||||||
|
"A5",
|
||||||
|
"A8"
|
||||||
|
],
|
||||||
|
"labware": "rep 2"
|
||||||
|
},
|
||||||
|
"Liquid_3": {
|
||||||
|
"slot": 3,
|
||||||
|
"well": [
|
||||||
|
"A4",
|
||||||
|
"A6",
|
||||||
|
"A10"
|
||||||
|
],
|
||||||
|
"labware": "rep 3"
|
||||||
|
},
|
||||||
|
"Liquid_6": {
|
||||||
|
"slot": 3,
|
||||||
|
"well": [
|
||||||
|
"A4",
|
||||||
|
"A6",
|
||||||
|
"A10"
|
||||||
|
],
|
||||||
|
"labware": "rep 3"
|
||||||
|
},
|
||||||
|
"dest_set_3": {
|
||||||
|
"slot": 3,
|
||||||
|
"well": [
|
||||||
|
"A4",
|
||||||
|
"A6",
|
||||||
|
"A10"
|
||||||
|
],
|
||||||
|
"labware": "rep 3"
|
||||||
|
},
|
||||||
|
"cell_lines": {
|
||||||
|
"slot": 4,
|
||||||
|
"well": [
|
||||||
|
"A1",
|
||||||
|
"A3",
|
||||||
|
"A5"
|
||||||
|
],
|
||||||
|
"labware": "DRUG + YOYO-MEDIA"
|
||||||
|
},
|
||||||
|
"cell_lines_2": {
|
||||||
|
"slot": 4,
|
||||||
|
"well": [
|
||||||
|
"A1",
|
||||||
|
"A3",
|
||||||
|
"A5"
|
||||||
|
],
|
||||||
|
"labware": "DRUG + YOYO-MEDIA"
|
||||||
|
},
|
||||||
|
"cell_lines_3": {
|
||||||
|
"slot": 4,
|
||||||
|
"well": [
|
||||||
|
"A1",
|
||||||
|
"A3",
|
||||||
|
"A5"
|
||||||
|
],
|
||||||
|
"labware": "DRUG + YOYO-MEDIA"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1 +1 @@
|
|||||||
__version__ = "0.10.16"
|
__version__ = "0.10.18"
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import asyncio
|
import asyncio
|
||||||
import os
|
import os
|
||||||
|
import platform
|
||||||
import shutil
|
import shutil
|
||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
from typing import Dict, Any, List
|
from typing import Dict, Any, List
|
||||||
|
|
||||||
import networkx as nx
|
import networkx as nx
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
@@ -17,9 +17,9 @@ unilabos_dir = os.path.dirname(os.path.dirname(current_dir))
|
|||||||
if unilabos_dir not in sys.path:
|
if unilabos_dir not in sys.path:
|
||||||
sys.path.append(unilabos_dir)
|
sys.path.append(unilabos_dir)
|
||||||
|
|
||||||
|
from unilabos.app.utils import cleanup_for_restart
|
||||||
from unilabos.utils.banner_print import print_status, print_unilab_banner
|
from unilabos.utils.banner_print import print_status, print_unilab_banner
|
||||||
from unilabos.config.config import load_config, BasicConfig, HTTPConfig
|
from unilabos.config.config import load_config, BasicConfig, HTTPConfig
|
||||||
from unilabos.app.utils import cleanup_for_restart
|
|
||||||
|
|
||||||
# Global restart flags (used by ws_client and web/server)
|
# Global restart flags (used by ws_client and web/server)
|
||||||
_restart_requested: bool = False
|
_restart_requested: bool = False
|
||||||
@@ -172,6 +172,12 @@ def parse_args():
|
|||||||
action="store_true",
|
action="store_true",
|
||||||
help="Disable sending update feedback to server",
|
help="Disable sending update feedback to server",
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--test_mode",
|
||||||
|
action="store_true",
|
||||||
|
default=False,
|
||||||
|
help="Test mode: all actions simulate execution and return mock results without running real hardware",
|
||||||
|
)
|
||||||
# workflow upload subcommand
|
# workflow upload subcommand
|
||||||
workflow_parser = subparsers.add_parser(
|
workflow_parser = subparsers.add_parser(
|
||||||
"workflow_upload",
|
"workflow_upload",
|
||||||
@@ -205,6 +211,12 @@ def parse_args():
|
|||||||
default=False,
|
default=False,
|
||||||
help="Whether to publish the workflow (default: False)",
|
help="Whether to publish the workflow (default: False)",
|
||||||
)
|
)
|
||||||
|
workflow_parser.add_argument(
|
||||||
|
"--description",
|
||||||
|
type=str,
|
||||||
|
default="",
|
||||||
|
help="Workflow description, used when publishing the workflow",
|
||||||
|
)
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
@@ -217,7 +229,10 @@ def main():
|
|||||||
args_dict = vars(args)
|
args_dict = vars(args)
|
||||||
|
|
||||||
# 环境检查 - 检查并自动安装必需的包 (可选)
|
# 环境检查 - 检查并自动安装必需的包 (可选)
|
||||||
if not args_dict.get("skip_env_check", False):
|
skip_env_check = args_dict.get("skip_env_check", False)
|
||||||
|
check_mode = args_dict.get("check_mode", False)
|
||||||
|
|
||||||
|
if not skip_env_check:
|
||||||
from unilabos.utils.environment_check import check_environment
|
from unilabos.utils.environment_check import check_environment
|
||||||
|
|
||||||
if not check_environment(auto_install=True):
|
if not check_environment(auto_install=True):
|
||||||
@@ -228,49 +243,75 @@ def main():
|
|||||||
|
|
||||||
# 加载配置文件,优先加载config,然后从env读取
|
# 加载配置文件,优先加载config,然后从env读取
|
||||||
config_path = args_dict.get("config")
|
config_path = args_dict.get("config")
|
||||||
if os.getcwd().endswith("unilabos_data"):
|
|
||||||
working_dir = os.path.abspath(os.getcwd())
|
|
||||||
else:
|
|
||||||
working_dir = os.path.abspath(os.path.join(os.getcwd(), "unilabos_data"))
|
|
||||||
|
|
||||||
if args_dict.get("working_dir"):
|
# === 解析 working_dir ===
|
||||||
working_dir = args_dict.get("working_dir", "")
|
# 规则1: working_dir 传入 → 检测 unilabos_data 子目录,已是则不修改
|
||||||
if config_path and not os.path.exists(config_path):
|
# 规则2: 仅 config_path 传入 → 用其父目录作为 working_dir
|
||||||
config_path = os.path.join(working_dir, "local_config.py")
|
# 规则4: 两者都传入 → 各用各的,但 working_dir 仍做 unilabos_data 子目录检测
|
||||||
if not os.path.exists(config_path):
|
raw_working_dir = args_dict.get("working_dir")
|
||||||
print_status(
|
if raw_working_dir:
|
||||||
f"当前工作目录 {working_dir} 未找到local_config.py,请通过 --config 传入 local_config.py 文件路径",
|
working_dir = os.path.abspath(raw_working_dir)
|
||||||
"error",
|
|
||||||
)
|
|
||||||
os._exit(1)
|
|
||||||
elif config_path and os.path.exists(config_path):
|
elif config_path and os.path.exists(config_path):
|
||||||
working_dir = os.path.dirname(config_path)
|
working_dir = os.path.dirname(os.path.abspath(config_path))
|
||||||
elif os.path.exists(working_dir) and os.path.exists(os.path.join(working_dir, "local_config.py")):
|
else:
|
||||||
config_path = os.path.join(working_dir, "local_config.py")
|
working_dir = os.path.abspath(os.getcwd())
|
||||||
elif not config_path and (
|
|
||||||
not os.path.exists(working_dir) or not os.path.exists(os.path.join(working_dir, "local_config.py"))
|
# unilabos_data 子目录自动检测
|
||||||
):
|
if os.path.basename(working_dir) != "unilabos_data":
|
||||||
print_status(f"未指定config路径,可通过 --config 传入 local_config.py 文件路径", "info")
|
unilabos_data_sub = os.path.join(working_dir, "unilabos_data")
|
||||||
print_status(f"您是否为第一次使用?并将当前路径 {working_dir} 作为工作目录? (Y/n)", "info")
|
if os.path.isdir(unilabos_data_sub):
|
||||||
if input() != "n":
|
working_dir = unilabos_data_sub
|
||||||
os.makedirs(working_dir, exist_ok=True)
|
elif not raw_working_dir and not (config_path and os.path.exists(config_path)):
|
||||||
config_path = os.path.join(working_dir, "local_config.py")
|
# 未显式指定路径,默认使用 cwd/unilabos_data
|
||||||
shutil.copy(
|
working_dir = os.path.abspath(os.path.join(os.getcwd(), "unilabos_data"))
|
||||||
os.path.join(os.path.dirname(os.path.dirname(__file__)), "config", "example_config.py"), config_path
|
|
||||||
)
|
# === 解析 config_path ===
|
||||||
print_status(f"已创建 local_config.py 路径: {config_path}", "info")
|
if config_path and not os.path.exists(config_path):
|
||||||
|
# config_path 传入但不存在,尝试在 working_dir 中查找
|
||||||
|
candidate = os.path.join(working_dir, "local_config.py")
|
||||||
|
if os.path.exists(candidate):
|
||||||
|
config_path = candidate
|
||||||
|
print_status(f"在工作目录中发现配置文件: {config_path}", "info")
|
||||||
else:
|
else:
|
||||||
|
print_status(
|
||||||
|
f"配置文件 {config_path} 不存在,工作目录 {working_dir} 中也未找到 local_config.py,"
|
||||||
|
f"请通过 --config 传入 local_config.py 文件路径",
|
||||||
|
"error",
|
||||||
|
)
|
||||||
os._exit(1)
|
os._exit(1)
|
||||||
# 加载配置文件
|
elif not config_path:
|
||||||
|
# 规则3: 未传入 config_path,尝试 working_dir/local_config.py
|
||||||
|
candidate = os.path.join(working_dir, "local_config.py")
|
||||||
|
if os.path.exists(candidate):
|
||||||
|
config_path = candidate
|
||||||
|
print_status(f"发现本地配置文件: {config_path}", "info")
|
||||||
|
else:
|
||||||
|
print_status(f"未指定config路径,可通过 --config 传入 local_config.py 文件路径", "info")
|
||||||
|
print_status(f"您是否为第一次使用?并将当前路径 {working_dir} 作为工作目录? (Y/n)", "info")
|
||||||
|
if check_mode or input() != "n":
|
||||||
|
os.makedirs(working_dir, exist_ok=True)
|
||||||
|
config_path = os.path.join(working_dir, "local_config.py")
|
||||||
|
shutil.copy(
|
||||||
|
os.path.join(os.path.dirname(os.path.dirname(__file__)), "config", "example_config.py"),
|
||||||
|
config_path,
|
||||||
|
)
|
||||||
|
print_status(f"已创建 local_config.py 路径: {config_path}", "info")
|
||||||
|
else:
|
||||||
|
os._exit(1)
|
||||||
|
|
||||||
|
# 加载配置文件 (check_mode 跳过)
|
||||||
print_status(f"当前工作目录为 {working_dir}", "info")
|
print_status(f"当前工作目录为 {working_dir}", "info")
|
||||||
load_config_from_file(config_path)
|
if not check_mode:
|
||||||
|
load_config_from_file(config_path)
|
||||||
|
|
||||||
# 根据配置重新设置日志级别
|
# 根据配置重新设置日志级别
|
||||||
from unilabos.utils.log import configure_logger, logger
|
from unilabos.utils.log import configure_logger, logger
|
||||||
|
|
||||||
if hasattr(BasicConfig, "log_level"):
|
if hasattr(BasicConfig, "log_level"):
|
||||||
logger.info(f"Log level set to '{BasicConfig.log_level}' from config file.")
|
logger.info(f"Log level set to '{BasicConfig.log_level}' from config file.")
|
||||||
configure_logger(loglevel=BasicConfig.log_level, working_dir=working_dir)
|
file_path = configure_logger(loglevel=BasicConfig.log_level, working_dir=working_dir)
|
||||||
|
if file_path is not None:
|
||||||
|
logger.info(f"[LOG_FILE] {file_path}")
|
||||||
|
|
||||||
if args.addr != parser.get_default("addr"):
|
if args.addr != parser.get_default("addr"):
|
||||||
if args.addr == "test":
|
if args.addr == "test":
|
||||||
@@ -314,17 +355,15 @@ def main():
|
|||||||
BasicConfig.slave_no_host = args_dict.get("slave_no_host", False)
|
BasicConfig.slave_no_host = args_dict.get("slave_no_host", False)
|
||||||
BasicConfig.upload_registry = args_dict.get("upload_registry", False)
|
BasicConfig.upload_registry = args_dict.get("upload_registry", False)
|
||||||
BasicConfig.no_update_feedback = args_dict.get("no_update_feedback", False)
|
BasicConfig.no_update_feedback = args_dict.get("no_update_feedback", False)
|
||||||
|
BasicConfig.test_mode = args_dict.get("test_mode", False)
|
||||||
|
if BasicConfig.test_mode:
|
||||||
|
print_status("启用测试模式:所有动作将模拟执行,不调用真实硬件", "warning")
|
||||||
BasicConfig.communication_protocol = "websocket"
|
BasicConfig.communication_protocol = "websocket"
|
||||||
machine_name = os.popen("hostname").read().strip()
|
machine_name = platform.node()
|
||||||
machine_name = "".join([c if c.isalnum() or c == "_" else "_" for c in machine_name])
|
machine_name = "".join([c if c.isalnum() or c == "_" else "_" for c in machine_name])
|
||||||
BasicConfig.machine_name = machine_name
|
BasicConfig.machine_name = machine_name
|
||||||
BasicConfig.vis_2d_enable = args_dict["2d_vis"]
|
BasicConfig.vis_2d_enable = args_dict["2d_vis"]
|
||||||
|
|
||||||
# Check mode 处理
|
|
||||||
check_mode = args_dict.get("check_mode", False)
|
|
||||||
BasicConfig.check_mode = check_mode
|
BasicConfig.check_mode = check_mode
|
||||||
if check_mode:
|
|
||||||
print_status("Check mode 启用,将进行 complete_registry 检查", "info")
|
|
||||||
|
|
||||||
from unilabos.resources.graphio import (
|
from unilabos.resources.graphio import (
|
||||||
read_node_link_json,
|
read_node_link_json,
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ class JobAddReq(BaseModel):
|
|||||||
action_type: str = Field(
|
action_type: str = Field(
|
||||||
examples=["unilabos_msgs.action._str_single_input.StrSingleInput"], description="action type", default=""
|
examples=["unilabos_msgs.action._str_single_input.StrSingleInput"], description="action type", default=""
|
||||||
)
|
)
|
||||||
|
sample_material: dict = Field(examples=[{"string": "string"}], description="sample uuid to material uuid")
|
||||||
action_args: dict = Field(examples=[{"string": "string"}], description="action arguments", default_factory=dict)
|
action_args: dict = Field(examples=[{"string": "string"}], description="action arguments", default_factory=dict)
|
||||||
task_id: str = Field(examples=["task_id"], description="task uuid (auto-generated if empty)", default="")
|
task_id: str = Field(examples=["task_id"], description="task uuid (auto-generated if empty)", default="")
|
||||||
job_id: str = Field(examples=["job_id"], description="goal uuid (auto-generated if empty)", default="")
|
job_id: str = Field(examples=["job_id"], description="goal uuid (auto-generated if empty)", default="")
|
||||||
|
|||||||
@@ -38,9 +38,9 @@ def register_devices_and_resources(lab_registry, gather_only=False) -> Optional[
|
|||||||
response = http_client.resource_registry({"resources": list(devices_to_register.values())})
|
response = http_client.resource_registry({"resources": list(devices_to_register.values())})
|
||||||
cost_time = time.time() - start_time
|
cost_time = time.time() - start_time
|
||||||
if response.status_code in [200, 201]:
|
if response.status_code in [200, 201]:
|
||||||
logger.info(f"[UniLab Register] 成功注册 {len(devices_to_register)} 个设备 {cost_time}ms")
|
logger.info(f"[UniLab Register] 成功注册 {len(devices_to_register)} 个设备 {cost_time}s")
|
||||||
else:
|
else:
|
||||||
logger.error(f"[UniLab Register] 设备注册失败: {response.status_code}, {response.text} {cost_time}ms")
|
logger.error(f"[UniLab Register] 设备注册失败: {response.status_code}, {response.text} {cost_time}s")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"[UniLab Register] 设备注册异常: {e}")
|
logger.error(f"[UniLab Register] 设备注册异常: {e}")
|
||||||
|
|
||||||
@@ -51,9 +51,9 @@ def register_devices_and_resources(lab_registry, gather_only=False) -> Optional[
|
|||||||
response = http_client.resource_registry({"resources": list(resources_to_register.values())})
|
response = http_client.resource_registry({"resources": list(resources_to_register.values())})
|
||||||
cost_time = time.time() - start_time
|
cost_time = time.time() - start_time
|
||||||
if response.status_code in [200, 201]:
|
if response.status_code in [200, 201]:
|
||||||
logger.info(f"[UniLab Register] 成功注册 {len(resources_to_register)} 个资源 {cost_time}ms")
|
logger.info(f"[UniLab Register] 成功注册 {len(resources_to_register)} 个资源 {cost_time}s")
|
||||||
else:
|
else:
|
||||||
logger.error(f"[UniLab Register] 资源注册失败: {response.status_code}, {response.text} {cost_time}ms")
|
logger.error(f"[UniLab Register] 资源注册失败: {response.status_code}, {response.text} {cost_time}s")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"[UniLab Register] 资源注册异常: {e}")
|
logger.error(f"[UniLab Register] 资源注册异常: {e}")
|
||||||
|
|
||||||
|
|||||||
@@ -4,8 +4,40 @@ UniLabOS 应用工具函数
|
|||||||
提供清理、重启等工具函数
|
提供清理、重启等工具函数
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import gc
|
import glob
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def patch_rclpy_dll_windows():
|
||||||
|
"""在 Windows + conda 环境下为 rclpy 打 DLL 加载补丁"""
|
||||||
|
if sys.platform != "win32" or not os.environ.get("CONDA_PREFIX"):
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
import rclpy
|
||||||
|
|
||||||
|
return
|
||||||
|
except ImportError as e:
|
||||||
|
if not str(e).startswith("DLL load failed"):
|
||||||
|
return
|
||||||
|
cp = os.environ["CONDA_PREFIX"]
|
||||||
|
impl = os.path.join(cp, "Lib", "site-packages", "rclpy", "impl", "implementation_singleton.py")
|
||||||
|
pyd = glob.glob(os.path.join(cp, "Lib", "site-packages", "rclpy", "_rclpy_pybind11*.pyd"))
|
||||||
|
if not os.path.exists(impl) or not pyd:
|
||||||
|
return
|
||||||
|
with open(impl, "r", encoding="utf-8") as f:
|
||||||
|
content = f.read()
|
||||||
|
lib_bin = os.path.join(cp, "Library", "bin").replace("\\", "/")
|
||||||
|
patch = f'# UniLabOS DLL Patch\nimport os,ctypes\nos.add_dll_directory("{lib_bin}") if hasattr(os,"add_dll_directory") else None\ntry: ctypes.CDLL("{pyd[0].replace(chr(92),"/")}")\nexcept: pass\n# End Patch\n'
|
||||||
|
shutil.copy2(impl, impl + ".bak")
|
||||||
|
with open(impl, "w", encoding="utf-8") as f:
|
||||||
|
f.write(patch + content)
|
||||||
|
|
||||||
|
|
||||||
|
patch_rclpy_dll_windows()
|
||||||
|
|
||||||
|
import gc
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ HTTP客户端模块
|
|||||||
|
|
||||||
提供与远程服务器通信的客户端功能,只有host需要用
|
提供与远程服务器通信的客户端功能,只有host需要用
|
||||||
"""
|
"""
|
||||||
|
import gzip
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
from typing import List, Dict, Any, Optional
|
from typing import List, Dict, Any, Optional
|
||||||
@@ -290,10 +290,17 @@ class HTTPClient:
|
|||||||
Returns:
|
Returns:
|
||||||
Response: API响应对象
|
Response: API响应对象
|
||||||
"""
|
"""
|
||||||
|
compressed_body = gzip.compress(
|
||||||
|
json.dumps(registry_data, ensure_ascii=False, default=str).encode("utf-8")
|
||||||
|
)
|
||||||
response = requests.post(
|
response = requests.post(
|
||||||
f"{self.remote_addr}/lab/resource",
|
f"{self.remote_addr}/lab/resource",
|
||||||
json=registry_data,
|
data=compressed_body,
|
||||||
headers={"Authorization": f"Lab {self.auth}"},
|
headers={
|
||||||
|
"Authorization": f"Lab {self.auth}",
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Content-Encoding": "gzip",
|
||||||
|
},
|
||||||
timeout=30,
|
timeout=30,
|
||||||
)
|
)
|
||||||
if response.status_code not in [200, 201]:
|
if response.status_code not in [200, 201]:
|
||||||
@@ -343,9 +350,10 @@ class HTTPClient:
|
|||||||
edges: List[Dict[str, Any]],
|
edges: List[Dict[str, Any]],
|
||||||
tags: Optional[List[str]] = None,
|
tags: Optional[List[str]] = None,
|
||||||
published: bool = False,
|
published: bool = False,
|
||||||
|
description: str = "",
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
导入工作流到服务器
|
导入工作流到服务器,如果 published 为 True,则额外发起发布请求
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
name: 工作流名称(顶层)
|
name: 工作流名称(顶层)
|
||||||
@@ -355,13 +363,12 @@ class HTTPClient:
|
|||||||
edges: 工作流边列表
|
edges: 工作流边列表
|
||||||
tags: 工作流标签列表,默认为空列表
|
tags: 工作流标签列表,默认为空列表
|
||||||
published: 是否发布工作流,默认为False
|
published: 是否发布工作流,默认为False
|
||||||
|
description: 工作流描述,发布时使用
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict: API响应数据,包含 code 和 data (uuid, name)
|
Dict: API响应数据,包含 code 和 data (uuid, name)
|
||||||
"""
|
"""
|
||||||
# target_lab_uuid 暂时使用默认值,后续由后端根据 ak/sk 获取
|
|
||||||
payload = {
|
payload = {
|
||||||
"target_lab_uuid": "28c38bb0-63f6-4352-b0d8-b5b8eb1766d5",
|
|
||||||
"name": name,
|
"name": name,
|
||||||
"data": {
|
"data": {
|
||||||
"workflow_uuid": workflow_uuid,
|
"workflow_uuid": workflow_uuid,
|
||||||
@@ -369,7 +376,6 @@ class HTTPClient:
|
|||||||
"nodes": nodes,
|
"nodes": nodes,
|
||||||
"edges": edges,
|
"edges": edges,
|
||||||
"tags": tags if tags is not None else [],
|
"tags": tags if tags is not None else [],
|
||||||
"published": published,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
# 保存请求到文件
|
# 保存请求到文件
|
||||||
@@ -390,11 +396,51 @@ class HTTPClient:
|
|||||||
res = response.json()
|
res = response.json()
|
||||||
if "code" in res and res["code"] != 0:
|
if "code" in res and res["code"] != 0:
|
||||||
logger.error(f"导入工作流失败: {response.text}")
|
logger.error(f"导入工作流失败: {response.text}")
|
||||||
|
return res
|
||||||
|
# 导入成功后,如果需要发布则额外发起发布请求
|
||||||
|
if published:
|
||||||
|
imported_uuid = res.get("data", {}).get("uuid", workflow_uuid)
|
||||||
|
publish_res = self.workflow_publish(imported_uuid, description)
|
||||||
|
res["publish_result"] = publish_res
|
||||||
return res
|
return res
|
||||||
else:
|
else:
|
||||||
logger.error(f"导入工作流失败: {response.status_code}, {response.text}")
|
logger.error(f"导入工作流失败: {response.status_code}, {response.text}")
|
||||||
return {"code": response.status_code, "message": response.text}
|
return {"code": response.status_code, "message": response.text}
|
||||||
|
|
||||||
|
def workflow_publish(self, workflow_uuid: str, description: str = "") -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
发布工作流
|
||||||
|
|
||||||
|
Args:
|
||||||
|
workflow_uuid: 工作流UUID
|
||||||
|
description: 工作流描述
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Dict: API响应数据
|
||||||
|
"""
|
||||||
|
payload = {
|
||||||
|
"uuid": workflow_uuid,
|
||||||
|
"description": description,
|
||||||
|
"published": True,
|
||||||
|
}
|
||||||
|
logger.info(f"正在发布工作流: {workflow_uuid}")
|
||||||
|
response = requests.patch(
|
||||||
|
f"{self.remote_addr}/lab/workflow/owner",
|
||||||
|
json=payload,
|
||||||
|
headers={"Authorization": f"Lab {self.auth}"},
|
||||||
|
timeout=60,
|
||||||
|
)
|
||||||
|
if response.status_code == 200:
|
||||||
|
res = response.json()
|
||||||
|
if "code" in res and res["code"] != 0:
|
||||||
|
logger.error(f"发布工作流失败: {response.text}")
|
||||||
|
else:
|
||||||
|
logger.info(f"工作流发布成功: {workflow_uuid}")
|
||||||
|
return res
|
||||||
|
else:
|
||||||
|
logger.error(f"发布工作流失败: {response.status_code}, {response.text}")
|
||||||
|
return {"code": response.status_code, "message": response.text}
|
||||||
|
|
||||||
|
|
||||||
# 创建默认客户端实例
|
# 创建默认客户端实例
|
||||||
http_client = HTTPClient()
|
http_client = HTTPClient()
|
||||||
|
|||||||
@@ -327,6 +327,7 @@ def job_add(req: JobAddReq) -> JobData:
|
|||||||
queue_item,
|
queue_item,
|
||||||
action_type=action_type,
|
action_type=action_type,
|
||||||
action_kwargs=action_args,
|
action_kwargs=action_args,
|
||||||
|
sample_material=req.sample_material,
|
||||||
server_info=server_info,
|
server_info=server_info,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -76,6 +76,7 @@ class JobInfo:
|
|||||||
start_time: float
|
start_time: float
|
||||||
last_update_time: float = field(default_factory=time.time)
|
last_update_time: float = field(default_factory=time.time)
|
||||||
ready_timeout: Optional[float] = None # READY状态的超时时间
|
ready_timeout: Optional[float] = None # READY状态的超时时间
|
||||||
|
always_free: bool = False # 是否为永久闲置动作(不受排队限制)
|
||||||
|
|
||||||
def update_timestamp(self):
|
def update_timestamp(self):
|
||||||
"""更新最后更新时间"""
|
"""更新最后更新时间"""
|
||||||
@@ -127,6 +128,15 @@ class DeviceActionManager:
|
|||||||
# 总是将job添加到all_jobs中
|
# 总是将job添加到all_jobs中
|
||||||
self.all_jobs[job_info.job_id] = job_info
|
self.all_jobs[job_info.job_id] = job_info
|
||||||
|
|
||||||
|
# always_free的动作不受排队限制,直接设为READY
|
||||||
|
if job_info.always_free:
|
||||||
|
job_info.status = JobStatus.READY
|
||||||
|
job_info.update_timestamp()
|
||||||
|
job_info.set_ready_timeout(10)
|
||||||
|
job_log = format_job_log(job_info.job_id, job_info.task_id, job_info.device_id, job_info.action_name)
|
||||||
|
logger.trace(f"[DeviceActionManager] Job {job_log} always_free, start immediately")
|
||||||
|
return True
|
||||||
|
|
||||||
# 检查是否有正在执行或准备执行的任务
|
# 检查是否有正在执行或准备执行的任务
|
||||||
if device_key in self.active_jobs:
|
if device_key in self.active_jobs:
|
||||||
# 有正在执行或准备执行的任务,加入队列
|
# 有正在执行或准备执行的任务,加入队列
|
||||||
@@ -176,11 +186,15 @@ class DeviceActionManager:
|
|||||||
logger.error(f"[DeviceActionManager] Job {job_log} is not in READY status, current: {job_info.status}")
|
logger.error(f"[DeviceActionManager] Job {job_log} is not in READY status, current: {job_info.status}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# 检查设备上是否是这个job
|
# always_free的job不需要检查active_jobs
|
||||||
if device_key not in self.active_jobs or self.active_jobs[device_key].job_id != job_id:
|
if not job_info.always_free:
|
||||||
job_log = format_job_log(job_info.job_id, job_info.task_id, job_info.device_id, job_info.action_name)
|
# 检查设备上是否是这个job
|
||||||
logger.error(f"[DeviceActionManager] Job {job_log} is not the active job for {device_key}")
|
if device_key not in self.active_jobs or self.active_jobs[device_key].job_id != job_id:
|
||||||
return False
|
job_log = format_job_log(
|
||||||
|
job_info.job_id, job_info.task_id, job_info.device_id, job_info.action_name
|
||||||
|
)
|
||||||
|
logger.error(f"[DeviceActionManager] Job {job_log} is not the active job for {device_key}")
|
||||||
|
return False
|
||||||
|
|
||||||
# 开始执行任务,将状态从READY转换为STARTED
|
# 开始执行任务,将状态从READY转换为STARTED
|
||||||
job_info.status = JobStatus.STARTED
|
job_info.status = JobStatus.STARTED
|
||||||
@@ -203,6 +217,13 @@ class DeviceActionManager:
|
|||||||
job_info = self.all_jobs[job_id]
|
job_info = self.all_jobs[job_id]
|
||||||
device_key = job_info.device_action_key
|
device_key = job_info.device_action_key
|
||||||
|
|
||||||
|
# always_free的job直接清理,不影响队列
|
||||||
|
if job_info.always_free:
|
||||||
|
job_info.status = JobStatus.ENDED
|
||||||
|
job_info.update_timestamp()
|
||||||
|
del self.all_jobs[job_id]
|
||||||
|
return None
|
||||||
|
|
||||||
# 移除活跃任务
|
# 移除活跃任务
|
||||||
if device_key in self.active_jobs and self.active_jobs[device_key].job_id == job_id:
|
if device_key in self.active_jobs and self.active_jobs[device_key].job_id == job_id:
|
||||||
del self.active_jobs[device_key]
|
del self.active_jobs[device_key]
|
||||||
@@ -234,9 +255,14 @@ class DeviceActionManager:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
def get_active_jobs(self) -> List[JobInfo]:
|
def get_active_jobs(self) -> List[JobInfo]:
|
||||||
"""获取所有正在执行的任务"""
|
"""获取所有正在执行的任务(含active_jobs和always_free的STARTED job)"""
|
||||||
with self.lock:
|
with self.lock:
|
||||||
return list(self.active_jobs.values())
|
jobs = list(self.active_jobs.values())
|
||||||
|
# 补充 always_free 的 STARTED job(它们不在 active_jobs 中)
|
||||||
|
for job in self.all_jobs.values():
|
||||||
|
if job.always_free and job.status == JobStatus.STARTED and job not in jobs:
|
||||||
|
jobs.append(job)
|
||||||
|
return jobs
|
||||||
|
|
||||||
def get_queued_jobs(self) -> List[JobInfo]:
|
def get_queued_jobs(self) -> List[JobInfo]:
|
||||||
"""获取所有排队中的任务"""
|
"""获取所有排队中的任务"""
|
||||||
@@ -261,6 +287,14 @@ class DeviceActionManager:
|
|||||||
job_info = self.all_jobs[job_id]
|
job_info = self.all_jobs[job_id]
|
||||||
device_key = job_info.device_action_key
|
device_key = job_info.device_action_key
|
||||||
|
|
||||||
|
# always_free的job直接清理
|
||||||
|
if job_info.always_free:
|
||||||
|
job_info.status = JobStatus.ENDED
|
||||||
|
del self.all_jobs[job_id]
|
||||||
|
job_log = format_job_log(job_info.job_id, job_info.task_id, job_info.device_id, job_info.action_name)
|
||||||
|
logger.trace(f"[DeviceActionManager] Always-free job {job_log} cancelled")
|
||||||
|
return True
|
||||||
|
|
||||||
# 如果是正在执行的任务
|
# 如果是正在执行的任务
|
||||||
if device_key in self.active_jobs and self.active_jobs[device_key].job_id == job_id:
|
if device_key in self.active_jobs and self.active_jobs[device_key].job_id == job_id:
|
||||||
# 清理active job状态
|
# 清理active job状态
|
||||||
@@ -334,13 +368,18 @@ class DeviceActionManager:
|
|||||||
timeout_jobs = []
|
timeout_jobs = []
|
||||||
|
|
||||||
with self.lock:
|
with self.lock:
|
||||||
# 统计READY状态的任务数量
|
# 收集所有需要检查的 READY 任务(active_jobs + always_free READY jobs)
|
||||||
ready_jobs_count = sum(1 for job in self.active_jobs.values() if job.status == JobStatus.READY)
|
ready_candidates = list(self.active_jobs.values())
|
||||||
|
for job in self.all_jobs.values():
|
||||||
|
if job.always_free and job.status == JobStatus.READY and job not in ready_candidates:
|
||||||
|
ready_candidates.append(job)
|
||||||
|
|
||||||
|
ready_jobs_count = sum(1 for job in ready_candidates if job.status == JobStatus.READY)
|
||||||
if ready_jobs_count > 0:
|
if ready_jobs_count > 0:
|
||||||
logger.trace(f"[DeviceActionManager] Checking {ready_jobs_count} READY jobs for timeout") # type: ignore # noqa: E501
|
logger.trace(f"[DeviceActionManager] Checking {ready_jobs_count} READY jobs for timeout") # type: ignore # noqa: E501
|
||||||
|
|
||||||
# 找到所有超时的READY任务(只检测,不处理)
|
# 找到所有超时的READY任务(只检测,不处理)
|
||||||
for job_info in self.active_jobs.values():
|
for job_info in ready_candidates:
|
||||||
if job_info.is_ready_timeout():
|
if job_info.is_ready_timeout():
|
||||||
timeout_jobs.append(job_info)
|
timeout_jobs.append(job_info)
|
||||||
job_log = format_job_log(
|
job_log = format_job_log(
|
||||||
@@ -427,6 +466,7 @@ class MessageProcessor:
|
|||||||
async with websockets.connect(
|
async with websockets.connect(
|
||||||
self.websocket_url,
|
self.websocket_url,
|
||||||
ssl=ssl_context,
|
ssl=ssl_context,
|
||||||
|
open_timeout=20,
|
||||||
ping_interval=WSConfig.ping_interval,
|
ping_interval=WSConfig.ping_interval,
|
||||||
ping_timeout=10,
|
ping_timeout=10,
|
||||||
additional_headers={
|
additional_headers={
|
||||||
@@ -458,6 +498,18 @@ class MessageProcessor:
|
|||||||
except websockets.exceptions.ConnectionClosed:
|
except websockets.exceptions.ConnectionClosed:
|
||||||
logger.warning("[MessageProcessor] Connection closed")
|
logger.warning("[MessageProcessor] Connection closed")
|
||||||
self.connected = False
|
self.connected = False
|
||||||
|
except TimeoutError:
|
||||||
|
logger.warning(
|
||||||
|
f"[MessageProcessor] Connection timeout (attempt {self.reconnect_count + 1}), "
|
||||||
|
f"server may be temporarily unavailable"
|
||||||
|
)
|
||||||
|
self.connected = False
|
||||||
|
except websockets.exceptions.InvalidStatus as e:
|
||||||
|
logger.warning(
|
||||||
|
f"[MessageProcessor] Server returned unexpected HTTP status {e.response.status_code}, "
|
||||||
|
f"WebSocket endpoint may not be ready yet"
|
||||||
|
)
|
||||||
|
self.connected = False
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"[MessageProcessor] Connection error: {str(e)}")
|
logger.error(f"[MessageProcessor] Connection error: {str(e)}")
|
||||||
logger.error(traceback.format_exc())
|
logger.error(traceback.format_exc())
|
||||||
@@ -466,18 +518,19 @@ class MessageProcessor:
|
|||||||
self.websocket = None
|
self.websocket = None
|
||||||
|
|
||||||
# 重连逻辑
|
# 重连逻辑
|
||||||
if self.is_running and self.reconnect_count < WSConfig.max_reconnect_attempts:
|
if not self.is_running:
|
||||||
|
break
|
||||||
|
if self.reconnect_count < WSConfig.max_reconnect_attempts:
|
||||||
self.reconnect_count += 1
|
self.reconnect_count += 1
|
||||||
|
backoff = min(WSConfig.reconnect_interval * (2 ** (self.reconnect_count - 1)), 60)
|
||||||
logger.info(
|
logger.info(
|
||||||
f"[MessageProcessor] Reconnecting in {WSConfig.reconnect_interval}s "
|
f"[MessageProcessor] Reconnecting in {backoff}s "
|
||||||
f"(attempt {self.reconnect_count}/{WSConfig.max_reconnect_attempts})"
|
f"(attempt {self.reconnect_count}/{WSConfig.max_reconnect_attempts})"
|
||||||
)
|
)
|
||||||
await asyncio.sleep(WSConfig.reconnect_interval)
|
await asyncio.sleep(backoff)
|
||||||
elif self.reconnect_count >= WSConfig.max_reconnect_attempts:
|
else:
|
||||||
logger.error("[MessageProcessor] Max reconnection attempts reached")
|
logger.error("[MessageProcessor] Max reconnection attempts reached")
|
||||||
break
|
break
|
||||||
else:
|
|
||||||
self.reconnect_count -= 1
|
|
||||||
|
|
||||||
async def _message_handler(self):
|
async def _message_handler(self):
|
||||||
"""处理接收到的消息"""
|
"""处理接收到的消息"""
|
||||||
@@ -545,7 +598,7 @@ class MessageProcessor:
|
|||||||
try:
|
try:
|
||||||
message_str = json.dumps(msg, ensure_ascii=False)
|
message_str = json.dumps(msg, ensure_ascii=False)
|
||||||
await self.websocket.send(message_str)
|
await self.websocket.send(message_str)
|
||||||
logger.trace(f"[MessageProcessor] Message sent: {msg.get('action', 'unknown')}") # type: ignore # noqa: E501
|
# logger.trace(f"[MessageProcessor] Message sent: {msg.get('action', 'unknown')}") # type: ignore # noqa: E501
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"[MessageProcessor] Failed to send message: {str(e)}")
|
logger.error(f"[MessageProcessor] Failed to send message: {str(e)}")
|
||||||
logger.error(traceback.format_exc())
|
logger.error(traceback.format_exc())
|
||||||
@@ -608,6 +661,24 @@ class MessageProcessor:
|
|||||||
if host_node:
|
if host_node:
|
||||||
host_node.handle_pong_response(pong_data)
|
host_node.handle_pong_response(pong_data)
|
||||||
|
|
||||||
|
def _check_action_always_free(self, device_id: str, action_name: str) -> bool:
|
||||||
|
"""检查该action是否标记为always_free,通过HostNode统一的_action_value_mappings查找"""
|
||||||
|
try:
|
||||||
|
host_node = HostNode.get_instance(0)
|
||||||
|
if not host_node:
|
||||||
|
return False
|
||||||
|
# noinspection PyProtectedMember
|
||||||
|
action_mappings = host_node._action_value_mappings.get(device_id)
|
||||||
|
if not action_mappings:
|
||||||
|
return False
|
||||||
|
# 尝试直接匹配或 auto- 前缀匹配
|
||||||
|
for key in [action_name, f"auto-{action_name}"]:
|
||||||
|
if key in action_mappings:
|
||||||
|
return action_mappings[key].get("always_free", False)
|
||||||
|
return False
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
||||||
async def _handle_query_action_state(self, data: Dict[str, Any]):
|
async def _handle_query_action_state(self, data: Dict[str, Any]):
|
||||||
"""处理query_action_state消息"""
|
"""处理query_action_state消息"""
|
||||||
device_id = data.get("device_id", "")
|
device_id = data.get("device_id", "")
|
||||||
@@ -622,6 +693,9 @@ class MessageProcessor:
|
|||||||
|
|
||||||
device_action_key = f"/devices/{device_id}/{action_name}"
|
device_action_key = f"/devices/{device_id}/{action_name}"
|
||||||
|
|
||||||
|
# 检查action是否为always_free
|
||||||
|
action_always_free = self._check_action_always_free(device_id, action_name)
|
||||||
|
|
||||||
# 创建任务信息
|
# 创建任务信息
|
||||||
job_info = JobInfo(
|
job_info = JobInfo(
|
||||||
job_id=job_id,
|
job_id=job_id,
|
||||||
@@ -631,6 +705,7 @@ class MessageProcessor:
|
|||||||
device_action_key=device_action_key,
|
device_action_key=device_action_key,
|
||||||
status=JobStatus.QUEUE,
|
status=JobStatus.QUEUE,
|
||||||
start_time=time.time(),
|
start_time=time.time(),
|
||||||
|
always_free=action_always_free,
|
||||||
)
|
)
|
||||||
|
|
||||||
# 添加到设备管理器
|
# 添加到设备管理器
|
||||||
@@ -657,6 +732,8 @@ class MessageProcessor:
|
|||||||
async def _handle_job_start(self, data: Dict[str, Any]):
|
async def _handle_job_start(self, data: Dict[str, Any]):
|
||||||
"""处理job_start消息"""
|
"""处理job_start消息"""
|
||||||
try:
|
try:
|
||||||
|
if not data.get("sample_material"):
|
||||||
|
data["sample_material"] = {}
|
||||||
req = JobAddReq(**data)
|
req = JobAddReq(**data)
|
||||||
|
|
||||||
job_log = format_job_log(req.job_id, req.task_id, req.device_id, req.action)
|
job_log = format_job_log(req.job_id, req.task_id, req.device_id, req.action)
|
||||||
@@ -688,6 +765,7 @@ class MessageProcessor:
|
|||||||
queue_item,
|
queue_item,
|
||||||
action_type=req.action_type,
|
action_type=req.action_type,
|
||||||
action_kwargs=req.action_args,
|
action_kwargs=req.action_args,
|
||||||
|
sample_material=req.sample_material,
|
||||||
server_info=req.server_info,
|
server_info=req.server_info,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -1120,6 +1198,11 @@ class QueueProcessor:
|
|||||||
logger.debug(f"[QueueProcessor] Sending busy status for {len(queued_jobs)} queued jobs")
|
logger.debug(f"[QueueProcessor] Sending busy status for {len(queued_jobs)} queued jobs")
|
||||||
|
|
||||||
for job_info in queued_jobs:
|
for job_info in queued_jobs:
|
||||||
|
# 快照可能已过期:在遍历过程中 end_job() 可能已将此 job 移至 READY,
|
||||||
|
# 此时不应再发送 busy/need_more,否则会覆盖已发出的 free=True 通知
|
||||||
|
if job_info.status != JobStatus.QUEUE:
|
||||||
|
continue
|
||||||
|
|
||||||
message = {
|
message = {
|
||||||
"action": "report_action_state",
|
"action": "report_action_state",
|
||||||
"data": {
|
"data": {
|
||||||
@@ -1301,7 +1384,7 @@ class WebSocketClient(BaseCommunicationClient):
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
self.message_processor.send_message(message)
|
self.message_processor.send_message(message)
|
||||||
logger.trace(f"[WebSocketClient] Device status published: {device_id}.{property_name}")
|
# logger.trace(f"[WebSocketClient] Device status published: {device_id}.{property_name}")
|
||||||
|
|
||||||
def publish_job_status(
|
def publish_job_status(
|
||||||
self, feedback_data: dict, item: QueueItem, status: str, return_info: Optional[dict] = None
|
self, feedback_data: dict, item: QueueItem, status: str, return_info: Optional[dict] = None
|
||||||
|
|||||||
@@ -95,8 +95,29 @@ def get_vessel_liquid_volume(G: nx.DiGraph, vessel: str) -> float:
|
|||||||
return total_volume
|
return total_volume
|
||||||
|
|
||||||
|
|
||||||
def is_integrated_pump(node_name):
|
def is_integrated_pump(node_class: str, node_name: str = "") -> bool:
|
||||||
return "pump" in node_name and "valve" in node_name
|
"""
|
||||||
|
判断是否为泵阀一体设备
|
||||||
|
"""
|
||||||
|
class_lower = (node_class or "").lower()
|
||||||
|
name_lower = (node_name or "").lower()
|
||||||
|
|
||||||
|
if "pump" not in class_lower and "pump" not in name_lower:
|
||||||
|
return False
|
||||||
|
|
||||||
|
integrated_markers = [
|
||||||
|
"valve",
|
||||||
|
"pump_valve",
|
||||||
|
"pumpvalve",
|
||||||
|
"integrated",
|
||||||
|
"transfer_pump",
|
||||||
|
]
|
||||||
|
|
||||||
|
for marker in integrated_markers:
|
||||||
|
if marker in class_lower or marker in name_lower:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def find_connected_pump(G, valve_node):
|
def find_connected_pump(G, valve_node):
|
||||||
@@ -186,7 +207,9 @@ def build_pump_valve_maps(G, pump_backbone):
|
|||||||
debug_print(f"🔧 过滤后的骨架: {filtered_backbone}")
|
debug_print(f"🔧 过滤后的骨架: {filtered_backbone}")
|
||||||
|
|
||||||
for node in filtered_backbone:
|
for node in filtered_backbone:
|
||||||
if is_integrated_pump(G.nodes[node]["class"]):
|
node_data = G.nodes.get(node, {})
|
||||||
|
node_class = node_data.get("class", "") or ""
|
||||||
|
if is_integrated_pump(node_class, node):
|
||||||
pumps_from_node[node] = node
|
pumps_from_node[node] = node
|
||||||
valve_from_node[node] = node
|
valve_from_node[node] = node
|
||||||
debug_print(f" - 集成泵-阀: {node}")
|
debug_print(f" - 集成泵-阀: {node}")
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ class BasicConfig:
|
|||||||
disable_browser = False # 禁止浏览器自动打开
|
disable_browser = False # 禁止浏览器自动打开
|
||||||
port = 8002 # 本地HTTP服务
|
port = 8002 # 本地HTTP服务
|
||||||
check_mode = False # CI 检查模式,用于验证 registry 导入和文件一致性
|
check_mode = False # CI 检查模式,用于验证 registry 导入和文件一致性
|
||||||
|
test_mode = False # 测试模式,所有动作不实际执行,返回模拟结果
|
||||||
# 'TRACE', 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'
|
# 'TRACE', 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'
|
||||||
log_level: Literal["TRACE", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "DEBUG"
|
log_level: Literal["TRACE", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "DEBUG"
|
||||||
|
|
||||||
@@ -145,5 +146,5 @@ def load_config(config_path=None):
|
|||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
exit(1)
|
exit(1)
|
||||||
else:
|
else:
|
||||||
config_path = os.path.join(os.path.dirname(__file__), "local_config.py")
|
config_path = os.path.join(os.path.dirname(__file__), "example_config.py")
|
||||||
load_config(config_path)
|
load_config(config_path)
|
||||||
|
|||||||
0
unilabos/devices/Qone_nmr/__init__.py
Normal file
0
unilabos/devices/Qone_nmr/__init__.py
Normal file
File diff suppressed because it is too large
Load Diff
@@ -30,9 +30,32 @@ from pylabrobot.liquid_handling.standard import (
|
|||||||
ResourceMove,
|
ResourceMove,
|
||||||
ResourceDrop,
|
ResourceDrop,
|
||||||
)
|
)
|
||||||
from pylabrobot.resources import ResourceHolder, ResourceStack, Tip, Deck, Plate, Well, TipRack, Resource, Container, Coordinate, TipSpot, Trash, PlateAdapter, TubeRack
|
from pylabrobot.resources import (
|
||||||
|
ResourceHolder,
|
||||||
|
ResourceStack,
|
||||||
|
Tip,
|
||||||
|
Deck,
|
||||||
|
Plate,
|
||||||
|
Well,
|
||||||
|
TipRack,
|
||||||
|
Resource,
|
||||||
|
Container,
|
||||||
|
Coordinate,
|
||||||
|
TipSpot,
|
||||||
|
Trash,
|
||||||
|
PlateAdapter,
|
||||||
|
TubeRack,
|
||||||
|
)
|
||||||
|
|
||||||
from unilabos.devices.liquid_handling.liquid_handler_abstract import LiquidHandlerAbstract, SimpleReturn
|
from unilabos.devices.liquid_handling.liquid_handler_abstract import (
|
||||||
|
LiquidHandlerAbstract,
|
||||||
|
SimpleReturn,
|
||||||
|
SetLiquidReturn,
|
||||||
|
SetLiquidFromPlateReturn,
|
||||||
|
TransferLiquidReturn,
|
||||||
|
)
|
||||||
|
from unilabos.registry.placeholder_type import ResourceSlot
|
||||||
|
from unilabos.resources.resource_tracker import ResourceTreeSet
|
||||||
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
|
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
|
||||||
|
|
||||||
|
|
||||||
@@ -68,19 +91,103 @@ class PRCXI9300Deck(Deck):
|
|||||||
该类定义了 PRCXI 9300 的工作台布局和槽位信息。
|
该类定义了 PRCXI 9300 的工作台布局和槽位信息。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, name: str, size_x: float, size_y: float, size_z: float, **kwargs):
|
# T1-T16 默认位置 (4列×4行)
|
||||||
super().__init__(name, size_x, size_y, size_z)
|
_DEFAULT_SITE_POSITIONS = [
|
||||||
self.slots = [None] * 16 # PRCXI 9300/9320 最大有 16 个槽位
|
(0, 0, 0), (138, 0, 0), (276, 0, 0), (414, 0, 0), # T1-T4
|
||||||
self.slot_locations = [Coordinate(0, 0, 0)] * 16
|
(0, 96, 0), (138, 96, 0), (276, 96, 0), (414, 96, 0), # T5-T8
|
||||||
|
(0, 192, 0), (138, 192, 0), (276, 192, 0), (414, 192, 0), # T9-T12
|
||||||
|
(0, 288, 0), (138, 288, 0), (276, 288, 0), (414, 288, 0), # T13-T16
|
||||||
|
]
|
||||||
|
_DEFAULT_SITE_SIZE = {"width": 128.0, "height": 86, "depth": 0}
|
||||||
|
_DEFAULT_CONTENT_TYPE = ["plate", "tip_rack", "plates", "tip_racks", "tube_rack", "adaptor"]
|
||||||
|
|
||||||
|
def __init__(self, name: str, size_x: float, size_y: float, size_z: float,
|
||||||
|
sites: Optional[List[Dict[str, Any]]] = None, **kwargs):
|
||||||
|
super().__init__(size_x, size_y, size_z, name)
|
||||||
|
if sites is not None:
|
||||||
|
self.sites: List[Dict[str, Any]] = [dict(s) for s in sites]
|
||||||
|
else:
|
||||||
|
self.sites = []
|
||||||
|
for i, (x, y, z) in enumerate(self._DEFAULT_SITE_POSITIONS):
|
||||||
|
self.sites.append({
|
||||||
|
"label": f"T{i + 1}",
|
||||||
|
"visible": True,
|
||||||
|
"position": {"x": x, "y": y, "z": z},
|
||||||
|
"size": dict(self._DEFAULT_SITE_SIZE),
|
||||||
|
"content_type": list(self._DEFAULT_CONTENT_TYPE),
|
||||||
|
})
|
||||||
|
# _ordering: label -> None, 用于外部通过 list(keys()).index(site) 将 Tn 转换为 spot index
|
||||||
|
self._ordering = collections.OrderedDict(
|
||||||
|
(site["label"], None) for site in self.sites
|
||||||
|
)
|
||||||
|
|
||||||
|
def _get_site_location(self, idx: int) -> Coordinate:
|
||||||
|
pos = self.sites[idx]["position"]
|
||||||
|
return Coordinate(pos["x"], pos["y"], pos["z"])
|
||||||
|
|
||||||
|
def _get_site_resource(self, idx: int) -> Optional[Resource]:
|
||||||
|
site_loc = self._get_site_location(idx)
|
||||||
|
for child in self.children:
|
||||||
|
if child.location == site_loc:
|
||||||
|
return child
|
||||||
|
return None
|
||||||
|
|
||||||
|
def assign_child_resource(
|
||||||
|
self,
|
||||||
|
resource: Resource,
|
||||||
|
location: Optional[Coordinate] = None,
|
||||||
|
reassign: bool = True,
|
||||||
|
spot: Optional[int] = None,
|
||||||
|
):
|
||||||
|
idx = spot
|
||||||
|
if spot is not None:
|
||||||
|
idx = spot
|
||||||
|
else:
|
||||||
|
for i, site in enumerate(self.sites):
|
||||||
|
site_loc = self._get_site_location(i)
|
||||||
|
if site.get("label") == resource.name:
|
||||||
|
idx = i
|
||||||
|
break
|
||||||
|
if location is not None and site_loc == location:
|
||||||
|
idx = i
|
||||||
|
break
|
||||||
|
|
||||||
|
if idx is None:
|
||||||
|
for i in range(len(self.sites)):
|
||||||
|
if self._get_site_resource(i) is None:
|
||||||
|
idx = i
|
||||||
|
break
|
||||||
|
|
||||||
|
if idx is None:
|
||||||
|
raise ValueError(f"No available site on deck '{self.name}' for resource '{resource.name}'")
|
||||||
|
|
||||||
|
if not reassign and self._get_site_resource(idx) is not None:
|
||||||
|
raise ValueError(f"Site {idx} ('{self.sites[idx]['label']}') is already occupied")
|
||||||
|
|
||||||
|
loc = self._get_site_location(idx)
|
||||||
|
super().assign_child_resource(resource, location=loc, reassign=reassign)
|
||||||
|
|
||||||
def assign_child_at_slot(self, resource: Resource, slot: int, reassign: bool = False) -> None:
|
def assign_child_at_slot(self, resource: Resource, slot: int, reassign: bool = False) -> None:
|
||||||
if self.slots[slot - 1] is not None and not reassign:
|
self.assign_child_resource(resource, spot=slot - 1, reassign=reassign)
|
||||||
raise ValueError(f"Spot {slot} is already occupied")
|
|
||||||
|
|
||||||
self.slots[slot - 1] = resource
|
def serialize(self) -> dict:
|
||||||
super().assign_child_resource(resource, location=self.slot_locations[slot - 1])
|
data = super().serialize()
|
||||||
|
sites_out = []
|
||||||
|
for i, site in enumerate(self.sites):
|
||||||
|
occupied = self._get_site_resource(i)
|
||||||
|
sites_out.append({
|
||||||
|
"label": site["label"],
|
||||||
|
"visible": site.get("visible", True),
|
||||||
|
"occupied_by": occupied.name if occupied is not None else None,
|
||||||
|
"position": site["position"],
|
||||||
|
"size": site["size"],
|
||||||
|
"content_type": site["content_type"],
|
||||||
|
})
|
||||||
|
data["sites"] = sites_out
|
||||||
|
return data
|
||||||
|
|
||||||
class PRCXI9300Container(Plate):
|
|
||||||
|
class PRCXI9300Container(Container):
|
||||||
"""PRCXI 9300 的专用 Container 类,继承自 Plate,用于槽位定位和未知模块。
|
"""PRCXI 9300 的专用 Container 类,继承自 Plate,用于槽位定位和未知模块。
|
||||||
|
|
||||||
该类定义了 PRCXI 9300 的工作台布局和槽位信息。
|
该类定义了 PRCXI 9300 的工作台布局和槽位信息。
|
||||||
@@ -93,11 +200,10 @@ class PRCXI9300Container(Plate):
|
|||||||
size_y: float,
|
size_y: float,
|
||||||
size_z: float,
|
size_z: float,
|
||||||
category: str,
|
category: str,
|
||||||
ordering: collections.OrderedDict,
|
|
||||||
model: Optional[str] = None,
|
model: Optional[str] = None,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
):
|
):
|
||||||
super().__init__(name, size_x, size_y, size_z, category=category, ordering=ordering, model=model)
|
super().__init__(name, size_x, size_y, size_z, category=category, model=model)
|
||||||
self._unilabos_state = {}
|
self._unilabos_state = {}
|
||||||
|
|
||||||
def load_state(self, state: Dict[str, Any]) -> None:
|
def load_state(self, state: Dict[str, Any]) -> None:
|
||||||
@@ -108,74 +214,81 @@ class PRCXI9300Container(Plate):
|
|||||||
def serialize_state(self) -> Dict[str, Dict[str, Any]]:
|
def serialize_state(self) -> Dict[str, Dict[str, Any]]:
|
||||||
data = super().serialize_state()
|
data = super().serialize_state()
|
||||||
data.update(self._unilabos_state)
|
data.update(self._unilabos_state)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
class PRCXI9300Plate(Plate):
|
class PRCXI9300Plate(Plate):
|
||||||
"""
|
"""
|
||||||
专用孔板类:
|
专用孔板类:
|
||||||
1. 继承自 PLR 原生 Plate,保留所有物理特性。
|
1. 继承自 PLR 原生 Plate,保留所有物理特性。
|
||||||
2. 增加 material_info 参数,用于在初始化时直接绑定 Unilab UUID。
|
2. 增加 material_info 参数,用于在初始化时直接绑定 Unilab UUID。
|
||||||
"""
|
"""
|
||||||
def __init__(self, name: str, size_x: float, size_y: float, size_z: float,
|
|
||||||
category: str = "plate",
|
def __init__(
|
||||||
ordered_items: collections.OrderedDict = None,
|
self,
|
||||||
ordering: Optional[collections.OrderedDict] = None,
|
name: str,
|
||||||
model: Optional[str] = None,
|
size_x: float,
|
||||||
material_info: Optional[Dict[str, Any]] = None,
|
size_y: float,
|
||||||
**kwargs):
|
size_z: float,
|
||||||
|
category: str = "plate",
|
||||||
|
ordered_items: collections.OrderedDict = None,
|
||||||
|
ordering: Optional[collections.OrderedDict] = None,
|
||||||
|
model: Optional[str] = None,
|
||||||
|
material_info: Optional[Dict[str, Any]] = None,
|
||||||
|
**kwargs,
|
||||||
|
):
|
||||||
# 如果 ordered_items 不为 None,直接使用
|
# 如果 ordered_items 不为 None,直接使用
|
||||||
|
items = None
|
||||||
|
ordering_param = None
|
||||||
if ordered_items is not None:
|
if ordered_items is not None:
|
||||||
items = ordered_items
|
items = ordered_items
|
||||||
elif ordering is not None:
|
elif ordering is not None:
|
||||||
# 检查 ordering 中的值是否是字符串(从 JSON 反序列化时的情况)
|
# 检查 ordering 中的值是否是字符串(从 JSON 反序列化时的情况)
|
||||||
# 如果是字符串,说明这是位置名称,需要让 Plate 自己创建 Well 对象
|
# 如果是字符串,说明这是位置名称,需要让 Plate 自己创建 Well 对象
|
||||||
# 我们只传递位置信息(键),不传递值,使用 ordering 参数
|
# 我们只传递位置信息(键),不传递值,使用 ordering 参数
|
||||||
if ordering and isinstance(next(iter(ordering.values()), None), str):
|
if ordering:
|
||||||
# ordering 的值是字符串,只使用键(位置信息)创建新的 OrderedDict
|
values = list(ordering.values())
|
||||||
# 传递 ordering 参数而不是 ordered_items,让 Plate 自己创建 Well 对象
|
value = values[0]
|
||||||
items = None
|
if isinstance(value, str):
|
||||||
# 使用 ordering 参数,只包含位置信息(键)
|
# ordering 的值是字符串,只使用键(位置信息)创建新的 OrderedDict
|
||||||
ordering_param = collections.OrderedDict((k, None) for k in ordering.keys())
|
# 传递 ordering 参数而不是 ordered_items,让 Plate 自己创建 Well 对象
|
||||||
|
items = None
|
||||||
|
# 使用 ordering 参数,只包含位置信息(键)
|
||||||
|
ordering_param = collections.OrderedDict((k, None) for k in ordering.keys())
|
||||||
|
elif value is None:
|
||||||
|
ordering_param = ordering
|
||||||
else:
|
else:
|
||||||
# ordering 的值已经是对象,可以直接使用
|
# ordering 的值已经是对象,可以直接使用
|
||||||
items = ordering
|
items = ordering
|
||||||
ordering_param = None
|
ordering_param = None
|
||||||
else:
|
|
||||||
items = None
|
|
||||||
ordering_param = None
|
|
||||||
|
|
||||||
# 根据情况传递不同的参数
|
# 根据情况传递不同的参数
|
||||||
if items is not None:
|
if items is not None:
|
||||||
super().__init__(name, size_x, size_y, size_z,
|
super().__init__(
|
||||||
ordered_items=items,
|
name, size_x, size_y, size_z, ordered_items=items, category=category, model=model, **kwargs
|
||||||
category=category,
|
)
|
||||||
model=model, **kwargs)
|
|
||||||
elif ordering_param is not None:
|
elif ordering_param is not None:
|
||||||
# 传递 ordering 参数,让 Plate 自己创建 Well 对象
|
# 传递 ordering 参数,让 Plate 自己创建 Well 对象
|
||||||
super().__init__(name, size_x, size_y, size_z,
|
super().__init__(
|
||||||
ordering=ordering_param,
|
name, size_x, size_y, size_z, ordering=ordering_param, category=category, model=model, **kwargs
|
||||||
category=category,
|
)
|
||||||
model=model, **kwargs)
|
|
||||||
else:
|
else:
|
||||||
super().__init__(name, size_x, size_y, size_z,
|
super().__init__(name, size_x, size_y, size_z, category=category, model=model, **kwargs)
|
||||||
category=category,
|
|
||||||
model=model, **kwargs)
|
|
||||||
|
|
||||||
self._unilabos_state = {}
|
self._unilabos_state = {}
|
||||||
if material_info:
|
if material_info:
|
||||||
self._unilabos_state["Material"] = material_info
|
self._unilabos_state["Material"] = material_info
|
||||||
|
|
||||||
|
|
||||||
def load_state(self, state: Dict[str, Any]) -> None:
|
def load_state(self, state: Dict[str, Any]) -> None:
|
||||||
super().load_state(state)
|
super().load_state(state)
|
||||||
self._unilabos_state = state
|
self._unilabos_state = state
|
||||||
|
|
||||||
|
|
||||||
def serialize_state(self) -> Dict[str, Dict[str, Any]]:
|
def serialize_state(self) -> Dict[str, Dict[str, Any]]:
|
||||||
try:
|
try:
|
||||||
data = super().serialize_state()
|
data = super().serialize_state()
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
data = {}
|
data = {}
|
||||||
if hasattr(self, '_unilabos_state') and self._unilabos_state:
|
if hasattr(self, "_unilabos_state") and self._unilabos_state:
|
||||||
safe_state = {}
|
safe_state = {}
|
||||||
for k, v in self._unilabos_state.items():
|
for k, v in self._unilabos_state.items():
|
||||||
# 如果是 Material 字典,深入检查
|
# 如果是 Material 字典,深入检查
|
||||||
@@ -188,35 +301,45 @@ class PRCXI9300Plate(Plate):
|
|||||||
else:
|
else:
|
||||||
# 打印日志提醒(可选)
|
# 打印日志提醒(可选)
|
||||||
# print(f"Warning: Removing non-serializable key {mk} from {self.name}")
|
# print(f"Warning: Removing non-serializable key {mk} from {self.name}")
|
||||||
pass
|
pass
|
||||||
safe_state[k] = safe_material
|
safe_state[k] = safe_material
|
||||||
# 其他顶层属性也进行类型检查
|
# 其他顶层属性也进行类型检查
|
||||||
elif isinstance(v, (str, int, float, bool, list, dict, type(None))):
|
elif isinstance(v, (str, int, float, bool, list, dict, type(None))):
|
||||||
safe_state[k] = v
|
safe_state[k] = v
|
||||||
|
|
||||||
data.update(safe_state)
|
data.update(safe_state)
|
||||||
return data # 其他顶层属性也进行类型检查
|
return data # 其他顶层属性也进行类型检查
|
||||||
|
|
||||||
|
|
||||||
class PRCXI9300TipRack(TipRack):
|
class PRCXI9300TipRack(TipRack):
|
||||||
""" 专用吸头盒类 """
|
"""专用吸头盒类"""
|
||||||
def __init__(self, name: str, size_x: float, size_y: float, size_z: float,
|
|
||||||
category: str = "tip_rack",
|
def __init__(
|
||||||
ordered_items: collections.OrderedDict = None,
|
self,
|
||||||
ordering: Optional[collections.OrderedDict] = None,
|
name: str,
|
||||||
model: Optional[str] = None,
|
size_x: float,
|
||||||
material_info: Optional[Dict[str, Any]] = None,
|
size_y: float,
|
||||||
**kwargs):
|
size_z: float,
|
||||||
|
category: str = "tip_rack",
|
||||||
|
ordered_items: collections.OrderedDict = None,
|
||||||
|
ordering: Optional[collections.OrderedDict] = None,
|
||||||
|
model: Optional[str] = None,
|
||||||
|
material_info: Optional[Dict[str, Any]] = None,
|
||||||
|
**kwargs,
|
||||||
|
):
|
||||||
# 如果 ordered_items 不为 None,直接使用
|
# 如果 ordered_items 不为 None,直接使用
|
||||||
if ordered_items is not None:
|
if ordered_items is not None:
|
||||||
items = ordered_items
|
items = ordered_items
|
||||||
elif ordering is not None:
|
elif ordering is not None:
|
||||||
# 检查 ordering 中的值是否是字符串(从 JSON 反序列化时的情况)
|
# 检查 ordering 中的值类型来决定如何处理:
|
||||||
# 如果是字符串,说明这是位置名称,需要让 TipRack 自己创建 Tip 对象
|
# - 字符串值(从 JSON 反序列化): 只用键创建 ordering_param
|
||||||
# 我们只传递位置信息(键),不传递值,使用 ordering 参数
|
# - None 值(从第二次往返序列化): 同样只用键创建 ordering_param
|
||||||
if ordering and isinstance(next(iter(ordering.values()), None), str):
|
# - 对象值(已经是实际的 Resource 对象): 直接作为 ordered_items 使用
|
||||||
# ordering 的值是字符串,只使用键(位置信息)创建新的 OrderedDict
|
first_val = next(iter(ordering.values()), None) if ordering else None
|
||||||
|
if not ordering or first_val is None or isinstance(first_val, str):
|
||||||
|
# ordering 的值是字符串或 None,只使用键(位置信息)创建新的 OrderedDict
|
||||||
# 传递 ordering 参数而不是 ordered_items,让 TipRack 自己创建 Tip 对象
|
# 传递 ordering 参数而不是 ordered_items,让 TipRack 自己创建 Tip 对象
|
||||||
items = None
|
items = None
|
||||||
# 使用 ordering 参数,只包含位置信息(键)
|
|
||||||
ordering_param = collections.OrderedDict((k, None) for k in ordering.keys())
|
ordering_param = collections.OrderedDict((k, None) for k in ordering.keys())
|
||||||
else:
|
else:
|
||||||
# ordering 的值已经是对象,可以直接使用
|
# ordering 的值已经是对象,可以直接使用
|
||||||
@@ -225,27 +348,23 @@ class PRCXI9300TipRack(TipRack):
|
|||||||
else:
|
else:
|
||||||
items = None
|
items = None
|
||||||
ordering_param = None
|
ordering_param = None
|
||||||
|
|
||||||
# 根据情况传递不同的参数
|
# 根据情况传递不同的参数
|
||||||
if items is not None:
|
if items is not None:
|
||||||
super().__init__(name, size_x, size_y, size_z,
|
super().__init__(
|
||||||
ordered_items=items,
|
name, size_x, size_y, size_z, ordered_items=items, category=category, model=model, **kwargs
|
||||||
category=category,
|
)
|
||||||
model=model, **kwargs)
|
|
||||||
elif ordering_param is not None:
|
elif ordering_param is not None:
|
||||||
# 传递 ordering 参数,让 TipRack 自己创建 Tip 对象
|
# 传递 ordering 参数,让 TipRack 自己创建 Tip 对象
|
||||||
super().__init__(name, size_x, size_y, size_z,
|
super().__init__(
|
||||||
ordering=ordering_param,
|
name, size_x, size_y, size_z, ordering=ordering_param, category=category, model=model, **kwargs
|
||||||
category=category,
|
)
|
||||||
model=model, **kwargs)
|
|
||||||
else:
|
else:
|
||||||
super().__init__(name, size_x, size_y, size_z,
|
super().__init__(name, size_x, size_y, size_z, category=category, model=model, **kwargs)
|
||||||
category=category,
|
|
||||||
model=model, **kwargs)
|
|
||||||
self._unilabos_state = {}
|
self._unilabos_state = {}
|
||||||
if material_info:
|
if material_info:
|
||||||
self._unilabos_state["Material"] = material_info
|
self._unilabos_state["Material"] = material_info
|
||||||
|
|
||||||
def load_state(self, state: Dict[str, Any]) -> None:
|
def load_state(self, state: Dict[str, Any]) -> None:
|
||||||
super().load_state(state)
|
super().load_state(state)
|
||||||
self._unilabos_state = state
|
self._unilabos_state = state
|
||||||
@@ -255,7 +374,7 @@ class PRCXI9300TipRack(TipRack):
|
|||||||
data = super().serialize_state()
|
data = super().serialize_state()
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
data = {}
|
data = {}
|
||||||
if hasattr(self, '_unilabos_state') and self._unilabos_state:
|
if hasattr(self, "_unilabos_state") and self._unilabos_state:
|
||||||
safe_state = {}
|
safe_state = {}
|
||||||
for k, v in self._unilabos_state.items():
|
for k, v in self._unilabos_state.items():
|
||||||
# 如果是 Material 字典,深入检查
|
# 如果是 Material 字典,深入检查
|
||||||
@@ -268,26 +387,33 @@ class PRCXI9300TipRack(TipRack):
|
|||||||
else:
|
else:
|
||||||
# 打印日志提醒(可选)
|
# 打印日志提醒(可选)
|
||||||
# print(f"Warning: Removing non-serializable key {mk} from {self.name}")
|
# print(f"Warning: Removing non-serializable key {mk} from {self.name}")
|
||||||
pass
|
pass
|
||||||
safe_state[k] = safe_material
|
safe_state[k] = safe_material
|
||||||
# 其他顶层属性也进行类型检查
|
# 其他顶层属性也进行类型检查
|
||||||
elif isinstance(v, (str, int, float, bool, list, dict, type(None))):
|
elif isinstance(v, (str, int, float, bool, list, dict, type(None))):
|
||||||
safe_state[k] = v
|
safe_state[k] = v
|
||||||
|
|
||||||
data.update(safe_state)
|
data.update(safe_state)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
class PRCXI9300Trash(Trash):
|
class PRCXI9300Trash(Trash):
|
||||||
"""PRCXI 9300 的专用 Trash 类,继承自 Trash。
|
"""PRCXI 9300 的专用 Trash 类,继承自 Trash。
|
||||||
|
|
||||||
该类定义了 PRCXI 9300 的工作台布局和槽位信息。
|
该类定义了 PRCXI 9300 的工作台布局和槽位信息。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, name: str, size_x: float, size_y: float, size_z: float,
|
def __init__(
|
||||||
category: str = "trash",
|
self,
|
||||||
material_info: Optional[Dict[str, Any]] = None,
|
name: str,
|
||||||
**kwargs):
|
size_x: float,
|
||||||
|
size_y: float,
|
||||||
|
size_z: float,
|
||||||
|
category: str = "trash",
|
||||||
|
material_info: Optional[Dict[str, Any]] = None,
|
||||||
|
**kwargs,
|
||||||
|
):
|
||||||
|
|
||||||
if name != "trash":
|
if name != "trash":
|
||||||
print(f"Warning: PRCXI9300Trash usually expects name='trash' for backend logic, but got '{name}'.")
|
print(f"Warning: PRCXI9300Trash usually expects name='trash' for backend logic, but got '{name}'.")
|
||||||
super().__init__(name, size_x, size_y, size_z, **kwargs)
|
super().__init__(name, size_x, size_y, size_z, **kwargs)
|
||||||
@@ -306,7 +432,7 @@ class PRCXI9300Trash(Trash):
|
|||||||
data = super().serialize_state()
|
data = super().serialize_state()
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
data = {}
|
data = {}
|
||||||
if hasattr(self, '_unilabos_state') and self._unilabos_state:
|
if hasattr(self, "_unilabos_state") and self._unilabos_state:
|
||||||
safe_state = {}
|
safe_state = {}
|
||||||
for k, v in self._unilabos_state.items():
|
for k, v in self._unilabos_state.items():
|
||||||
# 如果是 Material 字典,深入检查
|
# 如果是 Material 字典,深入检查
|
||||||
@@ -319,42 +445,51 @@ class PRCXI9300Trash(Trash):
|
|||||||
else:
|
else:
|
||||||
# 打印日志提醒(可选)
|
# 打印日志提醒(可选)
|
||||||
# print(f"Warning: Removing non-serializable key {mk} from {self.name}")
|
# print(f"Warning: Removing non-serializable key {mk} from {self.name}")
|
||||||
pass
|
pass
|
||||||
safe_state[k] = safe_material
|
safe_state[k] = safe_material
|
||||||
# 其他顶层属性也进行类型检查
|
# 其他顶层属性也进行类型检查
|
||||||
elif isinstance(v, (str, int, float, bool, list, dict, type(None))):
|
elif isinstance(v, (str, int, float, bool, list, dict, type(None))):
|
||||||
safe_state[k] = v
|
safe_state[k] = v
|
||||||
|
|
||||||
data.update(safe_state)
|
data.update(safe_state)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
class PRCXI9300TubeRack(TubeRack):
|
class PRCXI9300TubeRack(TubeRack):
|
||||||
"""
|
"""
|
||||||
专用管架类:用于 EP 管架、试管架等。
|
专用管架类:用于 EP 管架、试管架等。
|
||||||
继承自 PLR 的 TubeRack,并支持注入 material_info (UUID)。
|
继承自 PLR 的 TubeRack,并支持注入 material_info (UUID)。
|
||||||
"""
|
"""
|
||||||
def __init__(self, name: str, size_x: float, size_y: float, size_z: float,
|
|
||||||
category: str = "tube_rack",
|
def __init__(
|
||||||
items: Optional[Dict[str, Any]] = None,
|
self,
|
||||||
ordered_items: Optional[OrderedDict] = None,
|
name: str,
|
||||||
ordering: Optional[OrderedDict] = None,
|
size_x: float,
|
||||||
model: Optional[str] = None,
|
size_y: float,
|
||||||
material_info: Optional[Dict[str, Any]] = None,
|
size_z: float,
|
||||||
**kwargs):
|
category: str = "tube_rack",
|
||||||
|
items: Optional[Dict[str, Any]] = None,
|
||||||
|
ordered_items: Optional[OrderedDict] = None,
|
||||||
|
ordering: Optional[OrderedDict] = None,
|
||||||
|
model: Optional[str] = None,
|
||||||
|
material_info: Optional[Dict[str, Any]] = None,
|
||||||
|
**kwargs,
|
||||||
|
):
|
||||||
|
|
||||||
# 如果 ordered_items 不为 None,直接使用
|
# 如果 ordered_items 不为 None,直接使用
|
||||||
if ordered_items is not None:
|
if ordered_items is not None:
|
||||||
items_to_pass = ordered_items
|
items_to_pass = ordered_items
|
||||||
ordering_param = None
|
ordering_param = None
|
||||||
elif ordering is not None:
|
elif ordering is not None:
|
||||||
# 检查 ordering 中的值是否是字符串(从 JSON 反序列化时的情况)
|
# 检查 ordering 中的值类型来决定如何处理:
|
||||||
# 如果是字符串,说明这是位置名称,需要让 TubeRack 自己创建 Tube 对象
|
# - 字符串值(从 JSON 反序列化): 只用键创建 ordering_param
|
||||||
# 我们只传递位置信息(键),不传递值,使用 ordering 参数
|
# - None 值(从第二次往返序列化): 同样只用键创建 ordering_param
|
||||||
if ordering and isinstance(next(iter(ordering.values()), None), str):
|
# - 对象值(已经是实际的 Resource 对象): 直接作为 ordered_items 使用
|
||||||
# ordering 的值是字符串,只使用键(位置信息)创建新的 OrderedDict
|
first_val = next(iter(ordering.values()), None) if ordering else None
|
||||||
|
if not ordering or first_val is None or isinstance(first_val, str):
|
||||||
|
# ordering 的值是字符串或 None,只使用键(位置信息)创建新的 OrderedDict
|
||||||
# 传递 ordering 参数而不是 ordered_items,让 TubeRack 自己创建 Tube 对象
|
# 传递 ordering 参数而不是 ordered_items,让 TubeRack 自己创建 Tube 对象
|
||||||
items_to_pass = None
|
items_to_pass = None
|
||||||
# 使用 ordering 参数,只包含位置信息(键)
|
|
||||||
ordering_param = collections.OrderedDict((k, None) for k in ordering.keys())
|
ordering_param = collections.OrderedDict((k, None) for k in ordering.keys())
|
||||||
else:
|
else:
|
||||||
# ordering 的值已经是对象,可以直接使用
|
# ordering 的值已经是对象,可以直接使用
|
||||||
@@ -367,24 +502,16 @@ class PRCXI9300TubeRack(TubeRack):
|
|||||||
else:
|
else:
|
||||||
items_to_pass = None
|
items_to_pass = None
|
||||||
ordering_param = None
|
ordering_param = None
|
||||||
|
|
||||||
# 根据情况传递不同的参数
|
# 根据情况传递不同的参数
|
||||||
if items_to_pass is not None:
|
if items_to_pass is not None:
|
||||||
super().__init__(name, size_x, size_y, size_z,
|
super().__init__(name, size_x, size_y, size_z, ordered_items=items_to_pass, model=model, **kwargs)
|
||||||
ordered_items=items_to_pass,
|
|
||||||
model=model,
|
|
||||||
**kwargs)
|
|
||||||
elif ordering_param is not None:
|
elif ordering_param is not None:
|
||||||
# 传递 ordering 参数,让 TubeRack 自己创建 Tube 对象
|
# 传递 ordering 参数,让 TubeRack 自己创建 Tube 对象
|
||||||
super().__init__(name, size_x, size_y, size_z,
|
super().__init__(name, size_x, size_y, size_z, ordering=ordering_param, model=model, **kwargs)
|
||||||
ordering=ordering_param,
|
|
||||||
model=model,
|
|
||||||
**kwargs)
|
|
||||||
else:
|
else:
|
||||||
super().__init__(name, size_x, size_y, size_z,
|
super().__init__(name, size_x, size_y, size_z, model=model, **kwargs)
|
||||||
model=model,
|
|
||||||
**kwargs)
|
|
||||||
|
|
||||||
self._unilabos_state = {}
|
self._unilabos_state = {}
|
||||||
if material_info:
|
if material_info:
|
||||||
self._unilabos_state["Material"] = material_info
|
self._unilabos_state["Material"] = material_info
|
||||||
@@ -394,7 +521,7 @@ class PRCXI9300TubeRack(TubeRack):
|
|||||||
data = super().serialize_state()
|
data = super().serialize_state()
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
data = {}
|
data = {}
|
||||||
if hasattr(self, '_unilabos_state') and self._unilabos_state:
|
if hasattr(self, "_unilabos_state") and self._unilabos_state:
|
||||||
safe_state = {}
|
safe_state = {}
|
||||||
for k, v in self._unilabos_state.items():
|
for k, v in self._unilabos_state.items():
|
||||||
# 如果是 Material 字典,深入检查
|
# 如果是 Material 字典,深入检查
|
||||||
@@ -407,33 +534,41 @@ class PRCXI9300TubeRack(TubeRack):
|
|||||||
else:
|
else:
|
||||||
# 打印日志提醒(可选)
|
# 打印日志提醒(可选)
|
||||||
# print(f"Warning: Removing non-serializable key {mk} from {self.name}")
|
# print(f"Warning: Removing non-serializable key {mk} from {self.name}")
|
||||||
pass
|
pass
|
||||||
safe_state[k] = safe_material
|
safe_state[k] = safe_material
|
||||||
# 其他顶层属性也进行类型检查
|
# 其他顶层属性也进行类型检查
|
||||||
elif isinstance(v, (str, int, float, bool, list, dict, type(None))):
|
elif isinstance(v, (str, int, float, bool, list, dict, type(None))):
|
||||||
safe_state[k] = v
|
safe_state[k] = v
|
||||||
|
|
||||||
data.update(safe_state)
|
data.update(safe_state)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
class PRCXI9300PlateAdapter(PlateAdapter):
|
class PRCXI9300PlateAdapter(PlateAdapter):
|
||||||
"""
|
"""
|
||||||
专用板式适配器类:用于承载 Plate 的底座(如 PCR 适配器、磁吸架等)。
|
专用板式适配器类:用于承载 Plate 的底座(如 PCR 适配器、磁吸架等)。
|
||||||
支持注入 material_info (UUID)。
|
支持注入 material_info (UUID)。
|
||||||
"""
|
"""
|
||||||
def __init__(self, name: str, size_x: float, size_y: float, size_z: float,
|
|
||||||
category: str = "plate_adapter",
|
def __init__(
|
||||||
model: Optional[str] = None,
|
self,
|
||||||
material_info: Optional[Dict[str, Any]] = None,
|
name: str,
|
||||||
# 参数给予默认值 (标准96孔板尺寸)
|
size_x: float,
|
||||||
adapter_hole_size_x: float = 127.76,
|
size_y: float,
|
||||||
adapter_hole_size_y: float = 85.48,
|
size_z: float,
|
||||||
adapter_hole_size_z: float = 10.0, # 假设凹槽深度或板子放置高度
|
category: str = "plate_adapter",
|
||||||
dx: Optional[float] = None,
|
model: Optional[str] = None,
|
||||||
dy: Optional[float] = None,
|
material_info: Optional[Dict[str, Any]] = None,
|
||||||
dz: float = 0.0, # 默认Z轴偏移
|
# 参数给予默认值 (标准96孔板尺寸)
|
||||||
**kwargs):
|
adapter_hole_size_x: float = 127.76,
|
||||||
|
adapter_hole_size_y: float = 85.48,
|
||||||
|
adapter_hole_size_z: float = 10.0, # 假设凹槽深度或板子放置高度
|
||||||
|
dx: Optional[float] = None,
|
||||||
|
dy: Optional[float] = None,
|
||||||
|
dz: float = 0.0, # 默认Z轴偏移
|
||||||
|
**kwargs,
|
||||||
|
):
|
||||||
|
|
||||||
# 自动居中计算:如果未指定 dx/dy,则根据适配器尺寸和孔尺寸计算居中位置
|
# 自动居中计算:如果未指定 dx/dy,则根据适配器尺寸和孔尺寸计算居中位置
|
||||||
if dx is None:
|
if dx is None:
|
||||||
dx = (size_x - adapter_hole_size_x) / 2
|
dx = (size_x - adapter_hole_size_x) / 2
|
||||||
@@ -441,20 +576,20 @@ class PRCXI9300PlateAdapter(PlateAdapter):
|
|||||||
dy = (size_y - adapter_hole_size_y) / 2
|
dy = (size_y - adapter_hole_size_y) / 2
|
||||||
|
|
||||||
super().__init__(
|
super().__init__(
|
||||||
name=name,
|
name=name,
|
||||||
size_x=size_x,
|
size_x=size_x,
|
||||||
size_y=size_y,
|
size_y=size_y,
|
||||||
size_z=size_z,
|
size_z=size_z,
|
||||||
dx=dx,
|
dx=dx,
|
||||||
dy=dy,
|
dy=dy,
|
||||||
dz=dz,
|
dz=dz,
|
||||||
adapter_hole_size_x=adapter_hole_size_x,
|
adapter_hole_size_x=adapter_hole_size_x,
|
||||||
adapter_hole_size_y=adapter_hole_size_y,
|
adapter_hole_size_y=adapter_hole_size_y,
|
||||||
adapter_hole_size_z=adapter_hole_size_z,
|
adapter_hole_size_z=adapter_hole_size_z,
|
||||||
model=model,
|
model=model,
|
||||||
**kwargs
|
**kwargs,
|
||||||
)
|
)
|
||||||
|
|
||||||
self._unilabos_state = {}
|
self._unilabos_state = {}
|
||||||
if material_info:
|
if material_info:
|
||||||
self._unilabos_state["Material"] = material_info
|
self._unilabos_state["Material"] = material_info
|
||||||
@@ -464,7 +599,7 @@ class PRCXI9300PlateAdapter(PlateAdapter):
|
|||||||
data = super().serialize_state()
|
data = super().serialize_state()
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
data = {}
|
data = {}
|
||||||
if hasattr(self, '_unilabos_state') and self._unilabos_state:
|
if hasattr(self, "_unilabos_state") and self._unilabos_state:
|
||||||
safe_state = {}
|
safe_state = {}
|
||||||
for k, v in self._unilabos_state.items():
|
for k, v in self._unilabos_state.items():
|
||||||
# 如果是 Material 字典,深入检查
|
# 如果是 Material 字典,深入检查
|
||||||
@@ -477,15 +612,16 @@ class PRCXI9300PlateAdapter(PlateAdapter):
|
|||||||
else:
|
else:
|
||||||
# 打印日志提醒(可选)
|
# 打印日志提醒(可选)
|
||||||
# print(f"Warning: Removing non-serializable key {mk} from {self.name}")
|
# print(f"Warning: Removing non-serializable key {mk} from {self.name}")
|
||||||
pass
|
pass
|
||||||
safe_state[k] = safe_material
|
safe_state[k] = safe_material
|
||||||
# 其他顶层属性也进行类型检查
|
# 其他顶层属性也进行类型检查
|
||||||
elif isinstance(v, (str, int, float, bool, list, dict, type(None))):
|
elif isinstance(v, (str, int, float, bool, list, dict, type(None))):
|
||||||
safe_state[k] = v
|
safe_state[k] = v
|
||||||
|
|
||||||
data.update(safe_state)
|
data.update(safe_state)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
class PRCXI9300Handler(LiquidHandlerAbstract):
|
class PRCXI9300Handler(LiquidHandlerAbstract):
|
||||||
support_touch_tip = False
|
support_touch_tip = False
|
||||||
|
|
||||||
@@ -514,12 +650,14 @@ class PRCXI9300Handler(LiquidHandlerAbstract):
|
|||||||
tablets_info = []
|
tablets_info = []
|
||||||
count = 0
|
count = 0
|
||||||
for child in deck.children:
|
for child in deck.children:
|
||||||
if child.children:
|
# 如果放其他类型的物料,是不可以的
|
||||||
if "Material" in child.children[0]._unilabos_state:
|
if hasattr(child, "_unilabos_state") and "Material" in child._unilabos_state:
|
||||||
number = int(child.name.replace("T", ""))
|
number = int(child.name.replace("T", ""))
|
||||||
tablets_info.append(
|
tablets_info.append(
|
||||||
WorkTablets(Number=number, Code=f"T{number}", Material=child.children[0]._unilabos_state["Material"])
|
WorkTablets(
|
||||||
|
Number=number, Code=f"T{number}", Material=child._unilabos_state["Material"]
|
||||||
)
|
)
|
||||||
|
)
|
||||||
if is_9320:
|
if is_9320:
|
||||||
print("当前设备是9320")
|
print("当前设备是9320")
|
||||||
# 始终初始化 step_mode 属性
|
# 始终初始化 step_mode 属性
|
||||||
@@ -538,9 +676,14 @@ class PRCXI9300Handler(LiquidHandlerAbstract):
|
|||||||
super().post_init(ros_node)
|
super().post_init(ros_node)
|
||||||
self._unilabos_backend.post_init(ros_node)
|
self._unilabos_backend.post_init(ros_node)
|
||||||
|
|
||||||
def set_liquid(self, wells: list[Well], liquid_names: list[str], volumes: list[float]) -> SimpleReturn:
|
def set_liquid(self, wells: list[Well], liquid_names: list[str], volumes: list[float]) -> SetLiquidReturn:
|
||||||
return super().set_liquid(wells, liquid_names, volumes)
|
return super().set_liquid(wells, liquid_names, volumes)
|
||||||
|
|
||||||
|
def set_liquid_from_plate(
|
||||||
|
self, plate: ResourceSlot, well_names: list[str], liquid_names: list[str], volumes: list[float]
|
||||||
|
) -> SetLiquidFromPlateReturn:
|
||||||
|
return super().set_liquid_from_plate(plate, well_names, liquid_names, volumes)
|
||||||
|
|
||||||
def set_group(self, group_name: str, wells: List[Well], volumes: List[float]):
|
def set_group(self, group_name: str, wells: List[Well], volumes: List[float]):
|
||||||
return super().set_group(group_name, wells, volumes)
|
return super().set_group(group_name, wells, volumes)
|
||||||
|
|
||||||
@@ -660,7 +803,7 @@ class PRCXI9300Handler(LiquidHandlerAbstract):
|
|||||||
mix_liquid_height: Optional[float] = None,
|
mix_liquid_height: Optional[float] = None,
|
||||||
delays: Optional[List[int]] = None,
|
delays: Optional[List[int]] = None,
|
||||||
none_keys: List[str] = [],
|
none_keys: List[str] = [],
|
||||||
):
|
) -> TransferLiquidReturn:
|
||||||
return await super().transfer_liquid(
|
return await super().transfer_liquid(
|
||||||
sources,
|
sources,
|
||||||
targets,
|
targets,
|
||||||
@@ -799,7 +942,8 @@ class PRCXI9300Handler(LiquidHandlerAbstract):
|
|||||||
return await self._unilabos_backend.shaker_action(time, module_no, amplitude, is_wait)
|
return await self._unilabos_backend.shaker_action(time, module_no, amplitude, is_wait)
|
||||||
|
|
||||||
async def heater_action(self, temperature: float, time: int):
|
async def heater_action(self, temperature: float, time: int):
|
||||||
return await self._unilabos_backend.heater_action(temperature, time)
|
return await self._unilabos_backend.heater_action(temperature, time)
|
||||||
|
|
||||||
async def move_plate(
|
async def move_plate(
|
||||||
self,
|
self,
|
||||||
plate: Plate,
|
plate: Plate,
|
||||||
@@ -822,10 +966,11 @@ class PRCXI9300Handler(LiquidHandlerAbstract):
|
|||||||
drop_direction,
|
drop_direction,
|
||||||
pickup_direction,
|
pickup_direction,
|
||||||
pickup_distance_from_top,
|
pickup_distance_from_top,
|
||||||
target_plate_number = to,
|
target_plate_number=to,
|
||||||
**backend_kwargs,
|
**backend_kwargs,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class PRCXI9300Backend(LiquidHandlerBackend):
|
class PRCXI9300Backend(LiquidHandlerBackend):
|
||||||
"""PRCXI 9300 的后端实现,继承自 LiquidHandlerBackend。
|
"""PRCXI 9300 的后端实现,继承自 LiquidHandlerBackend。
|
||||||
|
|
||||||
@@ -878,31 +1023,28 @@ class PRCXI9300Backend(LiquidHandlerBackend):
|
|||||||
self.steps_todo_list.append(step)
|
self.steps_todo_list.append(step)
|
||||||
return step
|
return step
|
||||||
|
|
||||||
|
|
||||||
async def pick_up_resource(self, pickup: ResourcePickup, **backend_kwargs):
|
async def pick_up_resource(self, pickup: ResourcePickup, **backend_kwargs):
|
||||||
|
|
||||||
resource=pickup.resource
|
resource = pickup.resource
|
||||||
offset=pickup.offset
|
offset = pickup.offset
|
||||||
pickup_distance_from_top=pickup.pickup_distance_from_top
|
pickup_distance_from_top = pickup.pickup_distance_from_top
|
||||||
direction=pickup.direction
|
direction = pickup.direction
|
||||||
|
|
||||||
plate_number = int(resource.parent.name.replace("T", ""))
|
plate_number = int(resource.parent.name.replace("T", ""))
|
||||||
is_whole_plate = True
|
is_whole_plate = True
|
||||||
balance_height = 0
|
balance_height = 0
|
||||||
step = self.api_client.clamp_jaw_pick_up(plate_number, is_whole_plate, balance_height)
|
step = self.api_client.clamp_jaw_pick_up(plate_number, is_whole_plate, balance_height)
|
||||||
|
|
||||||
self.steps_todo_list.append(step)
|
self.steps_todo_list.append(step)
|
||||||
return step
|
return step
|
||||||
|
|
||||||
async def drop_resource(self, drop: ResourceDrop, **backend_kwargs):
|
async def drop_resource(self, drop: ResourceDrop, **backend_kwargs):
|
||||||
|
|
||||||
|
|
||||||
plate_number = None
|
plate_number = None
|
||||||
target_plate_number = backend_kwargs.get("target_plate_number", None)
|
target_plate_number = backend_kwargs.get("target_plate_number", None)
|
||||||
if target_plate_number is not None:
|
if target_plate_number is not None:
|
||||||
plate_number = int(target_plate_number.name.replace("T", ""))
|
plate_number = int(target_plate_number.name.replace("T", ""))
|
||||||
|
|
||||||
|
|
||||||
is_whole_plate = True
|
is_whole_plate = True
|
||||||
balance_height = 0
|
balance_height = 0
|
||||||
if plate_number is None:
|
if plate_number is None:
|
||||||
@@ -911,7 +1053,6 @@ class PRCXI9300Backend(LiquidHandlerBackend):
|
|||||||
self.steps_todo_list.append(step)
|
self.steps_todo_list.append(step)
|
||||||
return step
|
return step
|
||||||
|
|
||||||
|
|
||||||
async def heater_action(self, temperature: float, time: int):
|
async def heater_action(self, temperature: float, time: int):
|
||||||
print(f"\n\nHeater action: temperature={temperature}, time={time}\n\n")
|
print(f"\n\nHeater action: temperature={temperature}, time={time}\n\n")
|
||||||
# return await self.api_client.heater_action(temperature, time)
|
# return await self.api_client.heater_action(temperature, time)
|
||||||
@@ -968,7 +1109,7 @@ class PRCXI9300Backend(LiquidHandlerBackend):
|
|||||||
error_code = self.api_client.get_error_code()
|
error_code = self.api_client.get_error_code()
|
||||||
if error_code:
|
if error_code:
|
||||||
print(f"PRCXI9300 error code detected: {error_code}")
|
print(f"PRCXI9300 error code detected: {error_code}")
|
||||||
|
|
||||||
# 清除错误代码
|
# 清除错误代码
|
||||||
self.api_client.clear_error_code()
|
self.api_client.clear_error_code()
|
||||||
print("PRCXI9300 error code cleared.")
|
print("PRCXI9300 error code cleared.")
|
||||||
@@ -976,11 +1117,11 @@ class PRCXI9300Backend(LiquidHandlerBackend):
|
|||||||
# 执行重置
|
# 执行重置
|
||||||
print("Starting PRCXI9300 reset...")
|
print("Starting PRCXI9300 reset...")
|
||||||
self.api_client.call("IAutomation", "Reset")
|
self.api_client.call("IAutomation", "Reset")
|
||||||
|
|
||||||
# 检查重置状态并等待完成
|
# 检查重置状态并等待完成
|
||||||
while not self.is_reset_ok:
|
while not self.is_reset_ok:
|
||||||
print("Waiting for PRCXI9300 to reset...")
|
print("Waiting for PRCXI9300 to reset...")
|
||||||
if hasattr(self, '_ros_node') and self._ros_node is not None:
|
if hasattr(self, "_ros_node") and self._ros_node is not None:
|
||||||
await self._ros_node.sleep(1)
|
await self._ros_node.sleep(1)
|
||||||
else:
|
else:
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
@@ -998,7 +1139,7 @@ class PRCXI9300Backend(LiquidHandlerBackend):
|
|||||||
"""Pick up tips from the specified resource."""
|
"""Pick up tips from the specified resource."""
|
||||||
# INSERT_YOUR_CODE
|
# INSERT_YOUR_CODE
|
||||||
# Ensure use_channels is converted to a list of ints if it's an array
|
# Ensure use_channels is converted to a list of ints if it's an array
|
||||||
if hasattr(use_channels, 'tolist'):
|
if hasattr(use_channels, "tolist"):
|
||||||
_use_channels = use_channels.tolist()
|
_use_channels = use_channels.tolist()
|
||||||
else:
|
else:
|
||||||
_use_channels = list(use_channels) if use_channels is not None else None
|
_use_channels = list(use_channels) if use_channels is not None else None
|
||||||
@@ -1052,7 +1193,7 @@ class PRCXI9300Backend(LiquidHandlerBackend):
|
|||||||
|
|
||||||
async def drop_tips(self, ops: List[Drop], use_channels: List[int] = None):
|
async def drop_tips(self, ops: List[Drop], use_channels: List[int] = None):
|
||||||
"""Pick up tips from the specified resource."""
|
"""Pick up tips from the specified resource."""
|
||||||
if hasattr(use_channels, 'tolist'):
|
if hasattr(use_channels, "tolist"):
|
||||||
_use_channels = use_channels.tolist()
|
_use_channels = use_channels.tolist()
|
||||||
else:
|
else:
|
||||||
_use_channels = list(use_channels) if use_channels is not None else None
|
_use_channels = list(use_channels) if use_channels is not None else None
|
||||||
@@ -1135,7 +1276,7 @@ class PRCXI9300Backend(LiquidHandlerBackend):
|
|||||||
none_keys: List[str] = [],
|
none_keys: List[str] = [],
|
||||||
):
|
):
|
||||||
"""Mix liquid in the specified resources."""
|
"""Mix liquid in the specified resources."""
|
||||||
|
|
||||||
plate_indexes = []
|
plate_indexes = []
|
||||||
for op in targets:
|
for op in targets:
|
||||||
deck = op.parent.parent.parent
|
deck = op.parent.parent.parent
|
||||||
@@ -1178,7 +1319,7 @@ class PRCXI9300Backend(LiquidHandlerBackend):
|
|||||||
|
|
||||||
async def aspirate(self, ops: List[SingleChannelAspiration], use_channels: List[int] = None):
|
async def aspirate(self, ops: List[SingleChannelAspiration], use_channels: List[int] = None):
|
||||||
"""Aspirate liquid from the specified resources."""
|
"""Aspirate liquid from the specified resources."""
|
||||||
if hasattr(use_channels, 'tolist'):
|
if hasattr(use_channels, "tolist"):
|
||||||
_use_channels = use_channels.tolist()
|
_use_channels = use_channels.tolist()
|
||||||
else:
|
else:
|
||||||
_use_channels = list(use_channels) if use_channels is not None else None
|
_use_channels = list(use_channels) if use_channels is not None else None
|
||||||
@@ -1235,7 +1376,7 @@ class PRCXI9300Backend(LiquidHandlerBackend):
|
|||||||
|
|
||||||
async def dispense(self, ops: List[SingleChannelDispense], use_channels: List[int] = None):
|
async def dispense(self, ops: List[SingleChannelDispense], use_channels: List[int] = None):
|
||||||
"""Dispense liquid into the specified resources."""
|
"""Dispense liquid into the specified resources."""
|
||||||
if hasattr(use_channels, 'tolist'):
|
if hasattr(use_channels, "tolist"):
|
||||||
_use_channels = use_channels.tolist()
|
_use_channels = use_channels.tolist()
|
||||||
else:
|
else:
|
||||||
_use_channels = list(use_channels) if use_channels is not None else None
|
_use_channels = list(use_channels) if use_channels is not None else None
|
||||||
@@ -1416,7 +1557,6 @@ class PRCXI9300Api:
|
|||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
return success
|
return success
|
||||||
|
|
||||||
|
|
||||||
def call(self, service: str, method: str, params: Optional[list] = None) -> Any:
|
def call(self, service: str, method: str, params: Optional[list] = None) -> Any:
|
||||||
payload = json.dumps(
|
payload = json.dumps(
|
||||||
{"ServiceName": service, "MethodName": method, "Paramters": params or []}, separators=(",", ":")
|
{"ServiceName": service, "MethodName": method, "Paramters": params or []}, separators=(",", ":")
|
||||||
@@ -1543,7 +1683,7 @@ class PRCXI9300Api:
|
|||||||
assist_fun5: str = "",
|
assist_fun5: str = "",
|
||||||
liquid_method: str = "NormalDispense",
|
liquid_method: str = "NormalDispense",
|
||||||
axis: str = "Left",
|
axis: str = "Left",
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
return {
|
return {
|
||||||
"StepAxis": axis,
|
"StepAxis": axis,
|
||||||
"Function": "Imbibing",
|
"Function": "Imbibing",
|
||||||
@@ -1621,7 +1761,7 @@ class PRCXI9300Api:
|
|||||||
assist_fun5: str = "",
|
assist_fun5: str = "",
|
||||||
liquid_method: str = "NormalDispense",
|
liquid_method: str = "NormalDispense",
|
||||||
axis: str = "Left",
|
axis: str = "Left",
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
return {
|
return {
|
||||||
"StepAxis": axis,
|
"StepAxis": axis,
|
||||||
"Function": "Blending",
|
"Function": "Blending",
|
||||||
@@ -1681,11 +1821,11 @@ class PRCXI9300Api:
|
|||||||
"LiquidDispensingMethod": liquid_method,
|
"LiquidDispensingMethod": liquid_method,
|
||||||
}
|
}
|
||||||
|
|
||||||
def clamp_jaw_pick_up(self,
|
def clamp_jaw_pick_up(
|
||||||
|
self,
|
||||||
plate_no: int,
|
plate_no: int,
|
||||||
is_whole_plate: bool,
|
is_whole_plate: bool,
|
||||||
balance_height: int,
|
balance_height: int,
|
||||||
|
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
return {
|
return {
|
||||||
"StepAxis": "ClampingJaw",
|
"StepAxis": "ClampingJaw",
|
||||||
@@ -1695,7 +1835,7 @@ class PRCXI9300Api:
|
|||||||
"HoleRow": 1,
|
"HoleRow": 1,
|
||||||
"HoleCol": 1,
|
"HoleCol": 1,
|
||||||
"BalanceHeight": balance_height,
|
"BalanceHeight": balance_height,
|
||||||
"PlateOrHoleNum": f"T{plate_no}"
|
"PlateOrHoleNum": f"T{plate_no}",
|
||||||
}
|
}
|
||||||
|
|
||||||
def clamp_jaw_drop(
|
def clamp_jaw_drop(
|
||||||
@@ -1703,7 +1843,6 @@ class PRCXI9300Api:
|
|||||||
plate_no: int,
|
plate_no: int,
|
||||||
is_whole_plate: bool,
|
is_whole_plate: bool,
|
||||||
balance_height: int,
|
balance_height: int,
|
||||||
|
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
return {
|
return {
|
||||||
"StepAxis": "ClampingJaw",
|
"StepAxis": "ClampingJaw",
|
||||||
@@ -1713,7 +1852,7 @@ class PRCXI9300Api:
|
|||||||
"HoleRow": 1,
|
"HoleRow": 1,
|
||||||
"HoleCol": 1,
|
"HoleCol": 1,
|
||||||
"BalanceHeight": balance_height,
|
"BalanceHeight": balance_height,
|
||||||
"PlateOrHoleNum": f"T{plate_no}"
|
"PlateOrHoleNum": f"T{plate_no}",
|
||||||
}
|
}
|
||||||
|
|
||||||
def shaker_action(self, time: int, module_no: int, amplitude: int, is_wait: bool):
|
def shaker_action(self, time: int, module_no: int, amplitude: int, is_wait: bool):
|
||||||
@@ -1726,6 +1865,7 @@ class PRCXI9300Api:
|
|||||||
"AssistFun4": is_wait,
|
"AssistFun4": is_wait,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class DefaultLayout:
|
class DefaultLayout:
|
||||||
|
|
||||||
def __init__(self, product_name: str = "PRCXI9300"):
|
def __init__(self, product_name: str = "PRCXI9300"):
|
||||||
@@ -2104,7 +2244,9 @@ if __name__ == "__main__":
|
|||||||
size_y=50,
|
size_y=50,
|
||||||
size_z=10,
|
size_z=10,
|
||||||
category="tip_rack",
|
category="tip_rack",
|
||||||
ordered_items=collections.OrderedDict({k: f"{child_prefix}_{k}" for k, v in tip_racks["ordering"].items()}),
|
ordered_items=collections.OrderedDict(
|
||||||
|
{k: f"{child_prefix}_{k}" for k, v in tip_racks["ordering"].items()}
|
||||||
|
),
|
||||||
)
|
)
|
||||||
tip_rack_serialized = tip_rack.serialize()
|
tip_rack_serialized = tip_rack.serialize()
|
||||||
tip_rack_serialized["parent_name"] = deck.name
|
tip_rack_serialized["parent_name"] = deck.name
|
||||||
@@ -2299,43 +2441,37 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
A = tree_to_list([resource_plr_to_ulab(deck)])
|
A = tree_to_list([resource_plr_to_ulab(deck)])
|
||||||
with open("deck.json", "w", encoding="utf-8") as f:
|
with open("deck.json", "w", encoding="utf-8") as f:
|
||||||
A.insert(0, {
|
A.insert(
|
||||||
"id": "PRCXI",
|
0,
|
||||||
"name": "PRCXI",
|
{
|
||||||
"parent": None,
|
"id": "PRCXI",
|
||||||
"type": "device",
|
"name": "PRCXI",
|
||||||
"class": "liquid_handler.prcxi",
|
"parent": None,
|
||||||
"position": {
|
"type": "device",
|
||||||
"x": 0,
|
"class": "liquid_handler.prcxi",
|
||||||
"y": 0,
|
"position": {"x": 0, "y": 0, "z": 0},
|
||||||
"z": 0
|
"config": {
|
||||||
},
|
"deck": {
|
||||||
"config": {
|
"_resource_child_name": "PRCXI_Deck",
|
||||||
"deck": {
|
"_resource_type": "unilabos.devices.liquid_handling.prcxi.prcxi:PRCXI9300Deck",
|
||||||
"_resource_child_name": "PRCXI_Deck",
|
},
|
||||||
"_resource_type": "unilabos.devices.liquid_handling.prcxi.prcxi:PRCXI9300Deck"
|
"host": "192.168.0.121",
|
||||||
|
"port": 9999,
|
||||||
|
"timeout": 10.0,
|
||||||
|
"axis": "Right",
|
||||||
|
"channel_num": 1,
|
||||||
|
"setup": False,
|
||||||
|
"debug": True,
|
||||||
|
"simulator": True,
|
||||||
|
"matrix_id": "5de524d0-3f95-406c-86dd-f83626ebc7cb",
|
||||||
|
"is_9320": True,
|
||||||
},
|
},
|
||||||
"host": "192.168.0.121",
|
"data": {},
|
||||||
"port": 9999,
|
"children": ["PRCXI_Deck"],
|
||||||
"timeout": 10.0,
|
|
||||||
"axis": "Right",
|
|
||||||
"channel_num": 1,
|
|
||||||
"setup": False,
|
|
||||||
"debug": True,
|
|
||||||
"simulator": True,
|
|
||||||
"matrix_id": "5de524d0-3f95-406c-86dd-f83626ebc7cb",
|
|
||||||
"is_9320": True
|
|
||||||
},
|
},
|
||||||
"data": {},
|
)
|
||||||
"children": [
|
|
||||||
"PRCXI_Deck"
|
|
||||||
]
|
|
||||||
})
|
|
||||||
A[1]["parent"] = "PRCXI"
|
A[1]["parent"] = "PRCXI"
|
||||||
json.dump({
|
json.dump({"nodes": A, "links": []}, f, indent=4, ensure_ascii=False)
|
||||||
"nodes": A,
|
|
||||||
"links": []
|
|
||||||
}, f, indent=4, ensure_ascii=False)
|
|
||||||
|
|
||||||
handler = PRCXI9300Handler(
|
handler = PRCXI9300Handler(
|
||||||
deck=deck,
|
deck=deck,
|
||||||
@@ -2377,7 +2513,6 @@ if __name__ == "__main__":
|
|||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
os._exit(0)
|
os._exit(0)
|
||||||
|
|
||||||
|
|
||||||
prcxi_api = PRCXI9300Api(host="192.168.0.121", port=9999)
|
prcxi_api = PRCXI9300Api(host="192.168.0.121", port=9999)
|
||||||
prcxi_api.list_matrices()
|
prcxi_api.list_matrices()
|
||||||
prcxi_api.get_all_materials()
|
prcxi_api.get_all_materials()
|
||||||
|
|||||||
376
unilabos/devices/motor/ZDT_X42.py
Normal file
376
unilabos/devices/motor/ZDT_X42.py
Normal file
@@ -0,0 +1,376 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
ZDT X42 Closed-Loop Stepper Motor Driver
|
||||||
|
RS485 Serial Communication via USB-Serial Converter
|
||||||
|
|
||||||
|
- Baudrate: 115200
|
||||||
|
"""
|
||||||
|
|
||||||
|
import serial
|
||||||
|
import time
|
||||||
|
import threading
|
||||||
|
import struct
|
||||||
|
import logging
|
||||||
|
from typing import Optional, Any
|
||||||
|
|
||||||
|
try:
|
||||||
|
from unilabos.device_comms.universal_driver import UniversalDriver
|
||||||
|
except ImportError:
|
||||||
|
class UniversalDriver:
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
self.logger = logging.getLogger(self.__class__.__name__)
|
||||||
|
def execute_command_from_outer(self, command: Any): pass
|
||||||
|
|
||||||
|
from serial.rs485 import RS485Settings
|
||||||
|
|
||||||
|
|
||||||
|
class ZDTX42Driver(UniversalDriver):
|
||||||
|
"""
|
||||||
|
ZDT X42 闭环步进电机驱动器
|
||||||
|
|
||||||
|
支持功能:
|
||||||
|
- 速度模式运行
|
||||||
|
- 位置模式运行 (相对/绝对)
|
||||||
|
- 位置读取和清零
|
||||||
|
- 使能/禁用控制
|
||||||
|
|
||||||
|
通信协议:
|
||||||
|
- 帧格式: [设备ID] [功能码] [数据...] [校验位=0x6B]
|
||||||
|
- 响应长度根据功能码决定
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
port: str,
|
||||||
|
baudrate: int = 115200,
|
||||||
|
device_id: int = 1,
|
||||||
|
timeout: float = 0.5,
|
||||||
|
debug: bool = False
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
初始化 ZDT X42 电机驱动
|
||||||
|
|
||||||
|
Args:
|
||||||
|
port: 串口设备路径
|
||||||
|
baudrate: 波特率 (默认 115200)
|
||||||
|
device_id: 设备地址 (1-255)
|
||||||
|
timeout: 通信超时时间(秒)
|
||||||
|
debug: 是否启用调试输出
|
||||||
|
"""
|
||||||
|
super().__init__()
|
||||||
|
self.id = device_id
|
||||||
|
self.debug = debug
|
||||||
|
self.lock = threading.RLock()
|
||||||
|
self.status = "idle" # 对应注册表中的 status (str)
|
||||||
|
self.position = 0 # 对应注册表中的 position (int)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.ser = serial.Serial(
|
||||||
|
port=port,
|
||||||
|
baudrate=baudrate,
|
||||||
|
timeout=timeout,
|
||||||
|
bytesize=serial.EIGHTBITS,
|
||||||
|
parity=serial.PARITY_NONE,
|
||||||
|
stopbits=serial.STOPBITS_ONE
|
||||||
|
)
|
||||||
|
|
||||||
|
# 启用 RS485 模式
|
||||||
|
try:
|
||||||
|
self.ser.rs485_mode = RS485Settings(
|
||||||
|
rts_level_for_tx=True,
|
||||||
|
rts_level_for_rx=False
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
pass # RS485 模式是可选的
|
||||||
|
|
||||||
|
self.logger.info(
|
||||||
|
f"ZDT X42 Motor connected: {port} "
|
||||||
|
f"(Baud: {baudrate}, ID: {device_id})"
|
||||||
|
)
|
||||||
|
# 自动使能电机,确保初始状态可运动
|
||||||
|
self.enable(True)
|
||||||
|
|
||||||
|
# 启动背景轮询线程,确保 position 实时刷新
|
||||||
|
self._stop_event = threading.Event()
|
||||||
|
self._polling_thread = threading.Thread(
|
||||||
|
target=self._update_loop,
|
||||||
|
name=f"ZDTPolling_{port}",
|
||||||
|
daemon=True
|
||||||
|
)
|
||||||
|
self._polling_thread.start()
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error(f"Failed to open serial port {port}: {e}")
|
||||||
|
self.ser = None
|
||||||
|
|
||||||
|
def _update_loop(self):
|
||||||
|
"""背景循环读取电机位置"""
|
||||||
|
while not self._stop_event.is_set():
|
||||||
|
try:
|
||||||
|
self.get_position()
|
||||||
|
except Exception as e:
|
||||||
|
if self.debug:
|
||||||
|
self.logger.error(f"Polling error: {e}")
|
||||||
|
time.sleep(1.0) # 每1秒刷新一次位置数据
|
||||||
|
|
||||||
|
def _send(self, func_code: int, payload: list) -> bytes:
|
||||||
|
"""
|
||||||
|
发送指令并接收响应
|
||||||
|
|
||||||
|
Args:
|
||||||
|
func_code: 功能码
|
||||||
|
payload: 数据负载 (list of bytes)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
响应数据 (bytes)
|
||||||
|
"""
|
||||||
|
if not self.ser:
|
||||||
|
self.logger.error("Serial port not available")
|
||||||
|
return b""
|
||||||
|
|
||||||
|
with self.lock:
|
||||||
|
# 清空输入缓冲区
|
||||||
|
self.ser.reset_input_buffer()
|
||||||
|
|
||||||
|
# 构建消息: [ID] [功能码] [数据...] [校验位=0x6B]
|
||||||
|
message = bytes([self.id, func_code] + payload + [0x6B])
|
||||||
|
|
||||||
|
# 发送
|
||||||
|
self.ser.write(message)
|
||||||
|
|
||||||
|
# 根据功能码决定响应长度
|
||||||
|
# 查询类指令返回 10 字节,控制类指令返回 4 字节
|
||||||
|
read_len = 10 if func_code in [0x31, 0x32, 0x35, 0x24, 0x27] else 4
|
||||||
|
response = self.ser.read(read_len)
|
||||||
|
|
||||||
|
# 调试输出
|
||||||
|
if self.debug:
|
||||||
|
sent_hex = message.hex().upper()
|
||||||
|
recv_hex = response.hex().upper() if response else 'TIMEOUT'
|
||||||
|
print(f"[ID {self.id}] TX: {sent_hex} → RX: {recv_hex}")
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
|
def enable(self, on: bool = True) -> bool:
|
||||||
|
"""
|
||||||
|
使能/禁用电机
|
||||||
|
|
||||||
|
Args:
|
||||||
|
on: True=使能(锁轴), False=禁用(松轴)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
是否成功
|
||||||
|
"""
|
||||||
|
state = 1 if on else 0
|
||||||
|
resp = self._send(0xF3, [0xAB, state, 0])
|
||||||
|
return len(resp) >= 4
|
||||||
|
|
||||||
|
def move_speed(
|
||||||
|
self,
|
||||||
|
speed_rpm: int,
|
||||||
|
direction: str = "CW",
|
||||||
|
acceleration: int = 10
|
||||||
|
) -> bool:
|
||||||
|
"""
|
||||||
|
速度模式运行
|
||||||
|
|
||||||
|
Args:
|
||||||
|
speed_rpm: 转速 (RPM)
|
||||||
|
direction: 方向 ("CW"=顺时针, "CCW"=逆时针)
|
||||||
|
acceleration: 加速度 (0-255)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
是否成功
|
||||||
|
"""
|
||||||
|
dir_val = 0 if direction.upper() in ["CW", "顺时针"] else 1
|
||||||
|
speed_bytes = struct.pack('>H', int(speed_rpm))
|
||||||
|
self.status = f"moving@{speed_rpm}rpm"
|
||||||
|
resp = self._send(0xF6, [dir_val, speed_bytes[0], speed_bytes[1], acceleration, 0])
|
||||||
|
return len(resp) >= 4
|
||||||
|
|
||||||
|
def move_position(
|
||||||
|
self,
|
||||||
|
pulses: int,
|
||||||
|
speed_rpm: int,
|
||||||
|
direction: str = "CW",
|
||||||
|
acceleration: int = 10,
|
||||||
|
absolute: bool = False
|
||||||
|
) -> bool:
|
||||||
|
"""
|
||||||
|
位置模式运行
|
||||||
|
|
||||||
|
Args:
|
||||||
|
pulses: 脉冲数
|
||||||
|
speed_rpm: 转速 (RPM)
|
||||||
|
direction: 方向 ("CW"=顺时针, "CCW"=逆时针)
|
||||||
|
acceleration: 加速度 (0-255)
|
||||||
|
absolute: True=绝对位置, False=相对位置
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
是否成功
|
||||||
|
"""
|
||||||
|
dir_val = 0 if direction.upper() in ["CW", "顺时针"] else 1
|
||||||
|
speed_bytes = struct.pack('>H', int(speed_rpm))
|
||||||
|
self.status = f"moving_to_{pulses}"
|
||||||
|
pulse_bytes = struct.pack('>I', int(pulses))
|
||||||
|
abs_flag = 1 if absolute else 0
|
||||||
|
|
||||||
|
payload = [
|
||||||
|
dir_val,
|
||||||
|
speed_bytes[0], speed_bytes[1],
|
||||||
|
acceleration,
|
||||||
|
pulse_bytes[0], pulse_bytes[1], pulse_bytes[2], pulse_bytes[3],
|
||||||
|
abs_flag,
|
||||||
|
0
|
||||||
|
]
|
||||||
|
|
||||||
|
resp = self._send(0xFD, payload)
|
||||||
|
return len(resp) >= 4
|
||||||
|
|
||||||
|
def stop(self) -> bool:
|
||||||
|
"""
|
||||||
|
停止电机
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
是否成功
|
||||||
|
"""
|
||||||
|
self.status = "idle"
|
||||||
|
resp = self._send(0xFE, [0x98, 0])
|
||||||
|
return len(resp) >= 4
|
||||||
|
|
||||||
|
def rotate_quarter(self, speed_rpm: int = 60, direction: str = "CW") -> bool:
|
||||||
|
"""
|
||||||
|
电机旋转 1/4 圈 (阻塞式)
|
||||||
|
假设电机细分为 3200 脉冲/圈,1/4 圈 = 800 脉冲
|
||||||
|
"""
|
||||||
|
pulses = 800
|
||||||
|
success = self.move_position(pulses=pulses, speed_rpm=speed_rpm, direction=direction, absolute=False)
|
||||||
|
|
||||||
|
if success:
|
||||||
|
# 计算预估旋转时间并进行阻塞等待 (Time = revolutions / (RPM/60))
|
||||||
|
# 1/4 rev / (RPM/60) = 15.0 / RPM
|
||||||
|
estimated_time = 15.0 / max(1, speed_rpm)
|
||||||
|
time.sleep(estimated_time + 0.5) # 额外给 0.5 秒缓冲
|
||||||
|
self.status = "idle"
|
||||||
|
|
||||||
|
return success
|
||||||
|
|
||||||
|
def wait_time(self, duration_s: float) -> bool:
|
||||||
|
"""
|
||||||
|
等待指定时间 (秒)
|
||||||
|
"""
|
||||||
|
self.logger.info(f"Waiting for {duration_s} seconds...")
|
||||||
|
time.sleep(duration_s)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def set_zero(self) -> bool:
|
||||||
|
"""
|
||||||
|
清零当前位置
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
是否成功
|
||||||
|
"""
|
||||||
|
resp = self._send(0x0A, [])
|
||||||
|
return len(resp) >= 4
|
||||||
|
|
||||||
|
def get_position(self) -> Optional[int]:
|
||||||
|
"""
|
||||||
|
读取当前位置 (脉冲数)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
当前位置脉冲数,失败返回 None
|
||||||
|
"""
|
||||||
|
resp = self._send(0x32, [])
|
||||||
|
|
||||||
|
if len(resp) >= 8:
|
||||||
|
# 响应格式: [ID] [Func] [符号位] [数值4字节] [校验]
|
||||||
|
sign = resp[2] # 0=正, 1=负
|
||||||
|
value = struct.unpack('>I', resp[3:7])[0]
|
||||||
|
self.position = -value if sign == 1 else value
|
||||||
|
|
||||||
|
if self.debug:
|
||||||
|
print(f"[Position] Raw: {resp.hex().upper()}, Parsed: {self.position}")
|
||||||
|
|
||||||
|
return self.position
|
||||||
|
|
||||||
|
self.logger.warning("Failed to read position")
|
||||||
|
return None
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
"""关闭串口连接并停止线程"""
|
||||||
|
if hasattr(self, '_stop_event'):
|
||||||
|
self._stop_event.set()
|
||||||
|
|
||||||
|
if self.ser and self.ser.is_open:
|
||||||
|
self.ser.close()
|
||||||
|
self.logger.info("Serial port closed")
|
||||||
|
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# 测试和调试代码
|
||||||
|
# ============================================================
|
||||||
|
|
||||||
|
def test_motor():
|
||||||
|
"""基础功能测试"""
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
|
print("="*60)
|
||||||
|
print("ZDT X42 电机驱动测试")
|
||||||
|
print("="*60)
|
||||||
|
|
||||||
|
driver = ZDTX42Driver(
|
||||||
|
port="/dev/tty.usbserial-3110",
|
||||||
|
baudrate=115200,
|
||||||
|
device_id=2,
|
||||||
|
debug=True
|
||||||
|
)
|
||||||
|
|
||||||
|
if not driver.ser:
|
||||||
|
print("❌ 串口打开失败")
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 测试 1: 读取位置
|
||||||
|
print("\n[1] 读取当前位置")
|
||||||
|
pos = driver.get_position()
|
||||||
|
print(f"✓ 当前位置: {pos} 脉冲")
|
||||||
|
|
||||||
|
# 测试 2: 使能
|
||||||
|
print("\n[2] 使能电机")
|
||||||
|
driver.enable(True)
|
||||||
|
time.sleep(0.3)
|
||||||
|
print("✓ 电机已锁定")
|
||||||
|
|
||||||
|
# 测试 3: 相对位置运动
|
||||||
|
print("\n[3] 相对位置运动 (1000脉冲)")
|
||||||
|
driver.move_position(pulses=1000, speed_rpm=60, direction="CW")
|
||||||
|
time.sleep(2)
|
||||||
|
pos = driver.get_position()
|
||||||
|
print(f"✓ 新位置: {pos}")
|
||||||
|
|
||||||
|
# 测试 4: 速度运动
|
||||||
|
print("\n[4] 速度模式 (30RPM, 3秒)")
|
||||||
|
driver.move_speed(speed_rpm=30, direction="CW")
|
||||||
|
time.sleep(3)
|
||||||
|
driver.stop()
|
||||||
|
pos = driver.get_position()
|
||||||
|
print(f"✓ 停止后位置: {pos}")
|
||||||
|
|
||||||
|
# 测试 5: 禁用
|
||||||
|
print("\n[5] 禁用电机")
|
||||||
|
driver.enable(False)
|
||||||
|
print("✓ 电机已松开")
|
||||||
|
|
||||||
|
print("\n" + "="*60)
|
||||||
|
print("✅ 测试完成")
|
||||||
|
print("="*60)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\n❌ 测试失败: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
finally:
|
||||||
|
driver.close()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_motor()
|
||||||
@@ -19,10 +19,11 @@ from rclpy.node import Node
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
class LiquidHandlerJointPublisher(BaseROS2DeviceNode):
|
class LiquidHandlerJointPublisher(BaseROS2DeviceNode):
|
||||||
def __init__(self,resources_config:list, resource_tracker, rate=50, device_id:str = "lh_joint_publisher", **kwargs):
|
def __init__(self,resources_config:list, resource_tracker, rate=50, device_id:str = "lh_joint_publisher", registry_name: str = "lh_joint_publisher", **kwargs):
|
||||||
super().__init__(
|
super().__init__(
|
||||||
driver_instance=self,
|
driver_instance=self,
|
||||||
device_id=device_id,
|
device_id=device_id,
|
||||||
|
registry_name=registry_name,
|
||||||
status_types={},
|
status_types={},
|
||||||
action_value_mappings={},
|
action_value_mappings={},
|
||||||
hardware_interface={},
|
hardware_interface={},
|
||||||
|
|||||||
@@ -623,6 +623,119 @@ class ChinweDevice(UniversalDriver):
|
|||||||
time.sleep(duration)
|
time.sleep(duration)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def separation_step(self, motor_id: int = 5, speed: int = 60, pulses: int = 700,
|
||||||
|
max_cycles: int = 0, timeout: int = 300) -> bool:
|
||||||
|
"""
|
||||||
|
分液步骤 - 液位传感器与电机联动
|
||||||
|
当液位传感器检测到"有液"时,电机顺时针旋转指定脉冲数
|
||||||
|
当液位传感器检测到"无液"时,电机逆时针旋转指定脉冲数
|
||||||
|
|
||||||
|
:param motor_id: 电机ID (必须在初始化时配置的motor_ids中)
|
||||||
|
:param speed: 电机转速 (RPM)
|
||||||
|
:param pulses: 每次旋转的脉冲数 (默认700约为1/4圈,假设3200脉冲/圈)
|
||||||
|
:param max_cycles: 最大执行循环次数 (0=无限制,默认0)
|
||||||
|
:param timeout: 整体超时时间 (秒)
|
||||||
|
:return: 成功返回True,超时或失败返回False
|
||||||
|
"""
|
||||||
|
motor_id = int(motor_id)
|
||||||
|
speed = int(speed)
|
||||||
|
pulses = int(pulses)
|
||||||
|
max_cycles = int(max_cycles)
|
||||||
|
timeout = int(timeout)
|
||||||
|
|
||||||
|
# 检查电机是否存在
|
||||||
|
if motor_id not in self.motors:
|
||||||
|
self.logger.error(f"Motor {motor_id} not found in configured motors: {list(self.motors.keys())}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
# 检查传感器是否可用
|
||||||
|
if not self.sensor:
|
||||||
|
self.logger.error("Sensor not initialized")
|
||||||
|
return False
|
||||||
|
|
||||||
|
motor = self.motors[motor_id]
|
||||||
|
|
||||||
|
# 停止轮询线程,避免与 separation_step 同时读取传感器造成串口冲突
|
||||||
|
self.logger.info("Stopping polling thread for separation_step...")
|
||||||
|
self._stop_event.set()
|
||||||
|
if self._poll_thread and self._poll_thread.is_alive():
|
||||||
|
self._poll_thread.join(timeout=2.0)
|
||||||
|
|
||||||
|
# 使能电机
|
||||||
|
self.logger.info(f"Enabling motor {motor_id}...")
|
||||||
|
motor.enable(True)
|
||||||
|
time.sleep(0.2)
|
||||||
|
|
||||||
|
self.logger.info(f"Starting separation step: motor_id={motor_id}, speed={speed} RPM, "
|
||||||
|
f"pulses={pulses}, max_cycles={max_cycles}, timeout={timeout}s")
|
||||||
|
|
||||||
|
# 记录上一次的液位状态
|
||||||
|
last_level = None
|
||||||
|
cycle_count = 0
|
||||||
|
start_time = time.time()
|
||||||
|
error_count = 0
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
# 检查超时
|
||||||
|
if time.time() - start_time > timeout:
|
||||||
|
self.logger.warning(f"Separation step timeout after {timeout} seconds")
|
||||||
|
return False
|
||||||
|
|
||||||
|
# 检查循环次数限制
|
||||||
|
if max_cycles > 0 and cycle_count >= max_cycles:
|
||||||
|
self.logger.info(f"Separation step completed: reached max_cycles={max_cycles}")
|
||||||
|
return True
|
||||||
|
|
||||||
|
# 读取传感器数据
|
||||||
|
data = self.sensor.read_level()
|
||||||
|
|
||||||
|
if data is None:
|
||||||
|
error_count += 1
|
||||||
|
if error_count > 5:
|
||||||
|
self.logger.warning("Sensor read failed multiple times, retrying...")
|
||||||
|
error_count = 0
|
||||||
|
time.sleep(0.5)
|
||||||
|
continue
|
||||||
|
|
||||||
|
error_count = 0
|
||||||
|
current_level = data['level']
|
||||||
|
rssi = data['rssi']
|
||||||
|
|
||||||
|
# 检测状态变化 (包括首次检测)
|
||||||
|
if current_level != last_level:
|
||||||
|
cycle_count += 1
|
||||||
|
|
||||||
|
if current_level:
|
||||||
|
# 有液 -> 电机顺时针旋转
|
||||||
|
self.logger.info(f"[Cycle {cycle_count}] Liquid detected (RSSI={rssi}), "
|
||||||
|
f"rotating motor {motor_id} clockwise {pulses} pulses")
|
||||||
|
motor.run_position(pulses=pulses, speed_rpm=speed, direction=0, absolute=False)
|
||||||
|
|
||||||
|
# 等待电机完成 (预估时间)
|
||||||
|
estimated_time = 15.0 / max(1, speed)
|
||||||
|
time.sleep(estimated_time + 0.5)
|
||||||
|
|
||||||
|
else:
|
||||||
|
# 无液 -> 电机逆时针旋转
|
||||||
|
self.logger.info(f"[Cycle {cycle_count}] No liquid detected (RSSI={rssi}), "
|
||||||
|
f"rotating motor {motor_id} counter-clockwise {pulses} pulses")
|
||||||
|
motor.run_position(pulses=pulses, speed_rpm=speed, direction=1, absolute=False)
|
||||||
|
|
||||||
|
# 等待电机完成 (预估时间)
|
||||||
|
estimated_time = 15.0 / max(1, speed)
|
||||||
|
time.sleep(estimated_time + 0.5)
|
||||||
|
|
||||||
|
# 更新状态
|
||||||
|
last_level = current_level
|
||||||
|
|
||||||
|
# 轮询间隔
|
||||||
|
time.sleep(0.1)
|
||||||
|
finally:
|
||||||
|
# 恢复轮询线程
|
||||||
|
self.logger.info("Restarting polling thread...")
|
||||||
|
self._start_polling()
|
||||||
|
|
||||||
def execute_command_from_outer(self, command_dict: Dict[str, Any]) -> bool:
|
def execute_command_from_outer(self, command_dict: Dict[str, Any]) -> bool:
|
||||||
"""支持标准 JSON 指令调用"""
|
"""支持标准 JSON 指令调用"""
|
||||||
return super().execute_command_from_outer(command_dict)
|
return super().execute_command_from_outer(command_dict)
|
||||||
|
|||||||
379
unilabos/devices/separator/xkc_sensor.py
Normal file
379
unilabos/devices/separator/xkc_sensor.py
Normal file
@@ -0,0 +1,379 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
XKC RS485 液位传感器 (Modbus RTU)
|
||||||
|
|
||||||
|
说明:
|
||||||
|
1. 遵循 Modbus-RTU 协议。
|
||||||
|
2. 数据寄存器: 0x0001 (液位状态, 1=有液, 0=无液), 0x0002 (RSSI 信号强度)。
|
||||||
|
3. 地址寄存器: 0x0004 (可读写, 范围 1-254)。
|
||||||
|
4. 波特率寄存器: 0x0005 (可写, 代码表见 change_baudrate 方法)。
|
||||||
|
"""
|
||||||
|
|
||||||
|
import struct
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import logging
|
||||||
|
import serial
|
||||||
|
from typing import Optional, Dict, Any, List
|
||||||
|
|
||||||
|
from unilabos.device_comms.universal_driver import UniversalDriver
|
||||||
|
|
||||||
|
class TransportManager:
|
||||||
|
"""
|
||||||
|
统一通信管理类。
|
||||||
|
仅支持 串口 (Serial/有线) 连接。
|
||||||
|
"""
|
||||||
|
def __init__(self, port: str, baudrate: int = 9600, timeout: float = 3.0, logger=None):
|
||||||
|
self.port = port
|
||||||
|
self.baudrate = baudrate
|
||||||
|
self.timeout = timeout
|
||||||
|
self.logger = logger
|
||||||
|
self.lock = threading.RLock() # 线程锁,确保多设备共用一个连接时不冲突
|
||||||
|
|
||||||
|
self.serial = None
|
||||||
|
self._connect_serial()
|
||||||
|
|
||||||
|
def _connect_serial(self):
|
||||||
|
try:
|
||||||
|
self.serial = serial.Serial(
|
||||||
|
port=self.port,
|
||||||
|
baudrate=self.baudrate,
|
||||||
|
timeout=self.timeout
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
raise ConnectionError(f"Serial open failed: {e}")
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
"""关闭连接"""
|
||||||
|
if self.serial and self.serial.is_open:
|
||||||
|
self.serial.close()
|
||||||
|
|
||||||
|
def clear_buffer(self):
|
||||||
|
"""清空缓冲区 (Thread-safe)"""
|
||||||
|
with self.lock:
|
||||||
|
if self.serial:
|
||||||
|
self.serial.reset_input_buffer()
|
||||||
|
|
||||||
|
def write(self, data: bytes):
|
||||||
|
"""发送原始字节"""
|
||||||
|
with self.lock:
|
||||||
|
if self.serial:
|
||||||
|
self.serial.write(data)
|
||||||
|
|
||||||
|
def read(self, size: int) -> bytes:
|
||||||
|
"""读取指定长度字节"""
|
||||||
|
if self.serial:
|
||||||
|
return self.serial.read(size)
|
||||||
|
return b''
|
||||||
|
|
||||||
|
class XKCSensorDriver(UniversalDriver):
|
||||||
|
"""XKC RS485 液位传感器 (Modbus RTU)"""
|
||||||
|
|
||||||
|
def __init__(self, port: str, baudrate: int = 9600, device_id: int = 6,
|
||||||
|
threshold: int = 300, timeout: float = 3.0, debug: bool = False):
|
||||||
|
super().__init__()
|
||||||
|
self.port = port
|
||||||
|
self.baudrate = baudrate
|
||||||
|
self.device_id = device_id
|
||||||
|
self.threshold = threshold
|
||||||
|
self.timeout = timeout
|
||||||
|
self.debug = debug
|
||||||
|
self.level = False
|
||||||
|
self.rssi = 0
|
||||||
|
self.status = {"level": self.level, "rssi": self.rssi}
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.transport = TransportManager(port, baudrate, timeout, logger=self.logger)
|
||||||
|
self.logger.info(f"XKCSensorDriver connected to {port} (ID: {device_id})")
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error(f"Failed to connect XKCSensorDriver: {e}")
|
||||||
|
self.transport = None
|
||||||
|
|
||||||
|
# 启动背景轮询线程,确保 status 实时刷新
|
||||||
|
self._stop_event = threading.Event()
|
||||||
|
self._polling_thread = threading.Thread(
|
||||||
|
target=self._update_loop,
|
||||||
|
name=f"XKCPolling_{port}",
|
||||||
|
daemon=True
|
||||||
|
)
|
||||||
|
if self.transport:
|
||||||
|
self._polling_thread.start()
|
||||||
|
|
||||||
|
def _update_loop(self):
|
||||||
|
"""背景循环读取传感器数据"""
|
||||||
|
while not self._stop_event.is_set():
|
||||||
|
try:
|
||||||
|
self.read_level()
|
||||||
|
except Exception as e:
|
||||||
|
if self.debug:
|
||||||
|
self.logger.error(f"Polling error: {e}")
|
||||||
|
time.sleep(2.0) # 每2秒刷新一次数据
|
||||||
|
|
||||||
|
def _crc(self, data: bytes) -> bytes:
|
||||||
|
crc = 0xFFFF
|
||||||
|
for byte in data:
|
||||||
|
crc ^= byte
|
||||||
|
for _ in range(8):
|
||||||
|
if crc & 0x0001: crc = (crc >> 1) ^ 0xA001
|
||||||
|
else: crc >>= 1
|
||||||
|
return struct.pack('<H', crc)
|
||||||
|
|
||||||
|
def read_level(self) -> Optional[Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
读取液位。
|
||||||
|
返回: {'level': bool, 'rssi': int}
|
||||||
|
"""
|
||||||
|
if not self.transport:
|
||||||
|
return None
|
||||||
|
|
||||||
|
with self.transport.lock:
|
||||||
|
self.transport.clear_buffer()
|
||||||
|
# Modbus Read Registers: 01 03 00 01 00 02 CRC
|
||||||
|
payload = struct.pack('>HH', 0x0001, 0x0002)
|
||||||
|
msg = struct.pack('BB', self.device_id, 0x03) + payload
|
||||||
|
msg += self._crc(msg)
|
||||||
|
|
||||||
|
if self.debug:
|
||||||
|
self.logger.info(f"TX (ID {self.device_id}): {msg.hex().upper()}")
|
||||||
|
|
||||||
|
self.transport.write(msg)
|
||||||
|
|
||||||
|
# Read header
|
||||||
|
h = self.transport.read(3) # Addr, Func, Len
|
||||||
|
if self.debug:
|
||||||
|
self.logger.info(f"RX Header: {h.hex().upper()}")
|
||||||
|
|
||||||
|
if len(h) < 3: return None
|
||||||
|
length = h[2]
|
||||||
|
|
||||||
|
# Read body + CRC
|
||||||
|
body = self.transport.read(length + 2)
|
||||||
|
if self.debug:
|
||||||
|
self.logger.info(f"RX Body+CRC: {body.hex().upper()}")
|
||||||
|
if len(body) < length + 2:
|
||||||
|
# Firmware bug fix specific to some modules
|
||||||
|
if len(body) == 4 and length == 4:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
data = body[:-2]
|
||||||
|
# 根据手册说明:
|
||||||
|
# 寄存器 0x0001 (data[0:2]): 液位状态 (00 01 为有液, 00 00 为无液)
|
||||||
|
# 寄存器 0x0002 (data[2:4]): 信号强度 RSSI
|
||||||
|
|
||||||
|
hw_level = False
|
||||||
|
rssi = 0
|
||||||
|
|
||||||
|
if len(data) >= 4:
|
||||||
|
hw_level = ((data[0] << 8) | data[1]) == 1
|
||||||
|
rssi = (data[2] << 8) | data[3]
|
||||||
|
elif len(data) == 2:
|
||||||
|
# 兼容模式: 某些老固件可能只返回 1 个寄存器
|
||||||
|
rssi = (data[0] << 8) | data[1]
|
||||||
|
hw_level = rssi > self.threshold
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
# 最终判定: 优先使用硬件层级的 level 判定,但 RSSI 阈值逻辑作为补充/校验
|
||||||
|
# 注意: 如果用户显式设置了 THRESHOLD,我们可以在逻辑中做权衡
|
||||||
|
self.level = hw_level or (rssi > self.threshold)
|
||||||
|
self.rssi = rssi
|
||||||
|
result = {
|
||||||
|
'level': self.level,
|
||||||
|
'rssi': self.rssi
|
||||||
|
}
|
||||||
|
self.status = result
|
||||||
|
return result
|
||||||
|
|
||||||
|
def wait_level(self, target_state: bool, timeout: float = 60.0) -> bool:
|
||||||
|
"""
|
||||||
|
等待液位达到目标状态 (阻塞式)
|
||||||
|
"""
|
||||||
|
self.logger.info(f"Waiting for level: {target_state}")
|
||||||
|
start_time = time.time()
|
||||||
|
while (time.time() - start_time) < timeout:
|
||||||
|
res = self.read_level()
|
||||||
|
if res and res.get('level') == target_state:
|
||||||
|
return True
|
||||||
|
time.sleep(0.5)
|
||||||
|
self.logger.warning(f"Wait level timeout ({timeout}s)")
|
||||||
|
return False
|
||||||
|
|
||||||
|
def wait_for_liquid(self, target_state: bool, timeout: float = 120.0) -> bool:
|
||||||
|
"""
|
||||||
|
实时检测电导率(RSSI)并等待用户指定的“有液”或“无液”状态。
|
||||||
|
一旦检测到符合目标状态,立即返回。
|
||||||
|
|
||||||
|
Args:
|
||||||
|
target_state: True 为“有液”, False 为“无液”
|
||||||
|
timeout: 最大等待时间(秒)
|
||||||
|
"""
|
||||||
|
state_str = "有液" if target_state else "无液"
|
||||||
|
self.logger.info(f"开始实时检测电导率,等待状态: {state_str} (超时: {timeout}s)")
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
while (time.time() - start_time) < timeout:
|
||||||
|
res = self.read_level() # 内部已更新 self.level 和 self.rssi
|
||||||
|
if res:
|
||||||
|
current_level = res.get('level')
|
||||||
|
current_rssi = res.get('rssi')
|
||||||
|
if current_level == target_state:
|
||||||
|
self.logger.info(f"✅ 检测到目标状态: {state_str} (当前电导率/RSSI: {current_rssi})")
|
||||||
|
return True
|
||||||
|
|
||||||
|
if self.debug:
|
||||||
|
self.logger.debug(f"当前状态: {'有液' if current_level else '无液'}, RSSI: {current_rssi}")
|
||||||
|
|
||||||
|
time.sleep(0.2) # 高频采样
|
||||||
|
|
||||||
|
self.logger.warning(f"❌ 等待 {state_str} 状态超时 ({timeout}s)")
|
||||||
|
return False
|
||||||
|
|
||||||
|
def set_threshold(self, threshold: int):
|
||||||
|
"""设置液位判定阈值"""
|
||||||
|
self.threshold = int(threshold)
|
||||||
|
self.logger.info(f"Threshold updated to: {self.threshold}")
|
||||||
|
|
||||||
|
def change_device_id(self, new_id: int) -> bool:
|
||||||
|
"""
|
||||||
|
修改设备的 Modbus 从站地址。
|
||||||
|
寄存器: 0x0004, 功能码: 0x06
|
||||||
|
"""
|
||||||
|
if not (1 <= new_id <= 254):
|
||||||
|
self.logger.error(f"Invalid device ID: {new_id}. Must be 1-254.")
|
||||||
|
return False
|
||||||
|
|
||||||
|
self.logger.info(f"Changing device ID from {self.device_id} to {new_id}")
|
||||||
|
success = self._write_single_register(0x0004, new_id)
|
||||||
|
if success:
|
||||||
|
self.device_id = new_id # 更新内存中的地址
|
||||||
|
self.logger.info(f"Device ID update command sent successfully (target {new_id}).")
|
||||||
|
return success
|
||||||
|
|
||||||
|
def change_baudrate(self, baud_code: int) -> bool:
|
||||||
|
"""
|
||||||
|
更改通讯波特率 (寄存器: 0x0005)。
|
||||||
|
设置成功后传感器 LED 会闪烁,通常无数据返回。
|
||||||
|
|
||||||
|
波特率代码对照表 (16进制):
|
||||||
|
05: 2400
|
||||||
|
06: 4800
|
||||||
|
07: 9600 (默认)
|
||||||
|
08: 14400
|
||||||
|
09: 19200
|
||||||
|
0A: 28800
|
||||||
|
0C: 57600
|
||||||
|
0D: 115200
|
||||||
|
0E: 128000
|
||||||
|
0F: 256000
|
||||||
|
"""
|
||||||
|
self.logger.info(f"Sending baudrate change command (Code: {baud_code:02X})")
|
||||||
|
# 写入寄存器 0x0005
|
||||||
|
self._write_single_register(0x0005, baud_code)
|
||||||
|
self.logger.info("Baudrate change command executed. Device LED should flash. Please update connection settings.")
|
||||||
|
return True
|
||||||
|
|
||||||
|
def factory_reset(self) -> bool:
|
||||||
|
"""
|
||||||
|
恢复出厂设置 (通过广播地址 FF)。
|
||||||
|
设置地址为 01,逻辑为向 0x0004 写入 0x0002
|
||||||
|
"""
|
||||||
|
self.logger.info("Sending factory reset command via broadcast address FF...")
|
||||||
|
# 广播指令通常无回显
|
||||||
|
self._write_single_register(0x0004, 0x0002, slave_id=0xFF)
|
||||||
|
self.logger.info("Factory reset command sent. Device address should be 01 now.")
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _write_single_register(self, reg_addr: int, value: int, slave_id: Optional[int] = None) -> bool:
|
||||||
|
"""内部辅助函数: Modbus 功能码 06 写单个寄存器"""
|
||||||
|
if not self.transport: return False
|
||||||
|
|
||||||
|
target_id = slave_id if slave_id is not None else self.device_id
|
||||||
|
msg = struct.pack('BBHH', target_id, 0x06, reg_addr, value)
|
||||||
|
msg += self._crc(msg)
|
||||||
|
|
||||||
|
with self.transport.lock:
|
||||||
|
self.transport.clear_buffer()
|
||||||
|
if self.debug:
|
||||||
|
self.logger.info(f"TX Write (Reg {reg_addr:#06x}): {msg.hex().upper()}")
|
||||||
|
|
||||||
|
self.transport.write(msg)
|
||||||
|
|
||||||
|
# 广播地址、波特率修改或厂家特定指令可能无回显
|
||||||
|
if target_id == 0xFF or reg_addr == 0x0005:
|
||||||
|
time.sleep(0.5)
|
||||||
|
return True
|
||||||
|
|
||||||
|
# 等待返回 (正常应返回相同报文)
|
||||||
|
resp = self.transport.read(len(msg))
|
||||||
|
if self.debug:
|
||||||
|
self.logger.info(f"RX Write Response: {resp.hex().upper()}")
|
||||||
|
|
||||||
|
return resp == msg
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
if self.transport:
|
||||||
|
self.transport.close()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# 快速实例化测试
|
||||||
|
import logging
|
||||||
|
# 减少冗余日志,仅显示重要信息
|
||||||
|
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
|
||||||
|
|
||||||
|
# 硬件配置 (根据实际情况修改)
|
||||||
|
TEST_PORT = "/dev/tty.usbserial-3110"
|
||||||
|
SLAVE_ID = 1
|
||||||
|
THRESHOLD = 300
|
||||||
|
|
||||||
|
print("\n" + "="*50)
|
||||||
|
print(f" XKC RS485 传感器独立测试程序")
|
||||||
|
print(f" 端口: {TEST_PORT} | 地址: {SLAVE_ID} | 阈值: {THRESHOLD}")
|
||||||
|
print("="*50)
|
||||||
|
|
||||||
|
sensor = XKCSensorDriver(port=TEST_PORT, device_id=SLAVE_ID, threshold=THRESHOLD, debug=False)
|
||||||
|
|
||||||
|
try:
|
||||||
|
if sensor.transport:
|
||||||
|
print(f"\n开始实时连续采样测试 (持续 15 秒)...")
|
||||||
|
print(f"按 Ctrl+C 可提前停止\n")
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
duration = 15
|
||||||
|
count = 0
|
||||||
|
|
||||||
|
while time.time() - start_time < duration:
|
||||||
|
count += 1
|
||||||
|
res = sensor.read_level()
|
||||||
|
if res:
|
||||||
|
rssi = res['rssi']
|
||||||
|
level = res['level']
|
||||||
|
status_str = "【有液】" if level else "【无液】"
|
||||||
|
# 使用 \r 实现单行刷新显示 (或者不刷,直接打印历史)
|
||||||
|
# 为了方便查看变化,我们直接打印
|
||||||
|
elapsed = time.time() - start_time
|
||||||
|
print(f" [{elapsed:4.1f}s] 采样 {count:<3}: 电导率/RSSI = {rssi:<5} | 判定结果: {status_str}")
|
||||||
|
else:
|
||||||
|
print(f" [{time.time()-start_time:4.1f}s] 采样 {count:<3}: 通信失败 (无响应)")
|
||||||
|
|
||||||
|
time.sleep(0.5) # 每秒采样 2 次
|
||||||
|
|
||||||
|
print(f"\n--- 15 秒采样测试完成 (总计 {count} 次) ---")
|
||||||
|
|
||||||
|
# [3] 测试动态修改阈值
|
||||||
|
print(f"\n[3] 动态修改阈值演示...")
|
||||||
|
new_threshold = 400
|
||||||
|
sensor.set_threshold(new_threshold)
|
||||||
|
res = sensor.read_level()
|
||||||
|
if res:
|
||||||
|
print(f" 采样 (当前阈值={new_threshold}): 电导率/RSSI = {res['rssi']:<5} | 判定结果: {'【有液】' if res['level'] else '【无液】'}")
|
||||||
|
sensor.set_threshold(THRESHOLD) # 还原
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\n[!] 用户中断测试")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\n[!] 测试运行出错: {e}")
|
||||||
|
finally:
|
||||||
|
sensor.close()
|
||||||
|
print("\n--- 测试程序已退出 ---\n")
|
||||||
@@ -15,35 +15,35 @@ class VirtualPumpMode(Enum):
|
|||||||
|
|
||||||
class VirtualTransferPump:
|
class VirtualTransferPump:
|
||||||
"""虚拟转移泵类 - 模拟泵的基本功能,无需实际硬件 🚰"""
|
"""虚拟转移泵类 - 模拟泵的基本功能,无需实际硬件 🚰"""
|
||||||
|
|
||||||
_ros_node: BaseROS2DeviceNode
|
_ros_node: BaseROS2DeviceNode
|
||||||
|
|
||||||
def __init__(self, device_id: str = None, config: dict = None, **kwargs):
|
def __init__(self, device_id: str = None, config: dict = None, **kwargs):
|
||||||
"""
|
"""
|
||||||
初始化虚拟转移泵
|
初始化虚拟转移泵
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
device_id: 设备ID
|
device_id: 设备ID
|
||||||
config: 配置字典,包含max_volume, port等参数
|
config: 配置字典,包含max_volume, port等参数
|
||||||
**kwargs: 其他参数,确保兼容性
|
**kwargs: 其他参数,确保兼容性
|
||||||
"""
|
"""
|
||||||
self.device_id = device_id or "virtual_transfer_pump"
|
self.device_id = device_id or "virtual_transfer_pump"
|
||||||
|
|
||||||
# 从config或kwargs中获取参数,确保类型正确
|
# 从config或kwargs中获取参数,确保类型正确
|
||||||
if config:
|
if config:
|
||||||
self.max_volume = float(config.get('max_volume', 25.0))
|
self.max_volume = float(config.get("max_volume", 25.0))
|
||||||
self.port = config.get('port', 'VIRTUAL')
|
self.port = config.get("port", "VIRTUAL")
|
||||||
else:
|
else:
|
||||||
self.max_volume = float(kwargs.get('max_volume', 25.0))
|
self.max_volume = float(kwargs.get("max_volume", 25.0))
|
||||||
self.port = kwargs.get('port', 'VIRTUAL')
|
self.port = kwargs.get("port", "VIRTUAL")
|
||||||
|
|
||||||
self._transfer_rate = float(kwargs.get('transfer_rate', 0))
|
self._transfer_rate = float(kwargs.get("transfer_rate", 0))
|
||||||
self.mode = kwargs.get('mode', VirtualPumpMode.Normal)
|
self.mode = kwargs.get("mode", VirtualPumpMode.Normal)
|
||||||
|
|
||||||
# 状态变量 - 确保都是正确类型
|
# 状态变量 - 确保都是正确类型
|
||||||
self._status = "Idle"
|
self._status = "Idle"
|
||||||
self._position = 0.0 # float
|
self._position = 0.0 # float
|
||||||
self._max_velocity = 5.0 # float
|
self._max_velocity = 5.0 # float
|
||||||
self._current_volume = 0.0 # float
|
self._current_volume = 0.0 # float
|
||||||
|
|
||||||
# 🚀 新增:快速模式设置 - 大幅缩短执行时间
|
# 🚀 新增:快速模式设置 - 大幅缩短执行时间
|
||||||
@@ -52,14 +52,16 @@ class VirtualTransferPump:
|
|||||||
self._fast_dispense_time = 1.0 # 快速喷射时间(秒)
|
self._fast_dispense_time = 1.0 # 快速喷射时间(秒)
|
||||||
|
|
||||||
self.logger = logging.getLogger(f"VirtualTransferPump.{self.device_id}")
|
self.logger = logging.getLogger(f"VirtualTransferPump.{self.device_id}")
|
||||||
|
|
||||||
print(f"🚰 === 虚拟转移泵 {self.device_id} 已创建 === ✨")
|
print(f"🚰 === 虚拟转移泵 {self.device_id} 已创建 === ✨")
|
||||||
print(f"💨 快速模式: {'启用' if self._fast_mode else '禁用'} | 移动时间: {self._fast_move_time}s | 喷射时间: {self._fast_dispense_time}s")
|
print(
|
||||||
|
f"💨 快速模式: {'启用' if self._fast_mode else '禁用'} | 移动时间: {self._fast_move_time}s | 喷射时间: {self._fast_dispense_time}s"
|
||||||
|
)
|
||||||
print(f"📊 最大容量: {self.max_volume}mL | 端口: {self.port}")
|
print(f"📊 最大容量: {self.max_volume}mL | 端口: {self.port}")
|
||||||
|
|
||||||
def post_init(self, ros_node: BaseROS2DeviceNode):
|
def post_init(self, ros_node: BaseROS2DeviceNode):
|
||||||
self._ros_node = ros_node
|
self._ros_node = ros_node
|
||||||
|
|
||||||
async def initialize(self) -> bool:
|
async def initialize(self) -> bool:
|
||||||
"""初始化虚拟泵 🚀"""
|
"""初始化虚拟泵 🚀"""
|
||||||
self.logger.info(f"🔧 初始化虚拟转移泵 {self.device_id} ✨")
|
self.logger.info(f"🔧 初始化虚拟转移泵 {self.device_id} ✨")
|
||||||
@@ -68,33 +70,33 @@ class VirtualTransferPump:
|
|||||||
self._current_volume = 0.0
|
self._current_volume = 0.0
|
||||||
self.logger.info(f"✅ 转移泵 {self.device_id} 初始化完成 🚰")
|
self.logger.info(f"✅ 转移泵 {self.device_id} 初始化完成 🚰")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
async def cleanup(self) -> bool:
|
async def cleanup(self) -> bool:
|
||||||
"""清理虚拟泵 🧹"""
|
"""清理虚拟泵 🧹"""
|
||||||
self.logger.info(f"🧹 清理虚拟转移泵 {self.device_id} 🔚")
|
self.logger.info(f"🧹 清理虚拟转移泵 {self.device_id} 🔚")
|
||||||
self._status = "Idle"
|
self._status = "Idle"
|
||||||
self.logger.info(f"✅ 转移泵 {self.device_id} 清理完成 💤")
|
self.logger.info(f"✅ 转移泵 {self.device_id} 清理完成 💤")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# 基本属性
|
# 基本属性
|
||||||
@property
|
@property
|
||||||
def status(self) -> str:
|
def status(self) -> str:
|
||||||
return self._status
|
return self._status
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def position(self) -> float:
|
def position(self) -> float:
|
||||||
"""当前柱塞位置 (ml) 📍"""
|
"""当前柱塞位置 (ml) 📍"""
|
||||||
return self._position
|
return self._position
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def current_volume(self) -> float:
|
def current_volume(self) -> float:
|
||||||
"""当前注射器中的体积 (ml) 💧"""
|
"""当前注射器中的体积 (ml) 💧"""
|
||||||
return self._current_volume
|
return self._current_volume
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def max_velocity(self) -> float:
|
def max_velocity(self) -> float:
|
||||||
return self._max_velocity
|
return self._max_velocity
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def transfer_rate(self) -> float:
|
def transfer_rate(self) -> float:
|
||||||
return self._transfer_rate
|
return self._transfer_rate
|
||||||
@@ -103,17 +105,17 @@ class VirtualTransferPump:
|
|||||||
"""设置最大速度 (ml/s) 🌊"""
|
"""设置最大速度 (ml/s) 🌊"""
|
||||||
self._max_velocity = max(0.1, min(50.0, velocity)) # 限制在合理范围内
|
self._max_velocity = max(0.1, min(50.0, velocity)) # 限制在合理范围内
|
||||||
self.logger.info(f"🌊 设置最大速度为 {self._max_velocity} mL/s")
|
self.logger.info(f"🌊 设置最大速度为 {self._max_velocity} mL/s")
|
||||||
|
|
||||||
def get_status(self) -> str:
|
def get_status(self) -> str:
|
||||||
"""获取泵状态 📋"""
|
"""获取泵状态 📋"""
|
||||||
return self._status
|
return self._status
|
||||||
|
|
||||||
async def _simulate_operation(self, duration: float):
|
async def _simulate_operation(self, duration: float):
|
||||||
"""模拟操作延时 ⏱️"""
|
"""模拟操作延时 ⏱️"""
|
||||||
self._status = "Busy"
|
self._status = "Busy"
|
||||||
await self._ros_node.sleep(duration)
|
await self._ros_node.sleep(duration)
|
||||||
self._status = "Idle"
|
self._status = "Idle"
|
||||||
|
|
||||||
def _calculate_duration(self, volume: float, velocity: float = None) -> float:
|
def _calculate_duration(self, volume: float, velocity: float = None) -> float:
|
||||||
"""
|
"""
|
||||||
计算操作持续时间 ⏰
|
计算操作持续时间 ⏰
|
||||||
@@ -121,10 +123,10 @@ class VirtualTransferPump:
|
|||||||
"""
|
"""
|
||||||
if velocity is None:
|
if velocity is None:
|
||||||
velocity = self._max_velocity
|
velocity = self._max_velocity
|
||||||
|
|
||||||
# 📊 计算理论时间(用于日志显示)
|
# 📊 计算理论时间(用于日志显示)
|
||||||
theoretical_duration = abs(volume) / velocity
|
theoretical_duration = abs(volume) / velocity
|
||||||
|
|
||||||
# 🚀 如果启用快速模式,使用固定的快速时间
|
# 🚀 如果启用快速模式,使用固定的快速时间
|
||||||
if self._fast_mode:
|
if self._fast_mode:
|
||||||
# 根据操作类型选择快速时间
|
# 根据操作类型选择快速时间
|
||||||
@@ -132,13 +134,13 @@ class VirtualTransferPump:
|
|||||||
actual_duration = self._fast_move_time
|
actual_duration = self._fast_move_time
|
||||||
else: # 很小的操作
|
else: # 很小的操作
|
||||||
actual_duration = 0.5
|
actual_duration = 0.5
|
||||||
|
|
||||||
self.logger.debug(f"⚡ 快速模式: 理论时间 {theoretical_duration:.2f}s → 实际时间 {actual_duration:.2f}s")
|
self.logger.debug(f"⚡ 快速模式: 理论时间 {theoretical_duration:.2f}s → 实际时间 {actual_duration:.2f}s")
|
||||||
return actual_duration
|
return actual_duration
|
||||||
else:
|
else:
|
||||||
# 正常模式使用理论时间
|
# 正常模式使用理论时间
|
||||||
return theoretical_duration
|
return theoretical_duration
|
||||||
|
|
||||||
def _calculate_display_duration(self, volume: float, velocity: float = None) -> float:
|
def _calculate_display_duration(self, volume: float, velocity: float = None) -> float:
|
||||||
"""
|
"""
|
||||||
计算显示用的持续时间(用于日志) 📊
|
计算显示用的持续时间(用于日志) 📊
|
||||||
@@ -147,16 +149,16 @@ class VirtualTransferPump:
|
|||||||
if velocity is None:
|
if velocity is None:
|
||||||
velocity = self._max_velocity
|
velocity = self._max_velocity
|
||||||
return abs(volume) / velocity
|
return abs(volume) / velocity
|
||||||
|
|
||||||
# 新的set_position方法 - 专门用于SetPumpPosition动作
|
# 新的set_position方法 - 专门用于SetPumpPosition动作
|
||||||
async def set_position(self, position: float, max_velocity: float = None):
|
async def set_position(self, position: float, max_velocity: float = None):
|
||||||
"""
|
"""
|
||||||
移动到绝对位置 - 专门用于SetPumpPosition动作 🎯
|
移动到绝对位置 - 专门用于SetPumpPosition动作 🎯
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
position (float): 目标位置 (ml)
|
position (float): 目标位置 (ml)
|
||||||
max_velocity (float): 移动速度 (ml/s)
|
max_velocity (float): 移动速度 (ml/s)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
dict: 符合SetPumpPosition.action定义的结果
|
dict: 符合SetPumpPosition.action定义的结果
|
||||||
"""
|
"""
|
||||||
@@ -164,19 +166,19 @@ class VirtualTransferPump:
|
|||||||
# 验证并转换参数
|
# 验证并转换参数
|
||||||
target_position = float(position)
|
target_position = float(position)
|
||||||
velocity = float(max_velocity) if max_velocity is not None else self._max_velocity
|
velocity = float(max_velocity) if max_velocity is not None else self._max_velocity
|
||||||
|
|
||||||
# 限制位置在有效范围内
|
# 限制位置在有效范围内
|
||||||
target_position = max(0.0, min(float(self.max_volume), target_position))
|
target_position = max(0.0, min(float(self.max_volume), target_position))
|
||||||
|
|
||||||
# 计算移动距离
|
# 计算移动距离
|
||||||
volume_to_move = abs(target_position - self._position)
|
volume_to_move = abs(target_position - self._position)
|
||||||
|
|
||||||
# 📊 计算显示用的时间(用于日志)
|
# 📊 计算显示用的时间(用于日志)
|
||||||
display_duration = self._calculate_display_duration(volume_to_move, velocity)
|
display_duration = self._calculate_display_duration(volume_to_move, velocity)
|
||||||
|
|
||||||
# ⚡ 计算实际执行时间(快速模式)
|
# ⚡ 计算实际执行时间(快速模式)
|
||||||
actual_duration = self._calculate_duration(volume_to_move, velocity)
|
actual_duration = self._calculate_duration(volume_to_move, velocity)
|
||||||
|
|
||||||
# 🎯 确定操作类型和emoji
|
# 🎯 确定操作类型和emoji
|
||||||
if target_position > self._position:
|
if target_position > self._position:
|
||||||
operation_type = "吸液"
|
operation_type = "吸液"
|
||||||
@@ -187,28 +189,34 @@ class VirtualTransferPump:
|
|||||||
else:
|
else:
|
||||||
operation_type = "保持"
|
operation_type = "保持"
|
||||||
operation_emoji = "📍"
|
operation_emoji = "📍"
|
||||||
|
|
||||||
self.logger.info(f"🎯 SET_POSITION: {operation_type} {operation_emoji}")
|
self.logger.info(f"🎯 SET_POSITION: {operation_type} {operation_emoji}")
|
||||||
self.logger.info(f" 📍 位置: {self._position:.2f}mL → {target_position:.2f}mL (移动 {volume_to_move:.2f}mL)")
|
self.logger.info(
|
||||||
|
f" 📍 位置: {self._position:.2f}mL → {target_position:.2f}mL (移动 {volume_to_move:.2f}mL)"
|
||||||
|
)
|
||||||
self.logger.info(f" 🌊 速度: {velocity:.2f} mL/s")
|
self.logger.info(f" 🌊 速度: {velocity:.2f} mL/s")
|
||||||
self.logger.info(f" ⏰ 预计时间: {display_duration:.2f}s")
|
self.logger.info(f" ⏰ 预计时间: {display_duration:.2f}s")
|
||||||
|
|
||||||
if self._fast_mode:
|
if self._fast_mode:
|
||||||
self.logger.info(f" ⚡ 快速模式: 实际用时 {actual_duration:.2f}s")
|
self.logger.info(f" ⚡ 快速模式: 实际用时 {actual_duration:.2f}s")
|
||||||
|
|
||||||
# 🚀 模拟移动过程
|
# 🚀 模拟移动过程
|
||||||
if volume_to_move > 0.01: # 只有当移动距离足够大时才显示进度
|
if volume_to_move > 0.01: # 只有当移动距离足够大时才显示进度
|
||||||
start_position = self._position
|
start_position = self._position
|
||||||
steps = 5 if actual_duration > 0.5 else 2 # 根据实际时间调整步数
|
steps = 5 if actual_duration > 0.5 else 2 # 根据实际时间调整步数
|
||||||
step_duration = actual_duration / steps
|
step_duration = actual_duration / steps
|
||||||
|
|
||||||
self.logger.info(f"🚀 开始{operation_type}... {operation_emoji}")
|
self.logger.info(f"🚀 开始{operation_type}... {operation_emoji}")
|
||||||
|
|
||||||
for i in range(steps + 1):
|
for i in range(steps + 1):
|
||||||
# 计算当前位置和进度
|
# 计算当前位置和进度
|
||||||
progress = (i / steps) * 100 if steps > 0 else 100
|
progress = (i / steps) * 100 if steps > 0 else 100
|
||||||
current_pos = start_position + (target_position - start_position) * (i / steps) if steps > 0 else target_position
|
current_pos = (
|
||||||
|
start_position + (target_position - start_position) * (i / steps)
|
||||||
|
if steps > 0
|
||||||
|
else target_position
|
||||||
|
)
|
||||||
|
|
||||||
# 更新状态
|
# 更新状态
|
||||||
if i < steps:
|
if i < steps:
|
||||||
self._status = f"{operation_type}中"
|
self._status = f"{operation_type}中"
|
||||||
@@ -216,10 +224,10 @@ class VirtualTransferPump:
|
|||||||
else:
|
else:
|
||||||
self._status = "Idle"
|
self._status = "Idle"
|
||||||
status_emoji = "✅"
|
status_emoji = "✅"
|
||||||
|
|
||||||
self._position = current_pos
|
self._position = current_pos
|
||||||
self._current_volume = current_pos
|
self._current_volume = current_pos
|
||||||
|
|
||||||
# 显示进度(每25%或最后一步)
|
# 显示进度(每25%或最后一步)
|
||||||
if i == 0:
|
if i == 0:
|
||||||
self.logger.debug(f" 🔄 {operation_type}开始: {progress:.0f}%")
|
self.logger.debug(f" 🔄 {operation_type}开始: {progress:.0f}%")
|
||||||
@@ -227,7 +235,7 @@ class VirtualTransferPump:
|
|||||||
self.logger.debug(f" 🔄 {operation_type}进度: {progress:.0f}%")
|
self.logger.debug(f" 🔄 {operation_type}进度: {progress:.0f}%")
|
||||||
elif i == steps:
|
elif i == steps:
|
||||||
self.logger.info(f" ✅ {operation_type}完成: {progress:.0f}% | 当前位置: {current_pos:.2f}mL")
|
self.logger.info(f" ✅ {operation_type}完成: {progress:.0f}% | 当前位置: {current_pos:.2f}mL")
|
||||||
|
|
||||||
# 等待一小步时间
|
# 等待一小步时间
|
||||||
if i < steps and step_duration > 0:
|
if i < steps and step_duration > 0:
|
||||||
await self._ros_node.sleep(step_duration)
|
await self._ros_node.sleep(step_duration)
|
||||||
@@ -236,25 +244,27 @@ class VirtualTransferPump:
|
|||||||
self._position = target_position
|
self._position = target_position
|
||||||
self._current_volume = target_position
|
self._current_volume = target_position
|
||||||
self.logger.info(f" 📍 微调完成: {target_position:.2f}mL")
|
self.logger.info(f" 📍 微调完成: {target_position:.2f}mL")
|
||||||
|
|
||||||
# 确保最终位置准确
|
# 确保最终位置准确
|
||||||
self._position = target_position
|
self._position = target_position
|
||||||
self._current_volume = target_position
|
self._current_volume = target_position
|
||||||
self._status = "Idle"
|
self._status = "Idle"
|
||||||
|
|
||||||
# 📊 最终状态日志
|
# 📊 最终状态日志
|
||||||
if volume_to_move > 0.01:
|
if volume_to_move > 0.01:
|
||||||
self.logger.info(f"🎉 SET_POSITION 完成! 📍 最终位置: {self._position:.2f}mL | 💧 当前体积: {self._current_volume:.2f}mL")
|
self.logger.info(
|
||||||
|
f"🎉 SET_POSITION 完成! 📍 最终位置: {self._position:.2f}mL | 💧 当前体积: {self._current_volume:.2f}mL"
|
||||||
|
)
|
||||||
|
|
||||||
# 返回符合action定义的结果
|
# 返回符合action定义的结果
|
||||||
return {
|
return {
|
||||||
"success": True,
|
"success": True,
|
||||||
"message": f"✅ 成功移动到位置 {self._position:.2f}mL ({operation_type})",
|
"message": f"✅ 成功移动到位置 {self._position:.2f}mL ({operation_type})",
|
||||||
"final_position": self._position,
|
"final_position": self._position,
|
||||||
"final_volume": self._current_volume,
|
"final_volume": self._current_volume,
|
||||||
"operation_type": operation_type
|
"operation_type": operation_type,
|
||||||
}
|
}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
error_msg = f"❌ 设置位置失败: {str(e)}"
|
error_msg = f"❌ 设置位置失败: {str(e)}"
|
||||||
self.logger.error(error_msg)
|
self.logger.error(error_msg)
|
||||||
@@ -262,134 +272,136 @@ class VirtualTransferPump:
|
|||||||
"success": False,
|
"success": False,
|
||||||
"message": error_msg,
|
"message": error_msg,
|
||||||
"final_position": self._position,
|
"final_position": self._position,
|
||||||
"final_volume": self._current_volume
|
"final_volume": self._current_volume,
|
||||||
}
|
}
|
||||||
|
|
||||||
# 其他泵操作方法
|
# 其他泵操作方法
|
||||||
async def pull_plunger(self, volume: float, velocity: float = None):
|
async def pull_plunger(self, volume: float, velocity: float = None):
|
||||||
"""
|
"""
|
||||||
拉取柱塞(吸液) 📥
|
拉取柱塞(吸液) 📥
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
volume (float): 要拉取的体积 (ml)
|
volume (float): 要拉取的体积 (ml)
|
||||||
velocity (float): 拉取速度 (ml/s)
|
velocity (float): 拉取速度 (ml/s)
|
||||||
"""
|
"""
|
||||||
new_position = min(self.max_volume, self._position + volume)
|
new_position = min(self.max_volume, self._position + volume)
|
||||||
actual_volume = new_position - self._position
|
actual_volume = new_position - self._position
|
||||||
|
|
||||||
if actual_volume <= 0:
|
if actual_volume <= 0:
|
||||||
self.logger.warning("⚠️ 无法吸液 - 已达到最大容量")
|
self.logger.warning("⚠️ 无法吸液 - 已达到最大容量")
|
||||||
return
|
return
|
||||||
|
|
||||||
display_duration = self._calculate_display_duration(actual_volume, velocity)
|
display_duration = self._calculate_display_duration(actual_volume, velocity)
|
||||||
actual_duration = self._calculate_duration(actual_volume, velocity)
|
actual_duration = self._calculate_duration(actual_volume, velocity)
|
||||||
|
|
||||||
self.logger.info(f"📥 开始吸液: {actual_volume:.2f}mL")
|
self.logger.info(f"📥 开始吸液: {actual_volume:.2f}mL")
|
||||||
self.logger.info(f" 📍 位置: {self._position:.2f}mL → {new_position:.2f}mL")
|
self.logger.info(f" 📍 位置: {self._position:.2f}mL → {new_position:.2f}mL")
|
||||||
self.logger.info(f" ⏰ 预计时间: {display_duration:.2f}s")
|
self.logger.info(f" ⏰ 预计时间: {display_duration:.2f}s")
|
||||||
|
|
||||||
if self._fast_mode:
|
if self._fast_mode:
|
||||||
self.logger.info(f" ⚡ 快速模式: 实际用时 {actual_duration:.2f}s")
|
self.logger.info(f" ⚡ 快速模式: 实际用时 {actual_duration:.2f}s")
|
||||||
|
|
||||||
await self._simulate_operation(actual_duration)
|
await self._simulate_operation(actual_duration)
|
||||||
|
|
||||||
self._position = new_position
|
self._position = new_position
|
||||||
self._current_volume = new_position
|
self._current_volume = new_position
|
||||||
|
|
||||||
self.logger.info(f"✅ 吸液完成: {actual_volume:.2f}mL | 💧 当前体积: {self._current_volume:.2f}mL")
|
self.logger.info(f"✅ 吸液完成: {actual_volume:.2f}mL | 💧 当前体积: {self._current_volume:.2f}mL")
|
||||||
|
|
||||||
async def push_plunger(self, volume: float, velocity: float = None):
|
async def push_plunger(self, volume: float, velocity: float = None):
|
||||||
"""
|
"""
|
||||||
推出柱塞(排液) 📤
|
推出柱塞(排液) 📤
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
volume (float): 要推出的体积 (ml)
|
volume (float): 要推出的体积 (ml)
|
||||||
velocity (float): 推出速度 (ml/s)
|
velocity (float): 推出速度 (ml/s)
|
||||||
"""
|
"""
|
||||||
new_position = max(0, self._position - volume)
|
new_position = max(0, self._position - volume)
|
||||||
actual_volume = self._position - new_position
|
actual_volume = self._position - new_position
|
||||||
|
|
||||||
if actual_volume <= 0:
|
if actual_volume <= 0:
|
||||||
self.logger.warning("⚠️ 无法排液 - 已达到最小容量")
|
self.logger.warning("⚠️ 无法排液 - 已达到最小容量")
|
||||||
return
|
return
|
||||||
|
|
||||||
display_duration = self._calculate_display_duration(actual_volume, velocity)
|
display_duration = self._calculate_display_duration(actual_volume, velocity)
|
||||||
actual_duration = self._calculate_duration(actual_volume, velocity)
|
actual_duration = self._calculate_duration(actual_volume, velocity)
|
||||||
|
|
||||||
self.logger.info(f"📤 开始排液: {actual_volume:.2f}mL")
|
self.logger.info(f"📤 开始排液: {actual_volume:.2f}mL")
|
||||||
self.logger.info(f" 📍 位置: {self._position:.2f}mL → {new_position:.2f}mL")
|
self.logger.info(f" 📍 位置: {self._position:.2f}mL → {new_position:.2f}mL")
|
||||||
self.logger.info(f" ⏰ 预计时间: {display_duration:.2f}s")
|
self.logger.info(f" ⏰ 预计时间: {display_duration:.2f}s")
|
||||||
|
|
||||||
if self._fast_mode:
|
if self._fast_mode:
|
||||||
self.logger.info(f" ⚡ 快速模式: 实际用时 {actual_duration:.2f}s")
|
self.logger.info(f" ⚡ 快速模式: 实际用时 {actual_duration:.2f}s")
|
||||||
|
|
||||||
await self._simulate_operation(actual_duration)
|
await self._simulate_operation(actual_duration)
|
||||||
|
|
||||||
self._position = new_position
|
self._position = new_position
|
||||||
self._current_volume = new_position
|
self._current_volume = new_position
|
||||||
|
|
||||||
self.logger.info(f"✅ 排液完成: {actual_volume:.2f}mL | 💧 当前体积: {self._current_volume:.2f}mL")
|
self.logger.info(f"✅ 排液完成: {actual_volume:.2f}mL | 💧 当前体积: {self._current_volume:.2f}mL")
|
||||||
|
|
||||||
# 便捷操作方法
|
# 便捷操作方法
|
||||||
async def aspirate(self, volume: float, velocity: float = None):
|
async def aspirate(self, volume: float, velocity: float = None):
|
||||||
"""吸液操作 📥"""
|
"""吸液操作 📥"""
|
||||||
await self.pull_plunger(volume, velocity)
|
await self.pull_plunger(volume, velocity)
|
||||||
|
|
||||||
async def dispense(self, volume: float, velocity: float = None):
|
async def dispense(self, volume: float, velocity: float = None):
|
||||||
"""排液操作 📤"""
|
"""排液操作 📤"""
|
||||||
await self.push_plunger(volume, velocity)
|
await self.push_plunger(volume, velocity)
|
||||||
|
|
||||||
async def transfer(self, volume: float, aspirate_velocity: float = None, dispense_velocity: float = None):
|
async def transfer(self, volume: float, aspirate_velocity: float = None, dispense_velocity: float = None):
|
||||||
"""转移操作(先吸后排) 🔄"""
|
"""转移操作(先吸后排) 🔄"""
|
||||||
self.logger.info(f"🔄 开始转移操作: {volume:.2f}mL")
|
self.logger.info(f"🔄 开始转移操作: {volume:.2f}mL")
|
||||||
|
|
||||||
# 吸液
|
# 吸液
|
||||||
await self.aspirate(volume, aspirate_velocity)
|
await self.aspirate(volume, aspirate_velocity)
|
||||||
|
|
||||||
# 短暂停顿
|
# 短暂停顿
|
||||||
self.logger.debug("⏸️ 短暂停顿...")
|
self.logger.debug("⏸️ 短暂停顿...")
|
||||||
await self._ros_node.sleep(0.1)
|
await self._ros_node.sleep(0.1)
|
||||||
|
|
||||||
# 排液
|
# 排液
|
||||||
await self.dispense(volume, dispense_velocity)
|
await self.dispense(volume, dispense_velocity)
|
||||||
|
|
||||||
async def empty_syringe(self, velocity: float = None):
|
async def empty_syringe(self, velocity: float = None):
|
||||||
"""清空注射器"""
|
"""清空注射器"""
|
||||||
await self.set_position(0, velocity)
|
await self.set_position(0, velocity)
|
||||||
|
|
||||||
async def fill_syringe(self, velocity: float = None):
|
async def fill_syringe(self, velocity: float = None):
|
||||||
"""充满注射器"""
|
"""充满注射器"""
|
||||||
await self.set_position(self.max_volume, velocity)
|
await self.set_position(self.max_volume, velocity)
|
||||||
|
|
||||||
async def stop_operation(self):
|
async def stop_operation(self):
|
||||||
"""停止当前操作"""
|
"""停止当前操作"""
|
||||||
self._status = "Idle"
|
self._status = "Idle"
|
||||||
self.logger.info("Operation stopped")
|
self.logger.info("Operation stopped")
|
||||||
|
|
||||||
# 状态查询方法
|
# 状态查询方法
|
||||||
def get_position(self) -> float:
|
def get_position(self) -> float:
|
||||||
"""获取当前位置"""
|
"""获取当前位置"""
|
||||||
return self._position
|
return self._position
|
||||||
|
|
||||||
def get_current_volume(self) -> float:
|
def get_current_volume(self) -> float:
|
||||||
"""获取当前体积"""
|
"""获取当前体积"""
|
||||||
return self._current_volume
|
return self._current_volume
|
||||||
|
|
||||||
def get_remaining_capacity(self) -> float:
|
def get_remaining_capacity(self) -> float:
|
||||||
"""获取剩余容量"""
|
"""获取剩余容量"""
|
||||||
return self.max_volume - self._current_volume
|
return self.max_volume - self._current_volume
|
||||||
|
|
||||||
def is_empty(self) -> bool:
|
def is_empty(self) -> bool:
|
||||||
"""检查是否为空"""
|
"""检查是否为空"""
|
||||||
return self._current_volume <= 0.01 # 允许小量误差
|
return self._current_volume <= 0.01 # 允许小量误差
|
||||||
|
|
||||||
def is_full(self) -> bool:
|
def is_full(self) -> bool:
|
||||||
"""检查是否已满"""
|
"""检查是否已满"""
|
||||||
return self._current_volume >= (self.max_volume - 0.01) # 允许小量误差
|
return self._current_volume >= (self.max_volume - 0.01) # 允许小量误差
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"VirtualTransferPump({self.device_id}: {self._current_volume:.2f}/{self.max_volume} ml, {self._status})"
|
return (
|
||||||
|
f"VirtualTransferPump({self.device_id}: {self._current_volume:.2f}/{self.max_volume} ml, {self._status})"
|
||||||
|
)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return self.__str__()
|
return self.__str__()
|
||||||
|
|
||||||
@@ -398,20 +410,20 @@ class VirtualTransferPump:
|
|||||||
async def demo():
|
async def demo():
|
||||||
"""虚拟泵使用示例"""
|
"""虚拟泵使用示例"""
|
||||||
pump = VirtualTransferPump("demo_pump", {"max_volume": 50.0})
|
pump = VirtualTransferPump("demo_pump", {"max_volume": 50.0})
|
||||||
|
|
||||||
await pump.initialize()
|
await pump.initialize()
|
||||||
|
|
||||||
print(f"Initial state: {pump}")
|
print(f"Initial state: {pump}")
|
||||||
|
|
||||||
# 测试set_position方法
|
# 测试set_position方法
|
||||||
result = await pump.set_position(10.0, max_velocity=2.0)
|
result = await pump.set_position(10.0, max_velocity=2.0)
|
||||||
print(f"Set position result: {result}")
|
print(f"Set position result: {result}")
|
||||||
print(f"After setting position to 10ml: {pump}")
|
print(f"After setting position to 10ml: {pump}")
|
||||||
|
|
||||||
# 吸液测试
|
# 吸液测试
|
||||||
await pump.aspirate(5.0, velocity=2.0)
|
await pump.aspirate(5.0, velocity=2.0)
|
||||||
print(f"After aspirating 5ml: {pump}")
|
print(f"After aspirating 5ml: {pump}")
|
||||||
|
|
||||||
# 清空测试
|
# 清空测试
|
||||||
result = await pump.set_position(0.0)
|
result = await pump.set_position(0.0)
|
||||||
print(f"Empty result: {result}")
|
print(f"Empty result: {result}")
|
||||||
|
|||||||
@@ -11,9 +11,10 @@ Virtual Workbench Device - 模拟工作台设备
|
|||||||
|
|
||||||
注意:调用来自线程池,使用 threading.Lock 进行同步
|
注意:调用来自线程池,使用 threading.Lock 进行同步
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
from typing import Dict, Any, Optional
|
from typing import Dict, Any, Optional, List
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from threading import Lock, RLock
|
from threading import Lock, RLock
|
||||||
@@ -21,38 +22,47 @@ from threading import Lock, RLock
|
|||||||
from typing_extensions import TypedDict
|
from typing_extensions import TypedDict
|
||||||
|
|
||||||
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
|
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
|
||||||
from unilabos.utils.decorator import not_action
|
from unilabos.utils.decorator import not_action, always_free
|
||||||
|
from unilabos.resources.resource_tracker import SampleUUIDsType, LabSample, RETURN_UNILABOS_SAMPLES
|
||||||
|
|
||||||
|
|
||||||
# ============ TypedDict 返回类型定义 ============
|
# ============ TypedDict 返回类型定义 ============
|
||||||
|
|
||||||
|
|
||||||
class MoveToHeatingStationResult(TypedDict):
|
class MoveToHeatingStationResult(TypedDict):
|
||||||
"""move_to_heating_station 返回类型"""
|
"""move_to_heating_station 返回类型"""
|
||||||
|
|
||||||
success: bool
|
success: bool
|
||||||
station_id: int
|
station_id: int
|
||||||
material_id: str
|
material_id: str
|
||||||
material_number: int
|
material_number: int
|
||||||
message: str
|
message: str
|
||||||
|
unilabos_samples: List[LabSample]
|
||||||
|
|
||||||
|
|
||||||
class StartHeatingResult(TypedDict):
|
class StartHeatingResult(TypedDict):
|
||||||
"""start_heating 返回类型"""
|
"""start_heating 返回类型"""
|
||||||
|
|
||||||
success: bool
|
success: bool
|
||||||
station_id: int
|
station_id: int
|
||||||
material_id: str
|
material_id: str
|
||||||
material_number: int
|
material_number: int
|
||||||
message: str
|
message: str
|
||||||
|
unilabos_samples: List[LabSample]
|
||||||
|
|
||||||
|
|
||||||
class MoveToOutputResult(TypedDict):
|
class MoveToOutputResult(TypedDict):
|
||||||
"""move_to_output 返回类型"""
|
"""move_to_output 返回类型"""
|
||||||
|
|
||||||
success: bool
|
success: bool
|
||||||
station_id: int
|
station_id: int
|
||||||
material_id: str
|
material_id: str
|
||||||
|
unilabos_samples: List[LabSample]
|
||||||
|
|
||||||
|
|
||||||
class PrepareMaterialsResult(TypedDict):
|
class PrepareMaterialsResult(TypedDict):
|
||||||
"""prepare_materials 返回类型 - 批量准备物料"""
|
"""prepare_materials 返回类型 - 批量准备物料"""
|
||||||
|
|
||||||
success: bool
|
success: bool
|
||||||
count: int
|
count: int
|
||||||
material_1: int # 物料编号1
|
material_1: int # 物料编号1
|
||||||
@@ -61,12 +71,15 @@ class PrepareMaterialsResult(TypedDict):
|
|||||||
material_4: int # 物料编号4
|
material_4: int # 物料编号4
|
||||||
material_5: int # 物料编号5
|
material_5: int # 物料编号5
|
||||||
message: str
|
message: str
|
||||||
|
unilabos_samples: List[LabSample]
|
||||||
|
|
||||||
|
|
||||||
# ============ 状态枚举 ============
|
# ============ 状态枚举 ============
|
||||||
|
|
||||||
|
|
||||||
class HeatingStationState(Enum):
|
class HeatingStationState(Enum):
|
||||||
"""加热台状态枚举"""
|
"""加热台状态枚举"""
|
||||||
|
|
||||||
IDLE = "idle" # 空闲
|
IDLE = "idle" # 空闲
|
||||||
OCCUPIED = "occupied" # 已放置物料,等待加热
|
OCCUPIED = "occupied" # 已放置物料,等待加热
|
||||||
HEATING = "heating" # 加热中
|
HEATING = "heating" # 加热中
|
||||||
@@ -75,6 +88,7 @@ class HeatingStationState(Enum):
|
|||||||
|
|
||||||
class ArmState(Enum):
|
class ArmState(Enum):
|
||||||
"""机械臂状态枚举"""
|
"""机械臂状态枚举"""
|
||||||
|
|
||||||
IDLE = "idle" # 空闲
|
IDLE = "idle" # 空闲
|
||||||
BUSY = "busy" # 工作中
|
BUSY = "busy" # 工作中
|
||||||
|
|
||||||
@@ -82,6 +96,7 @@ class ArmState(Enum):
|
|||||||
@dataclass
|
@dataclass
|
||||||
class HeatingStation:
|
class HeatingStation:
|
||||||
"""加热台数据结构"""
|
"""加热台数据结构"""
|
||||||
|
|
||||||
station_id: int
|
station_id: int
|
||||||
state: HeatingStationState = HeatingStationState.IDLE
|
state: HeatingStationState = HeatingStationState.IDLE
|
||||||
current_material: Optional[str] = None # 当前物料 (如 "A1", "A2")
|
current_material: Optional[str] = None # 当前物料 (如 "A1", "A2")
|
||||||
@@ -108,8 +123,8 @@ class VirtualWorkbench:
|
|||||||
_ros_node: BaseROS2DeviceNode
|
_ros_node: BaseROS2DeviceNode
|
||||||
|
|
||||||
# 配置常量
|
# 配置常量
|
||||||
ARM_OPERATION_TIME: float = 3.0 # 机械臂操作时间(秒)
|
ARM_OPERATION_TIME: float = 2 # 机械臂操作时间(秒)
|
||||||
HEATING_TIME: float = 10.0 # 加热时间(秒)
|
HEATING_TIME: float = 60.0 # 加热时间(秒)
|
||||||
NUM_HEATING_STATIONS: int = 3 # 加热台数量
|
NUM_HEATING_STATIONS: int = 3 # 加热台数量
|
||||||
|
|
||||||
def __init__(self, device_id: Optional[str] = None, config: Optional[Dict[str, Any]] = None, **kwargs):
|
def __init__(self, device_id: Optional[str] = None, config: Optional[Dict[str, Any]] = None, **kwargs):
|
||||||
@@ -126,9 +141,9 @@ class VirtualWorkbench:
|
|||||||
self.data: Dict[str, Any] = {}
|
self.data: Dict[str, Any] = {}
|
||||||
|
|
||||||
# 从config中获取可配置参数
|
# 从config中获取可配置参数
|
||||||
self.ARM_OPERATION_TIME = float(self.config.get("arm_operation_time", 3.0))
|
self.ARM_OPERATION_TIME = float(self.config.get("arm_operation_time", self.ARM_OPERATION_TIME))
|
||||||
self.HEATING_TIME = float(self.config.get("heating_time", 10.0))
|
self.HEATING_TIME = float(self.config.get("heating_time", self.HEATING_TIME))
|
||||||
self.NUM_HEATING_STATIONS = int(self.config.get("num_heating_stations", 3))
|
self.NUM_HEATING_STATIONS = int(self.config.get("num_heating_stations", self.NUM_HEATING_STATIONS))
|
||||||
|
|
||||||
# 机械臂状态和锁 (使用threading.Lock)
|
# 机械臂状态和锁 (使用threading.Lock)
|
||||||
self._arm_lock = Lock()
|
self._arm_lock = Lock()
|
||||||
@@ -137,8 +152,7 @@ class VirtualWorkbench:
|
|||||||
|
|
||||||
# 加热台状态 (station_id -> HeatingStation) - 立即初始化,不依赖initialize()
|
# 加热台状态 (station_id -> HeatingStation) - 立即初始化,不依赖initialize()
|
||||||
self._heating_stations: Dict[int, HeatingStation] = {
|
self._heating_stations: Dict[int, HeatingStation] = {
|
||||||
i: HeatingStation(station_id=i)
|
i: HeatingStation(station_id=i) for i in range(1, self.NUM_HEATING_STATIONS + 1)
|
||||||
for i in range(1, self.NUM_HEATING_STATIONS + 1)
|
|
||||||
}
|
}
|
||||||
self._stations_lock = RLock() # 可重入锁,保护加热台状态
|
self._stations_lock = RLock() # 可重入锁,保护加热台状态
|
||||||
|
|
||||||
@@ -178,14 +192,16 @@ class VirtualWorkbench:
|
|||||||
station.heating_progress = 0.0
|
station.heating_progress = 0.0
|
||||||
|
|
||||||
# 初始化状态
|
# 初始化状态
|
||||||
self.data.update({
|
self.data.update(
|
||||||
"status": "Ready",
|
{
|
||||||
"arm_state": ArmState.IDLE.value,
|
"status": "Ready",
|
||||||
"arm_current_task": None,
|
"arm_state": ArmState.IDLE.value,
|
||||||
"heating_stations": self._get_stations_status(),
|
"arm_current_task": None,
|
||||||
"active_tasks_count": 0,
|
"heating_stations": self._get_stations_status(),
|
||||||
"message": "工作台就绪",
|
"active_tasks_count": 0,
|
||||||
})
|
"message": "工作台就绪",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
self.logger.info(f"工作台初始化完成: {self.NUM_HEATING_STATIONS}个加热台就绪")
|
self.logger.info(f"工作台初始化完成: {self.NUM_HEATING_STATIONS}个加热台就绪")
|
||||||
return True
|
return True
|
||||||
@@ -204,12 +220,14 @@ class VirtualWorkbench:
|
|||||||
with self._tasks_lock:
|
with self._tasks_lock:
|
||||||
self._active_tasks.clear()
|
self._active_tasks.clear()
|
||||||
|
|
||||||
self.data.update({
|
self.data.update(
|
||||||
"status": "Offline",
|
{
|
||||||
"arm_state": ArmState.IDLE.value,
|
"status": "Offline",
|
||||||
"heating_stations": {},
|
"arm_state": ArmState.IDLE.value,
|
||||||
"message": "工作台已关闭",
|
"heating_stations": {},
|
||||||
})
|
"message": "工作台已关闭",
|
||||||
|
}
|
||||||
|
)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _get_stations_status(self) -> Dict[int, Dict[str, Any]]:
|
def _get_stations_status(self) -> Dict[int, Dict[str, Any]]:
|
||||||
@@ -227,12 +245,14 @@ class VirtualWorkbench:
|
|||||||
|
|
||||||
def _update_data_status(self, message: Optional[str] = None):
|
def _update_data_status(self, message: Optional[str] = None):
|
||||||
"""更新状态数据"""
|
"""更新状态数据"""
|
||||||
self.data.update({
|
self.data.update(
|
||||||
"arm_state": self._arm_state.value,
|
{
|
||||||
"arm_current_task": self._arm_current_task,
|
"arm_state": self._arm_state.value,
|
||||||
"heating_stations": self._get_stations_status(),
|
"arm_current_task": self._arm_current_task,
|
||||||
"active_tasks_count": len(self._active_tasks),
|
"heating_stations": self._get_stations_status(),
|
||||||
})
|
"active_tasks_count": len(self._active_tasks),
|
||||||
|
}
|
||||||
|
)
|
||||||
if message:
|
if message:
|
||||||
self.data["message"] = message
|
self.data["message"] = message
|
||||||
|
|
||||||
@@ -280,6 +300,7 @@ class VirtualWorkbench:
|
|||||||
|
|
||||||
def prepare_materials(
|
def prepare_materials(
|
||||||
self,
|
self,
|
||||||
|
sample_uuids: SampleUUIDsType,
|
||||||
count: int = 5,
|
count: int = 5,
|
||||||
) -> PrepareMaterialsResult:
|
) -> PrepareMaterialsResult:
|
||||||
"""
|
"""
|
||||||
@@ -297,10 +318,7 @@ class VirtualWorkbench:
|
|||||||
# 生成物料列表 A1 - A{count}
|
# 生成物料列表 A1 - A{count}
|
||||||
materials = [i for i in range(1, count + 1)]
|
materials = [i for i in range(1, count + 1)]
|
||||||
|
|
||||||
self.logger.info(
|
self.logger.info(f"[准备物料] 生成 {count} 个物料: " f"A1-A{count} -> material_1~material_{count}")
|
||||||
f"[准备物料] 生成 {count} 个物料: "
|
|
||||||
f"A1-A{count} -> material_1~material_{count}"
|
|
||||||
)
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"success": True,
|
"success": True,
|
||||||
@@ -311,10 +329,12 @@ class VirtualWorkbench:
|
|||||||
"material_4": materials[3] if len(materials) > 3 else 0,
|
"material_4": materials[3] if len(materials) > 3 else 0,
|
||||||
"material_5": materials[4] if len(materials) > 4 else 0,
|
"material_5": materials[4] if len(materials) > 4 else 0,
|
||||||
"message": f"已准备 {count} 个物料: A1-A{count}",
|
"message": f"已准备 {count} 个物料: A1-A{count}",
|
||||||
|
"unilabos_samples": [LabSample(sample_uuid=sample_uuid, oss_path="", extra={"material_uuid": content} if isinstance(content, str) else content.serialize()) for sample_uuid, content in sample_uuids.items()]
|
||||||
}
|
}
|
||||||
|
|
||||||
def move_to_heating_station(
|
def move_to_heating_station(
|
||||||
self,
|
self,
|
||||||
|
sample_uuids: SampleUUIDsType,
|
||||||
material_number: int,
|
material_number: int,
|
||||||
) -> MoveToHeatingStationResult:
|
) -> MoveToHeatingStationResult:
|
||||||
"""
|
"""
|
||||||
@@ -391,6 +411,9 @@ class VirtualWorkbench:
|
|||||||
"material_id": material_id,
|
"material_id": material_id,
|
||||||
"material_number": material_number,
|
"material_number": material_number,
|
||||||
"message": f"{material_id}已成功移动到加热台{station_id}",
|
"message": f"{material_id}已成功移动到加热台{station_id}",
|
||||||
|
"unilabos_samples": [
|
||||||
|
LabSample(sample_uuid=sample_uuid, oss_path="", extra={"material_uuid": content} if isinstance(content, str) else content.serialize()) for
|
||||||
|
sample_uuid, content in sample_uuids.items()]
|
||||||
}
|
}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -403,10 +426,15 @@ class VirtualWorkbench:
|
|||||||
"material_id": material_id,
|
"material_id": material_id,
|
||||||
"material_number": material_number,
|
"material_number": material_number,
|
||||||
"message": f"移动失败: {str(e)}",
|
"message": f"移动失败: {str(e)}",
|
||||||
|
"unilabos_samples": [
|
||||||
|
LabSample(sample_uuid=sample_uuid, oss_path="", extra={"material_uuid": content} if isinstance(content, str) else content.serialize()) for
|
||||||
|
sample_uuid, content in sample_uuids.items()]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@always_free
|
||||||
def start_heating(
|
def start_heating(
|
||||||
self,
|
self,
|
||||||
|
sample_uuids: SampleUUIDsType,
|
||||||
station_id: int,
|
station_id: int,
|
||||||
material_number: int,
|
material_number: int,
|
||||||
) -> StartHeatingResult:
|
) -> StartHeatingResult:
|
||||||
@@ -429,6 +457,9 @@ class VirtualWorkbench:
|
|||||||
"material_id": "",
|
"material_id": "",
|
||||||
"material_number": material_number,
|
"material_number": material_number,
|
||||||
"message": f"无效的加热台ID: {station_id}",
|
"message": f"无效的加热台ID: {station_id}",
|
||||||
|
"unilabos_samples": [
|
||||||
|
LabSample(sample_uuid=sample_uuid, oss_path="", extra={"material_uuid": content} if isinstance(content, str) else content.serialize()) for
|
||||||
|
sample_uuid, content in sample_uuids.items()]
|
||||||
}
|
}
|
||||||
|
|
||||||
with self._stations_lock:
|
with self._stations_lock:
|
||||||
@@ -441,6 +472,9 @@ class VirtualWorkbench:
|
|||||||
"material_id": "",
|
"material_id": "",
|
||||||
"material_number": material_number,
|
"material_number": material_number,
|
||||||
"message": f"加热台{station_id}上没有物料",
|
"message": f"加热台{station_id}上没有物料",
|
||||||
|
"unilabos_samples": [
|
||||||
|
LabSample(sample_uuid=sample_uuid, oss_path="", extra={"material_uuid": content} if isinstance(content, str) else content.serialize()) for
|
||||||
|
sample_uuid, content in sample_uuids.items()]
|
||||||
}
|
}
|
||||||
|
|
||||||
if station.state == HeatingStationState.HEATING:
|
if station.state == HeatingStationState.HEATING:
|
||||||
@@ -450,6 +484,9 @@ class VirtualWorkbench:
|
|||||||
"material_id": station.current_material,
|
"material_id": station.current_material,
|
||||||
"material_number": material_number,
|
"material_number": material_number,
|
||||||
"message": f"加热台{station_id}已经在加热中",
|
"message": f"加热台{station_id}已经在加热中",
|
||||||
|
"unilabos_samples": [
|
||||||
|
LabSample(sample_uuid=sample_uuid, oss_path="", extra={"material_uuid": content} if isinstance(content, str) else content.serialize()) for
|
||||||
|
sample_uuid, content in sample_uuids.items()]
|
||||||
}
|
}
|
||||||
|
|
||||||
material_id = station.current_material
|
material_id = station.current_material
|
||||||
@@ -465,10 +502,21 @@ class VirtualWorkbench:
|
|||||||
|
|
||||||
self._update_data_status(f"加热台{station_id}开始加热{material_id}")
|
self._update_data_status(f"加热台{station_id}开始加热{material_id}")
|
||||||
|
|
||||||
# 模拟加热过程 (10秒)
|
# 打印当前所有正在加热的台位
|
||||||
|
with self._stations_lock:
|
||||||
|
heating_list = [
|
||||||
|
f"加热台{sid}:{s.current_material}"
|
||||||
|
for sid, s in self._heating_stations.items()
|
||||||
|
if s.state == HeatingStationState.HEATING and s.current_material
|
||||||
|
]
|
||||||
|
self.logger.info(f"[并行加热] 当前同时加热中: {', '.join(heating_list)}")
|
||||||
|
|
||||||
|
# 模拟加热过程
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
|
last_countdown_log = start_time
|
||||||
while True:
|
while True:
|
||||||
elapsed = time.time() - start_time
|
elapsed = time.time() - start_time
|
||||||
|
remaining = max(0.0, self.HEATING_TIME - elapsed)
|
||||||
progress = min(100.0, (elapsed / self.HEATING_TIME) * 100)
|
progress = min(100.0, (elapsed / self.HEATING_TIME) * 100)
|
||||||
|
|
||||||
with self._stations_lock:
|
with self._stations_lock:
|
||||||
@@ -476,6 +524,11 @@ class VirtualWorkbench:
|
|||||||
|
|
||||||
self._update_data_status(f"加热台{station_id}加热中: {progress:.1f}%")
|
self._update_data_status(f"加热台{station_id}加热中: {progress:.1f}%")
|
||||||
|
|
||||||
|
# 每5秒打印一次倒计时
|
||||||
|
if time.time() - last_countdown_log >= 5.0:
|
||||||
|
self.logger.info(f"[加热台{station_id}] {material_id} 剩余 {remaining:.1f}s")
|
||||||
|
last_countdown_log = time.time()
|
||||||
|
|
||||||
if elapsed >= self.HEATING_TIME:
|
if elapsed >= self.HEATING_TIME:
|
||||||
break
|
break
|
||||||
|
|
||||||
@@ -499,10 +552,14 @@ class VirtualWorkbench:
|
|||||||
"material_id": material_id,
|
"material_id": material_id,
|
||||||
"material_number": material_number,
|
"material_number": material_number,
|
||||||
"message": f"加热台{station_id}加热完成",
|
"message": f"加热台{station_id}加热完成",
|
||||||
|
"unilabos_samples": [
|
||||||
|
LabSample(sample_uuid=sample_uuid, oss_path="", extra={"material_uuid": content} if isinstance(content, str) else content.serialize()) for
|
||||||
|
sample_uuid, content in sample_uuids.items()]
|
||||||
}
|
}
|
||||||
|
|
||||||
def move_to_output(
|
def move_to_output(
|
||||||
self,
|
self,
|
||||||
|
sample_uuids: SampleUUIDsType,
|
||||||
station_id: int,
|
station_id: int,
|
||||||
material_number: int,
|
material_number: int,
|
||||||
) -> MoveToOutputResult:
|
) -> MoveToOutputResult:
|
||||||
@@ -525,6 +582,9 @@ class VirtualWorkbench:
|
|||||||
"material_id": "",
|
"material_id": "",
|
||||||
"output_position": f"C{output_number}",
|
"output_position": f"C{output_number}",
|
||||||
"message": f"无效的加热台ID: {station_id}",
|
"message": f"无效的加热台ID: {station_id}",
|
||||||
|
"unilabos_samples": [
|
||||||
|
LabSample(sample_uuid=sample_uuid, oss_path="", extra={"material_uuid": content} if isinstance(content, str) else content.serialize()) for
|
||||||
|
sample_uuid, content in sample_uuids.items()]
|
||||||
}
|
}
|
||||||
|
|
||||||
with self._stations_lock:
|
with self._stations_lock:
|
||||||
@@ -538,6 +598,9 @@ class VirtualWorkbench:
|
|||||||
"material_id": "",
|
"material_id": "",
|
||||||
"output_position": f"C{output_number}",
|
"output_position": f"C{output_number}",
|
||||||
"message": f"加热台{station_id}上没有物料",
|
"message": f"加热台{station_id}上没有物料",
|
||||||
|
"unilabos_samples": [
|
||||||
|
LabSample(sample_uuid=sample_uuid, oss_path="", extra={"material_uuid": content} if isinstance(content, str) else content.serialize()) for
|
||||||
|
sample_uuid, content in sample_uuids.items()]
|
||||||
}
|
}
|
||||||
|
|
||||||
if station.state != HeatingStationState.COMPLETED:
|
if station.state != HeatingStationState.COMPLETED:
|
||||||
@@ -547,6 +610,9 @@ class VirtualWorkbench:
|
|||||||
"material_id": material_id,
|
"material_id": material_id,
|
||||||
"output_position": f"C{output_number}",
|
"output_position": f"C{output_number}",
|
||||||
"message": f"加热台{station_id}尚未完成加热 (当前状态: {station.state.value})",
|
"message": f"加热台{station_id}尚未完成加热 (当前状态: {station.state.value})",
|
||||||
|
"unilabos_samples": [
|
||||||
|
LabSample(sample_uuid=sample_uuid, oss_path="", extra={"material_uuid": content} if isinstance(content, str) else content.serialize()) for
|
||||||
|
sample_uuid, content in sample_uuids.items()]
|
||||||
}
|
}
|
||||||
|
|
||||||
output_position = f"C{output_number}"
|
output_position = f"C{output_number}"
|
||||||
@@ -595,6 +661,9 @@ class VirtualWorkbench:
|
|||||||
"material_id": material_id,
|
"material_id": material_id,
|
||||||
"output_position": output_position,
|
"output_position": output_position,
|
||||||
"message": f"{material_id}已成功移动到{output_position}",
|
"message": f"{material_id}已成功移动到{output_position}",
|
||||||
|
"unilabos_samples": [
|
||||||
|
LabSample(sample_uuid=sample_uuid, oss_path="", extra={"material_uuid": content} if isinstance(content, str) else content.serialize()) for
|
||||||
|
sample_uuid, content in sample_uuids.items()]
|
||||||
}
|
}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -607,6 +676,9 @@ class VirtualWorkbench:
|
|||||||
"material_id": "",
|
"material_id": "",
|
||||||
"output_position": output_position,
|
"output_position": output_position,
|
||||||
"message": f"移动失败: {str(e)}",
|
"message": f"移动失败: {str(e)}",
|
||||||
|
"unilabos_samples": [
|
||||||
|
LabSample(sample_uuid=sample_uuid, oss_path="", extra={"material_uuid": content} if isinstance(content, str) else content.serialize()) for
|
||||||
|
sample_uuid, content in sample_uuids.items()]
|
||||||
}
|
}
|
||||||
|
|
||||||
# ============ 状态属性 ============
|
# ============ 状态属性 ============
|
||||||
|
|||||||
@@ -258,7 +258,7 @@ class BioyondResourceSynchronizer(ResourceSynchronizer):
|
|||||||
logger.info(f"[同步→Bioyond] ➕ 物料不存在于 Bioyond,将创建新物料并入库")
|
logger.info(f"[同步→Bioyond] ➕ 物料不存在于 Bioyond,将创建新物料并入库")
|
||||||
|
|
||||||
# 第1步:从配置中获取仓库配置
|
# 第1步:从配置中获取仓库配置
|
||||||
warehouse_mapping = self.bioyond_config.get("warehouse_mapping", {})
|
warehouse_mapping = self.workstation.bioyond_config.get("warehouse_mapping", {})
|
||||||
|
|
||||||
# 确定目标仓库名称
|
# 确定目标仓库名称
|
||||||
parent_name = None
|
parent_name = None
|
||||||
|
|||||||
0
unilabos/devices/xrd_d7mate/__init__.py
Normal file
0
unilabos/devices/xrd_d7mate/__init__.py
Normal file
0
unilabos/devices/zhida_hplc/__init__.py
Normal file
0
unilabos/devices/zhida_hplc/__init__.py
Normal file
@@ -96,10 +96,13 @@ serial:
|
|||||||
type: string
|
type: string
|
||||||
port:
|
port:
|
||||||
type: string
|
type: string
|
||||||
|
registry_name:
|
||||||
|
type: string
|
||||||
resource_tracker:
|
resource_tracker:
|
||||||
type: object
|
type: object
|
||||||
required:
|
required:
|
||||||
- device_id
|
- device_id
|
||||||
|
- registry_name
|
||||||
- port
|
- port
|
||||||
type: object
|
type: object
|
||||||
data:
|
data:
|
||||||
|
|||||||
@@ -67,6 +67,9 @@ camera:
|
|||||||
period:
|
period:
|
||||||
default: 0.1
|
default: 0.1
|
||||||
type: number
|
type: number
|
||||||
|
registry_name:
|
||||||
|
default: ''
|
||||||
|
type: string
|
||||||
resource_tracker:
|
resource_tracker:
|
||||||
type: object
|
type: object
|
||||||
required: []
|
required: []
|
||||||
|
|||||||
@@ -317,6 +317,47 @@ separator.chinwe:
|
|||||||
- port
|
- port
|
||||||
type: object
|
type: object
|
||||||
type: UniLabJsonCommand
|
type: UniLabJsonCommand
|
||||||
|
separation_step:
|
||||||
|
goal:
|
||||||
|
max_cycles: 0
|
||||||
|
motor_id: 5
|
||||||
|
pulses: 700
|
||||||
|
speed: 60
|
||||||
|
timeout: 300
|
||||||
|
handles: {}
|
||||||
|
schema:
|
||||||
|
description: 分液步骤 - 液位传感器与电机联动 (有液→顺时针, 无液→逆时针)
|
||||||
|
properties:
|
||||||
|
goal:
|
||||||
|
properties:
|
||||||
|
max_cycles:
|
||||||
|
default: 0
|
||||||
|
description: 最大循环次数 (0=无限制)
|
||||||
|
type: integer
|
||||||
|
motor_id:
|
||||||
|
default: '5'
|
||||||
|
description: 选择电机
|
||||||
|
enum:
|
||||||
|
- '4'
|
||||||
|
- '5'
|
||||||
|
title: '注: 4=搅拌, 5=旋钮'
|
||||||
|
type: string
|
||||||
|
pulses:
|
||||||
|
default: 700
|
||||||
|
description: 每次旋转脉冲数 (约1/4圈)
|
||||||
|
type: integer
|
||||||
|
speed:
|
||||||
|
default: 60
|
||||||
|
description: 电机转速 (RPM)
|
||||||
|
type: integer
|
||||||
|
timeout:
|
||||||
|
default: 300
|
||||||
|
description: 超时时间 (秒)
|
||||||
|
type: integer
|
||||||
|
required:
|
||||||
|
- motor_id
|
||||||
|
type: object
|
||||||
|
type: UniLabJsonCommand
|
||||||
wait_sensor_level:
|
wait_sensor_level:
|
||||||
goal:
|
goal:
|
||||||
target_state: 有液
|
target_state: 有液
|
||||||
|
|||||||
@@ -638,7 +638,7 @@ liquid_handler:
|
|||||||
placeholder_keys: {}
|
placeholder_keys: {}
|
||||||
result: {}
|
result: {}
|
||||||
schema:
|
schema:
|
||||||
description: 吸头迭代函数。用于自动管理和切换吸头架中的吸头,实现批量实验中的吸头自动分配和追踪。该函数监控吸头使用状态,自动切换到下一个可用吸头位置,确保实验流程的连续性。适用于高通量实验、批量处理、自动化流水线等需要大量吸头管理的应用场景。
|
description: 吸头迭代函数。用于自动管理和切换枪头盒中的吸头,实现批量实验中的吸头自动分配和追踪。该函数监控吸头使用状态,自动切换到下一个可用吸头位置,确保实验流程的连续性。适用于高通量实验、批量处理、自动化流水线等需要大量吸头管理的应用场景。
|
||||||
properties:
|
properties:
|
||||||
feedback: {}
|
feedback: {}
|
||||||
goal:
|
goal:
|
||||||
@@ -712,6 +712,43 @@ liquid_handler:
|
|||||||
title: set_group参数
|
title: set_group参数
|
||||||
type: object
|
type: object
|
||||||
type: UniLabJsonCommand
|
type: UniLabJsonCommand
|
||||||
|
auto-set_liquid_from_plate:
|
||||||
|
feedback: {}
|
||||||
|
goal: {}
|
||||||
|
goal_default:
|
||||||
|
liquid_names: null
|
||||||
|
plate: null
|
||||||
|
volumes: null
|
||||||
|
well_names: null
|
||||||
|
handles: {}
|
||||||
|
placeholder_keys: {}
|
||||||
|
result: {}
|
||||||
|
schema:
|
||||||
|
description: ''
|
||||||
|
properties:
|
||||||
|
feedback: {}
|
||||||
|
goal:
|
||||||
|
properties:
|
||||||
|
liquid_names:
|
||||||
|
type: string
|
||||||
|
plate:
|
||||||
|
type: string
|
||||||
|
volumes:
|
||||||
|
type: string
|
||||||
|
well_names:
|
||||||
|
type: string
|
||||||
|
required:
|
||||||
|
- plate
|
||||||
|
- well_names
|
||||||
|
- liquid_names
|
||||||
|
- volumes
|
||||||
|
type: object
|
||||||
|
result: {}
|
||||||
|
required:
|
||||||
|
- goal
|
||||||
|
title: set_liquid_from_plate参数
|
||||||
|
type: object
|
||||||
|
type: UniLabJsonCommand
|
||||||
auto-set_tiprack:
|
auto-set_tiprack:
|
||||||
feedback: {}
|
feedback: {}
|
||||||
goal: {}
|
goal: {}
|
||||||
@@ -721,7 +758,7 @@ liquid_handler:
|
|||||||
placeholder_keys: {}
|
placeholder_keys: {}
|
||||||
result: {}
|
result: {}
|
||||||
schema:
|
schema:
|
||||||
description: 吸头架设置函数。用于配置和初始化液体处理系统的吸头架信息,包括吸头架位置、类型、容量等参数。该函数建立吸头资源管理系统,为后续的吸头选择和使用提供基础配置。适用于系统初始化、吸头架更换、实验配置等需要吸头资源管理的操作场景。
|
description: 枪头盒设置函数。用于配置和初始化液体处理系统的枪头盒信息,包括枪头盒位置、类型、容量等参数。该函数建立吸头资源管理系统,为后续的吸头选择和使用提供基础配置。适用于系统初始化、枪头盒更换、实验配置等需要吸头资源管理的操作场景。
|
||||||
properties:
|
properties:
|
||||||
feedback: {}
|
feedback: {}
|
||||||
goal:
|
goal:
|
||||||
@@ -4093,32 +4130,32 @@ liquid_handler:
|
|||||||
- 0
|
- 0
|
||||||
handles:
|
handles:
|
||||||
input:
|
input:
|
||||||
- data_key: liquid
|
- data_key: sources
|
||||||
data_source: handle
|
data_source: handle
|
||||||
data_type: resource
|
data_type: resource
|
||||||
handler_key: sources
|
handler_key: sources
|
||||||
label: sources
|
label: 待移动液体
|
||||||
- data_key: liquid
|
- data_key: targets
|
||||||
data_source: executor
|
|
||||||
data_type: resource
|
|
||||||
handler_key: targets
|
|
||||||
label: targets
|
|
||||||
- data_key: liquid
|
|
||||||
data_source: executor
|
|
||||||
data_type: resource
|
|
||||||
handler_key: tip_rack
|
|
||||||
label: tip_rack
|
|
||||||
output:
|
|
||||||
- data_key: liquid
|
|
||||||
data_source: handle
|
data_source: handle
|
||||||
data_type: resource
|
data_type: resource
|
||||||
|
handler_key: targets
|
||||||
|
label: 转移目标
|
||||||
|
- data_key: tip_racks
|
||||||
|
data_source: handle
|
||||||
|
data_type: resource
|
||||||
|
handler_key: tip_rack
|
||||||
|
label: 枪头盒
|
||||||
|
output:
|
||||||
|
- data_key: sources.@flatten
|
||||||
|
data_source: executor
|
||||||
|
data_type: resource
|
||||||
handler_key: sources_out
|
handler_key: sources_out
|
||||||
label: sources
|
label: 移液后源孔
|
||||||
- data_key: liquid
|
- data_key: targets.@flatten
|
||||||
data_source: executor
|
data_source: executor
|
||||||
data_type: resource
|
data_type: resource
|
||||||
handler_key: targets_out
|
handler_key: targets_out
|
||||||
label: targets
|
label: 移液后目标孔
|
||||||
placeholder_keys:
|
placeholder_keys:
|
||||||
sources: unilabos_resources
|
sources: unilabos_resources
|
||||||
targets: unilabos_resources
|
targets: unilabos_resources
|
||||||
@@ -5114,19 +5151,34 @@ liquid_handler.biomek:
|
|||||||
- 0
|
- 0
|
||||||
handles:
|
handles:
|
||||||
input:
|
input:
|
||||||
- data_key: liquid
|
- data_key: sources
|
||||||
data_source: handle
|
data_source: handle
|
||||||
data_type: resource
|
data_type: resource
|
||||||
handler_key: liquid-input
|
handler_key: sources
|
||||||
io_type: target
|
io_type: target
|
||||||
label: Liquid Input
|
label: 待移动液体
|
||||||
|
- data_key: targets
|
||||||
|
data_source: handle
|
||||||
|
data_type: resource
|
||||||
|
handler_key: targets
|
||||||
|
label: 转移目标
|
||||||
|
- data_key: tip_racks
|
||||||
|
data_source: handle
|
||||||
|
data_type: resource
|
||||||
|
handler_key: tip_rack
|
||||||
|
label: 枪头盒
|
||||||
output:
|
output:
|
||||||
- data_key: liquid
|
- data_key: sources.@flatten
|
||||||
data_source: executor
|
data_source: executor
|
||||||
data_type: resource
|
data_type: resource
|
||||||
handler_key: liquid-output
|
handler_key: sources_out
|
||||||
io_type: source
|
io_type: source
|
||||||
label: Liquid Output
|
label: 移液后源孔
|
||||||
|
- data_key: targets.@flatten
|
||||||
|
data_source: executor
|
||||||
|
data_type: resource
|
||||||
|
handler_key: targets_out
|
||||||
|
label: 移液后目标孔
|
||||||
placeholder_keys:
|
placeholder_keys:
|
||||||
sources: unilabos_resources
|
sources: unilabos_resources
|
||||||
targets: unilabos_resources
|
targets: unilabos_resources
|
||||||
@@ -9284,7 +9336,13 @@ liquid_handler.prcxi:
|
|||||||
data_source: handle
|
data_source: handle
|
||||||
data_type: resource
|
data_type: resource
|
||||||
handler_key: input_wells
|
handler_key: input_wells
|
||||||
label: InputWells
|
label: 待设定液体孔
|
||||||
|
output:
|
||||||
|
- data_key: wells.@flatten
|
||||||
|
data_source: executor
|
||||||
|
data_type: resource
|
||||||
|
handler_key: output_wells
|
||||||
|
label: 已设定液体孔
|
||||||
placeholder_keys:
|
placeholder_keys:
|
||||||
wells: unilabos_resources
|
wells: unilabos_resources
|
||||||
result: {}
|
result: {}
|
||||||
@@ -9400,6 +9458,352 @@ liquid_handler.prcxi:
|
|||||||
title: LiquidHandlerSetLiquid
|
title: LiquidHandlerSetLiquid
|
||||||
type: object
|
type: object
|
||||||
type: LiquidHandlerSetLiquid
|
type: LiquidHandlerSetLiquid
|
||||||
|
set_liquid_from_plate:
|
||||||
|
feedback: {}
|
||||||
|
goal: {}
|
||||||
|
goal_default:
|
||||||
|
liquid_names: null
|
||||||
|
plate: null
|
||||||
|
volumes: null
|
||||||
|
well_names: null
|
||||||
|
handles:
|
||||||
|
input:
|
||||||
|
- data_key: '@this.0@@@plate'
|
||||||
|
data_source: handle
|
||||||
|
data_type: resource
|
||||||
|
handler_key: input_plate
|
||||||
|
label: 待设定液体板
|
||||||
|
output:
|
||||||
|
- data_key: plate.@flatten
|
||||||
|
data_source: executor
|
||||||
|
data_type: resource
|
||||||
|
handler_key: output_plate
|
||||||
|
label: 已设定液体板
|
||||||
|
- data_key: wells.@flatten
|
||||||
|
data_source: executor
|
||||||
|
data_type: resource
|
||||||
|
handler_key: output_wells
|
||||||
|
label: 已设定液体孔
|
||||||
|
- data_key: volumes
|
||||||
|
data_source: executor
|
||||||
|
data_type: number_array
|
||||||
|
handler_key: output_volumes
|
||||||
|
label: 各孔设定体积
|
||||||
|
placeholder_keys:
|
||||||
|
plate: unilabos_resources
|
||||||
|
result: {}
|
||||||
|
schema:
|
||||||
|
description: ''
|
||||||
|
properties:
|
||||||
|
feedback: {}
|
||||||
|
goal:
|
||||||
|
properties:
|
||||||
|
liquid_names:
|
||||||
|
items:
|
||||||
|
type: string
|
||||||
|
type: array
|
||||||
|
plate:
|
||||||
|
properties:
|
||||||
|
category:
|
||||||
|
type: string
|
||||||
|
children:
|
||||||
|
items:
|
||||||
|
type: string
|
||||||
|
type: array
|
||||||
|
config:
|
||||||
|
type: string
|
||||||
|
data:
|
||||||
|
type: string
|
||||||
|
id:
|
||||||
|
type: string
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
parent:
|
||||||
|
type: string
|
||||||
|
pose:
|
||||||
|
properties:
|
||||||
|
orientation:
|
||||||
|
properties:
|
||||||
|
w:
|
||||||
|
type: number
|
||||||
|
x:
|
||||||
|
type: number
|
||||||
|
y:
|
||||||
|
type: number
|
||||||
|
z:
|
||||||
|
type: number
|
||||||
|
required:
|
||||||
|
- x
|
||||||
|
- y
|
||||||
|
- z
|
||||||
|
- w
|
||||||
|
title: orientation
|
||||||
|
type: object
|
||||||
|
position:
|
||||||
|
properties:
|
||||||
|
x:
|
||||||
|
type: number
|
||||||
|
y:
|
||||||
|
type: number
|
||||||
|
z:
|
||||||
|
type: number
|
||||||
|
required:
|
||||||
|
- x
|
||||||
|
- y
|
||||||
|
- z
|
||||||
|
title: position
|
||||||
|
type: object
|
||||||
|
required:
|
||||||
|
- position
|
||||||
|
- orientation
|
||||||
|
title: pose
|
||||||
|
type: object
|
||||||
|
sample_id:
|
||||||
|
type: string
|
||||||
|
type:
|
||||||
|
type: string
|
||||||
|
required:
|
||||||
|
- id
|
||||||
|
- name
|
||||||
|
- sample_id
|
||||||
|
- children
|
||||||
|
- parent
|
||||||
|
- type
|
||||||
|
- category
|
||||||
|
- pose
|
||||||
|
- config
|
||||||
|
- data
|
||||||
|
title: plate
|
||||||
|
type: object
|
||||||
|
volumes:
|
||||||
|
items:
|
||||||
|
type: number
|
||||||
|
type: array
|
||||||
|
well_names:
|
||||||
|
items:
|
||||||
|
type: string
|
||||||
|
type: array
|
||||||
|
required:
|
||||||
|
- plate
|
||||||
|
- well_names
|
||||||
|
- liquid_names
|
||||||
|
- volumes
|
||||||
|
type: object
|
||||||
|
result:
|
||||||
|
$defs:
|
||||||
|
ResourceDict:
|
||||||
|
properties:
|
||||||
|
class:
|
||||||
|
description: Resource class name
|
||||||
|
title: Class
|
||||||
|
type: string
|
||||||
|
config:
|
||||||
|
additionalProperties: true
|
||||||
|
description: Resource configuration
|
||||||
|
title: Config
|
||||||
|
type: object
|
||||||
|
data:
|
||||||
|
additionalProperties: true
|
||||||
|
description: 'Resource data, eg: container liquid data'
|
||||||
|
title: Data
|
||||||
|
type: object
|
||||||
|
description:
|
||||||
|
default: ''
|
||||||
|
description: Resource description
|
||||||
|
title: Description
|
||||||
|
type: string
|
||||||
|
extra:
|
||||||
|
additionalProperties: true
|
||||||
|
description: 'Extra data, eg: slot index'
|
||||||
|
title: Extra
|
||||||
|
type: object
|
||||||
|
icon:
|
||||||
|
default: ''
|
||||||
|
description: Resource icon
|
||||||
|
title: Icon
|
||||||
|
type: string
|
||||||
|
id:
|
||||||
|
description: Resource ID
|
||||||
|
title: Id
|
||||||
|
type: string
|
||||||
|
model:
|
||||||
|
additionalProperties: true
|
||||||
|
description: Resource model
|
||||||
|
title: Model
|
||||||
|
type: object
|
||||||
|
name:
|
||||||
|
description: Resource name
|
||||||
|
title: Name
|
||||||
|
type: string
|
||||||
|
parent:
|
||||||
|
anyOf:
|
||||||
|
- $ref: '#/$defs/ResourceDict'
|
||||||
|
- type: 'null'
|
||||||
|
default: null
|
||||||
|
description: Parent resource object
|
||||||
|
parent_uuid:
|
||||||
|
anyOf:
|
||||||
|
- type: string
|
||||||
|
- type: 'null'
|
||||||
|
default: null
|
||||||
|
description: Parent resource uuid
|
||||||
|
title: Parent Uuid
|
||||||
|
pose:
|
||||||
|
$ref: '#/$defs/ResourceDictPosition'
|
||||||
|
description: Resource position
|
||||||
|
schema:
|
||||||
|
additionalProperties: true
|
||||||
|
description: Resource schema
|
||||||
|
title: Schema
|
||||||
|
type: object
|
||||||
|
type:
|
||||||
|
anyOf:
|
||||||
|
- const: device
|
||||||
|
type: string
|
||||||
|
- type: string
|
||||||
|
description: Resource type
|
||||||
|
title: Type
|
||||||
|
uuid:
|
||||||
|
description: Resource UUID
|
||||||
|
title: Uuid
|
||||||
|
type: string
|
||||||
|
required:
|
||||||
|
- id
|
||||||
|
- uuid
|
||||||
|
- name
|
||||||
|
- type
|
||||||
|
- class
|
||||||
|
- config
|
||||||
|
- data
|
||||||
|
- extra
|
||||||
|
title: ResourceDict
|
||||||
|
type: object
|
||||||
|
ResourceDictPosition:
|
||||||
|
properties:
|
||||||
|
cross_section_type:
|
||||||
|
default: rectangle
|
||||||
|
description: Cross section type
|
||||||
|
enum:
|
||||||
|
- rectangle
|
||||||
|
- circle
|
||||||
|
- rounded_rectangle
|
||||||
|
title: Cross Section Type
|
||||||
|
type: string
|
||||||
|
layout:
|
||||||
|
default: x-y
|
||||||
|
description: Resource layout
|
||||||
|
enum:
|
||||||
|
- 2d
|
||||||
|
- x-y
|
||||||
|
- z-y
|
||||||
|
- x-z
|
||||||
|
title: Layout
|
||||||
|
type: string
|
||||||
|
position:
|
||||||
|
$ref: '#/$defs/ResourceDictPositionObject'
|
||||||
|
description: Resource position
|
||||||
|
position3d:
|
||||||
|
$ref: '#/$defs/ResourceDictPositionObject'
|
||||||
|
description: Resource position in 3D space
|
||||||
|
rotation:
|
||||||
|
$ref: '#/$defs/ResourceDictPositionObject'
|
||||||
|
description: Resource rotation
|
||||||
|
scale:
|
||||||
|
$ref: '#/$defs/ResourceDictPositionScale'
|
||||||
|
description: Resource scale
|
||||||
|
size:
|
||||||
|
$ref: '#/$defs/ResourceDictPositionSize'
|
||||||
|
description: Resource size
|
||||||
|
title: ResourceDictPosition
|
||||||
|
type: object
|
||||||
|
ResourceDictPositionObject:
|
||||||
|
properties:
|
||||||
|
x:
|
||||||
|
default: 0.0
|
||||||
|
description: X coordinate
|
||||||
|
title: X
|
||||||
|
type: number
|
||||||
|
y:
|
||||||
|
default: 0.0
|
||||||
|
description: Y coordinate
|
||||||
|
title: Y
|
||||||
|
type: number
|
||||||
|
z:
|
||||||
|
default: 0.0
|
||||||
|
description: Z coordinate
|
||||||
|
title: Z
|
||||||
|
type: number
|
||||||
|
title: ResourceDictPositionObject
|
||||||
|
type: object
|
||||||
|
ResourceDictPositionScale:
|
||||||
|
properties:
|
||||||
|
x:
|
||||||
|
default: 0.0
|
||||||
|
description: x scale
|
||||||
|
title: X
|
||||||
|
type: number
|
||||||
|
y:
|
||||||
|
default: 0.0
|
||||||
|
description: y scale
|
||||||
|
title: Y
|
||||||
|
type: number
|
||||||
|
z:
|
||||||
|
default: 0.0
|
||||||
|
description: z scale
|
||||||
|
title: Z
|
||||||
|
type: number
|
||||||
|
title: ResourceDictPositionScale
|
||||||
|
type: object
|
||||||
|
ResourceDictPositionSize:
|
||||||
|
properties:
|
||||||
|
depth:
|
||||||
|
default: 0.0
|
||||||
|
description: Depth
|
||||||
|
title: Depth
|
||||||
|
type: number
|
||||||
|
height:
|
||||||
|
default: 0.0
|
||||||
|
description: Height
|
||||||
|
title: Height
|
||||||
|
type: number
|
||||||
|
width:
|
||||||
|
default: 0.0
|
||||||
|
description: Width
|
||||||
|
title: Width
|
||||||
|
type: number
|
||||||
|
title: ResourceDictPositionSize
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
plate:
|
||||||
|
items:
|
||||||
|
items:
|
||||||
|
$ref: '#/$defs/ResourceDict'
|
||||||
|
type: array
|
||||||
|
title: Plate
|
||||||
|
type: array
|
||||||
|
volumes:
|
||||||
|
items:
|
||||||
|
type: number
|
||||||
|
title: Volumes
|
||||||
|
type: array
|
||||||
|
wells:
|
||||||
|
items:
|
||||||
|
items:
|
||||||
|
$ref: '#/$defs/ResourceDict'
|
||||||
|
type: array
|
||||||
|
title: Wells
|
||||||
|
type: array
|
||||||
|
required:
|
||||||
|
- plate
|
||||||
|
- wells
|
||||||
|
- volumes
|
||||||
|
title: SetLiquidFromPlateReturn
|
||||||
|
type: object
|
||||||
|
required:
|
||||||
|
- goal
|
||||||
|
title: set_liquid_from_plate参数
|
||||||
|
type: object
|
||||||
|
type: UniLabJsonCommand
|
||||||
set_tiprack:
|
set_tiprack:
|
||||||
feedback: {}
|
feedback: {}
|
||||||
goal:
|
goal:
|
||||||
@@ -9745,32 +10149,32 @@ liquid_handler.prcxi:
|
|||||||
- 0
|
- 0
|
||||||
handles:
|
handles:
|
||||||
input:
|
input:
|
||||||
- data_key: liquid
|
- data_key: sources
|
||||||
data_source: handle
|
data_source: handle
|
||||||
data_type: resource
|
data_type: resource
|
||||||
handler_key: sources
|
handler_key: sources_identifier
|
||||||
label: sources
|
label: 待移动液体
|
||||||
- data_key: liquid
|
- data_key: targets
|
||||||
data_source: executor
|
data_source: handle
|
||||||
data_type: resource
|
data_type: resource
|
||||||
handler_key: targets
|
handler_key: targets_identifier
|
||||||
label: targets
|
label: 转移目标
|
||||||
- data_key: liquid
|
- data_key: tip_rack
|
||||||
data_source: executor
|
data_source: handle
|
||||||
data_type: resource
|
data_type: resource
|
||||||
handler_key: tip_rack
|
handler_key: tip_rack_identifier
|
||||||
label: tip_rack
|
label: 枪头盒
|
||||||
output:
|
output:
|
||||||
- data_key: liquid
|
- data_key: sources.@flatten
|
||||||
data_source: handle
|
data_source: executor
|
||||||
data_type: resource
|
data_type: resource
|
||||||
handler_key: sources_out
|
handler_key: sources_out
|
||||||
label: sources
|
label: 移液后源孔
|
||||||
- data_key: liquid
|
- data_key: targets.@flatten
|
||||||
data_source: executor
|
data_source: executor
|
||||||
data_type: resource
|
data_type: resource
|
||||||
handler_key: targets_out
|
handler_key: targets_out
|
||||||
label: targets
|
label: 移液后目标孔
|
||||||
placeholder_keys:
|
placeholder_keys:
|
||||||
sources: unilabos_resources
|
sources: unilabos_resources
|
||||||
targets: unilabos_resources
|
targets: unilabos_resources
|
||||||
|
|||||||
286
unilabos/registry/devices/motor.yaml
Normal file
286
unilabos/registry/devices/motor.yaml
Normal file
@@ -0,0 +1,286 @@
|
|||||||
|
motor.zdt_x42:
|
||||||
|
category:
|
||||||
|
- motor
|
||||||
|
class:
|
||||||
|
action_value_mappings:
|
||||||
|
auto-enable:
|
||||||
|
feedback: {}
|
||||||
|
goal: {}
|
||||||
|
goal_default:
|
||||||
|
'on': true
|
||||||
|
handles: {}
|
||||||
|
placeholder_keys: {}
|
||||||
|
result: {}
|
||||||
|
schema:
|
||||||
|
description: 使能或禁用电机。使能后电机进入锁轴状态,可接收运动指令;禁用后电机进入松轴状态。
|
||||||
|
properties:
|
||||||
|
feedback: {}
|
||||||
|
goal:
|
||||||
|
properties:
|
||||||
|
'on':
|
||||||
|
default: true
|
||||||
|
type: boolean
|
||||||
|
required: []
|
||||||
|
type: object
|
||||||
|
result: {}
|
||||||
|
required:
|
||||||
|
- goal
|
||||||
|
title: enable参数
|
||||||
|
type: object
|
||||||
|
type: UniLabJsonCommand
|
||||||
|
auto-get_position:
|
||||||
|
feedback: {}
|
||||||
|
goal: {}
|
||||||
|
goal_default: {}
|
||||||
|
handles: {}
|
||||||
|
placeholder_keys: {}
|
||||||
|
result: {}
|
||||||
|
schema:
|
||||||
|
description: 获取当前电机脉冲位置。
|
||||||
|
properties:
|
||||||
|
feedback: {}
|
||||||
|
goal:
|
||||||
|
properties: {}
|
||||||
|
required: []
|
||||||
|
type: object
|
||||||
|
result:
|
||||||
|
properties:
|
||||||
|
position:
|
||||||
|
type: integer
|
||||||
|
type: object
|
||||||
|
required:
|
||||||
|
- goal
|
||||||
|
title: get_position参数
|
||||||
|
type: object
|
||||||
|
type: UniLabJsonCommand
|
||||||
|
auto-move_position:
|
||||||
|
feedback: {}
|
||||||
|
goal: {}
|
||||||
|
goal_default:
|
||||||
|
absolute: false
|
||||||
|
acceleration: 10
|
||||||
|
direction: CW
|
||||||
|
pulses: 1000
|
||||||
|
speed_rpm: 60
|
||||||
|
handles: {}
|
||||||
|
placeholder_keys: {}
|
||||||
|
result: {}
|
||||||
|
schema:
|
||||||
|
description: 位置模式运行。控制电机移动到指定脉冲位置或相对于当前位置移动指定脉冲数。
|
||||||
|
properties:
|
||||||
|
feedback: {}
|
||||||
|
goal:
|
||||||
|
properties:
|
||||||
|
absolute:
|
||||||
|
default: false
|
||||||
|
type: boolean
|
||||||
|
acceleration:
|
||||||
|
default: 10
|
||||||
|
maximum: 255
|
||||||
|
minimum: 0
|
||||||
|
type: integer
|
||||||
|
direction:
|
||||||
|
default: CW
|
||||||
|
enum:
|
||||||
|
- CW
|
||||||
|
- CCW
|
||||||
|
type: string
|
||||||
|
pulses:
|
||||||
|
default: 1000
|
||||||
|
type: integer
|
||||||
|
speed_rpm:
|
||||||
|
default: 60
|
||||||
|
minimum: 0
|
||||||
|
type: integer
|
||||||
|
required:
|
||||||
|
- pulses
|
||||||
|
- speed_rpm
|
||||||
|
type: object
|
||||||
|
result: {}
|
||||||
|
required:
|
||||||
|
- goal
|
||||||
|
title: move_position参数
|
||||||
|
type: object
|
||||||
|
type: UniLabJsonCommand
|
||||||
|
auto-move_speed:
|
||||||
|
feedback: {}
|
||||||
|
goal: {}
|
||||||
|
goal_default:
|
||||||
|
acceleration: 10
|
||||||
|
direction: CW
|
||||||
|
speed_rpm: 60
|
||||||
|
handles: {}
|
||||||
|
placeholder_keys: {}
|
||||||
|
result: {}
|
||||||
|
schema:
|
||||||
|
description: 速度模式运行。控制电机以指定转速和方向持续转动。
|
||||||
|
properties:
|
||||||
|
feedback: {}
|
||||||
|
goal:
|
||||||
|
properties:
|
||||||
|
acceleration:
|
||||||
|
default: 10
|
||||||
|
maximum: 255
|
||||||
|
minimum: 0
|
||||||
|
type: integer
|
||||||
|
direction:
|
||||||
|
default: CW
|
||||||
|
enum:
|
||||||
|
- CW
|
||||||
|
- CCW
|
||||||
|
type: string
|
||||||
|
speed_rpm:
|
||||||
|
default: 60
|
||||||
|
minimum: 0
|
||||||
|
type: integer
|
||||||
|
required:
|
||||||
|
- speed_rpm
|
||||||
|
type: object
|
||||||
|
result: {}
|
||||||
|
required:
|
||||||
|
- goal
|
||||||
|
title: move_speed参数
|
||||||
|
type: object
|
||||||
|
type: UniLabJsonCommand
|
||||||
|
auto-rotate_quarter:
|
||||||
|
feedback: {}
|
||||||
|
goal: {}
|
||||||
|
goal_default:
|
||||||
|
direction: CW
|
||||||
|
speed_rpm: 60
|
||||||
|
handles: {}
|
||||||
|
placeholder_keys: {}
|
||||||
|
result: {}
|
||||||
|
schema:
|
||||||
|
description: 电机旋转 1/4 圈 (阻塞式)。
|
||||||
|
properties:
|
||||||
|
feedback: {}
|
||||||
|
goal:
|
||||||
|
properties:
|
||||||
|
direction:
|
||||||
|
default: CW
|
||||||
|
enum:
|
||||||
|
- CW
|
||||||
|
- CCW
|
||||||
|
type: string
|
||||||
|
speed_rpm:
|
||||||
|
default: 60
|
||||||
|
minimum: 1
|
||||||
|
type: integer
|
||||||
|
required: []
|
||||||
|
type: object
|
||||||
|
result: {}
|
||||||
|
required:
|
||||||
|
- goal
|
||||||
|
title: rotate_quarter参数
|
||||||
|
type: object
|
||||||
|
type: UniLabJsonCommand
|
||||||
|
auto-set_zero:
|
||||||
|
feedback: {}
|
||||||
|
goal: {}
|
||||||
|
goal_default: {}
|
||||||
|
handles: {}
|
||||||
|
placeholder_keys: {}
|
||||||
|
result: {}
|
||||||
|
schema:
|
||||||
|
description: 将当前电机位置设为零点。
|
||||||
|
properties:
|
||||||
|
feedback: {}
|
||||||
|
goal:
|
||||||
|
properties: {}
|
||||||
|
required: []
|
||||||
|
type: object
|
||||||
|
result: {}
|
||||||
|
required:
|
||||||
|
- goal
|
||||||
|
title: set_zero参数
|
||||||
|
type: object
|
||||||
|
type: UniLabJsonCommand
|
||||||
|
auto-stop:
|
||||||
|
feedback: {}
|
||||||
|
goal: {}
|
||||||
|
goal_default: {}
|
||||||
|
handles: {}
|
||||||
|
placeholder_keys: {}
|
||||||
|
result: {}
|
||||||
|
schema:
|
||||||
|
description: 立即停止电机运动。
|
||||||
|
properties:
|
||||||
|
feedback: {}
|
||||||
|
goal:
|
||||||
|
properties: {}
|
||||||
|
required: []
|
||||||
|
type: object
|
||||||
|
result: {}
|
||||||
|
required:
|
||||||
|
- goal
|
||||||
|
title: stop参数
|
||||||
|
type: object
|
||||||
|
type: UniLabJsonCommand
|
||||||
|
auto-wait_time:
|
||||||
|
feedback: {}
|
||||||
|
goal: {}
|
||||||
|
goal_default:
|
||||||
|
duration_s: 1.0
|
||||||
|
handles: {}
|
||||||
|
placeholder_keys: {}
|
||||||
|
result: {}
|
||||||
|
schema:
|
||||||
|
description: 等待指定时间 (秒)。
|
||||||
|
properties:
|
||||||
|
feedback: {}
|
||||||
|
goal:
|
||||||
|
properties:
|
||||||
|
duration_s:
|
||||||
|
default: 1.0
|
||||||
|
minimum: 0
|
||||||
|
type: number
|
||||||
|
required:
|
||||||
|
- duration_s
|
||||||
|
type: object
|
||||||
|
result: {}
|
||||||
|
required:
|
||||||
|
- goal
|
||||||
|
title: wait_time参数
|
||||||
|
type: object
|
||||||
|
type: UniLabJsonCommand
|
||||||
|
module: unilabos.devices.motor.ZDT_X42:ZDTX42Driver
|
||||||
|
status_types:
|
||||||
|
position: int
|
||||||
|
status: str
|
||||||
|
type: python
|
||||||
|
config_info: []
|
||||||
|
description: ZDT X42 闭环步进电机驱动。支持速度运行、精确位置控制、位置查询和清零功能。适用于各种需要精确运动控制的实验室自动化场景。
|
||||||
|
handles: []
|
||||||
|
icon: ''
|
||||||
|
init_param_schema:
|
||||||
|
config:
|
||||||
|
properties:
|
||||||
|
baudrate:
|
||||||
|
default: 115200
|
||||||
|
type: integer
|
||||||
|
debug:
|
||||||
|
default: false
|
||||||
|
type: boolean
|
||||||
|
device_id:
|
||||||
|
default: 1
|
||||||
|
type: integer
|
||||||
|
port:
|
||||||
|
type: string
|
||||||
|
timeout:
|
||||||
|
default: 0.5
|
||||||
|
type: number
|
||||||
|
required:
|
||||||
|
- port
|
||||||
|
type: object
|
||||||
|
data:
|
||||||
|
properties:
|
||||||
|
position:
|
||||||
|
type: integer
|
||||||
|
status:
|
||||||
|
type: string
|
||||||
|
required:
|
||||||
|
- status
|
||||||
|
- position
|
||||||
|
type: object
|
||||||
|
version: 1.0.0
|
||||||
148
unilabos/registry/devices/sensor.yaml
Normal file
148
unilabos/registry/devices/sensor.yaml
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
sensor.xkc_rs485:
|
||||||
|
category:
|
||||||
|
- sensor
|
||||||
|
- separator
|
||||||
|
class:
|
||||||
|
action_value_mappings:
|
||||||
|
auto-change_baudrate:
|
||||||
|
goal:
|
||||||
|
baud_code: 7
|
||||||
|
handles: {}
|
||||||
|
schema:
|
||||||
|
description: '更改通讯波特率 (设置成功后无返回,且需手动切换波特率重连)。代码表 (16进制): 05=2400, 06=4800,
|
||||||
|
07=9600, 08=14400, 09=19200, 0A=28800, 0C=57600, 0D=115200, 0E=128000,
|
||||||
|
0F=256000'
|
||||||
|
properties:
|
||||||
|
goal:
|
||||||
|
properties:
|
||||||
|
baud_code:
|
||||||
|
description: '波特率代码 (例如: 7 为 9600, 13 即 0x0D 为 115200)'
|
||||||
|
type: integer
|
||||||
|
required:
|
||||||
|
- baud_code
|
||||||
|
type: object
|
||||||
|
type: UniLabJsonCommand
|
||||||
|
auto-change_device_id:
|
||||||
|
goal:
|
||||||
|
new_id: 1
|
||||||
|
handles: {}
|
||||||
|
schema:
|
||||||
|
description: 修改传感器的 Modbus 从站地址
|
||||||
|
properties:
|
||||||
|
goal:
|
||||||
|
properties:
|
||||||
|
new_id:
|
||||||
|
description: 新的从站地址 (1-254)
|
||||||
|
maximum: 254
|
||||||
|
minimum: 1
|
||||||
|
type: integer
|
||||||
|
required:
|
||||||
|
- new_id
|
||||||
|
type: object
|
||||||
|
type: UniLabJsonCommand
|
||||||
|
auto-factory_reset:
|
||||||
|
goal: {}
|
||||||
|
handles: {}
|
||||||
|
schema:
|
||||||
|
description: 恢复出厂设置 (地址重置为 01)
|
||||||
|
properties:
|
||||||
|
goal:
|
||||||
|
type: object
|
||||||
|
type: UniLabJsonCommand
|
||||||
|
auto-read_level:
|
||||||
|
goal: {}
|
||||||
|
handles: {}
|
||||||
|
schema:
|
||||||
|
description: 直接读取当前液位及信号强度
|
||||||
|
properties:
|
||||||
|
goal:
|
||||||
|
type: object
|
||||||
|
type: object
|
||||||
|
type: UniLabJsonCommand
|
||||||
|
auto-set_threshold:
|
||||||
|
goal:
|
||||||
|
threshold: 300
|
||||||
|
handles: {}
|
||||||
|
schema:
|
||||||
|
description: 设置液位判定阈值
|
||||||
|
properties:
|
||||||
|
goal:
|
||||||
|
properties:
|
||||||
|
threshold:
|
||||||
|
type: integer
|
||||||
|
required:
|
||||||
|
- threshold
|
||||||
|
type: object
|
||||||
|
type: UniLabJsonCommand
|
||||||
|
auto-wait_for_liquid:
|
||||||
|
goal:
|
||||||
|
target_state: true
|
||||||
|
timeout: 120
|
||||||
|
handles: {}
|
||||||
|
schema:
|
||||||
|
description: 实时检测电导率(RSSI)并等待用户指定的状态
|
||||||
|
properties:
|
||||||
|
goal:
|
||||||
|
properties:
|
||||||
|
target_state:
|
||||||
|
default: true
|
||||||
|
description: 目标状态 (True=有液, False=无液)
|
||||||
|
type: boolean
|
||||||
|
timeout:
|
||||||
|
default: 120
|
||||||
|
description: 超时时间 (秒)
|
||||||
|
required:
|
||||||
|
- target_state
|
||||||
|
type: object
|
||||||
|
type: UniLabJsonCommand
|
||||||
|
auto-wait_level:
|
||||||
|
goal:
|
||||||
|
level: true
|
||||||
|
timeout: 10
|
||||||
|
handles: {}
|
||||||
|
schema:
|
||||||
|
description: 等待液位达到目标状态
|
||||||
|
properties:
|
||||||
|
goal:
|
||||||
|
properties:
|
||||||
|
level:
|
||||||
|
type: boolean
|
||||||
|
timeout:
|
||||||
|
type: number
|
||||||
|
required:
|
||||||
|
- level
|
||||||
|
type: object
|
||||||
|
type: UniLabJsonCommand
|
||||||
|
module: unilabos.devices.separator.xkc_sensor:XKCSensorDriver
|
||||||
|
status_types:
|
||||||
|
level: bool
|
||||||
|
rssi: int
|
||||||
|
type: python
|
||||||
|
config_info: []
|
||||||
|
description: XKC RS485 非接触式液位传感器 (Modbus RTU)
|
||||||
|
handles: []
|
||||||
|
icon: ''
|
||||||
|
init_param_schema:
|
||||||
|
config:
|
||||||
|
properties:
|
||||||
|
baudrate:
|
||||||
|
default: 9600
|
||||||
|
type: integer
|
||||||
|
debug:
|
||||||
|
default: false
|
||||||
|
type: boolean
|
||||||
|
device_id:
|
||||||
|
default: 1
|
||||||
|
type: integer
|
||||||
|
port:
|
||||||
|
type: string
|
||||||
|
threshold:
|
||||||
|
default: 300
|
||||||
|
type: integer
|
||||||
|
timeout:
|
||||||
|
default: 3.0
|
||||||
|
type: number
|
||||||
|
required:
|
||||||
|
- port
|
||||||
|
type: object
|
||||||
|
version: 1.0.0
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -4,6 +4,9 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
import inspect
|
import inspect
|
||||||
import importlib
|
import importlib
|
||||||
|
import threading
|
||||||
|
import traceback
|
||||||
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Dict, List, Union, Tuple
|
from typing import Any, Dict, List, Union, Tuple
|
||||||
|
|
||||||
@@ -60,6 +63,7 @@ class Registry:
|
|||||||
self.device_module_to_registry = {}
|
self.device_module_to_registry = {}
|
||||||
self.resource_type_registry = {}
|
self.resource_type_registry = {}
|
||||||
self._setup_called = False # 跟踪setup是否已调用
|
self._setup_called = False # 跟踪setup是否已调用
|
||||||
|
self._registry_lock = threading.Lock() # 多线程加载时的锁
|
||||||
# 其他状态变量
|
# 其他状态变量
|
||||||
# self.is_host_mode = False # 移至BasicConfig中
|
# self.is_host_mode = False # 移至BasicConfig中
|
||||||
|
|
||||||
@@ -85,6 +89,14 @@ class Registry:
|
|||||||
)
|
)
|
||||||
test_latency_schema["description"] = "用于测试延迟的动作,返回延迟时间和时间差。"
|
test_latency_schema["description"] = "用于测试延迟的动作,返回延迟时间和时间差。"
|
||||||
|
|
||||||
|
test_resource_method_info = host_node_enhanced_info.get("action_methods", {}).get("test_resource", {})
|
||||||
|
test_resource_schema = self._generate_unilab_json_command_schema(
|
||||||
|
test_resource_method_info.get("args", []),
|
||||||
|
"test_resource",
|
||||||
|
test_resource_method_info.get("return_annotation"),
|
||||||
|
)
|
||||||
|
test_resource_schema["description"] = "用于测试物料、设备和样本。"
|
||||||
|
|
||||||
self.device_type_registry.update(
|
self.device_type_registry.update(
|
||||||
{
|
{
|
||||||
"host_node": {
|
"host_node": {
|
||||||
@@ -163,6 +175,8 @@ class Registry:
|
|||||||
"res_id": "unilabos_resources", # 将当前实验室的全部物料id作为下拉框可选择
|
"res_id": "unilabos_resources", # 将当前实验室的全部物料id作为下拉框可选择
|
||||||
"device_id": "unilabos_devices", # 将当前实验室的全部设备id作为下拉框可选择
|
"device_id": "unilabos_devices", # 将当前实验室的全部设备id作为下拉框可选择
|
||||||
"parent": "unilabos_nodes", # 将当前实验室的设备/物料作为下拉框可选择
|
"parent": "unilabos_nodes", # 将当前实验室的设备/物料作为下拉框可选择
|
||||||
|
"class_name": "unilabos_class", # 当前实验室物料的class name
|
||||||
|
"slot_on_deck": "unilabos_resource_slot:parent", # 勾选的parent的config中的sites的name,展示name,参数对应slot(index)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"test_latency": {
|
"test_latency": {
|
||||||
@@ -176,8 +190,7 @@ class Registry:
|
|||||||
"result": {},
|
"result": {},
|
||||||
"schema": test_latency_schema,
|
"schema": test_latency_schema,
|
||||||
"goal_default": {
|
"goal_default": {
|
||||||
arg["name"]: arg["default"]
|
arg["name"]: arg["default"] for arg in test_latency_method_info.get("args", [])
|
||||||
for arg in test_latency_method_info.get("args", [])
|
|
||||||
},
|
},
|
||||||
"handles": {},
|
"handles": {},
|
||||||
},
|
},
|
||||||
@@ -186,32 +199,7 @@ class Registry:
|
|||||||
"goal": {},
|
"goal": {},
|
||||||
"feedback": {},
|
"feedback": {},
|
||||||
"result": {},
|
"result": {},
|
||||||
"schema": {
|
"schema": test_resource_schema,
|
||||||
"description": "",
|
|
||||||
"properties": {
|
|
||||||
"feedback": {},
|
|
||||||
"goal": {
|
|
||||||
"properties": {
|
|
||||||
"resource": ros_message_to_json_schema(Resource, "resource"),
|
|
||||||
"resources": {
|
|
||||||
"items": {
|
|
||||||
"properties": ros_message_to_json_schema(
|
|
||||||
Resource, "resources"
|
|
||||||
),
|
|
||||||
"type": "object",
|
|
||||||
},
|
|
||||||
"type": "array",
|
|
||||||
},
|
|
||||||
"device": {"type": "string"},
|
|
||||||
"devices": {"items": {"type": "string"}, "type": "array"},
|
|
||||||
},
|
|
||||||
"type": "object",
|
|
||||||
},
|
|
||||||
"result": {},
|
|
||||||
},
|
|
||||||
"title": "test_resource",
|
|
||||||
"type": "object",
|
|
||||||
},
|
|
||||||
"placeholder_keys": {
|
"placeholder_keys": {
|
||||||
"device": "unilabos_devices",
|
"device": "unilabos_devices",
|
||||||
"devices": "unilabos_devices",
|
"devices": "unilabos_devices",
|
||||||
@@ -261,67 +249,115 @@ class Registry:
|
|||||||
# 标记setup已被调用
|
# 标记setup已被调用
|
||||||
self._setup_called = True
|
self._setup_called = True
|
||||||
|
|
||||||
|
def _load_single_resource_file(
|
||||||
|
self, file: Path, complete_registry: bool, upload_registry: bool
|
||||||
|
) -> Tuple[Dict[str, Any], Dict[str, Any], bool]:
|
||||||
|
"""
|
||||||
|
加载单个资源文件 (线程安全)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
(data, complete_data, is_valid): 资源数据, 完整数据, 是否有效
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
with open(file, encoding="utf-8", mode="r") as f:
|
||||||
|
data = yaml.safe_load(io.StringIO(f.read()))
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"[UniLab Registry] 读取资源文件失败: {file}, 错误: {e}")
|
||||||
|
return {}, {}, False
|
||||||
|
|
||||||
|
if not data:
|
||||||
|
return {}, {}, False
|
||||||
|
|
||||||
|
complete_data = {}
|
||||||
|
for resource_id, resource_info in data.items():
|
||||||
|
if "version" not in resource_info:
|
||||||
|
resource_info["version"] = "1.0.0"
|
||||||
|
if "category" not in resource_info:
|
||||||
|
resource_info["category"] = [file.stem]
|
||||||
|
elif file.stem not in resource_info["category"]:
|
||||||
|
resource_info["category"].append(file.stem)
|
||||||
|
elif not isinstance(resource_info.get("category"), list):
|
||||||
|
resource_info["category"] = [resource_info["category"]]
|
||||||
|
if "config_info" not in resource_info:
|
||||||
|
resource_info["config_info"] = []
|
||||||
|
if "icon" not in resource_info:
|
||||||
|
resource_info["icon"] = ""
|
||||||
|
if "handles" not in resource_info:
|
||||||
|
resource_info["handles"] = []
|
||||||
|
if "init_param_schema" not in resource_info:
|
||||||
|
resource_info["init_param_schema"] = {}
|
||||||
|
if "config_info" in resource_info:
|
||||||
|
del resource_info["config_info"]
|
||||||
|
if "file_path" in resource_info:
|
||||||
|
del resource_info["file_path"]
|
||||||
|
complete_data[resource_id] = copy.deepcopy(dict(sorted(resource_info.items())))
|
||||||
|
if upload_registry:
|
||||||
|
class_info = resource_info.get("class", {})
|
||||||
|
if len(class_info) and "module" in class_info:
|
||||||
|
if class_info.get("type") == "pylabrobot":
|
||||||
|
res_class = get_class(class_info["module"])
|
||||||
|
if callable(res_class) and not isinstance(res_class, type):
|
||||||
|
res_instance = res_class(res_class.__name__)
|
||||||
|
res_ulr = tree_to_list([resource_plr_to_ulab(res_instance)])
|
||||||
|
resource_info["config_info"] = res_ulr
|
||||||
|
resource_info["registry_type"] = "resource"
|
||||||
|
resource_info["file_path"] = str(file.absolute()).replace("\\", "/")
|
||||||
|
|
||||||
|
complete_data = dict(sorted(complete_data.items()))
|
||||||
|
complete_data = copy.deepcopy(complete_data)
|
||||||
|
|
||||||
|
if complete_registry:
|
||||||
|
try:
|
||||||
|
with open(file, "w", encoding="utf-8") as f:
|
||||||
|
yaml.dump(complete_data, f, allow_unicode=True, default_flow_style=False, Dumper=NoAliasDumper)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"[UniLab Registry] 写入资源文件失败: {file}, 错误: {e}")
|
||||||
|
|
||||||
|
return data, complete_data, True
|
||||||
|
|
||||||
def load_resource_types(self, path: os.PathLike, complete_registry: bool, upload_registry: bool):
|
def load_resource_types(self, path: os.PathLike, complete_registry: bool, upload_registry: bool):
|
||||||
abs_path = Path(path).absolute()
|
abs_path = Path(path).absolute()
|
||||||
resource_path = abs_path / "resources"
|
resource_path = abs_path / "resources"
|
||||||
files = list(resource_path.glob("*/*.yaml"))
|
files = list(resource_path.glob("*/*.yaml"))
|
||||||
logger.trace(f"[UniLab Registry] load resources? {resource_path.exists()}, total: {len(files)}")
|
logger.debug(f"[UniLab Registry] resources: {resource_path.exists()}, total: {len(files)}")
|
||||||
current_resource_number = len(self.resource_type_registry) + 1
|
|
||||||
for i, file in enumerate(files):
|
|
||||||
with open(file, encoding="utf-8", mode="r") as f:
|
|
||||||
data = yaml.safe_load(io.StringIO(f.read()))
|
|
||||||
complete_data = {}
|
|
||||||
if data:
|
|
||||||
# 为每个资源添加文件路径信息
|
|
||||||
for resource_id, resource_info in data.items():
|
|
||||||
if "version" not in resource_info:
|
|
||||||
resource_info["version"] = "1.0.0"
|
|
||||||
if "category" not in resource_info:
|
|
||||||
resource_info["category"] = [file.stem]
|
|
||||||
elif file.stem not in resource_info["category"]:
|
|
||||||
resource_info["category"].append(file.stem)
|
|
||||||
elif not isinstance(resource_info.get("category"), list):
|
|
||||||
resource_info["category"] = [resource_info["category"]]
|
|
||||||
if "config_info" not in resource_info:
|
|
||||||
resource_info["config_info"] = []
|
|
||||||
if "icon" not in resource_info:
|
|
||||||
resource_info["icon"] = ""
|
|
||||||
if "handles" not in resource_info:
|
|
||||||
resource_info["handles"] = []
|
|
||||||
if "init_param_schema" not in resource_info:
|
|
||||||
resource_info["init_param_schema"] = {}
|
|
||||||
if "config_info" in resource_info:
|
|
||||||
del resource_info["config_info"]
|
|
||||||
if "file_path" in resource_info:
|
|
||||||
del resource_info["file_path"]
|
|
||||||
complete_data[resource_id] = copy.deepcopy(dict(sorted(resource_info.items())))
|
|
||||||
if upload_registry:
|
|
||||||
class_info = resource_info.get("class", {})
|
|
||||||
if len(class_info) and "module" in class_info:
|
|
||||||
if class_info.get("type") == "pylabrobot":
|
|
||||||
res_class = get_class(class_info["module"])
|
|
||||||
if callable(res_class) and not isinstance(
|
|
||||||
res_class, type
|
|
||||||
): # 有的是类,有的是函数,这里暂时只登记函数类的
|
|
||||||
res_instance = res_class(res_class.__name__)
|
|
||||||
res_ulr = tree_to_list([resource_plr_to_ulab(res_instance)])
|
|
||||||
resource_info["config_info"] = res_ulr
|
|
||||||
resource_info["registry_type"] = "resource"
|
|
||||||
resource_info["file_path"] = str(file.absolute()).replace("\\", "/")
|
|
||||||
complete_data = dict(sorted(complete_data.items()))
|
|
||||||
complete_data = copy.deepcopy(complete_data)
|
|
||||||
if complete_registry:
|
|
||||||
with open(file, "w", encoding="utf-8") as f:
|
|
||||||
yaml.dump(complete_data, f, allow_unicode=True, default_flow_style=False, Dumper=NoAliasDumper)
|
|
||||||
|
|
||||||
|
if not files:
|
||||||
|
return
|
||||||
|
|
||||||
|
# 使用线程池并行加载
|
||||||
|
max_workers = min(8, len(files))
|
||||||
|
results = []
|
||||||
|
|
||||||
|
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
||||||
|
future_to_file = {
|
||||||
|
executor.submit(self._load_single_resource_file, file, complete_registry, upload_registry): file
|
||||||
|
for file in files
|
||||||
|
}
|
||||||
|
for future in as_completed(future_to_file):
|
||||||
|
file = future_to_file[future]
|
||||||
|
try:
|
||||||
|
data, complete_data, is_valid = future.result()
|
||||||
|
if is_valid:
|
||||||
|
results.append((file, data))
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"[UniLab Registry] 处理资源文件异常: {file}, 错误: {e}")
|
||||||
|
|
||||||
|
# 线程安全地更新注册表
|
||||||
|
current_resource_number = len(self.resource_type_registry) + 1
|
||||||
|
with self._registry_lock:
|
||||||
|
for i, (file, data) in enumerate(results):
|
||||||
self.resource_type_registry.update(data)
|
self.resource_type_registry.update(data)
|
||||||
logger.trace( # type: ignore
|
logger.trace(
|
||||||
f"[UniLab Registry] Resource-{current_resource_number} File-{i+1}/{len(files)} "
|
f"[UniLab Registry] Resource-{current_resource_number} File-{i+1}/{len(results)} "
|
||||||
+ f"Add {list(data.keys())}"
|
+ f"Add {list(data.keys())}"
|
||||||
)
|
)
|
||||||
current_resource_number += 1
|
current_resource_number += 1
|
||||||
else:
|
|
||||||
logger.debug(f"[UniLab Registry] Res File-{i+1}/{len(files)} Not Valid YAML File: {file.absolute()}")
|
# 记录无效文件
|
||||||
|
valid_files = {r[0] for r in results}
|
||||||
|
for file in files:
|
||||||
|
if file not in valid_files:
|
||||||
|
logger.debug(f"[UniLab Registry] Res File Not Valid YAML File: {file.absolute()}")
|
||||||
|
|
||||||
def _extract_class_docstrings(self, module_string: str) -> Dict[str, str]:
|
def _extract_class_docstrings(self, module_string: str) -> Dict[str, str]:
|
||||||
"""
|
"""
|
||||||
@@ -673,213 +709,246 @@ class Registry:
|
|||||||
"handles": {},
|
"handles": {},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def _load_single_device_file(
|
||||||
|
self, file: Path, complete_registry: bool, get_yaml_from_goal_type
|
||||||
|
) -> Tuple[Dict[str, Any], Dict[str, Any], bool, List[str]]:
|
||||||
|
"""
|
||||||
|
加载单个设备文件 (线程安全)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
(data, complete_data, is_valid, device_ids): 设备数据, 完整数据, 是否有效, 设备ID列表
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
with open(file, encoding="utf-8", mode="r") as f:
|
||||||
|
data = yaml.safe_load(io.StringIO(f.read()))
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"[UniLab Registry] 读取设备文件失败: {file}, 错误: {e}")
|
||||||
|
return {}, {}, False, []
|
||||||
|
|
||||||
|
if not data:
|
||||||
|
return {}, {}, False, []
|
||||||
|
|
||||||
|
complete_data = {}
|
||||||
|
action_str_type_mapping = {
|
||||||
|
"UniLabJsonCommand": "UniLabJsonCommand",
|
||||||
|
"UniLabJsonCommandAsync": "UniLabJsonCommandAsync",
|
||||||
|
}
|
||||||
|
status_str_type_mapping = {}
|
||||||
|
device_ids = []
|
||||||
|
|
||||||
|
for device_id, device_config in data.items():
|
||||||
|
if "version" not in device_config:
|
||||||
|
device_config["version"] = "1.0.0"
|
||||||
|
if "category" not in device_config:
|
||||||
|
device_config["category"] = [file.stem]
|
||||||
|
elif file.stem not in device_config["category"]:
|
||||||
|
device_config["category"].append(file.stem)
|
||||||
|
if "config_info" not in device_config:
|
||||||
|
device_config["config_info"] = []
|
||||||
|
if "description" not in device_config:
|
||||||
|
device_config["description"] = ""
|
||||||
|
if "icon" not in device_config:
|
||||||
|
device_config["icon"] = ""
|
||||||
|
if "handles" not in device_config:
|
||||||
|
device_config["handles"] = []
|
||||||
|
if "init_param_schema" not in device_config:
|
||||||
|
device_config["init_param_schema"] = {}
|
||||||
|
if "class" in device_config:
|
||||||
|
if "status_types" not in device_config["class"] or device_config["class"]["status_types"] is None:
|
||||||
|
device_config["class"]["status_types"] = {}
|
||||||
|
if (
|
||||||
|
"action_value_mappings" not in device_config["class"]
|
||||||
|
or device_config["class"]["action_value_mappings"] is None
|
||||||
|
):
|
||||||
|
device_config["class"]["action_value_mappings"] = {}
|
||||||
|
enhanced_info = {}
|
||||||
|
if complete_registry:
|
||||||
|
device_config["class"]["status_types"].clear()
|
||||||
|
enhanced_info = get_enhanced_class_info(device_config["class"]["module"], use_dynamic=True)
|
||||||
|
if not enhanced_info.get("dynamic_import_success", False):
|
||||||
|
continue
|
||||||
|
device_config["class"]["status_types"].update(
|
||||||
|
{k: v["return_type"] for k, v in enhanced_info["status_methods"].items()}
|
||||||
|
)
|
||||||
|
for status_name, status_type in device_config["class"]["status_types"].items():
|
||||||
|
if isinstance(status_type, tuple) or status_type in ["Any", "None", "Unknown"]:
|
||||||
|
status_type = "String"
|
||||||
|
device_config["class"]["status_types"][status_name] = status_type
|
||||||
|
try:
|
||||||
|
target_type = self._replace_type_with_class(status_type, device_id, f"状态 {status_name}")
|
||||||
|
except ROSMsgNotFound:
|
||||||
|
continue
|
||||||
|
if target_type in [dict, list]:
|
||||||
|
target_type = String
|
||||||
|
status_str_type_mapping[status_type] = target_type
|
||||||
|
device_config["class"]["status_types"] = dict(sorted(device_config["class"]["status_types"].items()))
|
||||||
|
if complete_registry:
|
||||||
|
old_action_configs = {}
|
||||||
|
for action_name, action_config in device_config["class"]["action_value_mappings"].items():
|
||||||
|
old_action_configs[action_name] = action_config
|
||||||
|
|
||||||
|
device_config["class"]["action_value_mappings"] = {
|
||||||
|
k: v
|
||||||
|
for k, v in device_config["class"]["action_value_mappings"].items()
|
||||||
|
if not k.startswith("auto-")
|
||||||
|
}
|
||||||
|
device_config["class"]["action_value_mappings"].update(
|
||||||
|
{
|
||||||
|
f"auto-{k}": {
|
||||||
|
"type": "UniLabJsonCommandAsync" if v["is_async"] else "UniLabJsonCommand",
|
||||||
|
"goal": {},
|
||||||
|
"feedback": {},
|
||||||
|
"result": {},
|
||||||
|
"schema": self._generate_unilab_json_command_schema(
|
||||||
|
v["args"],
|
||||||
|
k,
|
||||||
|
v.get("return_annotation"),
|
||||||
|
old_action_configs.get(f"auto-{k}", {}).get("schema"),
|
||||||
|
),
|
||||||
|
"goal_default": {i["name"]: i["default"] for i in v["args"]},
|
||||||
|
"handles": old_action_configs.get(f"auto-{k}", {}).get("handles", []),
|
||||||
|
"placeholder_keys": {
|
||||||
|
i["name"]: (
|
||||||
|
"unilabos_resources"
|
||||||
|
if i["type"] == "unilabos.registry.placeholder_type:ResourceSlot"
|
||||||
|
or i["type"] == ("list", "unilabos.registry.placeholder_type:ResourceSlot")
|
||||||
|
else "unilabos_devices"
|
||||||
|
)
|
||||||
|
for i in v["args"]
|
||||||
|
if i.get("type", "")
|
||||||
|
in [
|
||||||
|
"unilabos.registry.placeholder_type:ResourceSlot",
|
||||||
|
"unilabos.registry.placeholder_type:DeviceSlot",
|
||||||
|
("list", "unilabos.registry.placeholder_type:ResourceSlot"),
|
||||||
|
("list", "unilabos.registry.placeholder_type:DeviceSlot"),
|
||||||
|
]
|
||||||
|
},
|
||||||
|
**({"always_free": True} if v.get("always_free") else {}),
|
||||||
|
}
|
||||||
|
for k, v in enhanced_info["action_methods"].items()
|
||||||
|
if k not in device_config["class"]["action_value_mappings"]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
for action_name, old_config in old_action_configs.items():
|
||||||
|
if action_name in device_config["class"]["action_value_mappings"]:
|
||||||
|
old_schema = old_config.get("schema", {})
|
||||||
|
if "description" in old_schema and old_schema["description"]:
|
||||||
|
device_config["class"]["action_value_mappings"][action_name]["schema"][
|
||||||
|
"description"
|
||||||
|
] = old_schema["description"]
|
||||||
|
device_config["init_param_schema"] = {}
|
||||||
|
device_config["init_param_schema"]["config"] = self._generate_unilab_json_command_schema(
|
||||||
|
enhanced_info["init_params"], "__init__"
|
||||||
|
)["properties"]["goal"]
|
||||||
|
device_config["init_param_schema"]["data"] = self._generate_status_types_schema(
|
||||||
|
enhanced_info["status_methods"]
|
||||||
|
)
|
||||||
|
|
||||||
|
device_config.pop("schema", None)
|
||||||
|
device_config["class"]["action_value_mappings"] = dict(
|
||||||
|
sorted(device_config["class"]["action_value_mappings"].items())
|
||||||
|
)
|
||||||
|
for action_name, action_config in device_config["class"]["action_value_mappings"].items():
|
||||||
|
if "handles" not in action_config:
|
||||||
|
action_config["handles"] = {}
|
||||||
|
elif isinstance(action_config["handles"], list):
|
||||||
|
if len(action_config["handles"]):
|
||||||
|
logger.error(f"设备{device_id} {action_name} 的handles配置错误,应该是字典类型")
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
action_config["handles"] = {}
|
||||||
|
if "type" in action_config:
|
||||||
|
action_type_str: str = action_config["type"]
|
||||||
|
if not action_type_str.startswith("UniLabJsonCommand"):
|
||||||
|
try:
|
||||||
|
target_type = self._replace_type_with_class(
|
||||||
|
action_type_str, device_id, f"动作 {action_name}"
|
||||||
|
)
|
||||||
|
except ROSMsgNotFound:
|
||||||
|
continue
|
||||||
|
action_str_type_mapping[action_type_str] = target_type
|
||||||
|
if target_type is not None:
|
||||||
|
action_config["goal_default"] = yaml.safe_load(
|
||||||
|
io.StringIO(get_yaml_from_goal_type(target_type.Goal))
|
||||||
|
)
|
||||||
|
action_config["schema"] = ros_action_to_json_schema(target_type)
|
||||||
|
else:
|
||||||
|
logger.warning(
|
||||||
|
f"[UniLab Registry] 设备 {device_id} 的动作 {action_name} 类型为空,跳过替换"
|
||||||
|
)
|
||||||
|
complete_data[device_id] = copy.deepcopy(dict(sorted(device_config.items())))
|
||||||
|
for status_name, status_type in device_config["class"]["status_types"].items():
|
||||||
|
device_config["class"]["status_types"][status_name] = status_str_type_mapping[status_type]
|
||||||
|
for action_name, action_config in device_config["class"]["action_value_mappings"].items():
|
||||||
|
if action_config["type"] not in action_str_type_mapping:
|
||||||
|
continue
|
||||||
|
action_config["type"] = action_str_type_mapping[action_config["type"]]
|
||||||
|
self._add_builtin_actions(device_config, device_id)
|
||||||
|
device_config["file_path"] = str(file.absolute()).replace("\\", "/")
|
||||||
|
device_config["registry_type"] = "device"
|
||||||
|
device_ids.append(device_id)
|
||||||
|
|
||||||
|
complete_data = dict(sorted(complete_data.items()))
|
||||||
|
complete_data = copy.deepcopy(complete_data)
|
||||||
|
try:
|
||||||
|
with open(file, "w", encoding="utf-8") as f:
|
||||||
|
yaml.dump(complete_data, f, allow_unicode=True, default_flow_style=False, Dumper=NoAliasDumper)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"[UniLab Registry] 写入设备文件失败: {file}, 错误: {e}")
|
||||||
|
|
||||||
|
return data, complete_data, True, device_ids
|
||||||
|
|
||||||
def load_device_types(self, path: os.PathLike, complete_registry: bool):
|
def load_device_types(self, path: os.PathLike, complete_registry: bool):
|
||||||
# return
|
|
||||||
abs_path = Path(path).absolute()
|
abs_path = Path(path).absolute()
|
||||||
devices_path = abs_path / "devices"
|
devices_path = abs_path / "devices"
|
||||||
device_comms_path = abs_path / "device_comms"
|
device_comms_path = abs_path / "device_comms"
|
||||||
files = list(devices_path.glob("*.yaml")) + list(device_comms_path.glob("*.yaml"))
|
files = list(devices_path.glob("*.yaml")) + list(device_comms_path.glob("*.yaml"))
|
||||||
logger.trace( # type: ignore
|
logger.trace(
|
||||||
f"[UniLab Registry] devices: {devices_path.exists()}, device_comms: {device_comms_path.exists()}, "
|
f"[UniLab Registry] devices: {devices_path.exists()}, device_comms: {device_comms_path.exists()}, "
|
||||||
+ f"total: {len(files)}"
|
+ f"total: {len(files)}"
|
||||||
)
|
)
|
||||||
current_device_number = len(self.device_type_registry) + 1
|
|
||||||
|
if not files:
|
||||||
|
return
|
||||||
|
|
||||||
from unilabos.app.web.utils.action_utils import get_yaml_from_goal_type
|
from unilabos.app.web.utils.action_utils import get_yaml_from_goal_type
|
||||||
|
|
||||||
for i, file in enumerate(files):
|
# 使用线程池并行加载
|
||||||
with open(file, encoding="utf-8", mode="r") as f:
|
max_workers = min(8, len(files))
|
||||||
data = yaml.safe_load(io.StringIO(f.read()))
|
results = []
|
||||||
complete_data = {}
|
|
||||||
action_str_type_mapping = {
|
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
||||||
"UniLabJsonCommand": "UniLabJsonCommand",
|
future_to_file = {
|
||||||
"UniLabJsonCommandAsync": "UniLabJsonCommandAsync",
|
executor.submit(self._load_single_device_file, file, complete_registry, get_yaml_from_goal_type): file
|
||||||
|
for file in files
|
||||||
}
|
}
|
||||||
status_str_type_mapping = {}
|
for future in as_completed(future_to_file):
|
||||||
if data:
|
file = future_to_file[future]
|
||||||
# 在添加到注册表前处理类型替换
|
try:
|
||||||
for device_id, device_config in data.items():
|
data, complete_data, is_valid, device_ids = future.result()
|
||||||
# 添加文件路径信息 - 使用规范化的完整文件路径
|
if is_valid:
|
||||||
if "version" not in device_config:
|
results.append((file, data, device_ids))
|
||||||
device_config["version"] = "1.0.0"
|
except Exception as e:
|
||||||
if "category" not in device_config:
|
traceback.print_exc()
|
||||||
device_config["category"] = [file.stem]
|
logger.warning(f"[UniLab Registry] 处理设备文件异常: {file}, 错误: {e}")
|
||||||
elif file.stem not in device_config["category"]:
|
|
||||||
device_config["category"].append(file.stem)
|
|
||||||
if "config_info" not in device_config:
|
|
||||||
device_config["config_info"] = []
|
|
||||||
if "description" not in device_config:
|
|
||||||
device_config["description"] = ""
|
|
||||||
if "icon" not in device_config:
|
|
||||||
device_config["icon"] = ""
|
|
||||||
if "handles" not in device_config:
|
|
||||||
device_config["handles"] = []
|
|
||||||
if "init_param_schema" not in device_config:
|
|
||||||
device_config["init_param_schema"] = {}
|
|
||||||
if "class" in device_config:
|
|
||||||
if (
|
|
||||||
"status_types" not in device_config["class"]
|
|
||||||
or device_config["class"]["status_types"] is None
|
|
||||||
):
|
|
||||||
device_config["class"]["status_types"] = {}
|
|
||||||
if (
|
|
||||||
"action_value_mappings" not in device_config["class"]
|
|
||||||
or device_config["class"]["action_value_mappings"] is None
|
|
||||||
):
|
|
||||||
device_config["class"]["action_value_mappings"] = {}
|
|
||||||
enhanced_info = {}
|
|
||||||
if complete_registry:
|
|
||||||
device_config["class"]["status_types"].clear()
|
|
||||||
enhanced_info = get_enhanced_class_info(device_config["class"]["module"], use_dynamic=True)
|
|
||||||
if not enhanced_info.get("dynamic_import_success", False):
|
|
||||||
continue
|
|
||||||
device_config["class"]["status_types"].update(
|
|
||||||
{k: v["return_type"] for k, v in enhanced_info["status_methods"].items()}
|
|
||||||
)
|
|
||||||
for status_name, status_type in device_config["class"]["status_types"].items():
|
|
||||||
if isinstance(status_type, tuple) or status_type in ["Any", "None", "Unknown"]:
|
|
||||||
status_type = "String" # 替换成ROS的String,便于显示
|
|
||||||
device_config["class"]["status_types"][status_name] = status_type
|
|
||||||
try:
|
|
||||||
target_type = self._replace_type_with_class(
|
|
||||||
status_type, device_id, f"状态 {status_name}"
|
|
||||||
)
|
|
||||||
except ROSMsgNotFound:
|
|
||||||
continue
|
|
||||||
if target_type in [
|
|
||||||
dict,
|
|
||||||
list,
|
|
||||||
]: # 对于嵌套类型返回的对象,暂时处理成字符串,无法直接进行转换
|
|
||||||
target_type = String
|
|
||||||
status_str_type_mapping[status_type] = target_type
|
|
||||||
device_config["class"]["status_types"] = dict(
|
|
||||||
sorted(device_config["class"]["status_types"].items())
|
|
||||||
)
|
|
||||||
if complete_registry:
|
|
||||||
# 保存原有的 action 配置(用于保留 schema 的 description 和 handles 等)
|
|
||||||
old_action_configs = {}
|
|
||||||
for action_name, action_config in device_config["class"]["action_value_mappings"].items():
|
|
||||||
old_action_configs[action_name] = action_config
|
|
||||||
|
|
||||||
device_config["class"]["action_value_mappings"] = {
|
# 线程安全地更新注册表
|
||||||
k: v
|
current_device_number = len(self.device_type_registry) + 1
|
||||||
for k, v in device_config["class"]["action_value_mappings"].items()
|
with self._registry_lock:
|
||||||
if not k.startswith("auto-")
|
for file, data, device_ids in results:
|
||||||
}
|
self.device_type_registry.update(data)
|
||||||
# 处理动作值映射
|
for device_id in device_ids:
|
||||||
device_config["class"]["action_value_mappings"].update(
|
logger.trace(
|
||||||
{
|
f"[UniLab Registry] Device-{current_device_number} Add {device_id} "
|
||||||
f"auto-{k}": {
|
|
||||||
"type": "UniLabJsonCommandAsync" if v["is_async"] else "UniLabJsonCommand",
|
|
||||||
"goal": {},
|
|
||||||
"feedback": {},
|
|
||||||
"result": {},
|
|
||||||
"schema": self._generate_unilab_json_command_schema(
|
|
||||||
v["args"],
|
|
||||||
k,
|
|
||||||
v.get("return_annotation"),
|
|
||||||
# 传入旧的 schema 以保留字段 description
|
|
||||||
old_action_configs.get(f"auto-{k}", {}).get("schema"),
|
|
||||||
),
|
|
||||||
"goal_default": {i["name"]: i["default"] for i in v["args"]},
|
|
||||||
# 保留原有的 handles 配置
|
|
||||||
"handles": old_action_configs.get(f"auto-{k}", {}).get("handles", []),
|
|
||||||
"placeholder_keys": {
|
|
||||||
i["name"]: (
|
|
||||||
"unilabos_resources"
|
|
||||||
if i["type"] == "unilabos.registry.placeholder_type:ResourceSlot"
|
|
||||||
or i["type"]
|
|
||||||
== ("list", "unilabos.registry.placeholder_type:ResourceSlot")
|
|
||||||
else "unilabos_devices"
|
|
||||||
)
|
|
||||||
for i in v["args"]
|
|
||||||
if i.get("type", "")
|
|
||||||
in [
|
|
||||||
"unilabos.registry.placeholder_type:ResourceSlot",
|
|
||||||
"unilabos.registry.placeholder_type:DeviceSlot",
|
|
||||||
("list", "unilabos.registry.placeholder_type:ResourceSlot"),
|
|
||||||
("list", "unilabos.registry.placeholder_type:DeviceSlot"),
|
|
||||||
]
|
|
||||||
},
|
|
||||||
}
|
|
||||||
# 不生成已配置action的动作
|
|
||||||
for k, v in enhanced_info["action_methods"].items()
|
|
||||||
if k not in device_config["class"]["action_value_mappings"]
|
|
||||||
}
|
|
||||||
)
|
|
||||||
# 恢复原有的 description 信息(非 auto- 开头的动作)
|
|
||||||
for action_name, old_config in old_action_configs.items():
|
|
||||||
if action_name in device_config["class"]["action_value_mappings"]: # 有一些会被删除
|
|
||||||
old_schema = old_config.get("schema", {})
|
|
||||||
if "description" in old_schema and old_schema["description"]:
|
|
||||||
device_config["class"]["action_value_mappings"][action_name]["schema"][
|
|
||||||
"description"
|
|
||||||
] = old_schema["description"]
|
|
||||||
device_config["init_param_schema"] = {}
|
|
||||||
device_config["init_param_schema"]["config"] = self._generate_unilab_json_command_schema(
|
|
||||||
enhanced_info["init_params"], "__init__"
|
|
||||||
)["properties"]["goal"]
|
|
||||||
device_config["init_param_schema"]["data"] = self._generate_status_types_schema(
|
|
||||||
enhanced_info["status_methods"]
|
|
||||||
)
|
|
||||||
|
|
||||||
device_config.pop("schema", None)
|
|
||||||
device_config["class"]["action_value_mappings"] = dict(
|
|
||||||
sorted(device_config["class"]["action_value_mappings"].items())
|
|
||||||
)
|
|
||||||
for action_name, action_config in device_config["class"]["action_value_mappings"].items():
|
|
||||||
if "handles" not in action_config:
|
|
||||||
action_config["handles"] = {}
|
|
||||||
elif isinstance(action_config["handles"], list):
|
|
||||||
if len(action_config["handles"]):
|
|
||||||
logger.error(f"设备{device_id} {action_name} 的handles配置错误,应该是字典类型")
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
action_config["handles"] = {}
|
|
||||||
if "type" in action_config:
|
|
||||||
action_type_str: str = action_config["type"]
|
|
||||||
# 通过Json发放指令,而不是通过特殊的ros action进行处理
|
|
||||||
if not action_type_str.startswith("UniLabJsonCommand"):
|
|
||||||
try:
|
|
||||||
target_type = self._replace_type_with_class(
|
|
||||||
action_type_str, device_id, f"动作 {action_name}"
|
|
||||||
)
|
|
||||||
except ROSMsgNotFound:
|
|
||||||
continue
|
|
||||||
action_str_type_mapping[action_type_str] = target_type
|
|
||||||
if target_type is not None:
|
|
||||||
action_config["goal_default"] = yaml.safe_load(
|
|
||||||
io.StringIO(get_yaml_from_goal_type(target_type.Goal))
|
|
||||||
)
|
|
||||||
action_config["schema"] = ros_action_to_json_schema(target_type)
|
|
||||||
else:
|
|
||||||
logger.warning(
|
|
||||||
f"[UniLab Registry] 设备 {device_id} 的动作 {action_name} 类型为空,跳过替换"
|
|
||||||
)
|
|
||||||
complete_data[device_id] = copy.deepcopy(dict(sorted(device_config.items()))) # 稍后dump到文件
|
|
||||||
for status_name, status_type in device_config["class"]["status_types"].items():
|
|
||||||
device_config["class"]["status_types"][status_name] = status_str_type_mapping[status_type]
|
|
||||||
for action_name, action_config in device_config["class"]["action_value_mappings"].items():
|
|
||||||
if action_config["type"] not in action_str_type_mapping:
|
|
||||||
continue
|
|
||||||
action_config["type"] = action_str_type_mapping[action_config["type"]]
|
|
||||||
# 添加内置的驱动命令动作
|
|
||||||
self._add_builtin_actions(device_config, device_id)
|
|
||||||
device_config["file_path"] = str(file.absolute()).replace("\\", "/")
|
|
||||||
device_config["registry_type"] = "device"
|
|
||||||
logger.trace( # type: ignore
|
|
||||||
f"[UniLab Registry] Device-{current_device_number} File-{i+1}/{len(files)} Add {device_id} "
|
|
||||||
+ f"[{data[device_id].get('name', '未命名设备')}]"
|
+ f"[{data[device_id].get('name', '未命名设备')}]"
|
||||||
)
|
)
|
||||||
current_device_number += 1
|
current_device_number += 1
|
||||||
complete_data = dict(sorted(complete_data.items()))
|
|
||||||
complete_data = copy.deepcopy(complete_data)
|
# 记录无效文件
|
||||||
with open(file, "w", encoding="utf-8") as f:
|
valid_files = {r[0] for r in results}
|
||||||
yaml.dump(complete_data, f, allow_unicode=True, default_flow_style=False, Dumper=NoAliasDumper)
|
for file in files:
|
||||||
self.device_type_registry.update(data)
|
if file not in valid_files:
|
||||||
else:
|
logger.debug(f"[UniLab Registry] Device File Not Valid YAML File: {file.absolute()}")
|
||||||
logger.debug(
|
|
||||||
f"[UniLab Registry] Device File-{i+1}/{len(files)} Not Valid YAML File: {file.absolute()}"
|
|
||||||
)
|
|
||||||
|
|
||||||
def obtain_registry_device_info(self):
|
def obtain_registry_device_info(self):
|
||||||
devices = []
|
devices = []
|
||||||
|
|||||||
@@ -46,3 +46,16 @@ BIOYOND_PolymerStation_8StockCarrier:
|
|||||||
init_param_schema: {}
|
init_param_schema: {}
|
||||||
registry_type: resource
|
registry_type: resource
|
||||||
version: 1.0.0
|
version: 1.0.0
|
||||||
|
BIOYOND_PolymerStation_TipBox:
|
||||||
|
category:
|
||||||
|
- bottle_carriers
|
||||||
|
- tip_racks
|
||||||
|
class:
|
||||||
|
module: unilabos.resources.bioyond.bottle_carriers:BIOYOND_PolymerStation_TipBox
|
||||||
|
type: pylabrobot
|
||||||
|
description: BIOYOND_PolymerStation_TipBox (4x6布局,24个枪头孔位)
|
||||||
|
handles: []
|
||||||
|
icon: ''
|
||||||
|
init_param_schema: {}
|
||||||
|
registry_type: resource
|
||||||
|
version: 1.0.0
|
||||||
|
|||||||
@@ -82,14 +82,3 @@ BIOYOND_PolymerStation_Solution_Beaker:
|
|||||||
icon: ''
|
icon: ''
|
||||||
init_param_schema: {}
|
init_param_schema: {}
|
||||||
version: 1.0.0
|
version: 1.0.0
|
||||||
BIOYOND_PolymerStation_TipBox:
|
|
||||||
category:
|
|
||||||
- bottles
|
|
||||||
- tip_boxes
|
|
||||||
class:
|
|
||||||
module: unilabos.resources.bioyond.bottles:BIOYOND_PolymerStation_TipBox
|
|
||||||
type: pylabrobot
|
|
||||||
handles: []
|
|
||||||
icon: ''
|
|
||||||
init_param_schema: {}
|
|
||||||
version: 1.0.0
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from pylabrobot.resources import create_homogeneous_resources, Coordinate, ResourceHolder, create_ordered_items_2d
|
from pylabrobot.resources import create_homogeneous_resources, Coordinate, ResourceHolder, create_ordered_items_2d, Container
|
||||||
|
|
||||||
from unilabos.resources.itemized_carrier import BottleCarrier
|
from unilabos.resources.itemized_carrier import BottleCarrier
|
||||||
from unilabos.resources.bioyond.bottles import (
|
from unilabos.resources.bioyond.bottles import (
|
||||||
@@ -9,6 +9,28 @@ from unilabos.resources.bioyond.bottles import (
|
|||||||
BIOYOND_PolymerStation_Reagent_Bottle,
|
BIOYOND_PolymerStation_Reagent_Bottle,
|
||||||
BIOYOND_PolymerStation_Flask,
|
BIOYOND_PolymerStation_Flask,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def BIOYOND_PolymerStation_Tip(name: str, size_x: float = 8.0, size_y: float = 8.0, size_z: float = 50.0) -> Container:
|
||||||
|
"""创建单个枪头资源
|
||||||
|
|
||||||
|
Args:
|
||||||
|
name: 枪头名称
|
||||||
|
size_x: 枪头宽度 (mm)
|
||||||
|
size_y: 枪头长度 (mm)
|
||||||
|
size_z: 枪头高度 (mm)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Container: 枪头容器
|
||||||
|
"""
|
||||||
|
return Container(
|
||||||
|
name=name,
|
||||||
|
size_x=size_x,
|
||||||
|
size_y=size_y,
|
||||||
|
size_z=size_z,
|
||||||
|
category="tip",
|
||||||
|
model="BIOYOND_PolymerStation_Tip",
|
||||||
|
)
|
||||||
# 命名约定:试剂瓶-Bottle,烧杯-Beaker,烧瓶-Flask,小瓶-Vial
|
# 命名约定:试剂瓶-Bottle,烧杯-Beaker,烧瓶-Flask,小瓶-Vial
|
||||||
|
|
||||||
|
|
||||||
@@ -322,3 +344,88 @@ def BIOYOND_Electrolyte_1BottleCarrier(name: str) -> BottleCarrier:
|
|||||||
carrier.num_items_z = 1
|
carrier.num_items_z = 1
|
||||||
carrier[0] = BIOYOND_PolymerStation_Solution_Beaker(f"{name}_beaker_1")
|
carrier[0] = BIOYOND_PolymerStation_Solution_Beaker(f"{name}_beaker_1")
|
||||||
return carrier
|
return carrier
|
||||||
|
|
||||||
|
|
||||||
|
def BIOYOND_PolymerStation_TipBox(
|
||||||
|
name: str,
|
||||||
|
size_x: float = 127.76, # 枪头盒宽度
|
||||||
|
size_y: float = 85.48, # 枪头盒长度
|
||||||
|
size_z: float = 100.0, # 枪头盒高度
|
||||||
|
barcode: str = None,
|
||||||
|
) -> BottleCarrier:
|
||||||
|
"""创建4×6枪头盒 (24个枪头) - 使用 BottleCarrier 结构
|
||||||
|
|
||||||
|
Args:
|
||||||
|
name: 枪头盒名称
|
||||||
|
size_x: 枪头盒宽度 (mm)
|
||||||
|
size_y: 枪头盒长度 (mm)
|
||||||
|
size_z: 枪头盒高度 (mm)
|
||||||
|
barcode: 条形码
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
BottleCarrier: 包含24个枪头孔位的枪头盒载架
|
||||||
|
|
||||||
|
布局说明:
|
||||||
|
- 4行×6列 (A-D, 1-6)
|
||||||
|
- 枪头孔位间距: 18mm (x方向) × 18mm (y方向)
|
||||||
|
- 起始位置居中对齐
|
||||||
|
- 索引顺序: 列优先 (0=A1, 1=B1, 2=C1, 3=D1, 4=A2, ...)
|
||||||
|
"""
|
||||||
|
# 枪头孔位参数
|
||||||
|
num_cols = 6 # 1-6 (x方向)
|
||||||
|
num_rows = 4 # A-D (y方向)
|
||||||
|
tip_diameter = 8.0 # 枪头孔位直径
|
||||||
|
tip_spacing_x = 18.0 # 列间距 (增加到18mm,更宽松)
|
||||||
|
tip_spacing_y = 18.0 # 行间距 (增加到18mm,更宽松)
|
||||||
|
|
||||||
|
# 计算起始位置 (居中对齐)
|
||||||
|
total_width = (num_cols - 1) * tip_spacing_x + tip_diameter
|
||||||
|
total_height = (num_rows - 1) * tip_spacing_y + tip_diameter
|
||||||
|
start_x = (size_x - total_width) / 2
|
||||||
|
start_y = (size_y - total_height) / 2
|
||||||
|
|
||||||
|
# 使用 create_ordered_items_2d 创建孔位
|
||||||
|
# create_ordered_items_2d 返回的 key 是数字索引: 0, 1, 2, ...
|
||||||
|
# 顺序是列优先: 先y后x (即 0=A1, 1=B1, 2=C1, 3=D1, 4=A2, 5=B2, ...)
|
||||||
|
sites = create_ordered_items_2d(
|
||||||
|
klass=ResourceHolder,
|
||||||
|
num_items_x=num_cols,
|
||||||
|
num_items_y=num_rows,
|
||||||
|
dx=start_x,
|
||||||
|
dy=start_y,
|
||||||
|
dz=5.0,
|
||||||
|
item_dx=tip_spacing_x,
|
||||||
|
item_dy=tip_spacing_y,
|
||||||
|
size_x=tip_diameter,
|
||||||
|
size_y=tip_diameter,
|
||||||
|
size_z=50.0, # 枪头深度
|
||||||
|
)
|
||||||
|
|
||||||
|
# 更新 sites 中每个 ResourceHolder 的名称
|
||||||
|
for k, v in sites.items():
|
||||||
|
v.name = f"{name}_{v.name}"
|
||||||
|
|
||||||
|
# 创建枪头盒载架
|
||||||
|
# 注意:不设置 category,使用默认的 "bottle_carrier",这样前端会显示为完整的矩形载架
|
||||||
|
tip_box = BottleCarrier(
|
||||||
|
name=name,
|
||||||
|
size_x=size_x,
|
||||||
|
size_y=size_y,
|
||||||
|
size_z=size_z,
|
||||||
|
sites=sites, # 直接使用数字索引的 sites
|
||||||
|
model="BIOYOND_PolymerStation_TipBox",
|
||||||
|
)
|
||||||
|
|
||||||
|
# 设置自定义属性
|
||||||
|
tip_box.barcode = barcode
|
||||||
|
tip_box.tip_count = 24 # 4行×6列
|
||||||
|
tip_box.num_items_x = num_cols
|
||||||
|
tip_box.num_items_y = num_rows
|
||||||
|
tip_box.num_items_z = 1
|
||||||
|
|
||||||
|
# ⭐ 枪头盒不需要放入子资源
|
||||||
|
# 与其他 carrier 不同,枪头盒在 Bioyond 中是一个整体
|
||||||
|
# 不需要追踪每个枪头的状态,保持为空的 ResourceHolder 即可
|
||||||
|
# 这样前端会显示24个空槽位,可以用于放置枪头
|
||||||
|
|
||||||
|
return tip_box
|
||||||
|
|||||||
@@ -116,7 +116,9 @@ def BIOYOND_PolymerStation_TipBox(
|
|||||||
size_z: float = 100.0, # 枪头盒高度
|
size_z: float = 100.0, # 枪头盒高度
|
||||||
barcode: str = None,
|
barcode: str = None,
|
||||||
):
|
):
|
||||||
"""创建4×6枪头盒 (24个枪头)
|
"""创建4×6枪头盒 (24个枪头) - 使用 BottleCarrier 结构
|
||||||
|
|
||||||
|
注意:此函数已弃用,请使用 bottle_carriers.py 中的版本
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
name: 枪头盒名称
|
name: 枪头盒名称
|
||||||
@@ -126,55 +128,11 @@ def BIOYOND_PolymerStation_TipBox(
|
|||||||
barcode: 条形码
|
barcode: 条形码
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
TipBoxCarrier: 包含24个枪头孔位的枪头盒
|
BottleCarrier: 包含24个枪头孔位的枪头盒载架
|
||||||
"""
|
"""
|
||||||
from pylabrobot.resources import Container, Coordinate
|
# 重定向到 bottle_carriers.py 中的实现
|
||||||
|
from unilabos.resources.bioyond.bottle_carriers import BIOYOND_PolymerStation_TipBox as TipBox_Carrier
|
||||||
# 创建枪头盒容器
|
return TipBox_Carrier(name=name, size_x=size_x, size_y=size_y, size_z=size_z, barcode=barcode)
|
||||||
tip_box = Container(
|
|
||||||
name=name,
|
|
||||||
size_x=size_x,
|
|
||||||
size_y=size_y,
|
|
||||||
size_z=size_z,
|
|
||||||
category="tip_rack",
|
|
||||||
model="BIOYOND_PolymerStation_TipBox_4x6",
|
|
||||||
)
|
|
||||||
|
|
||||||
# 设置自定义属性
|
|
||||||
tip_box.barcode = barcode
|
|
||||||
tip_box.tip_count = 24 # 4行×6列
|
|
||||||
tip_box.num_items_x = 6 # 6列
|
|
||||||
tip_box.num_items_y = 4 # 4行
|
|
||||||
|
|
||||||
# 创建24个枪头孔位 (4行×6列)
|
|
||||||
# 假设孔位间距为 9mm
|
|
||||||
tip_spacing_x = 9.0 # 列间距
|
|
||||||
tip_spacing_y = 9.0 # 行间距
|
|
||||||
start_x = 14.38 # 第一个孔位的x偏移
|
|
||||||
start_y = 11.24 # 第一个孔位的y偏移
|
|
||||||
|
|
||||||
for row in range(4): # A, B, C, D
|
|
||||||
for col in range(6): # 1-6
|
|
||||||
spot_name = f"{chr(65 + row)}{col + 1}" # A1, A2, ..., D6
|
|
||||||
x = start_x + col * tip_spacing_x
|
|
||||||
y = start_y + row * tip_spacing_y
|
|
||||||
|
|
||||||
# 创建枪头孔位容器
|
|
||||||
tip_spot = Container(
|
|
||||||
name=spot_name,
|
|
||||||
size_x=8.0, # 单个枪头孔位大小
|
|
||||||
size_y=8.0,
|
|
||||||
size_z=size_z - 10.0, # 略低于盒子高度
|
|
||||||
category="tip_spot",
|
|
||||||
)
|
|
||||||
|
|
||||||
# 添加到枪头盒
|
|
||||||
tip_box.assign_child_resource(
|
|
||||||
tip_spot,
|
|
||||||
location=Coordinate(x=x, y=y, z=0)
|
|
||||||
)
|
|
||||||
|
|
||||||
return tip_box
|
|
||||||
|
|
||||||
|
|
||||||
def BIOYOND_PolymerStation_Flask(
|
def BIOYOND_PolymerStation_Flask(
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
import json
|
|
||||||
from typing import Dict, Any
|
from typing import Dict, Any
|
||||||
|
|
||||||
from pylabrobot.resources import Container
|
from pylabrobot.resources import Container
|
||||||
from unilabos_msgs.msg import Resource
|
|
||||||
|
|
||||||
from unilabos.ros.msgs.message_converter import convert_from_ros_msg
|
|
||||||
|
|
||||||
|
|
||||||
class RegularContainer(Container):
|
class RegularContainer(Container):
|
||||||
@@ -16,12 +12,12 @@ class RegularContainer(Container):
|
|||||||
kwargs["size_y"] = 0
|
kwargs["size_y"] = 0
|
||||||
if "size_z" not in kwargs:
|
if "size_z" not in kwargs:
|
||||||
kwargs["size_z"] = 0
|
kwargs["size_z"] = 0
|
||||||
|
|
||||||
self.kwargs = kwargs
|
self.kwargs = kwargs
|
||||||
self.state = {}
|
|
||||||
super().__init__(*args, category="container", **kwargs)
|
super().__init__(*args, category="container", **kwargs)
|
||||||
|
|
||||||
def load_state(self, state: Dict[str, Any]):
|
def load_state(self, state: Dict[str, Any]):
|
||||||
self.state = state
|
super().load_state(state)
|
||||||
|
|
||||||
|
|
||||||
def get_regular_container(name="container"):
|
def get_regular_container(name="container"):
|
||||||
@@ -29,7 +25,6 @@ def get_regular_container(name="container"):
|
|||||||
r.category = "container"
|
r.category = "container"
|
||||||
return r
|
return r
|
||||||
|
|
||||||
#
|
|
||||||
# class RegularContainer(object):
|
# class RegularContainer(object):
|
||||||
# # 第一个参数必须是id传入
|
# # 第一个参数必须是id传入
|
||||||
# # noinspection PyShadowingBuiltins
|
# # noinspection PyShadowingBuiltins
|
||||||
@@ -89,4 +84,4 @@ def get_regular_container(name="container"):
|
|||||||
# return to_dict
|
# return to_dict
|
||||||
#
|
#
|
||||||
# def __str__(self):
|
# def __str__(self):
|
||||||
# return f"{self.id}"
|
# return f"{self.id}"
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ def canonicalize_nodes_data(
|
|||||||
if sample_id:
|
if sample_id:
|
||||||
logger.error(f"{node}的sample_id参数已弃用,sample_id: {sample_id}")
|
logger.error(f"{node}的sample_id参数已弃用,sample_id: {sample_id}")
|
||||||
for k in list(node.keys()):
|
for k in list(node.keys()):
|
||||||
if k not in ["id", "uuid", "name", "description", "schema", "model", "icon", "parent_uuid", "parent", "type", "class", "position", "config", "data", "children", "pose"]:
|
if k not in ["id", "uuid", "name", "description", "schema", "model", "icon", "parent_uuid", "parent", "type", "class", "position", "config", "data", "children", "pose", "extra"]:
|
||||||
v = node.pop(k)
|
v = node.pop(k)
|
||||||
node["config"][k] = v
|
node["config"][k] = v
|
||||||
if outer_host_node_id is not None:
|
if outer_host_node_id is not None:
|
||||||
@@ -151,12 +151,40 @@ def canonicalize_links_ports(links: List[Dict[str, Any]], resource_tree_set: Res
|
|||||||
"""
|
"""
|
||||||
# 构建 id 到 uuid 的映射
|
# 构建 id 到 uuid 的映射
|
||||||
id_to_uuid: Dict[str, str] = {}
|
id_to_uuid: Dict[str, str] = {}
|
||||||
|
uuid_to_id: Dict[str, str] = {}
|
||||||
for node in resource_tree_set.all_nodes:
|
for node in resource_tree_set.all_nodes:
|
||||||
id_to_uuid[node.res_content.id] = node.res_content.uuid
|
id_to_uuid[node.res_content.id] = node.res_content.uuid
|
||||||
|
uuid_to_id[node.res_content.uuid] = node.res_content.id
|
||||||
|
|
||||||
|
# 第三遍处理:为每个 link 添加 source_uuid 和 target_uuid
|
||||||
|
for link in links:
|
||||||
|
source_id = link.get("source")
|
||||||
|
target_id = link.get("target")
|
||||||
|
|
||||||
|
# 添加 source_uuid
|
||||||
|
if source_id and source_id in id_to_uuid:
|
||||||
|
link["source_uuid"] = id_to_uuid[source_id]
|
||||||
|
|
||||||
|
# 添加 target_uuid
|
||||||
|
if target_id and target_id in id_to_uuid:
|
||||||
|
link["target_uuid"] = id_to_uuid[target_id]
|
||||||
|
|
||||||
|
source_uuid = link.get("source_uuid")
|
||||||
|
target_uuid = link.get("target_uuid")
|
||||||
|
|
||||||
|
# 添加 source_uuid
|
||||||
|
if source_uuid and source_uuid in uuid_to_id:
|
||||||
|
link["source"] = uuid_to_id[source_uuid]
|
||||||
|
|
||||||
|
# 添加 target_uuid
|
||||||
|
if target_uuid and target_uuid in uuid_to_id:
|
||||||
|
link["target"] = uuid_to_id[target_uuid]
|
||||||
|
|
||||||
# 第一遍处理:将字符串类型的port转换为字典格式
|
# 第一遍处理:将字符串类型的port转换为字典格式
|
||||||
for link in links:
|
for link in links:
|
||||||
port = link.get("port")
|
port = link.get("port")
|
||||||
|
if port is None:
|
||||||
|
continue
|
||||||
if link.get("type", "physical") == "physical":
|
if link.get("type", "physical") == "physical":
|
||||||
link["type"] = "fluid"
|
link["type"] = "fluid"
|
||||||
if isinstance(port, int):
|
if isinstance(port, int):
|
||||||
@@ -179,13 +207,15 @@ def canonicalize_links_ports(links: List[Dict[str, Any]], resource_tree_set: Res
|
|||||||
link["port"] = {link["source"]: None, link["target"]: None}
|
link["port"] = {link["source"]: None, link["target"]: None}
|
||||||
|
|
||||||
# 构建边字典,键为(source节点, target节点),值为对应的port信息
|
# 构建边字典,键为(source节点, target节点),值为对应的port信息
|
||||||
edges = {(link["source"], link["target"]): link["port"] for link in links}
|
edges = {(link["source"], link["target"]): link["port"] for link in links if link.get("port")}
|
||||||
|
|
||||||
# 第二遍处理:填充反向边的dest信息
|
# 第二遍处理:填充反向边的dest信息
|
||||||
delete_reverses = []
|
delete_reverses = []
|
||||||
for i, link in enumerate(links):
|
for i, link in enumerate(links):
|
||||||
s, t = link["source"], link["target"]
|
s, t = link["source"], link["target"]
|
||||||
current_port = link["port"]
|
current_port = link.get("port")
|
||||||
|
if current_port is None:
|
||||||
|
continue
|
||||||
if current_port.get(t) is None:
|
if current_port.get(t) is None:
|
||||||
reverse_key = (t, s)
|
reverse_key = (t, s)
|
||||||
reverse_port = edges.get(reverse_key)
|
reverse_port = edges.get(reverse_key)
|
||||||
@@ -200,20 +230,6 @@ def canonicalize_links_ports(links: List[Dict[str, Any]], resource_tree_set: Res
|
|||||||
current_port[t] = current_port[s]
|
current_port[t] = current_port[s]
|
||||||
# 删除已被使用反向端口信息的反向边
|
# 删除已被使用反向端口信息的反向边
|
||||||
standardized_links = [link for i, link in enumerate(links) if i not in delete_reverses]
|
standardized_links = [link for i, link in enumerate(links) if i not in delete_reverses]
|
||||||
|
|
||||||
# 第三遍处理:为每个 link 添加 source_uuid 和 target_uuid
|
|
||||||
for link in standardized_links:
|
|
||||||
source_id = link.get("source")
|
|
||||||
target_id = link.get("target")
|
|
||||||
|
|
||||||
# 添加 source_uuid
|
|
||||||
if source_id and source_id in id_to_uuid:
|
|
||||||
link["source_uuid"] = id_to_uuid[source_id]
|
|
||||||
|
|
||||||
# 添加 target_uuid
|
|
||||||
if target_id and target_id in id_to_uuid:
|
|
||||||
link["target_uuid"] = id_to_uuid[target_id]
|
|
||||||
|
|
||||||
return standardized_links
|
return standardized_links
|
||||||
|
|
||||||
|
|
||||||
@@ -260,7 +276,7 @@ def read_node_link_json(
|
|||||||
resource_tree_set = canonicalize_nodes_data(nodes)
|
resource_tree_set = canonicalize_nodes_data(nodes)
|
||||||
|
|
||||||
# 标准化边数据
|
# 标准化边数据
|
||||||
links = data.get("links", [])
|
links = data.get("links", data.get("edges", []))
|
||||||
standardized_links = canonicalize_links_ports(links, resource_tree_set)
|
standardized_links = canonicalize_links_ports(links, resource_tree_set)
|
||||||
|
|
||||||
# 构建 NetworkX 图(需要转换回 dict 格式)
|
# 构建 NetworkX 图(需要转换回 dict 格式)
|
||||||
@@ -284,6 +300,8 @@ def modify_to_backend_format(data: list[dict[str, Any]]) -> list[dict[str, Any]]
|
|||||||
edge["sourceHandle"] = port[source]
|
edge["sourceHandle"] = port[source]
|
||||||
elif "source_port" in edge:
|
elif "source_port" in edge:
|
||||||
edge["sourceHandle"] = edge.pop("source_port")
|
edge["sourceHandle"] = edge.pop("source_port")
|
||||||
|
elif "source_handle" in edge:
|
||||||
|
edge["sourceHandle"] = edge.pop("source_handle")
|
||||||
else:
|
else:
|
||||||
typ = edge.get("type")
|
typ = edge.get("type")
|
||||||
if typ == "communication":
|
if typ == "communication":
|
||||||
@@ -292,6 +310,8 @@ def modify_to_backend_format(data: list[dict[str, Any]]) -> list[dict[str, Any]]
|
|||||||
edge["targetHandle"] = port[target]
|
edge["targetHandle"] = port[target]
|
||||||
elif "target_port" in edge:
|
elif "target_port" in edge:
|
||||||
edge["targetHandle"] = edge.pop("target_port")
|
edge["targetHandle"] = edge.pop("target_port")
|
||||||
|
elif "target_handle" in edge:
|
||||||
|
edge["targetHandle"] = edge.pop("target_handle")
|
||||||
else:
|
else:
|
||||||
typ = edge.get("type")
|
typ = edge.get("type")
|
||||||
if typ == "communication":
|
if typ == "communication":
|
||||||
@@ -597,6 +617,8 @@ def resource_plr_to_ulab(resource_plr: "ResourcePLR", parent_name: str = None, w
|
|||||||
"tube": "tube",
|
"tube": "tube",
|
||||||
"bottle_carrier": "bottle_carrier",
|
"bottle_carrier": "bottle_carrier",
|
||||||
"plate_adapter": "plate_adapter",
|
"plate_adapter": "plate_adapter",
|
||||||
|
"electrode_sheet": "electrode_sheet",
|
||||||
|
"material_hole": "material_hole",
|
||||||
}
|
}
|
||||||
if source in replace_info:
|
if source in replace_info:
|
||||||
return replace_info[source]
|
return replace_info[source]
|
||||||
@@ -757,9 +779,12 @@ def resource_bioyond_to_plr(bioyond_materials: list[dict], type_mapping: Dict[st
|
|||||||
bottle = plr_material[number] = initialize_resource(
|
bottle = plr_material[number] = initialize_resource(
|
||||||
{"name": f'{detail["name"]}_{number}', "class": reverse_type_mapping[typeName][0]}, resource_type=ResourcePLR
|
{"name": f'{detail["name"]}_{number}', "class": reverse_type_mapping[typeName][0]}, resource_type=ResourcePLR
|
||||||
)
|
)
|
||||||
bottle.tracker.liquids = [
|
# 只有具有 tracker 属性的容器才设置液体信息(如 Bottle, Well)
|
||||||
(detail["name"], float(detail.get("quantity", 0)) if detail.get("quantity") else 0)
|
# ResourceHolder 等不支持液体追踪的容器跳过
|
||||||
]
|
if hasattr(bottle, "tracker"):
|
||||||
|
bottle.tracker.liquids = [
|
||||||
|
(detail["name"], float(detail.get("quantity", 0)) if detail.get("quantity") else 0)
|
||||||
|
]
|
||||||
bottle.code = detail.get("code", "")
|
bottle.code = detail.get("code", "")
|
||||||
logger.debug(f" └─ [子物料] {detail['name']} → {plr_material.name}[{number}] (类型:{typeName})")
|
logger.debug(f" └─ [子物料] {detail['name']} → {plr_material.name}[{number}] (类型:{typeName})")
|
||||||
else:
|
else:
|
||||||
@@ -768,9 +793,11 @@ def resource_bioyond_to_plr(bioyond_materials: list[dict], type_mapping: Dict[st
|
|||||||
# 只对有 capacity 属性的容器(液体容器)处理液体追踪
|
# 只对有 capacity 属性的容器(液体容器)处理液体追踪
|
||||||
if hasattr(plr_material, 'capacity'):
|
if hasattr(plr_material, 'capacity'):
|
||||||
bottle = plr_material[0] if plr_material.capacity > 0 else plr_material
|
bottle = plr_material[0] if plr_material.capacity > 0 else plr_material
|
||||||
bottle.tracker.liquids = [
|
# 确保 bottle 有 tracker 属性才设置液体信息
|
||||||
(material["name"], float(material.get("quantity", 0)) if material.get("quantity") else 0)
|
if hasattr(bottle, "tracker"):
|
||||||
]
|
bottle.tracker.liquids = [
|
||||||
|
(material["name"], float(material.get("quantity", 0)) if material.get("quantity") else 0)
|
||||||
|
]
|
||||||
|
|
||||||
plr_materials.append(plr_material)
|
plr_materials.append(plr_material)
|
||||||
|
|
||||||
@@ -799,24 +826,29 @@ def resource_bioyond_to_plr(bioyond_materials: list[dict], type_mapping: Dict[st
|
|||||||
wh_name = loc.get("whName")
|
wh_name = loc.get("whName")
|
||||||
logger.debug(f"[物料位置] {unique_name} 尝试放置到 warehouse: {wh_name} (Bioyond坐标: x={loc.get('x')}, y={loc.get('y')}, z={loc.get('z')})")
|
logger.debug(f"[物料位置] {unique_name} 尝试放置到 warehouse: {wh_name} (Bioyond坐标: x={loc.get('x')}, y={loc.get('y')}, z={loc.get('z')})")
|
||||||
|
|
||||||
|
# Bioyond坐标映射 (重要!): x→行(1=A,2=B...), y→列(1=01,2=02...), z→层(通常=1)
|
||||||
|
# 必须在warehouse映射之前先获取坐标,以便后续调整
|
||||||
|
x = loc.get("x", 1) # 行号 (1-based: 1=A, 2=B, 3=C, 4=D)
|
||||||
|
y = loc.get("y", 1) # 列号 (1-based: 1=01, 2=02, 3=03...)
|
||||||
|
z = loc.get("z", 1) # 层号 (1-based, 通常为1)
|
||||||
|
|
||||||
# 特殊处理: Bioyond的"堆栈1"需要映射到"堆栈1左"或"堆栈1右"
|
# 特殊处理: Bioyond的"堆栈1"需要映射到"堆栈1左"或"堆栈1右"
|
||||||
# 根据列号(x)判断: 1-4映射到左侧, 5-8映射到右侧
|
# 根据列号(y)判断: 1-4映射到左侧, 5-8映射到右侧
|
||||||
if wh_name == "堆栈1":
|
if wh_name == "堆栈1":
|
||||||
x_val = loc.get("x", 1)
|
if 1 <= y <= 4:
|
||||||
if 1 <= x_val <= 4:
|
|
||||||
wh_name = "堆栈1左"
|
wh_name = "堆栈1左"
|
||||||
elif 5 <= x_val <= 8:
|
elif 5 <= y <= 8:
|
||||||
wh_name = "堆栈1右"
|
wh_name = "堆栈1右"
|
||||||
|
y = y - 4 # 调整列号: 5-8映射到1-4
|
||||||
else:
|
else:
|
||||||
logger.warning(f"物料 {material['name']} 的列号 x={x_val} 超出范围,无法映射到堆栈1左或堆栈1右")
|
logger.warning(f"物料 {material['name']} 的列号 y={y} 超出范围,无法映射到堆栈1左或堆栈1右")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# 特殊处理: Bioyond的"站内Tip盒堆栈"也需要进行拆分映射
|
# 特殊处理: Bioyond的"站内Tip盒堆栈"也需要进行拆分映射
|
||||||
if wh_name == "站内Tip盒堆栈":
|
if wh_name == "站内Tip盒堆栈":
|
||||||
y_val = loc.get("y", 1)
|
if y == 1:
|
||||||
if y_val == 1:
|
|
||||||
wh_name = "站内Tip盒堆栈(右)"
|
wh_name = "站内Tip盒堆栈(右)"
|
||||||
elif y_val in [2, 3]:
|
elif y in [2, 3]:
|
||||||
wh_name = "站内Tip盒堆栈(左)"
|
wh_name = "站内Tip盒堆栈(左)"
|
||||||
y = y - 1 # 调整列号,因为左侧仓库对应的 Bioyond y=2 实际上是它的第1列
|
y = y - 1 # 调整列号,因为左侧仓库对应的 Bioyond y=2 实际上是它的第1列
|
||||||
|
|
||||||
@@ -824,15 +856,6 @@ def resource_bioyond_to_plr(bioyond_materials: list[dict], type_mapping: Dict[st
|
|||||||
warehouse = deck.warehouses[wh_name]
|
warehouse = deck.warehouses[wh_name]
|
||||||
logger.debug(f"[Warehouse匹配] 找到warehouse: {wh_name} (容量: {warehouse.capacity}, 行×列: {warehouse.num_items_x}×{warehouse.num_items_y})")
|
logger.debug(f"[Warehouse匹配] 找到warehouse: {wh_name} (容量: {warehouse.capacity}, 行×列: {warehouse.num_items_x}×{warehouse.num_items_y})")
|
||||||
|
|
||||||
# Bioyond坐标映射 (重要!): x→行(1=A,2=B...), y→列(1=01,2=02...), z→层(通常=1)
|
|
||||||
x = loc.get("x", 1) # 行号 (1-based: 1=A, 2=B, 3=C, 4=D)
|
|
||||||
y = loc.get("y", 1) # 列号 (1-based: 1=01, 2=02, 3=03...)
|
|
||||||
z = loc.get("z", 1) # 层号 (1-based, 通常为1)
|
|
||||||
|
|
||||||
# 如果是右侧堆栈,需要调整列号 (5→1, 6→2, 7→3, 8→4)
|
|
||||||
if wh_name == "堆栈1右":
|
|
||||||
y = y - 4 # 将5-8映射到1-4
|
|
||||||
|
|
||||||
# 特殊处理竖向warehouse(站内试剂存放堆栈、测量小瓶仓库)
|
# 特殊处理竖向warehouse(站内试剂存放堆栈、测量小瓶仓库)
|
||||||
# 这些warehouse使用 vertical-col-major 布局
|
# 这些warehouse使用 vertical-col-major 布局
|
||||||
if wh_name in ["站内试剂存放堆栈", "测量小瓶仓库(测密度)"]:
|
if wh_name in ["站内试剂存放堆栈", "测量小瓶仓库(测密度)"]:
|
||||||
|
|||||||
@@ -18,3 +18,9 @@ def register():
|
|||||||
from unilabos.devices.liquid_handling.rviz_backend import UniLiquidHandlerRvizBackend
|
from unilabos.devices.liquid_handling.rviz_backend import UniLiquidHandlerRvizBackend
|
||||||
from unilabos.devices.liquid_handling.laiyu.backend.laiyu_v_backend import UniLiquidHandlerLaiyuBackend
|
from unilabos.devices.liquid_handling.laiyu.backend.laiyu_v_backend import UniLiquidHandlerLaiyuBackend
|
||||||
|
|
||||||
|
# noinspection PyUnresolvedReferences
|
||||||
|
from unilabos.resources.bioyond.decks import (
|
||||||
|
BIOYOND_PolymerReactionStation_Deck,
|
||||||
|
BIOYOND_PolymerPreparationStation_Deck,
|
||||||
|
BIOYOND_YB_Deck,
|
||||||
|
)
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ from pydantic import BaseModel, field_serializer, field_validator, ValidationErr
|
|||||||
from pydantic import Field
|
from pydantic import Field
|
||||||
from typing import List, Tuple, Any, Dict, Literal, Optional, cast, TYPE_CHECKING, Union
|
from typing import List, Tuple, Any, Dict, Literal, Optional, cast, TYPE_CHECKING, Union
|
||||||
|
|
||||||
|
from typing_extensions import TypedDict
|
||||||
|
|
||||||
from unilabos.resources.plr_additional_res_reg import register
|
from unilabos.resources.plr_additional_res_reg import register
|
||||||
from unilabos.utils.log import logger
|
from unilabos.utils.log import logger
|
||||||
|
|
||||||
@@ -13,24 +15,76 @@ if TYPE_CHECKING:
|
|||||||
from pylabrobot.resources import Resource as PLRResource
|
from pylabrobot.resources import Resource as PLRResource
|
||||||
|
|
||||||
|
|
||||||
|
EXTRA_CLASS = "unilabos_resource_class"
|
||||||
|
FRONTEND_POSE_EXTRA = "unilabos_frontend_pose_extra"
|
||||||
|
EXTRA_SAMPLE_UUID = "sample_uuid"
|
||||||
|
EXTRA_UNILABOS_SAMPLE_UUID = "unilabos_sample_uuid"
|
||||||
|
|
||||||
|
# 函数参数名常量 - 用于自动注入 sample_uuids 列表
|
||||||
|
PARAM_SAMPLE_UUIDS = "sample_uuids"
|
||||||
|
|
||||||
|
# JSON Command 中的系统参数字段名
|
||||||
|
JSON_UNILABOS_PARAM = "unilabos_param"
|
||||||
|
|
||||||
|
# 返回值中的 samples 字段名
|
||||||
|
RETURN_UNILABOS_SAMPLES = "unilabos_samples"
|
||||||
|
|
||||||
|
# sample_uuids 参数类型 (用于 virtual bench 等设备添加 sample_uuids 参数)
|
||||||
|
SampleUUIDsType = Dict[str, Optional["PLRResource"]]
|
||||||
|
|
||||||
|
|
||||||
|
class LabSample(TypedDict):
|
||||||
|
sample_uuid: str
|
||||||
|
oss_path: str
|
||||||
|
extra: Dict[str, Any]
|
||||||
|
|
||||||
|
|
||||||
|
class ResourceDictPositionSizeType(TypedDict):
|
||||||
|
depth: float
|
||||||
|
width: float
|
||||||
|
height: float
|
||||||
|
|
||||||
|
|
||||||
class ResourceDictPositionSize(BaseModel):
|
class ResourceDictPositionSize(BaseModel):
|
||||||
depth: float = Field(description="Depth", default=0.0) # z
|
depth: float = Field(description="Depth", default=0.0) # z
|
||||||
width: float = Field(description="Width", default=0.0) # x
|
width: float = Field(description="Width", default=0.0) # x
|
||||||
height: float = Field(description="Height", default=0.0) # y
|
height: float = Field(description="Height", default=0.0) # y
|
||||||
|
|
||||||
|
|
||||||
|
class ResourceDictPositionScaleType(TypedDict):
|
||||||
|
x: float
|
||||||
|
y: float
|
||||||
|
z: float
|
||||||
|
|
||||||
|
|
||||||
class ResourceDictPositionScale(BaseModel):
|
class ResourceDictPositionScale(BaseModel):
|
||||||
x: float = Field(description="x scale", default=0.0)
|
x: float = Field(description="x scale", default=0.0)
|
||||||
y: float = Field(description="y scale", default=0.0)
|
y: float = Field(description="y scale", default=0.0)
|
||||||
z: float = Field(description="z scale", default=0.0)
|
z: float = Field(description="z scale", default=0.0)
|
||||||
|
|
||||||
|
|
||||||
|
class ResourceDictPositionObjectType(TypedDict):
|
||||||
|
x: float
|
||||||
|
y: float
|
||||||
|
z: float
|
||||||
|
|
||||||
|
|
||||||
class ResourceDictPositionObject(BaseModel):
|
class ResourceDictPositionObject(BaseModel):
|
||||||
x: float = Field(description="X coordinate", default=0.0)
|
x: float = Field(description="X coordinate", default=0.0)
|
||||||
y: float = Field(description="Y coordinate", default=0.0)
|
y: float = Field(description="Y coordinate", default=0.0)
|
||||||
z: float = Field(description="Z coordinate", default=0.0)
|
z: float = Field(description="Z coordinate", default=0.0)
|
||||||
|
|
||||||
|
|
||||||
|
class ResourceDictPositionType(TypedDict):
|
||||||
|
size: ResourceDictPositionSizeType
|
||||||
|
scale: ResourceDictPositionScaleType
|
||||||
|
layout: Literal["2d", "x-y", "z-y", "x-z"]
|
||||||
|
position: ResourceDictPositionObjectType
|
||||||
|
position3d: ResourceDictPositionObjectType
|
||||||
|
rotation: ResourceDictPositionObjectType
|
||||||
|
cross_section_type: Literal["rectangle", "circle", "rounded_rectangle"]
|
||||||
|
|
||||||
|
|
||||||
class ResourceDictPosition(BaseModel):
|
class ResourceDictPosition(BaseModel):
|
||||||
size: ResourceDictPositionSize = Field(description="Resource size", default_factory=ResourceDictPositionSize)
|
size: ResourceDictPositionSize = Field(description="Resource size", default_factory=ResourceDictPositionSize)
|
||||||
scale: ResourceDictPositionScale = Field(description="Resource scale", default_factory=ResourceDictPositionScale)
|
scale: ResourceDictPositionScale = Field(description="Resource scale", default_factory=ResourceDictPositionScale)
|
||||||
@@ -47,6 +101,25 @@ class ResourceDictPosition(BaseModel):
|
|||||||
cross_section_type: Literal["rectangle", "circle", "rounded_rectangle"] = Field(
|
cross_section_type: Literal["rectangle", "circle", "rounded_rectangle"] = Field(
|
||||||
description="Cross section type", default="rectangle"
|
description="Cross section type", default="rectangle"
|
||||||
)
|
)
|
||||||
|
extra: Optional[Dict[str, Any]] = Field(description="Extra data", default=None)
|
||||||
|
|
||||||
|
|
||||||
|
class ResourceDictType(TypedDict):
|
||||||
|
id: str
|
||||||
|
uuid: str
|
||||||
|
name: str
|
||||||
|
description: str
|
||||||
|
resource_schema: Dict[str, Any]
|
||||||
|
model: Dict[str, Any]
|
||||||
|
icon: str
|
||||||
|
parent_uuid: Optional[str]
|
||||||
|
parent: Optional["ResourceDictType"]
|
||||||
|
type: Union[Literal["device"], str]
|
||||||
|
klass: str
|
||||||
|
pose: ResourceDictPositionType
|
||||||
|
config: Dict[str, Any]
|
||||||
|
data: Dict[str, Any]
|
||||||
|
extra: Dict[str, Any]
|
||||||
|
|
||||||
|
|
||||||
# 统一的资源字典模型,parent 自动序列化为 parent_uuid,children 不序列化
|
# 统一的资源字典模型,parent 自动序列化为 parent_uuid,children 不序列化
|
||||||
@@ -338,8 +411,18 @@ class ResourceTreeSet(object):
|
|||||||
"deck": "deck",
|
"deck": "deck",
|
||||||
"tip_rack": "tip_rack",
|
"tip_rack": "tip_rack",
|
||||||
"tip_spot": "tip_spot",
|
"tip_spot": "tip_spot",
|
||||||
|
"tip": "tip", # 添加 tip 类型支持
|
||||||
"tube": "tube",
|
"tube": "tube",
|
||||||
"bottle_carrier": "bottle_carrier",
|
"bottle_carrier": "bottle_carrier",
|
||||||
|
"material_hole": "material_hole",
|
||||||
|
"container": "container",
|
||||||
|
"material_plate": "material_plate",
|
||||||
|
"electrode_sheet": "electrode_sheet",
|
||||||
|
"warehouse": "warehouse",
|
||||||
|
"magazine_holder": "magazine_holder",
|
||||||
|
"resource_group": "resource_group",
|
||||||
|
"trash": "trash",
|
||||||
|
"plate_adapter": "plate_adapter",
|
||||||
}
|
}
|
||||||
if source in replace_info:
|
if source in replace_info:
|
||||||
return replace_info[source]
|
return replace_info[source]
|
||||||
@@ -383,6 +466,7 @@ class ResourceTreeSet(object):
|
|||||||
"position3d": raw_pos,
|
"position3d": raw_pos,
|
||||||
"rotation": d["rotation"],
|
"rotation": d["rotation"],
|
||||||
"cross_section_type": d.get("cross_section_type", "rectangle"),
|
"cross_section_type": d.get("cross_section_type", "rectangle"),
|
||||||
|
"extra": extra.get(FRONTEND_POSE_EXTRA)
|
||||||
}
|
}
|
||||||
|
|
||||||
# 先构建当前节点的字典(不包含children)
|
# 先构建当前节点的字典(不包含children)
|
||||||
@@ -393,7 +477,7 @@ class ResourceTreeSet(object):
|
|||||||
"parent": parent_resource, # 直接传入 ResourceDict 对象
|
"parent": parent_resource, # 直接传入 ResourceDict 对象
|
||||||
"parent_uuid": parent_uuid, # 使用 parent_uuid 而不是 parent 对象
|
"parent_uuid": parent_uuid, # 使用 parent_uuid 而不是 parent 对象
|
||||||
"type": replace_plr_type(d.get("category", "")),
|
"type": replace_plr_type(d.get("category", "")),
|
||||||
"class": d.get("class", ""),
|
"class": extra.get(EXTRA_CLASS, ""),
|
||||||
"position": pos,
|
"position": pos,
|
||||||
"pose": pos,
|
"pose": pos,
|
||||||
"config": {
|
"config": {
|
||||||
@@ -443,7 +527,7 @@ class ResourceTreeSet(object):
|
|||||||
trees.append(tree_instance)
|
trees.append(tree_instance)
|
||||||
return cls(trees)
|
return cls(trees)
|
||||||
|
|
||||||
def to_plr_resources(self) -> List["PLRResource"]:
|
def to_plr_resources(self, skip_devices=True) -> List["PLRResource"]:
|
||||||
"""
|
"""
|
||||||
将 ResourceTreeSet 转换为 PLR 资源列表
|
将 ResourceTreeSet 转换为 PLR 资源列表
|
||||||
|
|
||||||
@@ -468,6 +552,8 @@ class ResourceTreeSet(object):
|
|||||||
name_to_uuid[node.res_content.name] = node.res_content.uuid
|
name_to_uuid[node.res_content.name] = node.res_content.uuid
|
||||||
all_states[node.res_content.name] = node.res_content.data
|
all_states[node.res_content.name] = node.res_content.data
|
||||||
name_to_extra[node.res_content.name] = node.res_content.extra
|
name_to_extra[node.res_content.name] = node.res_content.extra
|
||||||
|
name_to_extra[node.res_content.name][FRONTEND_POSE_EXTRA] = node.res_content.pose.extra
|
||||||
|
name_to_extra[node.res_content.name][EXTRA_CLASS] = node.res_content.klass
|
||||||
for child in node.children:
|
for child in node.children:
|
||||||
collect_node_data(child, name_to_uuid, all_states, name_to_extra)
|
collect_node_data(child, name_to_uuid, all_states, name_to_extra)
|
||||||
|
|
||||||
@@ -512,7 +598,10 @@ class ResourceTreeSet(object):
|
|||||||
plr_dict = node_to_plr_dict(tree.root_node, has_model)
|
plr_dict = node_to_plr_dict(tree.root_node, has_model)
|
||||||
try:
|
try:
|
||||||
sub_cls = find_subclass(plr_dict["type"], PLRResource)
|
sub_cls = find_subclass(plr_dict["type"], PLRResource)
|
||||||
if sub_cls is None:
|
if skip_devices and plr_dict["type"] == "device":
|
||||||
|
logger.info(f"跳过更新 {plr_dict['name']} 设备是class")
|
||||||
|
continue
|
||||||
|
elif sub_cls is None:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"无法找到类型 {plr_dict['type']} 对应的 PLR 资源类。原始信息:{tree.root_node.res_content}"
|
f"无法找到类型 {plr_dict['type']} 对应的 PLR 资源类。原始信息:{tree.root_node.res_content}"
|
||||||
)
|
)
|
||||||
@@ -520,6 +609,11 @@ class ResourceTreeSet(object):
|
|||||||
if "category" not in spec.parameters:
|
if "category" not in spec.parameters:
|
||||||
plr_dict.pop("category", None)
|
plr_dict.pop("category", None)
|
||||||
plr_resource = sub_cls.deserialize(plr_dict, allow_marshal=True)
|
plr_resource = sub_cls.deserialize(plr_dict, allow_marshal=True)
|
||||||
|
from pylabrobot.resources import Coordinate
|
||||||
|
from pylabrobot.serializer import deserialize
|
||||||
|
|
||||||
|
location = cast(Coordinate, deserialize(plr_dict["location"]))
|
||||||
|
plr_resource.location = location
|
||||||
plr_resource.load_all_state(all_states)
|
plr_resource.load_all_state(all_states)
|
||||||
# 使用 DeviceNodeResourceTracker 设置 UUID 和 Extra
|
# 使用 DeviceNodeResourceTracker 设置 UUID 和 Extra
|
||||||
tracker.loop_set_uuid(plr_resource, name_to_uuid)
|
tracker.loop_set_uuid(plr_resource, name_to_uuid)
|
||||||
@@ -527,7 +621,7 @@ class ResourceTreeSet(object):
|
|||||||
plr_resources.append(plr_resource)
|
plr_resources.append(plr_resource)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"转换 PLR 资源失败: {e}")
|
logger.error(f"转换 PLR 资源失败: {e} {str(plr_dict)[:1000]}")
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
logger.error(f"堆栈: {traceback.format_exc()}")
|
logger.error(f"堆栈: {traceback.format_exc()}")
|
||||||
@@ -747,14 +841,27 @@ class ResourceTreeSet(object):
|
|||||||
f"从远端同步了 {added_count} 个物料子树"
|
f"从远端同步了 {added_count} 个物料子树"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
# 情况2: 二级是物料(不是 device)
|
# 二级物料已存在,比较三级子节点是否缺失
|
||||||
if remote_child_name not in local_children_map:
|
local_material = local_children_map[remote_child_name]
|
||||||
# 引入整个子树
|
local_material_children_map = {child.res_content.name: child for child in
|
||||||
remote_child.res_content.parent = local_device.res_content
|
local_material.children}
|
||||||
local_device.children.append(remote_child)
|
added_count = 0
|
||||||
logger.info(f"Device '{remote_root_id}': 从远端同步物料子树 '{remote_child_name}'")
|
for remote_sub in remote_child.children:
|
||||||
else:
|
remote_sub_name = remote_sub.res_content.name
|
||||||
logger.info(f"物料 '{remote_root_id}/{remote_child_name}' 已存在,跳过")
|
if remote_sub_name not in local_material_children_map:
|
||||||
|
remote_sub.res_content.parent = local_material.res_content
|
||||||
|
local_material.children.append(remote_sub)
|
||||||
|
added_count += 1
|
||||||
|
else:
|
||||||
|
logger.info(
|
||||||
|
f"物料 '{remote_root_id}/{remote_child_name}/{remote_sub_name}' "
|
||||||
|
f"已存在,跳过"
|
||||||
|
)
|
||||||
|
if added_count > 0:
|
||||||
|
logger.info(
|
||||||
|
f"物料 '{remote_root_id}/{remote_child_name}': "
|
||||||
|
f"从远端同步了 {added_count} 个子物料"
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
# 情况1: 一级节点是物料(不是 device)
|
# 情况1: 一级节点是物料(不是 device)
|
||||||
# 检查是否已存在
|
# 检查是否已存在
|
||||||
@@ -986,7 +1093,7 @@ class DeviceNodeResourceTracker(object):
|
|||||||
extra = name_to_extra_map[resource_name]
|
extra = name_to_extra_map[resource_name]
|
||||||
self.set_resource_extra(res, extra)
|
self.set_resource_extra(res, extra)
|
||||||
if len(extra):
|
if len(extra):
|
||||||
logger.debug(f"设置资源Extra: {resource_name} -> {extra}")
|
logger.trace(f"设置资源Extra: {resource_name} -> {extra}")
|
||||||
return 1
|
return 1
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|||||||
@@ -44,8 +44,7 @@ def ros2_device_node(
|
|||||||
# 从属性中自动发现可发布状态
|
# 从属性中自动发现可发布状态
|
||||||
if status_types is None:
|
if status_types is None:
|
||||||
status_types = {}
|
status_types = {}
|
||||||
if device_config is None:
|
assert device_config is not None, "device_config cannot be None"
|
||||||
raise ValueError("device_config cannot be None")
|
|
||||||
if action_value_mappings is None:
|
if action_value_mappings is None:
|
||||||
action_value_mappings = {}
|
action_value_mappings = {}
|
||||||
if hardware_interface is None:
|
if hardware_interface is None:
|
||||||
|
|||||||
@@ -4,8 +4,20 @@ import json
|
|||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
from typing import get_type_hints, TypeVar, Generic, Dict, Any, Type, TypedDict, Optional, List, TYPE_CHECKING, Union, \
|
from typing import (
|
||||||
Tuple
|
get_type_hints,
|
||||||
|
TypeVar,
|
||||||
|
Generic,
|
||||||
|
Dict,
|
||||||
|
Any,
|
||||||
|
Type,
|
||||||
|
TypedDict,
|
||||||
|
Optional,
|
||||||
|
List,
|
||||||
|
TYPE_CHECKING,
|
||||||
|
Union,
|
||||||
|
Tuple,
|
||||||
|
)
|
||||||
|
|
||||||
from concurrent.futures import ThreadPoolExecutor
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
import asyncio
|
import asyncio
|
||||||
@@ -48,6 +60,9 @@ from unilabos.resources.resource_tracker import (
|
|||||||
ResourceTreeSet,
|
ResourceTreeSet,
|
||||||
ResourceTreeInstance,
|
ResourceTreeInstance,
|
||||||
ResourceDictInstance,
|
ResourceDictInstance,
|
||||||
|
EXTRA_SAMPLE_UUID,
|
||||||
|
PARAM_SAMPLE_UUIDS,
|
||||||
|
JSON_UNILABOS_PARAM,
|
||||||
)
|
)
|
||||||
from unilabos.ros.utils.driver_creator import WorkstationNodeCreator, PyLabRobotCreator, DeviceClassCreator
|
from unilabos.ros.utils.driver_creator import WorkstationNodeCreator, PyLabRobotCreator, DeviceClassCreator
|
||||||
from rclpy.task import Task, Future
|
from rclpy.task import Task, Future
|
||||||
@@ -131,7 +146,7 @@ def init_wrapper(
|
|||||||
device_id: str,
|
device_id: str,
|
||||||
device_uuid: str,
|
device_uuid: str,
|
||||||
driver_class: type[T],
|
driver_class: type[T],
|
||||||
device_config: ResourceTreeInstance,
|
device_config: ResourceDictInstance,
|
||||||
status_types: Dict[str, Any],
|
status_types: Dict[str, Any],
|
||||||
action_value_mappings: Dict[str, Any],
|
action_value_mappings: Dict[str, Any],
|
||||||
hardware_interface: Dict[str, Any],
|
hardware_interface: Dict[str, Any],
|
||||||
@@ -216,14 +231,15 @@ class PropertyPublisher:
|
|||||||
|
|
||||||
def publish_property(self):
|
def publish_property(self):
|
||||||
try:
|
try:
|
||||||
self.node.lab_logger().trace(f"【.publish_property】开始发布属性: {self.name}")
|
# self.node.lab_logger().trace(f"【.publish_property】开始发布属性: {self.name}")
|
||||||
value = self.get_property()
|
value = self.get_property()
|
||||||
if self.print_publish:
|
if self.print_publish:
|
||||||
self.node.lab_logger().trace(f"【.publish_property】发布 {self.msg_type}: {value}")
|
pass
|
||||||
|
# self.node.lab_logger().trace(f"【.publish_property】发布 {self.msg_type}: {value}")
|
||||||
if value is not None:
|
if value is not None:
|
||||||
msg = convert_to_ros_msg(self.msg_type, value)
|
msg = convert_to_ros_msg(self.msg_type, value)
|
||||||
self.publisher_.publish(msg)
|
self.publisher_.publish(msg)
|
||||||
self.node.lab_logger().trace(f"【.publish_property】属性 {self.name} 发布成功")
|
# self.node.lab_logger().trace(f"【.publish_property】属性 {self.name} 发布成功")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.node.lab_logger().error(
|
self.node.lab_logger().error(
|
||||||
f"【.publish_property】发布属性 {self.publisher_.topic} 出错: {str(e)}\n{traceback.format_exc()}"
|
f"【.publish_property】发布属性 {self.publisher_.topic} 出错: {str(e)}\n{traceback.format_exc()}"
|
||||||
@@ -263,6 +279,7 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
self,
|
self,
|
||||||
driver_instance: T,
|
driver_instance: T,
|
||||||
device_id: str,
|
device_id: str,
|
||||||
|
registry_name: str,
|
||||||
device_uuid: str,
|
device_uuid: str,
|
||||||
status_types: Dict[str, Any],
|
status_types: Dict[str, Any],
|
||||||
action_value_mappings: Dict[str, Any],
|
action_value_mappings: Dict[str, Any],
|
||||||
@@ -284,6 +301,7 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
"""
|
"""
|
||||||
self.driver_instance = driver_instance
|
self.driver_instance = driver_instance
|
||||||
self.device_id = device_id
|
self.device_id = device_id
|
||||||
|
self.registry_name = registry_name
|
||||||
self.uuid = device_uuid
|
self.uuid = device_uuid
|
||||||
self.publish_high_frequency = False
|
self.publish_high_frequency = False
|
||||||
self.callback_group = ReentrantCallbackGroup()
|
self.callback_group = ReentrantCallbackGroup()
|
||||||
@@ -361,6 +379,7 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
from pylabrobot.resources.deck import Deck
|
from pylabrobot.resources.deck import Deck
|
||||||
from pylabrobot.resources import Coordinate
|
from pylabrobot.resources import Coordinate
|
||||||
from pylabrobot.resources import Plate
|
from pylabrobot.resources import Plate
|
||||||
|
|
||||||
# 物料传输到对应的node节点
|
# 物料传输到对应的node节点
|
||||||
client = self._resource_clients["c2s_update_resource_tree"]
|
client = self._resource_clients["c2s_update_resource_tree"]
|
||||||
request = SerialCommand.Request()
|
request = SerialCommand.Request()
|
||||||
@@ -388,33 +407,29 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
rts: ResourceTreeSet = ResourceTreeSet.from_raw_dict_list(input_resources)
|
rts: ResourceTreeSet = ResourceTreeSet.from_raw_dict_list(input_resources)
|
||||||
parent_resource = None
|
parent_resource = None
|
||||||
if bind_parent_id != self.node_name:
|
if bind_parent_id != self.node_name:
|
||||||
parent_resource = self.resource_tracker.figure_resource(
|
parent_resource = self.resource_tracker.figure_resource({"name": bind_parent_id})
|
||||||
{"name": bind_parent_id}
|
|
||||||
)
|
|
||||||
for r in rts.root_nodes:
|
for r in rts.root_nodes:
|
||||||
# noinspection PyUnresolvedReferences
|
# noinspection PyUnresolvedReferences
|
||||||
r.res_content.parent_uuid = parent_resource.unilabos_uuid
|
r.res_content.parent_uuid = parent_resource.unilabos_uuid
|
||||||
else:
|
else:
|
||||||
for r in rts.root_nodes:
|
for r in rts.root_nodes:
|
||||||
r.res_content.parent_uuid = self.uuid
|
r.res_content.parent_uuid = self.uuid
|
||||||
|
rts_plr_instances = rts.to_plr_resources()
|
||||||
if len(LIQUID_INPUT_SLOT) and LIQUID_INPUT_SLOT[0] == -1 and len(rts.root_nodes) == 1 and isinstance(rts.root_nodes[0], RegularContainer):
|
if len(rts.root_nodes) == 1 and isinstance(rts_plr_instances[0], RegularContainer):
|
||||||
# noinspection PyTypeChecker
|
# noinspection PyTypeChecker
|
||||||
container_instance: RegularContainer = rts.root_nodes[0]
|
container_instance: RegularContainer = rts_plr_instances[0]
|
||||||
found_resources = self.resource_tracker.figure_resource(
|
found_resources = self.resource_tracker.figure_resource(
|
||||||
{"id": container_instance.name}, try_mode=True
|
{"name": container_instance.name}, try_mode=True
|
||||||
)
|
)
|
||||||
if not len(found_resources):
|
if not len(found_resources):
|
||||||
self.resource_tracker.add_resource(container_instance)
|
self.resource_tracker.add_resource(container_instance)
|
||||||
logger.info(f"添加物料{container_instance.name}到资源跟踪器")
|
logger.info(f"添加物料{container_instance.name}到资源跟踪器")
|
||||||
else:
|
else:
|
||||||
assert (
|
assert len(found_resources) == 1, f"找到多个同名物料: {container_instance.name}, 请检查物料系统"
|
||||||
len(found_resources) == 1
|
|
||||||
), f"找到多个同名物料: {container_instance.name}, 请检查物料系统"
|
|
||||||
found_resource = found_resources[0]
|
found_resource = found_resources[0]
|
||||||
if isinstance(found_resource, RegularContainer):
|
if isinstance(found_resource, RegularContainer):
|
||||||
logger.info(f"更新物料{container_instance.name}的数据{found_resource.state}")
|
logger.info(f"更新物料{container_instance.name}的数据{found_resource.state}")
|
||||||
found_resource.state.update(json.loads(container_instance.state))
|
found_resource.state.update(container_instance.state)
|
||||||
elif isinstance(found_resource, dict):
|
elif isinstance(found_resource, dict):
|
||||||
raise ValueError("已不支持 字典 版本的RegularContainer")
|
raise ValueError("已不支持 字典 版本的RegularContainer")
|
||||||
else:
|
else:
|
||||||
@@ -422,14 +437,16 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
f"更新物料{container_instance.name}出现不支持的数据类型{type(found_resource)} {found_resource}"
|
f"更新物料{container_instance.name}出现不支持的数据类型{type(found_resource)} {found_resource}"
|
||||||
)
|
)
|
||||||
# noinspection PyUnresolvedReferences
|
# noinspection PyUnresolvedReferences
|
||||||
request.command = json.dumps({
|
request.command = json.dumps(
|
||||||
"action": "add",
|
{
|
||||||
"data": {
|
"action": "add",
|
||||||
"data": rts.dump(),
|
"data": {
|
||||||
"mount_uuid": parent_resource.unilabos_uuid if parent_resource is not None else "",
|
"data": rts.dump(),
|
||||||
"first_add": False,
|
"mount_uuid": parent_resource.unilabos_uuid if parent_resource is not None else self.uuid,
|
||||||
},
|
"first_add": False,
|
||||||
})
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
tree_response: SerialCommand.Response = await client.call_async(request)
|
tree_response: SerialCommand.Response = await client.call_async(request)
|
||||||
uuid_maps = json.loads(tree_response.response)
|
uuid_maps = json.loads(tree_response.response)
|
||||||
plr_instances = rts.to_plr_resources()
|
plr_instances = rts.to_plr_resources()
|
||||||
@@ -443,7 +460,7 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
}
|
}
|
||||||
res.response = json.dumps(final_response)
|
res.response = json.dumps(final_response)
|
||||||
# 如果driver自己就有assign的方法,那就使用driver自己的assign方法
|
# 如果driver自己就有assign的方法,那就使用driver自己的assign方法
|
||||||
if hasattr(self.driver_instance, "create_resource"):
|
if hasattr(self.driver_instance, "create_resource") and self.node_name != "host_node":
|
||||||
create_resource_func = getattr(self.driver_instance, "create_resource")
|
create_resource_func = getattr(self.driver_instance, "create_resource")
|
||||||
try:
|
try:
|
||||||
ret = create_resource_func(
|
ret = create_resource_func(
|
||||||
@@ -471,7 +488,9 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
if len(ADD_LIQUID_TYPE) == 1 and len(LIQUID_VOLUME) == 1 and len(LIQUID_INPUT_SLOT) > 1:
|
if len(ADD_LIQUID_TYPE) == 1 and len(LIQUID_VOLUME) == 1 and len(LIQUID_INPUT_SLOT) > 1:
|
||||||
ADD_LIQUID_TYPE = ADD_LIQUID_TYPE * len(LIQUID_INPUT_SLOT)
|
ADD_LIQUID_TYPE = ADD_LIQUID_TYPE * len(LIQUID_INPUT_SLOT)
|
||||||
LIQUID_VOLUME = LIQUID_VOLUME * len(LIQUID_INPUT_SLOT)
|
LIQUID_VOLUME = LIQUID_VOLUME * len(LIQUID_INPUT_SLOT)
|
||||||
self.lab_logger().warning(f"增加液体资源时,数量为1,自动补全为 {len(LIQUID_INPUT_SLOT)} 个")
|
self.lab_logger().warning(
|
||||||
|
f"增加液体资源时,数量为1,自动补全为 {len(LIQUID_INPUT_SLOT)} 个"
|
||||||
|
)
|
||||||
for liquid_type, liquid_volume, liquid_input_slot in zip(
|
for liquid_type, liquid_volume, liquid_input_slot in zip(
|
||||||
ADD_LIQUID_TYPE, LIQUID_VOLUME, LIQUID_INPUT_SLOT
|
ADD_LIQUID_TYPE, LIQUID_VOLUME, LIQUID_INPUT_SLOT
|
||||||
):
|
):
|
||||||
@@ -490,9 +509,15 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
input_wells = []
|
input_wells = []
|
||||||
for r in LIQUID_INPUT_SLOT:
|
for r in LIQUID_INPUT_SLOT:
|
||||||
input_wells.append(plr_instance.children[r])
|
input_wells.append(plr_instance.children[r])
|
||||||
final_response["liquid_input_resource_tree"] = ResourceTreeSet.from_plr_resources(input_wells).dump()
|
final_response["liquid_input_resource_tree"] = ResourceTreeSet.from_plr_resources(
|
||||||
|
input_wells
|
||||||
|
).dump()
|
||||||
res.response = json.dumps(final_response)
|
res.response = json.dumps(final_response)
|
||||||
if issubclass(parent_resource.__class__, Deck) and hasattr(parent_resource, "assign_child_at_slot") and "slot" in other_calling_param:
|
if (
|
||||||
|
issubclass(parent_resource.__class__, Deck)
|
||||||
|
and hasattr(parent_resource, "assign_child_at_slot")
|
||||||
|
and "slot" in other_calling_param
|
||||||
|
):
|
||||||
other_calling_param["slot"] = int(other_calling_param["slot"])
|
other_calling_param["slot"] = int(other_calling_param["slot"])
|
||||||
parent_resource.assign_child_at_slot(plr_instance, **other_calling_param)
|
parent_resource.assign_child_at_slot(plr_instance, **other_calling_param)
|
||||||
else:
|
else:
|
||||||
@@ -507,14 +532,16 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
rts_with_parent = ResourceTreeSet.from_plr_resources([parent_resource])
|
rts_with_parent = ResourceTreeSet.from_plr_resources([parent_resource])
|
||||||
if rts_with_parent.root_nodes[0].res_content.uuid_parent is None:
|
if rts_with_parent.root_nodes[0].res_content.uuid_parent is None:
|
||||||
rts_with_parent.root_nodes[0].res_content.parent_uuid = self.uuid
|
rts_with_parent.root_nodes[0].res_content.parent_uuid = self.uuid
|
||||||
request.command = json.dumps({
|
request.command = json.dumps(
|
||||||
"action": "add",
|
{
|
||||||
"data": {
|
"action": "add",
|
||||||
"data": rts_with_parent.dump(),
|
"data": {
|
||||||
"mount_uuid": rts_with_parent.root_nodes[0].res_content.uuid_parent,
|
"data": rts_with_parent.dump(),
|
||||||
"first_add": False,
|
"mount_uuid": rts_with_parent.root_nodes[0].res_content.uuid_parent,
|
||||||
},
|
"first_add": False,
|
||||||
})
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
tree_response: SerialCommand.Response = await client.call_async(request)
|
tree_response: SerialCommand.Response = await client.call_async(request)
|
||||||
uuid_maps = json.loads(tree_response.response)
|
uuid_maps = json.loads(tree_response.response)
|
||||||
self.resource_tracker.loop_update_uuid(input_resources, uuid_maps)
|
self.resource_tracker.loop_update_uuid(input_resources, uuid_maps)
|
||||||
@@ -811,7 +838,9 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
}
|
}
|
||||||
|
|
||||||
def _handle_update(
|
def _handle_update(
|
||||||
plr_resources: List[Union[ResourcePLR, ResourceDictInstance]], tree_set: ResourceTreeSet, additional_add_params: Dict[str, Any]
|
plr_resources: List[Union[ResourcePLR, ResourceDictInstance]],
|
||||||
|
tree_set: ResourceTreeSet,
|
||||||
|
additional_add_params: Dict[str, Any],
|
||||||
) -> Tuple[Dict[str, Any], List[ResourcePLR]]:
|
) -> Tuple[Dict[str, Any], List[ResourcePLR]]:
|
||||||
"""
|
"""
|
||||||
处理资源更新操作的内部函数
|
处理资源更新操作的内部函数
|
||||||
@@ -836,7 +865,10 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
original_parent_resource = original_instance.parent
|
original_parent_resource = original_instance.parent
|
||||||
original_parent_resource_uuid = getattr(original_parent_resource, "unilabos_uuid", None)
|
original_parent_resource_uuid = getattr(original_parent_resource, "unilabos_uuid", None)
|
||||||
target_parent_resource_uuid = tree.root_node.res_content.uuid_parent
|
target_parent_resource_uuid = tree.root_node.res_content.uuid_parent
|
||||||
not_same_parent = original_parent_resource_uuid != target_parent_resource_uuid and original_parent_resource is not None
|
not_same_parent = (
|
||||||
|
original_parent_resource_uuid != target_parent_resource_uuid
|
||||||
|
and original_parent_resource is not None
|
||||||
|
)
|
||||||
old_name = original_instance.name
|
old_name = original_instance.name
|
||||||
new_name = plr_resource.name
|
new_name = plr_resource.name
|
||||||
parent_appended = False
|
parent_appended = False
|
||||||
@@ -872,11 +904,35 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
else:
|
else:
|
||||||
# 判断是否变更了resource_site,重新登记
|
# 判断是否变更了resource_site,重新登记
|
||||||
target_site = original_instance.unilabos_extra.get("update_resource_site")
|
target_site = original_instance.unilabos_extra.get("update_resource_site")
|
||||||
sites = original_instance.parent.sites if original_instance.parent is not None and hasattr(original_instance.parent, "sites") else None
|
sites = (
|
||||||
site_names = list(original_instance.parent._ordering.keys()) if original_instance.parent is not None and hasattr(original_instance.parent, "sites") else []
|
original_instance.parent.sites
|
||||||
|
if original_instance.parent is not None and hasattr(original_instance.parent, "sites")
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
site_names = (
|
||||||
|
list(original_instance.parent._ordering.keys())
|
||||||
|
if original_instance.parent is not None and hasattr(original_instance.parent, "sites")
|
||||||
|
else []
|
||||||
|
)
|
||||||
if target_site is not None and sites is not None and site_names is not None:
|
if target_site is not None and sites is not None and site_names is not None:
|
||||||
site_index = sites.index(original_instance)
|
site_index = None
|
||||||
site_name = site_names[site_index]
|
try:
|
||||||
|
# sites 可能是 Resource 列表或 dict 列表 (如 PRCXI9300Deck)
|
||||||
|
# 只有itemized_carrier在使用,准备弃用
|
||||||
|
site_index = sites.index(original_instance)
|
||||||
|
except ValueError:
|
||||||
|
# dict 类型的 sites: 通过name匹配
|
||||||
|
for idx, site in enumerate(sites):
|
||||||
|
if original_instance.name == site["occupied_by"]:
|
||||||
|
site_index = idx
|
||||||
|
break
|
||||||
|
elif (original_instance.location.x == site["position"]["x"] and original_instance.location.y == site["position"]["y"] and original_instance.location.z == site["position"]["z"]):
|
||||||
|
site_index = idx
|
||||||
|
break
|
||||||
|
if site_index is None:
|
||||||
|
site_name = None
|
||||||
|
else:
|
||||||
|
site_name = site_names[site_index]
|
||||||
if site_name != target_site:
|
if site_name != target_site:
|
||||||
parent = self.transfer_to_new_resource(original_instance, tree, additional_add_params)
|
parent = self.transfer_to_new_resource(original_instance, tree, additional_add_params)
|
||||||
if parent is not None:
|
if parent is not None:
|
||||||
@@ -884,6 +940,17 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
parent_appended = True
|
parent_appended = True
|
||||||
|
|
||||||
# 加载状态
|
# 加载状态
|
||||||
|
# noinspection PyProtectedMember
|
||||||
|
original_instance._size_x = plr_resource._size_x
|
||||||
|
# noinspection PyProtectedMember
|
||||||
|
original_instance._size_y = plr_resource._size_y
|
||||||
|
# noinspection PyProtectedMember
|
||||||
|
original_instance._size_z = plr_resource._size_z
|
||||||
|
# noinspection PyProtectedMember
|
||||||
|
original_instance._local_size_z = plr_resource._local_size_z
|
||||||
|
original_instance.location = plr_resource.location
|
||||||
|
original_instance.rotation = plr_resource.rotation
|
||||||
|
original_instance.barcode = plr_resource.barcode
|
||||||
original_instance.load_all_state(states)
|
original_instance.load_all_state(states)
|
||||||
child_count = len(original_instance.get_all_children())
|
child_count = len(original_instance.get_all_children())
|
||||||
self.lab_logger().info(
|
self.lab_logger().info(
|
||||||
@@ -907,9 +974,7 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
action = i.get("action") # remove, add, update
|
action = i.get("action") # remove, add, update
|
||||||
resources_uuid: List[str] = i.get("data") # 资源数据
|
resources_uuid: List[str] = i.get("data") # 资源数据
|
||||||
additional_add_params = i.get("additional_add_params", {}) # 额外参数
|
additional_add_params = i.get("additional_add_params", {}) # 额外参数
|
||||||
self.lab_logger().trace(
|
self.lab_logger().trace(f"[资源同步] 处理 {action}, " f"resources count: {len(resources_uuid)}")
|
||||||
f"[资源同步] 处理 {action}, " f"resources count: {len(resources_uuid)}"
|
|
||||||
)
|
|
||||||
tree_set = None
|
tree_set = None
|
||||||
if action in ["add", "update"]:
|
if action in ["add", "update"]:
|
||||||
tree_set = await self.get_resource(
|
tree_set = await self.get_resource(
|
||||||
@@ -936,10 +1001,14 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
tree.root_node.res_content.parent_uuid = self.uuid
|
tree.root_node.res_content.parent_uuid = self.uuid
|
||||||
r = SerialCommand.Request()
|
r = SerialCommand.Request()
|
||||||
r.command = json.dumps(
|
r.command = json.dumps(
|
||||||
{"data": {"data": new_tree_set.dump()}, "action": "update"}) # 和Update Resource一致
|
{"data": {"data": new_tree_set.dump()}, "action": "update"}
|
||||||
|
) # 和Update Resource一致
|
||||||
response: SerialCommand_Response = await self._resource_clients[
|
response: SerialCommand_Response = await self._resource_clients[
|
||||||
"c2s_update_resource_tree"].call_async(r) # type: ignore
|
"c2s_update_resource_tree"
|
||||||
self.lab_logger().info(f"确认资源云端 Add 结果: {response.response}")
|
].call_async(
|
||||||
|
r
|
||||||
|
) # type: ignore
|
||||||
|
self.lab_logger().trace(f"确认资源云端 Add 结果: {response.response}")
|
||||||
results.append(result)
|
results.append(result)
|
||||||
elif action == "update":
|
elif action == "update":
|
||||||
if tree_set is None:
|
if tree_set is None:
|
||||||
@@ -958,10 +1027,14 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
tree.root_node.res_content.parent_uuid = self.uuid
|
tree.root_node.res_content.parent_uuid = self.uuid
|
||||||
r = SerialCommand.Request()
|
r = SerialCommand.Request()
|
||||||
r.command = json.dumps(
|
r.command = json.dumps(
|
||||||
{"data": {"data": new_tree_set.dump()}, "action": "update"}) # 和Update Resource一致
|
{"data": {"data": new_tree_set.dump()}, "action": "update"}
|
||||||
|
) # 和Update Resource一致
|
||||||
response: SerialCommand_Response = await self._resource_clients[
|
response: SerialCommand_Response = await self._resource_clients[
|
||||||
"c2s_update_resource_tree"].call_async(r) # type: ignore
|
"c2s_update_resource_tree"
|
||||||
self.lab_logger().info(f"确认资源云端 Update 结果: {response.response}")
|
].call_async(
|
||||||
|
r
|
||||||
|
) # type: ignore
|
||||||
|
self.lab_logger().trace(f"确认资源云端 Update 结果: {response.response}")
|
||||||
results.append(result)
|
results.append(result)
|
||||||
elif action == "remove":
|
elif action == "remove":
|
||||||
result = _handle_remove(resources_uuid)
|
result = _handle_remove(resources_uuid)
|
||||||
@@ -1107,6 +1180,7 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
"machine_name": BasicConfig.machine_name,
|
"machine_name": BasicConfig.machine_name,
|
||||||
"type": "slave",
|
"type": "slave",
|
||||||
"edge_device_id": self.device_id,
|
"edge_device_id": self.device_id,
|
||||||
|
"registry_name": self.registry_name,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ensure_ascii=False,
|
ensure_ascii=False,
|
||||||
@@ -1319,26 +1393,41 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
resource_inputs = action_kwargs[k] if is_sequence else [action_kwargs[k]]
|
resource_inputs = action_kwargs[k] if is_sequence else [action_kwargs[k]]
|
||||||
|
|
||||||
# 批量查询资源
|
# 批量查询资源
|
||||||
queried_resources = []
|
queried_resources: list = [None] * len(resource_inputs)
|
||||||
for resource_data in resource_inputs:
|
uuid_indices: list[tuple[int, str, dict]] = [] # (index, uuid, resource_data)
|
||||||
|
|
||||||
|
# 第一遍:处理没有uuid的资源,收集有uuid的资源信息
|
||||||
|
for idx, resource_data in enumerate(resource_inputs):
|
||||||
unilabos_uuid = resource_data.get("data", {}).get("unilabos_uuid")
|
unilabos_uuid = resource_data.get("data", {}).get("unilabos_uuid")
|
||||||
if unilabos_uuid is None:
|
if unilabos_uuid is None:
|
||||||
plr_resource = await self.get_resource_with_dir(
|
plr_resource = await self.get_resource_with_dir(
|
||||||
resource_id=resource_data["id"], with_children=True
|
resource_id=resource_data["id"], with_children=True
|
||||||
)
|
)
|
||||||
|
if "sample_id" in resource_data:
|
||||||
|
plr_resource.unilabos_extra[EXTRA_SAMPLE_UUID] = resource_data["sample_id"]
|
||||||
|
queried_resources[idx] = plr_resource
|
||||||
else:
|
else:
|
||||||
resource_tree = await self.get_resource([unilabos_uuid])
|
uuid_indices.append((idx, unilabos_uuid, resource_data))
|
||||||
plr_resource = resource_tree.to_plr_resources()[0]
|
|
||||||
if "sample_id" in resource_data:
|
# 第二遍:批量查询有uuid的资源
|
||||||
plr_resource.unilabos_extra["sample_uuid"] = resource_data["sample_id"]
|
if uuid_indices:
|
||||||
queried_resources.append(plr_resource)
|
uuids = [item[1] for item in uuid_indices]
|
||||||
|
resource_tree = await self.get_resource(uuids)
|
||||||
|
plr_resources = resource_tree.to_plr_resources()
|
||||||
|
for i, (idx, _, resource_data) in enumerate(uuid_indices):
|
||||||
|
plr_resource = plr_resources[i]
|
||||||
|
if "sample_id" in resource_data:
|
||||||
|
plr_resource.unilabos_extra[EXTRA_SAMPLE_UUID] = resource_data["sample_id"]
|
||||||
|
queried_resources[idx] = plr_resource
|
||||||
|
|
||||||
self.lab_logger().debug(f"资源查询结果: 共 {len(queried_resources)} 个资源")
|
self.lab_logger().debug(f"资源查询结果: 共 {len(queried_resources)} 个资源")
|
||||||
|
|
||||||
# 通过资源跟踪器获取本地实例
|
# 通过资源跟踪器获取本地实例
|
||||||
final_resources = queried_resources if is_sequence else queried_resources[0]
|
final_resources = queried_resources if is_sequence else queried_resources[0]
|
||||||
if not is_sequence:
|
if not is_sequence:
|
||||||
plr = self.resource_tracker.figure_resource({"name": final_resources.name}, try_mode=False)
|
plr = self.resource_tracker.figure_resource(
|
||||||
|
{"name": final_resources.name}, try_mode=False
|
||||||
|
)
|
||||||
# 保留unilabos_extra
|
# 保留unilabos_extra
|
||||||
if hasattr(final_resources, "unilabos_extra") and hasattr(plr, "unilabos_extra"):
|
if hasattr(final_resources, "unilabos_extra") and hasattr(plr, "unilabos_extra"):
|
||||||
plr.unilabos_extra = getattr(final_resources, "unilabos_extra", {}).copy()
|
plr.unilabos_extra = getattr(final_resources, "unilabos_extra", {}).copy()
|
||||||
@@ -1377,8 +1466,12 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
execution_success = True
|
execution_success = True
|
||||||
except Exception as _:
|
except Exception as _:
|
||||||
execution_error = traceback.format_exc()
|
execution_error = traceback.format_exc()
|
||||||
error(f"异步任务 {ACTION.__name__} 报错了\n{traceback.format_exc()}\n原始输入:{str(action_kwargs)[:1000]}")
|
error(
|
||||||
trace(f"异步任务 {ACTION.__name__} 报错了\n{traceback.format_exc()}\n原始输入:{action_kwargs}")
|
f"异步任务 {ACTION.__name__} 报错了\n{traceback.format_exc()}\n原始输入:{str(action_kwargs)[:1000]}"
|
||||||
|
)
|
||||||
|
trace(
|
||||||
|
f"异步任务 {ACTION.__name__} 报错了\n{traceback.format_exc()}\n原始输入:{action_kwargs}"
|
||||||
|
)
|
||||||
|
|
||||||
future = ROS2DeviceNode.run_async_func(ACTION, trace_error=False, **action_kwargs)
|
future = ROS2DeviceNode.run_async_func(ACTION, trace_error=False, **action_kwargs)
|
||||||
future.add_done_callback(_handle_future_exception)
|
future.add_done_callback(_handle_future_exception)
|
||||||
@@ -1398,9 +1491,11 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
except Exception as _:
|
except Exception as _:
|
||||||
execution_error = traceback.format_exc()
|
execution_error = traceback.format_exc()
|
||||||
error(
|
error(
|
||||||
f"同步任务 {ACTION.__name__} 报错了\n{traceback.format_exc()}\n原始输入:{str(action_kwargs)[:1000]}")
|
f"同步任务 {ACTION.__name__} 报错了\n{traceback.format_exc()}\n原始输入:{str(action_kwargs)[:1000]}"
|
||||||
|
)
|
||||||
trace(
|
trace(
|
||||||
f"同步任务 {ACTION.__name__} 报错了\n{traceback.format_exc()}\n原始输入:{action_kwargs}")
|
f"同步任务 {ACTION.__name__} 报错了\n{traceback.format_exc()}\n原始输入:{action_kwargs}"
|
||||||
|
)
|
||||||
|
|
||||||
future.add_done_callback(_handle_future_exception)
|
future.add_done_callback(_handle_future_exception)
|
||||||
|
|
||||||
@@ -1467,11 +1562,18 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
if isinstance(rs, list):
|
if isinstance(rs, list):
|
||||||
for r in rs:
|
for r in rs:
|
||||||
res = self.resource_tracker.parent_resource(r) # 获取 resource 对象
|
res = self.resource_tracker.parent_resource(r) # 获取 resource 对象
|
||||||
|
if res is None:
|
||||||
|
res = rs
|
||||||
|
if id(res) not in seen:
|
||||||
|
seen.add(id(res))
|
||||||
|
unique_resources.append(res)
|
||||||
else:
|
else:
|
||||||
res = self.resource_tracker.parent_resource(rs)
|
res = self.resource_tracker.parent_resource(rs)
|
||||||
if id(res) not in seen:
|
if res is None:
|
||||||
seen.add(id(res))
|
res = rs
|
||||||
unique_resources.append(res)
|
if id(res) not in seen:
|
||||||
|
seen.add(id(res))
|
||||||
|
unique_resources.append(res)
|
||||||
|
|
||||||
# 使用新的资源树接口
|
# 使用新的资源树接口
|
||||||
if unique_resources:
|
if unique_resources:
|
||||||
@@ -1523,20 +1625,37 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
try:
|
try:
|
||||||
function_name = target["function_name"]
|
function_name = target["function_name"]
|
||||||
function_args = target["function_args"]
|
function_args = target["function_args"]
|
||||||
|
# 获取 unilabos 系统参数
|
||||||
|
unilabos_param: Dict[str, Any] = target[JSON_UNILABOS_PARAM]
|
||||||
|
|
||||||
assert isinstance(function_args, dict), "执行动作时JSON必须为dict类型\n原JSON: {string}"
|
assert isinstance(function_args, dict), "执行动作时JSON必须为dict类型\n原JSON: {string}"
|
||||||
function = getattr(self.driver_instance, function_name)
|
function = getattr(self.driver_instance, function_name)
|
||||||
assert callable(
|
assert callable(
|
||||||
function
|
function
|
||||||
), f"执行动作时JSON中的function_name对应的函数不可调用: {function_name}\n原JSON: {string}"
|
), f"执行动作时JSON中的function_name对应的函数不可调用: {function_name}\n原JSON: {string}"
|
||||||
|
|
||||||
# 处理 ResourceSlot 类型参数
|
# 处理参数(包含 unilabos 系统参数如 sample_uuids)
|
||||||
args_list = default_manager._analyze_method_signature(function)["args"]
|
args_list = default_manager._analyze_method_signature(function, skip_unilabos_params=False)["args"]
|
||||||
for arg in args_list:
|
for arg in args_list:
|
||||||
arg_name = arg["name"]
|
arg_name = arg["name"]
|
||||||
arg_type = arg["type"]
|
arg_type = arg["type"]
|
||||||
|
|
||||||
# 跳过不在 function_args 中的参数
|
# 跳过不在 function_args 中的参数
|
||||||
if arg_name not in function_args:
|
if arg_name not in function_args:
|
||||||
|
# 处理 sample_uuids 参数注入
|
||||||
|
if arg_name == PARAM_SAMPLE_UUIDS:
|
||||||
|
raw_sample_uuids = unilabos_param.get(PARAM_SAMPLE_UUIDS, {})
|
||||||
|
# 将 material uuid 转换为 resource 实例
|
||||||
|
# key: sample_uuid, value: material_uuid -> resource 实例
|
||||||
|
resolved_sample_uuids: Dict[str, Any] = {}
|
||||||
|
for sample_uuid, material_uuid in raw_sample_uuids.items():
|
||||||
|
if material_uuid and self.resource_tracker:
|
||||||
|
resource = self.resource_tracker.uuid_to_resources.get(material_uuid)
|
||||||
|
resolved_sample_uuids[sample_uuid] = resource if resource else material_uuid
|
||||||
|
else:
|
||||||
|
resolved_sample_uuids[sample_uuid] = material_uuid
|
||||||
|
function_args[PARAM_SAMPLE_UUIDS] = resolved_sample_uuids
|
||||||
|
self.lab_logger().debug(f"[JsonCommand] 注入 {PARAM_SAMPLE_UUIDS}: {resolved_sample_uuids}")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# 处理单个 ResourceSlot
|
# 处理单个 ResourceSlot
|
||||||
@@ -1566,6 +1685,7 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
)
|
)
|
||||||
raise JsonCommandInitError(f"ResourceSlot列表参数转换失败: {arg_name}")
|
raise JsonCommandInitError(f"ResourceSlot列表参数转换失败: {arg_name}")
|
||||||
|
|
||||||
|
# todo: 默认反报送
|
||||||
return function(**function_args)
|
return function(**function_args)
|
||||||
except KeyError as ex:
|
except KeyError as ex:
|
||||||
raise JsonCommandInitError(
|
raise JsonCommandInitError(
|
||||||
@@ -1585,21 +1705,23 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
raise ValueError("至少需要提供一个 UUID")
|
raise ValueError("至少需要提供一个 UUID")
|
||||||
|
|
||||||
uuids_list = list(uuids)
|
uuids_list = list(uuids)
|
||||||
future = self._resource_clients["c2s_update_resource_tree"].call_async(SerialCommand.Request(
|
future = self._resource_clients["c2s_update_resource_tree"].call_async(
|
||||||
command=json.dumps(
|
SerialCommand.Request(
|
||||||
{
|
command=json.dumps(
|
||||||
"data": {"data": uuids_list, "with_children": True},
|
{
|
||||||
"action": "get",
|
"data": {"data": uuids_list, "with_children": True},
|
||||||
}
|
"action": "get",
|
||||||
|
}
|
||||||
|
)
|
||||||
)
|
)
|
||||||
))
|
)
|
||||||
|
|
||||||
# 等待结果(使用while循环,每次sleep 0.05秒,最多等待30秒)
|
# 等待结果(使用while循环,每次sleep 0.05秒,最多等待30秒)
|
||||||
timeout = 30.0
|
timeout = 30.0
|
||||||
elapsed = 0.0
|
elapsed = 0.0
|
||||||
while not future.done() and elapsed < timeout:
|
while not future.done() and elapsed < timeout:
|
||||||
time.sleep(0.05)
|
time.sleep(0.02)
|
||||||
elapsed += 0.05
|
elapsed += 0.02
|
||||||
|
|
||||||
if not future.done():
|
if not future.done():
|
||||||
raise Exception(f"资源查询超时: {uuids_list}")
|
raise Exception(f"资源查询超时: {uuids_list}")
|
||||||
@@ -1650,6 +1772,9 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
try:
|
try:
|
||||||
function_name = target["function_name"]
|
function_name = target["function_name"]
|
||||||
function_args = target["function_args"]
|
function_args = target["function_args"]
|
||||||
|
# 获取 unilabos 系统参数
|
||||||
|
unilabos_param: Dict[str, Any] = target.get(JSON_UNILABOS_PARAM, {})
|
||||||
|
|
||||||
assert isinstance(function_args, dict), "执行动作时JSON必须为dict类型\n原JSON: {string}"
|
assert isinstance(function_args, dict), "执行动作时JSON必须为dict类型\n原JSON: {string}"
|
||||||
function = getattr(self.driver_instance, function_name)
|
function = getattr(self.driver_instance, function_name)
|
||||||
assert callable(
|
assert callable(
|
||||||
@@ -1659,14 +1784,30 @@ class BaseROS2DeviceNode(Node, Generic[T]):
|
|||||||
function
|
function
|
||||||
), f"执行动作时JSON中的function并非异步: {function_name}\n原JSON: {string}"
|
), f"执行动作时JSON中的function并非异步: {function_name}\n原JSON: {string}"
|
||||||
|
|
||||||
# 处理 ResourceSlot 类型参数
|
# 处理参数(包含 unilabos 系统参数如 sample_uuids)
|
||||||
args_list = default_manager._analyze_method_signature(function)["args"]
|
args_list = default_manager._analyze_method_signature(function, skip_unilabos_params=False)["args"]
|
||||||
for arg in args_list:
|
for arg in args_list:
|
||||||
arg_name = arg["name"]
|
arg_name = arg["name"]
|
||||||
arg_type = arg["type"]
|
arg_type = arg["type"]
|
||||||
|
|
||||||
# 跳过不在 function_args 中的参数
|
# 跳过不在 function_args 中的参数
|
||||||
if arg_name not in function_args:
|
if arg_name not in function_args:
|
||||||
|
# 处理 sample_uuids 参数注入
|
||||||
|
if arg_name == PARAM_SAMPLE_UUIDS:
|
||||||
|
raw_sample_uuids = unilabos_param.get(PARAM_SAMPLE_UUIDS, {})
|
||||||
|
# 将 material uuid 转换为 resource 实例
|
||||||
|
# key: sample_uuid, value: material_uuid -> resource 实例
|
||||||
|
resolved_sample_uuids: Dict[str, Any] = {}
|
||||||
|
for sample_uuid, material_uuid in raw_sample_uuids.items():
|
||||||
|
if material_uuid and self.resource_tracker:
|
||||||
|
resource = self.resource_tracker.uuid_to_resources.get(material_uuid)
|
||||||
|
resolved_sample_uuids[sample_uuid] = resource if resource else material_uuid
|
||||||
|
else:
|
||||||
|
resolved_sample_uuids[sample_uuid] = material_uuid
|
||||||
|
function_args[PARAM_SAMPLE_UUIDS] = resolved_sample_uuids
|
||||||
|
self.lab_logger().debug(
|
||||||
|
f"[JsonCommandAsync] 注入 {PARAM_SAMPLE_UUIDS}: {resolved_sample_uuids}"
|
||||||
|
)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# 处理单个 ResourceSlot
|
# 处理单个 ResourceSlot
|
||||||
@@ -1891,6 +2032,7 @@ class ROS2DeviceNode:
|
|||||||
|
|
||||||
if driver_is_ros:
|
if driver_is_ros:
|
||||||
driver_params["device_id"] = device_id
|
driver_params["device_id"] = device_id
|
||||||
|
driver_params["registry_name"] = device_config.res_content.klass
|
||||||
driver_params["resource_tracker"] = self.resource_tracker
|
driver_params["resource_tracker"] = self.resource_tracker
|
||||||
self._driver_instance = self._driver_creator.create_instance(driver_params)
|
self._driver_instance = self._driver_creator.create_instance(driver_params)
|
||||||
if self._driver_instance is None:
|
if self._driver_instance is None:
|
||||||
@@ -1908,6 +2050,7 @@ class ROS2DeviceNode:
|
|||||||
children=children,
|
children=children,
|
||||||
driver_instance=self._driver_instance, # type: ignore
|
driver_instance=self._driver_instance, # type: ignore
|
||||||
device_id=device_id,
|
device_id=device_id,
|
||||||
|
registry_name=device_config.res_content.klass,
|
||||||
device_uuid=device_uuid,
|
device_uuid=device_uuid,
|
||||||
status_types=status_types,
|
status_types=status_types,
|
||||||
action_value_mappings=action_value_mappings,
|
action_value_mappings=action_value_mappings,
|
||||||
@@ -1919,6 +2062,7 @@ class ROS2DeviceNode:
|
|||||||
self._ros_node = BaseROS2DeviceNode(
|
self._ros_node = BaseROS2DeviceNode(
|
||||||
driver_instance=self._driver_instance,
|
driver_instance=self._driver_instance,
|
||||||
device_id=device_id,
|
device_id=device_id,
|
||||||
|
registry_name=device_config.res_content.klass,
|
||||||
device_uuid=device_uuid,
|
device_uuid=device_uuid,
|
||||||
status_types=status_types,
|
status_types=status_types,
|
||||||
action_value_mappings=action_value_mappings,
|
action_value_mappings=action_value_mappings,
|
||||||
@@ -1927,6 +2071,7 @@ class ROS2DeviceNode:
|
|||||||
resource_tracker=self.resource_tracker,
|
resource_tracker=self.resource_tracker,
|
||||||
)
|
)
|
||||||
self._ros_node: BaseROS2DeviceNode
|
self._ros_node: BaseROS2DeviceNode
|
||||||
|
# 将注册表类型名传递给BaseROS2DeviceNode,用于slave上报
|
||||||
self._ros_node.lab_logger().info(f"初始化完成 {self._ros_node.uuid} {self.driver_is_ros}")
|
self._ros_node.lab_logger().info(f"初始化完成 {self._ros_node.uuid} {self.driver_is_ros}")
|
||||||
self.driver_instance._ros_node = self._ros_node # type: ignore
|
self.driver_instance._ros_node = self._ros_node # type: ignore
|
||||||
self.driver_instance._execute_driver_command = self._ros_node._execute_driver_command # type: ignore
|
self.driver_instance._execute_driver_command = self._ros_node._execute_driver_command # type: ignore
|
||||||
@@ -1944,7 +2089,9 @@ class ROS2DeviceNode:
|
|||||||
asyncio.set_event_loop(loop)
|
asyncio.set_event_loop(loop)
|
||||||
loop.run_forever()
|
loop.run_forever()
|
||||||
|
|
||||||
ROS2DeviceNode._asyncio_loop_thread = threading.Thread(target=run_event_loop, daemon=True, name="ROS2DeviceNode")
|
ROS2DeviceNode._asyncio_loop_thread = threading.Thread(
|
||||||
|
target=run_event_loop, daemon=True, name="ROS2DeviceNode"
|
||||||
|
)
|
||||||
ROS2DeviceNode._asyncio_loop_thread.start()
|
ROS2DeviceNode._asyncio_loop_thread.start()
|
||||||
logger.info(f"循环线程已启动")
|
logger.info(f"循环线程已启动")
|
||||||
|
|
||||||
|
|||||||
@@ -6,12 +6,13 @@ from cv_bridge import CvBridge
|
|||||||
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode, DeviceNodeResourceTracker
|
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode, DeviceNodeResourceTracker
|
||||||
|
|
||||||
class VideoPublisher(BaseROS2DeviceNode):
|
class VideoPublisher(BaseROS2DeviceNode):
|
||||||
def __init__(self, device_id='video_publisher', device_uuid='', camera_index=0, period: float = 0.1, resource_tracker: DeviceNodeResourceTracker = None):
|
def __init__(self, device_id='video_publisher', registry_name="", device_uuid='', camera_index=0, period: float = 0.1, resource_tracker: DeviceNodeResourceTracker = None):
|
||||||
# 初始化BaseROS2DeviceNode,使用自身作为driver_instance
|
# 初始化BaseROS2DeviceNode,使用自身作为driver_instance
|
||||||
BaseROS2DeviceNode.__init__(
|
BaseROS2DeviceNode.__init__(
|
||||||
self,
|
self,
|
||||||
driver_instance=self,
|
driver_instance=self,
|
||||||
device_id=device_id,
|
device_id=device_id,
|
||||||
|
registry_name=registry_name,
|
||||||
device_uuid=device_uuid,
|
device_uuid=device_uuid,
|
||||||
status_types={},
|
status_types={},
|
||||||
action_value_mappings={},
|
action_value_mappings={},
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ class ControllerNode(BaseROS2DeviceNode):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
device_id: str,
|
device_id: str,
|
||||||
|
registry_name: str,
|
||||||
controller_func: Callable,
|
controller_func: Callable,
|
||||||
update_rate: float,
|
update_rate: float,
|
||||||
inputs: Dict[str, Dict[str, type | str]],
|
inputs: Dict[str, Dict[str, type | str]],
|
||||||
@@ -51,6 +52,7 @@ class ControllerNode(BaseROS2DeviceNode):
|
|||||||
self,
|
self,
|
||||||
driver_instance=self,
|
driver_instance=self,
|
||||||
device_id=device_id,
|
device_id=device_id,
|
||||||
|
registry_name=registry_name,
|
||||||
status_types=status_types,
|
status_types=status_types,
|
||||||
action_value_mappings=action_value_mappings,
|
action_value_mappings=action_value_mappings,
|
||||||
hardware_interface=hardware_interface,
|
hardware_interface=hardware_interface,
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
import collections
|
import collections
|
||||||
from dataclasses import dataclass, field
|
|
||||||
import json
|
import json
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
import uuid
|
import uuid
|
||||||
|
from dataclasses import dataclass, field
|
||||||
from typing import TYPE_CHECKING, Optional, Dict, Any, List, ClassVar, Set, Union
|
from typing import TYPE_CHECKING, Optional, Dict, Any, List, ClassVar, Set, Union
|
||||||
from typing_extensions import TypedDict
|
|
||||||
|
|
||||||
from action_msgs.msg import GoalStatus
|
from action_msgs.msg import GoalStatus
|
||||||
from geometry_msgs.msg import Point
|
from geometry_msgs.msg import Point
|
||||||
from rclpy.action import ActionClient, get_action_server_names_and_types_by_node
|
from rclpy.action import ActionClient, get_action_server_names_and_types_by_node
|
||||||
from rclpy.service import Service
|
from rclpy.service import Service
|
||||||
|
from typing_extensions import TypedDict
|
||||||
from unilabos_msgs.msg import Resource # type: ignore
|
from unilabos_msgs.msg import Resource # type: ignore
|
||||||
from unilabos_msgs.srv import (
|
from unilabos_msgs.srv import (
|
||||||
ResourceAdd,
|
ResourceAdd,
|
||||||
@@ -23,10 +23,20 @@ from unilabos_msgs.srv import (
|
|||||||
from unilabos_msgs.srv._serial_command import SerialCommand_Request, SerialCommand_Response
|
from unilabos_msgs.srv._serial_command import SerialCommand_Request, SerialCommand_Response
|
||||||
from unique_identifier_msgs.msg import UUID
|
from unique_identifier_msgs.msg import UUID
|
||||||
|
|
||||||
|
from unilabos.registry.placeholder_type import ResourceSlot, DeviceSlot
|
||||||
from unilabos.registry.registry import lab_registry
|
from unilabos.registry.registry import lab_registry
|
||||||
from unilabos.resources.container import RegularContainer
|
from unilabos.resources.container import RegularContainer
|
||||||
from unilabos.resources.graphio import initialize_resource
|
from unilabos.resources.graphio import initialize_resource
|
||||||
from unilabos.resources.registry import add_schema
|
from unilabos.resources.registry import add_schema
|
||||||
|
from unilabos.resources.resource_tracker import (
|
||||||
|
ResourceDict,
|
||||||
|
ResourceDictInstance,
|
||||||
|
ResourceTreeSet,
|
||||||
|
ResourceTreeInstance,
|
||||||
|
RETURN_UNILABOS_SAMPLES,
|
||||||
|
JSON_UNILABOS_PARAM,
|
||||||
|
PARAM_SAMPLE_UUIDS, SampleUUIDsType, LabSample,
|
||||||
|
)
|
||||||
from unilabos.ros.initialize_device import initialize_device_from_dict
|
from unilabos.ros.initialize_device import initialize_device_from_dict
|
||||||
from unilabos.ros.msgs.message_converter import (
|
from unilabos.ros.msgs.message_converter import (
|
||||||
get_msg_type,
|
get_msg_type,
|
||||||
@@ -37,17 +47,11 @@ from unilabos.ros.msgs.message_converter import (
|
|||||||
)
|
)
|
||||||
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode, ROS2DeviceNode, DeviceNodeResourceTracker
|
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode, ROS2DeviceNode, DeviceNodeResourceTracker
|
||||||
from unilabos.ros.nodes.presets.controller_node import ControllerNode
|
from unilabos.ros.nodes.presets.controller_node import ControllerNode
|
||||||
from unilabos.resources.resource_tracker import (
|
|
||||||
ResourceDict,
|
|
||||||
ResourceDictInstance,
|
|
||||||
ResourceTreeSet,
|
|
||||||
ResourceTreeInstance,
|
|
||||||
)
|
|
||||||
from unilabos.utils import logger
|
from unilabos.utils import logger
|
||||||
from unilabos.utils.exception import DeviceClassInvalid
|
from unilabos.utils.exception import DeviceClassInvalid
|
||||||
from unilabos.utils.log import warning
|
from unilabos.utils.log import warning
|
||||||
from unilabos.utils.type_check import serialize_result_info
|
from unilabos.utils.type_check import serialize_result_info
|
||||||
from unilabos.registry.placeholder_type import ResourceSlot, DeviceSlot
|
from unilabos.config.config import BasicConfig
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from unilabos.app.ws_client import QueueItem
|
from unilabos.app.ws_client import QueueItem
|
||||||
@@ -60,7 +64,8 @@ class DeviceActionStatus:
|
|||||||
|
|
||||||
class TestResourceReturn(TypedDict):
|
class TestResourceReturn(TypedDict):
|
||||||
resources: List[List[ResourceDict]]
|
resources: List[List[ResourceDict]]
|
||||||
devices: List[DeviceSlot]
|
devices: List[Dict[str, Any]]
|
||||||
|
unilabos_samples: List[LabSample]
|
||||||
|
|
||||||
|
|
||||||
class TestLatencyReturn(TypedDict):
|
class TestLatencyReturn(TypedDict):
|
||||||
@@ -245,6 +250,7 @@ class HostNode(BaseROS2DeviceNode):
|
|||||||
self,
|
self,
|
||||||
driver_instance=self,
|
driver_instance=self,
|
||||||
device_id=device_id,
|
device_id=device_id,
|
||||||
|
registry_name="host_node",
|
||||||
device_uuid=host_node_dict["uuid"],
|
device_uuid=host_node_dict["uuid"],
|
||||||
status_types={},
|
status_types={},
|
||||||
action_value_mappings=lab_registry.device_type_registry["host_node"]["class"]["action_value_mappings"],
|
action_value_mappings=lab_registry.device_type_registry["host_node"]["class"]["action_value_mappings"],
|
||||||
@@ -299,7 +305,8 @@ class HostNode(BaseROS2DeviceNode):
|
|||||||
} # 用来存储多个ActionClient实例
|
} # 用来存储多个ActionClient实例
|
||||||
self._action_value_mappings: Dict[str, Dict] = (
|
self._action_value_mappings: Dict[str, Dict] = (
|
||||||
{}
|
{}
|
||||||
) # 用来存储多个ActionClient的type, goal, feedback, result的变量名映射关系
|
) # device_id -> action_value_mappings(本地+远程设备统一存储)
|
||||||
|
self._slave_registry_configs: Dict[str, Dict] = {} # registry_name -> registry_config(含action_value_mappings)
|
||||||
self._goals: Dict[str, Any] = {} # 用来存储多个目标的状态
|
self._goals: Dict[str, Any] = {} # 用来存储多个目标的状态
|
||||||
self._online_devices: Set[str] = {f"{self.namespace}/{device_id}"} # 用于跟踪在线设备
|
self._online_devices: Set[str] = {f"{self.namespace}/{device_id}"} # 用于跟踪在线设备
|
||||||
self._last_discovery_time = 0.0 # 上次设备发现的时间
|
self._last_discovery_time = 0.0 # 上次设备发现的时间
|
||||||
@@ -633,6 +640,8 @@ class HostNode(BaseROS2DeviceNode):
|
|||||||
self.device_machine_names[device_id] = "本地"
|
self.device_machine_names[device_id] = "本地"
|
||||||
self.devices_instances[device_id] = d
|
self.devices_instances[device_id] = d
|
||||||
# noinspection PyProtectedMember
|
# noinspection PyProtectedMember
|
||||||
|
self._action_value_mappings[device_id] = d._ros_node._action_value_mappings
|
||||||
|
# noinspection PyProtectedMember
|
||||||
for action_name, action_value_mapping in d._ros_node._action_value_mappings.items():
|
for action_name, action_value_mapping in d._ros_node._action_value_mappings.items():
|
||||||
if action_name.startswith("auto-") or str(action_value_mapping.get("type", "")).startswith(
|
if action_name.startswith("auto-") or str(action_value_mapping.get("type", "")).startswith(
|
||||||
"UniLabJsonCommand"
|
"UniLabJsonCommand"
|
||||||
@@ -755,6 +764,7 @@ class HostNode(BaseROS2DeviceNode):
|
|||||||
item: "QueueItem",
|
item: "QueueItem",
|
||||||
action_type: str,
|
action_type: str,
|
||||||
action_kwargs: Dict[str, Any],
|
action_kwargs: Dict[str, Any],
|
||||||
|
sample_material: Dict[str, str],
|
||||||
server_info: Optional[Dict[str, Any]] = None,
|
server_info: Optional[Dict[str, Any]] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
@@ -768,18 +778,29 @@ class HostNode(BaseROS2DeviceNode):
|
|||||||
u = uuid.UUID(item.job_id)
|
u = uuid.UUID(item.job_id)
|
||||||
device_id = item.device_id
|
device_id = item.device_id
|
||||||
action_name = item.action_name
|
action_name = item.action_name
|
||||||
|
|
||||||
|
if BasicConfig.test_mode:
|
||||||
|
action_id = f"/devices/{device_id}/{action_name}"
|
||||||
|
self.lab_logger().info(
|
||||||
|
f"[TEST MODE] 模拟执行: {action_id} (job={item.job_id[:8]}), 参数: {str(action_kwargs)[:500]}"
|
||||||
|
)
|
||||||
|
# 根据注册表 handles 构建模拟返回值
|
||||||
|
mock_return = self._build_test_mode_return(device_id, action_name, action_kwargs)
|
||||||
|
self._handle_test_mode_result(item, action_id, mock_return)
|
||||||
|
return
|
||||||
|
|
||||||
if action_type.startswith("UniLabJsonCommand"):
|
if action_type.startswith("UniLabJsonCommand"):
|
||||||
if action_name.startswith("auto-"):
|
if action_name.startswith("auto-"):
|
||||||
action_name = action_name[5:]
|
action_name = action_name[5:]
|
||||||
action_id = f"/devices/{device_id}/_execute_driver_command"
|
action_id = f"/devices/{device_id}/_execute_driver_command"
|
||||||
action_kwargs = {
|
json_command: Dict[str, Any] = {
|
||||||
"string": json.dumps(
|
"function_name": action_name,
|
||||||
{
|
"function_args": action_kwargs,
|
||||||
"function_name": action_name,
|
JSON_UNILABOS_PARAM: {
|
||||||
"function_args": action_kwargs,
|
PARAM_SAMPLE_UUIDS: sample_material,
|
||||||
}
|
},
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
action_kwargs = {"string": json.dumps(json_command)}
|
||||||
if action_type.startswith("UniLabJsonCommandAsync"):
|
if action_type.startswith("UniLabJsonCommandAsync"):
|
||||||
action_id = f"/devices/{device_id}/_execute_driver_command_async"
|
action_id = f"/devices/{device_id}/_execute_driver_command_async"
|
||||||
else:
|
else:
|
||||||
@@ -790,24 +811,10 @@ class HostNode(BaseROS2DeviceNode):
|
|||||||
raise ValueError(f"ActionClient {action_id} not found.")
|
raise ValueError(f"ActionClient {action_id} not found.")
|
||||||
|
|
||||||
action_client: ActionClient = self._action_clients[action_id]
|
action_client: ActionClient = self._action_clients[action_id]
|
||||||
|
|
||||||
# 遍历action_kwargs下的所有子dict,将"sample_uuid"的值赋给"sample_id"
|
|
||||||
def assign_sample_id(obj):
|
|
||||||
if isinstance(obj, dict):
|
|
||||||
if "sample_uuid" in obj:
|
|
||||||
obj["sample_id"] = obj["sample_uuid"]
|
|
||||||
obj.pop("sample_uuid")
|
|
||||||
for k, v in obj.items():
|
|
||||||
if k != "unilabos_extra":
|
|
||||||
assign_sample_id(v)
|
|
||||||
elif isinstance(obj, list):
|
|
||||||
for item in obj:
|
|
||||||
assign_sample_id(item)
|
|
||||||
|
|
||||||
assign_sample_id(action_kwargs)
|
|
||||||
goal_msg = convert_to_ros_msg(action_client._action_type.Goal(), action_kwargs)
|
goal_msg = convert_to_ros_msg(action_client._action_type.Goal(), action_kwargs)
|
||||||
|
|
||||||
self.lab_logger().info(f"[Host Node] Sending goal for {action_id}: {str(goal_msg)[:1000]}")
|
# self.lab_logger().trace(f"[Host Node] Sending goal for {action_id}: {str(goal_msg)[:1000]}")
|
||||||
|
self.lab_logger().trace(f"[Host Node] Sending goal for {action_id}: {action_kwargs}")
|
||||||
self.lab_logger().trace(f"[Host Node] Sending goal for {action_id}: {goal_msg}")
|
self.lab_logger().trace(f"[Host Node] Sending goal for {action_id}: {goal_msg}")
|
||||||
action_client.wait_for_server()
|
action_client.wait_for_server()
|
||||||
goal_uuid_obj = UUID(uuid=list(u.bytes))
|
goal_uuid_obj = UUID(uuid=list(u.bytes))
|
||||||
@@ -819,6 +826,51 @@ class HostNode(BaseROS2DeviceNode):
|
|||||||
)
|
)
|
||||||
future.add_done_callback(lambda f: self.goal_response_callback(item, action_id, f))
|
future.add_done_callback(lambda f: self.goal_response_callback(item, action_id, f))
|
||||||
|
|
||||||
|
def _build_test_mode_return(
|
||||||
|
self, device_id: str, action_name: str, action_kwargs: Dict[str, Any]
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
根据注册表 handles 的 output 定义构建测试模式的模拟返回值
|
||||||
|
|
||||||
|
根据 data_key 中 @flatten 的层数决定嵌套数组层数,叶子值为空字典。
|
||||||
|
例如: "vessel" → {}, "plate.@flatten" → [{}], "a.@flatten.@flatten" → [[{}]]
|
||||||
|
"""
|
||||||
|
mock_return: Dict[str, Any] = {"test_mode": True, "action_name": action_name}
|
||||||
|
action_mappings = self._action_value_mappings.get(device_id, {})
|
||||||
|
action_mapping = action_mappings.get(action_name, {})
|
||||||
|
handles = action_mapping.get("handles", {})
|
||||||
|
if isinstance(handles, dict):
|
||||||
|
for output_handle in handles.get("output", []):
|
||||||
|
data_key = output_handle.get("data_key", "")
|
||||||
|
handler_key = output_handle.get("handler_key", "")
|
||||||
|
# 根据 @flatten 层数构建嵌套数组,叶子为空字典
|
||||||
|
flatten_count = data_key.count("@flatten")
|
||||||
|
value: Any = {}
|
||||||
|
for _ in range(flatten_count):
|
||||||
|
value = [value]
|
||||||
|
mock_return[handler_key] = value
|
||||||
|
return mock_return
|
||||||
|
|
||||||
|
def _handle_test_mode_result(
|
||||||
|
self, item: "QueueItem", action_id: str, mock_return: Dict[str, Any]
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
测试模式下直接构建结果并走正常的结果回调流程(跳过 ROS)
|
||||||
|
"""
|
||||||
|
job_id = item.job_id
|
||||||
|
status = "success"
|
||||||
|
return_info = serialize_result_info("", True, mock_return)
|
||||||
|
|
||||||
|
self.lab_logger().info(f"[TEST MODE] Result for {action_id} ({job_id[:8]}): {status}")
|
||||||
|
|
||||||
|
from unilabos.app.web.controller import store_job_result
|
||||||
|
store_job_result(job_id, status, return_info, mock_return)
|
||||||
|
|
||||||
|
# 发布状态到桥接器
|
||||||
|
for bridge in self.bridges:
|
||||||
|
if hasattr(bridge, "publish_job_status"):
|
||||||
|
bridge.publish_job_status(mock_return, item, status, return_info)
|
||||||
|
|
||||||
def goal_response_callback(self, item: "QueueItem", action_id: str, future) -> None:
|
def goal_response_callback(self, item: "QueueItem", action_id: str, future) -> None:
|
||||||
"""目标响应回调"""
|
"""目标响应回调"""
|
||||||
goal_handle = future.result()
|
goal_handle = future.result()
|
||||||
@@ -866,14 +918,14 @@ class HostNode(BaseROS2DeviceNode):
|
|||||||
# 适配后端的一些额外处理
|
# 适配后端的一些额外处理
|
||||||
return_value = return_info.get("return_value")
|
return_value = return_info.get("return_value")
|
||||||
if isinstance(return_value, dict):
|
if isinstance(return_value, dict):
|
||||||
unilabos_samples = return_value.pop("unilabos_samples", None)
|
unilabos_samples = return_value.pop(RETURN_UNILABOS_SAMPLES, None)
|
||||||
if isinstance(unilabos_samples, list) and unilabos_samples:
|
if isinstance(unilabos_samples, list) and unilabos_samples:
|
||||||
self.lab_logger().info(
|
self.lab_logger().info(
|
||||||
f"[Host Node] Job {job_id[:8]} returned {len(unilabos_samples)} sample(s): "
|
f"[Host Node] Job {job_id[:8]} returned {len(unilabos_samples)} sample(s): "
|
||||||
f"{[s.get('name', s.get('id', 'unknown')) if isinstance(s, dict) else str(s)[:20] for s in unilabos_samples[:5]]}"
|
f"{[s.get('name', s.get('id', 'unknown')) if isinstance(s, dict) else str(s)[:20] for s in unilabos_samples[:5]]}"
|
||||||
f"{'...' if len(unilabos_samples) > 5 else ''}"
|
f"{'...' if len(unilabos_samples) > 5 else ''}"
|
||||||
)
|
)
|
||||||
return_info["unilabos_samples"] = unilabos_samples
|
return_info["samples"] = unilabos_samples
|
||||||
suc = return_info.get("suc", False)
|
suc = return_info.get("suc", False)
|
||||||
if not suc:
|
if not suc:
|
||||||
status = "failed"
|
status = "failed"
|
||||||
@@ -1143,7 +1195,7 @@ class HostNode(BaseROS2DeviceNode):
|
|||||||
self.lab_logger().info(f"[Host Node-Resource] UUID映射: {len(uuid_mapping)} 个节点")
|
self.lab_logger().info(f"[Host Node-Resource] UUID映射: {len(uuid_mapping)} 个节点")
|
||||||
# 还需要加入到资源图中,暂不实现,考虑资源图新的获取方式
|
# 还需要加入到资源图中,暂不实现,考虑资源图新的获取方式
|
||||||
response.response = json.dumps(uuid_mapping)
|
response.response = json.dumps(uuid_mapping)
|
||||||
self.lab_logger().info(f"[Host Node-Resource] Resource tree add completed, success: {success}")
|
self.lab_logger().info(f"[Host Node-Resource] Resource tree update completed, success: {success}")
|
||||||
|
|
||||||
async def _resource_tree_update_callback(self, request: SerialCommand_Request, response: SerialCommand_Response):
|
async def _resource_tree_update_callback(self, request: SerialCommand_Request, response: SerialCommand_Response):
|
||||||
"""
|
"""
|
||||||
@@ -1178,8 +1230,12 @@ class HostNode(BaseROS2DeviceNode):
|
|||||||
def _node_info_update_callback(self, request, response):
|
def _node_info_update_callback(self, request, response):
|
||||||
"""
|
"""
|
||||||
更新节点信息回调
|
更新节点信息回调
|
||||||
|
|
||||||
|
处理两种消息:
|
||||||
|
1. 首次上报(main_slave_run): 带 devices_config + registry_config,存储 action_value_mappings
|
||||||
|
2. 设备重注册(SYNC_SLAVE_NODE_INFO): 带 edge_device_id + registry_name,用 registry_name 索引已存储的 mappings
|
||||||
"""
|
"""
|
||||||
# self.lab_logger().info(f"[Host Node] Node info update request received: {request}")
|
self.lab_logger().trace(f"[Host Node] Node info update request received: {request}")
|
||||||
try:
|
try:
|
||||||
from unilabos.app.communication import get_communication_client
|
from unilabos.app.communication import get_communication_client
|
||||||
from unilabos.app.web.client import HTTPClient, http_client
|
from unilabos.app.web.client import HTTPClient, http_client
|
||||||
@@ -1189,12 +1245,48 @@ class HostNode(BaseROS2DeviceNode):
|
|||||||
info = info["SYNC_SLAVE_NODE_INFO"]
|
info = info["SYNC_SLAVE_NODE_INFO"]
|
||||||
machine_name = info["machine_name"]
|
machine_name = info["machine_name"]
|
||||||
edge_device_id = info["edge_device_id"]
|
edge_device_id = info["edge_device_id"]
|
||||||
|
registry_name = info.get("registry_name", "")
|
||||||
self.device_machine_names[edge_device_id] = machine_name
|
self.device_machine_names[edge_device_id] = machine_name
|
||||||
|
|
||||||
|
# 用 registry_name 索引已存储的 registry_config,获取 action_value_mappings
|
||||||
|
if registry_name and registry_name in self._slave_registry_configs:
|
||||||
|
action_mappings = self._slave_registry_configs[registry_name].get(
|
||||||
|
"class", {}
|
||||||
|
).get("action_value_mappings", {})
|
||||||
|
if action_mappings:
|
||||||
|
self._action_value_mappings[edge_device_id] = action_mappings
|
||||||
|
self.lab_logger().info(
|
||||||
|
f"[Host Node] Loaded {len(action_mappings)} action mappings "
|
||||||
|
f"for remote device {edge_device_id} (registry: {registry_name})"
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
devices_config = info.pop("devices_config")
|
devices_config = info.pop("devices_config")
|
||||||
registry_config = info.pop("registry_config")
|
registry_config = info.pop("registry_config")
|
||||||
if registry_config:
|
if registry_config:
|
||||||
http_client.resource_registry({"resources": registry_config})
|
http_client.resource_registry({"resources": registry_config})
|
||||||
|
|
||||||
|
# 存储 slave 的 registry_config,用于后续 SYNC_SLAVE_NODE_INFO 索引
|
||||||
|
for reg_name, reg_data in registry_config.items():
|
||||||
|
if isinstance(reg_data, dict) and "class" in reg_data:
|
||||||
|
self._slave_registry_configs[reg_name] = reg_data
|
||||||
|
|
||||||
|
# 解析 devices_config,建立 device_id -> action_value_mappings 映射
|
||||||
|
if devices_config:
|
||||||
|
for device_tree in devices_config:
|
||||||
|
for device_dict in device_tree:
|
||||||
|
device_id = device_dict.get("id", "")
|
||||||
|
class_name = device_dict.get("class", "")
|
||||||
|
if device_id and class_name and class_name in self._slave_registry_configs:
|
||||||
|
action_mappings = self._slave_registry_configs[class_name].get(
|
||||||
|
"class", {}
|
||||||
|
).get("action_value_mappings", {})
|
||||||
|
if action_mappings:
|
||||||
|
self._action_value_mappings[device_id] = action_mappings
|
||||||
|
self.lab_logger().info(
|
||||||
|
f"[Host Node] Stored {len(action_mappings)} action mappings "
|
||||||
|
f"for remote device {device_id} (class: {class_name})"
|
||||||
|
)
|
||||||
|
|
||||||
self.lab_logger().debug(f"[Host Node] Node info update: {info}")
|
self.lab_logger().debug(f"[Host Node] Node info update: {info}")
|
||||||
response.response = "OK"
|
response.response = "OK"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -1491,6 +1583,7 @@ class HostNode(BaseROS2DeviceNode):
|
|||||||
|
|
||||||
def test_resource(
|
def test_resource(
|
||||||
self,
|
self,
|
||||||
|
sample_uuids: SampleUUIDsType,
|
||||||
resource: ResourceSlot = None,
|
resource: ResourceSlot = None,
|
||||||
resources: List[ResourceSlot] = None,
|
resources: List[ResourceSlot] = None,
|
||||||
device: DeviceSlot = None,
|
device: DeviceSlot = None,
|
||||||
@@ -1505,6 +1598,7 @@ class HostNode(BaseROS2DeviceNode):
|
|||||||
return {
|
return {
|
||||||
"resources": ResourceTreeSet.from_plr_resources([resource, *resources], known_newly_created=True).dump(),
|
"resources": ResourceTreeSet.from_plr_resources([resource, *resources], known_newly_created=True).dump(),
|
||||||
"devices": [device, *devices],
|
"devices": [device, *devices],
|
||||||
|
"unilabos_samples": [LabSample(sample_uuid=sample_uuid, oss_path="", extra={"material_uuid": content} if isinstance(content, str) else content.serialize()) for sample_uuid, content in sample_uuids.items()]
|
||||||
}
|
}
|
||||||
|
|
||||||
def handle_pong_response(self, pong_data: dict):
|
def handle_pong_response(self, pong_data: dict):
|
||||||
|
|||||||
@@ -7,10 +7,11 @@ from rclpy.callback_groups import ReentrantCallbackGroup
|
|||||||
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
|
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
|
||||||
|
|
||||||
class JointRepublisher(BaseROS2DeviceNode):
|
class JointRepublisher(BaseROS2DeviceNode):
|
||||||
def __init__(self,device_id,resource_tracker, **kwargs):
|
def __init__(self,device_id, registry_name, resource_tracker, **kwargs):
|
||||||
super().__init__(
|
super().__init__(
|
||||||
driver_instance=self,
|
driver_instance=self,
|
||||||
device_id=device_id,
|
device_id=device_id,
|
||||||
|
registry_name=registry_name,
|
||||||
status_types={},
|
status_types={},
|
||||||
action_value_mappings={},
|
action_value_mappings={},
|
||||||
hardware_interface={},
|
hardware_interface={},
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ from unilabos.resources.graphio import initialize_resources
|
|||||||
from unilabos.registry.registry import lab_registry
|
from unilabos.registry.registry import lab_registry
|
||||||
|
|
||||||
class ResourceMeshManager(BaseROS2DeviceNode):
|
class ResourceMeshManager(BaseROS2DeviceNode):
|
||||||
def __init__(self, resource_model: dict, resource_config: list,resource_tracker, device_id: str = "resource_mesh_manager", rate=50, **kwargs):
|
def __init__(self, resource_model: dict, resource_config: list,resource_tracker, device_id: str = "resource_mesh_manager", registry_name: str = "", rate=50, **kwargs):
|
||||||
"""初始化资源网格管理器节点
|
"""初始化资源网格管理器节点
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -37,6 +37,7 @@ class ResourceMeshManager(BaseROS2DeviceNode):
|
|||||||
super().__init__(
|
super().__init__(
|
||||||
driver_instance=self,
|
driver_instance=self,
|
||||||
device_id=device_id,
|
device_id=device_id,
|
||||||
|
registry_name=registry_name,
|
||||||
status_types={},
|
status_types={},
|
||||||
action_value_mappings={},
|
action_value_mappings={},
|
||||||
hardware_interface={},
|
hardware_interface={},
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode, DeviceNodeRe
|
|||||||
|
|
||||||
|
|
||||||
class ROS2SerialNode(BaseROS2DeviceNode):
|
class ROS2SerialNode(BaseROS2DeviceNode):
|
||||||
def __init__(self, device_id, port: str, baudrate: int = 9600, resource_tracker: DeviceNodeResourceTracker=None):
|
def __init__(self, device_id, registry_name, port: str, baudrate: int = 9600, resource_tracker: DeviceNodeResourceTracker=None):
|
||||||
# 保存属性,以便在调用父类初始化前使用
|
# 保存属性,以便在调用父类初始化前使用
|
||||||
self.port = port
|
self.port = port
|
||||||
self.baudrate = baudrate
|
self.baudrate = baudrate
|
||||||
@@ -28,6 +28,7 @@ class ROS2SerialNode(BaseROS2DeviceNode):
|
|||||||
BaseROS2DeviceNode.__init__(
|
BaseROS2DeviceNode.__init__(
|
||||||
self,
|
self,
|
||||||
driver_instance=self,
|
driver_instance=self,
|
||||||
|
registry_name=registry_name,
|
||||||
device_id=device_id,
|
device_id=device_id,
|
||||||
status_types={},
|
status_types={},
|
||||||
action_value_mappings={},
|
action_value_mappings={},
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ from typing import List, Dict, Any, Optional, TYPE_CHECKING
|
|||||||
|
|
||||||
import rclpy
|
import rclpy
|
||||||
from rosidl_runtime_py import message_to_ordereddict
|
from rosidl_runtime_py import message_to_ordereddict
|
||||||
from unilabos_msgs.msg import Resource
|
|
||||||
from unilabos_msgs.srv import ResourceUpdate
|
|
||||||
|
|
||||||
from unilabos.messages import * # type: ignore # protocol names
|
from unilabos.messages import * # type: ignore # protocol names
|
||||||
from rclpy.action import ActionServer, ActionClient
|
from rclpy.action import ActionServer, ActionClient
|
||||||
@@ -15,7 +13,6 @@ from rclpy.action.server import ServerGoalHandle
|
|||||||
from unilabos_msgs.srv._serial_command import SerialCommand_Request, SerialCommand_Response
|
from unilabos_msgs.srv._serial_command import SerialCommand_Request, SerialCommand_Response
|
||||||
|
|
||||||
from unilabos.compile import action_protocol_generators
|
from unilabos.compile import action_protocol_generators
|
||||||
from unilabos.resources.graphio import nested_dict_to_list
|
|
||||||
from unilabos.ros.initialize_device import initialize_device_from_dict
|
from unilabos.ros.initialize_device import initialize_device_from_dict
|
||||||
from unilabos.ros.msgs.message_converter import (
|
from unilabos.ros.msgs.message_converter import (
|
||||||
get_action_type,
|
get_action_type,
|
||||||
@@ -50,6 +47,7 @@ class ROS2WorkstationNode(BaseROS2DeviceNode):
|
|||||||
*,
|
*,
|
||||||
driver_instance: "WorkstationBase",
|
driver_instance: "WorkstationBase",
|
||||||
device_id: str,
|
device_id: str,
|
||||||
|
registry_name: str,
|
||||||
device_uuid: str,
|
device_uuid: str,
|
||||||
status_types: Dict[str, Any],
|
status_types: Dict[str, Any],
|
||||||
action_value_mappings: Dict[str, Any],
|
action_value_mappings: Dict[str, Any],
|
||||||
@@ -65,6 +63,7 @@ class ROS2WorkstationNode(BaseROS2DeviceNode):
|
|||||||
super().__init__(
|
super().__init__(
|
||||||
driver_instance=driver_instance,
|
driver_instance=driver_instance,
|
||||||
device_id=device_id,
|
device_id=device_id,
|
||||||
|
registry_name=registry_name,
|
||||||
device_uuid=device_uuid,
|
device_uuid=device_uuid,
|
||||||
status_types=status_types,
|
status_types=status_types,
|
||||||
action_value_mappings={**action_value_mappings, **self.protocol_action_mappings},
|
action_value_mappings={**action_value_mappings, **self.protocol_action_mappings},
|
||||||
@@ -231,15 +230,15 @@ class ROS2WorkstationNode(BaseROS2DeviceNode):
|
|||||||
try:
|
try:
|
||||||
# 统一处理单个或多个资源
|
# 统一处理单个或多个资源
|
||||||
resource_id = (
|
resource_id = (
|
||||||
protocol_kwargs[k]["id"] if v == "unilabos_msgs/Resource" else protocol_kwargs[k][0]["id"]
|
protocol_kwargs[k]["id"]
|
||||||
|
if v == "unilabos_msgs/Resource"
|
||||||
|
else protocol_kwargs[k][0]["id"]
|
||||||
)
|
)
|
||||||
resource_uuid = protocol_kwargs[k].get("uuid", None)
|
resource_uuid = protocol_kwargs[k].get("uuid", None)
|
||||||
r = SerialCommand_Request()
|
r = SerialCommand_Request()
|
||||||
r.command = json.dumps({"id": resource_id, "uuid": resource_uuid, "with_children": True})
|
r.command = json.dumps({"id": resource_id, "uuid": resource_uuid, "with_children": True})
|
||||||
# 发送请求并等待响应
|
# 发送请求并等待响应
|
||||||
response: SerialCommand_Response = await self._resource_clients[
|
response: SerialCommand_Response = await self._resource_clients["resource_get"].call_async(
|
||||||
"resource_get"
|
|
||||||
].call_async(
|
|
||||||
r
|
r
|
||||||
) # type: ignore
|
) # type: ignore
|
||||||
raw_data = json.loads(response.response)
|
raw_data = json.loads(response.response)
|
||||||
@@ -307,12 +306,54 @@ class ROS2WorkstationNode(BaseROS2DeviceNode):
|
|||||||
|
|
||||||
# 向Host更新物料当前状态
|
# 向Host更新物料当前状态
|
||||||
for k, v in goal.get_fields_and_field_types().items():
|
for k, v in goal.get_fields_and_field_types().items():
|
||||||
if v in ["unilabos_msgs/Resource", "sequence<unilabos_msgs/Resource>"]:
|
if v not in ["unilabos_msgs/Resource", "sequence<unilabos_msgs/Resource>"]:
|
||||||
r = ResourceUpdate.Request()
|
continue
|
||||||
r.resources = [
|
self.lab_logger().info(f"更新资源状态: {k}")
|
||||||
convert_to_ros_msg(Resource, rs) for rs in nested_dict_to_list(protocol_kwargs[k])
|
try:
|
||||||
]
|
# 去重:使用 seen 集合获取唯一的资源对象
|
||||||
response = await self._resource_clients["resource_update"].call_async(r)
|
seen = set()
|
||||||
|
unique_resources = []
|
||||||
|
|
||||||
|
# 获取资源数据,统一转换为列表
|
||||||
|
resource_data = protocol_kwargs[k]
|
||||||
|
is_sequence = v != "unilabos_msgs/Resource"
|
||||||
|
if not is_sequence:
|
||||||
|
resource_list = [resource_data] if isinstance(resource_data, dict) else resource_data
|
||||||
|
else:
|
||||||
|
# 处理序列类型,可能是嵌套列表
|
||||||
|
resource_list = []
|
||||||
|
if isinstance(resource_data, list):
|
||||||
|
for item in resource_data:
|
||||||
|
if isinstance(item, list):
|
||||||
|
resource_list.extend(item)
|
||||||
|
else:
|
||||||
|
resource_list.append(item)
|
||||||
|
else:
|
||||||
|
resource_list = [resource_data]
|
||||||
|
|
||||||
|
for res_data in resource_list:
|
||||||
|
if not isinstance(res_data, dict):
|
||||||
|
continue
|
||||||
|
res_name = res_data.get("id") or res_data.get("name")
|
||||||
|
if not res_name:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 使用 resource_tracker 获取本地 PLR 实例
|
||||||
|
plr = self.resource_tracker.figure_resource({"name": res_name}, try_mode=False)
|
||||||
|
# 获取父资源
|
||||||
|
res = self.resource_tracker.parent_resource(plr)
|
||||||
|
if res is None:
|
||||||
|
res = plr
|
||||||
|
if id(res) not in seen:
|
||||||
|
seen.add(id(res))
|
||||||
|
unique_resources.append(res)
|
||||||
|
|
||||||
|
# 使用新的资源树接口更新
|
||||||
|
if unique_resources:
|
||||||
|
await self.update_resource(unique_resources)
|
||||||
|
except Exception as e:
|
||||||
|
self.lab_logger().error(f"资源更新失败: {e}")
|
||||||
|
self.lab_logger().error(traceback.format_exc())
|
||||||
|
|
||||||
# 设置成功状态和返回值
|
# 设置成功状态和返回值
|
||||||
execution_success = True
|
execution_success = True
|
||||||
|
|||||||
@@ -52,7 +52,8 @@ class DeviceClassCreator(Generic[T]):
|
|||||||
if self.device_instance is not None:
|
if self.device_instance is not None:
|
||||||
for c in self.children:
|
for c in self.children:
|
||||||
if c.res_content.type != "device":
|
if c.res_content.type != "device":
|
||||||
self.resource_tracker.add_resource(c.get_plr_nested_dict())
|
res = ResourceTreeSet([ResourceTreeInstance(c)]).to_plr_resources()[0]
|
||||||
|
self.resource_tracker.add_resource(res)
|
||||||
|
|
||||||
def create_instance(self, data: Dict[str, Any]) -> T:
|
def create_instance(self, data: Dict[str, Any]) -> T:
|
||||||
"""
|
"""
|
||||||
@@ -119,7 +120,7 @@ class PyLabRobotCreator(DeviceClassCreator[T]):
|
|||||||
# return resource, source_type
|
# return resource, source_type
|
||||||
|
|
||||||
def _process_resource_references(
|
def _process_resource_references(
|
||||||
self, data: Any, to_dict=False, states=None, prefix_path="", name_to_uuid=None
|
self, data: Any, processed_child_names: Optional[Dict[str, Any]], to_dict=False, states=None, prefix_path="", name_to_uuid=None
|
||||||
) -> Any:
|
) -> Any:
|
||||||
"""
|
"""
|
||||||
递归处理资源引用,替换_resource_child_name对应的资源
|
递归处理资源引用,替换_resource_child_name对应的资源
|
||||||
@@ -164,6 +165,7 @@ class PyLabRobotCreator(DeviceClassCreator[T]):
|
|||||||
states[prefix_path] = resource_instance.serialize_all_state()
|
states[prefix_path] = resource_instance.serialize_all_state()
|
||||||
return serialized
|
return serialized
|
||||||
else:
|
else:
|
||||||
|
processed_child_names[child_name] = resource_instance
|
||||||
self.resource_tracker.add_resource(resource_instance)
|
self.resource_tracker.add_resource(resource_instance)
|
||||||
# 立即设置UUID,state已经在resource_ulab_to_plr中处理过了
|
# 立即设置UUID,state已经在resource_ulab_to_plr中处理过了
|
||||||
if name_to_uuid:
|
if name_to_uuid:
|
||||||
@@ -182,12 +184,12 @@ class PyLabRobotCreator(DeviceClassCreator[T]):
|
|||||||
result = {}
|
result = {}
|
||||||
for key, value in data.items():
|
for key, value in data.items():
|
||||||
new_prefix = f"{prefix_path}.{key}" if prefix_path else key
|
new_prefix = f"{prefix_path}.{key}" if prefix_path else key
|
||||||
result[key] = self._process_resource_references(value, to_dict, states, new_prefix, name_to_uuid)
|
result[key] = self._process_resource_references(value, processed_child_names, to_dict, states, new_prefix, name_to_uuid)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
elif isinstance(data, list):
|
elif isinstance(data, list):
|
||||||
return [
|
return [
|
||||||
self._process_resource_references(item, to_dict, states, f"{prefix_path}[{i}]", name_to_uuid)
|
self._process_resource_references(item, processed_child_names, to_dict, states, f"{prefix_path}[{i}]", name_to_uuid)
|
||||||
for i, item in enumerate(data)
|
for i, item in enumerate(data)
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -234,7 +236,7 @@ class PyLabRobotCreator(DeviceClassCreator[T]):
|
|||||||
# 首先处理资源引用
|
# 首先处理资源引用
|
||||||
states = {}
|
states = {}
|
||||||
processed_data = self._process_resource_references(
|
processed_data = self._process_resource_references(
|
||||||
data, to_dict=True, states=states, name_to_uuid=name_to_uuid
|
data, {}, to_dict=True, states=states, name_to_uuid=name_to_uuid
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -270,7 +272,12 @@ class PyLabRobotCreator(DeviceClassCreator[T]):
|
|||||||
arg_value = spec_args[param_name].annotation
|
arg_value = spec_args[param_name].annotation
|
||||||
data[param_name]["_resource_type"] = self.device_cls.__module__ + ":" + arg_value
|
data[param_name]["_resource_type"] = self.device_cls.__module__ + ":" + arg_value
|
||||||
logger.debug(f"自动补充 _resource_type: {data[param_name]['_resource_type']}")
|
logger.debug(f"自动补充 _resource_type: {data[param_name]['_resource_type']}")
|
||||||
processed_data = self._process_resource_references(data, to_dict=False, name_to_uuid=name_to_uuid)
|
processed_child_names = {}
|
||||||
|
processed_data = self._process_resource_references(data, processed_child_names, to_dict=False, name_to_uuid=name_to_uuid)
|
||||||
|
for child_name, resource_instance in processed_data.items():
|
||||||
|
for ind, name in enumerate([child.res_content.name for child in self.children]):
|
||||||
|
if name == child_name:
|
||||||
|
self.children.pop(ind)
|
||||||
self.device_instance = super(PyLabRobotCreator, self).create_instance(processed_data) # 补全变量后直接调用,调用的自身的attach_resource
|
self.device_instance = super(PyLabRobotCreator, self).create_instance(processed_data) # 补全变量后直接调用,调用的自身的attach_resource
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"PyLabRobot创建实例失败: {e}")
|
logger.error(f"PyLabRobot创建实例失败: {e}")
|
||||||
@@ -342,9 +349,10 @@ class WorkstationNodeCreator(DeviceClassCreator[T]):
|
|||||||
try:
|
try:
|
||||||
# 创建实例,额外补充一个给protocol node的字段,后面考虑取消
|
# 创建实例,额外补充一个给protocol node的字段,后面考虑取消
|
||||||
data["children"] = self.children
|
data["children"] = self.children
|
||||||
for child in self.children:
|
# super(WorkstationNodeCreator, self).create_instance(data)的时候会attach
|
||||||
if child.res_content.type != "device":
|
# for child in self.children:
|
||||||
self.resource_tracker.add_resource(child.get_plr_nested_dict())
|
# if child.res_content.type != "device":
|
||||||
|
# self.resource_tracker.add_resource(child.get_plr_nested_dict())
|
||||||
deck_dict = data.get("deck")
|
deck_dict = data.get("deck")
|
||||||
if deck_dict:
|
if deck_dict:
|
||||||
from pylabrobot.resources import Deck, Resource
|
from pylabrobot.resources import Deck, Resource
|
||||||
|
|||||||
@@ -339,13 +339,8 @@
|
|||||||
"z": 0
|
"z": 0
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
"max_volume": 500.0,
|
|
||||||
"type": "RegularContainer",
|
"type": "RegularContainer",
|
||||||
"category": "container",
|
"category": "container"
|
||||||
"max_temp": 200.0,
|
|
||||||
"min_temp": -20.0,
|
|
||||||
"has_stirrer": true,
|
|
||||||
"has_heater": true
|
|
||||||
},
|
},
|
||||||
"data": {
|
"data": {
|
||||||
"liquids": [],
|
"liquids": [],
|
||||||
@@ -769,9 +764,7 @@
|
|||||||
"size_y": 250,
|
"size_y": 250,
|
||||||
"size_z": 0,
|
"size_z": 0,
|
||||||
"type": "RegularContainer",
|
"type": "RegularContainer",
|
||||||
"category": "container",
|
"category": "container"
|
||||||
"reagent": "sodium_chloride",
|
|
||||||
"physical_state": "solid"
|
|
||||||
},
|
},
|
||||||
"data": {
|
"data": {
|
||||||
"current_mass": 500.0,
|
"current_mass": 500.0,
|
||||||
@@ -792,14 +785,11 @@
|
|||||||
"z": 0
|
"z": 0
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
"volume": 500.0,
|
|
||||||
"size_x": 600,
|
"size_x": 600,
|
||||||
"size_y": 250,
|
"size_y": 250,
|
||||||
"size_z": 0,
|
"size_z": 0,
|
||||||
"type": "RegularContainer",
|
"type": "RegularContainer",
|
||||||
"category": "container",
|
"category": "container"
|
||||||
"reagent": "sodium_carbonate",
|
|
||||||
"physical_state": "solid"
|
|
||||||
},
|
},
|
||||||
"data": {
|
"data": {
|
||||||
"current_mass": 500.0,
|
"current_mass": 500.0,
|
||||||
@@ -820,14 +810,11 @@
|
|||||||
"z": 0
|
"z": 0
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
"volume": 500.0,
|
|
||||||
"size_x": 650,
|
"size_x": 650,
|
||||||
"size_y": 250,
|
"size_y": 250,
|
||||||
"size_z": 0,
|
"size_z": 0,
|
||||||
"type": "RegularContainer",
|
"type": "RegularContainer",
|
||||||
"category": "container",
|
"category": "container"
|
||||||
"reagent": "magnesium_chloride",
|
|
||||||
"physical_state": "solid"
|
|
||||||
},
|
},
|
||||||
"data": {
|
"data": {
|
||||||
"current_mass": 500.0,
|
"current_mass": 500.0,
|
||||||
|
|||||||
446
unilabos/test/experiments/prcxi_9320_slim.json
Normal file
446
unilabos/test/experiments/prcxi_9320_slim.json
Normal file
@@ -0,0 +1,446 @@
|
|||||||
|
{
|
||||||
|
"nodes": [
|
||||||
|
{
|
||||||
|
"id": "PRCXI",
|
||||||
|
"name": "PRCXI",
|
||||||
|
"type": "device",
|
||||||
|
"class": "liquid_handler.prcxi",
|
||||||
|
"parent": "",
|
||||||
|
"pose": {
|
||||||
|
"size": {
|
||||||
|
"width": 562,
|
||||||
|
"height": 394,
|
||||||
|
"depth": 0
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"config": {
|
||||||
|
"axis": "Left",
|
||||||
|
"deck": {
|
||||||
|
"_resource_type": "unilabos.devices.liquid_handling.prcxi.prcxi:PRCXI9300Deck",
|
||||||
|
"_resource_child_name": "PRCXI_Deck"
|
||||||
|
},
|
||||||
|
"host": "10.20.30.184",
|
||||||
|
"port": 9999,
|
||||||
|
"debug": true,
|
||||||
|
"setup": true,
|
||||||
|
"is_9320": true,
|
||||||
|
"timeout": 10,
|
||||||
|
"matrix_id": "5de524d0-3f95-406c-86dd-f83626ebc7cb",
|
||||||
|
"simulator": true,
|
||||||
|
"channel_num": 2
|
||||||
|
},
|
||||||
|
"data": {
|
||||||
|
"reset_ok": true
|
||||||
|
},
|
||||||
|
"schema": {},
|
||||||
|
"description": "",
|
||||||
|
"model": null,
|
||||||
|
"position": {
|
||||||
|
"x": 0,
|
||||||
|
"y": 240,
|
||||||
|
"z": 0
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "PRCXI_Deck",
|
||||||
|
"name": "PRCXI_Deck",
|
||||||
|
"children": [],
|
||||||
|
"parent": "PRCXI",
|
||||||
|
"type": "deck",
|
||||||
|
"class": "",
|
||||||
|
"position": {
|
||||||
|
"x": 10,
|
||||||
|
"y": 10,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"config": {
|
||||||
|
"type": "PRCXI9300Deck",
|
||||||
|
"size_x": 542,
|
||||||
|
"size_y": 374,
|
||||||
|
"size_z": 0,
|
||||||
|
"rotation": {
|
||||||
|
"x": 0,
|
||||||
|
"y": 0,
|
||||||
|
"z": 0,
|
||||||
|
"type": "Rotation"
|
||||||
|
},
|
||||||
|
"category": "deck",
|
||||||
|
"barcode": null,
|
||||||
|
"preferred_pickup_location": null,
|
||||||
|
"sites": [
|
||||||
|
{
|
||||||
|
"label": "T1",
|
||||||
|
"visible": true,
|
||||||
|
"occupied_by": null,
|
||||||
|
"position": {
|
||||||
|
"x": 0,
|
||||||
|
"y": 0,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"size": {
|
||||||
|
"width": 128.0,
|
||||||
|
"height": 86,
|
||||||
|
"depth": 0
|
||||||
|
},
|
||||||
|
"content_type": [
|
||||||
|
"container",
|
||||||
|
"plate",
|
||||||
|
"tip_rack",
|
||||||
|
"plates",
|
||||||
|
"tip_racks",
|
||||||
|
"tube_rack",
|
||||||
|
"adaptor"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "T2",
|
||||||
|
"visible": true,
|
||||||
|
"occupied_by": null,
|
||||||
|
"position": {
|
||||||
|
"x": 138,
|
||||||
|
"y": 0,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"size": {
|
||||||
|
"width": 128.0,
|
||||||
|
"height": 86,
|
||||||
|
"depth": 0
|
||||||
|
},
|
||||||
|
"content_type": [
|
||||||
|
"plate",
|
||||||
|
"tip_rack",
|
||||||
|
"plates",
|
||||||
|
"tip_racks",
|
||||||
|
"tube_rack",
|
||||||
|
"adaptor"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "T3",
|
||||||
|
"visible": true,
|
||||||
|
"occupied_by": null,
|
||||||
|
"position": {
|
||||||
|
"x": 276,
|
||||||
|
"y": 0,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"size": {
|
||||||
|
"width": 128.0,
|
||||||
|
"height": 86,
|
||||||
|
"depth": 0
|
||||||
|
},
|
||||||
|
"content_type": [
|
||||||
|
"plate",
|
||||||
|
"tip_rack",
|
||||||
|
"plates",
|
||||||
|
"tip_racks",
|
||||||
|
"tube_rack",
|
||||||
|
"adaptor"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "T4",
|
||||||
|
"visible": true,
|
||||||
|
"occupied_by": null,
|
||||||
|
"position": {
|
||||||
|
"x": 414,
|
||||||
|
"y": 0,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"size": {
|
||||||
|
"width": 128.0,
|
||||||
|
"height": 86,
|
||||||
|
"depth": 0
|
||||||
|
},
|
||||||
|
"content_type": [
|
||||||
|
"plate",
|
||||||
|
"tip_rack",
|
||||||
|
"plates",
|
||||||
|
"tip_racks",
|
||||||
|
"tube_rack",
|
||||||
|
"adaptor"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "T5",
|
||||||
|
"visible": true,
|
||||||
|
"occupied_by": null,
|
||||||
|
"position": {
|
||||||
|
"x": 0,
|
||||||
|
"y": 96,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"size": {
|
||||||
|
"width": 128.0,
|
||||||
|
"height": 86,
|
||||||
|
"depth": 0
|
||||||
|
},
|
||||||
|
"content_type": [
|
||||||
|
"plate",
|
||||||
|
"tip_rack",
|
||||||
|
"plates",
|
||||||
|
"tip_racks",
|
||||||
|
"tube_rack",
|
||||||
|
"adaptor"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "T6",
|
||||||
|
"visible": true,
|
||||||
|
"occupied_by": null,
|
||||||
|
"position": {
|
||||||
|
"x": 138,
|
||||||
|
"y": 96,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"size": {
|
||||||
|
"width": 128.0,
|
||||||
|
"height": 86,
|
||||||
|
"depth": 0
|
||||||
|
},
|
||||||
|
"content_type": [
|
||||||
|
"plate",
|
||||||
|
"tip_rack",
|
||||||
|
"plates",
|
||||||
|
"tip_racks",
|
||||||
|
"tube_rack",
|
||||||
|
"adaptor"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "T7",
|
||||||
|
"visible": true,
|
||||||
|
"occupied_by": null,
|
||||||
|
"position": {
|
||||||
|
"x": 276,
|
||||||
|
"y": 96,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"size": {
|
||||||
|
"width": 128.0,
|
||||||
|
"height": 86,
|
||||||
|
"depth": 0
|
||||||
|
},
|
||||||
|
"content_type": [
|
||||||
|
"plate",
|
||||||
|
"tip_rack",
|
||||||
|
"plates",
|
||||||
|
"tip_racks",
|
||||||
|
"tube_rack",
|
||||||
|
"adaptor"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "T8",
|
||||||
|
"visible": true,
|
||||||
|
"occupied_by": null,
|
||||||
|
"position": {
|
||||||
|
"x": 414,
|
||||||
|
"y": 96,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"size": {
|
||||||
|
"width": 128.0,
|
||||||
|
"height": 86,
|
||||||
|
"depth": 0
|
||||||
|
},
|
||||||
|
"content_type": [
|
||||||
|
"plate",
|
||||||
|
"tip_rack",
|
||||||
|
"plates",
|
||||||
|
"tip_racks",
|
||||||
|
"tube_rack",
|
||||||
|
"adaptor"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "T9",
|
||||||
|
"visible": true,
|
||||||
|
"occupied_by": null,
|
||||||
|
"position": {
|
||||||
|
"x": 0,
|
||||||
|
"y": 192,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"size": {
|
||||||
|
"width": 128.0,
|
||||||
|
"height": 86,
|
||||||
|
"depth": 0
|
||||||
|
},
|
||||||
|
"content_type": [
|
||||||
|
"plate",
|
||||||
|
"tip_rack",
|
||||||
|
"plates",
|
||||||
|
"tip_racks",
|
||||||
|
"tube_rack",
|
||||||
|
"adaptor"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "T10",
|
||||||
|
"visible": true,
|
||||||
|
"occupied_by": null,
|
||||||
|
"position": {
|
||||||
|
"x": 138,
|
||||||
|
"y": 192,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"size": {
|
||||||
|
"width": 128.0,
|
||||||
|
"height": 86,
|
||||||
|
"depth": 0
|
||||||
|
},
|
||||||
|
"content_type": [
|
||||||
|
"plate",
|
||||||
|
"tip_rack",
|
||||||
|
"plates",
|
||||||
|
"tip_racks",
|
||||||
|
"tube_rack",
|
||||||
|
"adaptor"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "T11",
|
||||||
|
"visible": true,
|
||||||
|
"occupied_by": null,
|
||||||
|
"position": {
|
||||||
|
"x": 276,
|
||||||
|
"y": 192,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"size": {
|
||||||
|
"width": 128.0,
|
||||||
|
"height": 86,
|
||||||
|
"depth": 0
|
||||||
|
},
|
||||||
|
"content_type": [
|
||||||
|
"plate",
|
||||||
|
"tip_rack",
|
||||||
|
"plates",
|
||||||
|
"tip_racks",
|
||||||
|
"tube_rack",
|
||||||
|
"adaptor"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "T12",
|
||||||
|
"visible": true,
|
||||||
|
"occupied_by": null,
|
||||||
|
"position": {
|
||||||
|
"x": 414,
|
||||||
|
"y": 192,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"size": {
|
||||||
|
"width": 128.0,
|
||||||
|
"height": 86,
|
||||||
|
"depth": 0
|
||||||
|
},
|
||||||
|
"content_type": [
|
||||||
|
"plate",
|
||||||
|
"tip_rack",
|
||||||
|
"plates",
|
||||||
|
"tip_racks",
|
||||||
|
"tube_rack",
|
||||||
|
"adaptor"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "T13",
|
||||||
|
"visible": true,
|
||||||
|
"occupied_by": null,
|
||||||
|
"position": {
|
||||||
|
"x": 0,
|
||||||
|
"y": 288,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"size": {
|
||||||
|
"width": 128.0,
|
||||||
|
"height": 86,
|
||||||
|
"depth": 0
|
||||||
|
},
|
||||||
|
"content_type": [
|
||||||
|
"plate",
|
||||||
|
"tip_rack",
|
||||||
|
"plates",
|
||||||
|
"tip_racks",
|
||||||
|
"tube_rack",
|
||||||
|
"adaptor"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "T14",
|
||||||
|
"visible": true,
|
||||||
|
"occupied_by": null,
|
||||||
|
"position": {
|
||||||
|
"x": 138,
|
||||||
|
"y": 288,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"size": {
|
||||||
|
"width": 128.0,
|
||||||
|
"height": 86,
|
||||||
|
"depth": 0
|
||||||
|
},
|
||||||
|
"content_type": [
|
||||||
|
"plate",
|
||||||
|
"tip_rack",
|
||||||
|
"plates",
|
||||||
|
"tip_racks",
|
||||||
|
"tube_rack",
|
||||||
|
"adaptor"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "T15",
|
||||||
|
"visible": true,
|
||||||
|
"occupied_by": null,
|
||||||
|
"position": {
|
||||||
|
"x": 276,
|
||||||
|
"y": 288,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"size": {
|
||||||
|
"width": 128.0,
|
||||||
|
"height": 86,
|
||||||
|
"depth": 0
|
||||||
|
},
|
||||||
|
"content_type": [
|
||||||
|
"plate",
|
||||||
|
"tip_rack",
|
||||||
|
"plates",
|
||||||
|
"tip_racks",
|
||||||
|
"tube_rack",
|
||||||
|
"adaptor"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "T16",
|
||||||
|
"visible": true,
|
||||||
|
"occupied_by": null,
|
||||||
|
"position": {
|
||||||
|
"x": 414,
|
||||||
|
"y": 288,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"size": {
|
||||||
|
"width": 128.0,
|
||||||
|
"height": 86,
|
||||||
|
"depth": 0
|
||||||
|
},
|
||||||
|
"content_type": [
|
||||||
|
"plate",
|
||||||
|
"tip_rack",
|
||||||
|
"plates",
|
||||||
|
"tip_racks",
|
||||||
|
"tube_rack",
|
||||||
|
"adaptor"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"data": {}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"edges": []
|
||||||
|
}
|
||||||
29
unilabos/test/experiments/xkc_sensor_test.json
Normal file
29
unilabos/test/experiments/xkc_sensor_test.json
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"nodes": [
|
||||||
|
{
|
||||||
|
"id": "Liquid_Sensor_1",
|
||||||
|
"name": "XKC Sensor",
|
||||||
|
"children": [],
|
||||||
|
"parent": null,
|
||||||
|
"type": "device",
|
||||||
|
"class": "sensor.xkc_rs485",
|
||||||
|
"position": {
|
||||||
|
"x": 0,
|
||||||
|
"y": 0,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"config": {
|
||||||
|
"port": "/dev/tty.usbserial-3110",
|
||||||
|
"baudrate": 9600,
|
||||||
|
"device_id": 1,
|
||||||
|
"threshold": 300,
|
||||||
|
"timeout": 3.0
|
||||||
|
},
|
||||||
|
"data": {
|
||||||
|
"level": false,
|
||||||
|
"rssi": 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"links": []
|
||||||
|
}
|
||||||
28
unilabos/test/experiments/zdt_motor_test.json
Normal file
28
unilabos/test/experiments/zdt_motor_test.json
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"nodes": [
|
||||||
|
{
|
||||||
|
"id": "ZDT_Motor",
|
||||||
|
"name": "ZDT Motor",
|
||||||
|
"children": [],
|
||||||
|
"parent": null,
|
||||||
|
"type": "device",
|
||||||
|
"class": "motor.zdt_x42",
|
||||||
|
"position": {
|
||||||
|
"x": 0,
|
||||||
|
"y": 0,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"config": {
|
||||||
|
"port": "/dev/tty.usbserial-3110",
|
||||||
|
"baudrate": 115200,
|
||||||
|
"device_id": 1,
|
||||||
|
"debug": true
|
||||||
|
},
|
||||||
|
"data": {
|
||||||
|
"position": 0,
|
||||||
|
"status": "idle"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"links": []
|
||||||
|
}
|
||||||
@@ -1,187 +0,0 @@
|
|||||||
# UniLabOS 日志配置说明
|
|
||||||
|
|
||||||
> **文件位置**: `unilabos/utils/log.py`
|
|
||||||
> **最后更新**: 2026-01-11
|
|
||||||
> **维护者**: Uni-Lab-OS 开发团队
|
|
||||||
|
|
||||||
本文档说明 UniLabOS 日志系统中对第三方库和内部模块的日志级别配置,避免控制台被过多的 DEBUG 日志淹没。
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 📋 已屏蔽的日志
|
|
||||||
|
|
||||||
以下库/模块的日志已被设置为 **WARNING** 或 **INFO** 级别,不再显示 DEBUG 日志:
|
|
||||||
|
|
||||||
### 1. pymodbus(Modbus 通信库)
|
|
||||||
|
|
||||||
**配置位置**: `log.py` 第196-200行
|
|
||||||
|
|
||||||
```python
|
|
||||||
# pymodbus 库的日志太详细,设置为 WARNING
|
|
||||||
logging.getLogger('pymodbus').setLevel(logging.WARNING)
|
|
||||||
logging.getLogger('pymodbus.logging').setLevel(logging.WARNING)
|
|
||||||
logging.getLogger('pymodbus.logging.base').setLevel(logging.WARNING)
|
|
||||||
logging.getLogger('pymodbus.logging.decoders').setLevel(logging.WARNING)
|
|
||||||
```
|
|
||||||
|
|
||||||
**屏蔽原因**:
|
|
||||||
- pymodbus 在 DEBUG 级别会输出每一次 Modbus 通信的详细信息
|
|
||||||
- 包括 `Processing: 0x5 0x1e 0x0 0x0...` 等原始数据
|
|
||||||
- 包括 `decoded PDU function_code(3 sub -1) -> ReadHoldingRegistersResponse(...)` 等解码信息
|
|
||||||
- 这些信息对日常使用价值不大,但会快速刷屏
|
|
||||||
|
|
||||||
**典型被屏蔽的日志**:
|
|
||||||
```
|
|
||||||
[DEBUG] Processing: 0x5 0x1e 0x0 0x0 0x0 0x7 0x1 0x3 0x4 0x0 0x0 0x0 0x0 [handleFrame:72] [pymodbus.logging.base]
|
|
||||||
[DEBUG] decoded PDU function_code(3 sub -1) -> ReadHoldingRegistersResponse(...) [decode:79] [pymodbus.logging.decoders]
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### 2. websockets(WebSocket 库)
|
|
||||||
|
|
||||||
**配置位置**: `log.py` 第202-205行
|
|
||||||
|
|
||||||
```python
|
|
||||||
# websockets 库的日志输出较多,设置为 WARNING
|
|
||||||
logging.getLogger('websockets').setLevel(logging.WARNING)
|
|
||||||
logging.getLogger('websockets.client').setLevel(logging.WARNING)
|
|
||||||
logging.getLogger('websockets.server').setLevel(logging.WARNING)
|
|
||||||
```
|
|
||||||
|
|
||||||
**屏蔽原因**:
|
|
||||||
- WebSocket 连接、断开、心跳等信息在 DEBUG 级别会频繁输出
|
|
||||||
- 对于长时间运行的服务,这些日志意义不大
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### 3. ROS Host Node(设备状态更新)
|
|
||||||
|
|
||||||
**配置位置**: `log.py` 第207-208行
|
|
||||||
|
|
||||||
```python
|
|
||||||
# ROS 节点的状态更新日志过于频繁,设置为 INFO
|
|
||||||
logging.getLogger('unilabos.ros.nodes.presets.host_node').setLevel(logging.INFO)
|
|
||||||
```
|
|
||||||
|
|
||||||
**屏蔽原因**:
|
|
||||||
- 设备状态更新(如手套箱压力)每隔几秒就会更新一次
|
|
||||||
- DEBUG 日志会记录每一次状态变化,导致日志刷屏
|
|
||||||
- 这些频繁的状态更新对调试价值不大
|
|
||||||
|
|
||||||
**典型被屏蔽的日志**:
|
|
||||||
```
|
|
||||||
[DEBUG] [/devices/host_node] Status updated: BatteryStation.data_glove_box_pressure = 4.229457855224609 [property_callback:666] [unilabos.ros.nodes.presets.host_node]
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### 4. asyncio 和 urllib3
|
|
||||||
|
|
||||||
**配置位置**: `log.py` 第224-225行
|
|
||||||
|
|
||||||
```python
|
|
||||||
logging.getLogger("asyncio").setLevel(logging.INFO)
|
|
||||||
logging.getLogger("urllib3").setLevel(logging.INFO)
|
|
||||||
```
|
|
||||||
|
|
||||||
**屏蔽原因**:
|
|
||||||
- asyncio: 异步 IO 的内部调试信息
|
|
||||||
- urllib3: HTTP 请求库的连接池、重试等详细信息
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 🔧 如何临时启用这些日志(调试用)
|
|
||||||
|
|
||||||
### 方法1: 修改 log.py(永久启用)
|
|
||||||
|
|
||||||
在 `log.py` 的 `configure_logger()` 函数中,将对应库的日志级别改为 `logging.DEBUG`:
|
|
||||||
|
|
||||||
```python
|
|
||||||
# 临时启用 pymodbus 的 DEBUG 日志
|
|
||||||
logging.getLogger('pymodbus').setLevel(logging.DEBUG)
|
|
||||||
logging.getLogger('pymodbus.logging').setLevel(logging.DEBUG)
|
|
||||||
logging.getLogger('pymodbus.logging.base').setLevel(logging.DEBUG)
|
|
||||||
logging.getLogger('pymodbus.logging.decoders').setLevel(logging.DEBUG)
|
|
||||||
```
|
|
||||||
|
|
||||||
### 方法2: 在代码中临时启用(单次调试)
|
|
||||||
|
|
||||||
在需要调试的代码文件中添加:
|
|
||||||
|
|
||||||
```python
|
|
||||||
import logging
|
|
||||||
|
|
||||||
# 临时启用 pymodbus DEBUG 日志
|
|
||||||
logging.getLogger('pymodbus').setLevel(logging.DEBUG)
|
|
||||||
|
|
||||||
# 你的 Modbus 调试代码
|
|
||||||
...
|
|
||||||
|
|
||||||
# 调试完成后恢复
|
|
||||||
logging.getLogger('pymodbus').setLevel(logging.WARNING)
|
|
||||||
```
|
|
||||||
|
|
||||||
### 方法3: 使用环境变量或配置文件(推荐)
|
|
||||||
|
|
||||||
未来可以考虑在启动参数中添加 `--debug-modbus` 等选项来动态控制。
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 📊 日志级别说明
|
|
||||||
|
|
||||||
| 级别 | 数值 | 用途 | 是否显示 |
|
|
||||||
|------|------|------|---------|
|
|
||||||
| TRACE | 5 | 最详细的跟踪信息 | ✅ |
|
|
||||||
| DEBUG | 10 | 调试信息 | ✅ |
|
|
||||||
| INFO | 20 | 一般信息 | ✅ |
|
|
||||||
| WARNING | 30 | 警告信息 | ✅ |
|
|
||||||
| ERROR | 40 | 错误信息 | ✅ |
|
|
||||||
| CRITICAL | 50 | 严重错误 | ✅ |
|
|
||||||
|
|
||||||
**当前配置**:
|
|
||||||
- UniLabOS 自身代码: DEBUG 及以上全部显示
|
|
||||||
- pymodbus/websockets: **WARNING** 及以上显示(屏蔽 DEBUG/INFO)
|
|
||||||
- ROS host_node: **INFO** 及以上显示(屏蔽 DEBUG)
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## ⚠️ 重要提示
|
|
||||||
|
|
||||||
### 修改生效时间
|
|
||||||
- 修改 `log.py` 后需要 **重启 unilab 服务** 才能生效
|
|
||||||
- 不需要重新安装或重新编译
|
|
||||||
|
|
||||||
### 调试 Modbus 通信问题
|
|
||||||
如果需要调试 Modbus 通信故障,应该:
|
|
||||||
1. 临时启用 pymodbus DEBUG 日志(方法2)
|
|
||||||
2. 复现问题
|
|
||||||
3. 查看详细的通信日志
|
|
||||||
4. 调试完成后记得恢复 WARNING 级别
|
|
||||||
|
|
||||||
### 调试设备状态问题
|
|
||||||
如果需要调试设备状态更新问题:
|
|
||||||
```python
|
|
||||||
logging.getLogger('unilabos.ros.nodes.presets.host_node').setLevel(logging.DEBUG)
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 📝 维护记录
|
|
||||||
|
|
||||||
| 日期 | 修改内容 | 操作人 |
|
|
||||||
|------|---------|--------|
|
|
||||||
| 2026-01-11 | 初始创建,添加 pymodbus、websockets、ROS host_node 屏蔽 | - |
|
|
||||||
| 2026-01-07 | 添加 pymodbus 和 websockets 屏蔽(log-0107.py) | - |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 🔗 相关文件
|
|
||||||
|
|
||||||
- `log.py` - 日志配置主文件
|
|
||||||
- `unilabos/devices/workstation/coin_cell_assembly/` - 使用 Modbus 的扣电工作站代码
|
|
||||||
- `unilabos/ros/nodes/presets/host_node.py` - ROS 主机节点代码
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
**维护提示**: 如果添加了新的第三方库或发现新的日志刷屏问题,请在此文档中记录并更新 `log.py` 配置。
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user