예제 #1
0
        /// <summary>
        /// recogThreadの行う処理です.このメソッドを直接呼び出さないでください.
        /// </summary>
        private void recogThread_Task()
        {
            App.logger.Info("Surface.recogThread_Task() start");

            if (Started != null)
            {
                Started(this, new EventArgs());
            }

            Camera camera = Camera.GetInstance();

            camera.Start();

            /* Z軸をSpeed1(低速)でマイナス方向に動かして,最下点もしくは下降距離分だけ移動させる.*/
            MotorControler mc = MotorControler.GetInstance(parameterManager);

            speedBeforeStart = mc.Speed;


            bool   flag;
            double temp_z;

            mc.Inch(PlusMinus.Minus, motorSpeed, VectorId.Z);
            flag = true;
            while (flag)
            {
                int brightness = CountHitPixels();
                if (brightness > 8000)
                {
                    flag = false;
                }
                //if (brightness < 10000) flag = false;//
            }

            /*
             *          UpTopRecognized(this, new eventArgs());
             * LowBottomRecognized(this, new eventArgs());
             *          LowTopRecognized(this, new eventArgs());
             *          UpBottomRecognized(this, new eventArgs());
             */
            mc.SlowDownStop(VectorId.Z);
            surfaces[0] = mc.GetPoint().Z;
            System.Diagnostics.Debug.WriteLine(string.Format("{0}", mc.GetPoint().Z));
            Thread.Sleep(100);



            mc.Move(new Vector3(0.0, 0.0, -0.2));
            mc.Join();
            Thread.Sleep(100);


            mc.Inch(PlusMinus.Minus, motorSpeed, VectorId.Z);
            flag = true;
            while (flag)
            {
                int brightness = CountHitPixels();
                if (brightness < 5000)
                {
                    flag = false;                   //
                }
                // if (brightness < 4000) flag = false;
            }


            surfaces[1] = mc.GetPoint().Z;//
            // surfaces[2] = mc.GetPoint().Z;
            System.Diagnostics.Debug.WriteLine(string.Format("{0}", mc.GetPoint().Z));


            flag = true;
            while (flag)
            {
                int brightness = CountHitPixels();
                //if (brightness > 4000) flag = false;
                if (brightness > 5000)
                {
                    flag = false;
                }
            }


            mc.SlowDownStop(VectorId.Z);
            // surfaces[1] = mc.GetPoint().Z;
            surfaces[2] = mc.GetPoint().Z;//
            System.Diagnostics.Debug.WriteLine(string.Format("{0}", mc.GetPoint().Z));
            Thread.Sleep(100);
            // Thread.Sleep(150);//

            //  mc.Move(new Vector3(0.0, 0.0, -0.2));
            mc.Move(new Vector3(0.0, 0.0, -0.23));//
            mc.Join();
            Thread.Sleep(100);
            //Thread.Sleep(150);//

            mc.Inch(PlusMinus.Minus, motorSpeed, VectorId.Z);

            flag = true;
            while (flag)
            {
                int brightness = CountHitPixels();
                // if (brightness < 2000) flag = false;
                if (brightness < 3000)
                {
                    flag = false;                   //
                }
            }

            mc.SlowDownStop(VectorId.Z);
            surfaces[3] = mc.GetPoint().Z;
            System.Diagnostics.Debug.WriteLine(string.Format("{0}", mc.GetPoint().Z));
            Thread.Sleep(100);
            // Thread.Sleep(150);//

            // temp_z = (surfaces[0] - 0.05) - mc.GetPoint().Z;
            temp_z = (surfaces[0] + 0.02) - mc.GetPoint().Z;//
            mc.Move(new Vector3(0.0, 0.0, temp_z));
            mc.Join();
            Thread.Sleep(100);
            //Thread.Sleep(150);//


            /*
             *          // モータZ軸をマイナス方向(下方向)に稼働させる
             *          try {
             *                  // 下降の開始
             *                  mc.SetMotorSpeed(IO.MotorSpeed.Speed3);
             *                  mc.Inch(MechaAxisAddress.ZAddress, PlusMinus.Plus);
             *          } catch (MotorAxisException) {
             *                  // すでに最下点に位置していた場合は,catchのスコープが実行される.
             *                  isOnBottomLimit = true;
             *          }
             *
             *          // 最下点もしくは下降距離分に下降したかを監視する.
             *          double startPoint = mc.GetPoint().Z;
             *          while (!isOnBottomLimit) {
             *                  // 移動距離の確認
             *                  if (loweringDistance != 0) {
             *                          isOnBottomLimit = (loweringDistance <= Math.Abs(mc.GetPoint().Z - startPoint));
             *                  }
             *
             *                  // モータの移動可能最下点到達か否かの確認
             *                  isOnBottomLimit |=
             *                          (mc.GetAbnomalState(MechaAxisAddress.ZAddress)
             *                          == MotorAbnomalState.AxisLimitPlus);
             *          }
             *          // 最下点に到達したため,モータの移動を止める.
             *          // 最下点に到達時のイベントを発生させる
             *          mc.StopInching(MechaAxisAddress.ZAddress);
             *          if (OnMotorBottomLimited != null) {
             *                  OnMotorBottomLimited(this, new EventArgs());
             *          }
             *
             *          // 遅延を行わないとモータドライバが動作不良を起こす場合がある.
             *          Thread.Sleep(300);
             *
             * */



            /* -------------------- 下降の完了 ---------------- */

            /* 上昇しながら撮影中の画像を分析する */
            // 最下点からZ軸をプラス方向に動かす.
            // 撮影中の画像がゲルの中か否かを判定する.
            // ベース及びその他の場所と,ゲルの中の境界である座標を記録する.


/*
 *
 *
 *                      mc.Inch(PlusMinus.Plus, motorSpeed, VectorId.Z);
 *                      startPoint = mc.GetPoint().Z;
 *
 *
 *         // bool flag = true;
 *
 *          while (flag) {
 *              //Thread.Sleep(delayTime);
 *
 *              byte[] b = camera.ArrayImage;
 *              Mat src = new Mat(440, 512, MatType.CV_8U, b);
 *              Mat mat = src.Clone();
 *
 *              Cv2.GaussianBlur(mat, mat, Cv.Size(3, 3), -1);
 *              Mat gau = mat.Clone();
 *              Cv2.GaussianBlur(gau, gau, Cv.Size(31, 31), -1);
 *              Cv2.Subtract(gau, mat, mat);
 *              Cv2.Threshold(mat, mat, 10, 1, ThresholdType.Binary);
 *              int brightness = Cv2.CountNonZero(mat);
 *
 *              if (brightness > 3000) flag = false;
 *          }
 *
 *          mc.StopInching(MechaAxisAddress.ZAddress);
 *          surfaces[3] = mc.GetPoint().Z;
 *          System.Diagnostics.Debug.WriteLine(string.Format("{0}", mc.GetPoint().Z));
 *
 *
 *
 *        //  Vector3 speed = new Vector3(30, 30, 0.4);//つかわないけど
 *         // Vector3 tolerance = new Vector3(0.001, 0.001, 0.001);//つかわないけど
 *        //  Vector3 distance = new Vector3(0, 0, 0.3);
 *        //  mc.Move(distance, speed, tolerance);
 *
 *          Thread.Sleep(100);
 *
 *
 */

            /*
             *
             *
             * mc.Inch(PlusMinus.Plus, MotorSpeed, VectorId.Z);
             * double currentPoint = mc.GetPoint().Z;
             *
             * bool dice = true;
             *
             * while (dice) {
             *
             *  double nowPoint = mc.GetPoint().Z;
             *
             *  double renge = nowPoint - currentPoint;
             *
             *  if (renge > 0.20) dice = false;
             *
             *
             *
             *
             * }
             * mc.StopInching(MechaAxisAddress.ZAddress);
             *
             * Thread.Sleep(100);
             *
             *
             * /
             *
             *
             * //  mc.MoveDistance(0.2, VectorId.Z);
             * //   mc.Join();
             * // Thread.Sleep(2000);
             * //  System.Diagnostics.Debug.WriteLine(string.Format("{0}", mc.GetPoint().Z));
             * /*
             *
             * mc.Inch(PlusMinus.Plus, motorSpeed, VectorId.Z);
             * flag = true;
             * while (flag) {
             *
             *  byte[] b = camera.ArrayImage;
             *  Mat src = new Mat(440, 512, MatType.CV_8U, b);
             *  Mat mat = src.Clone();
             *
             *  Cv2.GaussianBlur(mat, mat, Cv.Size(3, 3), -1);
             *  Mat gau = mat.Clone();
             *  Cv2.GaussianBlur(gau, gau, Cv.Size(31, 31), -1);
             *  Cv2.Subtract(gau, mat, mat);
             *  Cv2.Threshold(mat, mat, 10, 1, ThresholdType.Binary);
             *  int brightness = Cv2.CountNonZero(mat);
             *
             *  if (brightness < 4000) flag = false;
             * }
             *
             *
             * surfaces[2] = mc.GetPoint().Z;
             * System.Diagnostics.Debug.WriteLine(string.Format("{0}", mc.GetPoint().Z));
             *
             */


/*
 *
 *
 *          flag = true;
 *          while (flag) {
 *
 *              byte[] b = camera.ArrayImage;
 *              Mat src = new Mat(440, 512, MatType.CV_8U, b);
 *              Mat mat = src.Clone();
 *
 *              Cv2.GaussianBlur(mat, mat, Cv.Size(3, 3), -1);
 *              Mat gau = mat.Clone();
 *              Cv2.GaussianBlur(gau, gau, Cv.Size(31, 31), -1);
 *              Cv2.Subtract(gau, mat, mat);
 *              Cv2.Threshold(mat, mat, 10, 1, ThresholdType.Binary);
 *              int brightness = Cv2.CountNonZero(mat);
 *
 *              if (brightness > 4000) flag = false;
 *          }
 *
 *          mc.StopInching(MechaAxisAddress.ZAddress);
 *          surfaces[1] = mc.GetPoint().Z;
 *          System.Diagnostics.Debug.WriteLine(string.Format("{0}", mc.GetPoint().Z));
 *
 *
 *          //mc.Move(distance, speed, tolerance);
 *
 *
 *
 *
 *
 *
 *
 *          // 遅延を行わないとモータドライバが動作不良を起こす場合がある.
 *          Thread.Sleep(100);
 *
 *
 */

/*
 *
 *          mc.Inch(PlusMinus.Plus, MotorSpeed, VectorId.Z);
 *          double current2Point = mc.GetPoint().Z;
 *
 *          bool dice2 = true;
 *
 *          while (dice2) {
 *
 *              double nowPoint = mc.GetPoint().Z;
 *
 *              double renge = nowPoint - current2Point;
 *
 *              if (renge > 0.20) dice2 = false;
 *
 *
 *
 *
 *           }
 *
 *          mc.StopInching(MechaAxisAddress.ZAddress);
 *
 *          Thread.Sleep(100);
 *
 */

            // mc.MoveDistance(0.2, VectorId.Z);
            // mc.Join();

            //  Thread.Sleep(2000);
            // System.Diagnostics.Debug.WriteLine(string.Format("{0}", mc.GetPoint().Z));

            /*          mc.Inch(PlusMinus.Plus, motorSpeed, VectorId.Z);
             *        flag = true;
             *        while (flag) {
             *
             *            byte[] b = camera.ArrayImage;
             *            Mat src = new Mat(440, 512, MatType.CV_8U, b);
             *            Mat mat = src.Clone();
             *
             *            Cv2.GaussianBlur(mat, mat, Cv.Size(3, 3), -1);
             *            Mat gau = mat.Clone();
             *            Cv2.GaussianBlur(gau, gau, Cv.Size(31, 31), -1);
             *            Cv2.Subtract(gau, mat, mat);
             *            Cv2.Threshold(mat, mat, 10, 1, ThresholdType.Binary);
             *            int brightness = Cv2.CountNonZero(mat);
             *
             *            if (brightness < 12000) flag = false;
             *        }
             *        mc.StopInching(MechaAxisAddress.ZAddress);
             *        surfaces[0] = mc.GetPoint().Z;
             *        System.Diagnostics.Debug.WriteLine(string.Format("{0}", mc.GetPoint().Z));
             *
             *
             *
             */


            //////////////

            //  mc.AAAAAA();


            /*
             *          stopWatch = new System.Diagnostics.Stopwatch();
             *          stopWatch.Start();
             *
             *          int idNo = 0;
             *          int index = 3;
             #if false
             *          bool[] votes = new bool[numOfVoting];
             *          int[] values = new int[numOfVoting];
             *          double[] points = new double[numOfVoting];
             *
             *                  Thread.Sleep(delayTime);
             *
             *                  // 今回の入力画像の演算結果を多数決配列の最後尾に設定する.
             *                  // 同時に今回の判定値やモータの座標もそれぞれ保持する.
             *                  votes[votes.Length - 1] = IsInGel();
             *                  values[values.Length - 1] = brightness;
             *                  points[points.Length - 1] = mc.GetPoint().Z;
             *
             *                  // 今回と過去一定回数分の結果で多数決をとる.
             *                  presentResult = Vote(votes);
             *
             *                  // 多数決の結果配列を一つずつシフトする
             *                  for (int i = 0; i < values.Length; ++i) {
             *                          votes[i] = (i == votes.Length - 1 ? false : votes[i + 1]);
             *                          values[i] = (i == values.Length - 1 ? 0 : values[i + 1]);
             *                          points[i] = (i == points.Length - 1 ? 0 : points[i + 1]);
             *                  }
             #endif
             *
             *          double previousZVal, presentZVal;
             *          int previousBrightness , presentBrightness;
             *          bool previousResult = false, presentResult = false;
             *          while (index >= 0) {
             *                  Thread.Sleep(delayTime);
             *                  presentZVal = Math.Round(mc.GetPoint().Z, 5);
             *                  presentResult = IsInGel();
             *                  bool isBorder = isBoudarySurface(previousResult, presentResult, index);
             *
             *                  // 撮像・入力画像の確認のイベントを発生させる.
             *                  SurfaceEventArgs eventArgs = new SurfaceEventArgs();
             *                  eventArgs.Id = idNo;
             #if false
             *                  eventArgs.ZValue = points[0];
             *                  eventArgs.Brightness = values[0];
             *                  eventArgs.Distance = Math.Abs(points[0] - startPoint);
             #endif
             *
             *                  presentBrightness = brightness;
             *                  eventArgs.ZValue = presentZVal;
             *                  eventArgs.Brightness = presentBrightness;
             *                  eventArgs.Distance = Math.Abs(presentZVal - startPoint);
             *                  eventArgs.IsInGel = presentResult;
             *                  eventArgs.IsBoundary = isBorder;
             *                  eventArgs.Second = Time;
             ++idNo;
             *                  if (Shooting != null) {
             *                          Thread t = new Thread(new ThreadStart(delegate() {
             *                                  Shooting(this, eventArgs);
             *                          }));
             *                          t.IsBackground = true;
             *                          t.Start();
             *                  }
             *
             *                  if (isBorder) {
             *                          // 境界面であった場合,多数決に用いたZ座標の値の内,最も古い値を境界面の座標として採用する.
             *                          // ここでは境界面として,その座標値を保持している.
             *                          //surfaces[index] = points[0];
             *                          surfaces[index] = presentZVal;
             *
             *                          // 境界面認識時のイベントを発生させる.
             *                          switch (index) {
             *                                  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;
             *                          }
             *
             *                          --index;
             *                  }
             *                  previousResult = presentResult;
             *                  previousBrightness = presentBrightness;
             *                  previousZVal = presentZVal;
             *
             *                  // 距離によるエラー終了
             *                  if (mc.GetPoint().Z - startPoint > distanceOut) {
             *                          mc.StopInching(MechaAxisAddress.ZAddress);
             *                          throw new SurfaceFailedException("Distance limit out");
             *                  }
             *          }
             *
             *          mc.StopInching(MechaAxisAddress.ZAddress);
             *          stopWatch.Stop();
             *          dateTime = DateTime.Now;
             *          latestCoord = new Vector2(mc.GetPoint().X, mc.GetPoint().Y);
             */
        }