/// <summary> /// Initializes a new instance of the <see cref="ProximitySensor"/> class /// </summary> /// <param name="proximitySensorType"> /// </param> /// <param name="analogInput"> /// The analog input to which the proximity sensor is attached /// </param> /// <exception cref="ArgumentNullException"> /// Thrown when the given <see cref="AnalogInput"/> is null /// </exception> /// <remarks> /// This sensor is only provides valid results for objects farther than 3" away or objects closer than /// <see cref="MaximumReadableDistance"/> from the sensor. Passing a trigger which attempts to obtain /// distances outside of this range will result in a large number of false positives. /// </remarks> public ProximitySensor(ProximitySensorType proximitySensorType, AnalogInput analogInput) { if (analogInput == null) { throw new ArgumentNullException("analogInput"); } this.proximitySensorType = proximitySensorType; MaximumReadableDistance = GetMaximumReadableDistance(analogInput); new Thread(() => { while (true) { if (IsEnabled && ObjectDetectionTrigger != null) { var distance = new Distance(analogInput.ReadRaw(), proximitySensorType); if (ObjectDetectionTrigger(MaximumReadableDistance, distance)) { if (ObjectDetected != null) { ObjectDetected(this, new ObjectDetectedEventArgs(distance, DateTime.UtcNow)); } } } Thread.Sleep(10); } }).Start(); }
/// <summary> /// Converts an analog voltage output from a proximity sensor to a distance in centimeters /// </summary> /// <param name="analogVoltage"> /// An analog voltage output from a proximity sensor /// </param> /// <param name="proximitySensorType"> /// The type of Sharp proximity sensor being used /// </param> /// <returns> /// The distance an object lies from the proximity sensor measured in centimeters /// </returns> public static double ToCentimeters(double analogVoltage, ProximitySensorType proximitySensorType) { double distance = 0; switch (proximitySensorType) { case ProximitySensorType.GP2Y0A21YK: distance = 41.543 * Math.Pow(analogVoltage + 0.30221, -1.5281);; break; case ProximitySensorType.GP2Y0A02YK0F: distance = 61.681 * Math.Pow(analogVoltage, -1.133); break; } return distance; }
/// <summary> /// Converts an analog voltage output from a proximity sensor to a distance in inches /// </summary> /// <param name="analogVoltage"> /// An analog voltage output from a proximity sensor /// </param> /// <param name="proximitySensorType"> /// The type of Sharp proximity sensor being used /// </param> /// <returns> /// The distance an object lies from the proximity sensor measured in inches /// </returns> public static double ToInches(double analogVoltage, ProximitySensorType proximitySensorType) { return ToCentimeters(analogVoltage, proximitySensorType) / 2.54; }
/// <summary> /// Initializes a new instance of the <see cref="Distance"/> class /// </summary> /// <param name="digitalVoltage"> /// The digital output voltage from the proximity sensor /// </param> /// <param name="proximitySensorType"> /// The type of Sharp proximity sensor being used /// </param> internal Distance(int digitalVoltage, ProximitySensorType proximitySensorType) { analogVoltage = AdcHelper.ToAnalogVoltage(digitalVoltage); this.digitalVoltage = digitalVoltage; this.proximitySensorType = proximitySensorType; }