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.simulation;
006
007import edu.wpi.first.hal.SimBoolean;
008import edu.wpi.first.hal.SimDouble;
009import edu.wpi.first.math.util.Units;
010import edu.wpi.first.wpilibj.Ultrasonic;
011
012/** Class to control a simulated {@link edu.wpi.first.wpilibj.Ultrasonic}. */
013public class UltrasonicSim {
014  private final SimBoolean m_simRangeValid;
015  private final SimDouble m_simRange;
016
017  /**
018   * Constructor.
019   *
020   * @param ultrasonic The real ultrasonic to simulate
021   */
022  public UltrasonicSim(Ultrasonic ultrasonic) {
023    // ping parameter is unused
024    this(-1, ultrasonic.getEchoChannel());
025  }
026
027  /**
028   * Constructor.
029   *
030   * @param ping unused.
031   * @param echo the ultrasonic's echo channel.
032   */
033  public UltrasonicSim(@SuppressWarnings("unused") int ping, int echo) {
034    SimDeviceSim simDevice = new SimDeviceSim("Ultrasonic", echo);
035    m_simRangeValid = simDevice.getBoolean("Range Valid");
036    m_simRange = simDevice.getDouble("Range (in)");
037  }
038
039  /**
040   * Sets if the range measurement is valid.
041   *
042   * @param valid True if valid
043   */
044  public void setRangeValid(boolean valid) {
045    m_simRangeValid.set(valid);
046  }
047
048  /**
049   * Sets the range measurement.
050   *
051   * @param inches The range in inches.
052   */
053  public void setRangeInches(double inches) {
054    m_simRange.set(inches);
055  }
056
057  /**
058   * Sets the range measurement.
059   *
060   * @param meters The range in meters.
061   */
062  public void setRangeMeters(double meters) {
063    m_simRange.set(Units.metersToInches(meters));
064  }
065}