using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using Aitex.Core.Util;
using MECF.Framework.Common.Alarm;
using MECF.Framework.Common.Beckhoff.Station;
using MECF.Framework.Common.CommonData.PowerSupplier;
using MECF.Framework.Common.RecipeCenter;
using MECF.Framework.Common.Routine;
using MECF.Framework.Common.Utilities;
using PunkHPX8_Core;
using PunkHPX8_RT.Devices.AXIS;
using PunkHPX8_RT.Devices.PlatingCell;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PunkHPX8_RT.Modules.PlatingCell
{
public class PlatingCellDepositionRoutine : RoutineBase, IRoutine
{
private enum RunRecipeStep
{
RunPowerStep,
RunPowerStepWait,
LoopRotationStart,
LoopVerticalGotoOffSet,
LoopVerticalGotoOffSetCheck,
LoopRotationStartIfCurrentIsStoped,//如果当前电机是停止状态,启动起来
LoopRotationChangeSpeed,
LoopRotationDelay,
LoopRotationStopIfCurrentIsRuning, //如果当前电机是启动状态,则停止
LoopROtationStopCheck,
LoopRotationBiRotation,
LoopRotationBiRotationCheck,
LoopEnd,
RotationStartIfCurrentIsStoped,
End
}
#region 常量
///
/// ROTATION电机转速比例
///
private const int SPEED_RATIO = 1;
private const int ROTATION_FAR_POSITION = 12 * 60 * 60 * 500 * 6; //以500rmp 运行12个小时的位置
#endregion
///
/// recipe
///
private DepRecipe _recipe;
///
/// Rotation axis
///
private JetAxisBase _rotationAxis;
///
/// Platingcell device
///
private PlatingCellDevice _device;
///
/// vertical axis entity
///
private PlatingCellVerticalEntity _verticalEntity;
///
/// 不通电
///
private bool _isZeroCurrent = false;
///
/// 是否启动smart spin
///
private bool _isSmartSpin = false;
///
/// Power step集合
///
List _powerSupplierStepPeriodDatas = new List();
///
/// 双向旋转routine
///
private RotationBiDirectionRoutine _rotationBiDirectionRoutine;
///
/// Plating启动时间
///
private DateTime _platingStartTime;
///
/// 当前执行到电镀步骤的索引
///
private int _depositionStepIndex = 0;
///
/// 构造函数
///
///
public PlatingCellDepositionRoutine(string module) : base(module)
{
_rotationBiDirectionRoutine = new RotationBiDirectionRoutine(module);
}
///
/// 中止
///
public void Abort()
{
Runner.Stop("Manual Abort");
}
///
/// 监控
///
///
public RState Monitor()
{ //没有上电保护,此刻给电
Runner.RunIf(RunRecipeStep.RunPowerStep, _recipe.IsEntryTypeCold, StartPowerStep, _delay_1ms)
//检验步阶电流是否完成
.WaitWithStopCondition(RunRecipeStep.RunPowerStepWait, CheckRecipeStepEndStatus, CheckRecipeStepStopStatus, false, _delay_1ms)
.LoopStart(RunRecipeStep.LoopRotationStart, "Start rotation cycle",_recipe.DepStepCount,NullFun)
//vertical 调整位置(第一步不用调,entry过程已经走到位置了)
.LoopRunIf(RunRecipeStep.LoopVerticalGotoOffSet, _recipe.DepSteps[_depositionStepIndex].PlatingZoffset!=0 && _depositionStepIndex!=0, () => StartVertical("Plate", _recipe.DepSteps[_depositionStepIndex].PlatingZoffset),_delay_1ms)
.LoopRunIfWithStopStatus(RunRecipeStep.LoopVerticalGotoOffSetCheck, _recipe.DepSteps[_depositionStepIndex].PlatingZoffset!=0 && _depositionStepIndex != 0, CheckVerticalEnd, CheckVerticalError)
//不带双向旋转,时间到了直接变速(如果当前电机是停止状态,启动起来)
.LoopRunIf(RunRecipeStep.LoopRotationStartIfCurrentIsStoped, CheckRotationIsIdle() && !_recipe.DepSteps[_depositionStepIndex].BiDireaction,
() => { return StartRotation(ROTATION_FAR_POSITION); }, _delay_1ms)
.LoopRunIf(RunRecipeStep.LoopRotationChangeSpeed, !_recipe.DepSteps[_depositionStepIndex].BiDireaction,() => ChangeRotationSpeed(_recipe.DepSteps[_depositionStepIndex].PlatingSpeed),_delay_1ms)
.LoopDelayIf(RunRecipeStep.LoopRotationDelay, !_recipe.DepSteps[_depositionStepIndex].BiDireaction, _recipe.DepSteps[_depositionStepIndex].DurartionSeconds *1000)
//带双向旋转,启动双向旋转的routine(如果当前电机是启动状态,则停止)
.LoopRunIf(RunRecipeStep.LoopRotationStopIfCurrentIsRuning, checkRotationIsRunning() && _recipe.DepSteps[_depositionStepIndex].BiDireaction,
_rotationAxis.StopPositionOperation, _delay_1ms)
.LoopRunIfWithStopStatus(RunRecipeStep.LoopROtationStopCheck, checkRotationIsRunning() && _recipe.DepSteps[_depositionStepIndex].BiDireaction,
CheckRotationIsIdle, CheckRotationPositionRunStop)
.LoopRunIf(RunRecipeStep.LoopRotationBiRotation, _recipe.DepSteps[_depositionStepIndex].BiDireaction,
() => _rotationBiDirectionRoutine.Start(_recipe.DepSteps[_depositionStepIndex].DurartionSeconds, _recipe.DepSteps[_depositionStepIndex].BiDFrequency, _recipe.DepSteps[_depositionStepIndex].PlatingSpeed) == RState.Running,_delay_1ms)
.LoopRunIfWithStopStatus(RunRecipeStep.LoopRotationBiRotationCheck, _recipe.DepSteps[_depositionStepIndex].BiDireaction,
() => CommonFunction.CheckRoutineEndState(_rotationBiDirectionRoutine),
() => CommonFunction.CheckRoutineStopState(_rotationBiDirectionRoutine))
.LoopEnd(RunRecipeStep.LoopEnd, UpdateDepositionIndex, _delay_1ms)
//如果电镀最后一步带双向旋转,后续电机会停止,需要再把电机启动起来
.RunIf(RunRecipeStep.RotationStartIfCurrentIsStoped, CheckRotationIsIdle(), () => { return StartRotation(ROTATION_FAR_POSITION); }, _delay_1ms)
.End(RunRecipeStep.End, NullFun);
return Runner.Status;
}
///
/// 判断当前电机是否在转
///
///
private bool checkRotationIsRunning()
{
return _rotationAxis.Status == RState.Running;
}
///
/// 判断电机当前状态是否停止
///
///
private bool CheckRotationIsIdle()
{
return _rotationAxis.Status == RState.End;
}
///
/// 检验Rotation是否运动失败
///
///
private bool CheckRotationPositionRunStop()
{
return _rotationAxis.Status == RState.Failed || _rotationAxis.Status == RState.Timeout;
}
///
/// 更新电镀步骤索引
///
///
private bool UpdateDepositionIndex()
{
if (_depositionStepIndex < _recipe.DepStepCount - 1)
{
_depositionStepIndex++;
}
return true;
}
///
/// 启动PowerSupplier
///
///
private bool StartPowerStep()
{
bool result = _device.PowerSupplier.StartSetStepPeriodNoWaitEnd(_powerSupplierStepPeriodDatas);
if (!result)
{
_device.PowerSupplier.DisableOperation("", null);
return false;
}
_platingStartTime = DateTime.Now;
return true;
}
///
/// 检验Powerstep是否启动完成
///
///
private bool CheckRecipeStepEndStatus()
{
if (_isZeroCurrent)
{
return true;
}
return _device.PowerSupplier.Status == RState.End;
}
///
/// 检验Powerstep是否启动失败
///
///
private bool CheckRecipeStepStopStatus()
{
if (_isZeroCurrent)
{
return false;
}
return _device.PowerSupplier.Status == RState.Failed || _device.PowerSupplier.Status == RState.Timeout;
}
///
/// rotation开始旋转
///
///
///
private bool StartRotation(int targetPosition)
{
bool result = _rotationAxis.ProfilePosition(targetPosition, _recipe.IntervalRinseSpeed * SPEED_RATIO * 6, 0, 0); //rpm->deg/s
if (!result)
{
NotifyError(eEvent.ERR_PLATINGCELL, "Start Rotation is failed", 0);
return false;
}
return true;
}
///
/// rotation改变速度
///
///
///
private bool ChangeRotationSpeed(int speed)
{
double _scale = _rotationAxis.ScaleFactor;
speed = (int)Math.Round(_scale * BeckhoffVelocityUtil.ConvertVelocityToDegPerSecondByRPM(speed), 0);
return _rotationAxis.ChangeSpeed(speed);
}
///
/// vertical 运行
///
/// 目标位置名称
/// 偏移量
///
private bool StartVertical(string positionName, double offset)
{
return _verticalEntity.CheckToPostMessage(Aitex.Core.RT.Log.eEvent.INFO_PLATINGCELL,
Module, (int)PlatingCellVerticalEntity.VerticalMsg.Position, positionName, offset);
}
///
/// 检验垂直电机是否运动完成
///
///
private bool CheckVerticalEnd()
{
return _verticalEntity.IsIdle;
}
///
/// 检验垂直是否出现错误
///
///
private bool CheckVerticalError()
{
return _verticalEntity.IsError;
}
///
/// 检验电流和电压合理性
///
///
///
///
///
private bool CheckSidePowerInvalid(double current, double voltage)
{
double maxVoltage = _recipe.DepMaxVoltageFault;
double warnVoltage = _recipe.DepMaxVoltageWarning;
if (voltage > maxVoltage)
{
NotifyError(eEvent.ERR_PLATINGCELL, $" voltage {voltage} is large than recipe max voltage {maxVoltage}", 0);
return true;
}
if (voltage > warnVoltage)
{
string str = $" voltage is {voltage} in warning";
if (AlarmListManager.Instance.AddWarn(Module, $"voltage", str))
{
LOG.WriteLog(eEvent.WARN_METAL, Module, str);
}
}
double maxErrorCurrent = _powerSupplierStepPeriodDatas[_depositionStepIndex].Current * (double)(1 + _recipe.DepCurrentFault * 0.01);
double minErrorCurrent = _powerSupplierStepPeriodDatas[_depositionStepIndex].Current * (double)(1 - _recipe.DepCurrentFault * 0.01);
double maxWarnCurrent = _powerSupplierStepPeriodDatas[_depositionStepIndex].Current * (double)(1 + _recipe.DepCurrentWarning * 0.01);
double minWarnCurrent = _powerSupplierStepPeriodDatas[_depositionStepIndex].Current * (double)(1 - _recipe.DepCurrentWarning * 0.01);
if (current > maxErrorCurrent)
{
NotifyError(eEvent.ERR_PLATINGCELL, $" current {current} is large than recipe max current {maxErrorCurrent}", 0);
return true;
}
if (current < minErrorCurrent)
{
NotifyError(eEvent.ERR_PLATINGCELL, $" current {current} is less than recipe min current {minErrorCurrent}", 0);
return true;
}
if ((current <= maxErrorCurrent && current >= maxWarnCurrent) || (current >= minErrorCurrent && current <= minWarnCurrent))
{
string str = $" current {current} is in warning";
if (AlarmListManager.Instance.AddWarn(Module, $" current", str))
{
LOG.WriteLog(eEvent.ERR_PLATINGCELL, Module, str);
}
}
return false;
}
///
/// 启动
///
///
///
public RState Start(params object[] objs)
{
_recipe = (DepRecipe)objs[0];
_isZeroCurrent = (bool)objs[1];
_powerSupplierStepPeriodDatas = (List)objs[2];
_rotationAxis = DEVICE.GetDevice($"{Module}.Rotation");
_device = DEVICE.GetDevice(Module);
_depositionStepIndex = 0;
//获取vertical entity
string vertical = ModuleMatcherManager.Instance.GetPlatingVerticalByCell(Module);
_verticalEntity = Singleton.Instance.GetModule(vertical);
return Runner.Start(Module, "start Deposition routine");
}
}
}