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
  • Field Details

  • 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
    • 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's hardwareMap
      Returns:
      volts (a charged 12 V FTC battery reads about 12.5 to 13.5 V), or NaN
    • batteryReader

      public static DoubleSupplier batteryReader(com.qualcomm.robotcore.hardware.HardwareMap hardwareMap)
      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 every BATTERY_READ_SEC s 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 than BATTERY_STALE_SEC s (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's hardwareMap
      Returns:
      a reader to ask every loop: volts, or NaN (no reading yet, or too old)