Class Hw
java.lang.Object
com.crossbotics.ftchelper.hardware.Hw
Gets devices from the robot configuration, with clear error messages.
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-
Method Summary
Modifier and TypeMethodDescriptionstatic <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.
-
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
-