private void SetPositionAndGetReading(int position) { try { _contoller.SetPwm(PwmChannel.C0, 0, position); Task.Delay(50).Wait(); if (_haveReading()) { PositionFound?.Invoke(this, new PositionalDistanceEventArgs { Angle = Angle(position), Proximity = _lastReading.Proximity, RawValue = _lastReading.RawValue }); } } catch (Exception ex) { SensorException?.Invoke(this, new ExceptionEventArgs { Exception = ex, Message = "An error occured setting Angle and getting distance." }); } }
public SonarCoordinator(PCA9685ServoContoller contoller) { _contoller = contoller; //sensor.ProximityReceived += (sender, args) => _lastReading = args; //sensor.SensorException += (sender, args) => SensorException?.Invoke(this, args); //Pass exceptions from Arduino "sensor" along. _minServoSetting = 250; _maxServoSetting = 800; _contoller.ResetDevice(); _contoller.SetPwmUpdateRate(60); _contoller.SetPwm(PwmChannel.C0, 0, _minServoSetting); Task.Delay(500).Wait(); _currentPosition = _minServoSetting; _arcUnitsPerDegree = (_maxServoSetting - _minServoSetting) / 180d; _stepSize = (int)Math.Floor(_arcUnitsPerDegree * 5); _haveReading = () => true; //_lastReading?.RawValue != default(double); }