You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

534 lines
20 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Rs.Framework;
using Rs.Motion;
using Rs.MotionPlat.Commom;
using Rs.MotionPlat.Entitys;
using Rs.MotionPlat.Module;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Rs.MotionPlat.Flow.NormalFlow
{
public enum EStockStatus
{
Ready,
UnReady
}
enum EOutStockFlowStep
{
,
,
,
,
,
,
,
}
enum EInStockFlowStep
{
,
,
,
,
,
,
,
,
}
public class StockFlowBase
{
public EStockStatus Status { get; set; } = EStockStatus.UnReady;
/// <summary>
/// 料盘的高度
/// </summary>
public double TrayHeight
{
get
{
return GlobalVar.TwentyTrayHeight / 20;
}
}
public int CurrentLayer { get; set; } = 20;
public int StockIndex { get; set; }
//protected ManualResetEvent actionFinished = new ManualResetEvent(false);
public bool UpFinished { get; set; }
public bool DownFinished { get; set; }
EOutStockFlowStep outStockStep = EOutStockFlowStep.;
EInStockFlowStep inStockStep = EInStockFlowStep.;
bool outStockFinished = true;
bool inStockFinished = true;
Stopwatch stopwatch = new Stopwatch();
TargetPosition targetPosition = new TargetPosition();
ErrorCode errCode = ErrorCode.Ok;
string logInfo = string.Empty;
bool canFlash = false;
/// <summary>
/// 等待事件完成
/// </summary>
public bool WaitFinished()
{
return UpFinished;
}
public virtual string GetClassName()
{
return string.Empty;
}
public void OutStock()
{
if (outStockFinished)
{
outStockFinished = false;
outStockStep = EOutStockFlowStep.;
}
else
return;
while (!outStockFinished)
{
switch (outStockStep)
{
case EOutStockFlowStep.:
//StockModule.ClampTrayCylinder(EIoOperate.Open, StockIndex,GetClassName());
StockManager.OpenTray(StockIndex);
stopwatch.Restart();
outStockStep = EOutStockFlowStep.;
break;
case EOutStockFlowStep.:
if (Ops.IsOn($"料仓夹磁气缸{StockIndex}原位"))
{
outStockStep = EOutStockFlowStep.;
}
else if (stopwatch.ElapsedMilliseconds > 5000)
{
Msgbox.ShowTipDialog(EButtonType.Retry, "抽屉锁原位异常,请处理后点击重试", "error", true);
}
break;
case EOutStockFlowStep.:
if (StockIndex == 1)
{
targetPosition.StockZ = GlobalVar.StockZ1OutStock;
}
else if (StockIndex == 2)
{
targetPosition.StockZ = GlobalVar.StockZ2OutStock;
}
else if (StockIndex == 3)
{
targetPosition.StockZ = GlobalVar.StockZ3OutStock;
}
errCode = AxisControl.GetAxis($"StockZ{StockIndex}").MovePos(targetPosition.StockZ, GlobalVar.WholeSpeed);
if (errCode == ErrorCode.Ok)
{
logInfo = GetClassName() + $"到出仓位,tz1:{targetPosition.StockZ}";
MessageQueue.Instance.Insert(logInfo);
outStockStep = EOutStockFlowStep.;
}
break;
case EOutStockFlowStep.:
if (Ops.IsStop($"StockZ{StockIndex}"))
{
logInfo = GetClassName() + $"已运动到出仓位";
MessageQueue.Instance.Insert(logInfo);
outStockStep = EOutStockFlowStep.;
}
break;
case EOutStockFlowStep.:
StockModule.DrawerLock(EIoOperate.Open, StockIndex);
stopwatch.Restart();
outStockStep = EOutStockFlowStep.;
break;
case EOutStockFlowStep.:
if (Ops.IsOn($"抽屉锁{StockIndex}原位"))
{
outStockStep = EOutStockFlowStep.;
}
else if (stopwatch.ElapsedMilliseconds > 20000)
{
Msg.ShowError(AlarmCollection.Get(AlarmConstID.).Transform(StockIndex).CN);
outStockStep = EOutStockFlowStep.;
}
break;
case EOutStockFlowStep.:
StockModule.DrawerPullCylinder(EIoOperate.Open, StockIndex);
stopwatch.Restart();
outStockStep = EOutStockFlowStep.;
break;
case EOutStockFlowStep.:
if (Ops.IsOn($"抽屉拉伸气缸{StockIndex}动位"))
{
FlashLight();
outStockFinished = true;
}
else if (stopwatch.ElapsedMilliseconds > 30000)
{
Msg.ShowError(AlarmCollection.Get(AlarmConstID.).Transform(StockIndex).CN);
outStockStep = EOutStockFlowStep.;
}
break;
default:
break;
}
}
}
/// <summary>
/// 进仓
/// </summary>
public void InStock(bool gotoStartPos = true)
{
if (inStockFinished)
{
inStockFinished = false;
inStockStep = EInStockFlowStep.;
}
else
return;
while (!inStockFinished)
{
switch (inStockStep)
{
case EInStockFlowStep.:
//StockModule.ClampTrayCylinder(EIoOperate.Open, StockIndex, GetClassName());
StockManager.OpenTray(StockIndex);
stopwatch.Restart();
inStockStep = EInStockFlowStep.;
break;
case EInStockFlowStep.:
if (Ops.IsOn($"料仓夹磁气缸{StockIndex}原位"))
{
inStockStep = EInStockFlowStep.;
}
else if (stopwatch.ElapsedMilliseconds > 20000)
{
Msgbox.ShowTipDialog(EButtonType.Retry, "抽屉锁原位异常,请处理后点击重试", "error", true);
}
break;
case EInStockFlowStep.:
if (Ops.IsOn($"抽屉锁{StockIndex}按钮"))
{
inStockStep = EInStockFlowStep.;
}
break;
case EInStockFlowStep.:
StockModule.DrawerPullCylinder(EIoOperate.Close, StockIndex);
stopwatch.Restart();
inStockStep = EInStockFlowStep.;
break;
case EInStockFlowStep.:
if (Ops.IsOn($"抽屉拉伸气缸{StockIndex}原位"))
{
Thread.Sleep(1000);
inStockStep = EInStockFlowStep.;
}
else if (stopwatch.ElapsedMilliseconds > 10000)
{
AlarmEntity alarmEntity = AlarmCollection.Get(AlarmConstID.).Transform(StockIndex);
Msgbox.ShowDialog(alarmEntity, EButtonType.Recheck, true);
inStockStep = EInStockFlowStep.;
}
break;
case EInStockFlowStep.:
StockModule.DrawerLock(EIoOperate.Close, StockIndex);
stopwatch.Restart();
inStockStep = EInStockFlowStep.;
break;
case EInStockFlowStep.:
if (Ops.IsOn($"抽屉锁{StockIndex}动位"))
{
ResetLayer();
if (gotoStartPos)
{
inStockStep = EInStockFlowStep.;
}
else
{
CloseFlashLight();
inStockFinished = true;
}
}
else if (stopwatch.ElapsedMilliseconds > 30000)
{
Msg.ShowError(AlarmCollection.Get(AlarmConstID.).Transform(StockIndex).CN);
inStockStep = EInStockFlowStep.;
}
break;
case EInStockFlowStep.:
Up();
inStockStep = EInStockFlowStep.;
break;
case EInStockFlowStep.:
CloseFlashLight();
inStockFinished = true;
break;
default:
break;
}
}
}
void ResetLayer()
{
CurrentLayer = 20;
}
public void Up()
{
if (CurrentLayer == 0)
{
logInfo = $"{GetClassName()} 料仓{StockIndex}已处于第{CurrentLayer}层,不能再上升";
MessageQueue.Instance.Warn(logInfo);
return;
}
if (Ops.IsOn($"料仓夹磁气缸{StockIndex}动位"))
{
ReleaseTray();
}
CurrentLayer--;
targetPosition.StockZ = GetStockZPosByLayer(CurrentLayer);
errCode = AxisControl.GetAxis($"StockZ{StockIndex}").MovePos(targetPosition.StockZ, GlobalVar.WholeSpeed);
if (errCode == ErrorCode.Ok)
{
logInfo = $"{GetClassName()} 料仓{StockIndex}运动到{CurrentLayer}层tz:{targetPosition.StockZ}";
MessageQueue.Instance.Insert(logInfo);
stopwatch.Restart();
while (true)
{
if (Ops.IsStop($"StockZ{StockIndex}"))
{
Thread.Sleep(500);
logInfo = $"{GetClassName()} 料仓{StockIndex}已运动到{CurrentLayer}层cz:{Ops.GetCurPosition($"StockZ{StockIndex}")}";
MessageQueue.Instance.Insert(logInfo);
//if (HasTray())
//{
// ClampTray();
// Thread.Sleep(GlobalVar.StepMotorMoveDelayTime);
//}
break;
}
else if (stopwatch.ElapsedMilliseconds > 5000)
{
//运动异常报警
errCode = AxisControl.GetAxis($"StockZ{StockIndex}").MovePos(targetPosition.StockZ, GlobalVar.WholeSpeed);
}
}
}
}
public void Down()
{
if (CurrentLayer == 20)
{
return;
}
//下降之前松开料盘
ReleaseTray();
CurrentLayer++;
targetPosition.StockZ = GetStockZPosByLayer(CurrentLayer);
errCode = AxisControl.GetAxis($"StockZ{StockIndex}").MovePos(targetPosition.StockZ, GlobalVar.WholeSpeed);
if (errCode == ErrorCode.Ok)
{
stopwatch.Restart();
while (true)
{
if (Ops.IsStop($"StockZ{StockIndex}"))
{
Thread.Sleep(GlobalVar.StepMotorMoveDelayTime);
break;
}
else if (stopwatch.ElapsedMilliseconds > 30000)
{
//运动异常报警
errCode = AxisControl.GetAxis($"StockZ{StockIndex}").MovePos(targetPosition.StockZ, GlobalVar.WholeSpeed);
}
}
}
}
/// <summary>
/// 松开料盘
/// </summary>
public void ReleaseTray()
{
StockModule.ClampTrayCylinder(EIoOperate.Open, StockIndex, GetClassName());
stopwatch.Restart();
while (true)
{
if (Ops.IsOn($"料仓夹磁气缸{StockIndex}原位"))
{
Thread.Sleep(1000);
break;
}
else if (stopwatch.ElapsedMilliseconds > 30000)
{
AlarmEntity alarmEntity = AlarmCollection.Get($"料仓{StockIndex}夹磁气缸原位异常");
if (alarmEntity != null)
{
Msgbox.ShowDialog(alarmEntity, EButtonType.Retry, true);
StockModule.ClampTrayCylinder(EIoOperate.Open, StockIndex, GetClassName());
stopwatch.Restart();
}
else
{
Msg.ShowError($"翻译文件中未找到 料仓{StockIndex}夹磁气缸原位异常");
}
}
}
}
/// <summary>
/// 抓紧料盘
/// </summary>
public void ClampTray()
{
StockModule.ClampTrayCylinder(EIoOperate.Close, StockIndex, GetClassName());
stopwatch.Restart();
while (true)
{
if (Ops.IsOn($"料仓夹磁气缸{StockIndex}动位") || GlobalVar.RunSpace)
{
logInfo = $"{GetClassName()} 检测到料仓夹磁气缸{StockIndex}动位 OK";
MessageQueue.Instance.Insert(logInfo);
Thread.Sleep(1000);
return;
}
else if (stopwatch.ElapsedMilliseconds > 30000)
{
AlarmEntity alarmEntity = AlarmCollection.Get($"料仓{StockIndex}夹磁气缸动位异常");
if (alarmEntity != null)
{
Msgbox.ShowDialog(alarmEntity, EButtonType.Retry, true);
StockModule.ClampTrayCylinder(EIoOperate.Close, StockIndex, GetClassName());
stopwatch.Restart();
}
else
{
Msg.ShowError($"翻译文件中未找到 料仓{StockIndex}夹磁气缸动位异常");
}
}
}
}
/// <summary>
/// 闪烁料仓灯
/// </summary>
public void FlashLight()
{
if (canFlash)
return;
else
canFlash = true;
Task.Run(() => {
while (canFlash)
{
Ops.On($"料仓{StockIndex}状态灯");
Thread.Sleep(200);
Ops.Off($"料仓{StockIndex}状态灯");
Thread.Sleep(200);
}
});
}
public void CloseFlashLight()
{
canFlash = false;
}
/// <summary>
/// 根据层数获取料仓Z的高度
/// </summary>
/// <param name="layer"></param>
/// <returns></returns>
private double GetStockZPosByLayer(int layer)
{
return (GetFirstLayerPos() - ((CurrentLayer - 1) * TrayHeight));
}
/// <summary>
/// 获取料仓首层的位置
/// </summary>
/// <returns></returns>
public double GetFirstLayerPos()
{
switch (StockIndex)
{
case 1:
return GlobalVar.StockZ1FirstTray;
case 2:
return GlobalVar.StockZ2FirstTray;
case 3:
return GlobalVar.StockZ3FirstTray;
}
return 0;
}
/// <summary>
/// 获取当前层数
/// </summary>
/// <returns></returns>
public int GetCurrentLayer()
{
return CurrentLayer;
}
/// <summary>
/// 是否有料盘
/// </summary>
/// <returns></returns>
public bool HasTray()
{
return Ops.IsOn($"满料{StockIndex}对射光电");
}
/// <summary>
/// 工作完成收料
/// </summary>
public void DischargeFinish()
{
Status = EStockStatus.UnReady;
EDischargeFinishStep finishStep = EDischargeFinishStep.;
while (true)
{
switch (finishStep)
{
case EDischargeFinishStep.:
FlashLight();
if (Ops.IsOn($"抽屉锁{StockIndex}按钮"))
{
finishStep = EDischargeFinishStep.;
}
break;
case EDischargeFinishStep.:
OutStock();
finishStep = EDischargeFinishStep.;
break;
case EDischargeFinishStep.:
InStock(false);
Status = EStockStatus.Ready;
return;
default:
break;
}
}
}
public void Reset()
{
Status = EStockStatus.UnReady;
}
}
}