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  public void setRangeValid(boolean valid) {
040    m_simRangeValid.set(valid);
041  }
042
043  public void setRangeInches(double inches) {
044    m_simRange.set(inches);
045  }
046
047  public void setRangeMeters(double meters) {
048    m_simRange.set(Units.metersToInches(meters));
049  }
050}