Ejemplo n.º 1
0
        private void action()
        {
            // Stack for temporary objects
            HObject[] OTemp = new HObject[20];
            long      SP_O  = 0;
            // Local iconic variables
            HObject ho_Circle, region;
            HObject ho_SelectedRegions, ho_outCircle;
            // Local control variables

            //HTuple hv_blackOrwhite, hv_threshold_value;
            //HTuple hv_method, hv_andOror, hv_method_min_value, hv_method_max_value;
            HTuple hv_outer_Row, hv_outer_Column;
            HTuple hv_outer_Radius, hv_Circularity;
            HTuple hv_Number = new HTuple(), hv_result = new HTuple();

            // Initialize local and output iconic variables
            //HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_Circle);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_outCircle);
            HOperatorSet.GenEmptyObj(out region);
            try
            {
                //HOperatorSet.SetColor(this.LWindowHandle, "green");
                //    out hv_Radius);
                ho_Circle.Dispose();
                HOperatorSet.GenCircle(out ho_Circle, DCenterRow, DCenterColumn, DRadius);
                HOperatorSet.Intersection(ho_Circle, algorithm.Region, out region);
                region_outer_circle(region, out ho_outCircle, out hv_outer_Row,
                                    out hv_outer_Column, out hv_outer_Radius, out hv_Circularity);

                //HOperatorSet.DispObj(ho_outCircle, this.LWindowHandle);
                RegionToDisp.Dispose();
                if (!RegionToDisp.IsInitialized())
                {
                    HOperatorSet.CopyObj(ho_outCircle, out RegionToDisp, 1, 1);
                }
                else
                {
                    HOperatorSet.ConcatObj(ho_outCircle, RegionToDisp, out RegionToDisp);
                }
                //RegionToDisp
                HOperatorSet.CountObj(ho_outCircle, out hv_Number);
                if ((int)(new HTuple(hv_Number.TupleEqual(1))) != 0)
                {
                    hv_result = hv_result.TupleConcat("center_row");
                    hv_result = hv_result.TupleConcat(hv_outer_Row * pixeldist);
                    hv_result = hv_result.TupleConcat("center_column");
                    hv_result = hv_result.TupleConcat(hv_outer_Column * pixeldist);
                    hv_result = hv_result.TupleConcat("diameter");
                    hv_result = hv_result.TupleConcat(2 * hv_outer_Radius * pixeldist);
                    hv_result = hv_result.TupleConcat("circularity");
                    hv_result = hv_result.TupleConcat(hv_Circularity);
                    result    = hv_result.Clone();
                }
                else
                {
                    hv_result = hv_result.TupleConcat("center_row");
                    hv_result = hv_result.TupleConcat(0);
                    hv_result = hv_result.TupleConcat("center_column");
                    hv_result = hv_result.TupleConcat(0);
                    hv_result = hv_result.TupleConcat("diameter");
                    hv_result = hv_result.TupleConcat(0);
                    hv_result = hv_result.TupleConcat("circularity");
                    hv_result = hv_result.TupleConcat(0);
                    result    = hv_result.Clone();
                }
            }
            catch (HalconException HDevExpDefaultException)
            {
                this.Result = new HTuple();
                MyDebug.ShowMessage("CircleToolsError:=" + HDevExpDefaultException);
                hv_result = hv_result.TupleConcat("center_row");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("center_column");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("diameter");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("circularity");
                hv_result = hv_result.TupleConcat(0);
                result    = hv_result.Clone();
            }
            finally
            {
                ho_Circle.Dispose();
                region.Dispose();
                ho_SelectedRegions.Dispose();
                ho_outCircle.Dispose();
                algorithm.Region.Dispose();
            }
        }
Ejemplo n.º 2
0
        public void ReadPos(string fileName)
        {
            try
            {
                PosArray = new List <int>();
                IOs      = new List <ushort>();
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(fileName);
                XmlNodeList xnl;
                XmlNode     xn = null;
                xn  = xmldoc.GetElementsByTagName("位置")[0];
                xnl = xn.ChildNodes;
                foreach (XmlNode x in xnl)
                {
                    XmlElement xe = x as XmlElement;
                    int        r  = 0;
                    int.TryParse(xe.Attributes["Pos"].Value, out r);
                    PosArray.Add(r);
                }
                xn  = xmldoc.GetElementsByTagName("相机电磁阀点")[0];
                xnl = xn.ChildNodes;
                foreach (XmlNode x in xnl)
                {
                    XmlElement xe = x as XmlElement;
                    ushort     r  = 0;
                    ushort.TryParse(xe.Attributes["IO"].Value, out r);
                    IOs.Add(r);
                }
                xn  = xmldoc.GetElementsByTagName("相机数量")[0];
                xnl = xn.ChildNodes;
                foreach (XmlNode x in xnl)
                {
                    XmlElement xe = x as XmlElement;
                    ushort     r  = 0;
                    ushort.TryParse(xe.Attributes["Count"].Value, out r);
                    cameraCount = r;
                }

                xn  = xmldoc.GetElementsByTagName("电磁阀数量")[0];
                xnl = xn.ChildNodes;
                foreach (XmlNode x in xnl)
                {
                    XmlElement xe = x as XmlElement;
                    ushort     r  = 0;
                    ushort.TryParse(xe.Attributes["Count"].Value, out r);
                    valveCount = r;
                }
            }
            catch (Exception e)
            {
                MyDebug.ShowMessage(e, "相机位置初始化错误");
            }
            XmlDocument xd   = new XmlDocument();
            string      path = PathHelper.dataPath + @"\Position.xml";

            xd.Load(fileName);

            XmlNode xnd;

            xnd = xd.GetElementsByTagName("速度")[0];

            if (xnd != null)
            {
                XmlNodeList xnl;
                xnl = xnd.ChildNodes;
                foreach (XmlNode x in xnl)
                {
                    Card.maxspeed = int.Parse(x.Attributes["Value"].Value);
                    Card.maxspeed = Card.maxspeed < Card.minspeed ? 1000 : Card.maxspeed;
                }
            }
        }
Ejemplo n.º 3
0
        private void OnImageProcessedEvent(CCamera instance, HObject ho_Image)
        {
            try
            {
                if (InvokeRequired)
                {
                    Invoke(new CCamera.OnImageProcessedEventHandler(OnImageProcessedEvent), new object[] { instance, ho_Image });
                    return;
                }
                kk++;
                //Console.WriteLine("kk:" +kk);
                instance.tempImage.Dispose();
                HOperatorSet.CopyImage(instance.Image, out instance.tempImage);

                HTuple cwindow = new HTuple();
                switch (instance.logicName)
                {
                case "CCD1": cwindow = hWindowControl1.HalconWindow;
                    break;

                case "CCD2": cwindow = hWindowControl2.HalconWindow;
                    break;

                case "CCD3": cwindow = hWindowControl3.HalconWindow;
                    break;

                case "CCD4": cwindow = hWindowControl4.HalconWindow;
                    break;

                case "CCD5": cwindow = hWindowControl5.HalconWindow;
                    break;
                    //case "CCD6": cwindow = hWindowControl6.HalconWindow;
                    //    break;
                }

                for (int i = 0; i < 6; i++)
                {
                    if (instance.logicName == "CCD" + i.ToString())
                    {
                        HOperatorSet.SetColor(cwindow, "green");
                        HOperatorSet.SetDraw(cwindow, "margin");
                        HTuple w, h;
                        HOperatorSet.GetImageSize(ho_Image, out w, out h);

                        HOperatorSet.SetPart(cwindow, 0, 0, h, w);

                        HOperatorSet.DispObj(ho_Image, cwindow);

                        if (instance.RegionToDisp.IsInitialized() && instance.resultHTuple.Length > 0)
                        {
                            HOperatorSet.DispObj(instance.RegionToDisp, cwindow);
                            HalconHelp.set_display_font(cwindow, 14, "courier", "false", "false");
                            HalconHelp.disp_message(cwindow, instance.resultHTuple, "window", 10, 10, "green", "false");
                        }
                    }
                }
                switch (instance.logicName)
                {
                case "CCD1": cwindow = hWindowControl6.HalconWindow;
                    break;

                case "CCD2": cwindow = hWindowControl7.HalconWindow;
                    break;

                case "CCD3": cwindow = hWindowControl8.HalconWindow;
                    break;

                case "CCD4": cwindow = hWindowControl9.HalconWindow;
                    break;

                case "CCD5": cwindow = hWindowControl10.HalconWindow;
                    break;
                }

                for (int i = 0; i < 6; i++)
                {
                    if (instance.logicName == "CCD" + i.ToString())
                    {
                        HOperatorSet.SetColor(cwindow, "green");
                        HOperatorSet.SetDraw(cwindow, "margin");
                        HTuple w, h;
                        HOperatorSet.GetImageSize(ho_Image, out w, out h);

                        HOperatorSet.SetPart(cwindow, 0, 0, h, w);

                        HOperatorSet.DispObj(ho_Image, cwindow);

                        if (instance.RegionToDisp.IsInitialized() && instance.resultHTuple.Length > 0)
                        {
                            HOperatorSet.DispObj(instance.RegionToDisp, cwindow);
                            //if (instance.logicName == "CCD1")
                            //{
                            //    HalconHelp.set_display_font(cwindow, 14, "courier", "false", "false");
                            //    HalconHelp.disp_message(cwindow, instance.resultHTuple, "window", 2, 2, "green", "false");
                            //}
                        }
                    }
                }
                DataRowCollection drc = ds.Tables["detailTable"].Rows;

                int    rs      = 0;
                string shijian = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
                string path    = PathHelper.currentProductPath + @"\" + shijian + ".txt";
                foreach (DataRow dr in drc)
                {
                    string s = dr[0].ToString();

                    if (dr[0].ToString() == instance.logicName)
                    {
                        if (instance.result.resultToShow.Count > rs)
                        {
                            double cl = double.Parse(instance.result.resultToShow[rs].data);
                            double xz = (double)dr[2];
                            dr[5] = Math.Round((cl + xz), 3);
                            if (double.Parse(dr[5].ToString()) <= double.Parse(dr[3].ToString()) && double.Parse(dr[5].ToString()) >= double.Parse(dr[4].ToString()))
                            {
                                dr[6] = "OK";
                            }
                            else
                            {
                                dr[6] = "NG";
                            }
                            if (instance.goodcountlist.Count() != 0)
                            {
                                dr[7] = Math.Round((double)(instance.goodcountlist[rs] / (double)Turntable.Instance.pn.totalCount * 100), 2).ToString() + "%";
                                //Console.WriteLine("" + instance.goodcountlist[rs] + "---" + (double)Turntable.Instance.pn.totalCount);
                            }
                            rs++;
                        }
                        else
                        {
                            dr[5] = 0;
                        }
                        // File.AppendAllText(path,dr[1].ToString() + "  " + dr[5].ToString(), Encoding.Default);
                    }
                }

                //File.AppendAllText(path,Environment.NewLine, Encoding.Default);
                DataHelper.CheckData(ds, instance.logicName, instance);
            }
            catch (Exception e)
            {
                MyDebug.ShowMessage("界面图像处" + e.Message);
            }
            finally
            {
                ho_Image.Dispose();
                instance.RegionToDisp.Dispose();
                instance.Image.Dispose();
                instance.resultHTuple = new HTuple();
                //UpdateProInfo();
            }
        }
Ejemplo n.º 4
0
        void Run()
        {
            //将当前线程绑定到指定的cpu核心上
            GetCpu.SetThreadAffinityMask(GetCpu.GetCurrentThread(), new UIntPtr(0x4));

            kzwbmlt12 = DateTime.Now;
            string   path = PathHelper.currentProductPath + @"\" + "spd" + ".txt";
            DateTime t1;
            int      xjfwi;

            spd = Card.maxspeed;
            int preSignal = 0, signal = 0;

            PCI408.PCI408_counter_config(0, 3);
            PCI408.PCI408_set_encoder(0, 0);
            int  fwjs1 = 0, fwjs2 = 0; sdkz3 = 100000;
            long cuowu = PCI408.PCI408_compare_set_filter_Extern(Card.cardNo, 100000);

            PCI408.PCI408_config_latch_mode(Card.cardNo, Card.zhouhao);
            sdkzt1 = DateTime.Now; sdkzt2 = DateTime.Now; sdkzt4 = DateTime.Now; sdkzt3 = DateTime.Now;
            long jcqbz;

            for (int xjh = 0; xjh < PosArray.Count(); xjh++)
            {
                PCI408.PCI408_compare_clear_points_Extern(0, (ushort)(xjh + 1));
                PCI408.PCI408_compare_config_Extern(Card.cardNo, (ushort)(xjh + 1), 1, Card.zhouhao, IOs[xjh]);
                PCI408.PCI408_compare_set_pulsetimes_Extern(0, (ushort)(xjh + 1), 20);
            }
            PCI408.PCI408_reset_latch_flag(Card.cardNo);
            long s5;

            currentPos = (int)PCI408.PCI408_get_encoder(Card.cardNo);
            int[] sigci = new int[5], psigci = new int[5];
            for (int sigfi = 0; sigfi < 5; sigfi++)
            {
                sigci[sigfi]  = 0;
                psigci[sigfi] = 0;
            }

            //?
            int zdwy = 40000;

            while (isStart)
            {
                try
                {
                    //    for (xjfwi = 1; xjfwi < cameraCount + 1; xjfwi++)
                    //{
                    //    xjfws[xjfwi].t2 = DateTime.Now;
                    //    //if (xjfws[xjfwi].tsc() > 200000 && copos[xjfwi].pos.Count() > 0)
                    //   //if (xjfws[xjfwi].tsc() > 200000)
                    //   // {
                    //   //     //copos[0].pos.Clear();
                    //   //     //copos[1].pos.Clear();
                    //   //     //copos[2].pos.Clear();
                    //   //     //copos[3].pos.Clear();
                    //   //     //copos[4].pos.Clear();
                    //   //     //copos[5].pos.Clear();
                    //   //     //copos[6].pos.Clear();
                    //   //     nutqueue.Clear();
                    //   //     xjfws.Initialize();
                    //   //     copos.Initialize();
                    //   //     timers.Initialize();
                    //   //     xjfws = new xjfw[7];
                    //   //     //for (xjfwi = 0; xjfwi < 5; xjfwi++)
                    //   //     //{
                    //   //     //    xjfws[xjfwi] = new xjfw();
                    //   //     //    xjfws[xjfwi].ID = xjfwi;
                    //   //     //    xjfws[xjfwi].t1 = DateTime.Now;
                    //   //     //    xjfws[xjfwi].t2 = DateTime.Now;
                    //   //     //}
                    //   //     copos = new coppos[7];
                    //   //     //for (int k = 0; k < 5; k++)
                    //   //     //{
                    //   //     //    copos[k] = new coppos();
                    //   //     //    copos[k].ID = k;
                    //   //     //    //copos[k].onwrite = false;
                    //   //     //}
                    //   //     timers = new MyTimer[7];
                    //   //     //for (int i = 0; i < timers.Length; i++)
                    //   //     //{
                    //   //     //    timers[i] = new MyTimer();
                    //   //     //    timers[i].ID = i;
                    //   //     //    //timers[i].OnTimerEvent += new MyTimer.OnTimerEventHandler(OnTimer);
                    //   //     //}
                    //   //     //Thread.Sleep(1000);
                    //   // }
                    //}
                    //    t1 = DateTime.Now;
                    currentPos = (int)PCI408.PCI408_get_encoder(Card.zhouhao);
                    for (int sr = 0; sr < cameraCount; sr++)
                    {
                        sigci[sr] = PCI408.PCI408_read_inbit(Card.cardNo, (ushort)xjzsr[sr]);
                        if (sigci[sr] == 0 && psigci[sr] == 1)
                        {
                            int length1 = dpdl[sr].Count(), jr = 200, jr1 = 5000;
                            for (int listi = 0; listi < length1; listi++)
                            {
                                int ssg = Math.Abs(dpdl[sr][listi] + PosArray[sr] - currentPos);
                                if (ssg < jr1)
                                {
                                    jr1 = ssg;
                                    jr  = listi;
                                }
                            }
                            if (jr != 200 && jr1 < 5000)
                            {
                                copos[sr].pos.Add(dpdl[sr][jr]);
                                //   Console.WriteLine("trun " + DateTime.Now.ToString("mm-ss-fff"));
                                dpdl[sr].RemoveAt(jr);
                            }
                        }
                        psigci[sr] = sigci[sr];
                    }
                    for (int sr1 = 0; sr1 < cameraCount; sr1++)
                    {
                        int length2 = dpdl[sr1].Count();
                        for (int listi1 = 0; listi1 < length2; listi1++)
                        {
                            if (Math.Abs(currentPos - dpdl[sr1][listi1]) > zdwy)
                            {
                                dpdl[sr1].RemoveAt(listi1);
                            }
                        }
                    }
                    jcqbz = PCI408.PCI408_get_latch_flag(Card.cardNo);    //读取锁存器状态
                    if ((jcqbz & 0xf00) > 0)
                    {
                        PCI408.PCI408_reset_latch_flag(Card.cardNo);
                        signal = PCI408.PCI408_get_latch_value(Card.zhouhao);
                    }
                    if (fwjs1 - fwjs2 > 4000)
                    {
                        fwjs1 = fwjs2;
                        for (int sr = 0; sr < cameraCount; sr++)
                        {
                            sigci[sr]  = 0;
                            psigci[sr] = 0;
                            dpdl[sr].Clear();
                            copos[sr].pos.Clear();
                        }
                        for (int xjh = 0; xjh < PosArray.Count(); xjh++)
                        {
                            PCI408.PCI408_set_encoder(0, 0);
                            PCI408.PCI408_compare_clear_points_Extern(0, (ushort)(xjh + 1));
                            PCI408.PCI408_compare_config_Extern(Card.cardNo, (ushort)(xjh + 1), 1, Card.zhouhao, IOs[xjh]);
                            PCI408.PCI408_compare_set_pulsetimes_Extern(0, (ushort)(xjh + 1), 5);
                        }
                        for (int xjs = 0; xjs < cameraCount; xjs++)
                        {
                            jcqdls[xjs].clear();
                            //File.AppendAllText(path,signal.ToString()+"  ", Encoding.Default);
                        }
                        PCI408.PCI408_reset_latch_flag(Card.cardNo);
                        preSignal = signal;
                        if (isStart)
                        {
                            nutqueue.Clear();
                        }
                    }
                    if (signal != preSignal)
                    {
                        kzwbmlt12 = DateTime.Now;
                        s5        = PCI408.PCI408_compare_get_points_remained_Extern(0, 1);
                        if (s5 > cameraCount + 3)
                        {
                            Nut nut = new Nut();
                            fwjs1         += 1;
                            nut.initialPos = signal;
                            nutqueue.Add(nut);
                            nut.cas = cameraCount;
                            pn.totalCount++;
                            speedTemp++;
                            ss = pn.totalCount;
                            for (int xjs = 0; xjs < cameraCount; xjs++)
                            {
                                PCI408.PCI408_compare_config_Extern(Card.cardNo, (ushort)(xjs + 1), 1, Card.zhouhao, 1);
                                PCI408.PCI408_compare_add_point_Extern(Card.cardNo, (ushort)(xjs + 1), PosArray[xjs] + (int)signal, 1, 9, IOs[xjs]);

                                // copos[nut.posNo + 1].pos.Add (nut.initialPos);
                                // copos[nut.posNo + 1].onwrite = true;
                                dpdl[xjs].Add(signal);
                                // Console.WriteLine("ss" + signal);
                                //File.AppendAllText(path,signal.ToString()+"  ", Encoding.Default);
                            }
                        }
                    }
                    niuli();

                    for (int i = nutqueue.Count - 1; i > -1; i--)
                    {
                        Nut nutc = new Nut();
                        nutc = nutqueue[i];
                        //if (currentPos < nutc.initialPos)
                        //{
                        //    nutqueue.Remove(nutc);
                        //    break;
                        //}
                        if (currentPos - nutc.initialPos > PosArray[cameraCount] - 1000)
                        {
                            int a = nutc.getTheHole();
                            switch (a.ToString())
                            {
                            case "1":
                                //pn.mnum++;
                                //Console.WriteLine("cipin" + pn.mnum);
                                PCI408.PCI408_compare_config_Extern(Card.cardNo, (ushort)(cameraCount + 1), 1, Card.zhouhao, 1);
                                PCI408.PCI408_compare_set_pulsetimes_Extern(0, (ushort)(cameraCount + 1), 10);
                                PCI408.PCI408_compare_add_point_Extern(Card.cardNo, (ushort)(cameraCount + 1), PosArray[cameraCount] + (int)nutc.initialPos, 1, 9, IOs[cameraCount]);
                                break;

                            //case "3":
                            //pn.nnum++;
                            //Console.WriteLine("未识别" + pn.nnum);
                            //PCI408.PCI408_compare_config_Extern(Card.cardNo, (ushort)(cameraCount + 1), 1, Card.zhouhao, 1);
                            //PCI408.PCI408_compare_set_pulsetimes_Extern(0, (ushort)(cameraCount + 1), 20);
                            //PCI408.PCI408_compare_add_point_Extern(Card.cardNo, (ushort)(cameraCount + 1), PosArray[cameraCount] + (int)nutc.initialPos, 1, 9, IOs[cameraCount]);
                            //break;
                            case "2":
                                pn.goodNum++;
                                fwjs2 = fwjs1;
                                PCI408.PCI408_compare_config_Extern(Card.cardNo, (ushort)(cameraCount + 2), 1, Card.zhouhao, 1);
                                PCI408.PCI408_compare_set_pulsetimes_Extern(0, (ushort)(cameraCount + 2), 10);
                                PCI408.PCI408_compare_add_point_Extern(Card.cardNo, (ushort)(cameraCount + 2), PosArray[cameraCount + 1] + (int)nutc.initialPos, 1, 9, IOs[cameraCount + 1]);
                                break;
                            }
                            jcjg2 = nutc.jiance;
                            Thread ccjcjg = new Thread(cunchujiancejieguo);
                            ccjcjg.Start();
                            nutqueue.Remove(nutc);
                            pn.badNum = pn.totalCount - pn.goodNum;
                        }
                    }
                    preSignal = signal;
                    kongzhiwaibu();
                    sdkzt1 = DateTime.Now;
                    sdkzt4 = DateTime.Now;
                    if (Card.sjc(sdkzt3, sdkzt4) < sdkz3)
                    {
                        sdkz3 = Card.sjc(sdkzt3, sdkzt4);
                    }
                    sdkzt3 = DateTime.Now;
                    //if (Card.sjc(sdkzt2, sdkzt1) > 60000)
                    //{
                    //    sdkzt2 = DateTime.Now;
                    //    if (sdkz3 < 10)
                    //    {
                    //        spd = spd + 100;
                    //        PCI408.PCI408_change_speed(Card.cardNo, spd);
                    //        File.AppendAllText(path, spd.ToString() + "  ", Encoding.Default);
                    //    }
                    //}
                }
                catch (Exception e)
                {
                    MyDebug.ShowMessage(e, "运动控制出错");
                }
                finally
                {
                    //MyDebug.ShowMessage(nutqueue.Count.ToString());
                }
            }
        }