Esempio n. 1
0
        private void TryDebugBtn_Click(object sender, EventArgs e)
        {
            txtFindTime.Clear();
            Stopwatch sw = new Stopwatch();

            sw.Start();
            TeachLoclPara.Blobs = TeachBlobLocaltPara;
            LocalManager LocalCtrl = new LocalManager();

            LocalCtrl.SetLocalModel(TeachLoclPara.localSetting.localModel);
            LocalCtrl.SetParam(GrabedImg, TeachLoclPara);
            LocalCtrl.doLocal();
            sw.Stop();
            LocalResult MyRlt = new LocalResult();

            MyRlt = LocalCtrl.GetResult();
            BlobLocalRlt BlobRlt = (BlobLocalRlt)MyRlt;

            if (GrabedImg != null && GrabedImg.IsInitialized())
            {
                view1.ResetView();
                view1.AddViewImage(GrabedImg);
                view1.SetDraw("blue");
                view1.AddViewObject(MyRlt.ShowContour);
                view1.Repaint();
            }
            if (BlobRlt.ListWid != null && BlobRlt.ListWid.Count > 0)
            {
                double X = BlobRlt.ListCol[0] * TeachLoclPara.Blobs.PixelSize;
                double H = BlobRlt.ListRow[0] * TeachLoclPara.Blobs.PixelSize;
                view1.SetString(100, 150, "red", "坐标X(mm):" + X.ToString() + "     Y(mm):" + H.ToString());
                if (BlobRlt.ListWid.Count > 1)
                {
                    X = BlobRlt.ListWid[1] * TeachLoclPara.Blobs.PixelSize;
                    H = BlobRlt.ListHei[1] * TeachLoclPara.Blobs.PixelSize;
                    view1.SetString(100, 750, "blue", "高度(mm):" + H.ToString());
                }
            }
            txtFindTime.Text = sw.ElapsedMilliseconds.ToString();
        }
Esempio n. 2
0
        private void RectTeachBtn_Click(object sender, EventArgs e)
        {
            int SelectIndex = CaliPtModelCbx.SelectedIndex;

            if (GrabedImg == null || !GrabedImg.IsInitialized())
            {
                MessageBox.Show("标定前,请先采集图片");
                return;
            }
            System.Threading.Tasks.Task.Factory.StartNew(new Action(() => {
                try{
                    int row1 = 0, col1 = 0, row2, col2;
                    if (SelectIndex == 0)
                    {
                        TeachCaliPara.ListRectRegion = new List <RectangleF>();
                        for (int i = 0; i < 4; i++)
                        {
                            view1.SetString(100, 100, "red", "左击拖动绘制矩形,右击释放");
                            view1.SetString(100, 150, "red", "绘制第" + i.ToString() + "矩形");
                            view1.DrawRect1(out row1, out col1, out row2, out col2);
                            HObject Rect, RectContour;
                            HOperatorSet.GenRectangle1(out Rect, row1, col1, row2, col2);
                            HOperatorSet.GenContourRegionXld(Rect, out RectContour, "border");
                            view1.Refresh();
                            view1.AddViewImage(GrabedImg);
                            view1.AddViewObject(RectContour);
                            view1.Repaint();
                            RectangleF rect = new RectangleF(col1, row1, col2 - col1, row2 - row1);
                            TeachCaliPara.ListRectRegion.Add(rect);
                        }
                    }
                }
                catch
                { }
            }));
        }
Esempio n. 3
0
        public void GetFocusValueProcess()
        {
            HTuple  Value = new HTuple();
            HObject Rect = new HObject();
            HObject ReduceImg = new HObject();
            HObject CropImg = new HObject();
            HObject RectContour = new HObject();
            HTuple  Width, Hei;

            while (IsContinue)
            {
                try
                {
                    #region
                    if (Img != null && Img.IsInitialized())
                    {
                        Img.Dispose();
                    }
                    //if (RectContour != null && RectContour.IsInitialized()) RectContour.Dispose();
                    Camera.Instance.GrabImg(CcdName, out Img);
                    HOperatorSet.GenRectangle1(out Rect, row1, col1, row2, col2);
                    HOperatorSet.GenContourRegionXld(Rect, out RectContour, "border");
                    HOperatorSet.ReduceDomain(Img, Rect, out ReduceImg);
                    Rect.Dispose();
                    HOperatorSet.CropDomain(ReduceImg, out CropImg);
                    ReduceImg.Dispose();
                    MyVisionBase.evaluate_definition(CropImg, out Value);
                    CropImg.Dispose();
                    view.ResetView();
                    view.AddViewImage(Img);
                    view.AddViewObject(RectContour);
                    HOperatorSet.GetImageSize(Img, out Width, out Hei);

                    view.Repaint();
                    view.SetString(Width.D / 2, Hei.D / 2, "red", Value.ToString());
                    System.Threading.Thread.Sleep(100);
                    //Img.Dispose();
                    //RectContour.Dispose();
                    #endregion
                }
                catch  {     }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 扎针定位
        /// </summary>
        /// <param name="PlcItem"></param>
        /// <param name="Obj"></param>
        public void DoProject(LD.Config.PlcDataItem PlcItem, Object Obj)
        {
            LD.Common.PlcDevice plcDevice = PlcItem.PlcDevice;
            if (plcDevice == LD.Common.PlcDevice.V_01_TriggerGrab)  //
            {
                NowVisionProcess = new DoVisionProcess();
                //起始信号清零
                LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.V_01_TriggerGrab, 0);
                FileLib.Logger.Pop("接受到PLC信号,开始扎针对位", false, "运行日志");
                short  ClearValue = 1;
                object obj        = new object();
                while (true)
                {
                    obj        = LD.Logic.PlcHandle.Instance.ReadValue(LD.Common.PlcDevice.V_01_TriggerGrab);
                    ClearValue = (short)obj;
                    if (ClearValue == 0)
                    {
                        break;
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(10);
                    }
                }
                System.Threading.Thread.Sleep(1);
                //读取机台编号
                int StageNum = (int)LD.Logic.PlcHandle.Instance.ReadValue(LD.Common.PlcDevice.ArtTestStageNumRead);
                FileLib.Logger.Pop("  读取机台编号:" + StageNum.ToString(), false, "运行日志");
                System.Threading.Thread.Sleep(1);
                //读取拍照位编号
                short GrabNum = (short)LD.Logic.PlcHandle.Instance.ReadValue(LD.Common.PlcDevice.ArtTestGrabNum);
                System.Threading.Thread.Sleep(1);
                //获取视觉示教的定位参数
                NowProjectPara = ProjectParaManager.Instance.GetProjectPara(StageNum);
                //获取当前拍照点的视觉坐标
                NowVisionPara = NowProjectPara.GetVisionPara(GrabNum);
                NowVisionProcess.SetVisionPara(NowVisionPara);
                NowVisionProcess.Do();
                if (GrabNum == 0)
                {
                    FirstLocalResult = NowVisionProcess.MyLocalResult;
                    FileLib.Logger.Pop(" 左边拍照定位完成,开始右边拍照定位:", false, "运行日志");
                }
                else
                {
                    SecondLocalResult = NowVisionProcess.MyLocalResult;
                    //示教点到旋转中心的坐标
                    Point2Db TeachPt2d1 = new Point2Db(FirstLocalResult.TeachPosToRot.Col, FirstLocalResult.TeachPosToRot.Row);
                    Point2Db TeachPt2d2 = new Point2Db(SecondLocalResult.TeachPosToRot.Col, SecondLocalResult.TeachPosToRot.Row);
                    FileLib.Logger.Pop(" 示教产品到旋转中心的坐标(左右两点):" + TeachPt2d1.Col.ToString("f3") + "  " + TeachPt2d1.Row.ToString("f3") + "  "
                                       + TeachPt2d2.Col.ToString("f3") + "  " + TeachPt2d2.Row.ToString("f3"), false, "运行日志");
                    //当前产品到旋转中心的坐标
                    Point2Db NowPosPt2d1 = new Point2Db(FirstLocalResult.PosToRot.Col, FirstLocalResult.PosToRot.Row);
                    Point2Db NowPosPt2d2 = new Point2Db(SecondLocalResult.PosToRot.Col, SecondLocalResult.PosToRot.Row);
                    FileLib.Logger.Pop(" 当前产品到旋转中心的坐标(左右两点):" + NowPosPt2d1.Col.ToString("f3") + "  " + NowPosPt2d1.Row.ToString("f3") + "  "
                                       + NowPosPt2d2.Col.ToString("f3") + "  " + NowPosPt2d2.Row.ToString("f3"), false, "运行日志");
                    List <Point2Db> TeachPtList = new List <Point2Db>();
                    List <Point2Db> NowPtList   = new List <Point2Db>();
                    //当前产品移动到产品示教时的位置,先计算出偏移矩阵
                    TeachPtList.Add(TeachPt2d1);
                    TeachPtList.Add(TeachPt2d2);
                    NowPtList.Add(NowPosPt2d1);
                    NowPtList.Add(NowPosPt2d2);
                    MyHomMat2D NowHomat = new MyHomMat2D();
                    bool       IsTrue   = false;
                    MyVisionBase.VectorToRigidHomMat(NowPtList, TeachPtList, out NowHomat, out IsTrue);
                    double AddX = 0, AddY = 0, AddTheta = 0;
                    MyVisionBase.CalculateThreeTapePos(NowPosPt2d1, NowPosPt2d2, TeachPt2d1, TeachPt2d2, out AddX, out AddY, out AddTheta);
                    FileLib.Logger.Pop(" 计算出的偏移补偿量:" + AddX.ToString("f3") + "  " + AddY.ToString("f3") + "  " + AddTheta.ToString("f3"), false, "运行日志");
                    ///再根据偏移矩阵计算出偏移量
                    double AddX1 = 0, AddY1 = 0, AddTheta1 = 0;
                    MyVisionBase.HomMat2dToAffinePara(NowHomat, out AddTheta1, out AddY1, out AddX1);
                    ViewControl view1 = DisplaySystem.GetViewControl(CameraTest.UpCam1);
                    view1.SetString(500, 500, "red", "   AddX: " + AddX.ToString("f4") + " mm ");
                    view1.SetString(500, 600, "red", "   AddY: " + AddY.ToString("f4") + " mm ");
                    view1.SetString(500, 700, "red", "   AddTheta: " + AddTheta.ToString("f4") + " 度 ");
                    ///
                    double addx0     = -AddX * 1000;
                    double addy0     = AddY * 1000;
                    double addtheta0 = AddTheta * 1000;
                    int    addx      = (int)addx0;
                    int    addy      = (int)addy0;
                    int    addtheta  = (int)addtheta0;
                    NowVisionPara = NowProjectPara.GetVisionPara(0);
                    int offsetx     = (int)(NowVisionPara.localPara.localSetting.Offset_x * 1000);
                    int offsety     = (int)(NowVisionPara.localPara.localSetting.Offset_y * 1000);
                    int offsettheta = (int)(NowVisionPara.localPara.localSetting.Offset_theta * 1000);

                    int newAddX     = addx + offsetx;
                    int NewAddY     = addy + offsety;
                    int NewAddTheta = -addtheta + offsettheta;
                    if (StageNum == 0 || StageNum == 2)//左边两个平台X轴为正常方向,后面两个平台为反方向
                    {
                        newAddX = addx + offsetx;
                    }
                    else
                    {
                        newAddX = -addx + offsetx;
                    }
                    LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestAddX, newAddX);
                    System.Threading.Thread.Sleep(5);
                    LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestAddY, NewAddY);
                    System.Threading.Thread.Sleep(5);
                    LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestAddTheta, NewAddTheta);//图像坐标系为逆时针,机械坐标系为顺时针
                    System.Threading.Thread.Sleep(5);
                    int    ReadAddX     = 0;
                    int    ReadAddY     = 0;
                    int    ReadAddTheta = 0;
                    object ObjRead      = 0;
                    int    WriteCount   = 0;
                    while (true)
                    {
                        ObjRead = LD.Logic.PlcHandle.Instance.ReadValue(LD.Common.PlcDevice.ArtTestAddX);
                        System.Threading.Thread.Sleep(5);
                        ReadAddX = (int)ObjRead;
                        ObjRead  = LD.Logic.PlcHandle.Instance.ReadValue(LD.Common.PlcDevice.ArtTestAddY);
                        System.Threading.Thread.Sleep(5);
                        ReadAddY = (int)ObjRead;
                        ObjRead  = LD.Logic.PlcHandle.Instance.ReadValue(LD.Common.PlcDevice.ArtTestAddTheta);
                        System.Threading.Thread.Sleep(5);
                        ReadAddTheta = (int)ObjRead;
                        if (ReadAddX == newAddX && ReadAddY == NewAddY && ReadAddTheta == NewAddTheta)
                        {
                            FileLib.Logger.Pop(" 补偿值成功写入", false, "运行日志");
                            break;
                        }
                        else
                        {
                            WriteCount++;
                            if (WriteCount % 10 == 0)
                            {
                                LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestAddX, newAddX);
                                System.Threading.Thread.Sleep(5);
                                LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestAddY, NewAddY);
                                System.Threading.Thread.Sleep(5);
                                LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestAddTheta, NewAddTheta);//图像坐标系为逆时针,机械坐标系为顺时针
                                System.Threading.Thread.Sleep(5);
                            }
                            if (WriteCount > 300)
                            {
                                break;
                            }
                        }
                    }
                    if (SecondLocalResult.IsLocalOk)
                    {
                        LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestCamLocalResult, 1); //告诉PLC定位结果OK
                        FileLib.Logger.Pop(" 告诉PLC补偿量已经发给PLC:", false, "运行日志");
                    }
                }
            }
        }
Esempio n. 5
0
        public void DoFOFProject1(LD.Config.PlcDataItem PlcItem, Object Obj)
        {
            LD.Common.PlcDevice plcDevice = PlcItem.PlcDevice;
            if (plcDevice == LD.Common.PlcDevice.V_02_TriggerGrab)
            {
                FileLib.Logger.Pop("接受到PLC信号,偏移检测", false, "FOF运行日志");
                NowUpVisionProcess = new DoUpDnVisionProcess();
                NowUpLocalRlt      = new LocalResult();
                NowDnLocalRlt      = new LocalResult();
                //起始信号清零
                FileLib.Logger.Pop("启动信号清零", false, "FOF运行日志");
                LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.V_02_TriggerGrab, 0);
                System.Threading.Thread.Sleep(1);
                short  ClearValue = 1;
                object obj        = new object();

                FileLib.Logger.Pop("启动地址清零成功", false, "FOF运行日志");
                //读取机台编号 4 代表FOF左侧FOF偏移检测 ,5代表FOF右侧偏移检测
                int StageNum = (int)LD.Logic.PlcHandle.Instance.ReadValue(LD.Common.PlcDevice.ArtTestStageNumRead);
                FileLib.Logger.Pop("  读取机台编号:" + StageNum.ToString(), false, "FOF运行日志");
                System.Threading.Thread.Sleep(1);
                //获取视觉示教的定位参数
                NowProjectPara = ProjectParaManager.Instance.GetProjectPara(StageNum);
                //获取当前拍照点的视觉坐标
                double OffsetX = 0;
                double OffsetY = 0;
                try {
                    FileLib.Logger.Pop("开始FOF偏移检测上相机定位", false, "FOF运行日志");
                    NowVisionPara = NowProjectPara.GetVisionPara(0);
                    NowUpVisionProcess.SetVisionPara(NowVisionPara);
                    NowUpVisionProcess.Do();
                    NowUpLocalRlt = NowUpVisionProcess.MyLocalResult;
                    BlobLocalRlt FofRlt = (BlobLocalRlt)NowUpLocalRlt;
                    double       x      = FofRlt.ListWid[0] * NowVisionPara.localPara.Blobs.PixelSize;
                    double       y      = FofRlt.ListHei[1] * NowVisionPara.localPara.Blobs.PixelSize;
                    OffsetX = x + NowVisionPara.localPara.localSetting.Offset_x;
                    OffsetY = y + NowVisionPara.localPara.localSetting.Offset_y;
                    FileLib.Logger.Pop("偏移量: " + "OffsetX: " + OffsetX.ToString("f4") + "  " + "OffsetY: "
                                       + OffsetY.ToString("f4"), false, "FOF运行日志");
                }
                catch (Exception e0) {
                    Logger.PopError1(e0, false, "视觉错误日志");
                    OffsetX = 999999;
                    OffsetY = 999999;
                }
                ViewControl view1 = DisplaySystem.GetViewControl(CameraTest.UpCam2);
                view1.SetString(500, 500, "red", "OffsetX:  " + OffsetX.ToString("f4") + " mm");
                view1.SetString(500, 600, "red", "OffsetY:  " + OffsetY.ToString("f4") + " mm");

                if (Math.Abs(OffsetX) < NowVisionPara.localPara.localSetting.Offset_x_range && Math.Abs(OffsetY)
                    < NowVisionPara.localPara.localSetting.Offset_y_range)
                {
                    LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.FOF_inspect_result, 1); //检测结果Ok
                    FileLib.Logger.Pop("检测结果写给Plc(FOF_inspect_result):OK", false, "FOF运行日志");
                }
                else
                {
                    LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.FOF_inspect_result, 2); //检测结果NG
                    FileLib.Logger.Pop("检测结果写给Plc(FOF_inspect_result):NG", false, "FOF运行日志");
                }
                FileLib.Logger.Pop("循环读取启动地址,保证清零成功", false, "FOF运行日志");
                int ReadCount = 1;
                while (true)
                {
                    obj        = LD.Logic.PlcHandle.Instance.ReadValue(LD.Common.PlcDevice.V_02_TriggerGrab);
                    ClearValue = (short)obj;
                    if (ClearValue == 0)
                    {
                        break;
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(10);
                        if (ReadCount % 10 == 0)
                        {
                            FileLib.Logger.Pop("重新执行一次清零", false, "FOF运行日志");
                            ReadCount = 1;
                            LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.V_02_TriggerGrab, 0);
                        }
                        if (ReadCount > 100)
                        {
                            FileLib.Logger.Pop("清零次数超过10,清零失败", false, "FOF运行日志");
                        }
                    }
                    ReadCount++;
                }
                FileLib.Logger.Pop("将偏移量写给PLC", false, "FOF运行日志");
                LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.FOF_offset_x, (int)(OffsetX * 1000));
                LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.FOF_offset_y, (int)(OffsetY * 1000));
                FileLib.Logger.Pop("偏移量写给PLC完成", false, "FOF运行日志");
                FileLib.Logger.Pop("。", false, "FOF运行日志");
                FileLib.Logger.Pop("。", false, "FOF运行日志");
                FileLib.Logger.Pop("。", false, "FOF运行日志");
            }
        }
Esempio n. 6
0
        public virtual void Do()
        {
            //1.打开光源
            FileLib.Logger.Pop("  打开光源:", false, "运行日志");
            foreach (LightPara item in MyVisionPara.camLightPara.lightPara)
            {
                LightCtrlManager.Instance.SetLightValue(item);
            }
            //2.采图
            if (GrabImg != null)
            {
                GrabImg.Dispose();
            }
            FileLib.Logger.Pop("  开始采图:", false, "运行日志");
            CameraCtrl.Instance.SetExpos(MyVisionPara.camLightPara.CamName, MyVisionPara.camLightPara.Exposure);
            //10.获取当前的机械坐标
            double X = 0, Y = 0, Z = 0, Theta = 0;

            if (CaliParaManager.Instance.GetCaliMode(MyVisionPara.localPara.localSetting.CoordiCam) == CaliModelEnum.HandEyeCali)
            {
                MotionManager.Instance.SetCoordi(MyVisionPara.localPara.localSetting.TeachCoordi);
                FileLib.Logger.Pop("当前的坐标系为:" + MyVisionPara.localPara.localSetting.TeachCoordi.ToString(), false, "运行日志");
                MotionManager.Instance.GetCoordiPos(out X, out Y, out Z, out Theta);
                FileLib.Logger.Pop("当前的机械坐标", false, "运行日志");
                FileLib.Logger.Pop("X:" + X.ToString("f4") + "  Y:" + Y.ToString("f4") +
                                   "  Z:" + Z.ToString("f4") + "  Theta:" + Theta.ToString("f4"), false, "运行日志");
            }

            if (CameraCtrl.Instance.GrabImg(MyVisionPara.camLightPara.CamName, out GrabImg))
            {
                LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestCameraGrabed, 1); //拍照OK
                FileLib.Logger.Pop("  采图OK:", false, "运行日志");
            }
            else
            {
                LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestCameraGrabed, 2); //拍照NG
                FileLib.Logger.Pop("  采图NG:", false, "运行日志");
            }
            ViewControl view1 = DisplaySystem.GetViewControl(CameraTest.UpCam1);

            if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam0)
            {
                view1 = DisplaySystem.GetViewControl(CameraTest.UpCam1);
            }
            if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam1)
            {
                view1 = DisplaySystem.GetViewControl(CameraTest.UpCam2);
            }
            if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam2)
            {
                view1 = DisplaySystem.GetViewControl(CameraTest.UpCam3);
            }
            view1.ResetView();
            view1.Refresh();
            view1.AddViewImage(GrabImg);
            view1.Repaint();
            MyVisionBase.SaveImg(GrabImg, "扎针对位图片");
            //3.关闭光源
            LightCtrlManager.Instance.SetAllLightTo0();
            FileLib.Logger.Pop("  关闭光源:", false, "运行日志");
            //4.0设置定位模式
            MyLocal.SetLocalModel(MyVisionPara.localPara.localSetting.localModel);
            //5.0设置定位参数
            MyLocal.SetParam(GrabImg, MyVisionPara.localPara);
            //6.执行定位
            try
            {
                MyLocal.doLocal();
                //7.告诉PLC定位结果完成
                if (MyVisionPara.ProjectVisionItem == ProjectVisionEnum.ProjectVision1)
                {
                    FileLib.Logger.Pop(" 扎针右侧视觉定位完成,先不发给PLC信号,等计算出偏移量后,将信号发给PLC:", false, "运行日志");
                }
                else
                {
                    LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestCamLocalResult, 1); //告诉PLC定位结果OK
                    FileLib.Logger.Pop("  告诉PLC拍照结果OK(告诉PLC定位结果OK):", false, "运行日志");
                }
            }
            catch (Exception e0)
            {
                Logger.PopError1(e0, false, "视觉错误日志");
                LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestCamLocalResult, 2); //告诉PLC定位结果NG
                FileLib.Logger.Pop("  告诉PLC拍照结果OK(告诉PLC定位结果NG):", false, "运行日志");
                MyLocalResult.IsLocalOk = false;
            }


            MyLocalResult = MyLocal.GetResult();
            view1.AddViewObject(MyLocalResult.ShowContour);
            //结合标定坐标计算出产品的实际位置
            //8.获取标定矩阵
            MyHomMat2D HomMat = new MyHomMat2D();

            HomMat = CaliParaManager.Instance.GetHomMAT(MyVisionPara.localPara.localSetting.CoordiCam); //获取示教的标定矩阵
            FileLib.Logger.Pop("  获取示教的标定矩阵:" + MyVisionPara.localPara.localSetting.CoordiCam.ToString(), false, "运行日志");
            FileLib.Logger.Pop(HomMat.ToString(), false, "运行日志");
            HalconDotNet.HTuple HHomMat = new HalconDotNet.HTuple();
            //标定矩阵的转换
            MyVisionBase.MyHomMatToHalcon(HomMat, out HHomMat);
            //9.图像坐标系的原点由左上角变到左下角
            MyVisionBase.AdjImgRow(GrabImg, ref MyLocalResult.row);
            St_VectorAngle PixelVector = new St_VectorAngle(MyLocalResult.row, MyLocalResult.col, MyLocalResult.angle);

            FileLib.Logger.Pop("当前的像素坐标", false, "运行日志");
            FileLib.Logger.Pop("Col:" + MyLocalResult.col.ToString("f4") + "  Row:" + MyLocalResult.row.ToString("f4") +
                               "  Theta:" + MyLocalResult.angle.ToString("f4"), false, "运行日志");

            CaliValue CaliPara = CaliParaManager.Instance.GetCaliValue(MyVisionPara.localPara.localSetting.CoordiCam);

            VectorAngle GrabPos = new VectorAngle(X, Y, Theta);

            //9 计算出当前产品距离旋转中心的坐标
            EyeToHandPos.TransEyeToHandPos(PixelVector, CaliPara, GrabPos, out PosToRot);
            //10计算出示教产品到旋转中心的坐标
            St_VectorAngle TeachPixelPos = new St_VectorAngle(MyLocalResult.TeachRow, MyLocalResult.TeachCol, MyLocalResult.TeachAngle);
            VectorAngle    TeachGrabPos  = new VectorAngle(MyLocalResult.TeachX, MyLocalResult.TeachY, MyLocalResult.TeachTheta);

            MyVisionBase.AdjImgRow(GrabImg, ref TeachPixelPos.Row);
            EyeToHandPos.TransEyeToHandPos(TeachPixelPos, CaliPara, TeachGrabPos, out TeachPosToRot);
            view1.Repaint();
            view1.SetString(100, 100, "red", "PosToRot: " + "   X: " + PosToRot.Col.ToString("f3") + "   Y: " +
                            PosToRot.Row.ToString("f3") + "   Theta: " + PosToRot.Angle.ToString("f3"));
            MyLocalResult.PosToRot      = PosToRot;
            MyLocalResult.TeachPosToRot = TeachPosToRot;
            FileLib.Logger.Pop("示教的产品相对旋转中心的偏移量", false, "运行日志");

            FileLib.Logger.Pop("PosToRot: " + "   X: " + PosToRot.Col.ToString("f3") + "   Y: " +
                               PosToRot.Row.ToString("f3") + "   Theta: " + PosToRot.Angle.ToString("f3"), false, "运行日志");

            FileLib.Logger.Pop("当前的产品相对旋转中心的偏移量", false, "运行日志");
            FileLib.Logger.Pop("PosToRot: " + "   X: " + TeachPosToRot.Col.ToString("f3") + "   Y: " +
                               TeachPosToRot.Row.ToString("f3") + "   Theta: " + TeachPosToRot.Angle.ToString("f3"), false, "运行日志");
            //11.计算出X Y Theta的补偿量,

            ////图像坐标转到机械坐标系
            //MyVisionBase.AffineTransPt(MyLocalResult.col, MyLocalResult.row, HHomMat, out MyLocalResult.x, out MyLocalResult.y);
            ////
            //MyVisionBase.AffineTransPt(MyLocalResult.TeachCol, MyLocalResult.TeachRow, HHomMat, out MyLocalResult.TeachX, out MyLocalResult.TeachY);
        }
Esempio n. 7
0
        private void btnCreateTemplate_Click(object sender, EventArgs e)
        {
            if (btnCreateTemplate.Text == "新建")
            {
                btnCreateTemplate.Enabled = true;

                panelOperator.Enabled  = true;
                groupBox1.Enabled      = true;
                this.Enabled           = true;
                btnCreateTemplate.Text = "保存";
                view1.roiController.reset();
                view1.roiController.NotifyRCObserver += UpdateViewData;
                view1.roiController.setROISign(ViewROI.ROIController.MODE_ROI_POS);
                if (CurrentImage == null)
                {
                    Logger.PopError("请先采集图片!", true);
                    return;
                }
                view1.SetDraw("red", "margin");
                //1.0界面去使能
                this.BeginInvoke(new Action(() => { ActionIsDrawingRoi(true); }));
                view1.Refresh();
                view1.AddImage(CurrentImage);
                if (RoiStr.Equals("Rect1"))
                {
                    view1.SetString(12, 12, "red", "绘制矩形区域作为模板区域");
                    Thread.Sleep(10);
                    view1.roiController.setROIShape(new ViewROI.ROIRectangle1(200));
                }
                #region
                //else if (RoiStr.Equals("Rect2"))
                //{
                //    HOperatorSet.ClearWindow(ShowWindow);
                //    MyVisionBase.hDispObj(ShowWindow, CurrentImage);
                //    MyVisionBase.disp_message(ShowWindow, "绘制矩形区域作为模板区域", "window", 12, 12, "red", "false");
                //    HTuple CenterRow, CenterCol, Rect2Phi, RectL1, RectL2;
                //    HOperatorSet.DrawRectangle2(ShowWindow, out CenterRow, out CenterCol, out Rect2Phi, out RectL1, out RectL2);
                //    ModelRoi = new HRegion();
                //    ModelRoi.GenRectangle2(CenterRow, CenterCol, Rect2Phi, RectL1, RectL2);
                //}
                //else if (RoiStr.Equals("Circle"))
                //{
                //    MyVisionBase.disp_message(ShowWindow, "绘制圆形区域作为模板区域", "window", 12, 12, "red", "false");
                //    HTuple CircleRow, CircleCol, CircleR;
                //    HOperatorSet.DrawCircle(ShowWindow, out CircleRow, out CircleCol, out CircleR);
                //    ModelRoi = new HRegion();
                //    ModelRoi.GenCircle(CircleRow, CircleCol, CircleR);
                //}
                //else if (RoiStr.Equals("CircleMod"))
                //{
                //    HTuple CircleRowIn, CircleColIn, CircleRIn, CircleRow, CircleCol, CircleR;
                //    MyVisionBase.disp_message(ShowWindow, "绘制外圆区域作为圆环模板区域的外圆", "window", 12, 12, "red", "false");
                //    HOperatorSet.DrawCircle(ShowWindow, out CircleRowIn, out CircleColIn, out CircleRIn);
                //    HOperatorSet.ClearWindow(ShowWindow);
                //    MyVisionBase.hDispObj(ShowWindow, CurrentImage);
                //    MyVisionBase.disp_message(ShowWindow, "绘制内圆区域作为圆环模板区域的内圆", "window", 12, 12, "red", "false");
                //    HOperatorSet.DrawCircleMod(ShowWindow, CircleRowIn, CircleColIn, CircleRIn, out CircleRow, out CircleCol, out CircleR);
                //    HRegion InnerCirRegion = new HRegion();
                //    ModelRoi = new HRegion();
                //    InnerCirRegion.GenCircle(CircleRowIn, CircleColIn, CircleRIn);
                //    ModelRoi.GenCircle(CircleRow, CircleCol, CircleR);
                //    ModelRoi.Difference(InnerCirRegion);
                //}
                //else if (RoiStr.Equals("Rect2+Rect2"))
                //{
                //    HTuple CenterRow, CenterCol, Rect2Phi, RectL1, RectL2;
                //    HRegion Rect2ROI = new HRegion();
                //    MyVisionBase.disp_message(ShowWindow, "绘制矩形1作为矩形联合区域的第一个矩形区域", "window", 12, 12, "red", "false");
                //    HOperatorSet.DrawRectangle2(ShowWindow, out CenterRow, out CenterCol, out Rect2Phi, out RectL1, out RectL2);
                //    ModelRoi = new HRegion();
                //    ModelRoi.GenRectangle2(CenterRow, CenterCol, Rect2Phi, RectL1, RectL2);
                //    HOperatorSet.ClearWindow(ShowWindow);
                //    MyVisionBase.hDispObj(ShowWindow, CurrentImage);
                //    MyVisionBase.disp_message(ShowWindow, "绘制矩形2作为矩形联合区域的第二个矩形区域", "window", 12, 12, "red", "false");
                //    HOperatorSet.DrawRectangle2(ShowWindow, out CenterRow, out CenterCol, out Rect2Phi, out RectL1, out RectL2);
                //    Rect2ROI.GenRectangle2(CenterRow, CenterCol, Rect2Phi, RectL1, RectL2);
                //    ModelRoi = ModelRoi.Union2(Rect2ROI);
                //}
                //else
                //{
                //    HOperatorSet.DrawRectangle1(ShowWindow, out row1, out column1, out row2, out column2);
                //    ModelRoi = new HRegion(row1, column1, row2, column2);
                //}
                #endregion
            }
            else if (btnCreateTemplate.Text == "保存")
            {
                btnCreateTemplate.Text = "新建";
                view1.roiController.NotifyRCObserver -= UpdateViewData;
                view1.roiController.reset();
                this.BeginInvoke(new Action(() => { ActionIsDrawingRoi(false); }));
                string files = TemplateParam.TemplatePath;
                if (false)
                {
                    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        files = saveFileDialog1.FileName;
                        TemplateParam.Save(files);
                    }
                }
                else
                {
                    TemplateParam.Save(files);
                    TemplateParam.Load(files);
                }
            }
        }