Esempio n. 1
0
        public bool  StartGrabImg()
        {
            //1.打开光源
            FileLib.Logger.Pop("  打开光源:", false, "FOF运行日志");
            foreach (LightPara item in MyVisionPara.camLightPara.lightPara)
            {
                LightCtrlManager.Instance.SetLightValue(item);
            }
            //2.采图
            FileLib.Logger.Pop("  开始采图:", false, "FOF运行日志");
            if (GrabImg != null)
            {
                GrabImg.Dispose();
            }
            CameraCtrl.Instance.SetExpos(MyVisionPara.camLightPara.CamName, MyVisionPara.camLightPara.Exposure);
            CameraCtrl.Instance.GrabImg(MyVisionPara.camLightPara.CamName, out GrabImg);
            ViewControl view0 = new ViewControl();

            view0 = DisplaySystem.GetViewControl(CameraTest.UpCam2);
            if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam2)
            {
                view0 = DisplaySystem.GetViewControl(CameraTest.UpCam3);
            }
            view0.ResetView();
            view0.Refresh();

            //3.关闭光源
            LightCtrlManager.Instance.SetAllLightTo0();
            FileLib.Logger.Pop("  关闭光源:", false, "FOF运行日志");
            return(true);
        }
Esempio n. 2
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. 3
0
 private void ContinueGrabBtn_Click(object sender, EventArgs e)
 {
     ContinueGrabBtn.Enabled = false;
     IsContinnueGrab         = true;
     System.Threading.Tasks.Task.Factory.StartNew(new Action(() => {
         while (IsContinnueGrab)
         {
             if (GrabedImg != null)
             {
                 GrabedImg.Dispose();
             }
             if (CameraCtrl.Instance.GrabImg(TeachCaliPara.camLightPara.CamName, out GrabedImg))
             {
                 view1.ResetView();
                 view1.Refresh();
                 view1.AddViewImage(GrabedImg);
                 view1.Repaint();
             }
             System.Threading.Thread.Sleep(100);
         }
     }));
 }
Esempio n. 4
0
        private void CamParaDgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try{
                if (e.RowIndex >= 0)
                {
                    //当前行对应的相机参数
                    CameraPara CurCamPara = (CameraPara)this.CamParaDgv.CurrentRow.DataBoundItem;
                    if (CurCamPara == null)
                    {
                        return;
                    }
                    switch (CamParaDgv.Columns[e.ColumnIndex].Name)
                    {
                    case "DeleteBtnDgvCol":
                        if (MessageBox.Show("是否删除该项设备?    ", "删除提示", MessageBoxButtons.YesNo,
                                            MessageBoxIcon.Question) != DialogResult.Yes)
                        {
                            return;
                        }
                        CameraParaManager.Instance.CameraParaList.Remove(CurCamPara);
                        break;

                    case "OpenCamBtnDgvCol":
                        CameraPara camParaI;
                        for (int i = 0; i < CameraParaManager.Instance.CameraParaList.Count() - 1; i++)
                        {
                            camParaI = CameraParaManager.Instance.CameraParaList[i];
                            if (CurCamPara.CameraName == camParaI.CameraName)
                            {
                                MessageBox.Show("相机已经示教,请重新选择相机");
                                break;
                            }
                        }
                        CameraCtrl.Instance.Init();
                        break;

                    case "GrabImgDgvCol":
                        ImgOut.Dispose();
                        CameraCtrl.Instance.GrabImg(CurCamPara.CameraName, out ImgOut);
                        view.ResetView();
                        view.AddViewImage(ImgOut);
                        //view.Refresh();
                        view.Repaint();
                        break;
                    }
                    this.CamParaDgv.Refresh();
                }
            }
            catch
            { }
        }
Esempio n. 5
0
 private void btnLoadLocalImage_Click(object sender, EventArgs e)
 {
     try {
         string         fileName;
         OpenFileDialog openFileDialog1 = new OpenFileDialog();
         openFileDialog1.Filter           = "All files (*.*)|*.*|bmp files (*.bmp)|*.bmp";
         openFileDialog1.Multiselect      = false;
         openFileDialog1.FilterIndex      = 1;
         openFileDialog1.Title            = "打开图片文件";
         openFileDialog1.RestoreDirectory = false;
         openFileDialog1.InitialDirectory = @"D:\Image\";
         if (openFileDialog1.ShowDialog() == DialogResult.OK)
         {
             fileName = openFileDialog1.FileName;
             HObject img;
             HTuple  wid, hei;
             HOperatorSet.GenEmptyObj(out img);
             HOperatorSet.ReadImage(out img, fileName);
             HOperatorSet.GetImageSize(img, out wid, out hei);
             //hWindowControl1.ImagePart = new Rectangle(0, 0, wid, hei);
             OrgImageWid = wid;
             OrgImageHei = hei;
             TeachLocalPara.Template.SetTrainImage(fileName);
             if (CurrentImage != null)
             {
                 CurrentImage.Dispose();
             }
             if (TempSrcImage != null)
             {
                 TempSrcImage.Dispose();
             }
             HOperatorSet.CopyImage(img, out CurrentImage);
             HOperatorSet.CopyImage(img, out TempSrcImage);
             view1.Refresh();
             view1.ResetView();
             view1.AddViewImage(CurrentImage);
             view1.Repaint();
             UpdateImageToSubForm();
             if (img != null)
             {
                 img.Dispose();
             }
         }
         openFileDialog1.Dispose();
     }
     catch (Exception ex) {
         MessageBox.Show(ex.Message.ToString());
     }
 }
Esempio n. 6
0
        private void FindLineBtn_Click(object sender, EventArgs e)
        {
            txtFindTime.Clear();
            Stopwatch sw = new Stopwatch();

            HObject RoiContour = new HObject();

            view1.ResetView();
            view1.AddViewImage(GrabedImg);
            HTuple ResultRows = new HTuple(), ResultCols = new HTuple();

            if ((TeachLinesPara.Row1s[0] == 0 || TeachLinesPara.Col1s[0] == 0))
            {
                MessageBox.Show("示教未完成,请重新示教。");
                return;
            }
            int lineCount = TeachLinesPara.Col1s.Count;

            sw.Start();
            for (int i = 0; i < lineCount; i++)
            {
                if (TeachLinesPara.Row1s[i] == 0 || TeachLinesPara.Row1s[i] == 0)
                {
                    MessageBox.Show("示教未完成,请重新示教。");
                    return;
                }
                //1.0显示直线的检测ROI
                MyVisionBase.gen_rake_ROI(GrabedImg, out RoiContour, TeachLinesPara.Elements[i], TeachLinesPara.DetectHeights[i], 2,
                                          TeachLinesPara.Row1s[i], TeachLinesPara.Col1s[i], TeachLinesPara.Row2s[i], TeachLinesPara.Col2s[i]);
                view1.SetDraw("blue", "margin");
                view1.AddViewObject(RoiContour);
                //2.0利用卡尺工具找出边界点
                MyVisionBase.Rake(GrabedImg, out RoiContour, TeachLinesPara.Elements[i], TeachLinesPara.DetectHeights[i], 2, 2,
                                  TeachLinesPara.Thresholds[i], "all", "first", TeachLinesPara.Row1s[i], TeachLinesPara.Col1s[i],
                                  TeachLinesPara.Row2s[i], TeachLinesPara.Col2s[i], out ResultRows, out ResultCols);
                MyVisionBase.ClipCenterElement(ResultRows, TeachLinesPara.CenterClips[i], out ResultRows);
                MyVisionBase.ClipCenterElement(ResultCols, TeachLinesPara.CenterClips[i], out ResultCols);
                HObject LinePtCross = new HObject();
                //3.显示拟合的点
                HOperatorSet.GenCrossContourXld(out LinePtCross, ResultRows, ResultCols, 12, 0.7);
                view1.SetDraw("green", "margin");
                view1.AddViewObject(LinePtCross);
            }
            sw.Stop();
            this.view1.Repaint();
            txtFindTime.Text = sw.ElapsedMilliseconds.ToString();
        }
Esempio n. 7
0
 private void GenVirtualBtn_Click(object sender, EventArgs e)
 {
     TryDebugBtn_Click(null, new EventArgs());
     if (FindCircleRow.Length >= 2 && FindCircleCol.Length >= 2)
     {
         HTuple OutLineRow1 = new HTuple(), OutLineCol1 = new HTuple(), OutLineRow2 = new HTuple(), OutLineCol2 = new HTuple();
         view1.ResetView();
         view1.AddImage(GrabedImg);
         //1.0找线
         TryDebugBtn_Click(null, new EventArgs());
         HTuple  LineAngle   = new HTuple();
         HObject LineArrow   = new HObject();
         HObject LitCirCross = new HObject();
         //2.0计算圆心连线与相机X轴的夹角
         OutLineRow1 = FindCircleRow[0].D;
         OutLineCol1 = FindCircleCol[0].D;
         OutLineRow2 = FindCircleRow[1].D;
         OutLineCol2 = FindCircleCol[1].D;
         LineTypePos.CalculateLineAng(OutLineRow1, OutLineCol1, OutLineRow2, OutLineCol2, out LineAngle, out LineArrow);
         LineAngle = LineAngle.D / Math.PI * 180.0;
         HTuple LitCirRow = new HTuple(), LitCircol = new HTuple();
         HTuple DistRr = (7.78 / 2.0) / 2.8 * FindCircleR[0].D;
         //3.0生成虚拟圆的圆心
         CircleTypePos.GenCirCenter(FindCircleRow[0].D, FindCircleCol[0].D, DistRr, out LitCirRow, out LitCircol);
         HOperatorSet.GenCrossContourXld(out LitCirCross, LitCirRow, LitCircol, 60, 0);
         view1.SetDraw("blue", "margin");
         view1.AddViewObject(LineArrow);
         view1.AddViewObject(LitCirCross);
         HHomMat2D MyHomMat = new HHomMat2D();
         MyHomMat = MyHomMat.HomMat2dRotate((TeachCirclePara.OffSetPixelTh + LineAngle) / 180.0 * Math.PI, FindCircleRow[0], FindCircleCol[0]);
         //4.0旋转生成圆的圆心
         MyHomMat.AffineTransPixel(LitCirRow, LitCircol, out LitCirRow, out LitCircol);
         HOperatorSet.GenCrossContourXld(out LitCirCross, LitCirRow, LitCircol, 60, 0);
         HObject FirstCirCont = new HObject(), SecondCirCont = new HObject();
         HOperatorSet.GenCircleContourXld(out FirstCirCont, LitCirRow[0].D, LitCircol[0].D, 60, 0, 2 * Math.PI, "positive", 0.1);
         HOperatorSet.GenCircleContourXld(out SecondCirCont, LitCirRow[1].D, LitCircol[1].D, 40, 0, 2 * Math.PI, "positive", 0.1);
         HOperatorSet.ConcatObj(LitCirCross, FirstCirCont, out LitCirCross);
         HOperatorSet.ConcatObj(LitCirCross, SecondCirCont, out LitCirCross);
         view1.SetDraw("red", "margin");
         view1.AddViewObject(LitCirCross);
     }
 }
Esempio n. 8
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. 9
0
 private void LoadImgBtn_Click(object sender, EventArgs e)
 {
     try{
         string         fileName;
         OpenFileDialog openFileDialog1 = new OpenFileDialog();
         openFileDialog1.Filter           = "All files (*.*)|*.*|bmp files (*.bmp)|*.bmp";
         openFileDialog1.Multiselect      = false;
         openFileDialog1.FilterIndex      = 1;
         openFileDialog1.Title            = "打开图片文件";
         openFileDialog1.RestoreDirectory = false;
         openFileDialog1.InitialDirectory = @"D:\Image\";
         if (openFileDialog1.ShowDialog() == DialogResult.OK)
         {
             fileName = openFileDialog1.FileName;
             HOperatorSet.ReadImage(out GrabedImg, fileName);
             view1.ResetView();
             view1.AddViewImage(GrabedImg);
             view1.Repaint();
         }
     }
     catch (Exception ex) {
         MessageBox.Show(ex.Message.ToString());
     }
 }
Esempio n. 10
0
        public override void Do()
        {
            //1.打开光源
            foreach (LightPara item in MyVisionPara.camLightPara.lightPara)
            {
                LightCtrlManager.Instance.SetLightValue(item);
            }
            //2.采图
            if (GrabImg != null)
            {
                GrabImg.Dispose();
            }
            CameraCtrl.Instance.SetExpos(MyVisionPara.camLightPara.CamName, MyVisionPara.camLightPara.Exposure);
            if (CameraCtrl.Instance.GrabImg(MyVisionPara.camLightPara.CamName, out GrabImg))
            {
                LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.FOF_Grabed_ok, 1); //拍照OK
            }
            else
            {
                LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.FOF_Grabed_ok, 2); //拍照NG
            }
            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();
            //4.0设置定位模式
            MyLocal.SetLocalModel(MyVisionPara.localPara.localSetting.localModel);
            //5.0设置定位参数
            MyLocal.SetParam(GrabImg, MyVisionPara.localPara);
            //6.执行定位
            try
            {
                MyLocal.doLocal();
                LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.FOF_Grabed_result, 1); //告诉PLC定位结果OK
            }
            catch (Exception e0)
            {
                Logger.PopError1(e0, false, "视觉日志");
                LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.FOF_Grabed_result, 2); //告诉PLC定位结果OK
            }

            //7.告诉PLC定位结果完成

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

            HomMat = CaliParaManager.Instance.GetHomMAT(MyVisionPara.localPara.localSetting.CoordiCam); //获取示教的标定矩阵
            HalconDotNet.HTuple HHomMat = new HalconDotNet.HTuple();
            //标定矩阵的转换
            MyVisionBase.MyHomMatToHalcon(HomMat, out HHomMat);
            //图像坐标系的原点由左上角变到左下角
            MyVisionBase.AdjImgRow(GrabImg, ref MyLocalResult.row);
            St_VectorAngle PixelVector = new St_VectorAngle(MyLocalResult.row, MyLocalResult.col, MyLocalResult.angle);
            CaliValue      CaliPara    = CaliParaManager.Instance.GetCaliValue(MyVisionPara.localPara.localSetting.CoordiCam);

            //将像素坐标转换到标定坐标
            //MyLocalResult.AffineTransResult()
            HalconDotNet.HTuple HomMatCali = new HalconDotNet.HTuple();
            MyVisionBase.MyHomMatToHalcon(CaliPara.HomMat, out HomMatCali);

            MyVisionBase.AffineTransPt(MyLocalResult.col, MyLocalResult.row, HomMatCali, out MyLocalResult.x, out MyLocalResult.y);

            //MyVisionBase.AffineTransPt()

            //获取当前坐标



            //double X = 0, Y = 0, Z = 0, Theta = 0;
            //if (CaliParaManager.Instance.GetCaliMode(MyVisionPara.localPara.localSetting.TeachCoordi) == CaliModelEnum.HandEyeCali)
            //{
            //    MotionManager.Instance.SetCoordi(MyVisionPara.localPara.localSetting.TeachCoordi);
            //    MotionManager.Instance.GetCoordiPos(out X, out Y, out Z, out Theta);
            //}
            //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;
        }
Esempio n. 11
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. 12
0
        public override void Do()
        {
            //1.打开光源
            FileLib.Logger.Pop("  打开光源:", false, "FOF运行日志");
            foreach (LightPara item in MyVisionPara.camLightPara.lightPara)
            {
                LightCtrlManager.Instance.SetLightValue(item);
            }
            //2.采图
            FileLib.Logger.Pop("  开始采图:", false, "FOF运行日志");
            if (GrabImg != null)
            {
                GrabImg.Dispose();
            }
            if (FilterImg != null)
            {
                FilterImg.Dispose();
            }
            CameraCtrl.Instance.SetExpos(MyVisionPara.camLightPara.CamName, MyVisionPara.camLightPara.Exposure);
            if (CameraCtrl.Instance.GrabImg(MyVisionPara.camLightPara.CamName, out GrabImg))
            {
                if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam2)
                {
                    LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.FOF_Grabed_ok, 1); //拍照OK
                }
                FileLib.Logger.Pop("  采图OK:", false, "FOF运行日志");
            }
            else
            {
                if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam2)
                {
                    LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.FOF_Grabed_ok, 2); //拍照NG
                }
                FileLib.Logger.Pop("  采图NG:", false, "FOF运行日志");
            }
            ViewControl view1 = new ViewControl();

            view1 = DisplaySystem.GetViewControl(CameraTest.UpCam2);
            if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam2)
            {
                view1 = DisplaySystem.GetViewControl(CameraTest.UpCam3);
            }
            view1.ResetView();
            view1.Refresh();

            //3.关闭光源
            //LightCtrlManager.Instance.SetAllLightTo0();
            //FileLib.Logger.Pop("  关闭光源:", false, "FOF运行日志");

            //4.0设置定位模式
            MyLocal.SetLocalModel(MyVisionPara.localPara.localSetting.localModel);
            //5.0设置定位参数
            FileLib.Logger.Pop("  开始偏移检测:", false, "FOF运行日志");
            MyVisionBase.SaveImg(GrabImg, "FOF偏移检测图片");


            if (MyVisionPara.camLightPara.IsFilter)
            {
                FileLib.Logger.Pop("  开始频域滤波,FilterC:" + MyVisionPara.camLightPara.FilterC.ToString(), false, "FOF运行日志");
                MyVisionBase.FilterImg(GrabImg, out FilterImg, MyVisionPara.camLightPara.FilterC);
                view1.AddViewImage(FilterImg);
                MyVisionBase.SaveImg(FilterImg, "FOF偏移检测图片");
                MyLocal.SetParam(FilterImg, MyVisionPara.localPara);
            }
            else
            {
                view1.AddViewImage(GrabImg);
                MyLocal.SetParam(GrabImg, MyVisionPara.localPara);
            }

            view1.Repaint();
            //6.执行定位
            try
            {
                FileLib.Logger.Pop("  开始定位:", false, "FOF运行日志");
                MyLocal.doLocal();
                if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam2)
                {
                    LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.FOF_Grabed_result, 1);
                }
                FileLib.Logger.Pop("  告诉PLC拍照结果OK(告诉PLC定位结果OK):", false, "FOF运行日志");
            }
            catch (Exception e0)
            {
                if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam2)
                {
                    LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.FOF_Grabed_result, 2);
                }
                Logger.PopError1(e0, false, "视觉错误日志");
                FileLib.Logger.Pop("  告诉PLC拍照结果OK(告诉PLC定位结果NG):", false, "FOF运行日志");
            }
            MyLocalResult = MyLocal.GetResult();
            view1.AddViewObject(MyLocalResult.ShowContour);
            view1.Repaint();
            //8.结合标定坐标计算出产品的实际位置
            MyHomMat2D HomMat = new MyHomMat2D();

            HomMat = CaliParaManager.Instance.GetHomMAT(MyVisionPara.localPara.localSetting.CoordiCam); //获取示教的标定矩阵
            FileLib.Logger.Pop("  获取标定矩阵(CoordiEnumItem):" + MyVisionPara.localPara.localSetting.CoordiCam.ToString()
                               , false, "FOF运行日志");
            FileLib.Logger.Pop(HomMat.ToString(), false, "FOF运行日志");
            HalconDotNet.HTuple HHomMat = new HalconDotNet.HTuple();
            //9.标定矩阵的转换
            MyVisionBase.MyHomMatToHalcon(HomMat, out HHomMat);
            //10.图像坐标系的原点由左上角变到左下角
            MyVisionBase.AdjImgRow(GrabImg, ref MyLocalResult.row);
            MyVisionBase.AffineTransPt(MyLocalResult.col, MyLocalResult.row, HHomMat, out MyLocalResult.x, out MyLocalResult.y);
            FileLib.Logger.Pop("col: " + MyLocalResult.col.ToString("f3") + " row: " + MyLocalResult.row.ToString("f3"), false, "FOF运行日志");
            FileLib.Logger.Pop("X: " + MyLocalResult.x.ToString("f3") + " Y:   " + MyLocalResult.y.ToString("f3"), false, "FOF运行日志");
            MyLocalResult.Theta = MyLocalResult.angle;
            if (FilterImg != null)
            {
                FilterImg.Dispose();
            }
            if (GrabImg != null)
            {
                GrabImg.Dispose();
            }
        }
Esempio n. 13
0
        private void CaliRotCenterBtn_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("是否示开始旋转中心标定?", "提示", MessageBoxButtons.YesNo);

            if (dr == DialogResult.No)
            {
                return;
            }
            System.Threading.Tasks.Task.Factory.StartNew(new Action(() => {
                ListMarkCenterU      = new List <Point2Db>();
                HObject GrabedImg    = new HObject();
                LocalManager MyLocal = new LocalManager(); //定位类初始化
                LocalResult MyResult = new LocalResult();
                MyLocal.SetLocalModel(TeachCaliPara.localPara.localSetting.localModel);
                HOperatorSet.GenEmptyObj(out FindCenterCont);
                //旋转一个角度拍一次照片,用定位的点拟合圆,计算出0度时标定点到旋转中心的偏移量
                for (int i = 0; i < TeachCaliPara.AngleStep; i++)
                {
                    if (MotionManager.Instance.SetCoordiPos(TeachCaliPara.StartRotPt.x, TeachCaliPara.StartRotPt.y, TeachCaliPara.StartRotPt.angle
                                                            + TeachCaliPara.AngleRange / TeachCaliPara.AngleStep * i))
                    {
                        if (GrabedImg != null)
                        {
                            GrabedImg.Dispose();
                        }
                        CameraCtrl.Instance.GrabImg(TeachCaliPara.cam, out GrabedImg); //采集图像
                        MyResult = new LocalResult();
                        MyLocal.SetParam(GrabedImg, TeachCaliPara.localPara);
                        MyLocal.doLocal();
                        MyResult = MyLocal.GetResult();
                        if (!MyResult.IsLocalOk)
                        {
                            MessageBox.Show("定位失败");
                            return;
                        }
                        view1.ResetView();
                        view1.Refresh();
                        view1.AddViewImage(GrabedImg);
                        view1.AddViewObject(MyResult.ShowContour);
                        view1.Repaint();
                        //view1.Refresh();
                        //this.Refresh();
                        Thread.Sleep(50);
                        Point2Db NowPt = new Point2Db();
                        NowPt.Col      = MyResult.col;
                        NowPt.Row      = MyResult.row;
                        ListMarkCenterU.Add(NowPt);
                        FindCenterCont = FindCenterCont.ConcatObj(MyResult.ShowContour);
                    }
                    else
                    {
                        MessageBox.Show("移动失败");
                        return;
                    }
                }
                #region 零度时候的定位坐标
                Point2Db ZeoroDegreePos = new Point2Db();
                if (MotionManager.Instance.SetCoordiPos(TeachCaliPara.StartRotPt.x, TeachCaliPara.StartRotPt.y, 0.0))
                {
                    if (GrabedImg != null)
                    {
                        GrabedImg.Dispose();
                    }
                    CameraCtrl.Instance.GrabImg(TeachCaliPara.cam, out GrabedImg); //采集图像
                    MyResult = new LocalResult();
                    MyLocal.SetParam(GrabedImg, TeachCaliPara.localPara);
                    MyLocal.doLocal();
                    MyResult = MyLocal.GetResult();
                    if (!MyResult.IsLocalOk)
                    {
                        MessageBox.Show("定位失败");
                        return;
                    }
                    ZeoroDegreePos = new Point2Db(MyResult.col, MyResult.row);
                }
                #endregion
                //通过找到的标定mark中心点集合,拟合出旋转中心
                HTuple CircleRows = new HTuple();
                HTuple CircleCols = new HTuple();
                MyVisionBase.ListPt2dToHTuple(ListMarkCenterU, out CircleRows, out CircleCols);
                HObject CircleContourU = new HObject();
                HTuple CenterRow       = new HTuple(), CenterCol = new HTuple();
                HTuple RotRadius       = new HTuple(), StartPhi = new HTuple(), EndPhi = new HTuple(), FitFlag = new HTuple();
                if (FindCirCenterCbx.SelectedValue.ToString() == "拟合圆")
                {
                    MyVisionBase.PtsToBestCircle1(out CircleContourU, CircleRows, CircleCols, 4, "arc", out CenterRow, out CenterCol,
                                                  out RotRadius, out StartPhi, out EndPhi, out FitFlag);//通过边界点拟合圆
                    RotR         = RotRadius.D;
                    RotCenterRow = CenterRow.D;
                    RotCenterCol = CenterCol.D;
                }
                else if (FindCirCenterCbx.SelectedValue.ToString() == "中垂线找圆心")
                {
                    FindCircleCenter(ListMarkCenterU, out RotCenterRow, out RotCenterCol, out RotR);
                    HOperatorSet.GenCircleContourXld(out CircleContourU, RotCenterRow, RotCenterCol, RotR, 0, Math.PI, "positive", 1);
                }
                HObject PtContour = new HObject();
                HOperatorSet.GenCrossContourXld(out PtContour, CircleRows, CircleCols, 20, 0.6);
                HOperatorSet.ConcatObj(PtContour, CircleContourU, out CircleContourU);
                // MyVisionBase.hDispObj(hWindowControl1.HalconWindow, CircleContourU);
                view1.AddViewObject(CircleContourU);
                view1.Repaint();
                MyVisionBase.HTupleToListPt2d(CircleRows, CircleCols, out ListMarkCenterU);
                //计算0度标定Mark中心到旋转中心的偏移量
                VectorCaliMarkToRot.Row = (CenterRow - ZeoroDegreePos.Row);
                VectorCaliMarkToRot.Col = (CenterCol - ZeoroDegreePos.Col);
                HObject MarkToRotArrow  = new HObject();
                MyVisionBase.GenArrowContourXld(out MarkToRotArrow, ZeoroDegreePos.Row, ZeoroDegreePos.Col, CenterRow, CenterCol, 30, 30);
                view1.AddViewObject(MarkToRotArrow);
                view1.Repaint();
            }));
        }