Skip to content

Commit 4f5c2de

Browse files
committed
Add setLoopTimestamp()
1 parent 504d9d3 commit 4f5c2de

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

wpilibc/src/main/native/cpp/TimedRobot.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void TimedRobot::StartCompetition() {
4646
if (currentTime.count() == 0 || status != 0) {
4747
break;
4848
}
49-
Timer::s_loopTimestamp = currentTime;
49+
Timer::SetLoopTimestamp(currentTime);
5050

5151
callback.func();
5252

wpilibc/src/main/native/include/frc/Timer.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ units::second_t GetTime();
3434
* the timer so Get() won't return a negative duration.
3535
*/
3636
class Timer {
37-
friend class TimedRobot;
38-
3937
public:
4038
/**
4139
* Create a new timer object.
@@ -132,16 +130,27 @@ class Timer {
132130

133131
/**
134132
* Return the system clock time in seconds for the start of the current
135-
* periodic loop. This is in the same time base as getFPGATimestamp(), but is
133+
* periodic loop. This is in the same time base as GetFPGATimestamp(), but is
136134
* stable through a loop. This value is only valid for robot programs that use
137135
* TimedRobot. It is updated at the beginning of every periodic callback
138-
* (including the normal periodic loop).
136+
* (including the normal periodic loop). Calling this from threads other than
137+
* than the main periodic loop has undefined behavior.
139138
*
140139
* @return Robot running time in seconds, as of the start of the current
141140
* periodic function.
142141
*/
143142
static units::second_t GetLoopTimestamp() { return s_loopTimestamp; }
144143

144+
/**
145+
* Sets the timestamp returned by GetLoopTimestamp(). Intended for library
146+
* use; calling this from team code may result in unexpected behavior.
147+
*
148+
* @param timestamp timestamp in seconds
149+
*/
150+
static void SetLoopTimestamp(units::second_t timestamp) {
151+
s_loopTimestamp = timestamp;
152+
}
153+
145154
/**
146155
* Return the approximate match time.
147156
*

wpilibj/src/main/java/edu/wpi/first/wpilibj/TimedRobot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void startCompetition() {
127127
if (currentTime == 0) {
128128
break;
129129
}
130-
Timer.s_loopTimestamp = currentTime / 1000000.0;
130+
Timer.setLoopTimestamp(currentTime / 1000000.0);
131131

132132
callback.func.run();
133133

wpilibj/src/main/java/edu/wpi/first/wpilibj/Timer.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* get() won't return a negative duration.
1212
*/
1313
public class Timer {
14-
static double s_loopTimestamp;
14+
private static double s_loopTimestamp;
1515

1616
/**
1717
* Return the system clock time in seconds. Return the time from the FPGA hardware clock in
@@ -27,14 +27,25 @@ public static double getFPGATimestamp() {
2727
* Return the system clock time in seconds for the start of the current periodic loop. This is
2828
* in the same time base as getFPGATimestamp(), but is stable through a loop. This value is only
2929
* valid for robot programs that use TimedRobot. It is updated at the beginning of every periodic
30-
* callback (including the normal periodic loop).
30+
* callback (including the normal periodic loop). Calling this from threads other than than the
31+
* main periodic loop has undefined behavior.
3132
*
3233
* @return Robot running time in seconds, as of the start of the current periodic function.
3334
*/
3435
public static double getLoopTimestamp() {
3536
return s_loopTimestamp;
3637
}
3738

39+
/**
40+
* Sets the timestamp returned by getLoopTimestamp(). Intended for library use; calling this from
41+
* team code may result in unexpected behavior.
42+
*
43+
* @param timestamp timestamp in seconds
44+
*/
45+
public static void setLoopTimestamp(double timestamp) {
46+
s_loopTimestamp = timestamp;
47+
}
48+
3849
/**
3950
* Return the approximate match time. The FMS does not send an official match time to the robots,
4051
* but does send an approximate match time. The value will count down the time remaining in the

0 commit comments

Comments
 (0)