mirror of
https://github.com/dptech-corp/Uni-Lab-OS.git
synced 2026-02-06 06:25:06 +00:00
修复了很多protocol,亲测能跑
This commit is contained in:
@@ -1,6 +1,28 @@
|
||||
from typing import List, Dict, Any
|
||||
import networkx as nx
|
||||
|
||||
|
||||
def find_connected_stirrer(G: nx.DiGraph, vessel: str = None) -> str:
|
||||
"""
|
||||
查找与指定容器相连的搅拌设备,或查找可用的搅拌设备
|
||||
"""
|
||||
# 查找所有搅拌设备节点
|
||||
stirrer_nodes = [node for node in G.nodes()
|
||||
if (G.nodes[node].get('class') or '') == 'virtual_stirrer']
|
||||
|
||||
if vessel:
|
||||
# 检查哪个搅拌设备与目标容器相连(机械连接)
|
||||
for stirrer in stirrer_nodes:
|
||||
if G.has_edge(stirrer, vessel) or G.has_edge(vessel, stirrer):
|
||||
return stirrer
|
||||
|
||||
# 如果没有指定容器或没有直接连接,返回第一个可用的搅拌设备
|
||||
if stirrer_nodes:
|
||||
return stirrer_nodes[0]
|
||||
|
||||
raise ValueError("系统中未找到可用的搅拌设备")
|
||||
|
||||
|
||||
def generate_stir_protocol(
|
||||
G: nx.DiGraph,
|
||||
stir_time: float,
|
||||
@@ -8,37 +30,24 @@ def generate_stir_protocol(
|
||||
settling_time: float
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
生成搅拌操作的协议序列
|
||||
|
||||
Args:
|
||||
G: 有向图,节点为设备和容器
|
||||
stir_time: 搅拌时间 (秒)
|
||||
stir_speed: 搅拌速度 (rpm)
|
||||
settling_time: 沉降时间 (秒)
|
||||
|
||||
Returns:
|
||||
List[Dict[str, Any]]: 搅拌操作的动作序列
|
||||
|
||||
Raises:
|
||||
ValueError: 当找不到搅拌设备时抛出异常
|
||||
|
||||
Examples:
|
||||
stir_protocol = generate_stir_protocol(G, 300.0, 500.0, 60.0)
|
||||
生成搅拌操作的协议序列 - 定时搅拌 + 沉降
|
||||
"""
|
||||
action_sequence = []
|
||||
|
||||
print(f"STIR: 开始生成搅拌协议")
|
||||
print(f" - 搅拌时间: {stir_time}秒")
|
||||
print(f" - 搅拌速度: {stir_speed} RPM")
|
||||
print(f" - 沉降时间: {settling_time}秒")
|
||||
|
||||
# 查找搅拌设备
|
||||
stirrer_nodes = [node for node in G.nodes()
|
||||
if G.nodes[node].get('class') == 'virtual_stirrer']
|
||||
|
||||
if not stirrer_nodes:
|
||||
raise ValueError("没有找到可用的搅拌设备")
|
||||
|
||||
# 使用第一个可用的搅拌器
|
||||
stirrer_id = stirrer_nodes[0]
|
||||
try:
|
||||
stirrer_id = find_connected_stirrer(G)
|
||||
print(f"STIR: 找到搅拌设备: {stirrer_id}")
|
||||
except ValueError as e:
|
||||
raise ValueError(f"无法找到搅拌设备: {str(e)}")
|
||||
|
||||
# 执行搅拌操作
|
||||
action_sequence.append({
|
||||
stir_action = {
|
||||
"device_id": stirrer_id,
|
||||
"action_name": "stir",
|
||||
"action_kwargs": {
|
||||
@@ -46,8 +55,11 @@ def generate_stir_protocol(
|
||||
"stir_speed": stir_speed,
|
||||
"settling_time": settling_time
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
action_sequence.append(stir_action)
|
||||
|
||||
print(f"STIR: 生成了 {len(action_sequence)} 个动作")
|
||||
return action_sequence
|
||||
|
||||
|
||||
@@ -58,33 +70,28 @@ def generate_start_stir_protocol(
|
||||
purpose: str
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
生成开始搅拌操作的协议序列
|
||||
|
||||
Args:
|
||||
G: 有向图,节点为设备和容器
|
||||
vessel: 搅拌容器
|
||||
stir_speed: 搅拌速度 (rpm)
|
||||
purpose: 搅拌目的
|
||||
|
||||
Returns:
|
||||
List[Dict[str, Any]]: 开始搅拌操作的动作序列
|
||||
生成开始搅拌操作的协议序列 - 持续搅拌
|
||||
"""
|
||||
action_sequence = []
|
||||
|
||||
# 查找搅拌设备
|
||||
stirrer_nodes = [node for node in G.nodes()
|
||||
if G.nodes[node].get('class') == 'virtual_stirrer']
|
||||
print(f"START_STIR: 开始生成启动搅拌协议")
|
||||
print(f" - 容器: {vessel}")
|
||||
print(f" - 搅拌速度: {stir_speed} RPM")
|
||||
print(f" - 目的: {purpose}")
|
||||
|
||||
if not stirrer_nodes:
|
||||
raise ValueError("没有找到可用的搅拌设备")
|
||||
|
||||
stirrer_id = stirrer_nodes[0]
|
||||
|
||||
# 验证容器是否存在
|
||||
# 验证容器存在
|
||||
if vessel not in G.nodes():
|
||||
raise ValueError(f"容器 {vessel} 不存在于图中")
|
||||
raise ValueError(f"容器 '{vessel}' 不存在于系统中")
|
||||
|
||||
action_sequence.append({
|
||||
# 查找搅拌设备
|
||||
try:
|
||||
stirrer_id = find_connected_stirrer(G, vessel)
|
||||
print(f"START_STIR: 找到搅拌设备: {stirrer_id}")
|
||||
except ValueError as e:
|
||||
raise ValueError(f"无法找到搅拌设备: {str(e)}")
|
||||
|
||||
# 执行开始搅拌操作
|
||||
start_stir_action = {
|
||||
"device_id": stirrer_id,
|
||||
"action_name": "start_stir",
|
||||
"action_kwargs": {
|
||||
@@ -92,8 +99,11 @@ def generate_start_stir_protocol(
|
||||
"stir_speed": stir_speed,
|
||||
"purpose": purpose
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
action_sequence.append(start_stir_action)
|
||||
|
||||
print(f"START_STIR: 生成了 {len(action_sequence)} 个动作")
|
||||
return action_sequence
|
||||
|
||||
|
||||
@@ -103,35 +113,54 @@ def generate_stop_stir_protocol(
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
生成停止搅拌操作的协议序列
|
||||
|
||||
Args:
|
||||
G: 有向图,节点为设备和容器
|
||||
vessel: 搅拌容器
|
||||
|
||||
Returns:
|
||||
List[Dict[str, Any]]: 停止搅拌操作的动作序列
|
||||
"""
|
||||
action_sequence = []
|
||||
|
||||
# 查找搅拌设备
|
||||
stirrer_nodes = [node for node in G.nodes()
|
||||
if G.nodes[node].get('class') == 'virtual_stirrer']
|
||||
print(f"STOP_STIR: 开始生成停止搅拌协议")
|
||||
print(f" - 容器: {vessel}")
|
||||
|
||||
if not stirrer_nodes:
|
||||
raise ValueError("没有找到可用的搅拌设备")
|
||||
|
||||
stirrer_id = stirrer_nodes[0]
|
||||
|
||||
# 验证容器是否存在
|
||||
# 验证容器存在
|
||||
if vessel not in G.nodes():
|
||||
raise ValueError(f"容器 {vessel} 不存在于图中")
|
||||
raise ValueError(f"容器 '{vessel}' 不存在于系统中")
|
||||
|
||||
action_sequence.append({
|
||||
# 查找搅拌设备
|
||||
try:
|
||||
stirrer_id = find_connected_stirrer(G, vessel)
|
||||
print(f"STOP_STIR: 找到搅拌设备: {stirrer_id}")
|
||||
except ValueError as e:
|
||||
raise ValueError(f"无法找到搅拌设备: {str(e)}")
|
||||
|
||||
# 执行停止搅拌操作
|
||||
stop_stir_action = {
|
||||
"device_id": stirrer_id,
|
||||
"action_name": "stop_stir",
|
||||
"action_kwargs": {
|
||||
"vessel": vessel
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return action_sequence
|
||||
action_sequence.append(stop_stir_action)
|
||||
|
||||
print(f"STOP_STIR: 生成了 {len(action_sequence)} 个动作")
|
||||
return action_sequence
|
||||
|
||||
|
||||
# 便捷函数
|
||||
def generate_fast_stir_protocol(
|
||||
G: nx.DiGraph,
|
||||
time: float = 300.0,
|
||||
speed: float = 800.0,
|
||||
settling: float = 60.0
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""快速搅拌的便捷函数"""
|
||||
return generate_stir_protocol(G, time, speed, settling)
|
||||
|
||||
|
||||
def generate_gentle_stir_protocol(
|
||||
G: nx.DiGraph,
|
||||
time: float = 600.0,
|
||||
speed: float = 200.0,
|
||||
settling: float = 120.0
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""温和搅拌的便捷函数"""
|
||||
return generate_stir_protocol(G, time, speed, settling)
|
||||
Reference in New Issue
Block a user