コード例 #1
0
        public async void ScannerTask()
        {
            int    direction = stepsSize;
            double distance;
            int    currentAngle = MidpointServoDegrees;
            int    nextAngle    = 0;

            gw.ServoPosition(MidpointServoDegrees); // set servo midpoint ready for first distance measure
            await Task.Delay(500);                  // give servo enough time to get to rotation midpoint

            while (true)                            // Scan infinitely
            {
                frameTimer.Restart();               // measures time to sense distance, move servo and update UI

                distance = distanceSensor.GetDistance().Centimeters;

                nextAngle = CalculateNextAngle(currentAngle, ref direction);

                MoveServo(nextAngle);  // more servo in readiness for next distance measurement

                UpdateUI(currentAngle, distance);

                frameTimer.Stop();

                if (frameTimer.ElapsedMilliseconds < FrameTimeMilliseconds)
                {
                    // drive consisent sonar scan cadence
                    await Task.Delay(FrameTimeMilliseconds - (int)frameTimer.ElapsedMilliseconds);
                }

                currentAngle = nextAngle;
            }
        }
コード例 #2
0
        private void SensorUpdateTimerCallback(object state)
        {
            try
            {
                UnitsNet.Length distance = hcsr04.GetDistance();

                Debug.WriteLine($"{DateTime.UtcNow.ToShortTimeString()} Timer triggered " +
                                $"Distance: {distance.Meters:0.00}M " +
                                $" {distance.Centimeters:0.0}cm " +
                                $" {distance.Millimeters}mm");
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
コード例 #3
0
        public Length GetDistance()
        {
            var distance = mDistanceSensor.GetDistance();

            return(distance);
        }