Skip to content

Replace XTimer with WPILib Timer and SimHooks - #697

Open
NormalDuck wants to merge 1 commit into
mainfrom
refactor/wpilib-timer
Open

NormalDuck wants to merge 1 commit into
mainfrom
refactor/wpilib-timer

Conversation

@NormalDuck

Copy link
Copy Markdown
Contributor

Why are we doing this?

XTimer was a static facade over a globally installed XTimerImpl, with a MockTimer for tests and a TimerWpiAdapter for the robot. WPILib already provides both halves: Timer for reading time and SimHooks for controlling simulated time in tests. Keeping our own copy meant extra Dagger bindings, extra setup in every test base class, and one more indirection on every timestamp read in the robot loop.

Whats changing?

  • Deleted XTimer, XTimerImpl, XSettableTimerImpl, MockTimer, TimerWpiAdapter, and MockTimerTest.
  • Removed the timer bindings from RobotModule, SimulationModule, and UnitTestModule, the accessor on BaseComponent, and the timer wiring in BaseRobot.
  • Library code that called XTimer.getFPGATimestamp() now calls Timer.getMonotonicTimestamp().
  • BaseWPITest pauses WPILib's simulated clock in setup and resumes it in teardown. Tests advance time with SimHooks.stepTiming(seconds) instead of timer.advanceTimeInSecondsBy(...) / setTimeInSeconds(...).
  • Updated AGENTS.md and the FSM ReadMe to describe the new test-time pattern.

This is a breaking change for robot projects: anything that uses XTimer or MockTimer will need the same migration.

Questions/notes for reviewers

  • The simulated clock does not start at zero and is not reset between tests, so tests that used to assert absolute times now measure against a timestamp captured in the test (HistoryBufferTests, FiniteStateMachineReadMeExampleTest).
  • FiniteStateMachineReadMeExampleTest.isFinishedOnlyOnceFinalAnalysisIsReached had its total-duration tolerance widened from one loop tick to three. The likely cause, not confirmed, is that the sim clock lands on exact threshold values where MockTimer's accumulated doubles tipped over slightly early, so each of the three timed states can take one extra tick. The per-phase duration tests still pass at the original one-tick tolerance.
  • In BaseWPITest.setUp, createDaggerComponent() runs before the clock is paused. Nothing depends on this today, but it could be reordered.

How this was tested

  • unit tests added
  • tested on robot

Existing tests were migrated rather than new ones added; ./gradlew build passes locally (tests, Checkstyle, Spotless). Not run on a robot.

🤖 Generated with Claude Code

Remove the XTimer facade, its implementations, and the Dagger timer
bindings. Library code reads Timer.getMonotonicTimestamp() directly, and
tests control time through WPILib's simulated clock: BaseWPITest pauses
it and tests advance it with SimHooks.stepTiming().

Co-Authored-By: Claude <noreply@anthropic.com>
@NormalDuck
NormalDuck requested a review from a team as a code owner November 16, 2026 04:50
@github-actions

Copy link
Copy Markdown

JUnit results

 72 files   - 1   72 suites   - 1   3s ⏱️ -1s
448 tests  - 3  445 ✅  - 2  3 💤  - 1  0 ❌ ±0 
473 runs   - 3  470 ✅  - 2  3 💤  - 1  0 ❌ ±0 

Results for commit 443401e. ± Comparison against base commit c0e2ecf.

This pull request removes 9 and adds 6 tests. Note that renamed tests count towards both.
xbot.common.injection.MockTimerTest ‑ test_command_timed_out()
xbot.common.injection.MockTimerTest ‑ test_starts_zero()
xbot.common.injection.MockTimerTest ‑ updated_by_mock()
xbot.common.injection.TestDevicePolice ‑ [1] xbot.common.logging.assertion.LoudRobotAssertionHandler@15ad5acb
xbot.common.injection.TestDevicePolice ‑ [1] xbot.common.logging.assertion.LoudRobotAssertionHandler@3155f190
xbot.common.injection.TestDevicePolice ‑ [1] xbot.common.logging.assertion.LoudRobotAssertionHandler@4b56b517
xbot.common.injection.TestDevicePolice ‑ [2] xbot.common.injection.TestDevicePolice$RecordingAssertions@30665461
xbot.common.injection.TestDevicePolice ‑ [2] xbot.common.injection.TestDevicePolice$RecordingAssertions@351e86b2
xbot.common.injection.TestDevicePolice ‑ [2] xbot.common.injection.TestDevicePolice$RecordingAssertions@7323c38c
xbot.common.injection.TestDevicePolice ‑ [1] xbot.common.logging.assertion.LoudRobotAssertionHandler@35ff072c
xbot.common.injection.TestDevicePolice ‑ [1] xbot.common.logging.assertion.LoudRobotAssertionHandler@4dffff9
xbot.common.injection.TestDevicePolice ‑ [1] xbot.common.logging.assertion.LoudRobotAssertionHandler@66abb2fa
xbot.common.injection.TestDevicePolice ‑ [2] xbot.common.injection.TestDevicePolice$RecordingAssertions@4bca8eaf
xbot.common.injection.TestDevicePolice ‑ [2] xbot.common.injection.TestDevicePolice$RecordingAssertions@4ebd8d2
xbot.common.injection.TestDevicePolice ‑ [2] xbot.common.injection.TestDevicePolice$RecordingAssertions@7b8cb283

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant