Skip to content

Commit

Permalink
[wpilib] Add Timer.isRunning() method (wpilibsuite#7220)
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebeaulac authored Oct 18, 2024
1 parent 0424e5b commit 796dbd3
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions wpilibc/src/main/native/cpp/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ bool Timer::AdvanceIfElapsed(units::second_t period) {
}
}

bool Timer::IsRunning() const {
return m_running;
}

units::second_t Timer::GetFPGATimestamp() {
// FPGA returns the timestamp in microseconds
return units::second_t{frc::RobotController::GetFPGATime() * 1.0e-6};
Expand Down
7 changes: 7 additions & 0 deletions wpilibc/src/main/native/include/frc/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ class Timer {
*/
bool AdvanceIfElapsed(units::second_t period);

/**
* Whether the timer is currently running.
*
* @return true if running.
*/
bool IsRunning() const;

/**
* Return the FPGA system clock time in seconds.
*
Expand Down
4 changes: 4 additions & 0 deletions wpilibc/src/test/native/cpp/TimerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,22 @@ TEST_F(TimerTest, StartStop) {

// Verify timer is initialized as stopped
EXPECT_EQ(timer.Get(), 0_s);
EXPECT_FALSE(timer.IsRunning());
frc::sim::StepTiming(500_ms);
EXPECT_EQ(timer.Get(), 0_s);
EXPECT_FALSE(timer.IsRunning());

// Verify timer increments after it's started
timer.Start();
frc::sim::StepTiming(500_ms);
EXPECT_EQ(timer.Get(), 500_ms);
EXPECT_TRUE(timer.IsRunning());

// Verify timer stops incrementing after it's stopped
timer.Stop();
frc::sim::StepTiming(500_ms);
EXPECT_EQ(timer.Get(), 500_ms);
EXPECT_FALSE(timer.IsRunning());
}

TEST_F(TimerTest, Reset) {
Expand Down
9 changes: 9 additions & 0 deletions wpilibj/src/main/java/edu/wpi/first/wpilibj/Timer.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,13 @@ public boolean advanceIfElapsed(double seconds) {
return false;
}
}

/**
* Whether the timer is currently running.
*
* @return true if running.
*/
public boolean isRunning() {
return m_running;
}
}
4 changes: 4 additions & 0 deletions wpilibj/src/test/java/edu/wpi/first/wpilibj/TimerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,22 @@ void startStopTest() {

// Verify timer is initialized as stopped
assertEquals(timer.get(), 0.0);
assertFalse(timer.isRunning());
SimHooks.stepTiming(0.5);
assertEquals(timer.get(), 0.0);
assertFalse(timer.isRunning());

// Verify timer increments after it's started
timer.start();
SimHooks.stepTiming(0.5);
assertEquals(timer.get(), 0.5, 1e-9);
assertTrue(timer.isRunning());

// Verify timer stops incrementing after it's stopped
timer.stop();
SimHooks.stepTiming(0.5);
assertEquals(timer.get(), 0.5, 1e-9);
assertFalse(timer.isRunning());
}

@Test
Expand Down

0 comments on commit 796dbd3

Please sign in to comment.