java.lang.Object
com.crossbotics.ftchelper.hardware.Hw

public final class Hw extends Object
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 Type
    Method
    Description
    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.
    static String
    missingMessage(com.qualcomm.robotcore.hardware.HardwareMap hardwareMap, Class<?> type, String name)
    The error text for a missing device.
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 for DcMotorEx.class gives you back a DcMotorEx with no extra conversion.

      Parameters:
      hardwareMap - the OpMode's hardwareMap
      type - the device type, e.g. DcMotorEx.class
      name - 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's hardwareMap
      type - the device type, e.g. DistanceSensor.class
      name - 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's hardwareMap, used to list the names that DO exist
      type - the device type that was asked for
      name - the name that was not found
      Returns:
      a message that names the missing device and lists the real names of that type