Exemple #1
0
        public void runnerContext()
        {
            var connectionInfo = GetDeviceConnectionInfo();

            if (connectionInfo.comPort == 0)
            {
                WriteLog("E,Device Not Found");

                return;
            }
            else
            {
                WriteLog($"I,COM Port={connectionInfo.comPort}");
                WriteLog($"I,Baud Rate={connectionInfo.baudRate}");
            }

            screenWidth  = Screen.PrimaryScreen.Bounds.Width;
            screenHeight = Screen.PrimaryScreen.Bounds.Height;

            WriteLog($"I,Screen Size={screenWidth}*{screenHeight}");

            hokuyo = new Hokuyo(connectionInfo.comPort, connectionInfo.baudRate);
            hokuyo.Connect();

            int buffer = 0;
            List <(double x, double y)> bufferPosition = new List <(double x, double y)>();

            while (true)
            {
                if (runner == null)
                {
                    break;
                }
                buffer++;
                try {
                    var rawDistanceValues = hokuyo.GetData();

                    IEnumerable <(double x, double y)> rawPoints = null;
                    LocationEnum locationType = LocationEnum.LeftBottom;
                    this.Invoke((MethodInvoker) delegate() {
                        locationType = (LocationEnum)location.SelectedIndex;
                    });

                    if (locationType == (int)LocationEnum.LeftBottom)
                    {
                        rawPoints = rawDistanceValues
                                    .Skip(128 - 44).Take(384 - 128)
                                    .Select((x, i) => new {
                            degree   = i * ((Math.PI / 2) / (384 - 128)),
                            distance = x
                        })
                                    .Select(x => GetPoint(x.degree, x.distance))
                                    .Where(x =>
                                           x.x >= decimal.ToInt32(MinX.Value) && x.x <= decimal.ToInt32(MaxX.Value) &&
                                           x.y >= decimal.ToInt32(MinY.Value) && x.y <= decimal.ToInt32(MaxY.Value));
                    }
                    else if (locationType == LocationEnum.RightBottom)
                    {
                        rawPoints = rawDistanceValues
                                    .Skip(128 - 44 + 384 - 128).Take(384 - 128)
                                    .Select((x, i) => new {
                            degree   = i * ((Math.PI / 2) / (384 - 128)) + (Math.PI / 2),
                            distance = x
                        })
                                    .Select(x => GetPoint(x.degree, x.distance))
                                    .Where(x =>
                                           x.x <= -decimal.ToInt32(MinX.Value) && x.x >= -decimal.ToInt32(MaxX.Value) &&
                                           x.y >= decimal.ToInt32(MinY.Value) && x.y <= decimal.ToInt32(MaxY.Value));
                    }


                    var currentPoint = (
                        x : rawPoints.Sum(x => x.x) / rawPoints.Count(),
                        y : rawPoints.Sum(x => x.y) / rawPoints.Count()
                        );

                    if (double.IsNaN(currentPoint.x) ||
                        double.IsNaN(currentPoint.y))
                    {
                        continue;
                    }

                    currentPoint = ConvertPosition(currentPoint, locationType);
                    bufferPosition.Add(currentPoint); // 加入緩衝

                    if (buffer % Convert.ToInt32(BufferCount.Value) == 0)
                    {
                        // 達到緩衝次數
                        buffer = 0;

                        // 計算平均值
                        currentPoint = (x : bufferPosition.Average(x => x.x), y : bufferPosition.Average(x => x.y));

                        bufferPosition.Clear();
                    }
                    else
                    {
                        // 緩衝中跳過
                        continue;
                    }

                    if (bindMouse)
                    {
                        WinAPI.SetCursorPos((int)currentPoint.x, (int)currentPoint.y);

                        bool autoClick = false;
                        this.Invoke((MethodInvoker) delegate() {
                            autoClick = AutoClickCheckBox.Checked;
                        });

                        if (autoClick)
                        {
                            WinAPI.mouse_event(WinAPI.MOUSEEVENTF_LEFTDOWN, (int)currentPoint.x, (int)currentPoint.y, 0, 0);
                            WinAPI.mouse_event(WinAPI.MOUSEEVENTF_LEFTUP, (int)currentPoint.x, (int)currentPoint.y, 0, 0);
                        }
                    }

                    WriteLog($"I,x: {currentPoint.x}, y: {currentPoint.y}");
                } catch (Exception e) {
                    WriteLog("E," + e.ToString());
                }
            }

            hokuyo.Disconnect();
            hokuyo = null;
        }
 public void TestInitialize()
 {
     hokuyo = new Hokuyo(hokuyoComPort, hokuyoBaudRate);
     hokuyo.Connect();
 }