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; } }
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); } }
public Length GetDistance() { var distance = mDistanceSensor.GetDistance(); return(distance); }