using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using CyberX8_Core;
using CyberX8_RT.Devices.AXIS;
using CyberX8_RT.Devices.SRD;
using MECF.Framework.Common.Routine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using static CyberX8_RT.Modules.SRD.UnloadingStateMachine;
namespace CyberX8_RT.Modules.SRD
{
public class SRDUnloadRoutine : RoutineBase, IRoutine
{
private enum UnloadStep
{
Unloading_Start,
Unloading_CheckArmHomed,
Unloading_CheckRotationHomed,
Unloading_WaferPresent,
Unloading_OpenDoor,
Unloading_CheckDoorOpened,
Unloading_ReleaseChuckVacuum,
Unloading_CheckVacuum,
Test,
End
}
#region 常量
///
/// Arm Home最大retry次数
///
private const int MAX_ARM_HOME_RETRIES = 3;
///
/// Rotation Home最大retry次数
///
private const int MAX_ROTATION_HOME_RETRIES = 3;
///
/// 旋转增加时长
///
private const int ROTATION_PLUS_TIME = 10;
#endregion
#region 内部变量
///
/// SRD Common
///
private SrdCommonDevice _srdCommon;
///
/// Arm Axis
///
private JetAxisBase _armAxis;
///
/// Rotation Axis
///
private JetAxisBase _rotationAxis;
///
/// Arm重试次数
///
private int _armRetryTimes = 0;
///
/// Rotation重试次数
///
private int _rotationRetryTimes = 0;
#endregion
///
/// 构造函数
///
///
public SRDUnloadRoutine(string module) : base(module)
{
}
///
/// 中止
///
public void Abort()
{
Runner.Stop("SRD Unloading Abort");
}
///
/// 监控
///
///
public RState Monitor()
{
Runner.Run(UnloadStep.Unloading_Start, NullFun, _delay_1ms)
.WaitWithStopCondition(UnloadStep.Unloading_CheckArmHomed, CheckArmEndStatus, CheckArmStopStatus)
.WaitWithStopCondition(UnloadStep.Unloading_CheckRotationHomed, CheckRotationEndStatus, CheckRotationStopStatus)
.Run(UnloadStep.Unloading_WaferPresent, CheckWaferPresent, _delay_1ms)
.Run(UnloadingState.Unloading_OpenDoor, OpenDoor, _delay_1ms)
.WaitWithStopCondition(UnloadingState.Unloading_CheckDoorOpened, CheckDoorOpenedEndStatus, CheckDoorOpenedStopStatus)
.Run(UnloadingState.Unloading_ReleaseChuckVacuum, ReleaseChuckVacuum, _delay_1ms)
.WaitWithStopCondition(UnloadStep.Unloading_CheckVacuum, CheckReleaseChuckEndStatus, CheckReleaseChuckStopStatus)
.End(UnloadStep.End, NullFun, _delay_1ms);
return Runner.Status;
}
///
/// 检验Arm home结束状态
///
///
private bool CheckArmEndStatus()
{
if (_armAxis.IsHomed && _armAxis.Status == RState.End)
{
_armRetryTimes = 0;
return true;
}
return false;
}
///
/// 检验Arm停止状态
///
///
private bool CheckArmStopStatus()
{
if (_armAxis.Status == RState.Failed || _armAxis.Status == RState.Timeout)
{
if (_armRetryTimes < MAX_ARM_HOME_RETRIES)
{
_armAxis.Home(false);
_armRetryTimes++;
}
else
{
NotifyError(eEvent.ERR_SRD, $"Arm Home Retry Home {_armRetryTimes + 1} times is over {MAX_ARM_HOME_RETRIES}", 0);
return true;
}
}
return false;
}
///
/// 检验Rotation home结束状态
///
///
private bool CheckRotationEndStatus()
{
if (_rotationAxis.IsHomed && _rotationAxis.Status == RState.End)
{
_rotationRetryTimes = 0;
return true;
}
return false;
}
///
/// 检验Rotation停止状态
///
///
private bool CheckRotationStopStatus()
{
if (_rotationAxis.Status == RState.Failed || _rotationAxis.Status == RState.Timeout)
{
if (_rotationRetryTimes < MAX_ROTATION_HOME_RETRIES)
{
_rotationAxis.Home(false);
_rotationRetryTimes++;
}
else
{
NotifyError(eEvent.ERR_SRD, $"Rotation Home Retry Home {_rotationRetryTimes + 1} times is over {MAX_ROTATION_HOME_RETRIES}", 0);
return true;
}
}
return false;
}
///
/// Check Wafer Present
///
///
///
private bool CheckWaferPresent()
{
if (_srdCommon.IsWaferPresence)
{
if (_srdCommon.WaferPresence != "WellPlaced")
{
NotifyError(eEvent.ERR_SRD, "Wafer Presence is not WellPlaced",0);
return false;
}
}
else
{
LOG.WriteLog(eEvent.INFO_SRD, Module, "CheckWaferPresent has been ignored");
}
return true;
}
///
/// Open Door
///
///
///
private bool OpenDoor()
{
return _srdCommon.DoorOpenAction("", null);
}
///
/// 检验DoorOpened
///
///
///
private bool CheckDoorOpenedEndStatus()
{
bool result = _srdCommon.Status == RState.End;
if (result)
{
if (_srdCommon.CommonData.DoorOpened && !_srdCommon.CommonData.DoorClosed)
{
return true;
}
else
{
NotifyError(eEvent.ERR_SRD, $"Opened {_srdCommon.CommonData.DoorOpened}&&Closed {_srdCommon.CommonData.DoorClosed}", 0);
return false;
}
}
return false;
}
///
/// 检验Door
///
///
private bool CheckDoorOpenedStopStatus()
{
return _srdCommon.Status == RState.Failed || _srdCommon.Status == RState.Timeout;
}
///
/// 关闭Chuck Vacuum,并检查Vacuum Level
///
///
///
private bool ReleaseChuckVacuum()
{
return _srdCommon.ChuckVacuumOffAction("", null);
}
///
/// 检验Release Chuck结束状态
///
///
private bool CheckReleaseChuckEndStatus()
{
return _srdCommon.Status==RState.End;
}
///
/// 检验Release Chuck停止状态
///
///
private bool CheckReleaseChuckStopStatus()
{
return _srdCommon.Status == RState.Failed||_srdCommon.Status==RState.Timeout;
}
///
/// 启动
///
///
///
public RState Start(params object[] objs)
{
_armAxis = DEVICE.GetDevice($"{Module}.Arm");
_rotationAxis = DEVICE.GetDevice($"{Module}.Rotation");
_srdCommon = DEVICE.GetDevice($"{Module}.Common");
return Runner.Start(Module, "Start Unload Wafer");
}
}
}