Class MatchTimer

java.lang.Object
com.crossbotics.ftchelper.match.MatchTimer

public class MatchTimer extends Object
Counts down a match period and runs "cues" at set times, for example a gamepad buzz with 20 seconds left, so drivers do not have to watch the field clock.

 MatchTimer timer = MatchTimer.teleOp()            // 2:00
         .rumbleAt(60, gamepad1, gamepad2)         // one minute left
         .rumbleAt(20, gamepad1, gamepad2);        // final 20 seconds
 waitForStart();
 timer.start();
 while (opModeIsActive()) {
     timer.update();                               // runs cues when their time comes
     t.line("Time", timer.format());               // "1:23"
 }
 

The field clock is the official one. This timer starts when YOUR OpMode starts, which can be a second off. Use it for reminders, not for split-second timing.

It never waits: update() only looks at the clock and returns. A cue is remembered as "the time left when it should happen", and each loop we check whether that time has come. This is the "remember WHEN, check every loop" idea from docs/guides/non-blocking.md. Docs: docs/modules/match.md (section "MatchTimer")

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    FTC autonomous length (seconds).
    static final double
    FTC driver-controlled (TeleOp) length (seconds).
  • Constructor Summary

    Constructors
    Constructor
    Description
    MatchTimer(double lengthSec)
    A timer for a period of this many seconds.
    MatchTimer(double lengthSec, LongSupplier clockNanos)
    For tests: your own clock.
  • Method Summary

    Modifier and Type
    Method
    Description
    static MatchTimer
    A 30-second autonomous timer.
    cueAt(double secondsLeft, Runnable action)
    Runs action once, when this many seconds are left.
    double
    Seconds since start (0 before).
    The time left as text, like the field clock: "1:23".
    boolean
    True when the time is up.
    boolean
    True after start().
    rumbleAt(double secondsLeft, com.qualcomm.robotcore.hardware.Gamepad... gamepads)
    Buzzes the gamepads three times when this many seconds are left.
    double
    Seconds left (the full length before start, never below 0).
    void
    Starts counting (call right after waitForStart()).
    static MatchTimer
    A 2-minute TeleOp timer.
    void
    Runs any cue whose time has come.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • AUTO_SECONDS

      public static final double AUTO_SECONDS
      FTC autonomous length (seconds).
      See Also:
    • TELEOP_SECONDS

      public static final double TELEOP_SECONDS
      FTC driver-controlled (TeleOp) length (seconds).
      See Also:
  • Constructor Details

    • MatchTimer

      public MatchTimer(double lengthSec)
      A timer for a period of this many seconds.
    • MatchTimer

      public MatchTimer(double lengthSec, LongSupplier clockNanos)
      For tests: your own clock.
  • Method Details

    • autonomous

      public static MatchTimer autonomous()
      A 30-second autonomous timer.
    • teleOp

      public static MatchTimer teleOp()
      A 2-minute TeleOp timer.
    • cueAt

      public MatchTimer cueAt(double secondsLeft, Runnable action)
      Runs action once, when this many seconds are left. The action must be quick (it runs inside your loop).
      Parameters:
      secondsLeft - seconds left on the clock, e.g. 20 for "final 20 seconds"
      action - the code to run, e.g. () -> led.flash() (() -> ... is a small piece of code we keep and run later)
      Returns:
      this timer, so you can chain more cues
    • rumbleAt

      public MatchTimer rumbleAt(double secondsLeft, com.qualcomm.robotcore.hardware.Gamepad... gamepads)
      Buzzes the gamepads three times when this many seconds are left. (Gamepad... gamepads means you can pass one gamepad, two, or more.)
    • start

      public void start()
      Starts counting (call right after waitForStart()). Starting again restarts it.
    • isRunning

      public boolean isRunning()
      True after start().
    • elapsedSec

      public double elapsedSec()
      Seconds since start (0 before).
    • secondsLeft

      public double secondsLeft()
      Seconds left (the full length before start, never below 0).
    • isOver

      public boolean isOver()
      True when the time is up.
    • update

      public void update()
      Runs any cue whose time has come. Call every loop. Cues already passed at start do not run.
    • format

      public String format()
      The time left as text, like the field clock: "1:23".