Example #1
0
            /// <param name="task">アクティビティのタスク</param>
            /// <param name="handler">タスク終了時のイベントハンドラ</param>
            /// <returns>アクティビティスレッド</returns>
            protected Thread Create(ActivityTask task, ActivityEventHandler handler)
            {
                ThreadStart start = new ThreadStart(delegate
                {
                    ActivityEventArgs args = new ActivityEventArgs();
                    try
                    {
                        task();
                        args.IsCompleted = true;
                    }
                    catch (ThreadAbortException ex)
                    {
                        args.IsAborted = true;
                        args.Exception = ex;
                    }
                    catch (Exception ex)
                    {
                        args.Exception = ex;
                    }
                    finally
                    {
                        MotorControler mc = MotorControler.GetInstance(parameterManager);
                        mc.AbortMoving();
                        mc.SlowDownStopAll();

                        if (handler != null)
                        {
                            handler(this, args);
                        }
                    }
                });

                return(Create(start));
            }
Example #2
0
        void MotorKeyHandler_Unloaded(object sender, System.Windows.RoutedEventArgs e)
        {
#if !_NoHardWare
            MotorControler mc = MotorControler.GetInstance(parameterManager);
            mc.AbortMoving();
            mc.SlowDownStopAll();
#endif
        }
Example #3
0
        private void surfacerecog()
        {
            Surface        surface = Surface.GetInstance(parameterManager);
            MotorControler mc      = MotorControler.GetInstance(parameterManager);

            /*
             *          string[] sp = myTrack.IdString.Split('-');
             *
             *          //string datfileName = string.Format("{0}.dat", System.DateTime.Now.ToString("yyyyMMdd_HHmmss"));
             *          string datfileName = string.Format(@"c:\test\bpm\{0}\{1}-{2}-{3}-{4}-{5}.dat", mod, mod, pl, sp[0], sp[1], System.DateTime.Now.ToString("ddHHmmss"));
             *          BinaryWriter writer = new BinaryWriter(File.Open(datfileName, FileMode.Create));
             */

            try
            {
                if (mc.IsMoving)
                {
                    MessageBoxResult r = MessageBox.Show(
                        Properties.Strings.SurfaceException01,
                        Properties.Strings.Abort + "?",
                        MessageBoxButton.YesNo);
                    if (r == MessageBoxResult.Yes)
                    {
                        mc.AbortMoving();
                    }
                    else
                    {
                        return;
                    }
                }
                mc.Join();

                //Surface surface = Surface.GetInstance(parameterManager);

                try
                {
                    surface.Start(true);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, Properties.Strings.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Properties.Strings.Error);
            }
            mc.Join();
        }
Example #4
0
        public List <Thread> CreateTask()
        {
            List <Thread> taskList = new List <Thread>();

            taskList.Add(Create(new ThreadStart(delegate {
                try {
                    task();
                } catch (ThreadAbortException ex) {
                } finally {
                    MotorControler mc = MotorControler.GetInstance();
                    mc.AbortMoving();
                    mc.SlowDownStopAll();
                }
            })));
            return(taskList);
        }
Example #5
0
        public List <Thread> CreateTask()
        {
            List <Thread> taskList = new List <Thread>();

            taskList.Add(Create(new ThreadStart(delegate {
                try {
                    task();
                } catch (ThreadAbortException ex) {
                } finally {
                    MotorControler mc = MotorControler.GetInstance();
                    mc.AbortMoving();
                    mc.SlowDownStopAll();
                }
            })));
            //SurfaceLanding sl = new SurfaceLanding(parameterManager);
            //taskList.AddRange(sl.CreateTask());
            return(taskList);
        }
Example #6
0
            private void nextTrackButton_Click(object sender, RoutedEventArgs e)
            {
                if (!parameterManager.TracksManager.IsInitialized)
                {
                    MessageBox.Show(Properties.Strings.TracksManagerException01);
                    return;
                }

                TracksManager tracksManager = parameterManager.TracksManager;

                if (tracksManager.TrackingIndex + 1 >= tracksManager.NumOfTracks)
                {
                    MessageBox.Show(Properties.Strings.TrackFoundComplete);
                    return;
                }
                tracksManager.UpdateTrack();
                UpdateTrackInfo();

                MotorControler mc = MotorControler.GetInstance();

                if (mc.IsMoving)
                {
                    if (askAbortMotorMoving())
                    {
                        mc.AbortMoving();
                        stage.WriteLine(Properties.Strings.AbortMotor);
                    }
                    else
                    {
                        return;
                    }
                }
                Track track = tracksManager.Track;

                stage.WriteLine(Properties.Strings.Moving);
#if !NoHardware
                mc.MovePointXY(track.MsX, track.MsX, delegate {
                    stage.WriteLine(Properties.Strings.MovingComplete);
                });
#endif
                tracksItem[tracksManager.TrackingIndex].FontWeight = FontWeights.Bold;
            }
Example #7
0
        private void recogThread_Task()
        {
            Camera camera = Camera.GetInstance();

            camera.Start();
            MotorControler mc = MotorControler.GetInstance(parameterManager);

            surfaces[0] = UpTop;
            surfaces[1] = UpBottom;
            surfaces[2] = LowTop;
            surfaces[3] = LowBottom;

            // 現在位置が,前回の表面認識結果に基づいて下ゲル以下にいる場合は下ゲル下部から上方向に順に表面認識を行う.
            // 一方,ベース及び上ゲル以上にいる場合は上ゲル上部から下方向に順に表面認識を行う.
            // そのためにforループの条件式を設定する
            GelLayer layerNow = getWhichLayerNow();
            int      i, end;
            Action   fomula;

            if (layerNow == GelLayer.Under || layerNow == GelLayer.UnderOut)
            {
                i      = surfaces.Length;
                end    = 0;
                fomula = (() => -- i);
            }
            else
            {
                i      = 0;
                end    = surfaces.Length;
                fomula = (() => ++ i);
            }

            for (i = 0; i < end; fomula())
            {
                mc.MovePointZ(surfaces[i]);
                mc.Join();
                bool   presentResult = false, previousResult = IsInGel();
                bool[] votes   = new bool[parent.NumOfVoting];
                int    counter = 1;
                while (presentResult == previousResult)
                {
                    previousResult = presentResult;
                    mc.MovePointZ(surfaces[i] + everyRange * counter);
                    while (mc.Moving)
                    {
                        for (int j = 0; j < votes.Length; ++j)
                        {
                            votes[i] = IsInGel();
                            Thread.Sleep(parent.DelayTime);
                        }
                        presentResult = Surface.Vote(votes);
                        if (presentResult != previousResult)
                        {
                            mc.AbortMoving();
                        }
                    }
                    counter *= -1;
                    counter += (counter > 0 ? 1 : 0);
                    if (Math.Abs(everyRange * counter) > searchRange)
                    {
                        throw new SurfaceFailedException("Distance limit out.");
                    }
                }
                surfaces[i] = mc.GetPoint().Z;
                // 境界面認識時のイベントを発生させる.
                ActivityEventArgs eventArgs = new ActivityEventArgs();
                eventArgs.ZValue = surfaces[i];
                switch (i)
                {
                case 3:
                    if (LowBottomRecognized != null)
                    {
                        LowBottomRecognized(this, eventArgs);
                    }
                    break;

                case 2:
                    if (LowTopRecognized != null)
                    {
                        LowTopRecognized(this, eventArgs);
                    }
                    break;

                case 1:
                    if (UpBottomRecognized != null)
                    {
                        UpBottomRecognized(this, eventArgs);
                    }
                    break;

                case 0:
                    if (UpTopRecognized != null)
                    {
                        UpTopRecognized(this, eventArgs);
                    }
                    break;
                }
            }
            dateTime = DateTime.Now;
        }
Example #8
0
        protected virtual void KeyHandleWorkspace_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (!IsControllable)
            {
                WriteLine(Properties.Strings.NoControllable);
                return;
            }

            // アクティビティが実行中であれば中止する.
            Activity activity = new Activity(parameterManager);

            activity.Abort();

            // モータが移動中であれば移動を中止する.
            MotorControler mc = MotorControler.GetInstance(parameterManager);

            if (mc.IsMoving)
            {
                mc.AbortMoving();
                WriteLine(Properties.Strings.AbortMotor);
            }
            try {
#if !_NoHardWare
                if (e.Key == Key.A)
                {
                    mc.ContinuousDrive(VectorId.X, PlusMinus.Minus, mc.Speed.X);
                }
                else if (e.Key == Key.D)
                {
                    mc.ContinuousDrive(VectorId.X, PlusMinus.Plus, mc.Speed.X);
                }
                else if (e.Key == Key.X)
                {
                    mc.ContinuousDrive(VectorId.Y, PlusMinus.Minus, mc.Speed.Y);
                }
                else if (e.Key == Key.W)
                {
                    mc.ContinuousDrive(VectorId.Y, PlusMinus.Plus, mc.Speed.Y);
                }
                else if (e.Key == Key.Q)
                {
                    mc.ContinuousDrive(VectorId.Z, PlusMinus.Minus, mc.Speed.Z);
                }
                else if (e.Key == Key.E)
                {
                    mc.ContinuousDrive(VectorId.Z, PlusMinus.Plus, mc.Speed.Z);
                }
                else if (e.Key == Key.N)
                {
                    mc.MoveInSpiral();
                }
                else if (e.Key == Key.O)
                {
                    mc.SetSpiralCenterPoint();
                }
                else if (e.Key == Key.I)
                {
                    mc.BackToSpiralCenter();
                }
                else if (e.Key == Key.B)
                {
                    mc.SpiralBack();
                }
#endif
            } catch (MotorException ex) {
                WriteLine(ex.Message);
            }
        }
Example #9
0
        }//BeamDetection

        private void task()
        {
            MotorControler mc      = MotorControler.GetInstance(parameterManager);
            Camera         camera  = Camera.GetInstance();
            TracksManager  tm      = parameterManager.TracksManager;
            Track          myTrack = tm.GetTrack(tm.TrackingIndex);
            Surface        surface = Surface.GetInstance(parameterManager);
            int            mod     = parameterManager.ModuleNo;
            int            pl      = parameterManager.PlateNo;

            if (mc.IsMoving)
            {
                MessageBoxResult r = MessageBox.Show(
                    Properties.Strings.SurfaceException01,
                    Properties.Strings.Abort + "?",
                    MessageBoxButton.YesNo);
                if (r == MessageBoxResult.Yes)
                {
                    mc.AbortMoving();
                }
                else
                {
                    return;
                }
            }

            // すでに表面認識が実行中であれば停止するかどうか尋ねる.
            //Surface surface = Surface.GetInstance(parameterManager);
            if (surface.IsActive)
            {
                MessageBoxResult r = MessageBox.Show(
                    Properties.Strings.SurfaceException02,
                    Properties.Strings.Abort + "?",
                    MessageBoxButton.YesNo);
                if (r == MessageBoxResult.Yes)
                {
                    surface.Abort();
                }
                else
                {
                    return;
                }
            }

            try
            {
                surface.Start(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Properties.Strings.Error);
            }

            mc.Join();

            try
            {
                string datarootdirpath        = string.Format(@"C:\test\bpm\{0}", mod);
                System.IO.DirectoryInfo mydir = System.IO.Directory.CreateDirectory(datarootdirpath);
                string[] sp    = myTrack.IdString.Split('-');
                string   uptxt = string.Format(@"c:\test\bpm\{0}\{1}-{2}-{3}-{4}_up.txt", mod, mod, pl, sp[0], sp[1]);
                string   dwtxt = string.Format(@"c:\test\bpm\{0}\{1}-{2}-{3}-{4}_dw.txt", mod, mod, pl - 1, sp[0], sp[1]);
                BeamDetection(uptxt, true);

                BeamPatternMatch bpm = new BeamPatternMatch(8, 200);
                bpm.ReadTrackDataTxtFile(dwtxt, false);

                bpm.ReadTrackDataTxtFile(uptxt, true);
                bpm.DoPatternMatch();

                stage.WriteLine(String.Format("pattern match dx,dy = {0}, {1}", bpm.GetPeakX() * 0.2625 * 0.001, bpm.GetPeakY() * 0.2625 * 0.001));
                Vector3 BfPoint = mc.GetPoint();
                mc.MoveDistance(bpm.GetPeakX() * 0.2625 * 0.001, VectorId.X);
                mc.Join();
                mc.MoveDistance(-bpm.GetPeakY() * 0.2625 * 0.001, VectorId.Y);
                mc.Join();
                Led led = Led.GetInstance();
                led.AdjustLight(parameterManager);
                Vector3 AfPoint = mc.GetPoint();
                stage.WriteLine(String.Format("Move dx,dy = {0}, {1}", BfPoint.X - AfPoint.X, BfPoint.Y - AfPoint.Y));
            }
            catch (ArgumentOutOfRangeException)
            {
                MessageBox.Show("ID numver is not existed。 ");
            }
            catch (System.Exception)
            {
                MessageBox.Show("No beam battern。 ");
            }
        }
Example #10
0
        private void task()
        {
            // モータが稼働中であれば停止するかどうかを尋ねる.
            int mod = parameterManager.ModuleNo;
            int pl  = parameterManager.PlateNo;
            //MotorControler mc = MotorControler.GetInstance(parameterManager);
            Camera        camera  = Camera.GetInstance();
            TracksManager tm      = parameterManager.TracksManager;
            Track         myTrack = tm.GetTrack(tm.TrackingIndex);
            Stopwatch     swsurf  = new Stopwatch();

            // Stopwatch Time = new Stopwatch();
            swsurf.Start();
            MotorControler mc = MotorControler.GetInstance(parameterManager);

            if (mc.IsMoving)
            {
                MessageBoxResult r = MessageBox.Show(
                    Properties.Strings.SurfaceException01,
                    Properties.Strings.Abort + "?",
                    MessageBoxButton.YesNo);
                if (r == MessageBoxResult.Yes)
                {
                    mc.AbortMoving();
                }
                else
                {
                    return;
                }
            }

            // すでに表面認識が実行中であれば停止するかどうか尋ねる.
            Surface surface = Surface.GetInstance(parameterManager);

            if (surface.IsActive)
            {
                MessageBoxResult r = MessageBox.Show(
                    Properties.Strings.SurfaceException02,
                    Properties.Strings.Abort + "?",
                    MessageBoxButton.YesNo);
                if (r == MessageBoxResult.Yes)
                {
                    surface.Abort();
                }
                else
                {
                    return;
                }
            }

            try
            {
                surface.Start(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Properties.Strings.Error);
            }
            double Time = surface.Time;

            mc.Join();
            swsurf.Stop();
            string[] spsurf  = myTrack.IdString.Split('-');
            string   logtxt_ = string.Format(@"c:\test\bpm\{0}\{1}-{2}_surftime.txt", mod, mod, pl);
            //string log_ = string.Format("{0} \n", sw.Elapsed);
            string       surftime = string.Format("{0} {1} {2} {3} \n", spsurf[0], spsurf[1], swsurf.Elapsed, Time);
            StreamWriter swr      = new StreamWriter(logtxt_, true, Encoding.ASCII);

            swr.Write(surftime);
            swr.Close();
        }
Example #11
0
        }//BeamDetection

        private void task()
        {
            MotorControler mc           = MotorControler.GetInstance(parameterManager);
            Camera         camera       = Camera.GetInstance();
            TracksManager  tm           = parameterManager.TracksManager;
            Track          myTrack      = tm.GetTrack(tm.TrackingIndex);
            Surface        surface      = Surface.GetInstance(parameterManager);
            Stopwatch      sw           = new Stopwatch();
            CoordManager   coordManager = new CoordManager(parameterManager);


            //CoordManager coordManager;
            int mod = parameterManager.ModuleNo;
            int pl  = parameterManager.PlateNo;

            sw.Start();

            try
            {
                //MotorControler mc = MotorControler.GetInstance(parameterManager);
                GridMark nearestMark = coordManager.GetTheNearestGridMark(mc.GetPoint());
                System.Diagnostics.Debug.WriteLine(String.Format("{0},  {1}", nearestMark.x, nearestMark.y));
                mc.MovePointXY(nearestMark.x, nearestMark.y);
                mc.Join();
            }
            catch (GridMarkNotFoundException ex)
            {
                System.Diagnostics.Debug.WriteLine(String.Format("{0}", ex.ToString()));
            }
            try
            {
                // MotorControler mc = MotorControler.GetInstance(parameterManager);
                IGridMarkRecognizer GridMarkRecognizer = coordManager;
                mc.SetSpiralCenterPoint();
                Led     led          = Led.GetInstance();
                Vector2 encoderPoint = new Vector2(-1, -1);
                encoderPoint.X = mc.GetPoint().X;
                encoderPoint.Y = mc.GetPoint().Y;//おこられたのでしかたなくこうする 吉田20150427
                Vector2 viewerPoint = new Vector2(-1, -1);

                bool continueFlag = true;
                while (continueFlag)
                {
                    led.AdjustLight(parameterManager);
                    viewerPoint = GridMarkRecognizer.SearchGridMarkx50();
                    if (viewerPoint.X < 0 || viewerPoint.Y < 0)
                    {
                        System.Diagnostics.Debug.WriteLine(String.Format("grid mark not found"));
                        mc.MoveInSpiral(true);
                        mc.Join();
                        continueFlag = (mc.SpiralIndex < 30);
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine(String.Format("******** {0}  {1}", viewerPoint.X, viewerPoint.Y));
                        encoderPoint = coordManager.TransToEmulsionCoord(viewerPoint);
                        mc.MovePointXY(encoderPoint);
                        mc.Join();
                        continueFlag = false;
                    }
                } // while

                //重心検出と移動を2回繰り返して、グリッドマークを視野中心にもっていく
                mc.MovePointXY(encoderPoint);
                mc.Join();
                viewerPoint  = GridMarkRecognizer.SearchGridMarkx50();
                encoderPoint = coordManager.TransToEmulsionCoord(viewerPoint);
                mc.MovePointXY(encoderPoint);
                mc.Join();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("exception");
            }

            //.........Get value of HFDX and HFDY................//

            try
            {
                Vector3  CurrentCenterPoint = mc.GetPoint();
                GridMark nearestMark        = coordManager.GetTheNearestGridMark(CurrentCenterPoint);
                System.Diagnostics.Debug.WriteLine(String.Format("{0},  {1}", CurrentCenterPoint.X, CurrentCenterPoint.Y));
                coordManager.HFDX = CurrentCenterPoint.X - nearestMark.x;
                coordManager.HFDY = CurrentCenterPoint.Y - nearestMark.y;
            }
            catch (EntryPointNotFoundException ex)
            {
                MessageBox.Show("エントリポイントが見当たりません。 " + ex.Message);
                System.Diagnostics.Debug.WriteLine("エントリポイントが見当たりません。 " + ex.Message);
            }

            //....... Move to track place after revised with shift value.....//

            double dstx = myTrack.MsX + coordManager.HFDX;
            double dsty = myTrack.MsY + coordManager.HFDY;

            mc.MovePointXY(dstx, dsty, delegate {
                //stage.WriteLine(Properties.Strings.MovingComplete);
            });

            mc.Join();

            Led led_ = Led.GetInstance();

            led_.AdjustLight(parameterManager);
            Thread.Sleep(500); //Wait for 5s

            //////////////////////////////Surfacerecog/////////////////////////////////////.................................../////////

            try
            {
                if (mc.IsMoving)
                {
                    MessageBoxResult r = MessageBox.Show(
                        Properties.Strings.SurfaceException01,
                        Properties.Strings.Abort + "?",
                        MessageBoxButton.YesNo);
                    if (r == MessageBoxResult.Yes)
                    {
                        mc.AbortMoving();
                    }
                    else
                    {
                        return;
                    }
                }

                //Surface surface = Surface.GetInstance(parameterManager);
                if (surface.IsActive)
                {
                    MessageBoxResult r = MessageBox.Show(
                        Properties.Strings.SurfaceException02,
                        Properties.Strings.Abort + "?",
                        MessageBoxButton.YesNo);
                    if (r == MessageBoxResult.Yes)
                    {
                        surface.Abort();
                    }
                    else
                    {
                        return;
                    }
                }

                try
                {
                    surface.Start(true);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, Properties.Strings.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Properties.Strings.Error);
            }
            mc.Join();

            //...............Beam Pattern Matching..................////////////////

            try
            {
                string datarootdirpath        = string.Format(@"C:\test\bpm\{0}", mod);
                System.IO.DirectoryInfo mydir = System.IO.Directory.CreateDirectory(datarootdirpath);
                string[] sp    = myTrack.IdString.Split('-');
                string   uptxt = string.Format(@"c:\test\bpm\{0}\{1}-{2}-{3}-{4}_up.txt", mod, mod, pl, sp[0], sp[1]);
                string   dwtxt = string.Format(@"c:\test\bpm\{0}\{1}-{2}-{3}-{4}_dw.txt", mod, mod, pl - 1, sp[0], sp[1]);
                BeamDetection(uptxt, true);

                BeamPatternMatch bpm = new BeamPatternMatch(8, 200);
                bpm.ReadTrackDataTxtFile(dwtxt, false);

                bpm.ReadTrackDataTxtFile(uptxt, true);
                bpm.DoPatternMatch();

                //stage.WriteLine(String.Format("pattern match dx,dy = {0}, {1}", bpm.GetPeakX() * 0.2625 * 0.001, bpm.GetPeakY() * 0.2625 * 0.001));
                Vector3 BfPoint = mc.GetPoint();
                mc.MoveDistance(bpm.GetPeakX() * 0.2625 * 0.001, VectorId.X);
                mc.Join();
                mc.MoveDistance(-bpm.GetPeakY() * 0.2625 * 0.001, VectorId.Y);
                mc.Join();
                Led led = Led.GetInstance();
                led.AdjustLight(parameterManager);
                Vector3 AfPoint = mc.GetPoint();
                //stage.WriteLine(String.Format("Move dx,dy = {0}, {1}", BfPoint.X - AfPoint.X, BfPoint.Y - AfPoint.Y));
            }
            catch (ArgumentOutOfRangeException)
            {
                MessageBox.Show("ID numver is not existed。 ");
            }
            catch (System.Exception)
            {
                MessageBox.Show("No beam battern。 ");
            }
            // }
            /////////////////////////////SurfaceRecog////////////////////////////////////////////////////////////////////////////

            Vector3 initial = mc.GetPoint();///////initialpoint

            double Sh = 0.5 / (surface.UpTop - surface.UpBottom);

            //ここから角度によって撮影間隔を変更するように書き換える。
            double tansi = Math.Sqrt(myTrack.MsDX * myTrack.MsDX + myTrack.MsDY * myTrack.MsDY);
            double theta = Math.Atan(tansi);
            //絶対値の大きさを入れる。dzはマイナスの値になるようにする。
            double dz;

            double dz_price_img = (6 * Math.Cos(theta) / Sh) / 1000;
            double dz_img       = dz_price_img * (-1);
            //
            string datarootdirpathw = string.Format(@"C:\test\{0}", myTrack.IdString);

            System.IO.DirectoryInfo mydir_ = System.IO.Directory.CreateDirectory(datarootdirpathw);
            //

            //必要なlistをまとめる
            //List<ImageTaking> LiITUpTrack = new List<ImageTaking>();

            List <OpenCvSharp.CPlusPlus.Point2d> Msdxdy = new List <OpenCvSharp.CPlusPlus.Point2d>();

            Msdxdy.Add(new OpenCvSharp.CPlusPlus.Point2d(myTrack.MsDX, myTrack.MsDY));

            List <OpenCvSharp.CPlusPlus.Point3d> LStage = new List <OpenCvSharp.CPlusPlus.Point3d>();
            List <OpenCvSharp.CPlusPlus.Point>   LPeak  = new List <OpenCvSharp.CPlusPlus.Point>(); //i番目の画像で実際に見つかったトラックの座標。pixel座標で視野中心からの差分。
            List <Point3d> LTrack = new List <Point3d>();                                           //i番目の画像で実際に見つかったトラックの座標のステージ座標

            List <List <ImageTaking> > UpTrackInfo = new List <List <ImageTaking> >();

            //エラー防止のために下ゲルの処理の際に必要なListをここに移動した。
            double Sh_low;

            Sh_low = 0.5 / (surface.LowTop - surface.LowBottom);

            List <OpenCvSharp.CPlusPlus.Point2d> Msdxdy_Low = new List <OpenCvSharp.CPlusPlus.Point2d>();

            //List<ImageTaking> LiITLowMid = new List<ImageTaking>();
            List <OpenCvSharp.CPlusPlus.Point3d> LStage_Low = new List <OpenCvSharp.CPlusPlus.Point3d>();
            List <OpenCvSharp.CPlusPlus.Point>   LPeak_Low  = new List <OpenCvSharp.CPlusPlus.Point>(); //i番目の画像で実際に見つかったトラックの座標。pixel座標で視野中心からの差分。
            List <Point3d>             LTrack_Low           = new List <Point3d>();                     //i番目の画像で実際に見つかったトラックの座標のステージ座標
            List <List <ImageTaking> > LowTrackInfo         = new List <List <ImageTaking> >();


            //今までのtrack追跡プログラムとは異なる角度等の使い方をする。
            dz_price_img = (6 * Math.Cos(theta) / Sh) / 1000;
            dz_img       = dz_price_img * (-1);
            dz           = dz_img;

            int gotobase   = 0;
            int not_detect = 0;

            for (int i = 0; gotobase < 1; i++)
            {
                ///////移動して画像処理をしたときに、baseの中に入らないようにする。
                Vector3 initialpos = mc.GetPoint();
                double  moverange  = 7 * dz_img;
                double  predpoint  = moverange + initialpos.Z;

                if (predpoint < surface.UpBottom)//もしもbaseに入りそうなら、8枚目の画像がちょうどbaseを撮影するようにdzを調整する。
                {
                    gotobase = 1;

                    dz = surface.UpBottom - initialpos.Z + 7 * dz_price_img;
                }
                ////////

                //gotobase = 1のときは、移動して画像を撮影するようにする。
                if (i != 0)
                {
                    Vector3 dstpoint = new Vector3(
                        LTrack[i - 1].X + Msdxdy[i].X * dz * Sh,
                        LTrack[i - 1].Y + Msdxdy[i].Y * dz * Sh,
                        LTrack[i - 1].Z + dz
                        );
                    mc.MovePoint(dstpoint);
                    mc.Join();
                }

                List <ImageTaking> LiITUpMid = TakeSequentialImage(
                    Msdxdy[i].X * Sh,
                    Msdxdy[i].Y * Sh,
                    dz_img,
                    8);

                ////画像の保存、座標の保存。
                LStage.Add(new OpenCvSharp.CPlusPlus.Point3d(LiITUpMid[7].StageCoord.X, LiITUpMid[7].StageCoord.Y, LiITUpMid[7].StageCoord.Z));
                LiITUpMid[7].img.ImWrite(datarootdirpathw + string.Format(@"\img_l_up_{0}.bmp", i));

                UpTrackInfo.Add(LiITUpMid);//撮影した8枚の画像と、撮影した位置を記録する。

                //撮影した画像をここで処理する。
                List <Mat> binimages = new List <Mat>();
                for (int t = 0; t <= 7; t++)
                {
                    Mat bin = (Mat)DogContrastBinalize(LiITUpMid[t].img);

                    double xx = myTrack.MsDX * myTrack.MsDX;
                    double yy = myTrack.MsDY * myTrack.MsDY;
                    if (Math.Sqrt(xx + yy) >= 0.4)
                    {
                        Cv2.Dilate(bin, bin, new Mat());
                    }
                    Cv2.Dilate(bin, bin, new Mat());
                    binimages.Add(bin);
                }

                //trackを重ねる処理を入れる。
                Point2d pixel_cen = TrackDetection(binimages, 256, 220, 3, 3, 4, 90, 3);//画像の8枚目におけるtrackのpixel座標を算出する。

                if (pixel_cen.X == -1 & pixel_cen.Y == -1)
                {
                    //追跡に失敗した時に最後に検出したtrack座標に移動してから、追跡に失敗した地点の画像を撮影するようにする。
                    //mc.MovePoint(LTrack[i - 1].X, LTrack[i - 1].Y, LTrack[i - 1].Z);
                    mc.Join();

                    not_detect = 1;
                    goto not_detect_track;
                }

                //検出したpixel座標をstage座標に変換するなどlistに追加する。

                LPeak.Add(new OpenCvSharp.CPlusPlus.Point(pixel_cen.X - 256, pixel_cen.Y - 220));

                double firstx = LStage[i].X - LPeak[i].X * 0.000267;
                double firsty = LStage[i].Y + LPeak[i].Y * 0.000267;
                double firstz = LStage[i].Z;
                LTrack.Add(new Point3d(firstx, firsty, firstz));
                //

                //上側乳剤層上面の1回目のtrack検出によって、次のtrackの位置を検出する角度を求める。
                //その角度が、1回目のtrack検出の結果によって大きな角度にならないように調整をする。

                if (i == 0)
                {
                    Msdxdy.Add(new OpenCvSharp.CPlusPlus.Point2d(myTrack.MsDX, myTrack.MsDY));
                }
                else if (i == 1)
                {
                    List <Point3d> LTrack_ghost = new List <Point3d>();
                    double         dzPrev       = (LStage[0].Z - surface.UpTop) * Sh;
                    double         Lghost_x     = LTrack[0].X - Msdxdy[i].X * dzPrev;
                    double         Lghost_y     = LTrack[0].Y - Msdxdy[i].Y * dzPrev;
                    LTrack_ghost.Add(new Point3d(Lghost_x, Lghost_y, surface.UpTop));//上側乳剤層上面にtrackがあるならどの位置にあるかを算出する。

                    string       txtfileName_ltrackghost = datarootdirpathw + string.Format(@"\LTrack_ghost.txt");
                    StreamWriter twriter_ltrackghost     = File.CreateText(txtfileName_ltrackghost);
                    twriter_ltrackghost.WriteLine("{0} {1} {2}", LTrack_ghost[0].X, LTrack_ghost[0].Y, LTrack_ghost[0].Z);
                    twriter_ltrackghost.Close();

                    OpenCvSharp.CPlusPlus.Point2d Tangle = ApproximateStraight(Sh, LTrack_ghost[0], LTrack[0], LTrack[1]);
                    Msdxdy.Add(new OpenCvSharp.CPlusPlus.Point2d(Tangle.X, Tangle.Y));
                }
                else
                {
                    OpenCvSharp.CPlusPlus.Point2d Tangle = ApproximateStraight(Sh, LTrack[i - 2], LTrack[i - 1], LTrack[i]);
                    Msdxdy.Add(new OpenCvSharp.CPlusPlus.Point2d(Tangle.X, Tangle.Y));
                }
            }//for i-loop

            //baseまたぎ
            int ltrack_counter = LTrack.Count();
            int msdxdy_counter = Msdxdy.Count();

            mc.MovePoint(
                LTrack[ltrack_counter - 1].X + Msdxdy[msdxdy_counter - 1].X * (surface.LowTop - surface.UpBottom),
                LTrack[ltrack_counter - 1].Y + Msdxdy[msdxdy_counter - 1].Y * (surface.LowTop - surface.UpBottom),
                surface.LowTop
                );
            mc.Join();

            //////ここから下gelの処理
            Msdxdy_Low.Add(new OpenCvSharp.CPlusPlus.Point2d(Msdxdy[msdxdy_counter - 1].X, Msdxdy[msdxdy_counter - 1].Y));

            //今までのtrack追跡プログラムとは異なる角度等の使い方をする。
            dz_price_img = (6 * Math.Cos(theta) / Sh_low) / 1000;
            dz_img       = dz_price_img * (-1);
            dz           = dz_img;

            int goto_dgel = 0;

            for (int i = 0; goto_dgel < 1; i++)
            {
                ///////移動して画像処理をしたときに、下gelの下に入らないようにする。
                Vector3 initialpos = mc.GetPoint();
                double  moverange  = 7 * dz_img;
                double  predpoint  = moverange + initialpos.Z;

                if (predpoint < surface.LowBottom)//もしもbaseに入りそうなら、8枚目の画像がちょうど下gelを撮影するようにdzを調整する。
                {
                    goto_dgel = 1;

                    dz = surface.LowBottom - initialpos.Z + 7 * dz_price_img;
                }
                ////////

                //goto_dgel == 1のときは、移動して画像を撮影するようにする。
                if (i != 0)
                {
                    Vector3 dstpoint = new Vector3(
                        LTrack_Low[i - 1].X + Msdxdy_Low[i].X * dz * Sh_low,
                        LTrack_Low[i - 1].Y + Msdxdy_Low[i].Y * dz * Sh_low,
                        LTrack_Low[i - 1].Z + dz
                        );
                    mc.MovePoint(dstpoint);
                    mc.Join();
                }

                //今までのtrack追跡プログラムとは異なる角度等の使い方をする。
                List <ImageTaking> LiITLowMid = TakeSequentialImage(
                    Msdxdy[i].X * Sh_low,
                    Msdxdy[i].Y * Sh_low,
                    dz_img,
                    8);

                //画像・座標の記録
                LStage_Low.Add(new OpenCvSharp.CPlusPlus.Point3d(LiITLowMid[7].StageCoord.X, LiITLowMid[7].StageCoord.Y, LiITLowMid[7].StageCoord.Z));
                LiITLowMid[7].img.ImWrite(datarootdirpathw + string.Format(@"\img_l_low_{0}.bmp", i));

                LowTrackInfo.Add(LiITLowMid);//撮影した8枚の画像と、撮影した位置を記録する。

                //撮影した画像をここで処理する。
                List <Mat> binimages = new List <Mat>();
                for (int t = 0; t <= 7; t++)
                {
                    Mat bin = (Mat)DogContrastBinalize(LiITLowMid[t].img);

                    double xx = myTrack.MsDX * myTrack.MsDX;
                    double yy = myTrack.MsDY * myTrack.MsDY;
                    if (Math.Sqrt(xx + yy) >= 0.4)
                    {
                        Cv2.Dilate(bin, bin, new Mat());
                    }
                    Cv2.Dilate(bin, bin, new Mat());
                    binimages.Add(bin);
                }
                //trackを重ねる処理を入れる。
                Point2d pixel_cen = TrackDetection(binimages, 256, 220, 3, 3, 4, 90, 3);//画像の8枚目におけるtrackのpixel座標を算出する。

                //もし検出に失敗した場合はループを抜ける。
                if (pixel_cen.X == -1 & pixel_cen.Y == -1)
                {
                    //mc.MovePoint(LTrack_Low[i - 1].X, LTrack_Low[i - 1].Y, LTrack_Low[i - 1].Z);
                    mc.Join();

                    not_detect = 1;
                    goto not_detect_track;
                }
                //

                //検出したpixel座標をstage座標に変換するなどlistに追加する。
                LPeak_Low.Add(new OpenCvSharp.CPlusPlus.Point(pixel_cen.X - 256, pixel_cen.Y - 220));

                double firstx = LStage_Low[i].X - LPeak_Low[i].X * 0.000267;
                double firsty = LStage_Low[i].Y + LPeak_Low[i].Y * 0.000267;
                double firstz = LStage_Low[i].Z;
                LTrack_Low.Add(new Point3d(firstx, firsty, firstz));
                //

                //ここからは、最小二乗法で角度を算出するプログラムである。
                //上側乳剤層上面の1回目のtrack検出によって、次のtrackの位置を検出する角度を求める。
                //その角度が、1回目のtrack検出の結果によって大きな角度にならないように調整をする。
                if (i == 0)
                {
                    OpenCvSharp.CPlusPlus.Point2d Tangle_l = ApproximateStraightBase(Sh, Sh_low, LTrack[ltrack_counter - 2], LTrack[ltrack_counter - 1], LTrack_Low[i], surface);
                    Msdxdy_Low.Add(new OpenCvSharp.CPlusPlus.Point2d(Tangle_l.X, Tangle_l.Y));
                }
                else if (i == 1)
                {
                    OpenCvSharp.CPlusPlus.Point2d Tangle_l = ApproximateStraightBase(Sh, Sh_low, LTrack[ltrack_counter - 1], LTrack_Low[i - 1], LTrack_Low[i], surface);
                    Msdxdy_Low.Add(new OpenCvSharp.CPlusPlus.Point2d(Tangle_l.X, Tangle_l.Y));
                }
                else
                {
                    OpenCvSharp.CPlusPlus.Point2d Tangle_l = ApproximateStraight(Sh_low, LTrack_Low[i - 2], LTrack_Low[i - 1], LTrack_Low[i]);
                    Msdxdy_Low.Add(new OpenCvSharp.CPlusPlus.Point2d(Tangle_l.X, Tangle_l.Y));
                }
            }//i_loop

            //
            int ltrack_low_count = LTrack_Low.Count();

            mc.MovePointXY(LTrack_Low[ltrack_low_count - 1].X, LTrack_Low[ltrack_low_count - 1].Y);

            mc.Join();

            //検出に失敗した場合は、ループを抜けてここに来る。
            not_detect_track :;//検出に失敗したと考えられる地点で画像を取得し、下ゲル下面まで移動する。(現在は下ゲル下面とするが、今後変更する可能性有。)

            if (not_detect != 0)
            {
                //写真撮影
                List <ImageTaking> NotDetect = TakeSequentialImage(
                    Msdxdy[0].X * Sh,
                    Msdxdy[0].Y * Sh,
                    0,
                    1);

                string       txtfileName_t_not_detect = datarootdirpathw + string.Format(@"\not_detect.txt");
                StreamWriter twriter_t_not_detect     = File.CreateText(txtfileName_t_not_detect);
                for (int i = 0; i < NotDetect.Count; i++)
                {
                    NotDetect[i].img.ImWrite(datarootdirpathw + string.Format(@"\img_t_not_detect.bmp"));
                    Vector3 p = NotDetect[i].StageCoord;
                    twriter_t_not_detect.WriteLine("{0} {1} {2} {3}", i, p.X, p.Y, p.Z);
                }
                twriter_t_not_detect.Close();

                mc.MovePointZ(surface.LowBottom);

                mc.Join();
            }


            //file write out up_gel
            string       txtfileName_sh_up = datarootdirpathw + string.Format(@"\Sh_up.txt");
            StreamWriter twriter_sh_up     = File.CreateText(txtfileName_sh_up);

            twriter_sh_up.WriteLine("{0}", Sh);
            twriter_sh_up.Close();

            //file write out
            string       txtfileName_t_info_up = datarootdirpathw + string.Format(@"\location_up.txt");
            StreamWriter twriter_t_info_up     = File.CreateText(txtfileName_t_info_up);

            for (int i = 0; i < UpTrackInfo.Count; i++)
            {
                for (int t = 0; t < UpTrackInfo[i].Count; t++)
                {
                    UpTrackInfo[i][t].img.ImWrite(datarootdirpathw + string.Format(@"\img_t_info_up_{0}-{1}.bmp", i, t));
                    Vector3 p = UpTrackInfo[i][t].StageCoord;
                    twriter_t_info_up.WriteLine("{0} {1} {2} {3} {4}", i, t, p.X, p.Y, p.Z);
                }
            }
            twriter_t_info_up.Close();

            string       txtfileName_lpeak = datarootdirpathw + string.Format(@"\lpeak_up.txt");
            StreamWriter twriter_lpeak     = File.CreateText(txtfileName_lpeak);

            for (int i = 0; i < LPeak.Count(); i++)
            {
                OpenCvSharp.CPlusPlus.Point p = LPeak[i];
                twriter_lpeak.WriteLine("{0} {1} {2}", i, p.X, p.Y);
            }
            twriter_lpeak.Close();

            string       txtfileName_ltrack = datarootdirpathw + string.Format(@"\ltrack_up.txt");
            StreamWriter twriter_ltrack     = File.CreateText(txtfileName_ltrack);

            for (int i = 0; i < LTrack.Count(); i++)
            {
                OpenCvSharp.CPlusPlus.Point3d p = LTrack[i];
                twriter_ltrack.WriteLine("{0} {1} {2} {3}", i, p.X, p.Y, p.Z);
            }
            twriter_ltrack.Close();

            string       txtfileName_msdxdy = datarootdirpathw + string.Format(@"\msdxdy.txt");
            StreamWriter twriter_msdxdy     = File.CreateText(txtfileName_msdxdy);

            for (int i = 0; i < Msdxdy.Count(); i++)
            {
                OpenCvSharp.CPlusPlus.Point2d p = Msdxdy[i];
                twriter_msdxdy.WriteLine("{0} {1} {2}", i, p.X, p.Y);
            }
            twriter_msdxdy.Close();


            //file write out low_gel
            string       txtfileName_sh_low = datarootdirpathw + string.Format(@"\Sh_low.txt");
            StreamWriter twriter_sh_low     = File.CreateText(txtfileName_sh_low);

            twriter_sh_low.WriteLine("{0}", Sh_low);
            twriter_sh_low.Close();

            string       txtfileName_t_info_low = datarootdirpathw + string.Format(@"\location_low.txt");
            StreamWriter twriter_t_info_low     = File.CreateText(txtfileName_t_info_low);

            for (int i = 0; i < LowTrackInfo.Count; i++)
            {
                for (int t = 0; t < LowTrackInfo[i].Count; t++)
                {
                    LowTrackInfo[i][t].img.ImWrite(datarootdirpathw + string.Format(@"\img_t_info_low_{0}-{1}.bmp", i, t));
                    Vector3 p = LowTrackInfo[i][t].StageCoord;
                    twriter_t_info_low.WriteLine("{0} {1} {2} {3} {4}", i, t, p.X, p.Y, p.Z);
                }
            }
            twriter_t_info_low.Close();

            string       txtfileName_lpeak_low = datarootdirpathw + string.Format(@"\lpeak_low.txt");
            StreamWriter twriter_lpeak_low     = File.CreateText(txtfileName_lpeak_low);

            for (int i = 0; i < LPeak_Low.Count(); i++)
            {
                OpenCvSharp.CPlusPlus.Point p = LPeak_Low[i];
                twriter_lpeak_low.WriteLine("{0} {1} {2}", i, p.X, p.Y);
            }
            twriter_lpeak_low.Close();

            string       txtfileName_ltrack_low = datarootdirpathw + string.Format(@"\ltrack_low.txt");
            StreamWriter twriter_ltrack_low     = File.CreateText(txtfileName_ltrack_low);

            for (int i = 0; i < LTrack_Low.Count(); i++)
            {
                OpenCvSharp.CPlusPlus.Point3d p = LTrack_Low[i];
                twriter_ltrack_low.WriteLine("{0} {1} {2} {3}", i, p.X, p.Y, p.Z);
            }
            twriter_ltrack_low.Close();

            string       txtfileName_msdxdy_low = datarootdirpathw + string.Format(@"\msdxdy_low.txt");
            StreamWriter twriter_msdxdy_low     = File.CreateText(txtfileName_msdxdy_low);

            for (int i = 0; i < Msdxdy_Low.Count(); i++)
            {
                OpenCvSharp.CPlusPlus.Point2d p = Msdxdy_Low[i];
                twriter_msdxdy_low.WriteLine("{0} {1} {2}", i, p.X, p.Y);
            }
            twriter_msdxdy_low.Close();//*/

            //.................Taking Photo in buttom of down layer.......................//

            try
            {
                string[] sp                    = myTrack.IdString.Split('-');
                string   dwtxt                 = string.Format(@"c:\test\bpm\{0}\{1}-{2}-{3}-{4}_dw.txt", mod, mod, pl, sp[0], sp[1]);
                string   datarootdirpath       = string.Format(@"C:\test\bpm\{0}", mod);
                System.IO.DirectoryInfo mydir  = System.IO.Directory.CreateDirectory(datarootdirpath);
                System.IO.DirectoryInfo mydir2 = System.IO.Directory.CreateDirectory(datarootdirpath);
                BeamDetection(dwtxt, false);
            }
            catch (ArgumentOutOfRangeException)
            {
                MessageBox.Show("ID is not exist ");
            }

            //...............Move to top surfacr of upper layer........................//
            try
            {
                Vector3 cc = mc.GetPoint();
                double  Zp = surface.UpTop;
                mc.MoveTo(new Vector3(cc.X, cc.Y, Zp));
                mc.Join();
            }
            catch (ArgumentOutOfRangeException)
            {
                MessageBox.Show("Cannot move to top surface of upperlayer ");
            }

            // .......................................................................//

            sw.Stop();
            string[] sp1     = myTrack.IdString.Split('-');
            string   logtxt_ = string.Format(@"c:\test\bpm\{0}\{1}-{2}_log_.txt", mod, mod, pl);
            //string log_ = string.Format("{0} \n", sw.Elapsed);
            string       log_ = string.Format("{0} {1} {2} \n", sp1[0], sp1[1], sw.Elapsed);
            StreamWriter swr  = new StreamWriter(logtxt_, true, Encoding.ASCII);

            swr.Write(log_);
            swr.Close();
        }
Example #12
0
            /// <summary>
            /// allTracksButtonのドロップダウンメニューにトラックの一覧を追加して,初期化します.
            /// </summary>
            public void InitializeTracksItemMenu()
            {
                TracksManager tracksManager = parameterManager.TracksManager;

                // トラックデータが読み込まれていないなど,TracksManagerが初期化されていない場合は終了する.
                if (!tracksManager.IsInitialized)
                {
                    nextTrackButton.IsEnabled = false;
                    allTracksButton.IsEnabled = false;
                    return;
                }

                // トラックが存在しない場合はnextTrackButton及びallTracksButtonを無効にする.
                // トラックが一つのみの場合はallTracksButtonのみを無効にする.
                if (tracksManager.NumOfTracks == 0)
                {
                    nextTrackButton.IsEnabled = false;
                }
                if (tracksManager.NumOfTracks <= 1)
                {
                    allTracksButton.IsEnabled = false;
                }

                // allTracksButtonのドロップダウンメニューの中身を初期化する
                tracksItem = new ComboBoxItem[tracksManager.NumOfTracks];
                for (int i = 0; i < tracksItem.Length; ++i)
                {
                    Track myTrack = tracksManager.GetTrack(i);
                    tracksItem[i]                     = new ComboBoxItem();
                    tracksItem[i].Content             = myTrack.IdString;
                    tracksItem[i].HorizontalAlignment = HorizontalAlignment.Left;
                    tracksItem[i].Selected           += delegate(object o, RoutedEventArgs e2) {
                        /* ComboBoxItemのClickイベントハンドラを追加 */
                        // 該当トラックの座標までモータ移動させる.
                        // ただし,モータが移動中の場合はその移動を中止させるかをユーザーに尋ねる.
                        MotorControler mc = MotorControler.GetInstance(parameterManager);
                        if (mc.IsMoving)
                        {
                            if (askAbortMotorMoving())
                            {
                                mc.AbortMoving();
                                if (stage != null)
                                {
                                    stage.WriteLine(Properties.Strings.MotorStop01);
                                }
                            }
                            else
                            {
                                // ユーザーがモータ動作を中止しない場合は終了
                                return;
                            }
                        }

                        // モータの移動動作
                        stage.WriteLine(Properties.Strings.Moving);
#if !NoHardware
                        double dstx = myTrack.MsX + window.CoordManager.HFDX;
                        double dsty = myTrack.MsY + window.CoordManager.HFDY;
                        mc.MovePointXY(dstx, dsty, delegate {
                            stage.WriteLine(Properties.Strings.MovingComplete);
                        });
#endif
                        // 追跡中のトラック番号を更新
                        tracksManager.TrackingIndex = myTrack.Index;

                        // 移動済みのメニュー項目は太字にするようにセット
                        ComboBoxItem thisItem = o as ComboBoxItem;
                        thisItem.FontWeight = FontWeights.Bold;
                        UpdateTrackInfo();
                    };
                    allTracksButton.Items.Add(tracksItem[i]);
                }
            }