Class FakeDistanceSensor

java.lang.Object
org.horizon36596.simloop.fakehardware.AbstractFakeDevice
org.horizon36596.simloop.fakehardware.FakeDistanceSensor
All Implemented Interfaces:
com.qualcomm.robotcore.hardware.DistanceSensor, com.qualcomm.robotcore.hardware.HardwareDevice, FakeDevice

public class FakeDistanceSensor extends AbstractFakeDevice implements com.qualcomm.robotcore.hardware.DistanceSensor
Fake DistanceSensor modelled on the REV 2m Distance Sensor (a VL53L0X time-of-flight chip). Unchanged robot code resolves it with hardwareMap.get(DistanceSensor.class, name) and reads getDistance(DistanceUnit) exactly as it does on the real robot.

Two ways to drive it

Whichever was called last wins: a scripted value clears any source.

What the robot reads, read out of the SDK 12.0 VL53L0X bytecode

  • Whole millimetres. The chip reports an integer number of millimetres; the driver divides it by 10 for CM, 1000 for METER and 25.4 for INCH, and returns it unchanged for MM. This fake rounds the true distance to the nearest millimetre and divides the same way, so a reading is the same double a robot would print.
  • Nothing in range is a large number, not the SDK's sentinel. The REV 2m driver never returns DistanceSensor.distanceOutOfRange: it passes on whatever the chip reports, and with no target in range that is the chip's own "no target" value (REV_2M_NO_TARGET_MM), which reads as roughly 819 cm or 322 in. Robot code that tests == distanceOutOfRange never sees a match on a real REV 2m, and in this sim it does not either. A sensor whose driver does return the sentinel is built with new FakeDistanceSensor(maxRangeMm, DistanceSensor.distanceOutOfRange); the sentinel is then returned as it is in every unit, never divided, so the comparison works.
  • Metadata: device name "REV 2M ToF Distance Sensor" and manufacturer Other, as the driver reports.

Not modelled: noise, the sensor's field of view (a ray caster decides what it sees), the target's reflectivity, the chip's timing budget and the driver's timeout (which reads 65535 mm). The SDK's concrete Rev2mDistanceSensor class is not faked, so hardwareMap.get( Rev2mDistanceSensor.class, name) does not resolve; robot code that asks for the DistanceSensor interface, as the SDK's own sample does, works.

Deterministic: no clock, no randomness. A fresh sensor sees nothing in range.

  • Nested Class Summary

    Nested classes/interfaces inherited from interface com.qualcomm.robotcore.hardware.HardwareDevice

    com.qualcomm.robotcore.hardware.HardwareDevice.Manufacturer
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    The farthest target a REV 2m sees, millimetres.
    static final double
    What a REV 2m reports with nothing in range, millimetres.

    Fields inherited from interface com.qualcomm.robotcore.hardware.DistanceSensor

    distanceOutOfRange
  • Constructor Summary

    Constructors
    Constructor
    Description
    A REV 2m Distance Sensor: REV_2M_MAX_RANGE_MM range, REV_2M_NO_TARGET_MM when nothing is in range.
    FakeDistanceSensor(double maxRangeMm, double outOfRangeReadingMm)
    A range sensor with its own range and its own "nothing in range" reading.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns "REV 2M ToF Distance Sensor", the string Rev2mDistanceSensor.getDeviceName returns.
    double
    getDistance(org.firstinspires.ftc.robotcore.external.navigation.DistanceUnit unit)
    The distance the sensor reports: the true distance rounded to whole millimetres, or the out-of-range reading when nothing is within range, converted the way the SDK's VL53L0X driver converts it (see the class javadoc).
    double
    Returns the farthest target the sensor reports, millimetres.
    double
    Returns what the sensor reports with nothing in range, millimetres, or the SDK's sentinel.
    int
    Returns 1, as the SDK's I2C device drivers report.
    boolean
    Returns true when the current target is within the sensor's range, so a reading is a real distance.
    void
    setDistance(org.firstinspires.ftc.robotcore.external.navigation.DistanceUnit unit, double distance)
    Put a target distance away from the sensor's face, from now on.
    void
    setDistanceSource(org.firstinspires.ftc.robotcore.external.navigation.DistanceUnit unit, DoubleSupplier source)
    Ask source how far the target is on every update(double) from now on.
    void
    Take every target away from now on, so the sensor reports out of range.
    void
    update(double deltaTime)
    Samples the source set with setDistanceSource(DistanceUnit, DoubleSupplier), if there is one.

    Methods inherited from class org.horizon36596.simloop.fakehardware.AbstractFakeDevice

    close, getConnectionInfo, getManufacturer, resetDeviceConfigurationForOpMode

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.qualcomm.robotcore.hardware.HardwareDevice

    close, getConnectionInfo, getManufacturer, resetDeviceConfigurationForOpMode
  • Field Details

    • REV_2M_MAX_RANGE_MM

      public static final double REV_2M_MAX_RANGE_MM
      The farthest target a REV 2m sees, millimetres. SPEC: REV's product name and datasheet range, 2 m, in the driver's default long-range mode. A real sensor's range falls short of this on a dark or angled target; that is not modelled.
      See Also:
    • REV_2M_NO_TARGET_MM

      public static final double REV_2M_NO_TARGET_MM
      What a REV 2m reports with nothing in range, millimetres. NEEDS MEASUREMENT (2026-09-28): 8190 mm is the VL53L0X "no target" reading FTC teams widely report from this sensor (819 cm, 322.4 in); it has not been read off this team's sensor. The SDK driver passes the chip's number straight through, so whatever the chip sends is what robot code sees.
      See Also:
  • Constructor Details

    • FakeDistanceSensor

      public FakeDistanceSensor()
      A REV 2m Distance Sensor: REV_2M_MAX_RANGE_MM range, REV_2M_NO_TARGET_MM when nothing is in range.
    • FakeDistanceSensor

      public FakeDistanceSensor(double maxRangeMm, double outOfRangeReadingMm)
      A range sensor with its own range and its own "nothing in range" reading.
      Parameters:
      maxRangeMm - the farthest target the sensor reports, millimetres (> 0); anything farther reads as outOfRangeReadingMm
      outOfRangeReadingMm - what the sensor reports with nothing in range, millimetres, or DistanceSensor.distanceOutOfRange for a driver that returns the SDK's sentinel (returned unconverted in every unit)
      Throws:
      IllegalArgumentException - if maxRangeMm is not positive and finite, or outOfRangeReadingMm is NaN or negative
  • Method Details

    • setDistance

      public void setDistance(org.firstinspires.ftc.robotcore.external.navigation.DistanceUnit unit, double distance)
      Put a target distance away from the sensor's face, from now on. Clears any source. Sim-only: a real DistanceSensor has no such method.
      Parameters:
      unit - the unit distance is in
      distance - how far the target is; +infinity means nothing is there, and anything past the sensor's range reads as out of range
      Throws:
      IllegalArgumentException - if unit is null, or distance is NaN or negative
    • setNothingInRange

      public void setNothingInRange()
      Take every target away from now on, so the sensor reports out of range. Clears any source.
    • setDistanceSource

      public void setDistanceSource(org.firstinspires.ftc.robotcore.external.navigation.DistanceUnit unit, DoubleSupplier source)
      Ask source how far the target is on every update(double) from now on. Replaces any earlier source and, from the next tick, any scripted value. Pass a null source to stop asking; the sensor then keeps the last distance the source gave.
      Parameters:
      unit - the unit the source answers in
      source - what decides how far the target is, asked once per harness tick; it may answer +infinity for nothing in view, never NaN or a negative number. May be null
      Throws:
      IllegalArgumentException - if source is not null and unit is
    • update

      public void update(double deltaTime)
      Samples the source set with setDistanceSource(DistanceUnit, DoubleSupplier), if there is one. A range sensor has no dynamics of its own.
      Specified by:
      update in interface FakeDevice
      Parameters:
      deltaTime - how far to advance, in seconds; unused
      Throws:
      IllegalArgumentException - if the source answers NaN or a negative distance
    • isTargetInRange

      public boolean isTargetInRange()
      Returns true when the current target is within the sensor's range, so a reading is a real distance.
      Returns:
      true when the current target is within the sensor's range, so a reading is a real distance
    • getMaxRangeMm

      public double getMaxRangeMm()
      Returns the farthest target the sensor reports, millimetres.
      Returns:
      the farthest target the sensor reports, millimetres
    • getOutOfRangeReadingMm

      public double getOutOfRangeReadingMm()
      Returns what the sensor reports with nothing in range, millimetres, or the SDK's sentinel.
      Returns:
      what the sensor reports with nothing in range, millimetres, or the SDK's sentinel
    • getDistance

      public double getDistance(org.firstinspires.ftc.robotcore.external.navigation.DistanceUnit unit)
      The distance the sensor reports: the true distance rounded to whole millimetres, or the out-of-range reading when nothing is within range, converted the way the SDK's VL53L0X driver converts it (see the class javadoc).
      Specified by:
      getDistance in interface com.qualcomm.robotcore.hardware.DistanceSensor
      Parameters:
      unit - the unit to report in
      Returns:
      the reading in unit; the SDK's DistanceSensor.distanceOutOfRange unconverted if this sensor was built to report it
    • getDeviceName

      public String getDeviceName()
      Returns "REV 2M ToF Distance Sensor", the string Rev2mDistanceSensor.getDeviceName returns.
      Specified by:
      getDeviceName in interface com.qualcomm.robotcore.hardware.HardwareDevice
      Overrides:
      getDeviceName in class AbstractFakeDevice
      Returns:
      "REV 2M ToF Distance Sensor", the string Rev2mDistanceSensor.getDeviceName returns
    • getVersion

      public int getVersion()
      Returns 1, as the SDK's I2C device drivers report.
      Specified by:
      getVersion in interface com.qualcomm.robotcore.hardware.HardwareDevice
      Overrides:
      getVersion in class AbstractFakeDevice
      Returns:
      1, as the SDK's I2C device drivers report