using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Web.UI; using System.Windows.Threading; using Gumming.Execute.Station; using Gumming.Execute.Workflow; using Control = System.Windows.Forms.Control; namespace Gumming.Execute { public class Context { public BasketView View; public static string CA = "ca"; public static string COT = "cot"; public static string DEV = "dev"; public static string HP = "hp"; public static string CP = "cp"; public static string CS = "cs"; public Dictionary StationUnitMap = new Dictionary(); public Dispatcher Dispatcher = Dispatcher.CurrentDispatcher; public Robot Robot { get; } public List Wafers { get; } = new List(); public FlowUnit[] Flow { get; } = new FlowUnit[] { new FlowUnit(CA, 1), new FlowUnit(COT, 2), new FlowUnit(HP, 3), new FlowUnit(CP, 4), new FlowUnit(CS, 5) }; public Context(int waferCount, Robot robot, BasketView basketView) { View = basketView; Robot = robot; for (int i = 0; i < waferCount; i++) { Wafers.Add(new Wafer($"wafer{i}", Flow, i)); } } // 标记为wafer已做完,释放cot public void DoneWafer(StationWork stationWork, Wafer wafer) { if (wafer.Works.Count == Flow.Length) { Console.WriteLine($"wafer {wafer.Name} has finished"); AppendLog($"wafer {wafer.Name} has finished"); wafer.Done = true; StationUnitMap[stationWork.FlowUnit.Name].ReleasePos(wafer.CurrentPos); } } public FlowUnit GetFlowUnitByStation(string stationName) { foreach (var flowUnit in Flow) { if (flowUnit.Name == stationName) { return flowUnit; } } return null; } //先从机器人手臂上取 private RobotHand GetTodoWaferByRobot(FlowUnit flowUnit, int pos) { foreach (var robotHand in Robot.Hands) { lock (Robot._lock) { //是上一个工位的wafer if (robotHand.CurrentWafer != null && !robotHand.IsFree && robotHand.CurrentWafer.CurrentStationWork != null && robotHand.CurrentWafer.CurrentStationWork.FlowUnit != null && robotHand.CurrentWafer.CurrentStationWork.FlowUnit.Index == flowUnit.Index - 1 && robotHand.CurrentWafer.CurrentStationWork.Done) { var beginLog = $"{robotHand.CurrentWafer.Name} do from Robot {robotHand.Name} to {flowUnit.Name}{pos} [:RobotToStation] begin by Robot {robotHand.Name} {DateTime.Now}"; Console.WriteLine(beginLog); AppendLog(beginLog); RobotExecute(flowUnit.Name + pos, robotHand.CurrentWafer.No, robotHand.GetRobotCode("Put")); var endLog = $"{robotHand.CurrentWafer.Name} do from Robot {robotHand.Name} to {flowUnit.Name}{pos} [:RobotToStation] end by Robot {robotHand.Name} {DateTime.Now}"; Console.WriteLine(endLog); AppendLog(endLog); return robotHand; } } } return null; } public RobotHand GetTodoWaferByFlow(FlowUnit flowUnit, int pos) { var robotWafer = GetTodoWaferByRobot(flowUnit, pos); if (robotWafer != null) { return robotWafer; } var hand = Robot.GetFreeHand(1); if (hand == null) { return null; } else { // 如果我是第一个工艺,从花篮取 if (flowUnit.Index == 1) { foreach (var wafer in Wafers) { if (wafer.Works.Count == 0) { wafer.CurrentPos = pos; hand.CurrentWafer = wafer; // Wafer currentWafer = GetCurrentWaferByFlow(flowUnit.Name, pos, hand); // 模拟等待 lock (Robot._lock) { var beginLog = $"{wafer.Name} do from cs0 to ca0 [:robot] begin by Robot {hand.Name} {DateTime.Now}"; Console.WriteLine(beginLog); AppendLog(beginLog); RobotExecute("cs0", wafer.No, hand.GetRobotCode("Get")); RobotExecute("ca0", wafer.No, hand.GetRobotCode("Put")); var endLog = $"{wafer.Name} do from cs0 to ca0 [:robot] end by Robot {hand.Name} {DateTime.Now}"; Console.WriteLine(endLog); AppendLog(endLog); } return hand; } } } else { foreach (var wafer in Wafers) { // 当前晶圆 当前工艺为空 且最后一个工艺做完,且最后一个工艺是本工艺的上一个 if (!wafer.Done && wafer.CurrentFlow != null) { if (wafer.Works.Count > 0) { var lastWork = wafer.Works[wafer.Works.Count - 1]; if (lastWork.Done && lastWork.FlowUnit.Index == flowUnit.Index - 1) { // wafer.CurrentPos = pos; hand.CurrentWafer = wafer; // 模拟等待 lock (Robot._lock) { var beginLog = $"{wafer.Name} do from {lastWork.StationName} to {flowUnit.Name}{pos} [:robot] begin by Robot {hand.Name} {DateTime.Now}"; Console.WriteLine(beginLog); AppendLog(beginLog); RobotExecute(lastWork.StationName, wafer.No, hand.GetRobotCode("Get")); RobotExecute(flowUnit.Name + pos, wafer.No, hand.GetRobotCode("Put")); var endLog = $"{wafer.Name} do from {lastWork.StationName} to {flowUnit.Name}{pos} [:robot] end by Robot {hand.Name} {DateTime.Now}"; Console.WriteLine(endLog); AppendLog(endLog); } return hand; } } } } } } Robot.ReleaseFreeHand(hand); return null; } public Wafer GetCurrentWaferByFlow(string stationName, int pos, RobotHand hand) { foreach (var wafer in Wafers) { // 当前晶圆 当前工艺为空 且最后一个工艺做完,且最后一个工艺是本工艺的上一个 if (!wafer.Done && wafer.CurrentFlow != null) { var current = wafer.CurrentStationWork; //当前工位做完了 if (current != null && current.Done && current.StationNo == pos && current.StationName.Equals(stationName + pos)) { var beginLog = $"{wafer.Name} do from {stationName}{pos} to Robot {hand.Name} [:StationToRobot] begin by Robot {hand.Name} {DateTime.Now}"; Console.WriteLine(beginLog); AppendLog(beginLog); lock (Robot._lock) { RobotExecute(stationName + pos, wafer.No, hand.GetRobotCode("Get")); hand.CurrentWafer = wafer; } var endLog = $"{wafer.Name} do from {stationName}{pos} to Robot {hand.Name} [:StationToRobot] end by Robot {hand.Name} {DateTime.Now}"; Console.WriteLine(endLog); AppendLog(endLog); return wafer; } } } return null; } private void RobotExecute(string key, int waferNo, int index) { int no = waferNo; if (key.Contains("cs")) { key = "cs0"; } else { no = 1; } var list = Robot.RobotCommand[key]; var getR = list[index]; string code = ""; switch (index) { case 0: case 2: code = "GETA"; break; case 1: case 3: code = "PUTA"; break; default: break; } var message = string.Format("{0} {1},{2}", code, getR, no); var callBack = GummingControl.Robot.Send(message); // if (callBack != ">") // { Console.WriteLine($"robot send message={message} waferNo={waferNo} callback={callBack}"); // throw new Exception($"err message={message} callback={callBack}"); // } } public void AppendLog(string message) { Dispatcher.Invoke(new Action(() => { //将引发问题的代码放入此处 //便可解决此问题 /*View.log.AppendText( message + Environment.NewLine);*/ } )); } } }