Class Sensor

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

public class Sensor extends Object
Sensor class using SimDevice; All fields will use the given supplier on real robots. Fake data is only given when connected to simulation.

Sensor models a named hardware sensor (such as an encoder, limit switch, or analog input) whose state is consumed by simulation suppliers and mechanism code. Each sensor owns one or more named SensorData fields that carry typed values (double, int, boolean, or long).

On a real robot every field delegates to the real-hardware supplier provided at construction time. In simulation, a SimDevice is registered with WPILib so the WPILib Glass simulation GUI can read and override individual field values, enabling hardware-in-the-loop-style testing without real hardware.

Trigger overrides (addSimTrigger(String, HALValue, BooleanSupplier)) allow simulation suppliers to inject specific sensor readings (e.g. a limit-switch trip) at a given moment during a simulated match, which is useful for automating regression tests.

Key fields and methods

Example

// Wrap a real encoder position in a simulated sensor field
SensorData posField = new SensorData("position", encoder::getPosition, 0.0);
Sensor encoderSensor = new Sensor("ArmEncoder", List.of(posField));

// Read the value (returns hardware value on robot, Glass value in sim)
double pos = encoderSensor.getAsDouble("position");

// Inject a forced value during simulation when a condition is met
encoderSensor.addSimTrigger("position",
    SensorData.convert(90.0),
    () -> DriverStation.isAutonomous());
  • Constructor Details

    • Sensor

      public Sensor(String sensorName, List<SensorData> sensorFields)
      Sensor constructor, for a sensor that will report the real data when connected to the robot or Simulation GUI data when connected to Sim.
      Parameters:
      sensorName - Name of the sensor.
      sensorFields - List of sensor fields. See SensorData.
      Throws:
      IllegalStateException - if two sensor fields share the same name.
    • Sensor

      public Sensor(SensorConfig cfg)
      Sensor simulation constructor.
      Parameters:
      cfg - SensorConfig class
      Throws:
      IllegalStateException - if two sensor fields in the config share the same name.
  • Method Details

    • getField

      public SensorData getField(String name)
      Get a field from the simulated or real sensor.
      Parameters:
      name - Name of the field
      Returns:
      SensorData of the field.
      Throws:
      IllegalArgumentException - if no field with the given name exists on this sensor.
    • getAsDouble

      public double getAsDouble(String name)
      Get a field from the simulated or real sensor.
      Parameters:
      name - Name of the field
      Returns:
      Value of the field as a double.
      Throws:
      IllegalArgumentException - if no field with the given name exists on this sensor.
      IllegalStateException - if the field is not a double field.
    • getAsInt

      public int getAsInt(String name)
      Get a field from the simulated or real sensor.
      Parameters:
      name - Name of the field
      Returns:
      Value of the field as an int.
      Throws:
      IllegalArgumentException - if no field with the given name exists on this sensor.
      IllegalStateException - if the field is not an int field.
    • getAsBoolean

      public boolean getAsBoolean(String name)
      Get a field from the simulated or real sensor.
      Parameters:
      name - Name of the field
      Returns:
      Value of the field as a boolean.
      Throws:
      IllegalArgumentException - if no field with the given name exists on this sensor.
      IllegalStateException - if the field is not a boolean field.
    • getAsLong

      public long getAsLong(String name)
      Get a field from the simulated or real sensor.
      Parameters:
      name - Name of the field
      Returns:
      Value of the field as a long.
      Throws:
      IllegalArgumentException - if no field with the given name exists on this sensor.
      IllegalStateException - if the field is not a long field.
    • getDevice

      public Optional<org.wpilib.hardware.hal.SimDevice> getDevice()
      Get the simulated device.
      Returns:
      Simulated device.
    • addSimTrigger

      public void addSimTrigger(String field, org.wpilib.hardware.hal.HALValue value, BooleanSupplier trigger)
      Set a simulated value based on a trigger.
      Parameters:
      field - Field name to set.
      value - HALValue to set.
      trigger - BooleanSupplier when to use.
      Throws:
      IllegalArgumentException - if no field with the given name exists on this sensor.