|
|
@@ -2,14 +2,17 @@
|
|
|
using Cognex.VisionPro;
|
|
|
using Cognex.VisionPro.ToolBlock;
|
|
|
using CSScripting;
|
|
|
+using CSScriptLib;
|
|
|
using MathNet.Numerics.Distributions;
|
|
|
using MathNet.Numerics.LinearAlgebra;
|
|
|
using MathNet.Numerics.RootFinding;
|
|
|
+using Newtonsoft.Json;
|
|
|
using NPOI.SS.Formula.Functions;
|
|
|
using NPOI.Util;
|
|
|
using Opc.Ua;
|
|
|
using OpenCvSharp;
|
|
|
using OpenCvSharp.Flann;
|
|
|
+using Org.BouncyCastle.Crypto;
|
|
|
using Prism.Events;
|
|
|
using Prism.Ioc;
|
|
|
using Prism.Mvvm;
|
|
|
@@ -20,6 +23,8 @@ using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Collections.ObjectModel;
|
|
|
using System.ComponentModel;
|
|
|
+using System.Data;
|
|
|
+using System.Diagnostics;
|
|
|
using System.Drawing;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
@@ -52,6 +57,7 @@ using TeamAAS_VP.Models.Product;
|
|
|
using TeamAAS_VP.Resources.Languages;
|
|
|
using TeamAAS_VP.Services;
|
|
|
using TeamAAS_VP.ViewModels.Home;
|
|
|
+using TeamAAS_VP.Views.Product;
|
|
|
using TouchSocket.Core;
|
|
|
using TouchSocket.SerialPorts;
|
|
|
using TouchSocket.Sockets;
|
|
|
@@ -78,10 +84,38 @@ namespace TeamAAS_VP.Core
|
|
|
ICalibrationService _calibrationService;
|
|
|
IRemoteCommandService _remoteCommandService;
|
|
|
ISystemDatabaseService _systemDatabaseService;
|
|
|
+ IMesService _mesService;
|
|
|
|
|
|
Timer yieldtimer;
|
|
|
private DateTime StartTime = DateTime.Now;
|
|
|
SerialPortClient client;
|
|
|
+ // Pressure collection
|
|
|
+ /// <summary>
|
|
|
+ /// 压力采集信号控制CTS
|
|
|
+ /// </summary>
|
|
|
+ private CancellationTokenSource _pressureCts;
|
|
|
+ /// <summary>
|
|
|
+ /// 压力采集超时CTS
|
|
|
+ /// </summary>
|
|
|
+ private CancellationTokenSource _pressureTimeoutCts;
|
|
|
+ /// <summary>
|
|
|
+ /// 压力采集CTS
|
|
|
+ /// </summary>
|
|
|
+ private CancellationTokenSource _pressureLinkedCts;
|
|
|
+ /// <summary>
|
|
|
+ /// 压力数据集合
|
|
|
+ /// </summary>
|
|
|
+ private List<(DateTime Timestamp, float Value)> _pressureSamples = new List<(DateTime, float)>();
|
|
|
+ /// <summary>
|
|
|
+ /// 采样间隔毫秒
|
|
|
+ /// </summary>
|
|
|
+ private int _pressureSampleIntervalMs = 50; //采样间隔毫秒
|
|
|
+ /// <summary>
|
|
|
+ /// 超时自动停止秒数
|
|
|
+ /// </summary>
|
|
|
+ private int _pressureMaxDurationSeconds = 30; //超时自动停止秒数
|
|
|
+ Stopwatch swCT = new Stopwatch();
|
|
|
+ List<LockResult> lockResultsAll = new List<LockResult>();//一个产品的总结果
|
|
|
#endregion
|
|
|
|
|
|
#region 属性
|
|
|
@@ -200,6 +234,16 @@ namespace TeamAAS_VP.Core
|
|
|
get { return _NumStatistics; }
|
|
|
set { SetProperty(ref _NumStatistics, value); }
|
|
|
}
|
|
|
+
|
|
|
+ private double _CTtime;
|
|
|
+ /// <summary>
|
|
|
+ /// ct
|
|
|
+ /// </summary>
|
|
|
+ public double CTtime
|
|
|
+ {
|
|
|
+ get { return _CTtime; }
|
|
|
+ set { SetProperty(ref _CTtime, value); }
|
|
|
+ }
|
|
|
#endregion
|
|
|
|
|
|
#region 命令
|
|
|
@@ -212,7 +256,8 @@ namespace TeamAAS_VP.Core
|
|
|
|
|
|
public Management(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IConfigService configService,
|
|
|
IRobotService robotService, ICameraService cameraService, IFeederService feederService, IPlcService plcService, IProductService productService,
|
|
|
- ICalibrationService calibrationService, IRemoteCommandService remoteCommandService, ISystemDatabaseService systemDatabaseService, ILightManagerService lightManagerService)
|
|
|
+ ICalibrationService calibrationService, IRemoteCommandService remoteCommandService, ISystemDatabaseService systemDatabaseService, ILightManagerService lightManagerService,
|
|
|
+ IMesService mesService)
|
|
|
{
|
|
|
_regionManager = regionManager;
|
|
|
_eventAggregator = ea;
|
|
|
@@ -229,29 +274,37 @@ namespace TeamAAS_VP.Core
|
|
|
_remoteCommandService = remoteCommandService;
|
|
|
_systemDatabaseService = systemDatabaseService;
|
|
|
_lightManagerService = lightManagerService;
|
|
|
+ _mesService = mesService;
|
|
|
}
|
|
|
|
|
|
#region 读取配置参数
|
|
|
- public void ReadConfig()
|
|
|
+ public async Task ReadConfig()
|
|
|
{
|
|
|
- ReadNumber();
|
|
|
+ await ReadNumber();
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 读取数量配置
|
|
|
/// </summary>
|
|
|
- public void ReadNumber()
|
|
|
+ public async Task ReadNumber()
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- if (File.Exists(FilePath.NumberStatisticsPath))
|
|
|
+ NumStatistics = new NumberStatistics();
|
|
|
+ var latestHeadChangeRecord = await _systemDatabaseService.GetLatestHeadChangeRecordAsync();
|
|
|
+ if (latestHeadChangeRecord != null)
|
|
|
{
|
|
|
- NumStatistics = FileHelper.ReadJsonFile<NumberStatistics>(FilePath.NumberStatisticsPath);
|
|
|
+ NumStatistics.HeaderNum = latestHeadChangeRecord.CurrentLockCount;
|
|
|
}
|
|
|
- else
|
|
|
+
|
|
|
+ var latestNozzleChangeRecord = await _systemDatabaseService.GetLatestNozzleChangeRecordAsync();
|
|
|
+ if (latestNozzleChangeRecord != null)
|
|
|
{
|
|
|
- NumStatistics = new NumberStatistics();
|
|
|
- FileHelper.WriteJsonFile(NumStatistics, FilePath.NumberStatisticsPath);
|
|
|
+ NumStatistics.NozzleNum = latestNozzleChangeRecord.CurrentLockCount;
|
|
|
}
|
|
|
+
|
|
|
+ NumStatistics.Feeder1Num = _configService.GetRemainingCountByFeederNumber(1);
|
|
|
+ NumStatistics.Feeder2Num = _configService.GetRemainingCountByFeederNumber(2);
|
|
|
+
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
@@ -617,7 +670,7 @@ namespace TeamAAS_VP.Core
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- SendTaskMessage(string.Format(Lang.电批连接失败0,ex.Message), MessageLevel.Alarm);
|
|
|
+ SendTaskMessage(string.Format(Lang.电批连接失败0, ex.Message), MessageLevel.Alarm);
|
|
|
LogHelper.WriteLogError(Lang.连接电批时出错, ex);
|
|
|
}
|
|
|
}
|
|
|
@@ -665,7 +718,7 @@ namespace TeamAAS_VP.Core
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
LogHelper.WriteLogError("初始化扭力测量仪时出错!", ex);
|
|
|
- SendTaskMessage("初始化扭力测量仪时出错!"+ex.Message, MessageLevel.Error);
|
|
|
+ SendTaskMessage("初始化扭力测量仪时出错!" + ex.Message, MessageLevel.Error);
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
@@ -1218,7 +1271,7 @@ namespace TeamAAS_VP.Core
|
|
|
}
|
|
|
|
|
|
//获取产品编码
|
|
|
- _productService.CurrentProductCode = plc.ReadNode<string>(addressConfig.In_Code.Address);
|
|
|
+ _productService.CurrentProductCode = plc.ReadNode<string>(string.Format(addressConfig.In_Code.Address, 0));
|
|
|
//如果产品编码为空,则以当前的时间戳作为编码
|
|
|
if (string.IsNullOrEmpty(_productService.CurrentProductCode))
|
|
|
{
|
|
|
@@ -1268,7 +1321,7 @@ namespace TeamAAS_VP.Core
|
|
|
|
|
|
// 执行视觉任务
|
|
|
var _cts = new CancellationTokenSource();
|
|
|
- var visionResult = await _remoteCommandService.ExecutePhotoGetSinglePoint(process, null, null, _cts.Token);
|
|
|
+ var visionResult = await _remoteCommandService.ExecutePhotoGetSinglePoint(process, null, null, _cts.Token, null);
|
|
|
if (visionResult.IsSucceed)
|
|
|
{
|
|
|
//披头偏差
|
|
|
@@ -1311,7 +1364,7 @@ namespace TeamAAS_VP.Core
|
|
|
SendTaskMessage(Lang.触发下相机拍照, MessageLevel.Debug);
|
|
|
//相机的编号
|
|
|
Int16 cameraIndex = plc.ReadNode<Int16>(addressConfig.In_DowmCameraNum.Address);
|
|
|
- SendTaskMessage(string.Format(Lang.编号0,cameraIndex), MessageLevel.Info);
|
|
|
+ SendTaskMessage(string.Format(Lang.编号0, cameraIndex), MessageLevel.Info);
|
|
|
|
|
|
// 获取流程
|
|
|
var process = _productService.GetCurrentProductProcedureModelById(currentProduct.FixedUpCamera1ProcedureId);
|
|
|
@@ -1335,7 +1388,7 @@ namespace TeamAAS_VP.Core
|
|
|
|
|
|
// 执行视觉任务
|
|
|
var _cts = new CancellationTokenSource();
|
|
|
- var visionResult = await _remoteCommandService.ExecutePhotoGetSinglePoint(process, null, null, _cts.Token);
|
|
|
+ var visionResult = await _remoteCommandService.ExecutePhotoGetSinglePoint(process, null, null, _cts.Token, null);
|
|
|
if (visionResult.IsSucceed)
|
|
|
{
|
|
|
//披头偏差
|
|
|
@@ -1410,11 +1463,21 @@ namespace TeamAAS_VP.Core
|
|
|
Int16 cameraIndex = plc.ReadNode<Int16>(addressConfig.In_UpCameraPutNum.Address);
|
|
|
SendTaskMessage(string.Format(Lang.编号0, cameraIndex), MessageLevel.Info);
|
|
|
|
|
|
+ //视觉流程ID
|
|
|
+ Guid procedureId = currentProduct.MoveDownCameraLockPhotoProcedureId;
|
|
|
+
|
|
|
+ //获取锁付拍照的点位
|
|
|
+ var pos = currentProduct.ScrewCameraPoints.FirstOrDefault(p => p.Number == cameraIndex);
|
|
|
+ if (pos != null)
|
|
|
+ {
|
|
|
+ procedureId = pos.VisionProcessID;
|
|
|
+ }
|
|
|
+
|
|
|
// 获取流程
|
|
|
- var process = _productService.GetCurrentProductProcedureModelById(currentProduct.MoveDownCameraLockPhotoProcedureId);
|
|
|
+ var process = _productService.GetCurrentProductProcedureModelById(procedureId);
|
|
|
if (process == null)
|
|
|
{
|
|
|
- SendTaskMessage(string.Format(Lang.未找到上相机锁附组装拍照对应的视觉流程流程ID0,currentProduct.MoveDownCameraLockPhotoProcedureId), MessageLevel.Alarm);
|
|
|
+ SendTaskMessage(string.Format(Lang.未找到上相机锁附组装拍照对应的视觉流程流程ID0, procedureId), MessageLevel.Alarm);
|
|
|
await plc.WriteNodeAsync(addressConfig.Out_UpCameraPutStatus.Address, (Int16)2);
|
|
|
return;
|
|
|
}
|
|
|
@@ -1432,7 +1495,7 @@ namespace TeamAAS_VP.Core
|
|
|
|
|
|
// 执行视觉任务
|
|
|
var _cts = new CancellationTokenSource();
|
|
|
- var visionResult = await _remoteCommandService.ExecutePhotoGetSinglePoint(process, null, new double[] { currentPosition.X, currentPosition.Y, currentPosition.U }, _cts.Token);
|
|
|
+ var visionResult = await _remoteCommandService.ExecutePhotoGetSinglePoint(process, null, new double[] { currentPosition.X, currentPosition.Y, currentPosition.U }, _cts.Token, null);
|
|
|
if (visionResult.IsSucceed)
|
|
|
{
|
|
|
UpCameraPutResults[cameraIndex] = new PointF((float)visionResult.X, (float)visionResult.Y);
|
|
|
@@ -1588,7 +1651,9 @@ namespace TeamAAS_VP.Core
|
|
|
SendTaskMessage(Lang.收到开始锁付信号, MessageLevel.Debug);
|
|
|
//位置编号
|
|
|
Int16 positionIndex = plc.ReadNode<Int16>(addressConfig.In_ProductNum.Address);
|
|
|
- SendTaskMessage(string.Format(Lang.编号0,positionIndex), MessageLevel.Info);
|
|
|
+ SendTaskMessage(string.Format(Lang.编号0, positionIndex), MessageLevel.Info);
|
|
|
+ // 开始采集压力值
|
|
|
+ StartPressureCollection(currentProduct.Name, _productService.CurrentProductCode, positionIndex);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
@@ -1615,7 +1680,7 @@ namespace TeamAAS_VP.Core
|
|
|
SendTaskMessage(Lang.收到停止锁付信号, MessageLevel.Debug);
|
|
|
//位置编号
|
|
|
Int16 positionIndex = plc.ReadNode<Int16>(addressConfig.In_ProductNum.Address);
|
|
|
- SendTaskMessage(string.Format(Lang.编号0,positionIndex), MessageLevel.Info);
|
|
|
+ SendTaskMessage(string.Format(Lang.编号0, positionIndex), MessageLevel.Info);
|
|
|
//获取当前锁付点
|
|
|
var screwPoint = currentProduct.ScrewPoints.FirstOrDefault(p => p.Number == positionIndex);
|
|
|
//获取当前的机器人点位
|
|
|
@@ -1623,7 +1688,17 @@ namespace TeamAAS_VP.Core
|
|
|
var currentPosition = robot.GetRobotPos();
|
|
|
//读取压力值
|
|
|
float pressure = plc.ReadNode<float>(addressConfig.In_PressureValue.Address);
|
|
|
-
|
|
|
+ //当前用户名
|
|
|
+ string currentUserName = _systemDatabaseService.GetCurrentUser().UserName;
|
|
|
+
|
|
|
+ //吸嘴最后一次的记录
|
|
|
+ var nozzleRecord = await _systemDatabaseService.GetLatestNozzleChangeRecordAsync();
|
|
|
+ //最后一次的吸嘴编号
|
|
|
+ string nozzleNumber = nozzleRecord != null ? nozzleRecord.NozzleBatchNumber : "N/A";
|
|
|
+ //披头最后一次的记录
|
|
|
+ var headRecord = await _systemDatabaseService.GetLatestHeadChangeRecordAsync();
|
|
|
+ //最后一次的披头编号
|
|
|
+ string headNumber = headRecord != null ? headRecord.HeadBatchNumber : "N/A";
|
|
|
|
|
|
await Task.Delay(60);
|
|
|
//获取电批结果
|
|
|
@@ -1640,26 +1715,54 @@ namespace TeamAAS_VP.Core
|
|
|
lockResult.ScrewdriverProgramNumber = ScrewDriver.ProgramNumber;
|
|
|
lockResult.ResultDetails = ScrewDriver.ErrorInfo;
|
|
|
lockResult.Pressure = pressure;
|
|
|
- lockResult.TargetTorque = ScrewDriver.TargetTorque;
|
|
|
+ lockResult.TargetTorqueUpperLimit = ScrewDriver.TargetTorqueUpperLimit;
|
|
|
+ lockResult.TargetTorqueLowerLimit = ScrewDriver.TargetTorqueLowerLimit;
|
|
|
lockResult.TargetTurnsUpperLimit = ScrewDriver.TargetTurnsUpperLimit;
|
|
|
lockResult.TargetTurnsLowerLimit = ScrewDriver.TargetTurnsLowerLimit;
|
|
|
lockResult.TargetLockSpeed = ScrewDriver.TargetLockSpeed;
|
|
|
- lockResult.StartPositionX = currentPosition.X;
|
|
|
- lockResult.StartPositionY = currentPosition.Y;
|
|
|
+ lockResult.PositionX = currentPosition.X;
|
|
|
+ lockResult.PositionY = currentPosition.Y;
|
|
|
lockResult.StartPositionZ = screwPoint.Z_Position_Start;
|
|
|
lockResult.EndPositionZ = currentPosition.Z;
|
|
|
lockResult.LockDescentSpeed = screwPoint.Z_Velocity_Stop;
|
|
|
lockResult.FeederNumber = screwPoint != null ? screwPoint.Feeder : 0;
|
|
|
- lockResult.NozzleNumber = "";
|
|
|
- lockResult.OperatorName = _systemDatabaseService.GetCurrentUser().UserName;
|
|
|
+ lockResult.NozzleNumber = nozzleNumber;
|
|
|
+ lockResult.HeadNumber = headNumber;
|
|
|
+ lockResult.OperatorName = currentUserName;
|
|
|
lockResult.ProductImagePath = "";
|
|
|
lockResult.WaveDatas = ScrewDriver.WaveDatas;
|
|
|
+ //获取系统参数
|
|
|
+ var systemConfig = _configService.GetSystemConfiguration();
|
|
|
+ //判断是否保存锁付曲线
|
|
|
+ if (systemConfig.IsSaveScrewCurve)
|
|
|
+ {
|
|
|
+ //锁付曲线的保存路径为:systemConfig.ScrewCurvePath//年月//年月日//配方名//产品SN//产品SN+螺丝索引
|
|
|
+ string datePath = DateTime.Now.ToString("yyyy-MM") + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\";
|
|
|
+ string dirPath = System.IO.Path.Combine(systemConfig.ScrewCurvePath, datePath, currentProduct.Name, lockResult.ProductNumber);
|
|
|
+ if (!Directory.Exists(dirPath))
|
|
|
+ {
|
|
|
+ Directory.CreateDirectory(dirPath);
|
|
|
+ }
|
|
|
+ string curveFileName = $"{lockResult.ProductNumber}-{lockResult.Number}.png";
|
|
|
+ //锁付CSV保存路径
|
|
|
+ string curveFileFullPath = System.IO.Path.Combine(dirPath, $"{lockResult.ProductNumber}-{lockResult.Number}.csv");
|
|
|
+
|
|
|
+ //锁付曲线保存路径
|
|
|
+ lockResult.LockCurveImagePath = System.IO.Path.Combine(dirPath, curveFileName);
|
|
|
+ var _ = lockResult.SaveToCsvAsync(curveFileFullPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ lockResultsAll.Add(lockResult);
|
|
|
App.Current.Dispatcher.Invoke(() =>
|
|
|
{
|
|
|
_eventAggregator.GetEvent<LockFinishNotification>().Publish(lockResult);
|
|
|
});
|
|
|
+ //记录锁付结果至数据库
|
|
|
await _systemDatabaseService.RecordLockResultAsync(lockResult);
|
|
|
- await plc.WriteNodeAsync(addressConfig.In_WorkDone.Address, (Int16)0);
|
|
|
+ //披头数量+1
|
|
|
+ NumStatistics.HeaderNum = await _systemDatabaseService.IncrementLatestHeadLockCountAsync(currentUserName);
|
|
|
+ //吸嘴数量+1
|
|
|
+ NumStatistics.NozzleNum = await _systemDatabaseService.IncrementLatestNozzleLockCountAsync(currentUserName);
|
|
|
|
|
|
//电批吸嘴报警
|
|
|
NumStatistics.HeaderNum++;//批头数量
|
|
|
@@ -1673,6 +1776,10 @@ namespace TeamAAS_VP.Core
|
|
|
{
|
|
|
System.Windows.MessageBox.Show("Nozzle Alarm!", "Alarm", MessageBoxButton.OK, MessageBoxImage.Warning);
|
|
|
}
|
|
|
+ }
|
|
|
+ await plc.WriteNodeAsync(addressConfig.In_WorkDone.Address, (Int16)0);
|
|
|
+ // 停止采集压力并保存
|
|
|
+ await StopAndSavePressureCollectionAsync(currentProduct.Name, _productService.CurrentProductCode, positionIndex);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
@@ -1717,9 +1824,14 @@ namespace TeamAAS_VP.Core
|
|
|
|
|
|
for (int i = 0; i < currentProduct.ScrewPoints.Count; i++)
|
|
|
{
|
|
|
- var localPoint = new PointF(currentProduct.ScrewPoints[i].X_Position, currentProduct.ScrewPoints[i].Y_Position);
|
|
|
+ var localPoint = new PointF(currentProduct.ScrewPoints[i].X_Position + currentProduct.ScrewPoints[i].X_Offset,
|
|
|
+ currentProduct.ScrewPoints[i].Y_Position + currentProduct.ScrewPoints[i].Y_Offset);
|
|
|
var worldPoint = local.ToOldCoord(localPoint.X, localPoint.Y);
|
|
|
var point = currentProduct.ScrewPoints[i].Clone();
|
|
|
+
|
|
|
+ //锁付点位在Local中已经加了偏移,这里写回PLC时需要将偏移清零,否则写入PLC端时会重复计算偏移
|
|
|
+ point.X_Offset = 0;
|
|
|
+ point.Y_Offset = 0;
|
|
|
point.X_Position = (float)worldPoint[0];
|
|
|
point.Y_Position = (float)worldPoint[1];
|
|
|
SendTaskMessage($"点:{i},{point.X_Position:F3},{point.Y_Position:F4}", MessageLevel.Info);
|
|
|
@@ -1731,12 +1843,11 @@ namespace TeamAAS_VP.Core
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- SendTaskMessage(string.Format(Lang.多相机坐标合并请求已结束或被取消state0,state), MessageLevel.Alarm);
|
|
|
+ SendTaskMessage(string.Format(Lang.多相机坐标合并请求已结束或被取消state0, state), MessageLevel.Alarm);
|
|
|
await plc.WriteNodeAsync(addressConfig.Out_CameraDone.Address, (Int16)0);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
// 螺丝供料器螺丝递减
|
|
|
else if (addressConfig.In_PickINC.Address.Contains(tuple.nodeId))
|
|
|
{
|
|
|
@@ -1758,11 +1869,11 @@ namespace TeamAAS_VP.Core
|
|
|
var feeders = _configService.GetRemainingCountByFeederNumber(state);
|
|
|
if (state == 1)
|
|
|
{
|
|
|
- NumStatistics.Feeder1Num = feeders;
|
|
|
+ NumStatistics.Feeder1Num = _configService.DecrementScrewCountByFeederNumber(feeders);
|
|
|
}
|
|
|
else if (state == 2)
|
|
|
{
|
|
|
- NumStatistics.Feeder2Num = feeders;
|
|
|
+ NumStatistics.Feeder2Num = _configService.DecrementScrewCountByFeederNumber(feeders);
|
|
|
}
|
|
|
FileHelper.WriteJsonFile(NumStatistics, FilePath.NumberStatisticsPath);
|
|
|
var res = _configService.CheckAlarm(state);
|
|
|
@@ -1775,11 +1886,11 @@ namespace TeamAAS_VP.Core
|
|
|
}
|
|
|
|
|
|
// 过站检查
|
|
|
- else
|
|
|
+ else
|
|
|
{
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
{
|
|
|
- if (string.Format(addressConfig.In_MesCheck.Address,i).Contains(tuple.nodeId))
|
|
|
+ if (string.Format(addressConfig.In_MesCheck.Address, i).Contains(tuple.nodeId))
|
|
|
{
|
|
|
if (tuple.value is Int16 state)
|
|
|
{
|
|
|
@@ -1791,28 +1902,54 @@ namespace TeamAAS_VP.Core
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
+ var code = plc.ReadNode<string>(string.Format(addressConfig.In_Code.Address, i));
|
|
|
+ //询问mes
|
|
|
if (state == 1)
|
|
|
{
|
|
|
- if(i == 0)
|
|
|
+ swCT.Restart();
|
|
|
+ //这里缺少从MES获取产品配方的内容,需要补充
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ (bool isSuccess, string response) = await _mesService.StationGetAsync(code, 2);
|
|
|
+ if (i == 0)
|
|
|
{
|
|
|
SendTaskMessage($"收到当前过站检查请求(当站是否生产)...", MessageLevel.Debug);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
SendTaskMessage($"收到{i}过站检查请求(当站是否生产)...", MessageLevel.Debug);
|
|
|
- }
|
|
|
+ }
|
|
|
+ if (isSuccess)
|
|
|
+ {
|
|
|
+ //产品发生改变并且供料器发生了变化时,需要通知PLC进行取料更改
|
|
|
+ await plc.WriteNodeAsync(addressConfig.Out_ScrewFeederIsChanged.Address, (Int16)1); //不发生改变
|
|
|
|
|
|
- //产品发生改变并且供料器发生了变化时,需要通知PLC进行取料更改
|
|
|
- await plc.WriteNodeAsync(addressConfig.Out_ScrewFeederIsChanged.Address, (Int16)1); //不发生改变
|
|
|
+ await plc.WriteNodeAsync(string.Format(addressConfig.Out_MesStatus.Address, i), (Int16)1);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //产品发生改变并且供料器发生了变化时,需要通知PLC进行取料更改
|
|
|
+ await plc.WriteNodeAsync(addressConfig.Out_ScrewFeederIsChanged.Address, (Int16)1); //不发生改变
|
|
|
|
|
|
- await plc.WriteNodeAsync(string.Format(addressConfig.Out_MesStatus.Address,i), (Int16)1);
|
|
|
+ await plc.WriteNodeAsync(string.Format(addressConfig.Out_MesStatus.Address, i), (Int16)0);
|
|
|
+ }
|
|
|
}
|
|
|
// 上传本站数据
|
|
|
else if (state == 2)
|
|
|
{
|
|
|
SendTaskMessage($"收到{i}上传本站数据请求...", MessageLevel.Debug);
|
|
|
+
|
|
|
+ //这里缺少上传MES的本站数据结果及开始和结束时间,需要补充
|
|
|
+
|
|
|
+ (bool isSuccess, string response) = await _mesService.SubmitGetAsync(code, true, DateTime.Now, DateTime.Now);
|
|
|
+ //假设上传成功,这里缺少上传失败的处理
|
|
|
+
|
|
|
await plc.WriteNodeAsync(string.Format(addressConfig.Out_MesStatus.Address, i), (Int16)3);
|
|
|
+ swCT.Stop();
|
|
|
+ CTtime = Math.Round(swCT.Elapsed.TotalSeconds, 3);
|
|
|
+ WriteProductLog(lockResultsAll, code, CTtime);
|
|
|
+ lockResultsAll.Clear();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
@@ -1822,7 +1959,7 @@ namespace TeamAAS_VP.Core
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
@@ -1843,6 +1980,147 @@ namespace TeamAAS_VP.Core
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ #region Pressure collection helpers
|
|
|
+ /// <summary>
|
|
|
+ /// 开始采集压力值
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="productName">配方名称</param>
|
|
|
+ /// <param name="productCode">产品编号</param>
|
|
|
+ /// <param name="positionIndex">锁付点编号</param>
|
|
|
+ private void StartPressureCollection(string productName,string productCode, int positionIndex)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ // cancel existing if any
|
|
|
+ _pressureCts?.Cancel();
|
|
|
+ _pressureTimeoutCts?.Cancel();
|
|
|
+ _pressureLinkedCts?.Dispose();
|
|
|
+ _pressureSamples.Clear();
|
|
|
+
|
|
|
+ _pressureCts = new CancellationTokenSource();
|
|
|
+ _pressureTimeoutCts = new CancellationTokenSource();
|
|
|
+
|
|
|
+ // timeout after configured seconds
|
|
|
+ _pressureTimeoutCts.CancelAfter(TimeSpan.FromSeconds(_pressureMaxDurationSeconds));
|
|
|
+
|
|
|
+ // link tokens so either stop or timeout cancels
|
|
|
+ _pressureLinkedCts = CancellationTokenSource.CreateLinkedTokenSource(_pressureCts.Token, _pressureTimeoutCts.Token);
|
|
|
+
|
|
|
+ var token = _pressureLinkedCts.Token;
|
|
|
+ var addressConfig = _configService.GetPlcAddresses();
|
|
|
+ var plc = _plcService.GetPlcByNumber(addressConfig.PlcNo) as OpcUaClientPLC;
|
|
|
+ if (plc == null || !plc.IsConnected) return;
|
|
|
+ Task.Run(async () =>
|
|
|
+ {
|
|
|
+ SendTaskMessage($"Pressure collection started for position {positionIndex}", MessageLevel.Debug);
|
|
|
+ while (!token.IsCancellationRequested)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ float pressure = plc.ReadNode<float>(addressConfig.In_PressureValue.Address);
|
|
|
+ lock (_pressureSamples)
|
|
|
+ {
|
|
|
+ _pressureSamples.Add((DateTime.Now, pressure));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ LogHelper.WriteLogError("采集压力值时出错", ex);
|
|
|
+ }
|
|
|
+ await Task.Delay(_pressureSampleIntervalMs);
|
|
|
+ }
|
|
|
+ // if cancelled due to timeout
|
|
|
+ if (_pressureTimeoutCts.IsCancellationRequested && !_pressureCts.IsCancellationRequested)
|
|
|
+ {
|
|
|
+ SendTaskMessage("Pressure collection timed out, saving collected data.", MessageLevel.Alarm);
|
|
|
+ await StopAndSavePressureCollectionAsync(productName, productCode,positionIndex);
|
|
|
+ }
|
|
|
+ }, token);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ LogHelper.WriteLogError("StartPressureCollection error", ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 停止采集并保存压力数据
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="productName"></param>
|
|
|
+ /// <param name="productCode"></param>
|
|
|
+ /// <param name="positionIndex"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private async Task StopAndSavePressureCollectionAsync(string productName, string productCode, int positionIndex)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ // cancel sampling
|
|
|
+ _pressureCts?.Cancel();
|
|
|
+ // allow worker to observe cancel
|
|
|
+ await Task.Delay(50);
|
|
|
+
|
|
|
+ List<(DateTime Timestamp, float Value)> snapshot;
|
|
|
+ lock (_pressureSamples)
|
|
|
+ {
|
|
|
+ snapshot = new List<(DateTime, float)>(_pressureSamples);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (snapshot.Count == 0)
|
|
|
+ {
|
|
|
+ SendTaskMessage("No pressure samples collected.", MessageLevel.Info);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // save to file under configured path
|
|
|
+ try
|
|
|
+ {
|
|
|
+ //获取系统参数
|
|
|
+ var systemConfig = _configService.GetSystemConfiguration();
|
|
|
+ //判断是否保存锁付曲线
|
|
|
+ if (systemConfig.IsSaveScrewCurve)
|
|
|
+ {
|
|
|
+ string basePath = systemConfig?.ScrewCurvePath ?? Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
|
|
|
+ string datePath = DateTime.Now.ToString("yyyy-MM") + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\";
|
|
|
+ string dirPath = Path.Combine(basePath, datePath, productName ?? "UnknownProduct", productCode ?? DateTime.Now.ToString("yyyyMMddHHmmssfff"));
|
|
|
+ if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath);
|
|
|
+ string fileName = $"pressure-{productCode}-{positionIndex}-{DateTime.Now.ToString("HHmmss")}.csv";
|
|
|
+ string fullPath = Path.Combine(dirPath, fileName);
|
|
|
+ var sb = new StringBuilder();
|
|
|
+ sb.AppendLine("Timestamp,Value");
|
|
|
+ foreach (var s in snapshot)
|
|
|
+ {
|
|
|
+ sb.AppendLine($"{s.Timestamp:O},{s.Value}");
|
|
|
+ }
|
|
|
+
|
|
|
+ File.WriteAllText(fullPath, sb.ToString());
|
|
|
+ SendTaskMessage($"Pressure samples saved to {fullPath}", MessageLevel.Debug);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ LogHelper.WriteLogError("保存压力采样文件时出错", ex);
|
|
|
+ SendTaskMessage("保存压力采样文件时出错: " + ex.Message, MessageLevel.Error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ LogHelper.WriteLogError("StopAndSavePressureCollectionAsync error", ex);
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ _pressureLinkedCts?.Cancel();
|
|
|
+ _pressureLinkedCts?.Dispose();
|
|
|
+ _pressureTimeoutCts?.Dispose();
|
|
|
+ _pressureCts?.Dispose();
|
|
|
+ _pressureSamples.Clear();
|
|
|
+ }
|
|
|
+ catch { }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
private async void DoYieldTime(object state)
|
|
|
{
|
|
|
try
|
|
|
@@ -1897,6 +2175,81 @@ namespace TeamAAS_VP.Core
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
+
|
|
|
+ #region 产品锁付记录进行存储
|
|
|
+ /// <summary>
|
|
|
+ /// 产品锁付记录进行存储
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="result">结果</param>
|
|
|
+ public void WriteProductLog(List<LockResult> lockResults,string rfid,double ct)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ string result = "";//产品的总结果
|
|
|
+ foreach (var item in lockResults)
|
|
|
+ {
|
|
|
+ result = "OK";
|
|
|
+ if (!item.LockPassed)
|
|
|
+ {
|
|
|
+ result = "NG";
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ DateTime dt = DateTime.Now;
|
|
|
+ int number = lockResults.Count;//螺丝数量
|
|
|
+ string path = $"E:\\Data\\生产数据\\{dt.ToString("yyyy-MM")}\\{dt.ToString("yyyy-MM-dd")}.csv";
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ //具体的ct需要优化
|
|
|
+ sb.Append($"{dt.ToString("yyyy-MM-dd HH:mm:ss")},{lockResults[0].ProductName},{Math.Round(ct, 3)},{rfid},{result},{lockResults[number-1].NozzleNumber},{lockResults[number - 1].HeadNumber},");
|
|
|
+ foreach (var item in lockResults)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ sb.Append($"{item.LockPassed},{item.ResultDetails},{item.LockTorque},{item.TargetTorqueUpperLimit},{item.TargetTorqueLowerLimit},{item.LockTurns},{item.TargetTurnsUpperLimit},{item.TargetTurnsLowerLimit},{item.Pressure},{item.PositionX},{item.PositionY},{item.StartPositionZ},{item.EndPositionZ},{item.LockDescentSpeed},");
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ sb.Append($",,,,,,,,,,,,,,");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //========================
|
|
|
+ if (!Directory.Exists(Path.GetDirectoryName(path))) //判断文件夹是否
|
|
|
+ {
|
|
|
+ Directory.CreateDirectory(Path.GetDirectoryName(path)); //创建文件夹
|
|
|
+ }
|
|
|
+
|
|
|
+ if (File.Exists(path))
|
|
|
+ {
|
|
|
+ // 使用 StreamWriter 以追加模式打开文件
|
|
|
+ StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8);
|
|
|
+ sw.WriteLine(sb.ToString().TrimEnd(','));
|
|
|
+ sw.Close();
|
|
|
+ sw.Dispose();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8);
|
|
|
+ StringBuilder sb1 = new StringBuilder();
|
|
|
+ StringBuilder sb_up = new StringBuilder();
|
|
|
+ StringBuilder sb_down = new StringBuilder();
|
|
|
+ sb1.Append("时间,产品名称,CT,RFID,产品结果,吸嘴数量,披头数量,");
|
|
|
+ for (int i = 0; i < number; i++)
|
|
|
+ {
|
|
|
+ sb1.Append($"螺丝{i + 1}结果,螺丝{i + 1}结果详情,螺丝{i + 1}扭矩,螺丝{i + 1}扭矩上限,螺丝{i + 1}扭矩下限,螺丝{i + 1}圈数,螺丝{i + 1}圈数上限,螺丝{i + 1}圈数下限,螺丝{i + 1}压力值,螺丝{i + 1}锁附X,螺丝{i + 1}锁附Y,螺丝{i + 1}开始位置Z,螺丝{i + 1}结束位置Z,螺丝{i + 1}锁附下降速度,");
|
|
|
+ }
|
|
|
+ sw.WriteLine(sb1.ToString().TrimEnd(','));
|
|
|
+ sw.WriteLine(sb.ToString().TrimEnd(','));
|
|
|
+ sw.Close();
|
|
|
+ sw.Dispose();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ LogHelper.WriteLogError("写入产品记录时出错!", ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
#endregion
|
|
|
}
|
|
|
}
|