Class SensorData

java.lang.Object
yams.core.motorcontrollers.simulation.SensorData

public class SensorData extends Object
Sensor data class to encapsulate sensor data.

SensorData represents a single named field within a Sensor. It wraps a real-hardware supplier (e.g. a lambda reading an encoder), a default value, and an optional SimValue registered with WPILib's simulation layer so the Glass GUI can override the value during simulation without touching production code.

At runtime the getValue() method applies the following priority order:

  1. Real robot always returns the live hardware supplier value immediately.
  2. Trigger override if any registered trigger condition is currently true, the associated override value is returned and written to Glass.
  3. Glass value if a SimValue exists and no trigger fired, returns whatever Glass has set (including the default).
  4. Supplier fallback returns the supplier value if no Glass value is available.

Key fields

  • m_name the human-readable field name used as the Glass widget label.
  • m_supplier provides the real sensor reading on a physical robot.
  • m_type (SensorData.HALValueType) guards typed accessors (getAsDouble(), getAsInt(), etc.) against incorrect casts.
  • m_defaultValue the initial value published to Glass at startup.
  • m_triggerValues ordered list of (condition, value) pairs checked before the Glass value each loop.
  • m_glassValue the live SimValue registered with the parent SimDevice; empty on a real robot.

Example

// Double field backed by a real encoder
SensorData posField = new SensorData("position", encoder::getPosition, 0.0);

// Boolean field backed by a limit switch
SensorData limitField = new SensorData("atLimit", limitSwitch::get, false);

// Force a specific value in simulation when autonomous starts
posField.addSimTrigger(SensorData.convert(180.0), DriverStation::isAutonomous);

// Read the value (type-safe)
double pos = posField.getAsDouble();
  • Constructor Details

    • SensorData

      public SensorData(String name, Supplier<org.wpilib.hardware.hal.HALValue> supplier, org.wpilib.hardware.hal.HALValue defaultValue, SensorData.HALValueType type)
      Sensor data constructor.
      Parameters:
      name - Name of sensor.
      supplier - Supplier<org.wpilib.hardware.hal.HALValue> of sensor. Use convert(DoubleSupplier) to convert primitive suppliers.
      defaultValue - Default value of sensor.
      type - SensorData.HALValueType of sensor.
    • SensorData

      public SensorData(String name, DoubleSupplier supplier, double defaultVal)
      Sensor data constructor.
      Parameters:
      name - Name of sensor.
      supplier - DoubleSupplier supplier
      defaultVal - Double default value.
    • SensorData

      public SensorData(String name, IntSupplier supplier, int defaultVal)
      Sensor data constructor.
      Parameters:
      name - Name of sensor.
      supplier - IntSupplier
      defaultVal - Int default value.
    • SensorData

      public SensorData(String name, BooleanSupplier supplier, boolean defaultVal)
      Sensor data constructor.
      Parameters:
      name - Name of sensor.
      supplier - BooleanSupplier
      defaultVal - Boolean default value.
    • SensorData

      public SensorData(String name, LongSupplier supplier, long defaultVal)
      Sensor data constructor.
      Parameters:
      name - Name of sensor.
      supplier - LongSupplier
      defaultVal - Long default value.
  • Method Details

    • convert

      public static Supplier<org.wpilib.hardware.hal.HALValue> convert(DoubleSupplier supplier)
      Convert a DoubleSupplier into a Supplier<org.wpilib.hardware.hal.HALValue>.
      Parameters:
      supplier - Double supplier.
      Returns:
      HALValue supplier.
    • convert

      public static Supplier<org.wpilib.hardware.hal.HALValue> convert(IntSupplier supplier)
      Convert an IntSupplier into a Supplier<org.wpilib.hardware.hal.HALValue>.
      Parameters:
      supplier - Int supplier.
      Returns:
      HALValue supplier.
    • convert

      public static Supplier<org.wpilib.hardware.hal.HALValue> convert(BooleanSupplier supplier)
      Convert a BooleanSupplier into a Supplier<org.wpilib.hardware.hal.HALValue>.
      Parameters:
      supplier - Boolean supplier.
      Returns:
      HALValue supplier.
    • convert

      public static Supplier<org.wpilib.hardware.hal.HALValue> convert(LongSupplier supplier)
      Convert a LongSupplier into a Supplier<org.wpilib.hardware.hal.HALValue>.
      Parameters:
      supplier - Long supplier.
      Returns:
      Supplier<org.wpilib.hardware.hal.HALValue>.
    • convert

      public static org.wpilib.hardware.hal.HALValue convert(double value)
      Convert a double into a HALValue.
      Parameters:
      value - Double value.
      Returns:
      HALValue.
    • convert

      public static org.wpilib.hardware.hal.HALValue convert(int value)
      Convert an int into a HALValue.
      Parameters:
      value - Int value.
      Returns:
      HALValue.
    • convert

      public static org.wpilib.hardware.hal.HALValue convert(boolean value)
      Convert a boolean into a HALValue.
      Parameters:
      value - Boolean value.
      Returns:
      HALValue.
    • convert

      public static org.wpilib.hardware.hal.HALValue convert(long value)
      Convert a long into a HALValue.
      Parameters:
      value - Long value.
      Returns:
      HALValue.
    • getAsDouble

      public double getAsDouble()
      Get the sensor value as a double.
      Returns:
      Sensor value.
      Throws:
      IllegalStateException - if this sensor field is not of type double.
    • getAsInt

      public int getAsInt()
      Get the sensor value as an int.
      Returns:
      Sensor value.
      Throws:
      IllegalStateException - if this sensor field is not of type int.
    • getAsLong

      public long getAsLong()
      Get the sensor value as a long.
      Returns:
      Sensor value.
      Throws:
      IllegalStateException - if this sensor field is not of type long.
    • getAsBoolean

      public boolean getAsBoolean()
      Get the sensor value as a boolean.
      Returns:
      Sensor value.
      Throws:
      IllegalStateException - if this sensor field is not of type boolean.
    • set

      public void set(org.wpilib.hardware.hal.HALValue val)
      Set the sensor value.
      Parameters:
      val - Value to set.
    • set

      public void set(int val)
      Set the sensor value.
      Parameters:
      val - Integer value to set.
      Implementation Note:
      The value is not checked for validity, ensure that this is the right data type for the field.
    • set

      public void set(double val)
      Set the sensor value.
      Parameters:
      val - Double value to set.
      Implementation Note:
      The value is not checked for validity, ensure that this is the right data type for the field.
    • set

      public void set(long val)
      Set the sensor value.
      Parameters:
      val - Long value to set.
      Implementation Note:
      The value is not checked for validity, ensure that this is the right data type for the field.
    • set

      public void set(boolean val)
      Set the sensor value.
      Parameters:
      val - Boolean value to set.
      Implementation Note:
      The value is not checked for validity, ensure that this is the right data type for the field.
    • getValue

      public org.wpilib.hardware.hal.HALValue getValue()
      Get the sensor value, real sensor value if the robot is real.
      Returns:
      Sensor value.
    • addSimTrigger

      public void addSimTrigger(org.wpilib.hardware.hal.HALValue value, BooleanSupplier trigger)
      Add a value set based on a trigger.
      Parameters:
      value - HALValue to set.
      trigger - BooleanSupplier when to use.
    • getName

      public String getName()
      Get the sensor name.
      Returns:
      Sensor name.
    • getType

      public SensorData.HALValueType getType()
      Get the sensor data type.
      Returns:
      SensorData.HALValueType
    • getDefault

      public org.wpilib.hardware.hal.HALValue getDefault()
      Get the default value.
      Returns:
      HALValue default value.
    • createValue

      public org.wpilib.hardware.hal.SimValue createValue(org.wpilib.hardware.hal.SimDevice device, org.wpilib.hardware.hal.SimDevice.Direction direction)
      Get the SimValue for the sensor.
      Parameters:
      device - SimDevice to create the SimValue for.
      direction - SimDevice.Direction of the SimValue.
      Returns:
      SimValue for the sensor.