Class Sensor
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
getField(String)retrieve aSensorDatafield by name.getAsDouble(String),getAsInt(String),getAsBoolean(String),getAsLong(String)typed convenience accessors that call through to the underlying field.-
addSimTrigger(String, org.wpilib.hardware.hal.HALValue, java.util.function.BooleanSupplier)inject a simulated override value whenever a condition is true. getDevice()returns the underlyingSimDevice(empty when running on a real robot).
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 Summary
ConstructorsConstructorDescriptionSensor(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.Sensor(SensorConfig cfg) Sensor simulation constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddSimTrigger(String field, org.wpilib.hardware.hal.HALValue value, BooleanSupplier trigger) Set a simulated value based on a trigger.booleangetAsBoolean(String name) Get a field from the simulated or real sensor.doublegetAsDouble(String name) Get a field from the simulated or real sensor.intGet a field from the simulated or real sensor.longGet a field from the simulated or real sensor.Optional<org.wpilib.hardware.hal.SimDevice> Get the simulated device.Get a field from the simulated or real sensor.
-
Constructor Details
-
Sensor
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. SeeSensorData.- Throws:
IllegalStateException- if two sensor fields share the same name.
-
Sensor
Sensor simulation constructor.- Parameters:
cfg-SensorConfigclass- Throws:
IllegalStateException- if two sensor fields in the config share the same name.
-
-
Method Details
-
getField
Get a field from the simulated or real sensor.- Parameters:
name- Name of the field- Returns:
SensorDataof the field.- Throws:
IllegalArgumentException- if no field with the given name exists on this sensor.
-
getAsDouble
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
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
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
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
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-HALValueto set.trigger-BooleanSupplierwhen to use.- Throws:
IllegalArgumentException- if no field with the given name exists on this sensor.
-