Skip to content

Commit

Permalink
[wpilib] Timer: Add getLoopTimestamp()
Browse files Browse the repository at this point in the history
This is updated at the start of each periodic function in TimedRobot, so is
stable throughout the callback function execution.
  • Loading branch information
PeterJohnson committed Nov 8, 2024
1 parent 661bae5 commit 504d9d3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions wpilibc/src/main/native/cpp/TimedRobot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <hal/Notifier.h>

#include "frc/Errors.h"
#include "frc/Timer.h"

using namespace frc;

Expand Down Expand Up @@ -45,6 +46,7 @@ void TimedRobot::StartCompetition() {
if (currentTime.count() == 0 || status != 0) {
break;
}
Timer::s_loopTimestamp = currentTime;

callback.func();

Expand Down
2 changes: 2 additions & 0 deletions wpilibc/src/main/native/cpp/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ units::second_t GetTime() {

using namespace frc;

units::second_t Timer::s_loopTimestamp = 0_s;

Timer::Timer() {
Reset();
}
Expand Down
15 changes: 15 additions & 0 deletions wpilibc/src/main/native/include/frc/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ units::second_t GetTime();
* the timer so Get() won't return a negative duration.
*/
class Timer {
friend class TimedRobot;

public:
/**
* Create a new timer object.
Expand Down Expand Up @@ -128,6 +130,18 @@ class Timer {
*/
static units::second_t GetFPGATimestamp();

/**
* Return the system clock time in seconds for the start of the current
* periodic loop. This is in the same time base as getFPGATimestamp(), but is
* stable through a loop. This value is only valid for robot programs that use
* TimedRobot. It is updated at the beginning of every periodic callback
* (including the normal periodic loop).
*
* @return Robot running time in seconds, as of the start of the current
* periodic function.
*/
static units::second_t GetLoopTimestamp() { return s_loopTimestamp; }

/**
* Return the approximate match time.
*
Expand All @@ -146,6 +160,7 @@ class Timer {
static units::second_t GetMatchTime();

private:
static units::second_t s_loopTimestamp;
units::second_t m_startTime = 0_s;
units::second_t m_accumulatedTime = 0_s;
bool m_running = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public void startCompetition() {
if (currentTime == 0) {
break;
}
Timer.s_loopTimestamp = currentTime / 1000000.0;

callback.func.run();

Expand Down
14 changes: 14 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 @@ -11,6 +11,8 @@
* get() won't return a negative duration.
*/
public class Timer {
static double s_loopTimestamp;

/**
* Return the system clock time in seconds. Return the time from the FPGA hardware clock in
* seconds since the FPGA started.
Expand All @@ -21,6 +23,18 @@ public static double getFPGATimestamp() {
return RobotController.getFPGATime() / 1000000.0;
}

/**
* Return the system clock time in seconds for the start of the current periodic loop. This is
* in the same time base as getFPGATimestamp(), but is stable through a loop. This value is only
* valid for robot programs that use TimedRobot. It is updated at the beginning of every periodic
* callback (including the normal periodic loop).
*
* @return Robot running time in seconds, as of the start of the current periodic function.
*/
public static double getLoopTimestamp() {
return s_loopTimestamp;
}

/**
* Return the approximate match time. The FMS does not send an official match time to the robots,
* but does send an approximate match time. The value will count down the time remaining in the
Expand Down

0 comments on commit 504d9d3

Please sign in to comment.