private void MeasurementTimer_Elapsed(object sender, ElapsedEventArgs e) { // lock (_lock) // { if (!(CenterSonarDevice is null)) { try { Log.Debug($"Updating distance measurements..."); var centerDistance = CenterSonarDevice.Distance.Centimeters; Log.Debug("Center distance measuremente updated ({distance} cm.)", Math.Round(centerDistance, 4, MidpointRounding.AwayFromZero)); Thread.Sleep(60); var leftDistance = LeftSonarDevice.Distance.Centimeters; Log.Debug("Left distance measuremente updated ({distance} cm.)", Math.Round(leftDistance, 4, MidpointRounding.AwayFromZero)); Thread.Sleep(60); var rightDistance = RightSonarDevice.Distance.Centimeters; Log.Debug("Right distance measuremente updated ({distance} cm.)", Math.Round(rightDistance, 4, MidpointRounding.AwayFromZero)); Thread.Sleep(60); Distance = new DistanceTuple(leftDistance, centerDistance, rightDistance); } catch (Exception ex) { Log.Error(ex.Message); } } // } }
/// <summary> /// Initializes a <see cref="Sonar"/> instance /// </summary> public Sonar() { Log.Debug("Initializing sonar hardware and services..."); Distance = new DistanceTuple(0, 0, 0); CenterSonarDevice = new Hcsr04(CENTER_TRIG, CENTER_ECHO); LeftSonarDevice = new Hcsr04(LEFT_TRIG, LEFT_ECHO); RightSonarDevice = new Hcsr04(RIGHT_TRIG, RIGHT_ECHO); Log.Debug("Sonar hardware and services initialized"); MeasurementTimer = new System.Timers.Timer(250); MeasurementTimer.Elapsed += MeasurementTimer_Elapsed; MeasurementTimer.AutoReset = true; MeasurementTimer.Enabled = true; }