Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wpilib] Timer: Add getLoopTimestamp() #7364

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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::SetLoopTimestamp(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
24 changes: 24 additions & 0 deletions wpilibc/src/main/native/include/frc/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,29 @@ 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). Calling this from threads other than
* than the main periodic loop has undefined behavior.
*
* @return Robot running time in seconds, as of the start of the current
* periodic function.
*/
static units::second_t GetLoopTimestamp() { return s_loopTimestamp; }

/**
* Sets the timestamp returned by GetLoopTimestamp(). Intended for library
* use; calling this from team code may result in unexpected behavior.
*
* @param timestamp timestamp in seconds
*/
static void SetLoopTimestamp(units::second_t timestamp) {
s_loopTimestamp = timestamp;
}

/**
* Return the approximate match time.
*
Expand All @@ -146,6 +169,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.setLoopTimestamp(currentTime / 1000000.0);

callback.func.run();

Expand Down
25 changes: 25 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 {
private 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,29 @@ 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). Calling this from threads other than than the
* main periodic loop has undefined behavior.
*
* @return Robot running time in seconds, as of the start of the current periodic function.
*/
public static double getLoopTimestamp() {
return s_loopTimestamp;
}

/**
* Sets the timestamp returned by getLoopTimestamp(). Intended for library use; calling this from
* team code may result in unexpected behavior.
*
* @param timestamp timestamp in seconds
*/
public static void setLoopTimestamp(double timestamp) {
s_loopTimestamp = timestamp;
}

/**
* 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
Loading