static void Main(string[] args) { try { MainStep mainStep = new MainStep(); mainStep.Start(); } catch (Exception ex) { Console.WriteLine("Error: {0}", ex.Message); Console.WriteLine(ex.StackTrace); Console.WriteLine("\r\nPress Any Key To Exist."); Console.ReadKey(); System.Environment.Exit(0); } }
public MainStep GetStepByObj(MainStep ms) { // below are hardcoded instructions for demonstration only, all the instructions should be retrieved from a database if (ms.TaskID == "c1-1" && ms.StepNum == 1) { ms.Instrn = "Write down the current time on a piece of paper according to the wall clock in the room."; ms.TotalSubSteps = 3; ms.SubSteps = new List<SubStep>(); SubStep ss; for (int i = 0; i < ms.TotalSubSteps; i++) { ss = new SubStep(); ss.SubStepNum = i; switch (ss.SubStepNum + 1) { case 1: ss.Instrn = "Get a piece of paper and a pen from the table."; break; case 2: ss.Instrn = "Read the time from the wall clock in the room."; break; case 3: ss.Instrn = "Write down the time on the piece of paper."; break; default: ss.Instrn = "None"; break; } ms.SubSteps.Add(ss); } } else if (ms.TaskID == "c1-1" && ms.StepNum == 2) { ms.Instrn = "Find the meaning of the word \"Computer\" in the dictionary on the table, then write it down on the same piece of paper below the time."; ms.TotalSubSteps = 3; ms.SubSteps = new List<SubStep>(); SubStep ss; for (int i = 0; i < ms.TotalSubSteps; i++) { ss = new SubStep(); ss.SubStepNum = i; switch (ss.SubStepNum + 1) { case 1: ss.Instrn = "Use the dictionary on the table."; break; case 2: ss.Instrn = "Open the dictionary and look for the meaning of the search word."; break; case 3: ss.Instrn = "Write down the meaning of the search word on the paper below the time."; break; default: ss.Instrn = "None"; break; } ms.SubSteps.Add(ss); } } else if (ms.TaskID == "c1-1" && ms.StepNum == 3) { ms.Instrn = "Write down the current time, then put the pen and paper on top of the dictionary neatly."; ms.TotalSubSteps = 3; ms.SubSteps = new List<SubStep>(); SubStep ss; for (int i = 0; i < ms.TotalSubSteps; i++) { ss = new SubStep(); ss.SubStepNum = i; switch (ss.SubStepNum + 1) { case 1: ss.Instrn = "Place the dictionary on the left side of the table."; break; case 2: ss.Instrn = "Write down the time on the same piece of paper."; break; case 3: ss.Instrn = "Put the piece of paper and pen on top of the dictionary."; break; default: ss.Instrn = "None"; break; } ms.SubSteps.Add(ss); } } return ms; }
private async void MainProc() { Step = MainStep.CheckInPlace; float verticalOffset = 0; double angle = 0; float instance1 = 0; float instance2 = 0; float instance3 = 0; RobotPos robotPos1 = new RobotPos(); RobotPos robotPos2 = new RobotPos(); while (!tokenSource.IsCancellationRequested) { try { switch (Step) { case MainStep.CheckInPlace: bool isInPlace = await HardWareManager.Instance.XinJiePlc.ReadM(100); if (!isInPlace) { break; } DelegateControls.Instance.DelegateTextbox("FormMain_txt_hRanging", null); DelegateControls.Instance.DelegateTextbox("FormMain_txt_vRanging1", null); DelegateControls.Instance.DelegateTextbox("FormMain_txt_vRanging2", null); DelegateControls.Instance.DelegateTextbox("FormMain_txt_vRanging3", null); DelegateControls.Instance.DelegateTextbox("FormMain_txt_hOffset", null); DelegateControls.Instance.DelegateTextbox("FormMain_txt_vOffset", null); DelegateControls.Instance.DelegateTextbox("FormMain_instanceScrew", null); DelegateControls.Instance.DelegateTextbox("FormMain_productType", null); verticalOffset = await HardWareManager.Instance.XinJiePlc.ReadD(510); //读取横向偏移量 if (Math.Abs(verticalOffset) > 80) { DelegateControls.Instance.DelegateRichTextBoxZd("FormMain_RichTextBoxZd", "水平方向偏移超限", Color.Red); break; } DelegateControls.Instance.DelegateTextbox("FormMain_txt_hRanging", verticalOffset.ToString()); HardWareManager.Instance.XinJiePlc.SetM(101, true); // await HardWareManager.Instance.YaskawaRobot.SetRobotPoint(100, $"0,{verticalOffset} ,0,0,0,0"); DelegateControls.Instance.DelegateRichTextBoxZd("FormMain_RichTextBoxZd", "水平测距完成"); Step = MainStep.GetAngleInstance; break; case MainStep.GetAngleInstance: bool getInstance = await HardWareManager.Instance.XinJiePlc.ReadM(110); if (!getInstance) { break; } instance1 = await HardWareManager.Instance.XinJiePlc.ReadD(500); instance2 = await HardWareManager.Instance.XinJiePlc.ReadD(502); HardWareManager.Instance.XinJiePlc.SetM(111, true); instance1 = -instance1; instance2 = -instance2; DelegateControls.Instance.DelegateTextbox("FormMain_txt_vRanging1", instance1.ToString()); DelegateControls.Instance.DelegateTextbox("FormMain_txt_vRanging2", instance2.ToString()); await HardWareManager.Instance.YaskawaRobot.SetRobotPoint(13, $"{instance1},{verticalOffset},0,0,0,{0}"); Thread.Sleep(300); await HardWareManager.Instance.YaskawaRobot.SetRobotPoint(14, $"{instance1},{verticalOffset},0,0,0,{0}"); Thread.Sleep(300); await HardWareManager.Instance.YaskawaRobot.SetRobotPoint(15, $"{instance2},{verticalOffset},0,0,0,{0}"); Thread.Sleep(300); await HardWareManager.Instance.YaskawaRobot.SetTypeBData(3, "1"); DelegateControls.Instance.DelegateRichTextBoxZd("FormMain_RichTextBoxZd", "垂直测距完成"); Step = MainStep.GetHolePoint2; break; case MainStep.GetInstance://获取两个差距值,用来计算角度 //bool getInstance1 = await HardWareManager.Instance.XinJiePlc.ReadM(120); //if (!getInstance1) // break; //instance3 = await HardWareManager.Instance.XinJiePlc.ReadD(504); //instance3 = -instance3; //HardWareManager.Instance.XinJiePlc.SetM(121, true); //DelegateControls.Instance.DelegateTextbox("FormMain_txt_vRanging3", instance3.ToString()); //Step = MainStep.GetHolePoint2; break; case MainStep.GetHolePoint1: string rtn = await HardWareManager.Instance.YaskawaRobot.GetTypeBData(1); if (rtn != "2\r\0") { break; } bool getInstance1 = await HardWareManager.Instance.XinJiePlc.ReadM(120); if (!getInstance1) { break; } instance3 = await HardWareManager.Instance.XinJiePlc.ReadD(504); // instance3 = -instance3; HardWareManager.Instance.XinJiePlc.SetM(121, true); DelegateControls.Instance.DelegateTextbox("FormMain_txt_vRanging3", instance3.ToString()); HObject _image = HardWareManager.Instance.Cmaera.SnapShot(); robotPos1 = await GetHolePoint(_image); // length = 700 - length; await HardWareManager.Instance.YaskawaRobot.SetRobotPoint(100, $"{-lengthX},{-lengthY},{instance3},0,0,{0}"); DelegateControls.Instance.DelegateTextbox("FormMain_txt_hOffset", (lengthY).ToString()); DelegateControls.Instance.DelegateTextbox("FormMain_txt_vOffset", (lengthX).ToString()); await HardWareManager.Instance.YaskawaRobot.SetTypeBData(2, "2"); DelegateControls.Instance.DelegateTextbox("FormMain_instanceScrew", Math.Abs(robotPos1.Y - robotPos2.Y).ToString()); if ((robotPos1.Y - robotPos2.Y) < Param.Instance.InstanceMax + Param.Instance.InstanceOffset && (robotPos1.Y - robotPos2.Y) > Param.Instance.InstanceMax - Param.Instance.InstanceOffset) { DelegateControls.Instance.DelegateTextbox("FormMain_productType", "大号产品"); await HardWareManager.Instance.YaskawaRobot.SetTypeBData(5, "3"); } else if (Math.Abs(robotPos1.Y - robotPos2.Y) < Param.Instance.InstanceLittle + Param.Instance.InstanceOffset && (robotPos1.Y - robotPos2.Y) > Param.Instance.InstanceLittle - Param.Instance.InstanceOffset) { DelegateControls.Instance.DelegateTextbox("FormMain_productType", "小号产品"); await HardWareManager.Instance.YaskawaRobot.SetTypeBData(5, "1"); } else if (Math.Abs(robotPos1.Y - robotPos2.Y) < Param.Instance.InstanceMiddle + Param.Instance.InstanceOffset && (robotPos1.Y - robotPos2.Y) > Param.Instance.InstanceMiddle - Param.Instance.InstanceOffset) { DelegateControls.Instance.DelegateTextbox("FormMain_productType", "中号产品"); await HardWareManager.Instance.YaskawaRobot.SetTypeBData(5, "2"); } else { throw new Exception("螺丝孔位置 无法匹配产品"); } DelegateControls.Instance.DelegateRichTextBoxZd("FormMain_RichTextBoxZd", "第二次拍照完成"); Step = MainStep.CheckInPlace; break; case MainStep.GetHolePoint2: rtn = await HardWareManager.Instance.YaskawaRobot.GetTypeBData(1); if (rtn != "1\r\0") { break; } _image = HardWareManager.Instance.Cmaera.SnapShot(); robotPos2 = await GetHolePoint(_image); await HardWareManager.Instance.YaskawaRobot.SetTypeBData(2, "1"); Step = MainStep.GetHolePoint1; DelegateControls.Instance.DelegateRichTextBoxZd("FormMain_RichTextBoxZd", "第一次拍照完成"); break; //VisionManager.Instance.ShapeModle.BackImage = _image.Clone(); //ModleFindResult result = VisionManager.Instance.ShapeModle.FindSimple(); //if (result != null) //{ // HTuple hommate2d; // HOperatorSet.VectorAngleToRigid(0, 0, 0, result.Row, result.Column, result.Angle, out hommate2d); // HOperatorSet.AffineTransContourXld(result.ModleRegion, out HObject hObject, hommate2d); // RegionX regionX = new RegionX(hObject, "green"); // DelegateControls.Instance.DelegateHdisplay("FormMain_hDisplay1", _image, new List<HalWindow.RegionX>() { regionX }); // HTuple _x = new HTuple(); // HTuple _y = new HTuple(); // VisionManager.Instance.Calibrate.GetRobotpoint(result.Row, result.Column, ref _x, ref _y); // robotPos2 = await HardWareManager.Instance.YaskawaRobot.GetRobotCPoint(); // robotPos2.Y += _x; // robotPos2.Z += _y; // await HardWareManager.Instance.YaskawaRobot.SetTypeBData(2, "1"); //} //else //{ // DelegateControls.Instance.DelegateHdisplay("FormMain_hDisplay1", _image, null); // DelegateControls.Instance.DelegateRichTextBoxZd("FormMain_RichTextBoxZd", "第一次取像未能找到第一个螺丝孔"); // HardWareManager.Instance.XinJiePlc.SetM(125, true); // Step = MainStep.CheckInPlace; // break; //} //await HardWareManager.Instance.YaskawaRobot.SetTypeBData(2, "1"); //Step = MainStep.GetHolePoint1; //DelegateControls.Instance.DelegateRichTextBoxZd("FormMain_RichTextBoxZd", "第一次拍照完成"); //break; } } catch (Exception ex) { HardWareManager.Instance.XinJiePlc.SetM(125, true); DelegateControls.Instance.DelegateRichTextBoxZd("FormMain_RichTextBoxZd", ex.Message); Step = MainStep.CheckInPlace; } Thread.Sleep(100); } }