001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.wpilibj.xrp;
006
007import edu.wpi.first.math.util.Units;
008import edu.wpi.first.wpilibj.AnalogInput;
009
010/** This class represents the ultrasonic rangefinder on an XRP robot. */
011public class XRPRangefinder {
012  private final AnalogInput m_rangefinder = new AnalogInput(2);
013
014  /**
015   * Constructs an XRPRangefinder.
016   *
017   * <p>Only one instance of a XRPRangefinder is supported.
018   */
019  public XRPRangefinder() {}
020
021  /**
022   * Get the measured distance in meters. Distance further than 4m will be reported as 4m.
023   *
024   * @return distance in meters
025   */
026  public double getDistanceMeters() {
027    return (m_rangefinder.getVoltage() / 5.0) * 4.0;
028  }
029
030  /**
031   * Get the measured distance in inches.
032   *
033   * @return distance in inches
034   */
035  public double getDistanceInches() {
036    return Units.metersToInches(getDistanceMeters());
037  }
038}