Update workstation code for YB4 0107

This commit is contained in:
Andy6M
2026-01-07 11:59:32 +08:00
parent 915a6a04c3
commit 936834f8c3
36 changed files with 3860 additions and 26826 deletions

View File

@@ -0,0 +1,107 @@
# 电池组装资源冲突问题修复说明
## 问题描述
在运行 `func_allpack_cmd` 函数时,遇到以下错误:
```
ValueError: Resource 'battery_0' already assigned to deck
```
**错误位置**`coin_cell_assembly.py` 第 849 行
```python
liaopan3.children[self.coin_num_N].assign_child_resource(battery, location=None)
```
## 原因分析
1. **资源名称冲突**
- 每次创建电池资源使用固定格式 `battery_{coin_num_N}`
- 如果程序重启或断点恢复,`coin_num_N` 可能重置为 0
- Deck 上可能已存在 `battery_0` 等同名资源
2. **缺少冲突处理**
- 在分配资源前没有检查目标位置是否已有资源
- 没有清理机制来移除旧资源
## 解决方案
### 1. 使用时间戳确保资源名称唯一
```python
# 之前
battery = ElectrodeSheet(name=f"battery_{self.coin_num_N}", ...)
# 修复后
timestamp_suffix = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
battery_name = f"battery_{self.coin_num_N}_{timestamp_suffix}"
battery = ElectrodeSheet(name=battery_name, ...)
```
### 2. 添加资源冲突检查和清理
```python
# 检查目标位置是否已有资源
target_slot = liaopan3.children[self.coin_num_N]
if target_slot.children:
logger.warning(f"位置 {self.coin_num_N} 已有资源,将先卸载旧资源")
try:
# 卸载所有现有子资源
for child in list(target_slot.children):
target_slot.unassign_child_resource(child)
logger.info(f"已卸载旧资源: {child.name}")
except Exception as e:
logger.error(f"卸载旧资源时出错: {e}")
```
### 3. 增强错误处理
```python
# 分配新资源到目标位置
try:
target_slot.assign_child_resource(battery, location=None)
logger.info(f"成功分配电池 {battery_name} 到位置 {self.coin_num_N}")
except Exception as e:
logger.error(f"分配电池资源失败: {e}")
raise
```
## 修复效果
**不再出现重复资源名称错误**
- 每个电池资源都有唯一的时间戳后缀
- 即使 `coin_num_N` 相同,资源名称也不会冲突
**自动清理旧资源**
- 在分配新资源前检查目标位置
- 自动卸载已存在的旧资源
**增强日志记录**
- 记录资源卸载操作
- 记录资源分配成功/失败
- 便于调试和问题追踪
## 测试建议
1. **正常运行测试**
```python
workstation.func_allpack_cmd(
elec_num=1,
elec_use_num=1,
elec_vol=20,
file_path="..."
)
```
2. **断点恢复测试**
- 运行一次后中断
- 再次运行相同参数
- 验证不会出现资源冲突错误
3. **连续运行测试**
- 连续多次运行
- 验证每次都能正常分配资源
## 相关文件
- `coin_cell_assembly.py` - 第 838-875 行(`func_pack_get_msg_cmd` 函数)