Beispiel #1
0
        private void Restart_Button_Click(object sender, EventArgs e)
        {
            if (DownCam_ComboBox.SelectedIndex == UpCam_ComboBox.SelectedIndex)
            {
                Global.Instance.DisplayText("Up cam can't be the same as downcam");
                return;
            }
            new Thread(delegate() {
                if (downVideoCapture.IsRunning())
                {
                    downVideoCapture.Close();
                }
                if (upVideoCapture.IsRunning())
                {
                    upVideoCapture.Close();
                }
            }).Start();

            while (downVideoCapture.IsRunning())
            {
                Global.DoBackgroundWork();
            }
            while (upVideoCapture.IsRunning())
            {
                Global.DoBackgroundWork();
            }

            upVideoCapture.Start(UpCam_ComboBox.SelectedIndex);
            downVideoCapture.Start(DownCam_ComboBox.SelectedIndex);

            //save
            Properties.Settings.Default.DownCam_index = DownCam_ComboBox.SelectedIndex + 1;
            Properties.Settings.Default.UpCam_index   = UpCam_ComboBox.SelectedIndex + 1;
            Properties.Settings.Default.Save();
        }
Beispiel #2
0
        public static Shapes.Thing FindClosest(VideoProcessing vp, Shapes.ShapeTypes type, double FindTolerance, int retries, string templatefilename = null, double threshold = -1)
        {
            while (retries-- > 0)
            {
                Global.DoBackgroundWork();
                //for (int i = 0; i < 3; i++) vp.GetMeasurementFrame(); //skip 10 frames
                Shapes.Thing thing = null;
                switch (type)
                {
                case Shapes.ShapeTypes.Circle:
                    thing = GetClosestCircle(vp, FindTolerance);
                    break;

                case Shapes.ShapeTypes.Fiducial:
                    var things = FindTemplates(vp, templatefilename, threshold);
                    thing = GetClosest(GetWithin(things, FindTolerance));
                    break;

                case Shapes.ShapeTypes.Rectangle:
                    thing = GetSmallestCenteredRectangle(FindRectangles(vp));
                    break;

                default:
                    Global.Instance.DisplayText("detection of " + type + " not yet supported", Color.Red);
                    break;
                }
                if (thing != null)
                {
                    return(thing);
                }
            }
            return(null);
        }
Beispiel #3
0
        public Bitmap GetFrame()
        {
            Global.DoBackgroundWork(); //make sure we get an updated image
            bool acquiredLock = false;

            if (lastFrame == null)
            {
                return(null);
            }
            Bitmap ret = null;

            while (ret == null)
            {
                try {
                    Monitor.TryEnter(NewFrameLock, ref acquiredLock);
                    if (acquiredLock)
                    {
                        ret = (Bitmap)lastFrame.Clone();
                    }
                    else
                    {
                        Thread.Sleep(1); //wait for someone else to give up the lock?
                    }
                } finally {
                    if (acquiredLock)
                    {
                        Monitor.Exit(NewFrameLock);
                    }
                }
            }
            return(ret);
        }
Beispiel #4
0
        public Bitmap GetMeasurementFrame()
        {
            Global.DoBackgroundWork();
            var frame = videoCapture.GetFrame();

            frame = ProcessFrame(frame);
            return(frame);
        }
Beispiel #5
0
        public void TakePhotoOfPartAtCurrentLocation(TapeObj t)
        {
            if (t == null)
            {
                return;
            }
            var dir = Global.BaseDirectory + @"\images\";

            Directory.CreateDirectory(dir);

            var size = t.GetComponentSize();

            if (size == null)
            {
                return;
            }
            var s = new PartLocation(size.Width, size.Height);                  //assumes 0 degree rotation

            s = (1.5 * s) / MainForm.cameraView.downVideoProcessing.mmPerPixel; //convert to pixels and ad extra 25%
            s.Rotate(t.PartAngle * Math.PI / 180d);                             //and rotate to final position
            s.X = Math.Abs(s.X);                                                //correct for sign changes
            s.Y = Math.Abs(s.Y);

            MainForm.cameraView.SetDownCameraFunctionSet("ComponentPhoto");
            Global.DoBackgroundWork(); //let new images be processed

            var topleft = MainForm.cameraView.downVideoProcessing.FrameCenter - (.5 * s);
            var rect    = new Rectangle(topleft.ToPoint(), s.ToSize());
            var filter  = new Crop(rect);

            using (var image = MainForm.cameraView.downVideoProcessing.GetMeasurementFrame()) {
                using (var cropped = filter.Apply(image)) {
                    var filename = dir + t.ID.Replace(" ", "_") + ".jpg";
                    if (File.Exists(filename))
                    {
                        File.Delete(filename);
                    }
                    cropped.Save(filename, ImageFormat.Jpeg);
                    t.TemplateFilename = filename;
                }
            }
        }
Beispiel #6
0
 public static List <Shapes.Rectangle> FindRectangles(VideoProcessing vp)
 {
     Global.DoBackgroundWork();
     return(FindRectangles(vp, vp.GetMeasurementFrame()));
 }
Beispiel #7
0
 public bool SetCurrentTapeMeasurement(string type)
 {
     MainForm.cameraView.SetDownCameraFunctionSet(type + "Tape");
     Global.DoBackgroundWork(100);
     return(true);
 }
Beispiel #8
0
        public PartLocation GetNextComponentPartLocation(TapeObj tapeObj)
        {
            if (tapeObj == null)
            {
                return(null);
            }
            MainForm.DisplayText("GotoNextPart_m(), tape id: " + tapeObj.ID);

            //Load & Parse Data
            if (!tapeObj.IsFullyCalibrated)
            {
                MainForm.ShowSimpleMessageBox("Tape " + tapeObj.ID + " is not yet calibrated.  Please do so and retry");
                return(null);
            }

            PartLocation targetLocation = tapeObj.GetCurrentPartLocation();

            MainForm.DisplayText("Part " + tapeObj.CurrentPartIndex() + "  Source Location = " + targetLocation, Color.Blue);

            // see if part exists for part based detection
            if (tapeObj.TemplateBased)
            {
                Global.Instance.mainForm.cameraView.DownCameraReset();
                if (!MainForm.Cnc.CNC_XY(targetLocation))
                {
                    return(null);
                }
                var thing = MainForm.GoToClosestThing(Shapes.ShapeTypes.Fiducial, 1, .2, null, tapeObj.TemplateFilename, .75);
                if (thing == null)
                {
                    MainForm.DisplayText("No Part Detected At This Location", Color.Red);
                    return(null);
                }
                MainForm.DisplayText("Part Detected : " + thing);
            }


            if (tapeObj.IsLocationBased)
            {
                targetLocation = GetLocationBasedComponent(tapeObj);
            }
            else
            // enhanced part detection
            if (tapeObj.FirstHole != null)
            {
                SetCurrentTapeMeasurement(tapeObj.TapeType);
                Cnc.CNC_XY(tapeObj.GetNearestCurrentPartHole());
                var hole = MainForm.GoToClosestThing(Shapes.ShapeTypes.Circle, 1.5, .1);
                if (hole == null)
                {
                    MainForm.DisplayText("Unable to detect part hole, aborting");
                    return(null);
                }
                var offset = tapeObj.GetCurrentPartLocation() - tapeObj.GetNearestCurrentPartHole();
                targetLocation = hole.ToPartLocation() + offset;
                MainForm.cameraView.DownCameraReset();
            }

            //------------------- PART SPECIFIC LOGIC GOES HERE --------------------//
            if (tapeObj.PartType == "QFN")
            {
                MainForm.DisplayText("USING ENHANCE PART PICKUP", Color.HotPink);
                if (!MainForm.Cnc.CNC_XY(targetLocation))
                {
                    return(null);
                }
                // setup view
                SetCurrentTapeMeasurement(tapeObj.TapeType);
                MainForm.cameraView.downSettings.FindRectangles = true;

                // MainForm.cameraView.downVideoProcessing.FindRectangles = true;
                // move closer and get exact coordinates plus rotation
                var rect = (Shapes.Rectangle)MainForm.GoToClosestThing(Shapes.ShapeTypes.Rectangle, 1.5, .025);

                if (rect == null)
                {
                    MainForm.cameraView.DownCameraReset();
                    return(null);
                }
                Global.DoBackgroundWork(500);
                var rectAngle = rect.AngleOffsetFrom90();

                targetLocation   = rect.ToPartLocation();
                targetLocation.A = tapeObj.OriginalPartOrientationVector.ToDegrees() + rectAngle;

                MainForm.cameraView.DownCameraReset();
            }

            return(targetLocation);
        }