feat: 更新配置文件以启用电力自动启动;优化线径数据处理逻辑,增加测量历史记录和稳定性检查功能

This commit is contained in:
zhu-mengmeng 2025-07-17 11:01:07 +08:00
parent 3be6b964b5
commit a8c4a929ff
3 changed files with 157 additions and 126 deletions

View File

@ -116,7 +116,7 @@
}
},
"electricity": {
"auto_start": false,
"auto_start": true,
"interval_minutes": 30
}
}

View File

@ -1160,6 +1160,11 @@ class SerialManager:
# 转换为浮点数
xj_value = float(number_str)
logging.info(f"线径数据: {xj_value}")
if xj_value/10000 > 10 or xj_value/10000 == 0:
# 过滤无效值:>10 或 =0 的值直接跳过
# if xj_value/10000 > 10 or xj_value/10000 == 0:
# logging.info(f"过滤无效线径值: {xj_value} (超出有效范围或为零)")
# return False
# 更新数据
self.data['xj'] = xj_value

View File

@ -2732,7 +2732,22 @@ class MainWindow(MainWindowUI):
# 更新UI显示实时回显最新测量值
self.statusBar().showMessage(f"线径数据: {xj_value:.3f}", 2000)
# 实时更新到微丝产线表格中
# 初始化线径测量历史记录(如果不存在)
if not hasattr(self, '_diameter_measurements'):
self._diameter_measurements = []
self._diameter_measurement_count = 0
# 如果当前值不为0添加到测量历史
if xj_value > 0:
self._diameter_measurements.append(xj_value)
self._diameter_measurement_count += 1
logging.info(f"添加线径测量值: {xj_value:.3}, 当前测量次数: {self._diameter_measurement_count}")
# 保持最多5次测量记录
if len(self._diameter_measurements) > 5:
self._diameter_measurements.pop(0)
# 实时更新到微丝产线表格中(显示为临时值)
try:
# 查找线径对应的检验项配置
xj_config = None
@ -2781,10 +2796,9 @@ class MainWindow(MainWindowUI):
# 设置单元格数据包括配置ID
item.setData(Qt.UserRole, xj_config.get('id'))
# 如果当前值不为0设置为临时值颜色灰色
if xj_value > 0:
# 设置为临时值颜色(灰色)
item.setForeground(QBrush(QColor("#666666")))
item.setToolTip("临时测量值,产品取走后将保存最终值")
item.setToolTip(f"临时测量值 ({self._diameter_measurement_count}/5)")
# 更新表格单元格
self.process_table.setItem(target_row, xj_column, item)
@ -2801,15 +2815,23 @@ class MainWindow(MainWindowUI):
except Exception as e:
logging.error(f"更新线径值到表格失败: {str(e)}")
# 如果当前值不为0记录为最后一次有效值
if xj_value > 0:
self._last_diameter_value = xj_value
logging.info(f"更新最后一次有效线径值: {xj_value:.3f}")
# 如果当前值为0并且有足够的测量历史检查是否可以保存最终值
elif xj_value == 0 and len(self._diameter_measurements) >= 5:
logging.info(f"检测到线径值变为0使用最后一次有效值 {self._diameter_measurements[-1]:.3f} 作为最终结果")
# 如果当前值为0并且之前有非零值说明产品已拿开保存最后一次有效值
elif xj_value == 0 and hasattr(self, '_last_diameter_value') and self._last_diameter_value > 0:
final_value = self._last_diameter_value
logging.info(f"检测到线径值变为0使用最后一次有效值 {final_value:.3f} 作为最终结果")
# 计算测量值的稳定性
measurements = self._diameter_measurements[-5:] # 取最后5次测量
min_value = min(measurements)
max_value = max(measurements)
avg_value = sum(measurements) / len(measurements)
# 计算误差范围4%
error_range = avg_value * 0.04
# 检查是否在4%误差范围内
if max_value - min_value <= error_range:
final_value = measurements[-1]
logging.info(f"连续5次测量稳定误差范围: {min_value:.3f} - {max_value:.3f}, 平均: {avg_value:.3f}, 最终值: {final_value:.3f}")
# 查找线径对应的检验项配置
xj_config = None
@ -2843,11 +2865,15 @@ class MainWindow(MainWindowUI):
else:
logging.info(f"未找到订单 {self._current_order_code} 的线径公差范围,直接保存值 {final_value:.3f}")
self.set_inspection_value('xj', xj_config, final_value)
# 重置最后一次有效值,避免重复处理
self._last_diameter_value = 0
else:
logging.warning("未找到线径对应的检验项配置")
else:
logging.warning(f"连续5次测量误差范围: {min_value:.3f} - {max_value:.3f}, 平均: {avg_value:.3f}, 超出4%误差范围")
# 重置测量历史
self._diameter_measurements = []
self._diameter_measurement_count = 0
except ValueError:
logging.warning(f"线径数据格式错误: {value_str}")
else: