using Aitex.Core.RT.Device; using Aitex.Core.RT.Device.Unit; using Aitex.Core.RT.Event; using Aitex.Core.RT.Routine; using Aitex.Core.RT.SCCore; using Aitex.Core.Util; using Aitex.Sorter.Common; using MECF.Framework.Common.Alarms; using MECF.Framework.Common.Device.Bases; using MECF.Framework.Common.Equipment; using MECF.Framework.Common.SubstrateTrackings; using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robot; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using FurnaceRT.Equipments.Systems; using static Aitex.Core.RT.Device.Unit.IoBoat; using System.Diagnostics; namespace FurnaceRT.Equipments.Boats { public class BoatRAxisMoveCycleTest : ModuleRoutine, IRoutine { enum RoutineStep { SetBoatSpeed, SetBoatDirection, BoatRAxisHome, BoatRAxisMove, SetBoatRAxisMoveStop, SetBoatInterval, Loop, EndLoop, Delay1, Delay2, Delay3, } private BoatRotationMode _targetMode; private float _speed; private float _interval; private float _rotateTime; private BoatModule _boatModule; private int _timeout = 0; private bool _isRotate; private BoatRotationDirection _direction; private Stopwatch _timer = new Stopwatch(); private int _count; public BoatRAxisMoveCycleTest(BoatModule boatModule) { _boatModule = boatModule; Module = boatModule.Module; Name = "RAxisMoveTest"; } public void Init(string direction = "CW") { _direction = (BoatRotationDirection)Enum.Parse(typeof(BoatRotationDirection), direction); } public Result Start(params object[] objs) { Reset(); _isRotate = _targetMode != BoatRotationMode.Stop; _timeout = SC.GetValue($"{Module}.MotionTimeout"); _count = SC.GetValue($"Boat.BoatRotationServo.CycleCount"); _speed = (float)SC.GetValue($"Boat.BoatRotationServo.MoveSpeed"); _interval = (float)SC.GetValue($"Boat.BoatRotationServo.IntervalTime"); _rotateTime = (float)SC.GetValue($"Boat.BoatRotationServo.RotateTime"); Notify($"Start"); return Result.RUN; } public void Abort() { _boatModule.RAxisDevice.ServoStop(); } public override Result Monitor() { try { PauseRountine(_boatModule.RAxisDevice.IsPause); if (_boatModule.RAxisDevice.IsPause) return Result.RUN; if (_boatModule.RAxisDevice.IsError) return Result.FAIL; Loop((int)RoutineStep.Loop, _count); SetBoatRAxisMove((int)RoutineStep.BoatRAxisMove, _direction, (int)_rotateTime); SetBoatRAxisMoveStop((int)RoutineStep.SetBoatRAxisMoveStop); Delay((int)RoutineStep.Delay1, 2); Delay((int)RoutineStep.Delay2, _interval); SetBoatRAxisHome((int)RoutineStep.BoatRAxisHome, _timeout); Delay((int)RoutineStep.Delay3, _interval); EndLoop((int)RoutineStep.EndLoop); } catch (RoutineBreakException) { return Result.RUN; } catch (RoutineFaildException ex) { return Result.FAIL; } Notify("Finished"); return Result.DONE; } private void SetBoatRAxisMoveStop(int id) { Tuple ret = Execute(id, () => { Notify($"Set RAxis boat stop"); _boatModule.RAxisDevice.ServoStop(); return true; }); if (ret.Item1) { if (ret.Item2 == Result.FAIL) { throw (new RoutineFaildException()); } else throw (new RoutineBreakException()); } } private void SetBoatRAxisHome(int id, int timeout) { Tuple ret = ExecuteAndWait(id, () => { Notify($"Boat RAxis home"); if (!_boatModule.RAxisDevice.SetServoHome()) { _boatModule.BoatRAxisHomeFailed.Set(); } return true; }, () => { if (_boatModule.RAxisDevice.IsError) return null; return _boatModule.RAxisDevice.IsHomeDone && _boatModule.RAxisDevice.IsReady; }, timeout * 1000); if (ret.Item1) { if (ret.Item2 == Result.FAIL) { _boatModule.RAxisDevice.ServoStop(); throw (new RoutineFaildException()); } else if (ret.Item2 == Result.TIMEOUT) //timeout { _boatModule.RAxisDevice.ServoStop(); _boatModule.BoatRAxisHomeTimeout.Set($"can not complete in {timeout} seconds"); throw (new RoutineFaildException()); } else throw (new RoutineBreakException()); } } private void SetBoatRAxisMove(int id, BoatRotationDirection direction, int timeout) { Tuple ret = ExecuteAndWait(id, () => { Notify($"Boat RAxis {direction}"); _timer.Restart(); string reason; if (!_boatModule.RAxisDevice.SetServoMoveTo(direction.ToString(), out reason)) { //_boatModule.BoatDevice.BoatRAxisMoveFailedForInterlock.Description = reason; _boatModule.BoatRAxisMoveFailedForInterlock.Set(reason); } return true; }, () => { if (_boatModule.RAxisDevice.IsError) return null; return _boatModule.RAxisDevice.IsMoving && _timer.ElapsedMilliseconds > timeout * 1000; }, timeout * 2 * 1000); if (ret.Item1) { if (ret.Item2 == Result.FAIL) { _boatModule.RAxisDevice.ServoStop(); throw (new RoutineFaildException()); } else if (ret.Item2 == Result.TIMEOUT) //timeout { _boatModule.RAxisDevice.ServoStop(); _boatModule.BoatRAxisMoveTimeOut.Set($"can not complete in {timeout} seconds"); throw (new RoutineFaildException()); } else throw (new RoutineBreakException()); } } } }