Example #1
0
 public override bool doLocal()
 {
     try
     {
         NowResult = new LocalResult();
         myBlobLocal.Set(NowImg, NowVisionPara);
         myBlobLocal.doLocal();
         LocalResult blobLocalRlt = new LocalResult();
         blobLocalRlt = myBlobLocal.GetResult();
         HTuple HomMat = new HTuple();                                                                //
                                                                                                      //计算当前图片到示教图片的偏移矩阵
         HOperatorSet.VectorAngleToRigid(blobLocalRlt.row, blobLocalRlt.col, blobLocalRlt.angle, blobLocalRlt.TeachRow,
                                         blobLocalRlt.TeachCol, blobLocalRlt.TeachAngle, out HomMat); //
         HObject AffineImg = new HObject();
         HOperatorSet.AffineTransImage(NowImg, out AffineImg, HomMat, "constant", "false");           //矫正图像位置
         myTwoLineLocal.Set(AffineImg, NowVisionPara);
         myTwoLineLocal.doLocal();
         LocalResult LineLocalResult = new LocalResult();
         //计算当前示教图片到当前图片的偏移矩阵
         HTuple ReHomMat = new HTuple();
         HOperatorSet.VectorAngleToRigid(blobLocalRlt.TeachRow, blobLocalRlt.TeachCol, blobLocalRlt.TeachAngle,
                                         blobLocalRlt.row, blobLocalRlt.col, blobLocalRlt.angle, out ReHomMat);//
         myTwoLineLocal.NowResult.AffineTransResult(ReHomMat, out LineLocalResult);
         //调整直线定位的位置和
         HOperatorSet.ConcatObj(blobLocalRlt.ShowContour, LineLocalResult.ShowContour, out LineLocalResult.ShowContour);
         NowResult = LineLocalResult;
         return(true);
     }
     catch
     { return(false); }
 }
        public override bool doLocal()
        {
            LocalResult TempTwoCirLocalRlt = new LocalResult();

            NowResult = new LineCircRectRlt();

            try{
                MyTempTwoCirLocal.Set(NowImg, NowVisionPara);
                MyTempTwoCirLocal.doLocal();
                TempTwoCirLocalRlt = MyTempTwoCirLocal.GetResult();
                HTuple HomMat = new HTuple(); //
                //计算当前图片到示教图片的偏移矩阵
                HOperatorSet.VectorAngleToRigid(TempTwoCirLocalRlt.row, TempTwoCirLocalRlt.col, TempTwoCirLocalRlt.angle, TempTwoCirLocalRlt.TeachRow,
                                                TempTwoCirLocalRlt.TeachCol, TempTwoCirLocalRlt.TeachAngle, out HomMat);//
                HObject AffineImg = new HObject();
                //矫正图像位置
                HOperatorSet.AffineTransImage(NowImg, out AffineImg, HomMat, "constant", "false");
                ((LineCircRectRlt)NowResult).IsOk = LineCircleRectInspBase.LineCircleRectInsp(AffineImg, NowVisionPara.LineCirRectInspParam,
                                                                                              out ((LineCircRectRlt)NowResult).DetectContour, out ((LineCircRectRlt)NowResult).NgContour); //
                HOperatorSet.CopyObj(TempTwoCirLocalRlt.ShowContour, out ((LineCircRectRlt)NowResult).ShowContour, 1, -1);
                //位置的逆变换
                HOperatorSet.VectorAngleToRigid(TempTwoCirLocalRlt.TeachRow, TempTwoCirLocalRlt.TeachCol, TempTwoCirLocalRlt.TeachAngle,
                                                TempTwoCirLocalRlt.row, TempTwoCirLocalRlt.col, TempTwoCirLocalRlt.angle, out HomMat);//
                NowResult.AffineTransResult(HomMat, out NowResult);
                return(true);
            }
            catch (Exception e0)
            {
                Logger.PopError1(e0, false, "视觉运行错误日志");
                return(false);
            }
        }
Example #3
0
 public override bool doLocal()
 {
     try
     {
         NowResult = new LocalResult();
         MyTempLocal.Set(NowImg, NowVisionPara);
         MyTempLocal.doLocal();
         LocalResult TempLocalResult = new LocalResult();
         TempLocalResult = MyTempLocal.GetResult();
         HTuple HomMat = new HTuple(); //
         //计算当前图片到示教图片的偏移矩阵
         HOperatorSet.VectorAngleToRigid(TempLocalResult.row, TempLocalResult.col, TempLocalResult.angle, NowVisionPara.Template.CenterY,
                                         NowVisionPara.Template.CenterX, NowVisionPara.Template.TemplateAngle, out HomMat);
         HObject AffineImg = new HObject();
         HOperatorSet.AffineTransImage(NowImg, out AffineImg, HomMat, "constant", "false");//矫正图像位
         //计算示教图片到当前图片的偏移矩阵
         HTuple ReHomMat = new HTuple();
         HOperatorSet.VectorAngleToRigid(NowVisionPara.Template.CenterY, NowVisionPara.Template.CenterX, NowVisionPara.Template.TemplateAngle,
                                         TempLocalResult.row, TempLocalResult.col, TempLocalResult.angle, out ReHomMat);
         MyLineCirLocal.Set(NowImg, NowVisionPara);
         MyLineCirLocal.doLocal();
         LocalResult CircleResult = new LocalResult();
         //位置的逆变换
         MyLineCirLocal.NowResult.AffineTransResult(ReHomMat, out CircleResult);
         HOperatorSet.ConcatObj(TempLocalResult.ShowContour, CircleResult.ShowContour, out CircleResult.ShowContour);
         NowResult = CircleResult;
         return(true);
     }
     catch
     { return(false); }
 }
Example #4
0
File: TempLocal.cs Project: Gz1d/Gz
        public override bool doLocal()
        {
            NowResult = new LocalResult();
            //1.0模板匹配
            St_TemplateParam TemplateParam = NowVisionPara.Template;
            RectangleF       roi           = new RectangleF();
            LocalSettingPara Setting       = NowVisionPara.localSetting;

            roi.X      = Setting.SearchAreaX;
            roi.Y      = Setting.SearchAreaY;
            roi.Width  = Setting.SearchWidth;
            roi.Height = Setting.SearchHeight;
            MatchingResult result;

            try{
                NowResult.IsLocalOk = TemplateParam.FindSharpTemplate(NowImg, roi, TemplateParam, out result);
                NowResult.row       = result.mRow;
                NowResult.col       = result.mCol;
                NowResult.angle     = result.mAngle;
                //NowResult.ShowContour = result.mContour;
                NowResult.ShowContour = result.getDetectionResults();
                return(true);
            }
            catch
            { return(false); }
        }
Example #5
0
 public override bool doLocal()
 {
     try
     {
         NowResult = new LocalResult();
         St_CirclesParam CirclesParam = new St_CirclesParam(2);
         CirclesParam = NowVisionPara.Circles;
         HTuple  CircleRows = new HTuple(), CircleCols = new HTuple(), CircleRs = new HTuple(), StartPhis = new HTuple(), EndPhis = new HTuple();
         HObject CircleContour = new HObject(), CircleCont = new HObject(), CenterCross = new HObject();
         //找出要找的圆
         CircleTypePos.FindCircle(NowImg, CirclesParam, out CircleRows, out CircleCols, out CircleRs, out StartPhis, out EndPhis,
                                  out CircleContour, out CircleCont, out CenterCross);
         HOperatorSet.ConcatObj(CircleContour, CircleCont, out CircleContour);
         HOperatorSet.ConcatObj(CircleContour, CenterCross, out CircleContour);
         CircleCont.Dispose();
         CenterCross.Dispose();
         NowResult.ShowContour = CircleContour;
         NowResult.row         = (CircleRows[0].D + CircleCols[1].D) / 2.0;
         NowResult.col         = (CircleCols[0].D + CircleCols[1].D) / 2.0;
         HTuple angle = new HTuple();
         HOperatorSet.AngleLx(CircleRows[0], CircleCols[0], CircleRows[1], CircleCols[1], out angle);
         NowResult.angle = angle.D;
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Example #6
0
File: LocalBase.cs Project: Gz1d/Gz
        public LocalResult Copy()
        {
            LocalResult NowLocalRlt = new LocalResult();

            NowLocalRlt.row           = this.row;
            NowLocalRlt.col           = this.col;
            NowLocalRlt.angle         = this.angle;
            NowLocalRlt.x             = this.x;
            NowLocalRlt.y             = this.y;
            NowLocalRlt.Theta         = this.Theta;
            NowLocalRlt.TeachRow      = this.TeachRow;
            NowLocalRlt.TeachCol      = this.TeachCol;
            NowLocalRlt.TeachAngle    = this.TeachAngle;
            NowLocalRlt.TeachX        = this.TeachX;
            NowLocalRlt.TeachY        = this.TeachY;
            NowLocalRlt.TeachTheta    = this.TeachTheta;
            NowLocalRlt.IsLocalOk     = this.IsLocalOk;
            NowLocalRlt.TeachPosToRot = this.TeachPosToRot;
            NowLocalRlt.PosToRot      = this.PosToRot;
            if (!(this.ShowContour == null || !this.ShowContour.IsInitialized()))
            {
                HOperatorSet.CopyObj(this.ShowContour, out NowLocalRlt.ShowContour, 1, -1);
            }
            else
            {
                HOperatorSet.GenEmptyObj(out NowLocalRlt.ShowContour);
            }
            return(NowLocalRlt);
        }
Example #7
0
 private void GetProductPixelPosBtn_Click(object sender, EventArgs e)  //计算示教产品的像素的坐标,并保存到配置文件
 {
     this.GetProductPixelPosBtn.Enabled   = false;
     this.ProductPixelPosTeachBtn.Enabled = true;
     try
     {
         LocalResult  localResult = new LocalResult();
         LocalManager myLocal     = new LocalManager();
         //1.0设置定位模式
         myLocal.SetLocalModel(LocalPara0.localSetting.localModel);
         //2.0设置定位参数
         myLocal.SetParam(CurImg, LocalPara0);
         //3.执行定位
         myLocal.doLocal();
         localResult = myLocal.GetResult();
         this.LocalPara0.localSetting.TeachImgLocal = new St_VectorAngle(localResult.row, localResult.col, localResult.angle);
         view1.ResetWindow();
         view1.AddImage(CurImg.CopyObj(1, -1));
         view1.SetDraw("blue", "margin");
         view1.AddViewObject(localResult.ShowContour.CopyObj(1, -1));
         view1.Repaint();
         this.ProductPixelPosTbx.Text = "Col:" + localResult.col.ToString("f2") + "   Row:" + localResult.row.ToString("f2") +
                                        "   Angle:" + localResult.angle.ToString("f2");
     }
     catch
     { }
 }
Example #8
0
 public override bool doLocal()
 {
     try
     {
         NowResult = new LocalResult();
         St_LinesParam LinesPara = new St_LinesParam(2);
         LinesPara = NowVisionPara.Lines;
         HTuple  LineRow1 = new HTuple(), LineCol1 = new HTuple(), LineRow2 = new HTuple(), LineCol2 = new HTuple();
         HObject LinesContour = new HObject(), LinePtCont = new HObject();
         //找出所有的直线
         LineTypePos.FindLine(LinesPara, NowImg, out LineRow1, out LineCol1, out LineRow2, out LineCol2, out LinesContour, out LinePtCont);
         HTuple CrossRow = new HTuple(), CrossCol = new HTuple(), IsParallel = new HTuple();
         //第一条直线和第二条直线的交点
         HOperatorSet.IntersectionLl(LineRow1[0], LineCol1[0], LineRow2[0], LineCol2[0], LineRow1[1], LineCol1[1], LineRow2[1], LineCol2[1],
                                     out CrossRow, out CrossCol, out IsParallel);
         HTuple angle;
         //图像坐标轴X轴到直线1的角度
         HOperatorSet.AngleLx(LineRow1[0], LineCol1[0], LineRow2[0], LineCol2[0], out angle);
         NowResult.row   = CrossRow.D;
         NowResult.col   = CrossCol.D;
         NowResult.angle = angle.D;
         HOperatorSet.ConcatObj(LinesContour, LinePtCont, out LinesContour);
         LinePtCont.Dispose();
         NowResult.ShowContour = LinesContour;
         return(true);
     }
     catch {
         return(false);
     }
 }
Example #9
0
 public void NewDoProject(LD.Config.PlcDataItem PlcItem, Object Obj)
 {
     LD.Common.PlcDevice plcDevice = PlcItem.PlcDevice;
     if (plcDevice == LD.Common.PlcDevice.ArtTestCameraStart)  //
     {
         //读取机台编号
         int StageNum = (int)LD.Logic.PlcHandle.Instance.ReadValue(LD.Common.PlcDevice.ArtTestStageNumRead);
         //读取拍照位编号
         int GrabNum = (int)LD.Logic.PlcHandle.Instance.ReadValue(LD.Common.PlcDevice.ArtTestGrabNum);
         //获取视觉示教的定位参数
         NowProjectPara = ProjectParaManager.Instance.GetProjectPara(StageNum);
         //获取当前拍照点的视觉坐标
         NowVisionPara = NowProjectPara.GetVisionPara(GrabNum);
         NowVisionProcess.SetVisionPara(NowVisionPara);
         NowVisionProcess.Do();
         FirstLocalResult = NowVisionProcess.MyLocalResult;
         St_VectorAngle PosToRot      = NowVisionProcess.PosToRot;
         St_VectorAngle TeachPosToRot = NowVisionProcess.TeachPosToRot;
         St_VectorAngle AddVector     = new St_VectorAngle();
         MyVisionBase.VectorAngleToMotionXYTh(PosToRot, TeachPosToRot, out AddVector);
         LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestAddX, -AddVector.Col);
         LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestAddY, AddVector.Row);
         LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestAddTheta, -AddVector.Angle);
         ViewControl view1 = DisplaySystem.GetViewControl(CameraTest.UpCam1);
     }
 }
Example #10
0
 public override bool doLocal()
 {
     try
     {
         NowResult = new LocalResult();
         MyTempLocal.Set(NowImg, NowVisionPara);
         MyTempLocal.doLocal();
         LocalResult TempLocalResult = new LocalResult();
         TempLocalResult = MyTempLocal.GetResult();
         HTuple HomMat = new HTuple();                                                                                      //
                                                                                                                            //¼ÆË㵱ǰͼƬµ½Ê¾½ÌͼƬµÄÆ«ÒƾØÕó
         HOperatorSet.VectorAngleToRigid(TempLocalResult.row, TempLocalResult.col, TempLocalResult.angle, NowVisionPara.Template.CenterY,
                                         NowVisionPara.Template.CenterX, NowVisionPara.Template.TemplateAngle, out HomMat); //
         HObject AffineImg = new HObject();
         HOperatorSet.AffineTransImage(NowImg, out AffineImg, HomMat, "constant", "false");                                 //½ÃÕýͼÏñλÖÃ
         MyBlobLocal.Set(AffineImg, NowVisionPara);
         MyBlobLocal.doLocal();
         //¼ÆË㵱ǰʾ½ÌͼƬµ½µ±Ç°Í¼Æ¬µÄÆ«ÒƾØÕó
         HTuple ReHomMat = new HTuple();
         HOperatorSet.VectorAngleToRigid(NowVisionPara.Template.CenterY, NowVisionPara.Template.CenterX, NowVisionPara.Template.TemplateAngle,
                                         TempLocalResult.row, TempLocalResult.col, TempLocalResult.angle, out ReHomMat);//
         MyBlobLocal.NowResult.AffineTransResult(ReHomMat, out NowResult);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Example #11
0
File: LocalBase.cs Project: Gz1d/Gz
        /// <summary>
        /// 这版软件的思想是将新图片模板匹配后,先转到和示教图片一样的位置,完成定位后,再将定位结果转到实际图片位置
        /// </summary>
        /// <param name="HomMat"></param>
        /// <param name="AffinResult"></param>
        public virtual void AffineTransResult(HTuple HomMat, out LocalResult AffinResult)
        {
            AffinResult = new LocalResult();
            MyVisionBase.AffineTransPt(row, col, HomMat, out AffinResult.row, out AffinResult.col);
            HTuple sx = new HTuple(), sy = new HTuple(), phi = new HTuple(), theta = new HTuple(), tx = new HTuple(), ty = new HTuple();

            HOperatorSet.HomMat2dToAffinePar(HomMat, out sx, out sy, out phi, out theta, out tx, out ty);
            AffinResult.angle = angle - phi.D;
            HOperatorSet.AffineTransContourXld(ShowContour, out AffinResult.ShowContour, HomMat);
            AffinResult.IsLocalOk = true;
        }
Example #12
0
 private void StartCaliBtn_Click(object sender, EventArgs e)
 {
     System.Threading.Tasks.Task.Factory.StartNew(new Action(() => {
         try {
             #region
             List <Point2Db> PixelPtList = new List <Point2Db>();//像素点的坐标集合
             LocalPara NowLocalPara      = TeachCaliPara.localPara;
             RectangleF RectFi           = new RectangleF();
             LocalManager MyLocal        = new LocalManager(); //定位类初始化
             LocalResult MyResult        = new LocalResult();
             Point2Db Pt         = new Point2Db();
             HObject CaliContour = new HObject();
             HOperatorSet.GenEmptyObj(out CaliContour);
             if (GrabedImg == null)
             {
                 MessageBox.Show(" 请先加载一张图片");
                 return;
             }
             for (int i = 0; i < TeachCaliPara.ListRectRegion.Count; i++)
             {
                 RectFi = TeachCaliPara.ListRectRegion[i];
                 TeachCaliPara.localPara.localSetting.SearchAreaX  = (int)RectFi.X;
                 TeachCaliPara.localPara.localSetting.SearchAreaY  = (int)RectFi.Y;
                 TeachCaliPara.localPara.localSetting.SearchWidth  = (int)RectFi.Width;
                 TeachCaliPara.localPara.localSetting.SearchHeight = (int)RectFi.Height;
                 MyLocal.SetLocalModel(TeachCaliPara.localPara.localSetting.localModel);
                 MyLocal.SetParam(GrabedImg, TeachCaliPara.localPara);
                 MyLocal.doLocal();
                 MyResult    = MyLocal.GetResult();
                 CaliContour = CaliContour.ConcatObj(MyResult.ShowContour);
                 //GetCaliMarkPts1(GrabedImg, MinGray, MaxGray, MarkR, out ContourU, out ListMarkCenterI);  //找出Mark点
                 HObject CenterContour = new HObject();
                 HOperatorSet.GenCrossContourXld(out CenterContour, MyResult.row, MyResult.col, 50, 0);  //生成找到的圆心轮廓
                 HOperatorSet.ConcatObj(CaliContour, CenterContour, out CaliContour);
                 Pt.Col = MyResult.col;
                 MyVisionBase.AdjImgRow(GrabedImg, ref MyResult.row);
                 Pt.Row = MyResult.row;
                 PixelPtList.Add(Pt);
                 view1.Refresh();
                 view1.AddViewImage(GrabedImg);
                 view1.AddViewObject(CaliContour);
                 view1.Repaint();
             }
             #endregion
             bool IsTrue = true;
             MyVisionBase.VectorToHomMat(PixelPtList, TeachCaliPara.ListPt2D, out TeachCaliPara.HomMat, out IsTrue);
         }
         catch
         { }
     }));
 }
Example #13
0
File: LocalBase.cs Project: Gz1d/Gz
        public override void AffineTransResult(HTuple HomMat, out LocalResult AffinResult)
        {
            AffinResult = this;
            MyVisionBase.AffineTransPt(row, col, HomMat, out AffinResult.row, out AffinResult.col);
            HTuple sx = new HTuple(), sy = new HTuple(), phi = new HTuple(), theta = new HTuple(), tx = new HTuple(), ty = new HTuple();

            HOperatorSet.HomMat2dToAffinePar(HomMat, out sx, out sy, out phi, out theta, out tx, out ty);
            AffinResult.angle = angle - phi.D;

            HOperatorSet.AffineTransContourXld(this.DetectContour, out ((LineCircRectRlt)AffinResult).DetectContour, HomMat);
            HObject AffineNgCont = new HObject();

            HOperatorSet.GenEmptyObj(out AffineNgCont);

            HObject myCont = new HObject();

            HOperatorSet.AffineTransRegion(this.NgContour, out AffineNgCont, HomMat, "constant");
            HOperatorSet.CopyObj(AffineNgCont, out ((LineCircRectRlt)AffinResult).NgContour, 1, -1);
        }
Example #14
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();
        }
Example #15
0
 public override bool doLocal()
 {
     try
     {
         NowResult = new LocalResult();
         St_LinesParam LinesPara = new St_LinesParam(2);
         LinesPara = NowVisionPara.Lines;
         HTuple  LineRow1 = new HTuple(), LineCol1 = new HTuple(), LineRow2 = new HTuple(), LineCol2 = new HTuple();
         HObject LinesContour = new HObject(), LinePtCont = new HObject();
         //找出所有的直线
         LineTypePos.FindLine(LinesPara, NowImg, out LineRow1, out LineCol1, out LineRow2, out LineCol2, out LinesContour, out LinePtCont);
         St_CirclesParam CirclesParam = new St_CirclesParam(2);
         CirclesParam = NowVisionPara.Circles;
         HTuple  CircleRows = new HTuple(), CircleCols = new HTuple(), CircleRs = new HTuple(), StartPhis = new HTuple(), EndPhis = new HTuple();
         HObject CircleContour = new HObject(), CircleCont = new HObject(), CenterCross = new HObject();
         HTuple  angle;
         //图像坐标轴X轴到直线1的角度
         HOperatorSet.AngleLx(LineRow1[0], LineCol1[0], LineRow2[0], LineCol2[0], out angle);
         //找出要找的圆
         CircleTypePos.FindCircle(NowImg, CirclesParam, out CircleRows, out CircleCols, out CircleRs, out StartPhis, out EndPhis,
                                  out CircleContour, out CircleCont, out CenterCross);
         HOperatorSet.ConcatObj(CircleContour, CircleCont, out CircleContour);
         HOperatorSet.ConcatObj(CircleContour, CenterCross, out CircleContour);
         CircleCont.Dispose();
         CenterCross.Dispose();
         HOperatorSet.ConcatObj(CircleContour, LinesContour, out CircleContour);
         HOperatorSet.ConcatObj(CircleContour, LinePtCont, out CircleContour);
         LinesContour.Dispose();
         LinePtCont.Dispose();
         NowResult.row         = CircleRows[0].D;
         NowResult.col         = CircleCols[0].D;
         NowResult.angle       = angle.D;
         NowResult.ShowContour = CircleContour;
         return(true);
     }
     catch
     { return(false); }
 }
Example #16
0
File: LocalBase.cs Project: Gz1d/Gz
 public virtual bool doLocal()
 {
     NowResult = new LocalResult();
     return(true);
 }
Example #17
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();
            }));
        }
Example #18
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);
        }
Example #19
0
        /// <summary>
        /// 获取九点标定的九个世界坐标点,九个像素坐标点
        /// </summary>
        /// <param name="StartPt">示教起始点</param>
        /// <param name="EndPt">示教终点</param>
        /// <param name="IsCameraMovingWithAxisX"></param>
        /// <param name="IsCameraMovingWithAxisY"></param>
        /// <param name="PixelPosP"></param>
        /// <param name="MotionPosW"></param>
        /// <returns></returns>
        public bool GetHandEyeCaliPt(Point2Db StartPt, Point2Db EndPt, bool IsCameraMovingWithAxisX, bool IsCameraMovingWithAxisY,
                                     out List <Point2Db> PixelPosP, out List <Point2Db> MotionPosW)
        {
            int StepCount = 3;

            PixelPosP  = new List <Point2Db>();
            MotionPosW = new List <Point2Db>();
            Point2Db      startPt = new Point2Db(Math.Min(StartPt.Col, EndPt.Col), Math.Min(StartPt.Row, EndPt.Row));
            Point2Db      endPt = new Point2Db(Math.Max(StartPt.Col, EndPt.Col), Math.Max(StartPt.Row, EndPt.Row));
            double        recWidth = endPt.Col - startPt.Col;
            double        recHeight = endPt.Row - startPt.Row;
            double        stepX = recWidth / (StepCount - 1);
            double        stepY = recHeight / (StepCount - 1);
            List <double> supposeX = new List <double>(), supposeY = new List <double>();
            List <double> currentX = new List <double>(), currentY = new List <double>();
            LocalManager  MyLocal  = new LocalManager(); //定位类初始化
            LocalResult   MyResult = new LocalResult();

            MyLocal.SetLocalModel(TeachCaliPara.localPara.localSetting.localModel);
            HOperatorSet.GenEmptyObj(out CaliContour);
            for (int row = 0; row <= StepCount - 1; row += 1)
            {
                for (int col = 0; col <= StepCount - 1; col += 1)
                {
                    Point2Db movePt     = new Point2Db();                   //九点标定时,相机移动到一个点,然后开始拍照
                    Point2Db CaliMovePt = new Point2Db();                   //九点标定时的点,坐下角为第一个点,右上角为第九个点
                    if (IsCameraMovingWithAxisX && IsCameraMovingWithAxisY) //相机跟随XY周移动
                    {
                        movePt = new Point2Db(startPt.Col + (StepCount - 1 - col) * stepX, startPt.Row + (StepCount - 1 - row) * stepY);
                    }
                    else if ((IsCameraMovingWithAxisX) && (!IsCameraMovingWithAxisY))    //相机跟随X轴移动
                    {
                        movePt = new Point2Db(startPt.Col + (StepCount - 1 - col) * stepX, startPt.Row + row * stepY);
                    }
                    else if ((!IsCameraMovingWithAxisX) && IsCameraMovingWithAxisY)      //相机跟随Y轴移动
                    {
                        movePt = new Point2Db(startPt.Col + col * stepX, startPt.Row + (StepCount - 1 - row) * stepY);
                    }
                    else if ((!IsCameraMovingWithAxisX) && (!IsCameraMovingWithAxisY))   //相机固定
                    {
                        movePt = new Point2Db(startPt.Col + col * stepX, startPt.Row + row * stepY);
                    }
                    CaliMovePt = new Point2Db(startPt.Col + col * stepX, startPt.Row + row * stepY);
                    // 控制平台移动到目标位置
                    if (!MotionManager.Instance.SetCoordiPos(movePt.Col, movePt.Row, TeachCaliPara.EndCaliPt.angle))
                    {
                        MessageBox.Show("X Y轴运动失败!");
                        return(false);
                    }
                    Thread.Sleep(2000);
                    GrabedImg = new HObject();
                    CameraCtrl.Instance.GrabImg(TeachCaliPara.cam, out GrabedImg);  //相机拍照抓取图片
                    Point2Db Pt = new Point2Db();
                    if (GrabedImg.IsInitialized())
                    {
                        MyLocal.SetParam(GrabedImg, TeachCaliPara.localPara); //设置定位参数
                        MyLocal.doLocal();                                    //执行定位算法
                        MyResult = MyLocal.GetResult();                       //获取定位结果
                        view1.ResetView();
                        CaliContour = CaliContour.ConcatObj(MyResult.ShowContour);
                        view1.AddViewImage(GrabedImg);
                        view1.AddViewObject(MyResult.ShowContour);
                        //GetCaliMarkPts1(GrabedImg, MinGray, MaxGray, MarkR, out ContourU, out ListMarkCenterI);  //找出Mark点
                        HObject CenterContour = new HObject();
                        HOperatorSet.GenCrossContourXld(out CenterContour, MyResult.row, MyResult.col, 50, 0);  //生成找到的圆心轮廓
                        HOperatorSet.ConcatObj(CaliContour, CenterContour, out CaliContour);
                        view1.AddViewObject(CaliContour);
                        view1.Repaint();
                        Pt.Col = MyResult.col;
                        Pt.Row = MyResult.row;
                        PixelPosP.Add(Pt);
                        MotionPosW.Add(CaliMovePt);
                    }
                    else
                    {
                        MessageBox.Show("采集图片失败");
                        return(false);
                    }
                }
            }
            return(true);
        }
Example #20
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, "运行日志");
                    }
                }
            }
        }
Example #21
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运行日志");
            }
        }