10 Commits

Author SHA1 Message Date
Calvin Cao
37afe6dea1 Merge pull request #150 from WenzheG/dev_wzg/Xrd_Raman
添加Raman和xrd相关代码
2025-11-07 10:25:53 +08:00
WenzheG
c8cd51f45b Delete unilabos/devices/opsky_Raman/fork_pr-流程.md 2025-11-06 09:36:47 +08:00
WenzheG
c25ec7aa54 添加Raman和xrd相关代码 2025-11-05 19:58:51 +08:00
Xuwznln
39bb7dc627 adjust with_children param 2025-11-03 16:31:37 +08:00
Xuwznln
0fda155f55 modify devices to use correct executor (sleep, create_task) 2025-11-03 15:49:11 +08:00
Xuwznln
6e3eacd2f0 support sleep and create_task in node 2025-11-03 15:42:12 +08:00
Xuwznln
062f1a2153 fix run async execution error 2025-10-31 21:43:25 +08:00
Junhan Chang
61e8d67800 modify workstation_architecture docs 2025-10-30 17:29:47 +08:00
ZiWei
d0884cdbd8 bioyond_HR (#133)
* feat: Enhance Bioyond synchronization and resource management

- Implemented synchronization for all material types (consumables, samples, reagents) from Bioyond, logging detailed information for each type.
- Improved error handling and logging during synchronization processes.
- Added functionality to save Bioyond material IDs in UniLab resources for future updates.
- Enhanced the `sync_to_external` method to handle material movements correctly, including querying and creating materials in Bioyond.
- Updated warehouse configurations to support new storage types and improved layout for better resource management.
- Introduced new resource types such as reactors and tip boxes, with detailed specifications.
- Modified warehouse factory to support column offsets for naming conventions (e.g., A05-D08).
- Improved resource tracking by merging extra attributes instead of overwriting them.
- Added a new method for updating resources in Bioyond, ensuring better synchronization of resource changes.

* feat: 添加TipBox和Reactor的配置到bottles.yaml

* fix: 修复液体投料方法中的volume参数处理逻辑
2025-10-29 12:10:05 +08:00
ZiWei
545ea45024 修复solid_feeding_vials方法中的volume参数处理逻辑,优化solvents参数的使用条件 2025-10-29 11:24:37 +08:00
50 changed files with 4598 additions and 2049 deletions

View File

@@ -24,6 +24,7 @@ extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.napoleon", # 如果您使用 Google 或 NumPy 风格的 docstrings
"sphinx_rtd_theme",
"sphinxcontrib.mermaid"
]
source_suffix = {
@@ -42,6 +43,8 @@ myst_enable_extensions = [
"substitution",
]
myst_fence_as_directive = ["mermaid"]
templates_path = ["_templates"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
@@ -203,3 +206,5 @@ def generate_action_includes(app):
def setup(app):
app.connect("builder-inited", generate_action_includes)
app.add_js_file("https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js")
app.add_js_file(None, body="mermaid.initialize({startOnLoad:true});")

View File

@@ -1,88 +1,26 @@
## 简单单变量动作函数
### `SendCmd`
```{literalinclude} ../../unilabos_msgs/action/SendCmd.action
:language: yaml
```
---
### `StrSingleInput`
```{literalinclude} ../../unilabos_msgs/action/StrSingleInput.action
:language: yaml
```
---
### `IntSingleInput`
```{literalinclude} ../../unilabos_msgs/action/IntSingleInput.action
:language: yaml
```
---
### `FloatSingleInput`
```{literalinclude} ../../unilabos_msgs/action/FloatSingleInput.action
:language: yaml
```
---
### `Point3DSeparateInput`
```{literalinclude} ../../unilabos_msgs/action/Point3DSeparateInput.action
:language: yaml
```
---
### `Wait`
```{literalinclude} ../../unilabos_msgs/action/Wait.action
:language: yaml
```
---
----
## 常量有机化学操作
Uni-Lab 常量有机化学指令集多数来自 [XDL](https://croningroup.gitlab.io/chemputer/xdl/standard/full_steps_specification.html#),包含有机合成实验中常见的操作,如加热、搅拌、冷却等。
### `Clean`
```{literalinclude} ../../unilabos_msgs/action/Clean.action
:language: yaml
```
---
### `EvacuateAndRefill`
```{literalinclude} ../../unilabos_msgs/action/EvacuateAndRefill.action
:language: yaml
```
---
### `Evaporate`
```{literalinclude} ../../unilabos_msgs/action/Evaporate.action
:language: yaml
```
---
### `HeatChill`
```{literalinclude} ../../unilabos_msgs/action/HeatChill.action
:language: yaml
```
---
----
### `HeatChillStart`
@@ -90,7 +28,7 @@ Uni-Lab 常量有机化学指令集多数来自 [XDL](https://croningroup.gitlab
:language: yaml
```
---
----
### `HeatChillStop`
@@ -98,7 +36,7 @@ Uni-Lab 常量有机化学指令集多数来自 [XDL](https://croningroup.gitlab
:language: yaml
```
---
----
### `PumpTransfer`
@@ -106,195 +44,12 @@ Uni-Lab 常量有机化学指令集多数来自 [XDL](https://croningroup.gitlab
:language: yaml
```
---
### `Separate`
```{literalinclude} ../../unilabos_msgs/action/Separate.action
:language: yaml
```
---
### `Stir`
```{literalinclude} ../../unilabos_msgs/action/Stir.action
:language: yaml
```
---
### `Add`
```{literalinclude} ../../unilabos_msgs/action/Add.action
:language: yaml
```
---
### `AddSolid`
```{literalinclude} ../../unilabos_msgs/action/AddSolid.action
:language: yaml
```
---
### `AdjustPH`
```{literalinclude} ../../unilabos_msgs/action/AdjustPH.action
:language: yaml
```
---
### `Centrifuge`
```{literalinclude} ../../unilabos_msgs/action/Centrifuge.action
:language: yaml
```
---
### `CleanVessel`
```{literalinclude} ../../unilabos_msgs/action/CleanVessel.action
:language: yaml
```
---
### `Crystallize`
```{literalinclude} ../../unilabos_msgs/action/Crystallize.action
:language: yaml
```
---
### `Dissolve`
```{literalinclude} ../../unilabos_msgs/action/Dissolve.action
:language: yaml
```
---
### `Dry`
```{literalinclude} ../../unilabos_msgs/action/Dry.action
:language: yaml
```
---
### `Filter`
```{literalinclude} ../../unilabos_msgs/action/Filter.action
:language: yaml
```
---
### `FilterThrough`
```{literalinclude} ../../unilabos_msgs/action/FilterThrough.action
:language: yaml
```
---
### `Hydrogenate`
```{literalinclude} ../../unilabos_msgs/action/Hydrogenate.action
:language: yaml
```
---
### `Purge`
```{literalinclude} ../../unilabos_msgs/action/Purge.action
:language: yaml
```
---
### `Recrystallize`
```{literalinclude} ../../unilabos_msgs/action/Recrystallize.action
:language: yaml
```
---
### `RunColumn`
```{literalinclude} ../../unilabos_msgs/action/RunColumn.action
:language: yaml
```
---
### `StartPurge`
```{literalinclude} ../../unilabos_msgs/action/StartPurge.action
:language: yaml
```
---
### `StartStir`
```{literalinclude} ../../unilabos_msgs/action/StartStir.action
:language: yaml
```
---
### `StopPurge`
```{literalinclude} ../../unilabos_msgs/action/StopPurge.action
:language: yaml
```
---
### `StopStir`
```{literalinclude} ../../unilabos_msgs/action/StopStir.action
:language: yaml
```
---
### `Transfer`
```{literalinclude} ../../unilabos_msgs/action/Transfer.action
:language: yaml
```
---
### `WashSolid`
```{literalinclude} ../../unilabos_msgs/action/WashSolid.action
:language: yaml
```
---
----
## 移液工作站及相关生物自动化设备操作
Uni-Lab 生物操作指令集多数来自 [PyLabRobot](https://docs.pylabrobot.org/user_guide/index.html),包含生物实验中常见的操作,如移液、混匀、离心等。
### `LiquidHandlerAspirate`
```{literalinclude} ../../unilabos_msgs/action/LiquidHandlerAspirate.action
:language: yaml
```
---
### `LiquidHandlerDiscardTips`
@@ -302,15 +57,7 @@ Uni-Lab 生物操作指令集多数来自 [PyLabRobot](https://docs.pylabrobot.o
:language: yaml
```
---
### `LiquidHandlerDispense`
```{literalinclude} ../../unilabos_msgs/action/LiquidHandlerDispense.action
:language: yaml
```
---
----
### `LiquidHandlerDropTips`
@@ -318,7 +65,7 @@ Uni-Lab 生物操作指令集多数来自 [PyLabRobot](https://docs.pylabrobot.o
:language: yaml
```
---
----
### `LiquidHandlerDropTips96`
@@ -326,7 +73,7 @@ Uni-Lab 生物操作指令集多数来自 [PyLabRobot](https://docs.pylabrobot.o
:language: yaml
```
---
----
### `LiquidHandlerMoveLid`
@@ -334,7 +81,7 @@ Uni-Lab 生物操作指令集多数来自 [PyLabRobot](https://docs.pylabrobot.o
:language: yaml
```
---
----
### `LiquidHandlerMovePlate`
@@ -342,7 +89,7 @@ Uni-Lab 生物操作指令集多数来自 [PyLabRobot](https://docs.pylabrobot.o
:language: yaml
```
---
----
### `LiquidHandlerMoveResource`
@@ -350,7 +97,7 @@ Uni-Lab 生物操作指令集多数来自 [PyLabRobot](https://docs.pylabrobot.o
:language: yaml
```
---
----
### `LiquidHandlerPickUpTips`
@@ -358,7 +105,7 @@ Uni-Lab 生物操作指令集多数来自 [PyLabRobot](https://docs.pylabrobot.o
:language: yaml
```
---
----
### `LiquidHandlerPickUpTips96`
@@ -366,7 +113,7 @@ Uni-Lab 生物操作指令集多数来自 [PyLabRobot](https://docs.pylabrobot.o
:language: yaml
```
---
----
### `LiquidHandlerReturnTips`
@@ -374,7 +121,7 @@ Uni-Lab 生物操作指令集多数来自 [PyLabRobot](https://docs.pylabrobot.o
:language: yaml
```
---
----
### `LiquidHandlerReturnTips96`
@@ -382,7 +129,7 @@ Uni-Lab 生物操作指令集多数来自 [PyLabRobot](https://docs.pylabrobot.o
:language: yaml
```
---
----
### `LiquidHandlerStamp`
@@ -390,129 +137,17 @@ Uni-Lab 生物操作指令集多数来自 [PyLabRobot](https://docs.pylabrobot.o
:language: yaml
```
---
### `LiquidHandlerTransfer`
```{literalinclude} ../../unilabos_msgs/action/LiquidHandlerTransfer.action
:language: yaml
```
---
### `LiquidHandlerAdd`
```{literalinclude} ../../unilabos_msgs/action/LiquidHandlerAdd.action
:language: yaml
```
---
### `LiquidHandlerIncubateBiomek`
```{literalinclude} ../../unilabos_msgs/action/LiquidHandlerIncubateBiomek.action
:language: yaml
```
---
### `LiquidHandlerMix`
```{literalinclude} ../../unilabos_msgs/action/LiquidHandlerMix.action
:language: yaml
```
---
### `LiquidHandlerMoveBiomek`
```{literalinclude} ../../unilabos_msgs/action/LiquidHandlerMoveBiomek.action
:language: yaml
```
---
### `LiquidHandlerMoveTo`
```{literalinclude} ../../unilabos_msgs/action/LiquidHandlerMoveTo.action
:language: yaml
```
---
### `LiquidHandlerOscillateBiomek`
```{literalinclude} ../../unilabos_msgs/action/LiquidHandlerOscillateBiomek.action
:language: yaml
```
---
### `LiquidHandlerProtocolCreation`
```{literalinclude} ../../unilabos_msgs/action/LiquidHandlerProtocolCreation.action
:language: yaml
```
---
### `LiquidHandlerRemove`
```{literalinclude} ../../unilabos_msgs/action/LiquidHandlerRemove.action
:language: yaml
```
---
### `LiquidHandlerSetGroup`
```{literalinclude} ../../unilabos_msgs/action/LiquidHandlerSetGroup.action
:language: yaml
```
---
### `LiquidHandlerSetLiquid`
```{literalinclude} ../../unilabos_msgs/action/LiquidHandlerSetLiquid.action
:language: yaml
```
---
### `LiquidHandlerSetTipRack`
```{literalinclude} ../../unilabos_msgs/action/LiquidHandlerSetTipRack.action
:language: yaml
```
---
### `LiquidHandlerTransferBiomek`
```{literalinclude} ../../unilabos_msgs/action/LiquidHandlerTransferBiomek.action
:language: yaml
```
---
### `LiquidHandlerTransferGroup`
```{literalinclude} ../../unilabos_msgs/action/LiquidHandlerTransferGroup.action
:language: yaml
```
---
----
## 多工作站及小车运行、物料转移
### `AGVTransfer`
```{literalinclude} ../../unilabos_msgs/action/AGVTransfer.action
:language: yaml
```
---
----
### `WorkStationRun`
@@ -520,64 +155,12 @@ Uni-Lab 生物操作指令集多数来自 [PyLabRobot](https://docs.pylabrobot.o
:language: yaml
```
---
### `ResetHandling`
```{literalinclude} ../../unilabos_msgs/action/ResetHandling.action
:language: yaml
```
---
### `ResourceCreateFromOuter`
```{literalinclude} ../../unilabos_msgs/action/ResourceCreateFromOuter.action
:language: yaml
```
---
### `ResourceCreateFromOuterEasy`
```{literalinclude} ../../unilabos_msgs/action/ResourceCreateFromOuterEasy.action
:language: yaml
```
---
### `SetPumpPosition`
```{literalinclude} ../../unilabos_msgs/action/SetPumpPosition.action
:language: yaml
```
---
## 固体分配与处理设备操作
### `SolidDispenseAddPowderTube`
```{literalinclude} ../../unilabos_msgs/action/SolidDispenseAddPowderTube.action
:language: yaml
```
---
## 其他设备操作
### `EmptyIn`
```{literalinclude} ../../unilabos_msgs/action/EmptyIn.action
:language: yaml
```
---
----
## 机械臂、夹爪等机器人设备
Uni-Lab 机械臂、机器人、夹爪和导航指令集沿用 ROS2 的 `control_msgs` 和 `nav2_msgs`
### `FollowJointTrajectory`
```yaml
@@ -645,8 +228,7 @@ trajectory_msgs/MultiDOFJointTrajectoryPoint multi_dof_error
```
---
----
### `GripperCommand`
```yaml
@@ -664,19 +246,42 @@ bool reached_goal # True iff the gripper position has reached the commanded setp
```
---
----
### `JointTrajectory`
```yaml
trajectory_msgs/JointTrajectory trajectory
---
---
```
---
----
### `ParallelGripperCommand`
```yaml
# Parallel grippers refer to an end effector where two opposing fingers grasp an object from opposite sides.
sensor_msgs/JointState command
# name: the name(s) of the joint this command is requesting
# position: desired position of each gripper joint (radians or meters)
# velocity: (optional, not used if empty) max velocity of the joint allowed while moving (radians or meters / second)
# effort: (optional, not used if empty) max effort of the joint allowed while moving (Newtons or Newton-meters)
---
sensor_msgs/JointState state # The current gripper state.
# position of each joint (radians or meters)
# optional: velocity of each joint (radians or meters / second)
# optional: effort of each joint (Newtons or Newton-meters)
bool stalled # True if the gripper is exerting max effort and not moving
bool reached_goal # True if the gripper position has reached the commanded setpoint
---
sensor_msgs/JointState state # The current gripper state.
# position of each joint (radians or meters)
# optional: velocity of each joint (radians or meters / second)
# optional: effort of each joint (Newtons or Newton-meters)
```
----
### `PointHead`
```yaml
@@ -686,13 +291,12 @@ string pointing_frame
builtin_interfaces/Duration min_duration
float64 max_velocity
---
---
float64 pointing_angle_error
```
---
----
### `SingleJointPosition`
```yaml
@@ -700,16 +304,15 @@ float64 position
builtin_interfaces/Duration min_duration
float64 max_velocity
---
---
std_msgs/Header header
float64 position
float64 velocity
float64 error
```
---
----
### `AssistedTeleop`
```yaml
@@ -721,10 +324,10 @@ builtin_interfaces/Duration total_elapsed_time
---
#feedback
builtin_interfaces/Duration current_teleop_duration
```
---
----
### `BackUp`
```yaml
@@ -738,10 +341,10 @@ builtin_interfaces/Duration total_elapsed_time
---
#feedback definition
float32 distance_traveled
```
---
----
### `ComputePathThroughPoses`
```yaml
@@ -756,10 +359,10 @@ nav_msgs/Path path
builtin_interfaces/Duration planning_time
---
#feedback definition
```
---
----
### `ComputePathToPose`
```yaml
@@ -774,10 +377,10 @@ nav_msgs/Path path
builtin_interfaces/Duration planning_time
---
#feedback definition
```
---
----
### `DriveOnHeading`
```yaml
@@ -791,10 +394,10 @@ builtin_interfaces/Duration total_elapsed_time
---
#feedback definition
float32 distance_traveled
```
---
----
### `DummyBehavior`
```yaml
@@ -805,10 +408,10 @@ std_msgs/String command
builtin_interfaces/Duration total_elapsed_time
---
#feedback definition
```
---
----
### `FollowPath`
```yaml
@@ -823,10 +426,10 @@ std_msgs/Empty result
#feedback definition
float32 distance_to_goal
float32 speed
```
---
----
### `FollowWaypoints`
```yaml
@@ -838,10 +441,10 @@ int32[] missed_waypoints
---
#feedback definition
uint32 current_waypoint
```
---
----
### `NavigateThroughPoses`
```yaml
@@ -859,10 +462,10 @@ builtin_interfaces/Duration estimated_time_remaining
int16 number_of_recoveries
float32 distance_remaining
int16 number_of_poses_remaining
```
---
----
### `NavigateToPose`
```yaml
@@ -879,10 +482,10 @@ builtin_interfaces/Duration navigation_time
builtin_interfaces/Duration estimated_time_remaining
int16 number_of_recoveries
float32 distance_remaining
```
---
----
### `SmoothPath`
```yaml
@@ -898,10 +501,10 @@ builtin_interfaces/Duration smoothing_duration
bool was_completed
---
#feedback definition
```
---
----
### `Spin`
```yaml
@@ -914,10 +517,10 @@ builtin_interfaces/Duration total_elapsed_time
---
#feedback definition
float32 angular_distance_traveled
```
---
----
### `Wait`
```yaml
@@ -929,6 +532,7 @@ builtin_interfaces/Duration total_elapsed_time
---
#feedback definition
builtin_interfaces/Duration time_left
```
---
----

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 629 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 269 KiB

File diff suppressed because it is too large Load Diff

View File

@@ -32,9 +32,8 @@ developer_guide/device_driver
developer_guide/add_device
developer_guide/add_action
developer_guide/actions
developer_guide/workstation_architecture
developer_guide/add_protocol
developer_guide/add_batteryPLC
developer_guide/materials_tutorial.md
```
## 接口文档

View File

@@ -2,6 +2,7 @@
sphinx>=7.0.0
sphinx-rtd-theme>=2.0.0
myst-parser>=2.0.0
sphinxcontrib-mermaid
# 用于支持Jupyter notebook文档
myst-nb>=1.0.0

View File

@@ -24,13 +24,42 @@
"Drip_back": "3a162cf9-6aac-565a-ddd7-682ba1796a4a"
},
"material_type_mappings": {
"烧杯": ["BIOYOND_PolymerStation_1FlaskCarrier", "3a14196b-24f2-ca49-9081-0cab8021bf1a"],
"试剂瓶": ["BIOYOND_PolymerStation_1BottleCarrier", ""],
"样品板": ["BIOYOND_PolymerStation_6StockCarrier", "3a14196e-b7a0-a5da-1931-35f3000281e9"],
"分装板": ["BIOYOND_PolymerStation_6VialCarrier", "3a14196e-5dfe-6e21-0c79-fe2036d052c4"],
"样品瓶": ["BIOYOND_PolymerStation_Solid_Stock", "3a14196a-cf7d-8aea-48d8-b9662c7dba94"],
"90%分装小瓶": ["BIOYOND_PolymerStation_Solid_Vial", "3a14196c-cdcf-088d-dc7d-5cf38f0ad9ea"],
"10%分装小瓶": ["BIOYOND_PolymerStation_Liquid_Vial", "3a14196c-76be-2279-4e22-7310d69aed68"]
"烧杯": [
"BIOYOND_PolymerStation_1FlaskCarrier",
"3a14196b-24f2-ca49-9081-0cab8021bf1a"
],
"试剂瓶": [
"BIOYOND_PolymerStation_1BottleCarrier",
""
],
"样品板": [
"BIOYOND_PolymerStation_6StockCarrier",
"3a14196e-b7a0-a5da-1931-35f3000281e9"
],
"分装板": [
"BIOYOND_PolymerStation_6VialCarrier",
"3a14196e-5dfe-6e21-0c79-fe2036d052c4"
],
"样品瓶": [
"BIOYOND_PolymerStation_Solid_Stock",
"3a14196a-cf7d-8aea-48d8-b9662c7dba94"
],
"90%分装小瓶": [
"BIOYOND_PolymerStation_Solid_Vial",
"3a14196c-cdcf-088d-dc7d-5cf38f0ad9ea"
],
"10%分装小瓶": [
"BIOYOND_PolymerStation_Liquid_Vial",
"3a14196c-76be-2279-4e22-7310d69aed68"
],
"枪头盒": [
"BIOYOND_PolymerStation_TipBox",
""
],
"反应器": [
"BIOYOND_PolymerStation_Reactor",
""
]
}
},
"deck": {
@@ -46,8 +75,7 @@
{
"id": "Bioyond_Deck",
"name": "Bioyond_Deck",
"children": [
],
"children": [],
"parent": "reaction_station_bioyond",
"type": "deck",
"class": "BIOYOND_PolymerReactionStation_Deck",

View File

@@ -3,7 +3,8 @@
"""
import asyncio
from typing import Dict, Any, Optional, List
from typing import Dict, Any, List
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
class SmartPumpController:
@@ -14,6 +15,8 @@ class SmartPumpController:
适用于实验室自动化系统中的液体处理任务。
"""
_ros_node: BaseROS2DeviceNode
def __init__(self, device_id: str = "smart_pump_01", port: str = "/dev/ttyUSB0"):
"""
初始化智能泵控制器
@@ -30,6 +33,9 @@ class SmartPumpController:
self.calibration_factor = 1.0
self.pump_mode = "continuous" # continuous, volume, rate
def post_init(self, ros_node: BaseROS2DeviceNode):
self._ros_node = ros_node
def connect_device(self, timeout: int = 10) -> bool:
"""
连接到泵设备
@@ -90,7 +96,7 @@ class SmartPumpController:
pump_time = (volume / flow_rate) * 60 # 转换为秒
self.current_flow_rate = flow_rate
await asyncio.sleep(min(pump_time, 3.0)) # 模拟泵送过程
await self._ros_node.sleep(min(pump_time, 3.0)) # 模拟泵送过程
self.total_volume_pumped += volume
self.current_flow_rate = 0.0
@@ -170,6 +176,8 @@ class AdvancedTemperatureController:
适用于需要精确温度控制的化学反应和材料处理过程。
"""
_ros_node: BaseROS2DeviceNode
def __init__(self, controller_id: str = "temp_controller_01"):
"""
初始化温度控制器
@@ -185,6 +193,9 @@ class AdvancedTemperatureController:
self.pid_enabled = True
self.temperature_history: List[Dict] = []
def post_init(self, ros_node: BaseROS2DeviceNode):
self._ros_node = ros_node
def set_target_temperature(self, temperature: float, rate: float = 10.0) -> bool:
"""
设置目标温度
@@ -238,7 +249,7 @@ class AdvancedTemperatureController:
}
)
await asyncio.sleep(step_time)
await self._ros_node.sleep(step_time)
# 保持历史记录不超过100条
if len(self.temperature_history) > 100:
@@ -330,6 +341,8 @@ class MultiChannelAnalyzer:
常用于光谱分析、电化学测量等应用场景。
"""
_ros_node: BaseROS2DeviceNode
def __init__(self, analyzer_id: str = "analyzer_01", channels: int = 8):
"""
初始化多通道分析仪
@@ -344,6 +357,9 @@ class MultiChannelAnalyzer:
self.is_measuring = False
self.sample_rate = 1000 # Hz
def post_init(self, ros_node: BaseROS2DeviceNode):
self._ros_node = ros_node
def configure_channel(self, channel: int, enabled: bool = True, unit: str = "V") -> bool:
"""
配置通道
@@ -376,7 +392,7 @@ class MultiChannelAnalyzer:
# 模拟数据采集
measurements = []
for second in range(duration):
for _ in range(duration):
timestamp = asyncio.get_event_loop().time()
frame_data = {}
@@ -391,7 +407,7 @@ class MultiChannelAnalyzer:
measurements.append({"timestamp": timestamp, "data": frame_data})
await asyncio.sleep(1.0) # 每秒采集一次
await self._ros_node.sleep(1.0) # 每秒采集一次
self.is_measuring = False
@@ -465,6 +481,8 @@ class AutomatedDispenser:
集成称重功能,确保分配精度和重现性。
"""
_ros_node: BaseROS2DeviceNode
def __init__(self, dispenser_id: str = "dispenser_01"):
"""
初始化自动分配器
@@ -479,6 +497,9 @@ class AutomatedDispenser:
self.container_capacity = 1000.0 # mL
self.precision_mode = True
def post_init(self, ros_node: BaseROS2DeviceNode):
self._ros_node = ros_node
def move_to_position(self, x: float, y: float, z: float) -> bool:
"""
移动到指定位置
@@ -517,7 +538,7 @@ class AutomatedDispenser:
if viscosity == "high":
dispense_time *= 2 # 高粘度液体需要更长时间
await asyncio.sleep(min(dispense_time, 5.0)) # 最多等待5秒
await self._ros_node.sleep(min(dispense_time, 5.0)) # 最多等待5秒
self.dispensed_total += volume

View File

@@ -12,6 +12,7 @@ from serial import Serial
from serial.serialutil import SerialException
from unilabos.messages import Point3D
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
class GrblCNCConnectionError(Exception):
@@ -32,6 +33,7 @@ class GrblCNCInfo:
class GrblCNCAsync:
_status: str = "Offline"
_position: Point3D = Point3D(x=0.0, y=0.0, z=0.0)
_ros_node: BaseROS2DeviceNode
def __init__(self, port: str, address: str = "1", limits: tuple[int, int, int, int, int, int] = (-150, 150, -200, 0, 0, 60)):
self.port = port
@@ -58,6 +60,9 @@ class GrblCNCAsync:
self._run_future: Optional[Future[Any]] = None
self._run_lock = Lock()
def post_init(self, ros_node: BaseROS2DeviceNode):
self._ros_node = ros_node
def _read_all(self):
data = self._serial.read_until(b"\n")
data_decoded = data.decode()
@@ -148,7 +153,7 @@ class GrblCNCAsync:
try:
await self._query(command)
while True:
await asyncio.sleep(0.2) # Wait for 0.5 seconds before polling again
await self._ros_node.sleep(0.2) # Wait for 0.5 seconds before polling again
status = await self.get_status()
if "Idle" in status:
@@ -214,7 +219,7 @@ class GrblCNCAsync:
self._pose_number = i
self.pose_number_remaining = len(points) - i
await self.set_position(point)
await asyncio.sleep(0.5)
await self._ros_node.sleep(0.5)
self._step_number = -1
async def stop_operation(self):
@@ -235,7 +240,7 @@ class GrblCNCAsync:
async def open(self):
if self._read_task:
raise GrblCNCConnectionError
self._read_task = asyncio.create_task(self._read_loop())
self._read_task = self._ros_node.create_task(self._read_loop())
try:
await self.get_status()

View File

@@ -2,6 +2,8 @@ import time
import asyncio
from pydantic import BaseModel
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
class Point3D(BaseModel):
x: float
@@ -14,10 +16,15 @@ def d(a: Point3D, b: Point3D) -> float:
class MockCNCAsync:
_ros_node: BaseROS2DeviceNode["MockCNCAsync"]
def __init__(self):
self._position: Point3D = Point3D(x=0.0, y=0.0, z=0.0)
self._status = "Idle"
def post_create(self, ros_node):
self._ros_node = ros_node
@property
def position(self) -> Point3D:
return self._position
@@ -38,5 +45,5 @@ class MockCNCAsync:
self._position.x = current_pos.x + (position.x - current_pos.x) / 20 * (i+1)
self._position.y = current_pos.y + (position.y - current_pos.y) / 20 * (i+1)
self._position.z = current_pos.z + (position.z - current_pos.z) / 20 * (i+1)
await asyncio.sleep(move_time / 20)
await self._ros_node.sleep(move_time / 20)
self._status = "Idle"

View File

@@ -15,9 +15,12 @@ from typing import List, Optional, Dict, Any, Union, Tuple
from dataclasses import dataclass
from abc import ABC, abstractmethod
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
# 基础导入
try:
from pylabrobot.resources import Deck, Plate, TipRack, Tip, Resource, Well
PYLABROBOT_AVAILABLE = True
except ImportError:
# 如果 pylabrobot 不可用,创建基础的模拟类
@@ -42,17 +45,16 @@ except ImportError:
class Well(Resource):
pass
# LaiYu_Liquid 控制器导入
try:
from .controllers.pipette_controller import (
PipetteController, TipStatus, LiquidClass, LiquidParameters
)
from .controllers.xyz_controller import (
XYZController, MachineConfig, CoordinateOrigin, MotorAxis
)
from .controllers.pipette_controller import PipetteController, TipStatus, LiquidClass, LiquidParameters
from .controllers.xyz_controller import XYZController, MachineConfig, CoordinateOrigin, MotorAxis
CONTROLLERS_AVAILABLE = True
except ImportError:
CONTROLLERS_AVAILABLE = False
# 创建模拟的控制器类
class PipetteController:
def __init__(self, *args, **kwargs):
@@ -71,17 +73,20 @@ except ImportError:
def connect_device(self):
return True
logger = logging.getLogger(__name__)
class LaiYuLiquidError(RuntimeError):
"""LaiYu_Liquid 设备异常"""
pass
@dataclass
class LaiYuLiquidConfig:
"""LaiYu_Liquid 设备配置"""
port: str = "/dev/cu.usbserial-3130" # RS485转USB端口
address: int = 1 # 设备地址
baudrate: int = 9600 # 波特率
@@ -155,7 +160,17 @@ class LaiYuLiquidDeck:
class LaiYuLiquidContainer:
"""LaiYu_Liquid 容器类"""
def __init__(self, name: str, size_x: float = 0, size_y: float = 0, size_z: float = 0, container_type: str = "", volume: float = 0.0, max_volume: float = 1000.0, lid_height: float = 0.0):
def __init__(
self,
name: str,
size_x: float = 0,
size_y: float = 0,
size_z: float = 0,
container_type: str = "",
volume: float = 0.0,
max_volume: float = 1000.0,
lid_height: float = 0.0,
):
self.name = name
self.size_x = size_x
self.size_y = size_y
@@ -197,17 +212,22 @@ class LaiYuLiquidContainer:
def assign_child_resource(self, resource, location=None):
"""分配子资源 - 与 PyLabRobot 资源管理系统兼容"""
if hasattr(resource, 'name'):
self.child_resources[resource.name] = {
'resource': resource,
'location': location
}
if hasattr(resource, "name"):
self.child_resources[resource.name] = {"resource": resource, "location": location}
class LaiYuLiquidTipRack:
"""LaiYu_Liquid 吸头架类"""
def __init__(self, name: str, size_x: float = 0, size_y: float = 0, size_z: float = 0, tip_count: int = 96, tip_volume: float = 1000.0):
def __init__(
self,
name: str,
size_x: float = 0,
size_y: float = 0,
size_z: float = 0,
tip_count: int = 96,
tip_volume: float = 1000.0,
):
self.name = name
self.size_x = size_x
self.size_y = size_y
@@ -240,10 +260,7 @@ class LaiYuLiquidTipRack:
def assign_child_resource(self, resource, location=None):
"""分配子资源到指定位置"""
self.child_resources[resource.name] = {
'resource': resource,
'location': location
}
self.child_resources[resource.name] = {"resource": resource, "location": location}
def get_module_info():
@@ -253,24 +270,17 @@ def get_module_info():
"version": "1.0.0",
"description": "LaiYu液体处理工作站模块提供移液器控制、XYZ轴控制和资源管理功能",
"author": "UniLabOS Team",
"capabilities": [
"移液器控制",
"XYZ轴运动控制",
"吸头架管理",
"板和容器管理",
"资源位置管理"
],
"dependencies": {
"required": ["serial"],
"optional": ["pylabrobot"]
}
"capabilities": ["移液器控制", "XYZ轴运动控制", "吸头架管理", "板和容器管理", "资源位置管理"],
"dependencies": {"required": ["serial"], "optional": ["pylabrobot"]},
}
class LaiYuLiquidBackend:
"""LaiYu_Liquid 硬件通信后端"""
def __init__(self, config: LaiYuLiquidConfig, deck: Optional['LaiYuLiquidDeck'] = None):
_ros_node: BaseROS2DeviceNode
def __init__(self, config: LaiYuLiquidConfig, deck: Optional["LaiYuLiquidDeck"] = None):
self.config = config
self.deck = deck # 工作台引用,用于获取资源位置信息
self.pipette_controller = None
@@ -283,6 +293,9 @@ class LaiYuLiquidBackend:
self.tip_attached = False
self.current_volume = 0.0
def post_init(self, ros_node: BaseROS2DeviceNode):
self._ros_node = ros_node
def _validate_position(self, x: float, y: float, z: float) -> bool:
"""验证位置是否在安全范围内"""
try:
@@ -348,7 +361,7 @@ class LaiYuLiquidBackend:
safe_position = (
self.config.deck_width / 2, # 工作台中心X
self.config.deck_height / 2, # 工作台中心Y
self.config.safe_height # 安全高度Z
self.config.safe_height, # 安全高度Z
)
if not self._validate_position(*safe_position):
@@ -375,17 +388,12 @@ class LaiYuLiquidBackend:
try:
if CONTROLLERS_AVAILABLE:
# 初始化移液器控制器
self.pipette_controller = PipetteController(
port=self.config.port,
address=self.config.address
)
self.pipette_controller = PipetteController(port=self.config.port, address=self.config.address)
# 初始化XYZ控制器
machine_config = MachineConfig()
self.xyz_controller = XYZController(
port=self.config.port,
baudrate=self.config.baudrate,
machine_config=machine_config
port=self.config.port, baudrate=self.config.baudrate, machine_config=machine_config
)
# 连接设备
@@ -412,10 +420,10 @@ class LaiYuLiquidBackend:
async def stop(self):
"""停止设备"""
try:
if self.pipette_controller and hasattr(self.pipette_controller, 'disconnect'):
if self.pipette_controller and hasattr(self.pipette_controller, "disconnect"):
await asyncio.to_thread(self.pipette_controller.disconnect)
if self.xyz_controller and hasattr(self.xyz_controller, 'disconnect'):
if self.xyz_controller and hasattr(self.xyz_controller, "disconnect"):
await asyncio.to_thread(self.xyz_controller.disconnect)
self.is_connected = False
@@ -432,7 +440,7 @@ class LaiYuLiquidBackend:
raise LaiYuLiquidError("设备未连接")
# 模拟移动
await asyncio.sleep(0.1) # 模拟移动时间
await self._ros_node.sleep(0.1) # 模拟移动时间
self.current_position = (x, y, z)
logger.debug(f"移动到位置: ({x}, {y}, {z})")
return True
@@ -472,9 +480,11 @@ class LaiYuLiquidBackend:
pickup_z = tip_z - self.config.tip_pickup_force_depth
retract_z = tip_z + self.config.tip_pickup_retract_height
if not (self._validate_position(tip_x, tip_y, safe_z) and
self._validate_position(tip_x, tip_y, pickup_z) and
self._validate_position(tip_x, tip_y, retract_z)):
if not (
self._validate_position(tip_x, tip_y, safe_z)
and self._validate_position(tip_x, tip_y, pickup_z)
and self._validate_position(tip_x, tip_y, retract_z)
):
logger.error("枪头拾取位置超出安全范围")
return False
@@ -487,8 +497,7 @@ class LaiYuLiquidBackend:
safe_z = tip_z + self.config.tip_approach_height
logger.info(f"移动到枪头上方安全位置: ({tip_x:.2f}, {tip_y:.2f}, {safe_z:.2f})")
move_success = await asyncio.to_thread(
self.xyz_controller.move_to_work_coord,
tip_x, tip_y, safe_z
self.xyz_controller.move_to_work_coord, tip_x, tip_y, safe_z
)
if not move_success:
logger.error("移动到枪头上方失败")
@@ -498,22 +507,20 @@ class LaiYuLiquidBackend:
pickup_z = tip_z - self.config.tip_pickup_force_depth
logger.info(f"Z轴下降到枪头拾取位置: {pickup_z:.2f}mm")
z_down_success = await asyncio.to_thread(
self.xyz_controller.move_to_work_coord,
tip_x, tip_y, pickup_z
self.xyz_controller.move_to_work_coord, tip_x, tip_y, pickup_z
)
if not z_down_success:
logger.error("Z轴下降到枪头位置失败")
return False
# 3. 等待一小段时间确保枪头牢固附着
await asyncio.sleep(0.2)
await self._ros_node.sleep(0.2)
# 4. Z轴上升到回退高度
retract_z = tip_z + self.config.tip_pickup_retract_height
logger.info(f"Z轴上升到回退高度: {retract_z:.2f}mm")
z_up_success = await asyncio.to_thread(
self.xyz_controller.move_to_work_coord,
tip_x, tip_y, retract_z
self.xyz_controller.move_to_work_coord, tip_x, tip_y, retract_z
)
if not z_up_success:
logger.error("Z轴上升失败")
@@ -533,7 +540,7 @@ class LaiYuLiquidBackend:
else:
# 模拟模式
logger.info("模拟模式:执行枪头拾取动作")
await asyncio.sleep(1.0) # 模拟整个拾取过程的时间
await self._ros_node.sleep(1.0) # 模拟整个拾取过程的时间
self.current_position = (tip_x, tip_y, tip_z + self.config.tip_pickup_retract_height)
# 6. 标记枪头已附着
@@ -578,8 +585,10 @@ class LaiYuLiquidBackend:
safe_z = drop_z + self.config.safe_height
drop_height_z = drop_z + self.config.tip_drop_height
if not (self._validate_position(drop_x, drop_y, safe_z) and
self._validate_position(drop_x, drop_y, drop_height_z)):
if not (
self._validate_position(drop_x, drop_y, safe_z)
and self._validate_position(drop_x, drop_y, drop_height_z)
):
logger.error("枪头丢弃位置超出安全范围")
return False
@@ -592,8 +601,7 @@ class LaiYuLiquidBackend:
safe_z = drop_z + self.config.tip_drop_height
logger.info(f"移动到丢弃位置上方: ({drop_x:.2f}, {drop_y:.2f}, {safe_z:.2f})")
move_success = await asyncio.to_thread(
self.xyz_controller.move_to_work_coord,
drop_x, drop_y, safe_z
self.xyz_controller.move_to_work_coord, drop_x, drop_y, safe_z
)
if not move_success:
logger.error("移动到丢弃位置上方失败")
@@ -602,8 +610,7 @@ class LaiYuLiquidBackend:
# 2. Z轴下降到丢弃高度
logger.info(f"Z轴下降到丢弃高度: {drop_z:.2f}mm")
z_down_success = await asyncio.to_thread(
self.xyz_controller.move_to_work_coord,
drop_x, drop_y, drop_z
self.xyz_controller.move_to_work_coord, drop_x, drop_y, drop_z
)
if not z_down_success:
logger.error("Z轴下降到丢弃位置失败")
@@ -619,13 +626,12 @@ class LaiYuLiquidBackend:
logger.warning(f"枪头弹出命令失败: {e}")
# 4. 等待一小段时间确保枪头完全脱离
await asyncio.sleep(0.3)
await self._ros_node.sleep(0.3)
# 5. Z轴上升到安全高度
logger.info(f"Z轴上升到安全高度: {safe_z:.2f}mm")
z_up_success = await asyncio.to_thread(
self.xyz_controller.move_to_work_coord,
drop_x, drop_y, safe_z
self.xyz_controller.move_to_work_coord, drop_x, drop_y, safe_z
)
if not z_up_success:
logger.error("Z轴上升失败")
@@ -645,7 +651,7 @@ class LaiYuLiquidBackend:
else:
# 模拟模式
logger.info("模拟模式:执行枪头丢弃动作")
await asyncio.sleep(0.8) # 模拟整个丢弃过程的时间
await self._ros_node.sleep(0.8) # 模拟整个丢弃过程的时间
self.current_position = (drop_x, drop_y, drop_z + self.config.tip_drop_height)
# 7. 标记枪头已脱离,清空体积
@@ -671,7 +677,7 @@ class LaiYuLiquidBackend:
raise LaiYuLiquidError(f"体积超出范围: {volume}")
# 模拟吸取
await asyncio.sleep(0.3)
await self._ros_node.sleep(0.3)
self.current_volume += volume
logger.debug(f"{location} 吸取 {volume} μL")
return True
@@ -693,7 +699,7 @@ class LaiYuLiquidBackend:
raise LaiYuLiquidError(f"分配体积无效: {volume}")
# 模拟分配
await asyncio.sleep(0.3)
await self._ros_node.sleep(0.3)
self.current_volume -= volume
logger.debug(f"{location} 分配 {volume} μL")
return True
@@ -765,8 +771,9 @@ class LaiYuLiquid:
await self.backend.stop()
self.is_setup = False
async def transfer(self, source: str, target: str, volume: float,
tip_rack: str = "tip_rack_1", tip_position: int = 0) -> bool:
async def transfer(
self, source: str, target: str, volume: float, tip_rack: str = "tip_rack_1", tip_position: int = 0
) -> bool:
"""液体转移"""
try:
if not self.is_setup:
@@ -788,7 +795,7 @@ class LaiYuLiquid:
("吸取液体", self.backend.aspirate(volume, source)),
("移动到目标位置", self.backend.move_to(*target_pos)),
("分配液体", self.backend.dispense(volume, target)),
("丢弃吸头", self.backend.drop_tip())
("丢弃吸头", self.backend.drop_tip()),
]
for step_name, step_coro in steps:
@@ -823,7 +830,7 @@ class LaiYuLiquid:
"current_position": self.backend.current_position,
"tip_attached": self.backend.tip_attached,
"current_volume": self.backend.current_volume,
"resources": self.deck.list_resources()
"resources": self.deck.list_resources(),
}
@@ -846,7 +853,7 @@ def create_quick_setup() -> LaiYuLiquidDeck:
create_tip_rack_1000ul,
create_tip_rack_200ul,
create_96_well_plate,
create_waste_container
create_waste_container,
)
# 添加基本资源
@@ -877,5 +884,5 @@ __all__ = [
"LaiYuLiquidTipRack",
"LaiYuLiquidError",
"create_quick_setup",
"get_module_info"
"get_module_info",
]

View File

@@ -25,6 +25,8 @@ from pylabrobot.resources import (
Tip,
)
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
class LiquidHandlerMiddleware(LiquidHandler):
def __init__(self, backend: LiquidHandlerBackend, deck: Deck, simulator: bool = False, channel_num: int = 8):
@@ -536,6 +538,7 @@ class LiquidHandlerMiddleware(LiquidHandler):
class LiquidHandlerAbstract(LiquidHandlerMiddleware):
"""Extended LiquidHandler with additional operations."""
support_touch_tip = True
_ros_node: BaseROS2DeviceNode
def __init__(self, backend: LiquidHandlerBackend, deck: Deck, simulator: bool=False, channel_num:int = 8):
"""Initialize a LiquidHandler.
@@ -548,8 +551,11 @@ class LiquidHandlerAbstract(LiquidHandlerMiddleware):
self.group_info = dict()
super().__init__(backend, deck, simulator, channel_num)
def post_init(self, ros_node: BaseROS2DeviceNode):
self._ros_node = ros_node
@classmethod
def set_liquid(self, wells: list[Well], liquid_names: list[str], volumes: list[float]):
def set_liquid(cls, wells: list[Well], liquid_names: list[str], volumes: list[float]):
"""Set the liquid in a well."""
for well, liquid_name, volume in zip(wells, liquid_names, volumes):
well.set_liquids([(liquid_name, volume)]) # type: ignore
@@ -1081,7 +1087,7 @@ class LiquidHandlerAbstract(LiquidHandlerMiddleware):
print(f"Waiting time: {msg}")
print(f"Current time: {time.strftime('%H:%M:%S')}")
print(f"Time to finish: {time.strftime('%H:%M:%S', time.localtime(time.time() + seconds))}")
await asyncio.sleep(seconds)
await self._ros_node.sleep(seconds)
if msg:
print(f"Done: {msg}")
print(f"Current time: {time.strftime('%H:%M:%S')}")

View File

@@ -30,6 +30,7 @@ from pylabrobot.liquid_handling.standard import (
from pylabrobot.resources import Tip, Deck, Plate, Well, TipRack, Resource, Container, Coordinate, TipSpot, Trash
from unilabos.devices.liquid_handling.liquid_handler_abstract import LiquidHandlerAbstract
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
class PRCXIError(RuntimeError):
@@ -162,6 +163,10 @@ class PRCXI9300Handler(LiquidHandlerAbstract):
)
super().__init__(backend=self._unilabos_backend, deck=deck, simulator=simulator, channel_num=channel_num)
def post_init(self, ros_node: BaseROS2DeviceNode):
super().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]):
return super().set_liquid(wells, liquid_names, volumes)
@@ -424,6 +429,7 @@ class PRCXI9300Backend(LiquidHandlerBackend):
_num_channels = 8 # 默认通道数为 8
_is_reset_ok = False
_ros_node: BaseROS2DeviceNode
@property
def is_reset_ok(self) -> bool:
@@ -456,6 +462,9 @@ class PRCXI9300Backend(LiquidHandlerBackend):
self._execute_setup = setup
self.debug = debug
def post_init(self, ros_node: BaseROS2DeviceNode):
self._ros_node = ros_node
def create_protocol(self, protocol_name):
self.protocol_name = protocol_name
self.steps_todo_list = []
@@ -500,7 +509,7 @@ class PRCXI9300Backend(LiquidHandlerBackend):
self.api_client.call("IAutomation", "Reset")
while not self.is_reset_ok:
print("Waiting for PRCXI9300 to reset...")
await asyncio.sleep(1)
await self._ros_node.sleep(1)
print("PRCXI9300 reset successfully.")
except ConnectionRefusedError as e:
raise RuntimeError(
@@ -533,7 +542,9 @@ class PRCXI9300Backend(LiquidHandlerBackend):
tipspot_index = tipspot.parent.children.index(tipspot)
tip_columns.append(tipspot_index // 8)
if len(set(tip_columns)) != 1:
raise ValueError("All pickups must be from the same tip column. Found different columns: " + str(tip_columns))
raise ValueError(
"All pickups must be from the same tip column. Found different columns: " + str(tip_columns)
)
PlateNo = plate_indexes[0] + 1
hole_col = tip_columns[0] + 1
hole_row = 1
@@ -1109,12 +1120,15 @@ class PRCXI9300Api:
"LiquidDispensingMethod": liquid_method,
}
class DefaultLayout:
def __init__(self, product_name: str = "PRCXI9300"):
self.labresource = {}
if product_name not in ["PRCXI9300", "PRCXI9320"]:
raise ValueError(f"Unsupported product_name: {product_name}. Only 'PRCXI9300' and 'PRCXI9320' are supported.")
raise ValueError(
f"Unsupported product_name: {product_name}. Only 'PRCXI9300' and 'PRCXI9320' are supported."
)
if product_name == "PRCXI9300":
self.rows = 2
@@ -1129,25 +1143,93 @@ class DefaultLayout:
self.layout = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
self.trash_slot = 16
self.waste_liquid_slot = 12
self.default_layout = {"MatrixId":f"{time.time()}","MatrixName":f"{time.time()}","MatrixCount":16,"WorkTablets":
[{"Number": 1, "Code": "T1", "Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0}},
{"Number": 2, "Code": "T2", "Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0}},
{"Number": 3, "Code": "T3", "Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0}},
{"Number": 4, "Code": "T4", "Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0}},
{"Number": 5, "Code": "T5", "Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0}},
{"Number": 6, "Code": "T6", "Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0}},
{"Number": 7, "Code": "T7", "Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0}},
{"Number": 8, "Code": "T8", "Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0}},
{"Number": 9, "Code": "T9", "Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0}},
{"Number": 10, "Code": "T10", "Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0}},
{"Number": 11, "Code": "T11", "Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0}},
{"Number": 12, "Code": "T12", "Material": {"uuid": "730067cf07ae43849ddf4034299030e9", "materialEnum": 0}}, # 这个设置成废液槽,用储液槽表示
{"Number": 13, "Code": "T13", "Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0}},
{"Number": 14, "Code": "T14", "Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0}},
{"Number": 15, "Code": "T15", "Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0}},
{"Number": 16, "Code": "T16", "Material": {"uuid": "730067cf07ae43849ddf4034299030e9", "materialEnum": 0}} # 这个设置成垃圾桶,用储液槽表示
]
}
self.default_layout = {
"MatrixId": f"{time.time()}",
"MatrixName": f"{time.time()}",
"MatrixCount": 16,
"WorkTablets": [
{
"Number": 1,
"Code": "T1",
"Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0},
},
{
"Number": 2,
"Code": "T2",
"Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0},
},
{
"Number": 3,
"Code": "T3",
"Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0},
},
{
"Number": 4,
"Code": "T4",
"Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0},
},
{
"Number": 5,
"Code": "T5",
"Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0},
},
{
"Number": 6,
"Code": "T6",
"Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0},
},
{
"Number": 7,
"Code": "T7",
"Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0},
},
{
"Number": 8,
"Code": "T8",
"Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0},
},
{
"Number": 9,
"Code": "T9",
"Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0},
},
{
"Number": 10,
"Code": "T10",
"Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0},
},
{
"Number": 11,
"Code": "T11",
"Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0},
},
{
"Number": 12,
"Code": "T12",
"Material": {"uuid": "730067cf07ae43849ddf4034299030e9", "materialEnum": 0},
}, # 这个设置成废液槽,用储液槽表示
{
"Number": 13,
"Code": "T13",
"Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0},
},
{
"Number": 14,
"Code": "T14",
"Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0},
},
{
"Number": 15,
"Code": "T15",
"Material": {"uuid": "57b1e4711e9e4a32b529f3132fc5931f", "materialEnum": 0},
},
{
"Number": 16,
"Code": "T16",
"Material": {"uuid": "730067cf07ae43849ddf4034299030e9", "materialEnum": 0},
}, # 这个设置成垃圾桶,用储液槽表示
],
}
def get_layout(self) -> Dict[str, Any]:
return {
@@ -1155,7 +1237,7 @@ class DefaultLayout:
"columns": self.columns,
"layout": self.layout,
"trash_slot": self.trash_slot,
"waste_liquid_slot": self.waste_liquid_slot
"waste_liquid_slot": self.waste_liquid_slot,
}
def get_trash_slot(self) -> int:
@@ -1181,14 +1263,16 @@ class DefaultLayout:
# 计算总需求
total_needed = sum(count for _, _, count in needs)
if total_needed > len(available_positions):
raise ValueError(f"需要 {total_needed} 个位置,但只有 {len(available_positions)} 个可用位置排除位置12和16")
raise ValueError(
f"需要 {total_needed} 个位置,但只有 {len(available_positions)} 个可用位置排除位置12和16"
)
# 依次分配位置
current_pos = 0
for reagent_name, material_name, count in needs:
material_uuid = self.labresource[material_name]['uuid']
material_enum = self.labresource[material_name]['materialEnum']
material_uuid = self.labresource[material_name]["uuid"]
material_enum = self.labresource[material_name]["materialEnum"]
for _ in range(count):
if current_pos >= len(available_positions):
@@ -1196,17 +1280,18 @@ class DefaultLayout:
position = available_positions[current_pos]
# 找到对应的tablet并更新
for tablet in self.default_layout['WorkTablets']:
if tablet['Number'] == position:
tablet['Material']['uuid'] = material_uuid
tablet['Material']['materialEnum'] = material_enum
layout_list.append(dict(reagent_name=reagent_name, material_name=material_name, positions=position))
for tablet in self.default_layout["WorkTablets"]:
if tablet["Number"] == position:
tablet["Material"]["uuid"] = material_uuid
tablet["Material"]["materialEnum"] = material_enum
layout_list.append(
dict(reagent_name=reagent_name, material_name=material_name, positions=position)
)
break
current_pos += 1
return self.default_layout, layout_list
if __name__ == "__main__":
# Example usage
# 1. 用导出的json给每个T1 T2板子设定相应的物料如果是孔板和枪头盒要对应区分
@@ -1302,9 +1387,6 @@ if __name__ == "__main__":
# # # plate2.set_well_liquids(plate_2_liquids)
# handler = PRCXI9300Handler(deck=deck, host="10.181.214.132", port=9999,
# timeout=10.0, setup=False, debug=False,
# simulator=True,
@@ -1391,10 +1473,7 @@ if __name__ == "__main__":
# # input("Press Enter to continue...") # Wait for user input before proceeding
# # print("PRCXI9300Handler initialized with deck and host settings.")
### 9320 ###
### 9320 ###
deck = PRCXI9300Deck(name="PRCXI_Deck", size_x=100, size_y=100, size_z=100)
@@ -1412,12 +1491,15 @@ if __name__ == "__main__":
new_plate: PRCXI9300Container = PRCXI9300Container.deserialize(well_containers)
return new_plate
def get_tip_rack(name: str, child_prefix: str="tip") -> PRCXI9300Container:
def get_tip_rack(name: str, child_prefix: str = "tip") -> PRCXI9300Container:
tip_racks = opentrons_96_tiprack_10ul(name).serialize()
tip_rack = PRCXI9300Container(
name=name, size_x=50, size_y=50, size_z=10, category="tip_rack", ordering=collections.OrderedDict({
k: f"{child_prefix}_{k}" for k, v in tip_racks["ordering"].items()
})
name=name,
size_x=50,
size_y=50,
size_z=10,
category="tip_rack",
ordering=collections.OrderedDict({k: f"{child_prefix}_{k}" for k, v in tip_racks["ordering"].items()}),
)
tip_rack_serialized = tip_rack.serialize()
tip_rack_serialized["parent_name"] = deck.name
@@ -1629,6 +1711,7 @@ if __name__ == "__main__":
)
backend: PRCXI9300Backend = handler.backend
from pylabrobot.resources import set_volume_tracking
set_volume_tracking(enabled=True)
# res = backend.api_client.get_all_materials()
asyncio.run(handler.setup()) # Initialize the handler and setup the connection
@@ -1652,26 +1735,25 @@ if __name__ == "__main__":
asyncio.run(handler.run_protocol())
time.sleep(5)
os._exit(0)
# 第一种情景:一个孔往多个孔加液
# 第一种情景:一个孔往多个孔加液
# plate_2_liquids = handler.set_group("water", [plate2.children[0]], [300])
# plate5_liquids = handler.set_group("master_mix", plate5.children[:23], [100]*23)
# 第二个情景:多个孔往多个孔加液(但是个数得对应)
plate_2_liquids = handler.set_group("water", plate2.children[:23], [300]*23)
plate5_liquids = handler.set_group("master_mix", plate5.children[:23], [100]*23)
# 第二个情景:多个孔往多个孔加液(但是个数得对应)
plate_2_liquids = handler.set_group("water", plate2.children[:23], [300] * 23)
plate5_liquids = handler.set_group("master_mix", plate5.children[:23], [100] * 23)
# plate11.set_well_liquids([("Water", 100) if (i % 8 == 0 and i // 8 < 6) else (None, 100) for i in range(96)]) # Set liquids for every 8 wells in plate8
# plate11.set_well_liquids([("Water", 100) if (i % 8 == 0 and i // 8 < 6) else (None, 100) for i in range(96)]) # Set liquids for every 8 wells in plate8
# A = tree_to_list([resource_plr_to_ulab(deck)])
# # with open("deck.json", "w", encoding="utf-8") as f:
# # json.dump(A, f, indent=4, ensure_ascii=False)
# A = tree_to_list([resource_plr_to_ulab(deck)])
# # with open("deck.json", "w", encoding="utf-8") as f:
# # json.dump(A, f, indent=4, ensure_ascii=False)
# print(plate11.get_well(0).tracker.get_used_volume())
# print(plate11.get_well(0).tracker.get_used_volume())
# Initialize the backend and setup the connection
asyncio.run(handler.transfer_group("water", "master_mix", 10)) # Reset tip tracking
# asyncio.run(handler.pick_up_tips([plate8.children[8]],[0]))
# print(plate8.children[8])
# asyncio.run(handler.run_protocol())
@@ -1685,121 +1767,118 @@ if __name__ == "__main__":
# print(plate1.children[0])
# asyncio.run(handler.discard_tips([0]))
# asyncio.run(handler.add_liquid(
# asp_vols=[10]*7,
# dis_vols=[10]*7,
# reagent_sources=plate11.children[:7],
# targets=plate1.children[2:9],
# use_channels=[0],
# flow_rates=[None] * 7,
# offsets=[Coordinate(0, 0, 0)] * 7,
# liquid_height=[None] * 7,
# blow_out_air_volume=[None] * 2,
# delays=None,
# mix_time=3,
# mix_vol=5,
# spread="custom",
# ))
# asyncio.run(handler.add_liquid(
# asp_vols=[10]*7,
# dis_vols=[10]*7,
# reagent_sources=plate11.children[:7],
# targets=plate1.children[2:9],
# use_channels=[0],
# flow_rates=[None] * 7,
# offsets=[Coordinate(0, 0, 0)] * 7,
# liquid_height=[None] * 7,
# blow_out_air_volume=[None] * 2,
# delays=None,
# mix_time=3,
# mix_vol=5,
# spread="custom",
# ))
# asyncio.run(handler.run_protocol()) # Run the protocol
# # # asyncio.run(handler.transfer_liquid(
# # # asp_vols=[10]*2,
# # # dis_vols=[10]*2,
# # # sources=plate11.children[:2],
# # # targets=plate11.children[-2:],
# # # use_channels=[0],
# # # offsets=[Coordinate(0, 0, 0)] * 4,
# # # liquid_height=[None] * 2,
# # # blow_out_air_volume=[None] * 2,
# # # delays=None,
# # # mix_times=3,
# # # mix_vol=5,
# # # spread="wide",
# # # tip_racks=[plate8]
# # # ))
# # # asyncio.run(handler.remove_liquid(
# # # vols=[10]*2,
# # # sources=plate11.children[:2],
# # # waste_liquid=plate11.children[43],
# # # use_channels=[0],
# # # offsets=[Coordinate(0, 0, 0)] * 4,
# # # liquid_height=[None] * 2,
# # # blow_out_air_volume=[None] * 2,
# # # delays=None,
# # # spread="wide"
# # # ))
# # asyncio.run(handler.run_protocol())
# # # asyncio.run(handler.discard_tips())
# # # asyncio.run(handler.mix(well_containers.children[:8
# # # ], mix_time=3, mix_vol=50, height_to_bottom=0.5, offsets=Coordinate(0, 0, 0), mix_rate=100))
# # #print(json.dumps(handler._unilabos_backend.steps_todo_list, indent=2)) # Print matrix info
# # # asyncio.run(handler.transfer_liquid(
# # # asp_vols=[10]*2,
# # # dis_vols=[10]*2,
# # # sources=plate11.children[:2],
# # # targets=plate11.children[-2:],
# # # use_channels=[0],
# # # offsets=[Coordinate(0, 0, 0)] * 4,
# # # liquid_height=[None] * 2,
# # # blow_out_air_volume=[None] * 2,
# # # delays=None,
# # # mix_times=3,
# # # mix_vol=5,
# # # spread="wide",
# # # tip_racks=[plate8]
# # # ))
# # # asyncio.run(handler.remove_liquid(
# # # vols=[10]*2,
# # # sources=plate11.children[:2],
# # # waste_liquid=plate11.children[43],
# # # use_channels=[0],
# # # offsets=[Coordinate(0, 0, 0)] * 4,
# # # liquid_height=[None] * 2,
# # # blow_out_air_volume=[None] * 2,
# # # delays=None,
# # # spread="wide"
# # # ))
# # asyncio.run(handler.run_protocol())
# # # asyncio.run(handler.discard_tips())
# # # asyncio.run(handler.mix(well_containers.children[:8
# # # ], mix_time=3, mix_vol=50, height_to_bottom=0.5, offsets=Coordinate(0, 0, 0), mix_rate=100))
# # #print(json.dumps(handler._unilabos_backend.steps_todo_list, indent=2)) # Print matrix info
# # # asyncio.run(handler.remove_liquid(
# # # vols=[100]*16,
# # # sources=well_containers.children[-16:],
# # # waste_liquid=well_containers.children[:16], # 这个有些奇怪,但是好像也只能这么写
# # # use_channels=[0, 1, 2, 3, 4, 5, 6, 7],
# # # flow_rates=[None] * 32,
# # # offsets=[Coordinate(0, 0, 0)] * 32,
# # # liquid_height=[None] * 32,
# # # blow_out_air_volume=[None] * 32,
# # # spread="wide",
# # # ))
# # # asyncio.run(handler.transfer_liquid(
# # # asp_vols=[100]*16,
# # # dis_vols=[100]*16,
# # # tip_racks=[tip_rack],
# # # sources=well_containers.children[-16:],
# # # targets=well_containers.children[:16],
# # # use_channels=[0, 1, 2, 3, 4, 5, 6, 7],
# # # offsets=[Coordinate(0, 0, 0)] * 32,
# # # asp_flow_rates=[None] * 16,
# # # dis_flow_rates=[None] * 16,
# # # liquid_height=[None] * 32,
# # # blow_out_air_volume=[None] * 32,
# # # mix_times=3,
# # # mix_vol=50,
# # # spread="wide",
# # # ))
# # print(json.dumps(handler._unilabos_backend.steps_todo_list, indent=2)) # Print matrix info
# # # input("pick_up_tips add step")
#asyncio.run(handler.run_protocol()) # Run the protocol
# # # input("Running protocol...")
# # # input("Press Enter to continue...") # Wait for user input before proceeding
# # # print("PRCXI9300Handler initialized with deck and host settings.")
# 一些推荐版位组合的测试样例:
# 一些推荐版位组合的测试样例:
# # # asyncio.run(handler.remove_liquid(
# # # vols=[100]*16,
# # # sources=well_containers.children[-16:],
# # # waste_liquid=well_containers.children[:16], # 这个有些奇怪,但是好像也只能这么写
# # # use_channels=[0, 1, 2, 3, 4, 5, 6, 7],
# # # flow_rates=[None] * 32,
# # # offsets=[Coordinate(0, 0, 0)] * 32,
# # # liquid_height=[None] * 32,
# # # blow_out_air_volume=[None] * 32,
# # # spread="wide",
# # # ))
# # # asyncio.run(handler.transfer_liquid(
# # # asp_vols=[100]*16,
# # # dis_vols=[100]*16,
# # # tip_racks=[tip_rack],
# # # sources=well_containers.children[-16:],
# # # targets=well_containers.children[:16],
# # # use_channels=[0, 1, 2, 3, 4, 5, 6, 7],
# # # offsets=[Coordinate(0, 0, 0)] * 32,
# # # asp_flow_rates=[None] * 16,
# # # dis_flow_rates=[None] * 16,
# # # liquid_height=[None] * 32,
# # # blow_out_air_volume=[None] * 32,
# # # mix_times=3,
# # # mix_vol=50,
# # # spread="wide",
# # # ))
# # print(json.dumps(handler._unilabos_backend.steps_todo_list, indent=2)) # Print matrix info
# # # input("pick_up_tips add step")
# asyncio.run(handler.run_protocol()) # Run the protocol
# # # input("Running protocol...")
# # # input("Press Enter to continue...") # Wait for user input before proceeding
# # # print("PRCXI9300Handler initialized with deck and host settings.")
# 一些推荐版位组合的测试样例:
# 一些推荐版位组合的测试样例:
with open("prcxi_material.json", "r") as f:
material_info = json.load(f)
layout = DefaultLayout("PRCXI9320")
layout.add_lab_resource(material_info)
MatrixLayout_1, dict_1 = layout.recommend_layout([
MatrixLayout_1, dict_1 = layout.recommend_layout(
[
("reagent_1", "96 细胞培养皿", 3),
("reagent_2", "12道储液槽", 1),
("reagent_3", "200μL Tip头", 7),
("reagent_4", "10μL加长 Tip头", 1),
])
]
)
print(dict_1)
MatrixLayout_2, dict_2 = layout.recommend_layout([
MatrixLayout_2, dict_2 = layout.recommend_layout(
[
("reagent_1", "96深孔板", 4),
("reagent_2", "12道储液槽", 1),
("reagent_3", "200μL Tip头", 1),
("reagent_4", "10μL加长 Tip头", 1),
])
]
)
# with open("prcxi_material.json", "r") as f:
# material_info = json.load(f)

View File

@@ -0,0 +1,20 @@
{
"nodes": [
{
"id": "opsky_ATR30007",
"name": "opsky_ATR30007",
"parent": null,
"type": "device",
"class": "opsky_ATR30007",
"position": {
"x": 620.6111111111111,
"y": 171,
"z": 0
},
"config": {},
"data": {},
"children": []
}
],
"links": []
}

View File

@@ -0,0 +1,71 @@
import socket
import time
import csv
from datetime import datetime
import threading
csv_lock = threading.Lock() # 防止多线程写CSV冲突
def scan_once(ip="192.168.1.50", port_in=2001, port_out=2002,
csv_file="scan_results.csv", timeout=5, retries=3):
"""
改进版扫码函数:
- 自动重试
- 全程超时保护
- 更安全的socket关闭
- 文件写入加锁
"""
def save_result(qrcode):
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with csv_lock:
with open(csv_file, mode="a", newline="") as f:
writer = csv.writer(f)
writer.writerow([timestamp, qrcode])
print(f"✅ 已保存结果: {timestamp}, {qrcode}")
result = None
for attempt in range(1, retries + 1):
print(f"\n🟡 扫码尝试 {attempt}/{retries} ...")
try:
# -------- Step 1: 触发拍照 --------
with socket.create_connection((ip, port_in), timeout=2) as client_in:
cmd = "start"
client_in.sendall(cmd.encode("ascii")) #把字符串转为byte字节流规则是ascii码
print(f"→ 已发送触发指令: {cmd}")
# -------- Step 2: 等待识别结果 --------
with socket.create_connection((ip, port_out), timeout=timeout) as client_out:
print(f" 已连接相机输出端口 {port_out},等待结果...")
# recv最多阻塞timeout秒
client_out.settimeout(timeout)
data = client_out.recv(2048).decode("ascii", errors="ignore").strip() #结果输出为ascii字符串遇到无法解析的字节则忽略
# .strip():去掉字符串头尾的空白字符(包括 \n, \r, 空格等),便于后续判断是否为空或写入 CSV。
if data:
print(f"📷 识别结果: {data}")
save_result(data) #调用 save_result(data) 把时间戳 + 识别字符串写入 CSV线程安全
result = data #把局部变量 result 设为 data用于函数返回值
break #如果读取成功跳出重试循环for attempt in ...),不再进行后续重试。
else:
print("⚠️ 相机返回空数据,重试中...")
except socket.timeout:
print("⏰ 超时未收到识别结果,重试中...")
except ConnectionRefusedError:
print("❌ 无法连接到扫码器端口,请检查设备是否在线。")
except OSError as e:
print(f"⚠️ 网络错误: {e}")
except Exception as e:
print(f"❌ 未知异常: {e}")
time.sleep(0.5) # 两次扫描之间稍作延时
# -------- Step 3: 返回最终结果 --------
if result:
print(f"✅ 扫码成功:{result}")
else:
print("❌ 多次尝试后仍未获取二维码结果")
return result

View File

@@ -0,0 +1,398 @@
# opsky_atr30007.py
import logging
import time as time_mod
import csv
from datetime import datetime
from typing import Optional, Dict, Any
# 兼容 pymodbus 在不同版本中的位置与 API
try:
from pymodbus.client import ModbusTcpClient
except Exception:
ModbusTcpClient = None
# 导入 run_raman_test假定与本文件同目录
# 如果你的项目是包结构且原先使用相对导入,请改回 `from .raman_module import run_raman_test`
try:
from .raman_module import run_raman_test
except Exception:
# 延迟导入失败不会阻止主流程(在 run 时会再尝试)
run_raman_test = None
logger = logging.getLogger("opsky")
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s", "%y-%m-%d %H:%M:%S")
ch.setFormatter(formatter)
logger.addHandler(ch)
class opsky_ATR30007:
"""
封装 UniLabOS 设备动作逻辑,兼容 pymodbus 2.x / 3.x。
放在独立文件中opsky_atr30007.py
"""
def __init__(
self,
plc_ip: str = "192.168.1.88",
plc_port: int = 502,
robot_ip: str = "192.168.1.200",
robot_port: int = 502,
scan_csv_file: str = "scan_results.csv",
):
self.plc_ip = plc_ip
self.plc_port = plc_port
self.robot_ip = robot_ip
self.robot_port = robot_port
self.scan_csv_file = scan_csv_file
# ----------------- 参数字符串转换 helpers -----------------
@staticmethod
def _str_to_int(s, default):
try:
return int(float(str(s).strip()))
except Exception:
return int(default)
@staticmethod
def _str_to_float(s, default):
try:
return float(str(s).strip())
except Exception:
return float(default)
@staticmethod
def _str_to_bool(s, default):
try:
v = str(s).strip().lower()
if v in ("true", "1", "yes", "y", "t"):
return True
if v in ("false", "0", "no", "n", "f"):
return False
return default
except Exception:
return default
# ----------------- Modbus / 安全读写 -----------------
@staticmethod
def _adapt_req_kwargs_for_read(func_name: str, args: tuple, kwargs: dict):
# 如果调用方传的是 (address, count) positional在新版接口可能是 address=..., count=...
if len(args) == 2 and func_name.startswith("read_"):
address, count = args
args = ()
kwargs.setdefault("address", address)
kwargs.setdefault("count", count)
return args, kwargs
@staticmethod
def _adapt_req_kwargs_for_write(func_name: str, args: tuple, kwargs: dict):
if len(args) == 2 and func_name.startswith("write_"):
address, value = args
args = ()
kwargs.setdefault("address", address)
kwargs.setdefault("value", value)
return args, kwargs
def ensure_connected(self, client, name, ip, port):
"""确保连接存在,失败则尝试重连并返回新的 client 或 None"""
if client is None:
return None
try:
# 不同 pymodbus 版本可能有不同方法检测 socket
is_open = False
try:
is_open = bool(client.is_socket_open())
except Exception:
# fallback: try to read nothing or attempt connection test
try:
# 轻试一次
is_open = client.connected if hasattr(client, "connected") else False
except Exception:
is_open = False
if not is_open:
logger.warning("%s 掉线,尝试重连...", name)
try:
client.close()
except Exception:
pass
time_mod.sleep(0.5)
if ModbusTcpClient:
new_client = ModbusTcpClient(ip, port=port)
try:
if new_client.connect():
logger.info("%s 重新连接成功 (%s:%s)", name, ip, port)
return new_client
except Exception:
pass
logger.warning("%s 重连失败", name)
time_mod.sleep(1)
return None
return client
except Exception as e:
logger.exception("%s 连接检查异常: %s", name, e)
return None
def safe_read(self, client, name, func, *args, retries=3, delay=0.3, **kwargs):
"""兼容 pymodbus 2.x/3.x 的读函数,返回 response 或 None"""
if client is None:
return None
for attempt in range(1, retries + 1):
try:
# adapt args/kwargs for different API styles
args, kwargs = self._adapt_req_kwargs_for_read(func.__name__, args, kwargs)
# unit->slave compatibility
if "unit" in kwargs:
kwargs["slave"] = kwargs.pop("unit")
res = func(*args, **kwargs)
# pymodbus Response 在不同版本表现不同,尽量检测错误
if res is None:
raise RuntimeError("返回 None")
if hasattr(res, "isError") and res.isError():
raise RuntimeError("Modbus 返回 isError()")
return res
except Exception as e:
logger.warning("%s 读异常 (尝试 %d/%d): %s", name, attempt, retries, e)
time_mod.sleep(delay)
logger.error("%s 连续读取失败 %d", name, retries)
return None
def safe_write(self, client, name, func, *args, retries=3, delay=0.3, **kwargs):
"""兼容 pymodbus 2.x/3.x 的写函数,返回 True/False"""
if client is None:
return False
for attempt in range(1, retries + 1):
try:
args, kwargs = self._adapt_req_kwargs_for_write(func.__name__, args, kwargs)
if "unit" in kwargs:
kwargs["slave"] = kwargs.pop("unit")
res = func(*args, **kwargs)
if res is None:
raise RuntimeError("返回 None")
if hasattr(res, "isError") and res.isError():
raise RuntimeError("Modbus 返回 isError()")
return True
except Exception as e:
logger.warning("%s 写异常 (尝试 %d/%d): %s", name, attempt, retries, e)
time_mod.sleep(delay)
logger.error("%s 连续写入失败 %d", name, retries)
return False
def wait_with_quit_check(self, robot, seconds, addr_quit=270):
"""等待指定时间,同时每 0.2s 检查 R270 是否为 1立即退出"""
if robot is None:
time_mod.sleep(seconds)
return False
checks = max(1, int(seconds / 0.2))
for _ in range(checks):
rr = self.safe_read(robot, "机器人", robot.read_holding_registers, address=addr_quit, count=1)
if rr and getattr(rr, "registers", [None])[0] == 1:
logger.info("检测到 R270=1立即退出等待")
return True
time_mod.sleep(0.2)
return False
# ----------------- 主流程 run_once -----------------
def run_once(
self,
integration_time: str = "5000",
laser_power: str = "200",
save_csv: str = "true",
save_plot: str = "true",
normalize: str = "true",
norm_max: str = "1.0",
**_: Any,
) -> Dict[str, Any]:
result: Dict[str, Any] = {"success": False, "event": "none", "details": {}}
integration_time_v = self._str_to_int(integration_time, 5000)
laser_power_v = self._str_to_int(laser_power, 200)
save_csv_v = self._str_to_bool(save_csv, True)
save_plot_v = self._str_to_bool(save_plot, True)
normalize_v = self._str_to_bool(normalize, True)
norm_max_v = None if norm_max in (None, "", "none", "null") else self._str_to_float(norm_max, 1.0)
if ModbusTcpClient is None:
result["details"]["error"] = "未安装 pymodbus无法执行连接"
logger.error(result["details"]["error"])
return result
# 建立连接
plc = ModbusTcpClient(self.plc_ip, port=self.plc_port)
robot = ModbusTcpClient(self.robot_ip, port=self.robot_port)
try:
if not plc.connect():
result["details"]["error"] = "无法连接 PLC"
logger.error(result["details"]["error"])
return result
if not robot.connect():
plc.close()
result["details"]["error"] = "无法连接 机器人"
logger.error(result["details"]["error"])
return result
logger.info("✅ PLC 与 机器人连接成功")
time_mod.sleep(0.2)
# 伺服使能 (coil 写示例)
if self.safe_write(plc, "PLC", plc.write_coil, 10, True):
logger.info("✅ 伺服使能成功 (M10=True)")
else:
logger.warning("⚠️ 伺服使能失败")
# 初始化 CSV 文件
try:
with open(self.scan_csv_file, "w", newline="", encoding="utf-8") as f:
csv.writer(f).writerow(["Bottle_No", "Scan_Result", "Time"])
except Exception as e:
logger.warning("⚠️ 初始化CSV失败: %s", e)
bottle_count = 0
logger.info("🟢 等待机器人触发信号... (R260=1扫码 / R256=1拉曼 / R270=1退出)")
# 主循环:仅响应事件(每次循环后短暂 sleep
while True:
plc = self.ensure_connected(plc, "PLC", self.plc_ip, self.plc_port) or plc
robot = self.ensure_connected(robot, "机器人", self.robot_ip, self.robot_port) or robot
# 检查退出寄存器
quit_signal = self.safe_read(robot, "机器人", robot.read_holding_registers, 270, 1)
if quit_signal and getattr(quit_signal, "registers", [None])[0] == 1:
logger.info("🟥 检测到 R270=1准备退出...")
result["event"] = "quit"
result["success"] = True
break
# 读取关键寄存器256..260
rr = self.safe_read(robot, "机器人", robot.read_holding_registers, 256, 5)
if not rr or not hasattr(rr, "registers"):
time_mod.sleep(0.3)
continue
r256, r257, r258, r259, r260 = (rr.registers + [0, 0, 0, 0, 0])[:5]
# ---------- 扫码逻辑 ----------
if r260 == 1:
bottle_count += 1
logger.info("📸 第 %d 瓶触发扫码 (R260=1)", bottle_count)
try:
# 调用外部扫码函数(用户实现)
from .dmqfengzhuang import scan_once as scan_once_local
scan_result = scan_once_local(ip="192.168.1.50", port_in=2001, port_out=2002)
if scan_result:
logger.info("✅ 扫码成功: %s", scan_result)
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open(self.scan_csv_file, "a", newline="", encoding="utf-8") as f:
csv.writer(f).writerow([bottle_count, scan_result, timestamp])
else:
logger.warning("⚠️ 扫码失败或无返回")
except Exception as e:
logger.exception("❌ 扫码异常: %s", e)
# 写 R260->0, R261->1
self.safe_write(robot, "机器人", robot.write_register, 260, 0)
time_mod.sleep(0.15)
self.safe_write(robot, "机器人", robot.write_register, 261, 1)
logger.info("➡️ 扫码完成 (R260→0, R261→1)")
result["event"] = "scan"
result["success"] = True
# ---------- 拉曼逻辑 ----------
if r256 == 1:
logger.info("⚙️ 检测到 R256=1放瓶完成")
# PLC 电机右转指令
self.safe_write(plc, "PLC", plc.write_register, 1199, 1)
self.safe_write(plc, "PLC", plc.write_register, 1200, 1)
logger.info("➡️ 电机右转中...")
if self.wait_with_quit_check(robot, 3):
result["event"] = "quit"
break
self.safe_write(plc, "PLC", plc.write_register, 1199, 0)
logger.info("✅ 电机右转完成")
# 调用拉曼测试(尽量捕获异常并记录)
logger.info("🧪 开始拉曼测试...")
try:
# 尝试使用模块导入好的 run_raman_test否则再动态导入
rr_func = run_raman_test
if rr_func is None:
from raman_module import run_raman_test as rr_func
success, file_prefix, df = rr_func(
integration_time=integration_time_v,
laser_power=laser_power_v,
save_csv=save_csv_v,
save_plot=save_plot_v,
normalize=normalize_v,
norm_max=norm_max_v,
)
if success:
logger.info("✅ 拉曼测试完成: %s", file_prefix)
result["event"] = "raman"
result["success"] = True
else:
logger.warning("⚠️ 拉曼测试失败")
except Exception as e:
logger.exception("❌ 拉曼模块异常: %s", e)
# 电机左转回位
self.safe_write(plc, "PLC", plc.write_register, address=1299, value=1)
self.safe_write(plc, "PLC", plc.write_register, address=1300, value=1)
logger.info("⬅️ 电机左转中...")
if self.wait_with_quit_check(robot, 3):
result["event"] = "quit"
break
self.safe_write(plc, "PLC", plc.write_register, address=1299, value=0)
logger.info("✅ 电机左转完成")
# 通知机器人拉曼完成 R257=1
self.safe_write(robot, "机器人", robot.write_register, address=257, value=1)
logger.info("✅ 已写入 R257=1拉曼完成")
# 延迟后清零 R256
logger.info("⏳ 延迟4秒后清零 R256")
if self.wait_with_quit_check(robot, 4):
result["event"] = "quit"
break
self.safe_write(robot, "机器人", robot.write_register, address=256, value=0)
logger.info("✅ 已清零 R256")
# 等待机器人清 R257
logger.info("等待 R257 清零中...")
while True:
rr2 = self.safe_read(robot, "机器人", robot.read_holding_registers, address=257, count=1)
if rr2 and getattr(rr2, "registers", [None])[0] == 0:
logger.info("✅ 检测到 R257=0准备下一循环")
break
if self.wait_with_quit_check(robot, 1):
result["event"] = "quit"
break
time_mod.sleep(0.2)
time_mod.sleep(0.25)
finally:
logger.info("🧹 开始清理...")
try:
self.safe_write(plc, "PLC", plc.write_coil, address=10, value=False)
except Exception:
pass
for addr in [256, 257, 260, 261, 270]:
try:
self.safe_write(robot, "机器人", robot.write_register, address=addr, value=0)
except Exception:
pass
try:
if plc:
plc.close()
except Exception:
pass
try:
if robot:
robot.close()
except Exception:
pass
logger.info("🔚 已关闭所有连接")
return result

View File

@@ -0,0 +1,180 @@
# raman_module.py
import os
import time as time_mod
import numpy as np
import pandas as pd
# clr / ATRWrapper 依赖:在真实环境中使用 Windows + .NET wrapper
# 本模块对缺少 clr 或 Wrapper 的情况提供“仿真”回退,方便离线/调试运行。
try:
import clr
has_clr = True
except Exception:
clr = None
has_clr = False
# 本函数返回 (success: bool, file_prefix: str|None, df: pandas.DataFrame|None)
def run_raman_test(integration_time=5000, laser_power=200,
save_csv=True, save_plot=True,
normalize=False, norm_max=None,
max_wavenum=1300):
"""
拉曼测试流程(更稳健的实现):
- 若能加载 ATRWrapper 则使用之
- 否则生成模拟光谱(方便调试)
返回 (success, file_prefix, df)
"""
timestamp = time_mod.strftime("%Y%m%d_%H%M%S")
file_prefix = f"raman_{timestamp}"
wrapper = None
used_real_device = False
try:
if has_clr:
try:
# 请根据你的 DLL 路径调整
dll_path = r"D:\Raman\Raman_RS\ATRWrapper\ATRWrapper.dll"
if os.path.exists(dll_path):
clr.AddReference(dll_path)
else:
# 试图直接 AddReference 名称(若已在 PATH
try:
clr.AddReference("ATRWrapper")
except Exception:
pass
from Optosky.Wrapper import ATRWrapper # May raise
wrapper = ATRWrapper()
used_real_device = True
except Exception as e:
# 无法加载真实 wrapper -> fallback
print("⚠️ 未能加载 ATRWrapper使用模拟数据。详细:", e)
wrapper = None
if wrapper is None:
# 生成模拟光谱(方便调试)
# 模拟波数轴 50..1300
WaveNum = np.linspace(50, max_wavenum, 1024)
# 合成几条高斯峰 + 噪声
def gauss(x, mu, sig, A):
return A * np.exp(-0.5 * ((x - mu) / sig) ** 2)
Spect_data = (gauss(WaveNum, 200, 8, 1000) +
gauss(WaveNum, 520, 12, 600) +
gauss(WaveNum, 810, 20, 400) +
50 * np.random.normal(scale=1.0, size=WaveNum.shape))
Spect_bLC = Spect_data - np.min(Spect_data) * 0.05 # 简单 baseline
Spect_smooth = np.convolve(Spect_bLC, np.ones(3) / 3, mode="same")
df = pd.DataFrame({
"WaveNum": WaveNum,
"Raw_Spect": Spect_data,
"BaseLineCorrected": Spect_bLC,
"Smooth_Spect": Spect_smooth
})
success = True
file_prefix = f"raman_sim_{timestamp}"
# 保存 CSV / 绘图 等同真实设备
else:
# 使用真实设备 API根据你提供的 wrapper 调用)
On_flag = wrapper.OpenDevice()
print("通讯连接状态:", On_flag)
if not On_flag:
wrapper.CloseDevice()
return False, None, None
wrapper.SetIntegrationTime(int(integration_time))
wrapper.SetLdPower(int(laser_power), 1)
# 可能的冷却设置(如果 wrapper 支持)
try:
wrapper.SetCool(-5)
except Exception:
pass
Spect = wrapper.AcquireSpectrum()
Spect_data = np.array(Spect.get_Data())
if not Spect.get_Success():
print("光谱采集失败")
try:
wrapper.CloseDevice()
except Exception:
pass
return False, None, None
WaveNum = np.array(wrapper.GetWaveNum())
Spect_bLC = np.array(wrapper.BaseLineCorrect(Spect_data))
Spect_smooth = np.array(wrapper.SmoothBoxcar(Spect_bLC, 3))
df = pd.DataFrame({
"WaveNum": WaveNum,
"Raw_Spect": Spect_data,
"BaseLineCorrected": Spect_bLC,
"Smooth_Spect": Spect_smooth
})
wrapper.CloseDevice()
success = True
# 如果需要限定波数范围
mask = df["WaveNum"] <= max_wavenum
df = df[mask].reset_index(drop=True)
# 可选归一化
if normalize:
arr = df["Smooth_Spect"].values
mn, mx = arr.min(), arr.max()
if mx == mn:
df["Smooth_Spect"] = 0.0
else:
scale = 1.0 if norm_max is None else float(norm_max)
df["Smooth_Spect"] = (arr - mn) / (mx - mn) * scale
# 同时处理其它列(可选)
arr_raw = df["Raw_Spect"].values
mn_r, mx_r = arr_raw.min(), arr_raw.max()
if mx_r == mn_r:
df["Raw_Spect"] = 0.0
else:
scale = 1.0 if norm_max is None else float(norm_max)
df["Raw_Spect"] = (arr_raw - mn_r) / (mx_r - mn_r) * scale
# 保存 CSV
if save_csv:
csv_filename = f"{file_prefix}.csv"
df.to_csv(csv_filename, index=False)
print("✅ CSV 文件已生成:", csv_filename)
# 绘图(使用 matplotlib注意不要启用 GUI 后台
if save_plot:
try:
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
plt.figure(figsize=(8, 5))
plt.plot(df["WaveNum"], df["Raw_Spect"], linestyle='-', alpha=0.6, label="原始")
plt.plot(df["WaveNum"], df["BaseLineCorrected"], linestyle='--', alpha=0.8, label="基线校正")
plt.plot(df["WaveNum"], df["Smooth_Spect"], linewidth=1.2, label="平滑")
plt.xlabel("WaveNum (cm^-1)")
plt.ylabel("Intensity (a.u.)")
plt.title(f"Raman {file_prefix}")
plt.grid(True)
plt.legend()
plt.tight_layout()
plot_filename = f"{file_prefix}.png"
plt.savefig(plot_filename, dpi=300, bbox_inches="tight")
plt.close()
# 小短暂等待以确保文件系统刷新
time_mod.sleep(0.2)
print("✅ 图像已生成:", plot_filename)
except Exception as e:
print("⚠️ 绘图失败:", e)
return success, file_prefix, df
except Exception as e:
print("拉曼测试异常:", e)
try:
if wrapper is not None:
try:
wrapper.CloseDevice()
except Exception:
pass
except Exception:
pass
return False, None, None

View File

@@ -0,0 +1,209 @@
import time
import csv
from datetime import datetime
from pymodbus.client import ModbusTcpClient
from dmqfengzhuang import scan_once
from raman_module import run_raman_test
# =================== 配置 ===================
PLC_IP = "192.168.1.88"
PLC_PORT = 502
ROBOT_IP = "192.168.1.200"
ROBOT_PORT = 502
SCAN_CSV_FILE = "scan_results.csv"
# =================== 通用函数 ===================
def ensure_connected(client, name, ip, port):
if not client.is_socket_open():
print(f"{name} 掉线,正在重连...")
client.close()
time.sleep(1)
new_client = ModbusTcpClient(ip, port=port)
if new_client.connect():
print(f"{name} 重新连接成功 ({ip}:{port})")
return new_client
else:
print(f"{name} 重连失败,稍后重试...")
time.sleep(3)
return None
return client
def safe_read(client, name, func, *args, retries=3, delay=0.3, **kwargs):
for _ in range(retries):
try:
res = func(*args, **kwargs)
if res and not (hasattr(res, "isError") and res.isError()):
return res
except Exception as e:
print(f"{name} 读异常: {e}")
time.sleep(delay)
print(f"{name} 连续读取失败 {retries}")
return None
def safe_write(client, name, func, *args, retries=3, delay=0.3, **kwargs):
for _ in range(retries):
try:
res = func(*args, **kwargs)
if res and not (hasattr(res, "isError") and res.isError()):
return True
except Exception as e:
print(f"{name} 写异常: {e}")
time.sleep(delay)
print(f"{name} 连续写入失败 {retries}")
return False
def wait_with_quit_check(robot, seconds, addr_quit=270):
for _ in range(int(seconds / 0.2)):
rr = safe_read(robot, "机器人", robot.read_holding_registers,
address=addr_quit, count=1)
if rr and rr.registers[0] == 1:
print("检测到 R270=1立即退出循环")
return True
time.sleep(0.2)
return False
# =================== 初始化 ===================
plc = ModbusTcpClient(PLC_IP, port=PLC_PORT)
robot = ModbusTcpClient(ROBOT_IP, port=ROBOT_PORT)
if not plc.connect():
print("无法连接 PLC")
exit()
if not robot.connect():
print("无法连接 机器人")
plc.close()
exit()
print("✅ PLC 与 机器人连接成功")
time.sleep(0.5)
# 伺服使能
if safe_write(plc, "PLC", plc.write_coil, address=10, value=True):
print("✅ 伺服使能成功 (M10=True)")
else:
print("⚠️ 伺服使能失败")
# 初始化扫码 CSV
with open(SCAN_CSV_FILE, "w", newline="", encoding="utf-8") as f:
csv.writer(f).writerow(["Bottle_No", "Scan_Result", "Time"])
bottle_count = 0
print("🟢 等待机器人触发信号... (R260=1扫码 / R256=1拉曼 / R270=1退出)")
# =================== 主监听循环 ===================
while True:
plc = ensure_connected(plc, "PLC", PLC_IP, PLC_PORT) or plc
robot = ensure_connected(robot, "机器人", ROBOT_IP, ROBOT_PORT) or robot
# 退出命令检测
quit_signal = safe_read(robot, "机器人", robot.read_holding_registers,
address=270, count=1)
if quit_signal and quit_signal.registers[0] == 1:
print("🟥 检测到 R270=1准备退出程序...")
break
# 读取关键寄存器
rr = safe_read(robot, "机器人", robot.read_holding_registers,
address=256, count=5)
if not rr:
time.sleep(0.3)
continue
r256, _, r258, r259, r260 = rr.registers[:5]
# ----------- 扫码部分 (R260=1) -----------
if r260 == 1:
bottle_count += 1
print(f"📸 第 {bottle_count} 瓶触发扫码 (R260=1)")
try:
result = scan_once(ip="192.168.1.50", port_in=2001, port_out=2002)
if result:
print(f"✅ 扫码成功: {result}")
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open(SCAN_CSV_FILE, "a", newline="", encoding="utf-8") as f:
csv.writer(f).writerow([bottle_count, result, timestamp])
else:
print("⚠️ 扫码失败或无返回")
except Exception as e:
print(f"❌ 扫码异常: {e}")
safe_write(robot, "机器人", robot.write_register, address=260, value=0)
time.sleep(0.2)
safe_write(robot, "机器人", robot.write_register, address=261, value=1)
print("➡️ 扫码完成 (R260→0, R261→1)")
# ----------- 拉曼 + 电机部分 (R256=1) -----------
if r256 == 1:
print("⚙️ 检测到 R256=1放瓶完成")
# 电机右转
safe_write(plc, "PLC", plc.write_register, address=1199, value=1)
safe_write(plc, "PLC", plc.write_register, address=1200, value=1)
print("➡️ 电机右转中...")
if wait_with_quit_check(robot, 3):
break
safe_write(plc, "PLC", plc.write_register, address=1199, value=0)
print("✅ 电机右转完成")
# 拉曼测试
print("🧪 开始拉曼测试...")
try:
success, file_prefix, df = run_raman_test(
integration_time=5000,
laser_power=200,
save_csv=True,
save_plot=True,
normalize=True,
norm_max=1.0
)
if success:
print(f"✅ 拉曼完成:{file_prefix}.csv / .png")
else:
print("⚠️ 拉曼失败")
except Exception as e:
print(f"❌ 拉曼测试异常: {e}")
# 电机左转
safe_write(plc, "PLC", plc.write_register, address=1299, value=1)
safe_write(plc, "PLC", plc.write_register, address=1300, value=1)
print("⬅️ 电机左转中...")
if wait_with_quit_check(robot, 3):
break
safe_write(plc, "PLC", plc.write_register, address=1299, value=0)
print("✅ 电机左转完成")
# 写入拉曼完成信号
safe_write(robot, "机器人", robot.write_register, address=257, value=1)
print("✅ 已写入 R257=1拉曼完成")
# 延迟清零 R256
print("⏳ 延迟4秒后清零 R256")
if wait_with_quit_check(robot, 4):
break
safe_write(robot, "机器人", robot.write_register, address=256, value=0)
print("✅ 已清零 R256")
# 等待机器人清零 R257
print("等待 R257 清零中...")
while True:
rr2 = safe_read(robot, "机器人", robot.read_holding_registers, address=257, count=1)
if rr2 and rr2.registers[0] == 0:
print("✅ 检测到 R257=0准备下一循环")
break
if wait_with_quit_check(robot, 1):
break
time.sleep(0.2)
time.sleep(0.2)
# =================== 程序退出清理 ===================
print("🧹 开始清理...")
safe_write(plc, "PLC", plc.write_coil, address=10, value=False)
for addr in [256, 257, 260, 261, 270]:
safe_write(robot, "机器人", robot.write_register, address=addr, value=0)
plc.close()
robot.close()
print("✅ 程序已退出,设备全部复位。")

View File

@@ -8,6 +8,8 @@ import serial.tools.list_ports
from serial import Serial
from serial.serialutil import SerialException
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
class RunzeSyringePumpMode(Enum):
Normal = 0
@@ -77,6 +79,8 @@ class RunzeSyringePumpInfo:
class RunzeSyringePumpAsync:
_ros_node: BaseROS2DeviceNode
def __init__(self, port: str, address: str = "1", volume: float = 25000, mode: RunzeSyringePumpMode = None):
self.port = port
self.address = address
@@ -102,6 +106,9 @@ class RunzeSyringePumpAsync:
self._run_future: Optional[Future[Any]] = None
self._run_lock = Lock()
def post_init(self, ros_node: BaseROS2DeviceNode):
self._ros_node = ros_node
def _adjust_total_steps(self):
self.total_steps = 6000 if self.mode == RunzeSyringePumpMode.Normal else 48000
self.total_steps_vel = 48000 if self.mode == RunzeSyringePumpMode.AccuratePosVel else 6000
@@ -182,7 +189,7 @@ class RunzeSyringePumpAsync:
try:
await self._query(command)
while True:
await asyncio.sleep(0.5) # Wait for 0.5 seconds before polling again
await self._ros_node.sleep(0.5) # Wait for 0.5 seconds before polling again
status = await self.query_device_status()
if status == '`':
@@ -364,7 +371,7 @@ class RunzeSyringePumpAsync:
if self._read_task:
raise RunzeSyringePumpConnectionError
self._read_task = asyncio.create_task(self._read_loop())
self._read_task = self._ros_node.create_task(self._read_loop())
try:
await self.query_device_status()

View File

@@ -3,10 +3,14 @@ import logging
import time as time_module
from typing import Dict, Any, Optional
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
class VirtualCentrifuge:
"""Virtual centrifuge device - 简化版,只保留核心功能"""
_ros_node: BaseROS2DeviceNode
def __init__(self, device_id: Optional[str] = None, config: Optional[Dict[str, Any]] = None, **kwargs):
# 处理可能的不同调用方式
if device_id is None and "id" in kwargs:
@@ -33,6 +37,9 @@ class VirtualCentrifuge:
if key not in skip_keys and not hasattr(self, key):
setattr(self, key, value)
def post_init(self, ros_node: BaseROS2DeviceNode):
self._ros_node = ros_node
async def initialize(self) -> bool:
"""Initialize virtual centrifuge"""
self.logger.info(f"Initializing virtual centrifuge {self.device_id}")
@@ -132,7 +139,7 @@ class VirtualCentrifuge:
break
# 每秒更新一次
await asyncio.sleep(1.0)
await self._ros_node.sleep(1.0)
# 离心完成
self.data.update({

View File

@@ -2,9 +2,13 @@ import asyncio
import logging
from typing import Dict, Any, Optional
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
class VirtualColumn:
"""Virtual column device for RunColumn protocol 🏛️"""
_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:
@@ -28,6 +32,9 @@ class VirtualColumn:
print(f"🏛️ === 虚拟色谱柱 {self.device_id} 已创建 === ✨")
print(f"📏 柱参数: 流速={self._max_flow_rate}mL/min | 长度={self._column_length}cm | 直径={self._column_diameter}cm 🔬")
def post_init(self, ros_node: BaseROS2DeviceNode):
self._ros_node = ros_node
async def initialize(self) -> bool:
"""Initialize virtual column 🚀"""
self.logger.info(f"🔧 初始化虚拟色谱柱 {self.device_id}")
@@ -101,7 +108,7 @@ class VirtualColumn:
step_time = separation_time / steps
for i in range(steps):
await asyncio.sleep(step_time)
await self._ros_node.sleep(step_time)
progress = (i + 1) / steps * 100
volume_processed = (i + 1) * 5.0 # 假设每步处理5mL

View File

@@ -4,16 +4,19 @@ import time as time_module
from typing import Dict, Any, Optional
from unilabos.compile.utils.vessel_parser import get_vessel
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
class VirtualFilter:
"""Virtual filter device - 完全按照 Filter.action 规范 🌊"""
_ros_node: BaseROS2DeviceNode
def __init__(self, device_id: Optional[str] = None, config: Optional[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')
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_filter"
self.config = config or {}
@@ -21,29 +24,34 @@ class VirtualFilter:
self.data = {}
# 从config或kwargs中获取配置参数
self.port = self.config.get('port') or kwargs.get('port', 'VIRTUAL')
self._max_temp = self.config.get('max_temp') or kwargs.get('max_temp', 100.0)
self._max_stir_speed = self.config.get('max_stir_speed') or kwargs.get('max_stir_speed', 1000.0)
self._max_volume = self.config.get('max_volume') or kwargs.get('max_volume', 500.0)
self.port = self.config.get("port") or kwargs.get("port", "VIRTUAL")
self._max_temp = self.config.get("max_temp") or kwargs.get("max_temp", 100.0)
self._max_stir_speed = self.config.get("max_stir_speed") or kwargs.get("max_stir_speed", 1000.0)
self._max_volume = self.config.get("max_volume") or kwargs.get("max_volume", 500.0)
# 处理其他kwargs参数
skip_keys = {'port', 'max_temp', 'max_stir_speed', 'max_volume'}
skip_keys = {"port", "max_temp", "max_stir_speed", "max_volume"}
for key, value in kwargs.items():
if key not in skip_keys and not hasattr(self, key):
setattr(self, key, value)
def post_init(self, ros_node: BaseROS2DeviceNode):
self._ros_node = ros_node
async def initialize(self) -> bool:
"""Initialize virtual filter 🚀"""
self.logger.info(f"🔧 初始化虚拟过滤器 {self.device_id}")
# 按照 Filter.action 的 feedback 字段初始化
self.data.update({
self.data.update(
{
"status": "Idle",
"progress": 0.0, # Filter.action feedback
"current_temp": 25.0, # Filter.action feedback
"filtered_volume": 0.0, # Filter.action feedback
"message": "Ready for filtration"
})
"message": "Ready for filtration",
}
)
self.logger.info(f"✅ 过滤器 {self.device_id} 初始化完成 🌊")
return True
@@ -52,9 +60,7 @@ class VirtualFilter:
"""Cleanup virtual filter 🧹"""
self.logger.info(f"🧹 清理虚拟过滤器 {self.device_id} 🔚")
self.data.update({
"status": "Offline"
})
self.data.update({"status": "Offline"})
self.logger.info(f"✅ 过滤器 {self.device_id} 清理完成 💤")
return True
@@ -67,7 +73,7 @@ class VirtualFilter:
stir_speed: float = 300.0,
temp: float = 25.0,
continue_heatchill: bool = False,
volume: float = 0.0
volume: float = 0.0,
) -> bool:
"""Execute filter action - 完全按照 Filter.action 参数 🌊"""
vessel_id, _ = get_vessel(vessel)
@@ -92,41 +98,34 @@ class VirtualFilter:
if temp > self._max_temp or temp < 4.0:
error_msg = f"🌡️ 温度 {temp}°C 超出范围 (4-{self._max_temp}°C) ⚠️"
self.logger.error(f"{error_msg}")
self.data.update({
"status": f"Error: 温度超出范围 ⚠️",
"message": error_msg
})
self.data.update({"status": f"Error: 温度超出范围 ⚠️", "message": error_msg})
return False
if stir and stir_speed > self._max_stir_speed:
error_msg = f"🌪️ 搅拌速度 {stir_speed} RPM 超出范围 (0-{self._max_stir_speed} RPM) ⚠️"
self.logger.error(f"{error_msg}")
self.data.update({
"status": f"Error: 搅拌速度超出范围 ⚠️",
"message": error_msg
})
self.data.update({"status": f"Error: 搅拌速度超出范围 ⚠️", "message": error_msg})
return False
if volume > self._max_volume:
error_msg = f"💧 过滤体积 {volume} mL 超出范围 (0-{self._max_volume} mL) ⚠️"
self.logger.error(f"{error_msg}")
self.data.update({
"status": f"Error",
"message": error_msg
})
self.data.update({"status": f"Error", "message": error_msg})
return False
# 开始过滤
filter_volume = volume if volume > 0 else 50.0
self.logger.info(f"🚀 开始过滤 {filter_volume}mL 液体 💧")
self.data.update({
self.data.update(
{
"status": f"Running",
"current_temp": temp,
"filtered_volume": 0.0,
"progress": 0.0,
"message": f"🚀 Starting filtration: {vessel_id}{filtrate_vessel_id}"
})
"message": f"🚀 Starting filtration: {vessel_id}{filtrate_vessel_id}",
}
)
try:
# 过滤过程 - 实时更新进度
@@ -157,13 +156,15 @@ class VirtualFilter:
status_msg += f" | 🌪️ 搅拌: {stir_speed} RPM"
status_msg += f" | 🌡️ {temp}°C | 📊 {progress:.1f}% | 💧 已过滤: {current_filtered:.1f}mL"
self.data.update({
self.data.update(
{
"progress": progress, # Filter.action feedback
"current_temp": temp, # Filter.action feedback
"filtered_volume": current_filtered, # Filter.action feedback
"status": "Running",
"message": f"🌊 Filtering: {progress:.1f}% complete, {current_filtered:.1f}mL filtered"
})
"message": f"🌊 Filtering: {progress:.1f}% complete, {current_filtered:.1f}mL filtered",
}
)
# 进度日志每25%打印一次)
if progress >= 25 and progress % 25 < 1:
@@ -172,7 +173,7 @@ class VirtualFilter:
if remaining <= 0:
break
await asyncio.sleep(1.0)
await self._ros_node.sleep(1.0)
# 过滤完成
final_temp = temp if continue_heatchill else 25.0
@@ -181,13 +182,15 @@ class VirtualFilter:
final_status += " | 🔥 继续加热搅拌"
self.logger.info(f"🔥 继续保持加热搅拌状态 🌪️")
self.data.update({
self.data.update(
{
"status": final_status,
"progress": 100.0, # Filter.action feedback
"current_temp": final_temp, # Filter.action feedback
"filtered_volume": filter_volume, # Filter.action feedback
"message": f"✅ Filtration completed: {filter_volume}mL filtered from {vessel_id}"
})
"message": f"✅ Filtration completed: {filter_volume}mL filtered from {vessel_id}",
}
)
self.logger.info(f"🎉 过滤完成! 💧 {filter_volume}mL 从 {vessel_id} 过滤到 {filtrate_vessel_id}")
self.logger.info(f"📊 最终状态: 温度 {final_temp}°C | 进度 100% | 体积 {filter_volume}mL 🏁")
@@ -196,10 +199,7 @@ class VirtualFilter:
except Exception as e:
error_msg = f"过滤过程中发生错误: {str(e)} 💥"
self.logger.error(f"{error_msg}")
self.data.update({
"status": f"Error",
"message": f"❌ Filtration failed: {str(e)}"
})
self.data.update({"status": f"Error", "message": f"❌ Filtration failed: {str(e)}"})
return False
# === 核心状态属性 - 按照 Filter.action feedback 字段 ===

View File

@@ -3,9 +3,13 @@ import logging
import time as time_module # 重命名time模块避免与参数冲突
from typing import Dict, Any
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
class VirtualHeatChill:
"""Virtual heat chill device for HeatChillProtocol testing 🌡️"""
_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:
@@ -35,6 +39,9 @@ class VirtualHeatChill:
print(f"🌡️ === 虚拟温控设备 {self.device_id} 已创建 === ✨")
print(f"🔥 温度范围: {self._min_temp}°C ~ {self._max_temp}°C | 🌪️ 最大搅拌: {self._max_stir_speed} RPM")
def post_init(self, ros_node: BaseROS2DeviceNode):
self._ros_node = ros_node
async def initialize(self) -> bool:
"""Initialize virtual heat chill 🚀"""
self.logger.info(f"🔧 初始化虚拟温控设备 {self.device_id}")
@@ -177,7 +184,7 @@ class VirtualHeatChill:
break
# 等待1秒后再次检查
await asyncio.sleep(1.0)
await self._ros_node.sleep(1.0)
# 操作完成
final_stir_info = f" | 🌪️ 搅拌: {stir_speed} RPM" if stir else ""

View File

@@ -3,13 +3,19 @@ import logging
import time as time_module
from typing import Dict, Any, Optional
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
def debug_print(message):
"""调试输出 🔍"""
print(f"🌪️ [ROTAVAP] {message}", flush=True)
class VirtualRotavap:
"""Virtual rotary evaporator device - 简化版,只保留核心功能 🌪️"""
_ros_node: BaseROS2DeviceNode
def __init__(self, device_id: Optional[str] = None, config: Optional[Dict[str, Any]] = None, **kwargs):
# 处理可能的不同调用方式
if device_id is None and "id" in kwargs:
@@ -38,12 +44,16 @@ class VirtualRotavap:
print(f"🌪️ === 虚拟旋转蒸发仪 {self.device_id} 已创建 === ✨")
print(f"🔥 温度范围: 10°C ~ {self._max_temp}°C | 🌀 转速范围: 10 ~ {self._max_rotation_speed} RPM")
def post_init(self, ros_node: BaseROS2DeviceNode):
self._ros_node = ros_node
async def initialize(self) -> bool:
"""Initialize virtual rotary evaporator 🚀"""
self.logger.info(f"🔧 初始化虚拟旋转蒸发仪 {self.device_id}")
# 只保留核心状态
self.data.update({
self.data.update(
{
"status": "🏠 待机中",
"rotavap_state": "Ready", # Ready, Evaporating, Completed, Error
"current_temp": 25.0,
@@ -53,25 +63,30 @@ class VirtualRotavap:
"evaporated_volume": 0.0,
"progress": 0.0,
"remaining_time": 0.0,
"message": "🌪️ Ready for evaporation"
})
"message": "🌪️ Ready for evaporation",
}
)
self.logger.info(f"✅ 旋转蒸发仪 {self.device_id} 初始化完成 🌪️")
self.logger.info(f"📊 设备规格: 温度范围 10°C ~ {self._max_temp}°C | 转速范围 10 ~ {self._max_rotation_speed} RPM")
self.logger.info(
f"📊 设备规格: 温度范围 10°C ~ {self._max_temp}°C | 转速范围 10 ~ {self._max_rotation_speed} RPM"
)
return True
async def cleanup(self) -> bool:
"""Cleanup virtual rotary evaporator 🧹"""
self.logger.info(f"🧹 清理虚拟旋转蒸发仪 {self.device_id} 🔚")
self.data.update({
self.data.update(
{
"status": "💤 离线",
"rotavap_state": "Offline",
"current_temp": 25.0,
"rotation_speed": 0.0,
"vacuum_pressure": 1.0,
"message": "💤 System offline"
})
"message": "💤 System offline",
}
)
self.logger.info(f"✅ 旋转蒸发仪 {self.device_id} 清理完成 💤")
return True
@@ -84,7 +99,7 @@ class VirtualRotavap:
time: float = 180.0,
stir_speed: float = 100.0,
solvent: str = "",
**kwargs
**kwargs,
) -> bool:
"""Execute evaporate action - 简化版 🌪️"""
@@ -114,11 +129,11 @@ class VirtualRotavap:
self.logger.info(f"🧪 识别到溶剂: {solvent}")
# 根据溶剂调整参数
solvent_lower = solvent.lower()
if any(s in solvent_lower for s in ['water', 'aqueous']):
if any(s in solvent_lower for s in ["water", "aqueous"]):
temp = max(temp, 80.0)
pressure = max(pressure, 0.2)
self.logger.info(f"💧 水系溶剂:调整参数 → 温度 {temp}°C, 压力 {pressure} bar")
elif any(s in solvent_lower for s in ['ethanol', 'methanol', 'acetone']):
elif any(s in solvent_lower for s in ["ethanol", "methanol", "acetone"]):
temp = min(temp, 50.0)
pressure = min(pressure, 0.05)
self.logger.info(f"⚡ 易挥发溶剂:调整参数 → 温度 {temp}°C, 压力 {pressure} bar")
@@ -136,46 +151,53 @@ class VirtualRotavap:
if temp > self._max_temp or temp < 10.0:
error_msg = f"🌡️ 温度 {temp}°C 超出范围 (10-{self._max_temp}°C) ⚠️"
self.logger.error(f"{error_msg}")
self.data.update({
self.data.update(
{
"status": f"❌ 错误: 温度超出范围",
"rotavap_state": "Error",
"current_temp": 25.0,
"progress": 0.0,
"evaporated_volume": 0.0,
"message": error_msg
})
"message": error_msg,
}
)
return False
if stir_speed > self._max_rotation_speed or stir_speed < 10.0:
error_msg = f"🌀 旋转速度 {stir_speed} RPM 超出范围 (10-{self._max_rotation_speed} RPM) ⚠️"
self.logger.error(f"{error_msg}")
self.data.update({
self.data.update(
{
"status": f"❌ 错误: 转速超出范围",
"rotavap_state": "Error",
"current_temp": 25.0,
"progress": 0.0,
"evaporated_volume": 0.0,
"message": error_msg
})
"message": error_msg,
}
)
return False
if pressure < 0.01 or pressure > 1.0:
error_msg = f"💨 真空度 {pressure} bar 超出范围 (0.01-1.0 bar) ⚠️"
self.logger.error(f"{error_msg}")
self.data.update({
self.data.update(
{
"status": f"❌ 错误: 压力超出范围",
"rotavap_state": "Error",
"current_temp": 25.0,
"progress": 0.0,
"evaporated_volume": 0.0,
"message": error_msg
})
"message": error_msg,
}
)
return False
# 开始蒸发 - 🔧 现在time已经确保是float类型
self.logger.info(f"🚀 启动蒸发程序! 预计用时 {time/60:.1f}分钟 ⏱️")
self.data.update({
self.data.update(
{
"status": f"🌪️ 蒸发中: {actual_vessel}",
"rotavap_state": "Evaporating",
"current_temp": temp,
@@ -185,8 +207,9 @@ class VirtualRotavap:
"remaining_time": time,
"progress": 0.0,
"evaporated_volume": 0.0,
"message": f"🌪️ Evaporating {actual_vessel} at {temp}°C, {pressure} bar, {stir_speed} RPM"
})
"message": f"🌪️ Evaporating {actual_vessel} at {temp}°C, {pressure} bar, {stir_speed} RPM",
}
)
try:
# 蒸发过程 - 实时更新进度
@@ -201,9 +224,9 @@ class VirtualRotavap:
progress = min(100.0, (elapsed / total_time) * 100)
# 模拟蒸发体积 - 根据溶剂类型调整
if solvent and any(s in solvent.lower() for s in ['water', 'aqueous']):
if solvent and any(s in solvent.lower() for s in ["water", "aqueous"]):
evaporated_vol = progress * 0.6 # 水系溶剂蒸发慢
elif solvent and any(s in solvent.lower() for s in ['ethanol', 'methanol', 'acetone']):
elif solvent and any(s in solvent.lower() for s in ["ethanol", "methanol", "acetone"]):
evaporated_vol = progress * 1.0 # 易挥发溶剂蒸发快
else:
evaporated_vol = progress * 0.8 # 默认蒸发量
@@ -211,18 +234,22 @@ class VirtualRotavap:
# 🔧 更新状态 - 确保包含所有必需字段
status_msg = f"🌪️ 蒸发中: {actual_vessel} | 🌡️ {temp}°C | 💨 {pressure} bar | 🌀 {stir_speed} RPM | 📊 {progress:.1f}% | ⏰ 剩余: {remaining:.0f}s"
self.data.update({
self.data.update(
{
"remaining_time": remaining,
"progress": progress,
"evaporated_volume": evaporated_vol,
"current_temp": temp,
"status": status_msg,
"message": f"🌪️ Evaporating: {progress:.1f}% complete, 💧 {evaporated_vol:.1f}mL evaporated, ⏰ {remaining:.0f}s remaining"
})
"message": f"🌪️ Evaporating: {progress:.1f}% complete, 💧 {evaporated_vol:.1f}mL evaporated, ⏰ {remaining:.0f}s remaining",
}
)
# 进度日志每25%打印一次)
if progress >= 25 and int(progress) % 25 == 0 and int(progress) != last_logged_progress:
self.logger.info(f"📊 蒸发进度: {progress:.0f}% | 💧 已蒸发: {evaporated_vol:.1f}mL | ⏰ 剩余: {remaining:.0f}s ✨")
self.logger.info(
f"📊 蒸发进度: {progress:.0f}% | 💧 已蒸发: {evaporated_vol:.1f}mL | ⏰ 剩余: {remaining:.0f}s ✨"
)
last_logged_progress = int(progress)
# 时间到了,退出循环
@@ -230,17 +257,18 @@ class VirtualRotavap:
break
# 每秒更新一次
await asyncio.sleep(1.0)
await self._ros_node.sleep(1.0)
# 蒸发完成
if solvent and any(s in solvent.lower() for s in ['water', 'aqueous']):
if solvent and any(s in solvent.lower() for s in ["water", "aqueous"]):
final_evaporated = 60.0 # 水系溶剂
elif solvent and any(s in solvent.lower() for s in ['ethanol', 'methanol', 'acetone']):
elif solvent and any(s in solvent.lower() for s in ["ethanol", "methanol", "acetone"]):
final_evaporated = 100.0 # 易挥发溶剂
else:
final_evaporated = 80.0 # 默认
self.data.update({
self.data.update(
{
"status": f"✅ 蒸发完成: {actual_vessel} | 💧 蒸发量: {final_evaporated:.1f}mL",
"rotavap_state": "Completed",
"evaporated_volume": final_evaporated,
@@ -249,8 +277,9 @@ class VirtualRotavap:
"remaining_time": 0.0,
"rotation_speed": 0.0,
"vacuum_pressure": 1.0,
"message": f"✅ Evaporation completed: {final_evaporated}mL evaporated from {actual_vessel}"
})
"message": f"✅ Evaporation completed: {final_evaporated}mL evaporated from {actual_vessel}",
}
)
self.logger.info(f"🎉 蒸发操作完成! ✨")
self.logger.info(f"📊 蒸发结果:")
@@ -270,7 +299,8 @@ class VirtualRotavap:
error_msg = f"蒸发过程中发生错误: {str(e)} 💥"
self.logger.error(f"{error_msg}")
self.data.update({
self.data.update(
{
"status": f"❌ 蒸发错误: {str(e)}",
"rotavap_state": "Error",
"current_temp": 25.0,
@@ -278,8 +308,9 @@ class VirtualRotavap:
"evaporated_volume": 0.0,
"rotation_speed": 0.0,
"vacuum_pressure": 1.0,
"message": f"❌ Evaporation failed: {str(e)}"
})
"message": f"❌ Evaporation failed: {str(e)}",
}
)
return False
# === 核心状态属性 ===

View File

@@ -2,10 +2,14 @@ import asyncio
import logging
from typing import Dict, Any, Optional
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
class VirtualSeparator:
"""Virtual separator device for SeparateProtocol testing"""
_ros_node: BaseROS2DeviceNode
def __init__(self, device_id: Optional[str] = None, config: Optional[Dict[str, Any]] = None, **kwargs):
# 处理可能的不同调用方式
if device_id is None and "id" in kwargs:
@@ -36,6 +40,9 @@ class VirtualSeparator:
if key not in skip_keys and not hasattr(self, key):
setattr(self, key, value)
def post_init(self, ros_node: BaseROS2DeviceNode):
self._ros_node = ros_node
async def initialize(self) -> bool:
"""Initialize virtual separator"""
print(f"=== VirtualSeparator {self.device_id} initialize() called! ===")
@@ -119,14 +126,14 @@ class VirtualSeparator:
for repeat in range(repeats):
# 搅拌阶段
for progress in range(0, 51, 10):
await asyncio.sleep(simulation_time / (repeats * 10))
await self._ros_node.sleep(simulation_time / (repeats * 10))
overall_progress = ((repeat * 100) + (progress * 0.5)) / repeats
self.data["progress"] = overall_progress
self.data["message"] = f"{repeat+1}次分离 - 搅拌中 ({progress}%)"
# 静置分相阶段
for progress in range(50, 101, 10):
await asyncio.sleep(simulation_time / (repeats * 10))
await self._ros_node.sleep(simulation_time / (repeats * 10))
overall_progress = ((repeat * 100) + (progress * 0.5)) / repeats
self.data["progress"] = overall_progress
self.data["message"] = f"{repeat+1}次分离 - 静置分相中 ({progress}%)"

View File

@@ -2,11 +2,16 @@ import time
import asyncio
from typing import Union
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
class VirtualSolenoidValve:
"""
虚拟电磁阀门 - 简单的开关型阀门,只有开启和关闭两个状态
"""
_ros_node: BaseROS2DeviceNode
def __init__(self, device_id: str = None, config: dict = None, **kwargs):
# 从配置中获取参数,提供默认值
if config is None:
@@ -22,6 +27,9 @@ class VirtualSolenoidValve:
self._valve_state = "Closed" # "Open" or "Closed"
self._is_open = False
def post_init(self, ros_node: BaseROS2DeviceNode):
self._ros_node = ros_node
async def initialize(self) -> bool:
"""初始化设备"""
self._status = "Idle"
@@ -63,7 +71,7 @@ class VirtualSolenoidValve:
self._status = "Busy"
# 模拟阀门响应时间
await asyncio.sleep(self.response_time)
await self._ros_node.sleep(self.response_time)
# 处理不同的命令格式
if isinstance(command, str):

View File

@@ -3,6 +3,8 @@ import logging
import re
from typing import Dict, Any, Optional
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
class VirtualSolidDispenser:
"""
虚拟固体粉末加样器 - 用于处理 Add Protocol 中的固体试剂添加 ⚗️
@@ -13,6 +15,8 @@ class VirtualSolidDispenser:
- 简单反馈:成功/失败 + 消息 📊
"""
_ros_node: BaseROS2DeviceNode
def __init__(self, device_id: str = None, config: Dict[str, Any] = None, **kwargs):
self.device_id = device_id or "virtual_solid_dispenser"
self.config = config or {}
@@ -32,6 +36,9 @@ class VirtualSolidDispenser:
print(f"⚗️ === 虚拟固体分配器 {self.device_id} 创建成功! === ✨")
print(f"📊 设备规格: 最大容量 {self.max_capacity}g | 精度 {self.precision}g 🎯")
def post_init(self, ros_node: BaseROS2DeviceNode):
self._ros_node = ros_node
async def initialize(self) -> bool:
"""初始化固体加样器 🚀"""
self.logger.info(f"🔧 初始化固体分配器 {self.device_id}")
@@ -263,7 +270,7 @@ class VirtualSolidDispenser:
for i in range(steps):
progress = (i + 1) / steps * 100
await asyncio.sleep(step_time)
await self._ros_node.sleep(step_time)
if i % 2 == 0: # 每隔一步显示进度
self.logger.debug(f"📊 加样进度: {progress:.0f}% | {amount_emoji} 正在分配 {reagent}...")

View File

@@ -3,9 +3,13 @@ import logging
import time as time_module
from typing import Dict, Any
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
class VirtualStirrer:
"""Virtual stirrer device for StirProtocol testing - 功能完整版 🌪️"""
_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:
@@ -34,6 +38,9 @@ class VirtualStirrer:
print(f"🌪️ === 虚拟搅拌器 {self.device_id} 已创建 === ✨")
print(f"🔧 速度范围: {self._min_speed} ~ {self._max_speed} RPM | 📱 端口: {self.port}")
def post_init(self, ros_node: BaseROS2DeviceNode):
self._ros_node = ros_node
async def initialize(self) -> bool:
"""Initialize virtual stirrer 🚀"""
self.logger.info(f"🔧 初始化虚拟搅拌器 {self.device_id}")
@@ -134,7 +141,7 @@ class VirtualStirrer:
if remaining <= 0:
break
await asyncio.sleep(1.0)
await self._ros_node.sleep(1.0)
self.logger.info(f"✅ 搅拌阶段完成! 🌪️ {stir_speed} RPM × {stir_time}s")
@@ -176,7 +183,7 @@ class VirtualStirrer:
if remaining <= 0:
break
await asyncio.sleep(1.0)
await self._ros_node.sleep(1.0)
self.logger.info(f"✅ 沉降阶段完成! 🛑 静置 {settling_time}s")

View File

@@ -4,6 +4,8 @@ from enum import Enum
from typing import Union, Optional
import logging
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
class VirtualPumpMode(Enum):
Normal = 0
@@ -14,6 +16,8 @@ class VirtualPumpMode(Enum):
class VirtualTransferPump:
"""虚拟转移泵类 - 模拟泵的基本功能,无需实际硬件 🚰"""
_ros_node: BaseROS2DeviceNode
def __init__(self, device_id: str = None, config: dict = None, **kwargs):
"""
初始化虚拟转移泵
@@ -53,6 +57,9 @@ class VirtualTransferPump:
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}")
def post_init(self, ros_node: BaseROS2DeviceNode):
self._ros_node = ros_node
async def initialize(self) -> bool:
"""初始化虚拟泵 🚀"""
self.logger.info(f"🔧 初始化虚拟转移泵 {self.device_id}")
@@ -104,7 +111,7 @@ class VirtualTransferPump:
async def _simulate_operation(self, duration: float):
"""模拟操作延时 ⏱️"""
self._status = "Busy"
await asyncio.sleep(duration)
await self._ros_node.sleep(duration)
self._status = "Idle"
def _calculate_duration(self, volume: float, velocity: float = None) -> float:
@@ -223,7 +230,7 @@ class VirtualTransferPump:
# 等待一小步时间
if i < steps and step_duration > 0:
await asyncio.sleep(step_duration)
await self._ros_node.sleep(step_duration)
else:
# 移动距离很小,直接完成
self._position = target_position
@@ -341,7 +348,7 @@ class VirtualTransferPump:
# 短暂停顿
self.logger.debug("⏸️ 短暂停顿...")
await asyncio.sleep(0.1)
await self._ros_node.sleep(0.1)
# 排液
await self.dispense(volume, dispense_velocity)

View File

@@ -233,7 +233,7 @@ class BioyondV1RPC(BaseRequest):
return response.get("data", {})
def material_outbound(self, material_id: str, location_name: str, quantity: int) -> dict:
"""指定库位出库物料"""
"""指定库位出库物料(通过库位名称)"""
location_id = LOCATION_MAPPING.get(location_name, location_name)
params = {
@@ -251,7 +251,36 @@ class BioyondV1RPC(BaseRequest):
})
if not response or response['code'] != 1:
return {}
return None
return response
def material_outbound_by_id(self, material_id: str, location_id: str, quantity: int) -> dict:
"""指定库位出库物料直接使用location_id
Args:
material_id: 物料ID
location_id: 库位ID不是库位名称是UUID
quantity: 数量
Returns:
dict: API响应失败返回None
"""
params = {
"materialId": material_id,
"locationId": location_id,
"quantity": quantity
}
response = self.post(
url=f'{self.host}/api/lims/storage/outbound',
params={
"apiKey": self.api_key,
"requestTime": self.get_current_time_iso8601(),
"data": params
})
if not response or response['code'] != 1:
return None
return response
# ==================== 工作流查询相关接口 ====================

View File

@@ -232,7 +232,7 @@ class BioyondReactionStation(BioyondWorkstation):
temperature: 温度设定(°C)
"""
# 处理 volume 参数:优先使用直接传入的 volume,否则从 solvents 中提取
if volume is None and solvents is not None:
if not volume and solvents is not None:
# 参数类型转换:如果是字符串则解析为字典
if isinstance(solvents, str):
try:

View File

@@ -85,8 +85,90 @@ class BioyondResourceSynchronizer(ResourceSynchronizer):
def sync_to_external(self, resource: Any) -> bool:
"""将本地物料数据变更同步到Bioyond系统"""
try:
if self.bioyond_api_client is None:
logger.error("Bioyond API客户端未初始化")
# ✅ 跳过仓库类型的资源 - 仓库是容器,不是物料
resource_category = getattr(resource, "category", None)
if resource_category == "warehouse":
logger.debug(f"[同步→Bioyond] 跳过仓库类型资源: {resource.name} (仓库是容器,不需要同步为物料)")
return True
logger.info(f"[同步→Bioyond] 收到物料变更: {resource.name}")
# 获取物料的 Bioyond ID
extra_info = getattr(resource, "unilabos_extra", {})
material_bioyond_id = extra_info.get("material_bioyond_id")
# ⭐ 如果没有 Bioyond ID尝试从 Bioyond 系统中按名称查询
if not material_bioyond_id:
logger.warning(f"[同步→Bioyond] 物料 {resource.name} 没有 Bioyond ID尝试按名称查询...")
try:
# 查询所有类型的物料0=耗材, 1=样品, 2=试剂
import json
all_materials = []
for type_mode in [0, 1, 2]:
query_params = json.dumps({
"typeMode": type_mode,
"filter": "", # 空字符串表示查询所有
"includeDetail": True
})
materials = self.bioyond_api_client.stock_material(query_params)
if materials:
all_materials.extend(materials)
logger.info(f"[同步→Bioyond] 查询到 {len(all_materials)} 个物料")
# 按名称匹配
for mat in all_materials:
if mat.get("name") == resource.name:
material_bioyond_id = mat.get("id")
mat_type = mat.get("typeName", "未知")
logger.info(f"✅ 找到物料 {resource.name} ({mat_type}) 的 Bioyond ID: {material_bioyond_id[:8]}...")
# 保存 ID 到资源对象
extra_info["material_bioyond_id"] = material_bioyond_id
setattr(resource, "unilabos_extra", extra_info)
break
if not material_bioyond_id:
logger.warning(f"⚠️ 在 Bioyond 系统中未找到名为 {resource.name} 的物料")
logger.info(f"[同步→Bioyond] 这是一个新物料,将创建并入库到 Bioyond 系统")
# 不返回,继续执行后续的创建+入库流程
except Exception as e:
logger.error(f"查询 Bioyond 物料失败: {e}")
import traceback
traceback.print_exc()
return False
# 检查是否有位置更新请求
update_site = extra_info.get("update_resource_site")
if not update_site:
logger.debug(f"[同步→Bioyond] 无位置更新请求")
return True
# ===== 物料移动/创建流程 =====
if material_bioyond_id:
logger.info(f"[同步→Bioyond] 🔄 开始移动物料 {resource.name}{update_site}")
else:
logger.info(f"[同步→Bioyond] 开始创建新物料 {resource.name} 并入库到 {update_site}") # 第1步获取仓库配置
from .config import WAREHOUSE_MAPPING
warehouse_mapping = WAREHOUSE_MAPPING
# 确定目标仓库名称(通过遍历所有仓库的库位配置)
parent_name = None
target_location_uuid = None
for warehouse_name, warehouse_info in warehouse_mapping.items():
site_uuids = warehouse_info.get("site_uuids", {})
if update_site in site_uuids:
parent_name = warehouse_name
target_location_uuid = site_uuids[update_site]
logger.info(f"[同步] 目标仓库: {parent_name}/{update_site}")
logger.info(f"[同步] 目标库位UUID: {target_location_uuid[:8]}...")
break
if not parent_name or not target_location_uuid:
logger.error(f"❌ 库位 {update_site} 没有在 WAREHOUSE_MAPPING 中配置")
logger.debug(f"可用仓库: {list(warehouse_mapping.keys())}")
return False
bioyond_material = resource_plr_to_bioyond(
@@ -171,11 +253,22 @@ class BioyondWorkstation(WorkstationBase):
def post_init(self, ros_node: ROS2WorkstationNode):
self._ros_node = ros_node
# ⭐ 上传 deck包括所有 warehouses 及其中的物料)
# 注意:如果有从 Bioyond 同步的物料,它们已经被放置到 warehouse 中了
# 所以只需要上传 deck物料会作为 warehouse 的 children 一起上传
logger.info("正在上传 deck包括 warehouses 和物料)到云端...")
ROS2DeviceNode.run_async_func(self._ros_node.update_resource, True, **{
"resources": [self.deck]
})
# 清理临时变量(物料已经在 deck 的 warehouse children 中,不需要单独上传)
if hasattr(self, "_synced_resources"):
logger.info(f"{len(self._synced_resources)} 个从Bioyond同步的物料已包含在 deck 中")
self._synced_resources = []
def transfer_resource_to_another(self, resource: List[ResourceSlot], mount_resource: List[ResourceSlot], sites: List[str], mount_device_id: DeviceSlot):
time.sleep(3)
ROS2DeviceNode.run_async_func(self._ros_node.transfer_resource_to_another, True, **{
"plr_resources": resource,
"target_device_id": mount_device_id,

View File

@@ -1,583 +0,0 @@
"""
工作站物料管理基类
Workstation Material Management Base Class
基于PyLabRobot的物料管理系统
"""
from typing import Dict, Any, List, Optional, Union, Type
from abc import ABC, abstractmethod
import json
from pylabrobot.resources import (
Resource as PLRResource,
Container,
Deck,
Coordinate as PLRCoordinate,
)
from unilabos.ros.nodes.resource_tracker import DeviceNodeResourceTracker
from unilabos.utils.log import logger
from unilabos.resources.graphio import resource_plr_to_ulab, resource_ulab_to_plr
class MaterialManagementBase(ABC):
"""物料管理基类
定义工作站物料管理的标准接口:
1. 物料初始化 - 根据配置创建物料资源
2. 物料追踪 - 实时跟踪物料位置和状态
3. 物料查找 - 按类型、位置、状态查找物料
4. 物料转换 - PyLabRobot与UniLab资源格式转换
"""
def __init__(
self,
device_id: str,
deck_config: Dict[str, Any],
resource_tracker: DeviceNodeResourceTracker,
children_config: Dict[str, Dict[str, Any]] = None
):
self.device_id = device_id
self.deck_config = deck_config
self.resource_tracker = resource_tracker
self.children_config = children_config or {}
# 创建主台面
self.plr_deck = self._create_deck()
# 扩展ResourceTracker
self._extend_resource_tracker()
# 注册deck到resource tracker
self.resource_tracker.add_resource(self.plr_deck)
# 初始化子资源
self.plr_resources = {}
self._initialize_materials()
def _create_deck(self) -> Deck:
"""创建主台面"""
return Deck(
name=f"{self.device_id}_deck",
size_x=self.deck_config.get("size_x", 1000.0),
size_y=self.deck_config.get("size_y", 1000.0),
size_z=self.deck_config.get("size_z", 500.0),
origin=PLRCoordinate(0, 0, 0)
)
def _extend_resource_tracker(self):
"""扩展ResourceTracker以支持PyLabRobot特定功能"""
def find_by_type(resource_type):
"""按类型查找资源"""
return self._find_resources_by_type_recursive(self.plr_deck, resource_type)
def find_by_category(category: str):
"""按类别查找资源"""
found = []
for resource in self._get_all_resources():
if hasattr(resource, 'category') and resource.category == category:
found.append(resource)
return found
def find_by_name_pattern(pattern: str):
"""按名称模式查找资源"""
import re
found = []
for resource in self._get_all_resources():
if re.search(pattern, resource.name):
found.append(resource)
return found
# 动态添加方法到resource_tracker
self.resource_tracker.find_by_type = find_by_type
self.resource_tracker.find_by_category = find_by_category
self.resource_tracker.find_by_name_pattern = find_by_name_pattern
def _find_resources_by_type_recursive(self, resource, target_type):
"""递归查找指定类型的资源"""
found = []
if isinstance(resource, target_type):
found.append(resource)
# 递归查找子资源
children = getattr(resource, "children", [])
for child in children:
found.extend(self._find_resources_by_type_recursive(child, target_type))
return found
def _get_all_resources(self) -> List[PLRResource]:
"""获取所有资源"""
all_resources = []
def collect_resources(resource):
all_resources.append(resource)
children = getattr(resource, "children", [])
for child in children:
collect_resources(child)
collect_resources(self.plr_deck)
return all_resources
def _initialize_materials(self):
"""初始化物料"""
try:
# 确定创建顺序,确保父资源先于子资源创建
creation_order = self._determine_creation_order()
# 按顺序创建资源
for resource_id in creation_order:
config = self.children_config[resource_id]
self._create_plr_resource(resource_id, config)
logger.info(f"物料管理系统初始化完成,共创建 {len(self.plr_resources)} 个资源")
except Exception as e:
logger.error(f"物料初始化失败: {e}")
def _determine_creation_order(self) -> List[str]:
"""确定资源创建顺序"""
order = []
visited = set()
def visit(resource_id: str):
if resource_id in visited:
return
visited.add(resource_id)
config = self.children_config.get(resource_id, {})
parent_id = config.get("parent")
# 如果有父资源,先访问父资源
if parent_id and parent_id in self.children_config:
visit(parent_id)
order.append(resource_id)
for resource_id in self.children_config:
visit(resource_id)
return order
def _create_plr_resource(self, resource_id: str, config: Dict[str, Any]):
"""创建PyLabRobot资源"""
try:
resource_type = config.get("type", "unknown")
data = config.get("data", {})
location_config = config.get("location", {})
# 创建位置坐标
location = PLRCoordinate(
x=location_config.get("x", 0.0),
y=location_config.get("y", 0.0),
z=location_config.get("z", 0.0)
)
# 根据类型创建资源
resource = self._create_resource_by_type(resource_id, resource_type, config, data, location)
if resource:
# 设置父子关系
parent_id = config.get("parent")
if parent_id and parent_id in self.plr_resources:
parent_resource = self.plr_resources[parent_id]
parent_resource.assign_child_resource(resource, location)
else:
# 直接放在deck上
self.plr_deck.assign_child_resource(resource, location)
# 保存资源引用
self.plr_resources[resource_id] = resource
# 注册到resource tracker
self.resource_tracker.add_resource(resource)
logger.debug(f"创建资源成功: {resource_id} ({resource_type})")
except Exception as e:
logger.error(f"创建资源失败 {resource_id}: {e}")
@abstractmethod
def _create_resource_by_type(
self,
resource_id: str,
resource_type: str,
config: Dict[str, Any],
data: Dict[str, Any],
location: PLRCoordinate
) -> Optional[PLRResource]:
"""根据类型创建资源 - 子类必须实现"""
pass
# ============ 物料查找接口 ============
def find_materials_by_type(self, material_type: str) -> List[PLRResource]:
"""按材料类型查找物料"""
return self.resource_tracker.find_by_category(material_type)
def find_material_by_id(self, resource_id: str) -> Optional[PLRResource]:
"""按ID查找物料"""
return self.plr_resources.get(resource_id)
def find_available_positions(self, position_type: str) -> List[PLRResource]:
"""查找可用位置"""
positions = self.resource_tracker.find_by_category(position_type)
available = []
for pos in positions:
if hasattr(pos, 'is_available') and pos.is_available():
available.append(pos)
elif hasattr(pos, 'children') and len(pos.children) == 0:
available.append(pos)
return available
def get_material_inventory(self) -> Dict[str, int]:
"""获取物料库存统计"""
inventory = {}
for resource in self._get_all_resources():
if hasattr(resource, 'category'):
category = resource.category
inventory[category] = inventory.get(category, 0) + 1
return inventory
# ============ 物料状态更新接口 ============
def update_material_location(self, material_id: str, new_location: PLRCoordinate) -> bool:
"""更新物料位置"""
try:
material = self.find_material_by_id(material_id)
if material:
material.location = new_location
return True
return False
except Exception as e:
logger.error(f"更新物料位置失败: {e}")
return False
def move_material(self, material_id: str, target_container_id: str) -> bool:
"""移动物料到目标容器"""
try:
material = self.find_material_by_id(material_id)
target = self.find_material_by_id(target_container_id)
if material and target:
# 从原位置移除
if material.parent:
material.parent.unassign_child_resource(material)
# 添加到新位置
target.assign_child_resource(material)
return True
return False
except Exception as e:
logger.error(f"移动物料失败: {e}")
return False
# ============ 资源转换接口 ============
def convert_to_unilab_format(self, plr_resource: PLRResource) -> Dict[str, Any]:
"""将PyLabRobot资源转换为UniLab格式"""
return resource_plr_to_ulab(plr_resource)
def convert_from_unilab_format(self, unilab_resource: Dict[str, Any]) -> PLRResource:
"""将UniLab格式转换为PyLabRobot资源"""
return resource_ulab_to_plr(unilab_resource)
def get_deck_state(self) -> Dict[str, Any]:
"""获取Deck状态"""
try:
return {
"deck_info": {
"name": self.plr_deck.name,
"size": {
"x": self.plr_deck.size_x,
"y": self.plr_deck.size_y,
"z": self.plr_deck.size_z
},
"children_count": len(self.plr_deck.children)
},
"resources": {
resource_id: self.convert_to_unilab_format(resource)
for resource_id, resource in self.plr_resources.items()
},
"inventory": self.get_material_inventory()
}
except Exception as e:
logger.error(f"获取Deck状态失败: {e}")
return {"error": str(e)}
# ============ 数据持久化接口 ============
def save_state_to_file(self, file_path: str) -> bool:
"""保存状态到文件"""
try:
state = self.get_deck_state()
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(state, f, indent=2, ensure_ascii=False)
logger.info(f"状态已保存到: {file_path}")
return True
except Exception as e:
logger.error(f"保存状态失败: {e}")
return False
def load_state_from_file(self, file_path: str) -> bool:
"""从文件加载状态"""
try:
with open(file_path, 'r', encoding='utf-8') as f:
state = json.load(f)
# 重新创建资源
self._recreate_resources_from_state(state)
logger.info(f"状态已从文件加载: {file_path}")
return True
except Exception as e:
logger.error(f"加载状态失败: {e}")
return False
def _recreate_resources_from_state(self, state: Dict[str, Any]):
"""从状态重新创建资源"""
# 清除现有资源
self.plr_resources.clear()
self.plr_deck.children.clear()
# 从状态重新创建
resources_data = state.get("resources", {})
for resource_id, resource_data in resources_data.items():
try:
plr_resource = self.convert_from_unilab_format(resource_data)
self.plr_resources[resource_id] = plr_resource
self.plr_deck.assign_child_resource(plr_resource)
except Exception as e:
logger.error(f"重新创建资源失败 {resource_id}: {e}")
class CoinCellMaterialManagement(MaterialManagementBase):
"""纽扣电池物料管理类
从 button_battery_station 抽取的物料管理功能
"""
def _create_resource_by_type(
self,
resource_id: str,
resource_type: str,
config: Dict[str, Any],
data: Dict[str, Any],
location: PLRCoordinate
) -> Optional[PLRResource]:
"""根据类型创建纽扣电池相关资源"""
# 导入纽扣电池资源类
from unilabos.device_comms.button_battery_station import (
MaterialPlate, PlateSlot, ClipMagazine, BatteryPressSlot,
TipBox64, WasteTipBox, BottleRack, Battery, ElectrodeSheet
)
try:
if resource_type == "material_plate":
return self._create_material_plate(resource_id, config, data, location)
elif resource_type == "plate_slot":
return self._create_plate_slot(resource_id, config, data, location)
elif resource_type == "clip_magazine":
return self._create_clip_magazine(resource_id, config, data, location)
elif resource_type == "battery_press_slot":
return self._create_battery_press_slot(resource_id, config, data, location)
elif resource_type == "tip_box":
return self._create_tip_box(resource_id, config, data, location)
elif resource_type == "waste_tip_box":
return self._create_waste_tip_box(resource_id, config, data, location)
elif resource_type == "bottle_rack":
return self._create_bottle_rack(resource_id, config, data, location)
elif resource_type == "battery":
return self._create_battery(resource_id, config, data, location)
else:
logger.warning(f"未知的资源类型: {resource_type}")
return None
except Exception as e:
logger.error(f"创建资源失败 {resource_id} ({resource_type}): {e}")
return None
def _create_material_plate(self, resource_id: str, config: Dict[str, Any], data: Dict[str, Any], location: PLRCoordinate):
"""创建料板"""
from unilabos.device_comms.button_battery_station import MaterialPlate, ElectrodeSheet
plate = MaterialPlate(
name=resource_id,
size_x=config.get("size_x", 80.0),
size_y=config.get("size_y", 80.0),
size_z=config.get("size_z", 10.0),
hole_diameter=config.get("hole_diameter", 15.0),
hole_depth=config.get("hole_depth", 8.0),
hole_spacing_x=config.get("hole_spacing_x", 20.0),
hole_spacing_y=config.get("hole_spacing_y", 20.0),
number=data.get("number", "")
)
plate.location = location
# 如果有预填充的极片数据,创建极片
electrode_sheets = data.get("electrode_sheets", [])
for i, sheet_data in enumerate(electrode_sheets):
if i < len(plate.children): # 确保不超过洞位数量
hole = plate.children[i]
sheet = ElectrodeSheet(
name=f"{resource_id}_sheet_{i}",
diameter=sheet_data.get("diameter", 14.0),
thickness=sheet_data.get("thickness", 0.1),
mass=sheet_data.get("mass", 0.01),
material_type=sheet_data.get("material_type", "cathode"),
info=sheet_data.get("info", "")
)
hole.place_electrode_sheet(sheet)
return plate
def _create_plate_slot(self, resource_id: str, config: Dict[str, Any], data: Dict[str, Any], location: PLRCoordinate):
"""创建板槽位"""
from unilabos.device_comms.button_battery_station import PlateSlot
slot = PlateSlot(
name=resource_id,
max_plates=config.get("max_plates", 8)
)
slot.location = location
return slot
def _create_clip_magazine(self, resource_id: str, config: Dict[str, Any], data: Dict[str, Any], location: PLRCoordinate):
"""创建子弹夹"""
from unilabos.device_comms.button_battery_station import ClipMagazine
magazine = ClipMagazine(
name=resource_id,
size_x=config.get("size_x", 150.0),
size_y=config.get("size_y", 100.0),
size_z=config.get("size_z", 50.0),
hole_diameter=config.get("hole_diameter", 15.0),
hole_depth=config.get("hole_depth", 40.0),
hole_spacing=config.get("hole_spacing", 25.0),
max_sheets_per_hole=config.get("max_sheets_per_hole", 100)
)
magazine.location = location
return magazine
def _create_battery_press_slot(self, resource_id: str, config: Dict[str, Any], data: Dict[str, Any], location: PLRCoordinate):
"""创建电池压制槽"""
from unilabos.device_comms.button_battery_station import BatteryPressSlot
slot = BatteryPressSlot(
name=resource_id,
diameter=config.get("diameter", 20.0),
depth=config.get("depth", 15.0)
)
slot.location = location
return slot
def _create_tip_box(self, resource_id: str, config: Dict[str, Any], data: Dict[str, Any], location: PLRCoordinate):
"""创建枪头盒"""
from unilabos.device_comms.button_battery_station import TipBox64
tip_box = TipBox64(
name=resource_id,
size_x=config.get("size_x", 127.8),
size_y=config.get("size_y", 85.5),
size_z=config.get("size_z", 60.0),
with_tips=data.get("with_tips", True)
)
tip_box.location = location
return tip_box
def _create_waste_tip_box(self, resource_id: str, config: Dict[str, Any], data: Dict[str, Any], location: PLRCoordinate):
"""创建废枪头盒"""
from unilabos.device_comms.button_battery_station import WasteTipBox
waste_box = WasteTipBox(
name=resource_id,
size_x=config.get("size_x", 127.8),
size_y=config.get("size_y", 85.5),
size_z=config.get("size_z", 60.0),
max_tips=config.get("max_tips", 100)
)
waste_box.location = location
return waste_box
def _create_bottle_rack(self, resource_id: str, config: Dict[str, Any], data: Dict[str, Any], location: PLRCoordinate):
"""创建瓶架"""
from unilabos.device_comms.button_battery_station import BottleRack
rack = BottleRack(
name=resource_id,
size_x=config.get("size_x", 210.0),
size_y=config.get("size_y", 140.0),
size_z=config.get("size_z", 100.0),
bottle_diameter=config.get("bottle_diameter", 30.0),
bottle_height=config.get("bottle_height", 100.0),
position_spacing=config.get("position_spacing", 35.0)
)
rack.location = location
return rack
def _create_battery(self, resource_id: str, config: Dict[str, Any], data: Dict[str, Any], location: PLRCoordinate):
"""创建电池"""
from unilabos.device_comms.button_battery_station import Battery
battery = Battery(
name=resource_id,
diameter=config.get("diameter", 20.0),
height=config.get("height", 3.2),
max_volume=config.get("max_volume", 100.0),
barcode=data.get("barcode", "")
)
battery.location = location
return battery
# ============ 纽扣电池特定查找方法 ============
def find_material_plates(self):
"""查找所有料板"""
from unilabos.device_comms.button_battery_station import MaterialPlate
return self.resource_tracker.find_by_type(MaterialPlate)
def find_batteries(self):
"""查找所有电池"""
from unilabos.device_comms.button_battery_station import Battery
return self.resource_tracker.find_by_type(Battery)
def find_electrode_sheets(self):
"""查找所有极片"""
found = []
plates = self.find_material_plates()
for plate in plates:
for hole in plate.children:
if hasattr(hole, 'has_electrode_sheet') and hole.has_electrode_sheet():
found.append(hole._electrode_sheet)
return found
def find_plate_slots(self):
"""查找所有板槽位"""
from unilabos.device_comms.button_battery_station import PlateSlot
return self.resource_tracker.find_by_type(PlateSlot)
def find_clip_magazines(self):
"""查找所有子弹夹"""
from unilabos.device_comms.button_battery_station import ClipMagazine
return self.resource_tracker.find_by_type(ClipMagazine)
def find_press_slots(self):
"""查找所有压制槽"""
from unilabos.device_comms.button_battery_station import BatteryPressSlot
return self.resource_tracker.find_by_type(BatteryPressSlot)

View File

@@ -0,0 +1,26 @@
{
"nodes": [
{
"id": "XRD_D7MATE_STATION",
"name": "XRD_D7MATE",
"parent": null,
"type": "device",
"class": "xrd_d7mate",
"position": {
"x": 720.0,
"y": 200.0,
"z": 0
},
"config": {
"host": "127.0.0.1",
"port": 6001,
"timeout": 10.0
},
"data": {
"input_hint": "start 支持单字符串输入:'sample_name 样品A start_theta 10.0 end_theta 80.0 increment 0.02 exp_time 0.1 [wait_minutes 3]';也支持等号形式 'sample_id=样品A start_theta=10.0 end_theta=80.0 increment=0.02 exp_time=0.1 wait_minutes=3'"
},
"children": []
}
],
"links": []
}

View File

@@ -0,0 +1,939 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
XRD D7-Mate设备驱动
支持XRD D7-Mate设备的TCP通信协议包括自动模式控制、上样流程、数据获取、下样流程和高压电源控制等功能。
通信协议版本1.0.0
"""
import json
import socket
import struct
import time
from typing import Dict, List, Optional, Tuple, Any, Union
class XRDClient:
def __init__(self, host='127.0.0.1', port=6001, timeout=10.0):
"""
初始化XRD D7-Mate客户端
Args:
host (str): 设备IP地址
port (int): 通信端口默认6001
timeout (float): 超时时间,单位秒
"""
self.host = host
self.port = port
self.timeout = timeout
self.sock = None
self._ros_node = None # ROS节点引用由框架设置
def post_init(self, ros_node):
"""
ROS节点初始化后的回调方法保存ROS节点引用但不自动连接
Args:
ros_node: ROS节点实例
"""
self._ros_node = ros_node
ros_node.lab_logger().info(f"XRD D7-Mate设备已初始化将在需要时连接: {self.host}:{self.port}")
# 不自动连接,只有在调用具体功能时才建立连接
def connect(self):
"""
建立TCP连接到XRD D7-Mate设备
Raises:
ConnectionError: 连接失败时抛出
"""
try:
self.sock = socket.create_connection((self.host, self.port), timeout=self.timeout)
self.sock.settimeout(self.timeout)
except Exception as e:
raise ConnectionError(f"Failed to connect to {self.host}:{self.port} - {str(e)}")
def close(self):
"""
关闭与XRD D7-Mate设备的TCP连接
"""
if self.sock:
try:
self.sock.close()
except Exception:
pass # 忽略关闭时的错误
finally:
self.sock = None
def _ensure_connection(self) -> bool:
"""
确保连接存在,如果不存在则尝试建立连接
Returns:
bool: 连接是否成功建立
"""
if self.sock is None:
try:
self.connect()
return True
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().error(f"建立连接失败: {e}")
return False
return True
def _receive_with_length_prefix(self) -> dict:
"""
使用长度前缀协议接收数据
Returns:
dict: 解析后的JSON响应数据
Raises:
ConnectionError: 连接错误
TimeoutError: 超时错误
"""
try:
# 首先接收4字节的长度信息
length_data = bytearray()
while len(length_data) < 4:
chunk = self.sock.recv(4 - len(length_data))
if not chunk:
raise ConnectionError("Connection closed while receiving length prefix")
length_data.extend(chunk)
# 解析长度(大端序无符号整数)
data_length = struct.unpack('>I', length_data)[0]
if self._ros_node:
self._ros_node.lab_logger().info(f"接收到数据长度: {data_length} 字节")
# 根据长度接收实际数据
json_data = bytearray()
while len(json_data) < data_length:
remaining = data_length - len(json_data)
chunk = self.sock.recv(min(4096, remaining))
if not chunk:
raise ConnectionError("Connection closed while receiving JSON data")
json_data.extend(chunk)
# 解码JSON数据优先使用UTF-8失败时尝试GBK
try:
json_str = json_data.decode('utf-8')
except UnicodeDecodeError:
json_str = json_data.decode('gbk')
# 解析JSON
result = json.loads(json_str)
if self._ros_node:
self._ros_node.lab_logger().info(f"成功解析JSON响应: {result}")
return result
except socket.timeout:
if self._ros_node:
self._ros_node.lab_logger().warning(f"接收超时")
raise TimeoutError(f"recv() timed out after {self.timeout:.1f}s")
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().error(f"接收数据失败: {e}")
raise ConnectionError(f"Failed to receive data: {str(e)}")
def _send_command(self, cmd: dict) -> dict:
"""
使用长度前缀协议发送命令到XRD D7-Mate设备并接收响应
Args:
cmd (dict): 要发送的命令字典
Returns:
dict: 设备响应的JSON数据
Raises:
ConnectionError: 连接错误
TimeoutError: 超时错误
"""
# 确保连接存在,如果不存在则建立连接
if not self.sock:
try:
self.connect()
if self._ros_node:
self._ros_node.lab_logger().info(f"为命令重新建立连接")
except Exception as e:
raise ConnectionError(f"Failed to establish connection: {str(e)}")
try:
# 序列化命令为JSON
json_str = json.dumps(cmd, ensure_ascii=False)
payload = json_str.encode('utf-8')
# 计算JSON数据长度并打包为4字节大端序无符号整数
length_prefix = struct.pack('>I', len(payload))
if self._ros_node:
self._ros_node.lab_logger().info(f"发送JSON命令到XRD D7-Mate: {json_str}")
self._ros_node.lab_logger().info(f"发送数据长度: {len(payload)} 字节")
# 发送长度前缀
self.sock.sendall(length_prefix)
# 发送JSON数据
self.sock.sendall(payload)
# 使用长度前缀协议接收响应
response = self._receive_with_length_prefix()
return response
except Exception as e:
# 如果是连接错误,尝试重新连接一次
if "远程主机强迫关闭了一个现有的连接" in str(e) or "10054" in str(e):
if self._ros_node:
self._ros_node.lab_logger().warning(f"连接被远程主机关闭,尝试重新连接: {e}")
try:
self.close()
self.connect()
# 重新发送命令
json_str = json.dumps(cmd, ensure_ascii=False)
payload = json_str.encode('utf-8')
if self._ros_node:
self._ros_node.lab_logger().info(f"重新发送JSON命令到XRD D7-Mate: {json_str}")
self.sock.sendall(payload)
# 重新接收响应
buffer = bytearray()
start = time.time()
while True:
try:
chunk = self.sock.recv(4096)
if not chunk:
break
buffer.extend(chunk)
# 尝试解码和解析JSON
try:
text = buffer.decode('utf-8', errors='strict')
text = text.strip()
if text.startswith('{'):
brace_count = 0
json_end = -1
for i, char in enumerate(text):
if char == '{':
brace_count += 1
elif char == '}':
brace_count -= 1
if brace_count == 0:
json_end = i + 1
break
if json_end > 0:
text = text[:json_end]
result = json.loads(text)
if self._ros_node:
self._ros_node.lab_logger().info(f"重连后成功解析JSON响应: {result}")
return result
except (UnicodeDecodeError, json.JSONDecodeError):
pass
except socket.timeout:
if self._ros_node:
self._ros_node.lab_logger().warning(f"重连后接收超时")
raise TimeoutError(f"recv() timed out after reconnection")
if time.time() - start > self.timeout * 2:
raise TimeoutError(f"No complete JSON received after reconnection")
except Exception as retry_e:
if self._ros_node:
self._ros_node.lab_logger().error(f"重连失败: {retry_e}")
raise ConnectionError(f"Connection retry failed: {str(retry_e)}")
if isinstance(e, (ConnectionError, TimeoutError)):
raise
else:
raise ConnectionError(f"Command send failed: {str(e)}")
# ==================== 自动模式控制 ====================
def start_auto_mode(self, status: bool) -> dict:
"""
启动或停止自动模式
Args:
status (bool): True-启动自动模式False-停止自动模式
Returns:
dict: 响应结果包含status、timestamp、message
"""
if not self.sock:
try:
self.connect()
if self._ros_node:
self._ros_node.lab_logger().info("XRD D7-Mate设备重新连接成功")
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().warning(f"XRD D7-Mate设备连接失败: {e}")
return {"status": False, "message": "设备连接异常"}
try:
# 按协议要求content 直接为布尔值使用传入的status参数
cmd = {
"command": "START_AUTO_MODE",
"content": {
"status": bool(True)
}
}
if self._ros_node:
self._ros_node.lab_logger().info(f"发送自动模式控制命令: {cmd}")
response = self._send_command(cmd)
if self._ros_node:
self._ros_node.lab_logger().info(f"收到自动模式控制响应: {response}")
return response
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().error(f"自动模式控制失败: {e}")
return {"status": False, "message": f"自动模式控制失败: {str(e)}"}
# ==================== 上样流程 ====================
def get_sample_request(self) -> dict:
"""
上样请求,检查是否允许上样
Returns:
dict: 响应结果包含status、timestamp、message
"""
if not self.sock:
try:
self.connect()
if self._ros_node:
self._ros_node.lab_logger().info("XRD D7-Mate设备重新连接成功")
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().warning(f"XRD D7-Mate设备连接失败: {e}")
return {"status": False, "message": "设备连接异常"}
try:
cmd = {
"command": "GET_SAMPLE_REQUEST",
}
if self._ros_node:
self._ros_node.lab_logger().info(f"发送上样请求命令: {cmd}")
response = self._send_command(cmd)
if self._ros_node:
self._ros_node.lab_logger().info(f"收到上样请求响应: {response}")
return response
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().error(f"上样请求失败: {e}")
return {"status": False, "message": f"上样请求失败: {str(e)}"}
def send_sample_ready(self, sample_id: str, start_theta: float, end_theta: float,
increment: float, exp_time: float) -> dict:
"""
送样完成后,发送样品信息和采集参数
Args:
sample_id (str): 样品标识符
start_theta (float): 起始角度≥5°
end_theta (float): 结束角度≥5.5°且必须大于start_theta
increment (float): 角度增量≥0.005
exp_time (float): 曝光时间0.1-5.0秒)
Returns:
dict: 响应结果包含status、timestamp、message等
"""
# 参数验证
if start_theta < 5.0:
return {"status": False, "message": "起始角度必须≥5°"}
if end_theta < 5.5:
return {"status": False, "message": "结束角度必须≥5.5°"}
if end_theta <= start_theta:
return {"status": False, "message": "结束角度必须大于起始角度"}
if increment < 0.005:
return {"status": False, "message": "角度增量必须≥0.005"}
if not (0.1 <= exp_time <= 5.0):
return {"status": False, "message": "曝光时间必须在0.1-5.0秒之间"}
if not self.sock:
try:
self.connect()
if self._ros_node:
self._ros_node.lab_logger().info("XRD D7-Mate设备重新连接成功")
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().warning(f"XRD D7-Mate设备连接失败: {e}")
return {"status": False, "message": "设备连接异常"}
try:
cmd = {
"command": "SEND_SAMPLE_READY",
"content": {
"sample_id": sample_id,
"start_theta": start_theta,
"end_theta": end_theta,
"increment": increment,
"exp_time": exp_time
}
}
if self._ros_node:
self._ros_node.lab_logger().info(f"发送样品准备完成命令: {cmd}")
response = self._send_command(cmd)
if self._ros_node:
self._ros_node.lab_logger().info(f"收到样品准备完成响应: {response}")
return response
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().error(f"样品准备完成失败: {e}")
return {"status": False, "message": f"样品准备完成失败: {str(e)}"}
# ==================== 数据获取 ====================
def get_current_acquire_data(self) -> dict:
"""
获取当前正在采集的样品数据
Returns:
dict: 响应结果包含status、timestamp、sample_id、Energy、Intensity等
"""
if not self.sock:
try:
self.connect()
if self._ros_node:
self._ros_node.lab_logger().info("XRD D7-Mate设备重新连接成功")
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().warning(f"XRD D7-Mate设备连接失败: {e}")
return {"status": False, "message": "设备连接异常"}
try:
cmd = {
"command": "GET_CURRENT_ACQUIRE_DATA",
}
if self._ros_node:
self._ros_node.lab_logger().info(f"发送获取采集数据命令: {cmd}")
response = self._send_command(cmd)
if self._ros_node:
self._ros_node.lab_logger().info(f"收到获取采集数据响应: {response}")
return response
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().error(f"获取采集数据失败: {e}")
return {"status": False, "message": f"获取采集数据失败: {str(e)}"}
def get_sample_status(self) -> dict:
"""
获取工位样品状态及设备状态
Returns:
dict: 响应结果包含status、timestamp、Station等
"""
if not self.sock:
try:
self.connect()
if self._ros_node:
self._ros_node.lab_logger().info("XRD D7-Mate设备重新连接成功")
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().warning(f"XRD D7-Mate设备连接失败: {e}")
return {"status": False, "message": "设备连接异常"}
try:
cmd = {
"command": "GET_SAMPLE_STATUS",
}
if self._ros_node:
self._ros_node.lab_logger().info(f"发送获取样品状态命令: {cmd}")
response = self._send_command(cmd)
if self._ros_node:
self._ros_node.lab_logger().info(f"收到获取样品状态响应: {response}")
return response
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().error(f"获取样品状态失败: {e}")
return {"status": False, "message": f"获取样品状态失败: {str(e)}"}
# ==================== 下样流程 ====================
def get_sample_down(self, sample_station: int) -> dict:
"""
下样请求
Args:
sample_station (int): 下样工位1, 2, 3
Returns:
dict: 响应结果包含status、timestamp、sample_info等
"""
# 参数验证
if sample_station not in [1, 2, 3]:
return {"status": False, "message": "下样工位必须是1、2或3"}
if not self.sock:
try:
self.connect()
if self._ros_node:
self._ros_node.lab_logger().info("XRD D7-Mate设备重新连接成功")
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().warning(f"XRD D7-Mate设备连接失败: {e}")
return {"status": False, "message": "设备连接异常"}
try:
# 按协议要求content 直接为整数工位号
cmd = {
"command": "GET_SAMPLE_DOWN",
"content": {
"Sample station":int(3)
}
}
if self._ros_node:
self._ros_node.lab_logger().info(f"发送下样请求命令: {cmd}")
response = self._send_command(cmd)
if self._ros_node:
self._ros_node.lab_logger().info(f"收到下样请求响应: {response}")
return response
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().error(f"下样请求失败: {e}")
return {"status": False, "message": f"下样请求失败: {str(e)}"}
def send_sample_down_ready(self) -> dict:
"""
下样完成命令
Returns:
dict: 响应结果包含status、timestamp、message
"""
if not self.sock:
try:
self.connect()
if self._ros_node:
self._ros_node.lab_logger().info("XRD D7-Mate设备重新连接成功")
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().warning(f"XRD D7-Mate设备连接失败: {e}")
return {"status": False, "message": "设备连接异常"}
try:
cmd = {
"command": "SEND_SAMPLE_DOWN_READY",
}
if self._ros_node:
self._ros_node.lab_logger().info(f"发送下样完成命令: {cmd}")
response = self._send_command(cmd)
if self._ros_node:
self._ros_node.lab_logger().info(f"收到下样完成响应: {response}")
return response
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().error(f"下样完成失败: {e}")
return {"status": False, "message": f"下样完成失败: {str(e)}"}
# ==================== 高压电源控制 ====================
def set_power_on(self) -> dict:
"""
高压电源开启
Returns:
dict: 响应结果包含status、timestamp、message
"""
if not self.sock:
try:
self.connect()
if self._ros_node:
self._ros_node.lab_logger().info("XRD D7-Mate设备重新连接成功")
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().warning(f"XRD D7-Mate设备连接失败: {e}")
return {"status": False, "message": "设备连接异常"}
try:
cmd = {
"command": "SET_POWER_ON",
}
if self._ros_node:
self._ros_node.lab_logger().info(f"发送高压电源开启命令: {cmd}")
response = self._send_command(cmd)
if self._ros_node:
self._ros_node.lab_logger().info(f"收到高压开启响应: {response}")
return response
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().error(f"高压开启失败: {e}")
return {"status": False, "message": f"高压开启失败: {str(e)}"}
def set_power_off(self) -> dict:
"""
高压电源关闭
Returns:
dict: 响应结果包含status、timestamp、message
"""
if not self.sock:
try:
self.connect()
if self._ros_node:
self._ros_node.lab_logger().info("XRD D7-Mate设备重新连接成功")
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().warning(f"XRD D7-Mate设备连接失败: {e}")
return {"status": False, "message": "设备连接异常"}
try:
cmd = {
"command": "SET_POWER_OFF",
}
if self._ros_node:
self._ros_node.lab_logger().info(f"发送高压电源关闭命令: {cmd}")
response = self._send_command(cmd)
if self._ros_node:
self._ros_node.lab_logger().info(f"收到高压关闭响应: {response}")
return response
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().error(f"高压关闭失败: {e}")
return {"status": False, "message": f"高压关闭失败: {str(e)}"}
def set_voltage_current(self, voltage: float, current: float) -> dict:
"""
设置高压电源电压和电流
Args:
voltage (float): 电压值kV
current (float): 电流值mA
Returns:
dict: 响应结果包含status、timestamp、message
"""
if not self.sock:
try:
self.connect()
if self._ros_node:
self._ros_node.lab_logger().info("XRD D7-Mate设备重新连接成功")
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().warning(f"XRD D7-Mate设备连接失败: {e}")
return {"status": False, "message": "设备连接异常"}
try:
cmd = {
"command": "SET_VOLTAGE_CURRENT",
"content": {
"voltage": voltage,
"current": current
}
}
if self._ros_node:
self._ros_node.lab_logger().info(f"发送设置电压电流命令: {cmd}")
response = self._send_command(cmd)
if self._ros_node:
self._ros_node.lab_logger().info(f"收到设置电压电流响应: {response}")
return response
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().error(f"设置电压电流失败: {e}")
return {"status": False, "message": f"设置电压电流失败: {str(e)}"}
def start(self, sample_id: str = "", start_theta: float = 10.0, end_theta: float = 80.0,
increment: float = 0.05, exp_time: float = 0.1, wait_minutes: float = 3.0,
string: str = "") -> dict:
"""
Start 主流程:
1) 启动自动模式;
2) 发送上样请求并等待允许;
3) 等待指定分钟后发送样品准备完成(携带采集参数);
4) 周期性轮询采集数据与工位状态;
5) 一旦任一下样位变为 True执行下样流程GET_SAMPLE_DOWN + SEND_SAMPLE_DOWN_READY
Args:
sample_id: 样品名称
start_theta: 起始角度≥5°
end_theta: 结束角度≥5.5°,且必须大于 start_theta
increment: 角度增量≥0.005
exp_time: 曝光时间0.1-5.0 秒)
wait_minutes: 在允许上样后、发送样品准备完成前的等待分钟数(默认 3 分钟)
string: 字符串格式的参数输入,如果提供则优先解析使用
Returns:
dict: {"return_info": str, "success": bool}
"""
try:
# 强制类型转换:除 sample_id 外的所有输入均转换为 float若为字符串
def _to_float(v, default):
try:
return float(v)
except (TypeError, ValueError):
return float(default)
if not isinstance(sample_id, str):
sample_id = str(sample_id)
if isinstance(start_theta, str):
start_theta = _to_float(start_theta, 10.0)
if isinstance(end_theta, str):
end_theta = _to_float(end_theta, 80.0)
if isinstance(increment, str):
increment = _to_float(increment, 0.05)
if isinstance(exp_time, str):
exp_time = _to_float(exp_time, 0.1)
if isinstance(wait_minutes, str):
wait_minutes = _to_float(wait_minutes, 3.0)
# 不再从 string 参数解析覆盖;保留参数但忽略字符串解析,统一使用结构化输入
# 确保设备连接
if not self.sock:
try:
self.connect()
if self._ros_node:
self._ros_node.lab_logger().info("XRD D7-Mate设备连接成功开始执行start流程")
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().error(f"XRD D7-Mate设备连接失败: {e}")
return {"return_info": f"设备连接失败: {str(e)}", "success": False}
# 1) 启动自动模式
r_auto = self.start_auto_mode(True)
if not r_auto.get("status", False):
return {"return_info": f"启动自动模式失败: {r_auto.get('message', '未知')}", "success": False}
if self._ros_node:
self._ros_node.lab_logger().info(f"自动模式已启动: {r_auto}")
# 2) 上样请求
r_req = self.get_sample_request()
if not r_req.get("status", False):
return {"return_info": f"上样请求未允许: {r_req.get('message', '未知')}", "success": False}
if self._ros_node:
self._ros_node.lab_logger().info(f"上样已允许: {r_req}")
# 3) 等待指定分钟后发送样品准备完成
wait_seconds = max(0.0, float(wait_minutes)) * 60.0
if self._ros_node:
self._ros_node.lab_logger().info(f"等待 {wait_minutes} 分钟后发送样品准备完成")
time.sleep(wait_seconds)
r_ready = self.send_sample_ready(sample_id=sample_id,
start_theta=start_theta,
end_theta=end_theta,
increment=increment,
exp_time=exp_time)
if not r_ready.get("status", False):
return {"return_info": f"样品准备完成失败: {r_ready.get('message', '未知')}", "success": False}
if self._ros_node:
self._ros_node.lab_logger().info(f"样品准备完成已发送: {r_ready}")
# 4) 轮询采集数据与工位状态
polling_interval = 5.0 # 秒
down_station_idx: Optional[int] = None
while True:
try:
r_data = self.get_current_acquire_data()
if self._ros_node:
self._ros_node.lab_logger().info(f"采集中数据: {r_data}")
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().warning(f"获取采集数据失败: {e}")
try:
r_status = self.get_sample_status()
if self._ros_node:
self._ros_node.lab_logger().info(f"工位状态: {r_status}")
station = r_status.get("Station", {})
if isinstance(station, dict):
for idx in (1, 2, 3):
key = f"DownStation{idx}"
val = station.get(key)
if isinstance(val, bool) and val:
down_station_idx = idx
break
if down_station_idx is not None:
break
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().warning(f"获取工位状态失败: {e}")
time.sleep(polling_interval)
if down_station_idx is None:
return {"return_info": "未检测到任一下样位 True流程未完成", "success": False}
# 5) 下样流程
r_down = self.get_sample_down(down_station_idx)
if not r_down.get("status", False):
return {"return_info": f"下样请求失败(工位 {down_station_idx}): {r_down.get('message', '未知')}", "success": False}
if self._ros_node:
self._ros_node.lab_logger().info(f"下样请求成功(工位 {down_station_idx}): {r_down}")
r_ready_down = self.send_sample_down_ready()
if not r_ready_down.get("status", False):
return {"return_info": f"下样完成发送失败: {r_ready_down.get('message', '未知')}", "success": False}
if self._ros_node:
self._ros_node.lab_logger().info(f"下样完成已发送: {r_ready_down}")
return {"return_info": f"Start流程完成工位 {down_station_idx} 已下样", "success": True}
except Exception as e:
if self._ros_node:
self._ros_node.lab_logger().error(f"Start流程异常: {e}")
return {"return_info": f"Start流程异常: {str(e)}", "success": False}
def _parse_start_params(self, params: Union[str, Dict[str, Any]]) -> Dict[str, Any]:
"""
解析UI输入参数为 Start 流程参数。
- 从UI字典中读取各个字段的字符串值
- 将数值字段从字符串转换为 float 类型
- 保留 sample_id 为字符串类型
返回:
dict: {sample_id, start_theta, end_theta, increment, exp_time, wait_minutes}
"""
# 如果传入为字典,则直接按键读取;否则给出警告并使用空字典
if isinstance(params, dict):
p = params
else:
p = {}
if self._ros_node:
self._ros_node.lab_logger().warning("start 参数应为结构化字典")
def _to_float(v, default):
"""将UI输入的字符串值转换为float处理空值和无效值"""
if v is None or v == '':
return float(default)
try:
# 处理字符串输入来自UI
if isinstance(v, str):
v = v.strip()
if v == '':
return float(default)
return float(v)
except (TypeError, ValueError):
return float(default)
# 从UI输入字典中读取参数
sample_id = p.get('sample_id') or p.get('sample_name') or '样品名称'
if not isinstance(sample_id, str):
sample_id = str(sample_id)
# 将UI字符串输入转换为float
result: Dict[str, Any] = {
'sample_id': sample_id,
'start_theta': _to_float(p.get('start_theta'), 10.0),
'end_theta': _to_float(p.get('end_theta'), 80.0),
'increment': _to_float(p.get('increment'), 0.05),
'exp_time': _to_float(p.get('exp_time'), 0.1),
'wait_minutes': _to_float(p.get('wait_minutes'), 3.0),
}
return result
def start_from_string(self, params: Union[str, Dict[str, Any]]) -> dict:
"""
从UI输入参数执行 Start 主流程。
接收来自用户界面的参数字典,其中数值字段为字符串格式,自动转换为正确的类型。
参数:
params: UI输入参数字典例如:
{
'sample_id': 'teste',
'start_theta': '10.0', # UI字符串输入
'end_theta': '25.0', # UI字符串输入
'increment': '0.05', # UI字符串输入
'exp_time': '0.10', # UI字符串输入
'wait_minutes': '0.5' # UI字符串输入
}
返回:
dict: 执行结果
"""
parsed = self._parse_start_params(params)
sample_id = parsed.get('sample_id', '样品名称')
start_theta = float(parsed.get('start_theta', 10.0))
end_theta = float(parsed.get('end_theta', 80.0))
increment = float(parsed.get('increment', 0.05))
exp_time = float(parsed.get('exp_time', 0.1))
wait_minutes = float(parsed.get('wait_minutes', 3.0))
return self.start(
sample_id=sample_id,
start_theta=start_theta,
end_theta=end_theta,
increment=increment,
exp_time=exp_time,
wait_minutes=wait_minutes,
)
# 测试函数
def test_xrd_client():
"""
测试XRD客户端功能
"""
client = XRDClient(host='127.0.0.1', port=6001)
try:
# 测试连接
client.connect()
print("连接成功")
# 测试启动自动模式
result = client.start_auto_mode(True)
print(f"启动自动模式: {result}")
# 测试上样请求
result = client.get_sample_request()
print(f"上样请求: {result}")
# 测试获取样品状态
result = client.get_sample_status()
print(f"样品状态: {result}")
# 测试高压开启
result = client.set_power_on()
print(f"高压开启: {result}")
except Exception as e:
print(f"测试失败: {e}")
finally:
client.close()
if __name__ == "__main__":
test_xrd_client()
# 为了兼容性,提供别名
XRD_D7Mate = XRDClient

View File

@@ -0,0 +1,86 @@
opsky_ATR30007:
category:
- characterization_optic
- opsky_ATR30007
class:
action_value_mappings:
auto-run_once:
feedback: {}
goal: {}
goal_default:
integration_time: '5000'
laser_power: '200'
norm_max: '1.0'
normalize: 'true'
save_csv: 'true'
save_plot: 'true'
handles: {}
result: {}
schema:
description: 执行一次站控-扫码-拉曼流程的大函数入口,参数以字符串形式传入。
properties:
feedback: {}
goal:
properties:
integration_time:
default: '5000'
type: string
laser_power:
default: '200'
type: string
norm_max:
default: '1.0'
type: string
normalize:
default: 'true'
type: string
save_csv:
default: 'true'
type: string
save_plot:
default: 'true'
type: string
required: []
type: object
result: {}
required: []
title: run_once 参数
type: object
type: UniLabJsonCommand
module: unilabos.devices.opsky_Raman.opsky_ATR30007:opsky_ATR30007
status_types: {}
type: python
config_info: []
description: OPSKY ATR30007 光纤拉曼模块,提供单一入口大函数以执行一次完整流程。
handles: []
icon: ''
init_param_schema:
config:
properties:
plc_ip:
default: 192.168.1.88
type: string
plc_port:
default: 502
type: integer
robot_ip:
default: 192.168.1.200
type: string
robot_port:
default: 502
type: integer
scan_csv_file:
default: scan_results.csv
type: string
required:
- plc_ip
- plc_port
- robot_ip
- robot_port
- scan_csv_file
type: object
data:
properties: {}
required: []
type: object
version: 1.0.0

View File

@@ -0,0 +1,557 @@
xrd_d7mate:
category:
- xrd_d7mate
class:
action_value_mappings:
auto-close:
feedback: {}
goal: {}
goal_default: {}
handles: {}
result: {}
schema:
description: 安全关闭与XRD D7-Mate设备的TCP连接释放网络资源。
properties:
feedback: {}
goal:
properties: {}
required: []
type: object
result: {}
required:
- goal
title: close参数
type: object
type: UniLabJsonCommand
auto-connect:
feedback: {}
goal: {}
goal_default: {}
handles: {}
result: {}
schema:
description: 与XRD D7-Mate设备建立TCP连接配置超时参数。
properties:
feedback: {}
goal:
properties: {}
required: []
type: object
result: {}
required:
- goal
title: connect参数
type: object
type: UniLabJsonCommand
auto-post_init:
feedback: {}
goal: {}
goal_default:
ros_node: null
handles: {}
result: {}
schema:
description: ''
properties:
feedback: {}
goal:
properties:
ros_node:
type: string
required:
- ros_node
type: object
result: {}
required:
- goal
title: post_init参数
type: object
type: UniLabJsonCommand
get_current_acquire_data:
feedback: {}
goal: {}
goal_default: {}
handles: {}
result: {}
schema:
description: ''
properties:
feedback:
properties: {}
required: []
title: EmptyIn_Feedback
type: object
goal:
properties: {}
required: []
title: EmptyIn_Goal
type: object
result:
properties:
return_info:
type: string
required:
- return_info
title: EmptyIn_Result
type: object
required:
- goal
title: EmptyIn
type: object
type: EmptyIn
get_sample_down:
feedback: {}
goal:
sample_station: 1
goal_default:
int_input: 0
handles: {}
result: {}
schema:
description: ''
properties:
feedback:
properties: {}
required: []
title: IntSingleInput_Feedback
type: object
goal:
properties:
int_input:
maximum: 2147483647
minimum: -2147483648
type: integer
required:
- int_input
title: IntSingleInput_Goal
type: object
result:
properties:
return_info:
type: string
success:
type: boolean
required:
- return_info
- success
title: IntSingleInput_Result
type: object
required:
- goal
title: IntSingleInput
type: object
type: IntSingleInput
get_sample_request:
feedback: {}
goal: {}
goal_default: {}
handles: {}
result: {}
schema:
description: ''
properties:
feedback:
properties: {}
required: []
title: EmptyIn_Feedback
type: object
goal:
properties: {}
required: []
title: EmptyIn_Goal
type: object
result:
properties:
return_info:
type: string
required:
- return_info
title: EmptyIn_Result
type: object
required:
- goal
title: EmptyIn
type: object
type: EmptyIn
get_sample_status:
feedback: {}
goal: {}
goal_default: {}
handles: {}
result: {}
schema:
description: ''
properties:
feedback:
properties: {}
required: []
title: EmptyIn_Feedback
type: object
goal:
properties: {}
required: []
title: EmptyIn_Goal
type: object
result:
properties:
return_info:
type: string
required:
- return_info
title: EmptyIn_Result
type: object
required:
- goal
title: EmptyIn
type: object
type: EmptyIn
send_sample_down_ready:
feedback: {}
goal: {}
goal_default: {}
handles: {}
result: {}
schema:
description: ''
properties:
feedback:
properties: {}
required: []
title: EmptyIn_Feedback
type: object
goal:
properties: {}
required: []
title: EmptyIn_Goal
type: object
result:
properties:
return_info:
type: string
required:
- return_info
title: EmptyIn_Result
type: object
required:
- goal
title: EmptyIn
type: object
type: EmptyIn
send_sample_ready:
feedback: {}
goal:
end_theta: 80.0
exp_time: 0.5
increment: 0.02
sample_id: ''
start_theta: 10.0
goal_default:
end_theta: 80.0
exp_time: 0.5
increment: 0.02
sample_id: Sample001
start_theta: 10.0
handles: {}
result: {}
schema:
description: 送样完成后,发送样品信息和采集参数
properties:
feedback:
properties: {}
required: []
title: SampleReadyInput_Feedback
type: object
goal:
properties:
end_theta:
description: 结束角度≥5.5°且必须大于start_theta
minimum: 5.5
type: number
exp_time:
description: 曝光时间0.1-5.0秒)
maximum: 5.0
minimum: 0.1
type: number
increment:
description: 角度增量≥0.005
minimum: 0.005
type: number
sample_id:
description: 样品标识符
type: string
start_theta:
description: 起始角度≥5°
minimum: 5.0
type: number
required:
- sample_id
- start_theta
- end_theta
- increment
- exp_time
title: SampleReadyInput_Goal
type: object
result:
properties:
return_info:
type: string
success:
type: boolean
required:
- return_info
- success
title: SampleReadyInput_Result
type: object
required:
- goal
title: SampleReadyInput
type: object
type: UniLabJsonCommand
set_power_off:
feedback: {}
goal: {}
goal_default: {}
handles: {}
result: {}
schema:
description: ''
properties:
feedback:
properties: {}
required: []
title: EmptyIn_Feedback
type: object
goal:
properties: {}
required: []
title: EmptyIn_Goal
type: object
result:
properties:
return_info:
type: string
required:
- return_info
title: EmptyIn_Result
type: object
required:
- goal
title: EmptyIn
type: object
type: EmptyIn
set_power_on:
feedback: {}
goal: {}
goal_default: {}
handles: {}
result: {}
schema:
description: ''
properties:
feedback:
properties: {}
required: []
title: EmptyIn_Feedback
type: object
goal:
properties: {}
required: []
title: EmptyIn_Goal
type: object
result:
properties:
return_info:
type: string
required:
- return_info
title: EmptyIn_Result
type: object
required:
- goal
title: EmptyIn
type: object
type: EmptyIn
set_voltage_current:
feedback: {}
goal:
current: 30.0
voltage: 40.0
goal_default:
current: 30.0
voltage: 40.0
handles: {}
result: {}
schema:
description: 设置高压电源电压和电流
properties:
feedback:
properties: {}
required: []
title: VoltageCurrentInput_Feedback
type: object
goal:
properties:
current:
description: 电流值mA
type: number
voltage:
description: 电压值kV
type: number
required:
- voltage
- current
title: VoltageCurrentInput_Goal
type: object
result:
properties:
return_info:
type: string
success:
type: boolean
required:
- return_info
- success
title: VoltageCurrentInput_Result
type: object
required:
- goal
title: VoltageCurrentInput
type: object
type: UniLabJsonCommand
start:
feedback: {}
goal: {}
goal_default:
end_theta: 80.0
exp_time: 0.1
increment: 0.05
sample_id: 样品名称
start_theta: 10.0
string: ''
wait_minutes: 3.0
handles: {}
result: {}
schema:
description: 启动自动模式→上样→等待→样品准备→监控→检测下样位→执行下样流程。
properties:
feedback: {}
goal:
properties:
end_theta:
description: 结束角度≥5.5°且必须大于start_theta
minimum: 5.5
type: string
exp_time:
description: 曝光时间0.1-5.0秒)
maximum: 5.0
minimum: 0.1
type: string
increment:
description: 角度增量≥0.005
minimum: 0.005
type: string
sample_id:
description: 样品标识符
type: string
start_theta:
description: 起始角度≥5°
minimum: 5.0
type: string
string:
description: 字符串格式的参数输入,如果提供则优先解析使用
type: string
wait_minutes:
description: 允许上样后等待分钟数
minimum: 0.0
type: number
required:
- sample_id
- start_theta
- end_theta
- increment
- exp_time
title: StartWorkflow_Goal
type: object
result:
properties:
return_info:
type: string
success:
type: boolean
required:
- return_info
- success
title: StartWorkflow_Result
type: object
required:
- goal
title: StartWorkflow
type: object
type: UniLabJsonCommand
start_auto_mode:
feedback: {}
goal:
status: true
goal_default:
status: true
handles: {}
result: {}
schema:
description: 启动或停止自动模式
properties:
feedback:
properties: {}
required: []
title: BoolSingleInput_Feedback
type: object
goal:
properties:
status:
description: True-启动自动模式False-停止自动模式
type: boolean
required:
- status
title: BoolSingleInput_Goal
type: object
result:
properties:
return_info:
type: string
success:
type: boolean
required:
- return_info
- success
title: BoolSingleInput_Result
type: object
required:
- goal
title: BoolSingleInput
type: object
type: UniLabJsonCommand
module: unilabos.devices.xrd_d7mate.xrd_d7mate:XRDClient
status_types: {}
type: python
config_info: []
description: XRD D7-Mate X射线衍射分析设备通过TCP通信实现远程控制与状态监控支持自动模式控制、上样流程、数据获取、下样流程和高压电源控制等功能。
handles: []
icon: ''
init_param_schema:
config:
properties:
host:
default: 127.0.0.1
type: string
port:
default: 6001
type: string
timeout:
default: 10.0
type: string
required: []
type: object
data:
properties: {}
required: []
type: object
version: 1.0.0

View File

@@ -48,3 +48,25 @@ BIOYOND_PolymerStation_Solution_Beaker:
icon: ''
init_param_schema: {}
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
BIOYOND_PolymerStation_Reactor:
category:
- bottles
- reactors
class:
module: unilabos.resources.bioyond.bottles:BIOYOND_PolymerStation_Reactor
type: pylabrobot
handles: []
icon: ''
init_param_schema: {}
version: 1.0.0

View File

@@ -90,3 +90,89 @@ def BIOYOND_PolymerStation_Reagent_Bottle(
barcode=barcode,
model="BIOYOND_PolymerStation_Reagent_Bottle",
)
def BIOYOND_PolymerStation_Reactor(
name: str,
diameter: float = 30.0,
height: float = 80.0,
max_volume: float = 50000.0, # 50mL
barcode: str = None,
) -> Bottle:
"""创建反应器"""
return Bottle(
name=name,
diameter=diameter,
height=height,
max_volume=max_volume,
barcode=barcode,
model="BIOYOND_PolymerStation_Reactor",
)
def BIOYOND_PolymerStation_TipBox(
name: str,
size_x: float = 127.76, # 枪头盒宽度
size_y: float = 85.48, # 枪头盒长度
size_z: float = 100.0, # 枪头盒高度
barcode: str = None,
):
"""创建4×6枪头盒 (24个枪头)
Args:
name: 枪头盒名称
size_x: 枪头盒宽度 (mm)
size_y: 枪头盒长度 (mm)
size_z: 枪头盒高度 (mm)
barcode: 条形码
Returns:
TipBoxCarrier: 包含24个枪头孔位的枪头盒
"""
from pylabrobot.resources import Container, Coordinate
# 创建枪头盒容器
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

View File

@@ -1,7 +1,22 @@
from os import name
from pylabrobot.resources import Deck, Coordinate, Rotation
from unilabos.resources.bioyond.warehouses import bioyond_warehouse_1x4x4, bioyond_warehouse_1x4x2, bioyond_warehouse_liquid_and_lid_handling, bioyond_warehouse_1x2x2, bioyond_warehouse_1x3x3, bioyond_warehouse_10x1x1, bioyond_warehouse_3x3x1, bioyond_warehouse_3x3x1_2, bioyond_warehouse_5x1x1
from unilabos.resources.bioyond.warehouses import (
bioyond_warehouse_1x4x4,
bioyond_warehouse_1x4x4_right, # 新增:右侧仓库 (A05D08)
bioyond_warehouse_1x4x2,
bioyond_warehouse_liquid_and_lid_handling,
bioyond_warehouse_1x2x2,
bioyond_warehouse_1x3x3,
bioyond_warehouse_10x1x1,
bioyond_warehouse_3x3x1,
bioyond_warehouse_3x3x1_2,
bioyond_warehouse_5x1x1,
bioyond_warehouse_1x8x4,
bioyond_warehouse_reagent_storage,
bioyond_warehouse_liquid_preparation,
bioyond_warehouse_tipbox_storage, # 新增Tip盒堆栈
)
class BIOYOND_PolymerReactionStation_Deck(Deck):
@@ -20,15 +35,22 @@ class BIOYOND_PolymerReactionStation_Deck(Deck):
def setup(self) -> None:
# 添加仓库
# 说明: 堆栈1物理上分为左右两部分
# - 堆栈1左: A01D04 (4行×4列, 位于反应站左侧)
# - 堆栈1右: A05D08 (4行×4列, 位于反应站右侧)
self.warehouses = {
"堆栈1": bioyond_warehouse_1x4x4("堆栈1"),
"堆栈2": bioyond_warehouse_1x4x4("堆栈2"),
"站内试剂存放堆栈": bioyond_warehouse_liquid_and_lid_handling("站内试剂存放堆栈"),
"堆栈1": bioyond_warehouse_1x4x4("堆栈1"), # 左侧堆栈: A01D04
"堆栈1右": bioyond_warehouse_1x4x4_right("堆栈1右"), # 右侧堆栈: A05D08
"站内试剂存放堆栈": bioyond_warehouse_reagent_storage("站内试剂存放堆栈"), # A01A02
"移液站内10%分装液体准备仓库": bioyond_warehouse_liquid_preparation("移液站内10%分装液体准备仓库"), # A01B04
"站内Tip盒堆栈": bioyond_warehouse_tipbox_storage("站内Tip盒堆栈"), # A01B03, 存放枪头盒
}
self.warehouse_locations = {
"堆栈1": Coordinate(0.0, 430.0, 0.0),
"堆栈2": Coordinate(2550.0, 430.0, 0.0),
"站内试剂存放堆栈": Coordinate(800.0, 475.0, 0.0),
"堆栈1": Coordinate(0.0, 430.0, 0.0), # 左侧位置
"堆栈1右": Coordinate(2500.0, 430.0, 0.0), # 右侧位置
"站内试剂存放堆栈": Coordinate(1100.0, 475.0, 0.0),
"移液站内10%分装液体准备仓库": Coordinate(1500.0, 300.0, 0.0),
"站内Tip盒堆栈": Coordinate(1800.0, 300.0, 0.0), # TODO: 根据实际位置调整坐标
}
self.warehouses["站内试剂存放堆栈"].rotation = Rotation(z=90)

View File

@@ -2,7 +2,7 @@ from unilabos.resources.warehouse import WareHouse, warehouse_factory
def bioyond_warehouse_1x4x4(name: str) -> WareHouse:
"""创建BioYond 4x1x4仓库"""
"""创建BioYond 4x4x1仓库 (左侧堆栈: A01D04)"""
return warehouse_factory(
name=name,
num_items_x=4,
@@ -15,6 +15,25 @@ def bioyond_warehouse_1x4x4(name: str) -> WareHouse:
item_dy=106.0,
item_dz=130.0,
category="warehouse",
col_offset=0, # 从01开始: A01, A02, A03, A04
)
def bioyond_warehouse_1x4x4_right(name: str) -> WareHouse:
"""创建BioYond 4x4x1仓库 (右侧堆栈: A05D08)"""
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,
category="warehouse",
col_offset=4, # 从05开始: A05, A06, A07, A08
)
@@ -159,3 +178,71 @@ def bioyond_warehouse_liquid_and_lid_handling(name: str) -> WareHouse:
category="warehouse",
removed_positions=None
)
def bioyond_warehouse_1x8x4(name: str) -> WareHouse:
"""创建BioYond 8x4x1反应站堆栈A01D08"""
return warehouse_factory(
name=name,
num_items_x=8, # 8列01-08
num_items_y=4, # 4行A-D
num_items_z=1, # 1层
dx=10.0,
dy=10.0,
dz=10.0,
item_dx=147.0,
item_dy=106.0,
item_dz=130.0,
category="warehouse",
)
def bioyond_warehouse_reagent_storage(name: str) -> WareHouse:
"""创建BioYond站内试剂存放堆栈A01A02, 1行×2列"""
return warehouse_factory(
name=name,
num_items_x=2, # 2列01-02
num_items_y=1, # 1行A
num_items_z=1, # 1层
dx=10.0,
dy=10.0,
dz=10.0,
item_dx=137.0,
item_dy=96.0,
item_dz=120.0,
category="warehouse",
)
def bioyond_warehouse_liquid_preparation(name: str) -> WareHouse:
"""创建BioYond移液站内10%分装液体准备仓库A01B04"""
return warehouse_factory(
name=name,
num_items_x=4, # 4列01-04
num_items_y=2, # 2行A-B
num_items_z=1, # 1层
dx=10.0,
dy=10.0,
dz=10.0,
item_dx=137.0,
item_dy=96.0,
item_dz=120.0,
category="warehouse",
)
def bioyond_warehouse_tipbox_storage(name: str) -> WareHouse:
"""创建BioYond站内Tip盒堆栈A01B03用于存放枪头盒"""
return warehouse_factory(
name=name,
num_items_x=3, # 3列01-03
num_items_y=2, # 2行A-B
num_items_z=1, # 1层
dx=10.0,
dy=10.0,
dz=10.0,
item_dx=137.0,
item_dy=96.0,
item_dz=120.0,
category="warehouse",
)

View File

@@ -580,6 +580,8 @@ def resource_plr_to_ulab(resource_plr: "ResourcePLR", parent_name: str = None, w
"trash": "trash",
"deck": "deck",
"tip_rack": "tip_rack",
"warehouse": "warehouse",
"container": "container",
}
if source in replace_info:
return replace_info[source]
@@ -632,9 +634,24 @@ def resource_bioyond_to_plr(bioyond_materials: list[dict], type_mapping: Dict[st
type_mapping.get(material.get("typeName"), ("RegularContainer", ""))[0] if type_mapping else "RegularContainer"
)
plr_material: ResourcePLR = initialize_resource(
plr_material_result = initialize_resource(
{"name": material["name"], "class": className}, resource_type=ResourcePLR
)
# initialize_resource 可能返回列表或单个对象
if isinstance(plr_material_result, list):
if len(plr_material_result) == 0:
logger.warning(f"物料 {material['name']} 初始化失败,跳过")
continue
plr_material = plr_material_result[0]
else:
plr_material = plr_material_result
# 确保 plr_material 是 ResourcePLR 实例
if not isinstance(plr_material, ResourcePLR):
logger.warning(f"物料 {material['name']} 不是有效的 ResourcePLR 实例,类型: {type(plr_material)}")
continue
plr_material.code = material.get("code", "") and material.get("barCode", "") or ""
plr_material.unilabos_uuid = str(uuid.uuid4())
@@ -659,6 +676,8 @@ def resource_bioyond_to_plr(bioyond_materials: list[dict], type_mapping: Dict[st
]
bottle.code = detail.get("code", "")
else:
# 只对有 capacity 属性的容器(液体容器)处理液体追踪
if hasattr(plr_material, 'capacity'):
bottle = plr_material[0] if plr_material.capacity > 0 else plr_material
bottle.tracker.liquids = [
(material["name"], float(material.get("quantity", 0)) if material.get("quantity") else 0)
@@ -668,16 +687,55 @@ def resource_bioyond_to_plr(bioyond_materials: list[dict], type_mapping: Dict[st
if deck and hasattr(deck, "warehouses"):
for loc in material.get("locations", []):
if hasattr(deck, "warehouses") and loc.get("whName") in deck.warehouses:
warehouse = deck.warehouses[loc["whName"]]
idx = (
(loc.get("y", 0) - 1) * warehouse.num_items_x * warehouse.num_items_y
+ (loc.get("x", 0) - 1) * warehouse.num_items_x
+ (loc.get("z", 0) - 1)
)
wh_name = loc.get("whName")
# 特殊处理: Bioyond的"堆栈1"需要映射到"堆栈1左"或"堆栈1右"
# 根据列号(x)判断: 1-4映射到左侧, 5-8映射到右侧
if wh_name == "堆栈1":
x_val = loc.get("x", 1)
if 1 <= x_val <= 4:
wh_name = "堆栈1左"
elif 5 <= x_val <= 8:
wh_name = "堆栈1右"
else:
logger.warning(f"物料 {material['name']} 的列号 x={x_val} 超出范围无法映射到堆栈1左或堆栈1右")
continue
if hasattr(deck, "warehouses") and wh_name in deck.warehouses:
warehouse = deck.warehouses[wh_name]
# Bioyond坐标映射 (重要!): x→行(1=A,2=B...), y→列(1=01,2=02...), z→层(通常=1)
# PyLabRobot warehouse是列优先存储: A01,B01,C01,D01, A02,B02,C02,D02, ...
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
# 特殊处理对于1行×N列的横向warehouse如站内试剂存放堆栈
# Bioyond的y坐标表示线性位置序号而不是列号
if warehouse.num_items_y == 1:
# 1行warehouse: 直接用y作为线性索引
idx = y - 1
logger.debug(f"1行warehouse {wh_name}: y={y} → idx={idx}")
else:
# 多行warehouse: 使用列优先索引 (与Bioyond坐标系统一致)
# warehouse keys顺序: A01,B01,C01,D01, A02,B02,C02,D02, ...
# 索引计算: idx = (col-1) * num_rows + (row-1) + (layer-1) * (rows * cols)
row_idx = x - 1 # x表示行: 转为0-based
col_idx = y - 1 # y表示列: 转为0-based
layer_idx = z - 1 # 转为0-based
idx = layer_idx * (warehouse.num_items_x * warehouse.num_items_y) + col_idx * warehouse.num_items_y + row_idx
logger.debug(f"多行warehouse {wh_name}: x={x}(行),y={y}(列) → row={row_idx},col={col_idx} → idx={idx}")
if 0 <= idx < warehouse.capacity:
if warehouse[idx] is None or isinstance(warehouse[idx], ResourceHolder):
warehouse[idx] = plr_material
logger.debug(f"✅ 物料 {material['name']} 放置到 {wh_name}[{idx}] (Bioyond坐标: x={loc.get('x')}, y={loc.get('y')})")
else:
logger.warning(f"物料 {material['name']} 的索引 {idx} 超出仓库 {wh_name} 容量 {warehouse.capacity}")
return plr_materials
@@ -714,8 +772,8 @@ def resource_plr_to_bioyond(plr_resources: list[ResourcePLR], type_mapping: dict
bottle = resource[0] if resource.capacity > 0 else resource
material = {
"typeId": "3a14196b-24f2-ca49-9081-0cab8021bf1a",
"name": resource.get("name", ""),
"unit": "",
"name": resource.name if hasattr(resource, "name") else "",
"unit": "", # 修复Bioyond API 要求 unit 字段不能为空
"quantity": sum(qty for _, qty in bottle.tracker.liquids) if hasattr(bottle, "tracker") else 0,
"Parameters": "{}"
}
@@ -759,6 +817,8 @@ def initialize_resource(resource_config: dict, resource_type: Any = None) -> Uni
elif type(resource_class_config) == str:
# Allow special resource class names to be used
if resource_class_config not in lab_registry.resource_type_registry:
logger.warning(f"❌ 类 {resource_class_config} 不在 registry 中,返回原始配置")
logger.debug(f" 可用的类: {list(lab_registry.resource_type_registry.keys())[:10]}...")
return [resource_config]
# If the resource class is a string, look up the class in the
# resource_type_registry and import it

View File

@@ -23,6 +23,7 @@ def warehouse_factory(
empty: bool = False,
category: str = "warehouse",
model: Optional[str] = None,
col_offset: int = 0, # 新增列起始偏移量用于生成A05-D08等命名
):
# 创建16个板架位 (4层 x 4位置)
locations = []
@@ -44,7 +45,9 @@ def warehouse_factory(
name_prefix=name,
)
len_x, len_y = (num_items_x, num_items_y) if num_items_z == 1 else (num_items_y, num_items_z) if num_items_x == 1 else (num_items_x, num_items_z)
keys = [f"{LETTERS[j]}{i + 1}" for i in range(len_x) for j in range(len_y)]
# 应用列偏移量支持A05-D08等命名
# 使用列优先顺序生成keys (与Bioyond坐标系统一致): A01,B01,C01,D01, A02,B02,C02,D02, ...
keys = [f"{LETTERS[j]}{i + 1 + col_offset:02d}" for i in range(len_x) for j in range(len_y)]
sites = {i: site for i, site in zip(keys, _sites.values())}
return WareHouse(

View File

@@ -53,7 +53,7 @@ from unilabos.ros.nodes.resource_tracker import (
)
from unilabos.ros.x.rclpyx import get_event_loop
from unilabos.ros.utils.driver_creator import WorkstationNodeCreator, PyLabRobotCreator, DeviceClassCreator
from unilabos.utils.async_util import run_async_func
from rclpy.task import Task, Future
from unilabos.utils.import_manager import default_manager
from unilabos.utils.log import info, debug, warning, error, critical, logger, trace
from unilabos.utils.type_check import get_type_class, TypeEncoder, get_result_info_str
@@ -555,6 +555,15 @@ class BaseROS2DeviceNode(Node, Generic[T]):
rclpy.get_global_executor().add_node(self)
self.lab_logger().debug(f"ROS节点初始化完成")
async def sleep(self, rel_time: float, callback_group=None):
if callback_group is None:
callback_group = self.callback_group
await ROS2DeviceNode.async_wait_for(self, rel_time, callback_group)
@classmethod
async def create_task(cls, func, trace_error=True, **kwargs) -> Task:
return ROS2DeviceNode.run_async_func(func, trace_error, **kwargs)
async def update_resource(self, resources: List["ResourcePLR"]):
r = SerialCommand.Request()
tree_set = ResourceTreeSet.from_plr_resources(resources)
@@ -647,7 +656,7 @@ class BaseROS2DeviceNode(Node, Generic[T]):
].call_async(
SerialCommand.Request(
command=json.dumps(
{"data": {"data": resources_uuid, "with_children": False}, "action": "get"}
{"data": {"data": resources_uuid, "with_children": True if action == "add" else "update"}, "action": "get"}
)
)
) # type: ignore
@@ -1385,18 +1394,27 @@ class ROS2DeviceNode:
它不继承设备类,而是通过代理模式访问设备类的属性和方法。
"""
# 类变量,用于循环管理
_loop = None
_loop_running = False
_loop_thread = None
@classmethod
def run_async_func(cls, func, trace_error=True, **kwargs) -> Task:
def _handle_future_exception(fut):
try:
fut.result()
except Exception as e:
error(f"异步任务 {func.__name__} 报错了")
error(traceback.format_exc())
future = rclpy.get_global_executor().create_task(func(**kwargs))
if trace_error:
future.add_done_callback(_handle_future_exception)
return future
@classmethod
def get_loop(cls):
return cls._loop
@classmethod
def run_async_func(cls, func, trace_error=True, **kwargs):
return run_async_func(func, loop=cls._loop, trace_error=trace_error, **kwargs)
async def async_wait_for(cls, node: Node, wait_time: float, callback_group=None):
future = Future()
timer = node.create_timer(wait_time, lambda : future.set_result(None), callback_group=callback_group, clock=node.get_clock())
await future
timer.cancel()
node.destroy_timer(timer)
@property
def driver_instance(self):
@@ -1436,11 +1454,6 @@ class ROS2DeviceNode:
print_publish: 是否打印发布信息
driver_is_ros:
"""
# 在初始化时检查循环状态
if ROS2DeviceNode._loop_running and ROS2DeviceNode._loop_thread is not None:
pass
elif ROS2DeviceNode._loop_thread is None:
self._start_loop()
# 保存设备类是否支持异步上下文
self._has_async_context = hasattr(driver_class, "__aenter__") and hasattr(driver_class, "__aexit__")
@@ -1529,17 +1542,6 @@ class ROS2DeviceNode:
except Exception as e:
self._ros_node.lab_logger().error(f"设备后初始化失败: {e}")
def _start_loop(self):
def run_event_loop():
loop = asyncio.new_event_loop()
ROS2DeviceNode._loop = loop
asyncio.set_event_loop(loop)
loop.run_forever()
ROS2DeviceNode._loop_thread = threading.Thread(target=run_event_loop, daemon=True, name="ROS2DeviceNodeLoop")
ROS2DeviceNode._loop_thread.start()
logger.info(f"循环线程已启动")
class DeviceInfoType(TypedDict):
id: str

View File

@@ -18,7 +18,8 @@ from unilabos_msgs.srv import (
ResourceDelete,
ResourceUpdate,
ResourceList,
SerialCommand, ResourceGet,
SerialCommand,
ResourceGet,
) # type: ignore
from unilabos_msgs.srv._serial_command import SerialCommand_Request, SerialCommand_Response
from unique_identifier_msgs.msg import UUID

View File

@@ -848,9 +848,15 @@ class DeviceNodeResourceTracker(object):
extra: extra字典值
"""
if isinstance(resource, dict):
resource["extra"] = extra
# ⭐ 修复合并extra而不是覆盖
current_extra = resource.get("extra", {})
current_extra.update(extra)
resource["extra"] = current_extra
else:
setattr(resource, "unilabos_extra", extra)
# ⭐ 修复合并unilabos_extra而不是覆盖
current_extra = getattr(resource, "unilabos_extra", {})
current_extra.update(extra)
setattr(resource, "unilabos_extra", current_extra)
def _traverse_and_process(self, resource, process_func) -> int:
"""

View File

@@ -1,22 +0,0 @@
import asyncio
import traceback
from asyncio import get_event_loop
from unilabos.utils.log import error
def run_async_func(func, *, loop=None, trace_error=True, **kwargs):
if loop is None:
loop = get_event_loop()
def _handle_future_exception(fut):
try:
fut.result()
except Exception as e:
error(f"异步任务 {func.__name__} 报错了")
error(traceback.format_exc())
future = asyncio.run_coroutine_threadsafe(func(**kwargs), loop)
if trace_error:
future.add_done_callback(_handle_future_exception)
return future