Class SensorData
java.lang.Object
yams.core.motorcontrollers.simulation.SensorData
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:
- Real robot always returns the live hardware supplier value immediately.
- Trigger override if any registered
triggercondition is currentlytrue, the associated override value is returned and written to Glass. - Glass value if a
SimValueexists and no trigger fired, returns whatever Glass has set (including the default). - Supplier fallback returns the supplier value if no Glass value is available.
Key fields
m_namethe human-readable field name used as the Glass widget label.m_supplierprovides the real sensor reading on a physical robot.m_type(SensorData.HALValueType) guards typed accessors (getAsDouble(),getAsInt(), etc.) against incorrect casts.m_defaultValuethe initial value published to Glass at startup.m_triggerValuesordered list of(condition, value)pairs checked before the Glass value each loop.m_glassValuethe liveSimValueregistered with the parentSimDevice; 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();
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionSensorData(String name, BooleanSupplier supplier, boolean defaultVal) Sensor data constructor.SensorData(String name, DoubleSupplier supplier, double defaultVal) Sensor data constructor.SensorData(String name, IntSupplier supplier, int defaultVal) Sensor data constructor.SensorData(String name, LongSupplier supplier, long defaultVal) Sensor data constructor.SensorData(String name, Supplier<org.wpilib.hardware.hal.HALValue> supplier, org.wpilib.hardware.hal.HALValue defaultValue, SensorData.HALValueType type) Sensor data constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddSimTrigger(org.wpilib.hardware.hal.HALValue value, BooleanSupplier trigger) Add a value set based on a trigger.static org.wpilib.hardware.hal.HALValueconvert(boolean value) Convert a boolean into aHALValue.static org.wpilib.hardware.hal.HALValueconvert(double value) Convert a double into aHALValue.static org.wpilib.hardware.hal.HALValueconvert(int value) Convert an int into aHALValue.static org.wpilib.hardware.hal.HALValueconvert(long value) Convert a long into aHALValue.static Supplier<org.wpilib.hardware.hal.HALValue> convert(BooleanSupplier supplier) Convert aBooleanSupplierinto aSupplier<org.wpilib.hardware.hal.HALValue>.static Supplier<org.wpilib.hardware.hal.HALValue> convert(DoubleSupplier supplier) Convert aDoubleSupplierinto aSupplier<org.wpilib.hardware.hal.HALValue>.static Supplier<org.wpilib.hardware.hal.HALValue> convert(IntSupplier supplier) Convert anIntSupplierinto aSupplier<org.wpilib.hardware.hal.HALValue>.static Supplier<org.wpilib.hardware.hal.HALValue> convert(LongSupplier supplier) Convert aLongSupplierinto aSupplier<org.wpilib.hardware.hal.HALValue>.org.wpilib.hardware.hal.SimValuecreateValue(org.wpilib.hardware.hal.SimDevice device, org.wpilib.hardware.hal.SimDevice.Direction direction) Get theSimValuefor the sensor.booleanGet the sensor value as a boolean.doubleGet the sensor value as a double.intgetAsInt()Get the sensor value as an int.longGet the sensor value as a long.org.wpilib.hardware.hal.HALValueGet the default value.getName()Get the sensor name.getType()Get the sensor data type.org.wpilib.hardware.hal.HALValuegetValue()Get the sensor value, real sensor value if the robot is real.voidset(boolean val) Set the sensor value.voidset(double val) Set the sensor value.voidset(int val) Set the sensor value.voidset(long val) Set the sensor value.voidset(org.wpilib.hardware.hal.HALValue val) Set the sensor value.
-
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. Useconvert(DoubleSupplier)to convert primitive suppliers.defaultValue- Default value of sensor.type-SensorData.HALValueTypeof sensor.
-
SensorData
Sensor data constructor.- Parameters:
name- Name of sensor.supplier-DoubleSuppliersupplierdefaultVal- Double default value.
-
SensorData
Sensor data constructor.- Parameters:
name- Name of sensor.supplier-IntSupplierdefaultVal- Int default value.
-
SensorData
Sensor data constructor.- Parameters:
name- Name of sensor.supplier-BooleanSupplierdefaultVal- Boolean default value.
-
SensorData
Sensor data constructor.- Parameters:
name- Name of sensor.supplier-LongSupplierdefaultVal- Long default value.
-
-
Method Details
-
convert
Convert aDoubleSupplierinto aSupplier<org.wpilib.hardware.hal.HALValue>.- Parameters:
supplier- Double supplier.- Returns:
- HALValue supplier.
-
convert
Convert anIntSupplierinto aSupplier<org.wpilib.hardware.hal.HALValue>.- Parameters:
supplier- Int supplier.- Returns:
- HALValue supplier.
-
convert
Convert aBooleanSupplierinto aSupplier<org.wpilib.hardware.hal.HALValue>.- Parameters:
supplier- Boolean supplier.- Returns:
- HALValue supplier.
-
convert
Convert aLongSupplierinto aSupplier<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 aHALValue.- Parameters:
value- Double value.- Returns:
HALValue.
-
convert
public static org.wpilib.hardware.hal.HALValue convert(int value) Convert an int into aHALValue.- Parameters:
value- Int value.- Returns:
HALValue.
-
convert
public static org.wpilib.hardware.hal.HALValue convert(boolean value) Convert a boolean into aHALValue.- Parameters:
value- Boolean value.- Returns:
HALValue.
-
convert
public static org.wpilib.hardware.hal.HALValue convert(long value) Convert a long into aHALValue.- 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
Add a value set based on a trigger.- Parameters:
value-HALValueto set.trigger-BooleanSupplierwhen to use.
-
getName
-
getType
-
getDefault
public org.wpilib.hardware.hal.HALValue getDefault()Get the default value.- Returns:
HALValuedefault value.
-
createValue
public org.wpilib.hardware.hal.SimValue createValue(org.wpilib.hardware.hal.SimDevice device, org.wpilib.hardware.hal.SimDevice.Direction direction) Get theSimValuefor the sensor.- Parameters:
device-SimDeviceto create theSimValuefor.direction-SimDevice.Directionof theSimValue.- Returns:
SimValuefor the sensor.
-