Class Hw
The SDK's hardwareMap.get(...) gives a short error when a name is wrong.
get(com.qualcomm.robotcore.hardware.HardwareMap, java.lang.Class<? extends T>, java.lang.String) tells you which names DO exist, so you can spot the typo fast.
The "robot configuration" is the list of names you typed on the Driver Station for each motor, servo and sensor port. The code must use exactly the same names.
DcMotorEx intake = Hw.get(hardwareMap, DcMotorEx.class, "intake");
DistanceSensor dist = Hw.tryGet(hardwareMap, DistanceSensor.class, "dist"); // null if not there
Docs: docs/modules/hardware.md-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final doubleHow oftenbatteryReader(com.qualcomm.robotcore.hardware.HardwareMap)asks the hubs again, seconds.static final doubleAbatteryReader(com.qualcomm.robotcore.hardware.HardwareMap)answer older than this (seconds) is not used (NaN). -
Method Summary
Modifier and TypeMethodDescriptionstatic DoubleSupplierbatteryReader(com.qualcomm.robotcore.hardware.HardwareMap hardwareMap) The battery's voltage for code that runs every loop: a reader that never waits.static doublebatteryVolts(com.qualcomm.robotcore.hardware.HardwareMap hardwareMap) The battery's voltage now, from the hubs' voltage sensors (the lowest reading, as the Characterizer does: every hub is on the same battery).static <T> TGets a device by its configuration name, or stops the OpMode with a helpful message.static StringmissingMessage(com.qualcomm.robotcore.hardware.HardwareMap hardwareMap, Class<?> type, String name) The error text for a missing device.static <T> TtryGet(com.qualcomm.robotcore.hardware.HardwareMap hardwareMap, Class<? extends T> type, String name) Gets a device if it is in the configuration, else returns null.
-
Field Details
-
BATTERY_READ_SEC
public static final double BATTERY_READ_SECHow oftenbatteryReader(com.qualcomm.robotcore.hardware.HardwareMap)asks the hubs again, seconds.- See Also:
-
BATTERY_STALE_SEC
public static final double BATTERY_STALE_SECAbatteryReader(com.qualcomm.robotcore.hardware.HardwareMap)answer older than this (seconds) is not used (NaN).- See Also:
-
-
Method Details
-
get
public static <T> T get(com.qualcomm.robotcore.hardware.HardwareMap hardwareMap, Class<? extends T> type, String name) Gets a device by its configuration name, or stops the OpMode with a helpful message.Stopping right away in INIT is on purpose: a robot with a missing motor should not start a match and fail in a strange way later.
<T>is a "type placeholder" (Java calls it a generic): it stands for whatever device type you ask for, so asking forDcMotorEx.classgives you back aDcMotorExwith no extra conversion.- Parameters:
hardwareMap- the OpMode'shardwareMaptype- the device type, e.g.DcMotorEx.classname- the name in the Driver Station configuration (exact, case matters)- Returns:
- the device
- Throws:
IllegalArgumentException- if there is no device of that type with that name
-
tryGet
public static <T> T tryGet(com.qualcomm.robotcore.hardware.HardwareMap hardwareMap, Class<? extends T> type, String name) Gets a device if it is in the configuration, else returns null. Use it for parts that are not on every robot (for example a test sensor). Always check for null before you use it.- Parameters:
hardwareMap- the OpMode'shardwareMaptype- the device type, e.g.DistanceSensor.classname- the name in the Driver Station configuration (exact, case matters)- Returns:
- the device, or null ("nothing") if there is none with that type and name
-
missingMessage
public static String missingMessage(com.qualcomm.robotcore.hardware.HardwareMap hardwareMap, Class<?> type, String name) The error text for a missing device. Public so tests and tools can show it too.- Parameters:
hardwareMap- the OpMode'shardwareMap, used to list the names that DO existtype- the device type that was asked forname- the name that was not found- Returns:
- a message that names the missing device and lists the real names of that type
-
batteryVolts
public static double batteryVolts(com.qualcomm.robotcore.hardware.HardwareMap hardwareMap) The battery's voltage now, from the hubs' voltage sensors (the lowest reading, as the Characterizer does: every hub is on the same battery). NaN if there is no voltage sensor (or none reads).Each call asks every hub once and WAITS for the answers (a few milliseconds): never call it in the loop. For a reading in the loop use
batteryReader(HardwareMap), which asks on the library's helper thread.telemetry.addData("Battery", Hw.batteryVolts(hardwareMap)); // in INIT, once- Parameters:
hardwareMap- the OpMode'shardwareMap- Returns:
- volts (a charged 12 V FTC battery reads about 12.5 to 13.5 V), or NaN
-
batteryReader
The battery's voltage for code that runs every loop: a reader that never waits. Each time you ask it (getAsDouble()) it answers at once with the latest reading, and at most everyBATTERY_READ_SECs it asks the hubs again on the library's helper thread (BackgroundWorker), so the slow hub question never holds up the loop. NaN before the first answer, with no voltage sensor, or when the last answer is older thanBATTERY_STALE_SECs (the helper thread is stuck): then use nothing.flywheel.batteryCompensation(Hw.batteryReader(hardwareMap), 12.6); // 12.6 V: from the Flywheel Finder- Parameters:
hardwareMap- the OpMode'shardwareMap- Returns:
- a reader to ask every loop: volts, or NaN (no reading yet, or too old)
-