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.

596 lines
28 KiB
C#

using Rs.Controls;
using Rs.Framework;
using Rs.Motion;
using Rs.MotionPlat.Commom;
using Rs.MotionPlat.Entitys;
using Rs.MotionPlat.Flow.Common;
using Rs.MotionPlat.Flow.NormalFlow;
using Rs.MotionPlat.Flow.SubFlow;
using Rs.MotionPlat.Module;
using Rs.MotionPlat.Vision;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Rs.MotionPlat.Flow
{
enum ETakeTrayFlowStep
{
,
,
,
,
//等待夹磁气缸松开,
,
,
,
,
,
//到拍照位,
//等待运动到拍照位,
//通知上料仓拍照,
//处理上料仓拍照数据,
,
,
,
,
,
,
,
,
,
,
,
}
public enum ETakeStatus
{
Idle,
Taking,
TakeOK
}
public enum ELoadUnloadType
{
Load,
Unload
}
/// <summary>
/// 料盘搬运流程
/// </summary>
public class TakeTrayFlow
{
private static TakeTrayFlow intance;
string logInfo = string.Empty;
public ETakeStatus TakeStatus { get; set; } = ETakeStatus.Idle;
AlarmEntity alarmEntity = null;
public static TakeTrayFlow Instance
{
get
{
if(intance == null)
intance = new TakeTrayFlow();
return intance;
}
}
Stopwatch stopwatch = new Stopwatch();
ETakeTrayFlowStep step = ETakeTrayFlowStep.;
private ETrayType m_fromTray = ETrayType.Empty;
private ETrayType m_toTray = ETrayType.Ok;
private bool finisehd = true;
TargetPosition targetPosition = new TargetPosition();
ErrorCode errCode = ErrorCode.Ok;
int fromStockIndex = 2;
int toStockIndex = 1;
VisionResult vr = new VisionResult();
/// <summary>
/// 料盘搬运
/// </summary>
/// <param name="fromTray">需要搬运料仓的料盘</param>
/// <param name="toTray">料盘搬运的目的料仓</param>
/// <param name="takeTrayFinishedEvent">搬运料盘被拿起后事件</param>
/// <param name="arrivedDumpPosEvent">到达放料盘位上方事件</param>
/// <param name="dumpTrayFinishedEvent">放料盘完成后事件</param>
public void Take(ETrayType fromTray, ETrayType toTray,bool isNeedGoDown=true,bool placeTrayGrab=false)
{
switch (fromTray)
{
case ETrayType.Input:
fromStockIndex = 2;
break;
case ETrayType.Empty:
fromStockIndex = 3;
break;
default:
break;
}
switch (toTray)
{
case ETrayType.Ok:
toStockIndex = 1;
break;
case ETrayType.Input:
toStockIndex = 2;
break;
case ETrayType.Empty:
toStockIndex = 3;
break;
default:
break;
}
if (!finisehd)
return;
else
finisehd = false;
TakeStatus = ETakeStatus.Taking;
step = ETakeTrayFlowStep.;
m_fromTray = fromTray;
m_toTray = toTray;
while (!finisehd)
{
if (MachineManage.Instance.MachineStatus == EMachineStatus.Stop)
{
Thread.Sleep(10);
continue;
}
switch (step)
{
case ETakeTrayFlowStep.:
if(fromTray== ETrayType.Empty)
{
targetPosition.TakeTrayX = GlobalVar.EmptyStockGrabPos + GlobalVar.ClampCenter2CameraCenterDist;
}
else if(fromTray== ETrayType.Input)
{
targetPosition.TakeTrayX = GlobalVar.InputStockGrabPos + GlobalVar.ClampCenter2CameraCenterDist;
}
else
{
Msg.ShowError($"cann't from take tray from {fromTray}");
return ;
}
if(GroupAxisMove.TakeXMove(GetClassName(), targetPosition.TakeTrayX, GlobalVar.WholeSpeed))
{
if (GlobalVar.VirtualAxis)
{
Thread.Sleep(GlobalVar.VirtualAxisMoveTime);
}
AxisPosPrint.PrintTakeTrayXTargetPos($"准备运动到料仓{m_fromTray}取料盘位上方", targetPosition, GetClassName());
step = ETakeTrayFlowStep.;
}
break;
case ETakeTrayFlowStep.:
if (Ops.IsStop("TakeTrayX") || GlobalVar.VirtualAxis)
{
if(fromTray== ETrayType.Empty)
{
StockManager.CloseTray(fromStockIndex);
}
AxisPosPrint.PrintTakeTrayXCurrentPos($"已运动到料仓{m_fromTray}取料盘位上方", GetClassName());
step = ETakeTrayFlowStep.;
}
break;
case ETakeTrayFlowStep.:
if (Ops.IsOn($"满料{fromStockIndex}对射光电"))
{
logInfo = GetClassName() + $"料仓{m_fromTray}料盘准备就绪";
MessageQueue.Instance.Insert(logInfo);
TakeTrayModule.CylinderGoDown(GetClassName());
stopwatch.Restart();
step = ETakeTrayFlowStep.;
}
else
{
alarmEntity = AlarmCollection.Get(AlarmConstID.);
Msgbox.ShowDialog(alarmEntity, EButtonType.Retry);
}
break;
case ETakeTrayFlowStep.:
if (Ops.IsOn("料盘搬运气缸动位"))
{
logInfo = GetClassName() + $"料盘搬运气缸动位 OK";
MessageQueue.Instance.Insert(logInfo);
stopwatch.Stop();
//打开真空
TakeTrayModule.VacOperation(EIoOperate.Open,GetClassName());
//松开夹爪
//StockModule.ClampTrayCylinder(EIoOperate.Open, fromStockIndex,GetClassName());
StockManager.OpenTray(fromStockIndex);
stopwatch.Restart();
step = ETakeTrayFlowStep.;
}
else if (stopwatch.ElapsedMilliseconds > 5000)
{
alarmEntity = AlarmCollection.Get(AlarmConstID.);
if (alarmEntity != null)
{
Msgbox.ShowDialog (alarmEntity, EButtonType.Retry);
step = ETakeTrayFlowStep.;
}
else
{
Msg.ShowError("翻译文件中未找到料盘搬运气缸动位异常");
}
stopwatch.Restart();
}
break;
//case ETakeTrayFlowStep.等待夹磁气缸松开:
// if(Ops.IsOn($"料仓夹磁气缸{fromStockIndex}原位"))
// {
// step = ETakeTrayFlowStep.取料盘完成抬起;
// }
// else if(stopwatch.ElapsedMilliseconds>5000)
// {
// alarmEntity = AlarmCollection.Get($"料仓{fromStockIndex}夹磁气缸原位异常");
// if (alarmEntity != null)
// {
// Msgbox.ShowDialog(alarmEntity, EButtonType.Retry);
// step = ETakeTrayFlowStep.等待夹磁气缸松开;
// }
// else
// {
// Msg.ShowError($"料仓{fromStockIndex}夹磁气缸原位异常");
// }
// stopwatch.Restart();
// }
// break;
case ETakeTrayFlowStep.:
TakeTrayModule.CylinderGoUp(GetClassName());
stopwatch.Restart();
step = ETakeTrayFlowStep.;
break;
case ETakeTrayFlowStep.:
if ((Ops.IsOn("料盘搬运气缸原位")))
{
logInfo = GetClassName()+$"检测到上下气缸原位 OK";
MessageQueue.Instance.Insert(logInfo);
stopwatch.Stop();
step = ETakeTrayFlowStep.;
}
else if(stopwatch.ElapsedMilliseconds > 5000)
{
alarmEntity = AlarmCollection.Get(AlarmConstID.);
if (alarmEntity != null)
{
MessageQueue.Instance.Warn(GetClassName() + alarmEntity.CN);
Msgbox.ShowDialog(alarmEntity, EButtonType.Retry);
}
else
{
Msg.ShowError("翻译文件中未找到料盘搬运气缸原位异常");
}
stopwatch.Restart();
}
break;
case ETakeTrayFlowStep.:
if (TakeTrayModule.HasTray())
{
logInfo = GetClassName() + $"料盘已被抓起 OK";
MessageQueue.Instance.Insert(logInfo);
step = ETakeTrayFlowStep.;
}
else
{
alarmEntity = AlarmCollection.Get(AlarmConstID.).Transform(fromTray.ToString());
if (alarmEntity != null)
{
Msgbox.ShowDialog(alarmEntity, EButtonType.Retry);
step = ETakeTrayFlowStep.;
}
else
{
Msg.ShowError("翻译文件中未找到抓料盘未抓起");
}
}
break;
case ETakeTrayFlowStep.:
StockManager.Up(fromStockIndex);
logInfo = GetClassName() + $"料仓{fromTray}上升一层";
MessageQueue.Instance.Insert(logInfo);
step = ETakeTrayFlowStep.;
break;
case ETakeTrayFlowStep.:
if(fromTray== ETrayType.Input)
{
step = ETakeTrayFlowStep.;
}
else
{
step = ETakeTrayFlowStep.;
}
break;
case ETakeTrayFlowStep.:
StockGrabFlow.Instance.Grab(fromTray);
if(placeTrayGrab)
{
step = ETakeTrayFlowStep.;
}
else
{
step = ETakeTrayFlowStep.;
}
break;
case ETakeTrayFlowStep.:
StockGrabFlow.Instance.Grab(toTray);
step = ETakeTrayFlowStep.;
break;
//case ETakeTrayFlowStep.到拍照位:
// targetPosition.TakeTrayX = GlobalVar.InputStockGrabPos;
// if(GroupAxisMove.TakeXMove(GetClassName(), targetPosition.TakeTrayX, GlobalVar.WholeSpeed))
// {
// logInfo = GetClassName() + $"到料仓{toTray}拍照位";
// MessageQueue.Instance.Insert(logInfo);
// step = ETakeTrayFlowStep.等待运动到拍照位;
// }
// break;
//case ETakeTrayFlowStep.等待运动到拍照位:
// if(Ops.IsStop("TakeTrayX"))
// {
// step = ETakeTrayFlowStep.通知上料仓拍照;
// }
// break;
//case ETakeTrayFlowStep.通知上料仓拍照:
// if(GlobalVar.RunSpace)
// {
// step = ETakeTrayFlowStep.到放料盘上方;
// }
// else
// {
// vr = VisionHelper.Grab(Vision.EVisionScene.HaveOrNot);
// if (vr.Result)
// {
// step = ETakeTrayFlowStep.处理上料仓拍照数据;
// }
// else
// {
// Msg.ShowError("有无相机拍照失败,点击确定后重拍");
// }
// }
// break;
//case ETakeTrayFlowStep.处理上料仓拍照数据:
// List<int> noHasProduct = new List<int>();
// for(int i=0;i<vr.Products.Length;i++)
// {
// if (vr.Products[i]=='1')
// {
// GlobalTray.InputTray.ChangeStatus(i + 1, ESlotStatus.Have);
// }
// else
// {
// noHasProduct.Add(i + 1);
// GlobalTray.InputTray.ChangeStatus(i + 1, ESlotStatus.NotHave);
// }
// }
// if(noHasProduct.Count>0)
// {
// EButtonType btn = Msgbox.ShowDialog(EButtonType.Retry | EButtonType.Skip, "料盘未放满,请处理","tray info",true);
// if(btn== EButtonType.Retry)
// {
// step = ETakeTrayFlowStep.通知上料仓拍照;
// }
// else if(btn== EButtonType.Skip)
// {
// step = ETakeTrayFlowStep.到放料盘上方;
// }
// }
// else
// {
// step = ETakeTrayFlowStep.到放料盘上方;
// }
// break;
case ETakeTrayFlowStep.:
//if (DischargeFlow.Instance.XYCanGoLocalArea())
{
if(toTray== ETrayType.Ok)
{
targetPosition.TakeTrayX = GlobalVar.OkStockGrabPos + GlobalVar.ClampCenter2CameraCenterDist;
}
else if(toTray== ETrayType.Input)
{
targetPosition.TakeTrayX = GlobalVar.InputStockGrabPos + GlobalVar.ClampCenter2CameraCenterDist;
}
else if(toTray== ETrayType.Empty)
{
targetPosition.TakeTrayX = GlobalVar.EmptyStockGrabPos + GlobalVar.ClampCenter2CameraCenterDist;
}
//takeTrayFinishedEvent?.Invoke();
if(GroupAxisMove.TakeXMove(GetClassName(),targetPosition.TakeTrayX,GlobalVar.WholeSpeed))
{
if (GlobalVar.VirtualAxis)
{
Thread.Sleep(GlobalVar.VirtualAxisMoveTime);
}
logInfo = GetClassName() + $"到{toTray}料仓放料盘上方,tx:{targetPosition.TakeTrayX}";
MessageQueue.Instance.Insert(logInfo);
step = ETakeTrayFlowStep.;
}
}
//else
//{
// //alarmEntity = AlarmCollection.Get(AlarmConstID.运动不安全).Transform("LoadY", "TurnvoerY");
// //AlarmMessageBox.ShowDialog(alarmEntity, ETipButton.Ok, null);
//}
break;
case ETakeTrayFlowStep.:
if (Ops.IsStop("TakeTrayX") || GlobalVar.VirtualAxis)
{
//if(toTray== ETrayType.Empty)
//{
// if(StockManager.HasTray(3))
// {
// StockManager.Down(3);
// }
//}
//StockManager.OpenTray(toStockIndex);
logInfo = GetClassName() + $"已运动到{toTray}料仓放料盘上方";
MessageQueue.Instance.Insert(logInfo);
//arrivedDumpPosEvent?.Invoke();
step = ETakeTrayFlowStep.;
}
break;
case ETakeTrayFlowStep.:
//if ((StockManager.Instance.GetStock(m_toTray).Status == EStockTrayStatus.Unloaded || StockManager.Instance.GetStock(m_toTray).Status == EStockTrayStatus.Empty) || GlobalVar.VirtualAxis)
{
StockManager.OpenTray(toStockIndex);
TakeTrayModule.CylinderGoDown(GetClassName());
step = ETakeTrayFlowStep.;
stopwatch.Restart();
}
break;
case ETakeTrayFlowStep.:
if (Ops.IsOn("料盘搬运气缸动位"))
{
logInfo = GetClassName() + "检测到料盘搬运气缸动位 OK";
MessageQueue.Instance.Insert(logInfo);
stopwatch.Stop();
//if(toTray!= ETrayType.Empty)
//{
// //夹住料盘
// Ops.On($"料仓夹磁气缸{toStockIndex}夹");
// Ops.Off($"料仓夹磁气缸{toStockIndex}开");
// Thread.Sleep(100);
//}
////关闭真空吸
//Ops.On("料盘真空关");
//Ops.Off("料盘真空开");
//logInfo = GetClassName() + $"料盘真空关 on";
//MessageQueue.Instance.Insert(logInfo);
//Thread.Sleep(100);
if(isNeedGoDown)
{
StockManager.Down(toStockIndex);
}
StockManager.CloseTray(toStockIndex);
//关闭真空吸
TakeTrayModule.VacOperation(EIoOperate.Close,GetClassName());
step = ETakeTrayFlowStep.;
}
else if(stopwatch.ElapsedMilliseconds>5000)
{
alarmEntity = AlarmCollection.Get(AlarmConstID.);
if (alarmEntity != null)
{
LogHelper.Debug(GetClassName() + alarmEntity.CN);
Msg.ShowError(alarmEntity.CN);
//AlarmMessageBox.ShowDialog(alarmEntity, ETipButton.Ok, null);
}
else
{
Msg.ShowError("翻译文件中未找到上下气缸动位");
}
stopwatch.Stop();
stopwatch.Restart();
}
break;
case ETakeTrayFlowStep.:
TakeTrayModule.CylinderGoUp(GetClassName());
step = ETakeTrayFlowStep.;
stopwatch.Restart();
break;
case ETakeTrayFlowStep.:
if (Ops.IsOn("料盘搬运气缸原位"))
{
logInfo = GetClassName() + "料盘搬运气缸原位 OK";
MessageQueue.Instance.Insert(logInfo);
step = ETakeTrayFlowStep.;
}
else if(stopwatch.ElapsedMilliseconds>5000)
{
alarmEntity = AlarmCollection.Get(AlarmConstID.);
if (alarmEntity != null)
{
LogHelper.Debug(GetClassName() + alarmEntity.CN);
Msg.ShowError(alarmEntity.CN);
//AlarmMessageBox.ShowDialog(alarmEntity, ETipButton.Ok, null);
}
else
{
Msg.ShowError("翻译文件中未找到上下气缸动位");
}
stopwatch.Stop();
stopwatch.Restart();
}
break;
case ETakeTrayFlowStep.:
if (!TakeTrayModule.HasTray())
{
logInfo = GetClassName() + $"放料盘完成";
MessageQueue.Instance.Insert(logInfo);
step = ETakeTrayFlowStep.;
if(toTray== ETrayType.Empty)
{
StockManager.CloseTray(toStockIndex);
StockManager.OpenTray(toStockIndex);
}
//if (mLoadUnloadType == ELoadUnloadType.Load)
//{
// StockManager.Instance.Load(m_toTray, EStockTrayLoadMode.AfterTakeTray);
//}
}
else
{
step = ETakeTrayFlowStep.;
//粘料盘
//logInfo = "检测到料盘未放下,请处理后点击确定重放";
//MessageQueue.Instance.Warn(GetClassName() + logInfo);
//TestCenterMessageBox.Show(AlarmConstID.UpDownCylinderExceptionAlarm, logInfo, ETipButton.Ok);
//TestCenterMessageBox.WaitResult(AlarmConstID.UpDownCylinderExceptionAlarm);
//step = ETakeTrayFlowStep.到放料盘上方;
}
break;
case ETakeTrayFlowStep.:
if(GroupAxisMove.TakeXMove(GetClassName(), GlobalVar.TakeTrayXWaitPos, GlobalVar.WholeSpeed))
{
logInfo = GetClassName() + $"到待机位";
MessageQueue.Instance.Insert(logInfo);
step = ETakeTrayFlowStep.;
}
break;
case ETakeTrayFlowStep.:
if(Ops.IsStop("TakeTrayX"))
{
logInfo = GetClassName() + $"从{fromTray}搬运到{toTray}完成";
MessageQueue.Instance.Insert(logInfo);
TakeStatus = ETakeStatus.TakeOK;
//dumpTrayFinishedEvent?.Invoke();
finisehd = true;
}
break;
}
Thread.Sleep(10);
}
}
public string GetCurStep()
{
return step.ToString();
}
public string GetClassName()
{
return this.GetType().Name + "-";
}
private void PrintXYCurrentPos(string prefixLog)
{
//LogHelper.Debug(GetClassName() + $"{prefixLog}当前位置:cx:{Ops.GetCurPosition(AxisControl.LoadX)},cy:{Ops.GetCurPosition(AxisControl.LoadY)}");
}
}
}