private async void button1_Click_1(object sender, EventArgs e)
        {
            HObject _image = HardWareManager.Instance.Cmaera.SnapShot();

            VisionManager.Instance.ShapeModle.BackImage = _image.Clone();
            //DelegateControls.Instance.
            ModleFindResult result = VisionManager.Instance.ShapeModle.FindSimple();

            // return;
            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 calibratedata = VisionManager.Instance.Calibrate.CalibrateData;

                HTuple _x = new HTuple();
                HTuple _y = new HTuple();
                HOperatorSet.AffineTransPoint2d(calibratedata, result.Row, result.Column, out HTuple x, out HTuple y);
                HOperatorSet.AffineTransPoint2d(calibratedata, 1024, 1536, out _x, out _y);
                double   ox       = -x + _x + VisionManager.Instance.Calibrate.ToolOffsetX;
                double   oy       = -y + _y + VisionManager.Instance.Calibrate.ToolOffsetY;
                RobotPos robotPos = await HardWareManager.Instance.YaskawaRobot.GetRobotCPoint();

                double x1 = ox + robotPos.Y;
                double x2 = oy + robotPos.Z;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            HTuple calibratedata = VisionManager.Instance.Calibrate.CalibrateData;

            if (calibratedata != null)
            {
                HObject _image = m_Camera.SnapShot();
                m_ShapeModle.BackImage = _image;
                ModleFindResult result = m_ShapeModle.FindSimple();
                if (result != null)
                {
                    HOperatorSet.AffineTransPoint2d(calibratedata, result.Row, result.Column, out HTuple x, out HTuple y);
                    HOperatorSet.AffineTransPoint2d(calibratedata, 1024, 1536, out HTuple _x, out HTuple _y);
                    double ox = _x - x + offx;
                    double oy = _y - y + offy;
                    VisionManager.Instance.Calibrate.ToolOffsetX = ox;
                    VisionManager.Instance.Calibrate.ToolOffsetY = oy;
                    // Robot.RelativeMove(ox, oy);
                }
            }
        }
        private void btn_GetTargetPos_Click(object sender, EventArgs e)
        {
            DataGridViewRow dataGridViewRow = null;

            if (dGV_CameraPos.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择一行来更新点位数据");
                return;
            }
            dataGridViewRow = dGV_CameraPos.SelectedRows[0];
            HObject _image = m_Camera.SnapShot();

            m_ShapeModle.BackImage = _image;
            ModleFindResult result = m_ShapeModle.FindSimple();

            if (result != null)
            {
                dataGridViewRow.Cells[1].Value = result.Row.ToString();
                dataGridViewRow.Cells[2].Value = result.Column.ToString();
            }
        }
        private async void MainProc()
        {
            Step = MainStep.CheckInPlace;
            float      verticalOffset = 0;
            double     angle          = 0;
            float      instance1      = 0;
            float      instance2      = 0;
            float      instance3      = 0;
            Time_Meter time_Meter1    = new Time_Meter();
            Time_Meter time_Meter2    = new Time_Meter();
            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", "水平测距完成");
                        time_Meter1.Start();
                        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("f2"));
                        DelegateControls.Instance.DelegateTextbox("FormMain_txt_vOffset", (lengthX).ToString("f2"));

                        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.LockScrew;
                        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;

                    case MainStep.LockScrew:
                        rtn = await HardWareManager.Instance.YaskawaRobot.GetTypeBData(11);

                        if (rtn != "1\r\0")
                        {
                            DelegateControls.Instance.DelegateRichTextBoxZd("FormMain_RichTextBoxZd", "本周期结束");

                            Step = MainStep.CheckInPlace;
                            break;
                        }
                        rtn = await HardWareManager.Instance.YaskawaRobot.GetTypeBData(10);

                        if (rtn != "1\r\0")
                        {
                            break;
                        }
                        _image = HardWareManager.Instance.Cmaera.SnapShot();

                        await GetHolePoint(_image);

                        await HardWareManager.Instance.YaskawaRobot.SetRobotPoint(100, $"{-lengthX},{-lengthY},{instance3},0,0,{0}");

                        DelegateControls.Instance.DelegateTextbox("FormMain_txt_hOffset", (lengthY).ToString("f2"));
                        DelegateControls.Instance.DelegateTextbox("FormMain_txt_vOffset", (lengthX).ToString("f2"));

                        await HardWareManager.Instance.YaskawaRobot.SetTypeBData(9, "1");

                        break;
                    }
                }
                catch (Exception ex)
                {
                    HardWareManager.Instance.XinJiePlc.SetM(125, true);

                    DelegateControls.Instance.DelegateRichTextBoxZd("FormMain_RichTextBoxZd", ex.Message);
                    Step = MainStep.CheckInPlace;
                }
                Thread.Sleep(100);
            }
        }