RobotCore
1.0
WPILib port to raspberry Pi
Toggle main menu visibility
Loading...
Searching...
No Matches
Timer.java
1
// Copyright (c) FIRST and other WPILib contributors.
2
// Open Source Software; you can modify and/or share it under the terms of
3
// the WPILib BSD license file in the root directory of this project.
4
5
package
org.wpilib.system;
6
7
import
static
org.wpilib.units.Units.Seconds;
8
9
import
org.wpilib.driverstation.MatchState;
10
import
org.wpilib.units.measure.Time;
11
18
public
class
Timer
{
26
public
static
double
getTimestamp
() {
27
return
RobotController
.
getTime
() / 1_000_000_000.0;
28
}
29
35
public
static
double
getMonotonicTimestamp
() {
36
return
RobotController
.
getMonotonicTime
() / 1_000_000_000.0;
37
}
38
56
public
static
double
getMatchTime
() {
57
return
MatchState
.
getMatchTime
();
58
}
59
67
public
static
void
delay
(
final
Time period) {
68
delay
(period.in(Seconds));
69
}
70
78
public
static
void
delay
(
final
double
seconds) {
79
try
{
80
Thread.sleep((
long
) (seconds * 1e3));
81
}
catch
(
final
InterruptedException ex) {
82
Thread.currentThread().interrupt();
83
}
84
}
85
86
private
long
m_startTimeNs;
87
private
double
m_startTimeRemainderNs;
88
private
double
m_accumulatedTimeNs;
89
private
boolean
m_running;
90
96
public
Timer
() {
97
reset
();
98
}
99
112
public
static
Timer
createStarted
() {
113
var timer =
new
Timer
();
114
timer.start();
115
return
timer;
116
}
117
118
private
long
getNsClock() {
119
return
RobotController
.
getTime
();
120
}
121
122
private
double
getNanoseconds() {
123
if
(m_running) {
124
return
getNsClock() - m_startTimeNs - m_startTimeRemainderNs + m_accumulatedTimeNs;
125
}
else
{
126
return
m_accumulatedTimeNs;
127
}
128
}
129
137
public
double
get
() {
138
return
getNanoseconds() / 1_000_000_000.0;
139
}
140
146
public
final
void
reset
() {
147
m_accumulatedTimeNs = 0.0;
148
m_startTimeNs = getNsClock();
149
m_startTimeRemainderNs = 0.0;
150
}
151
157
public
void
start
() {
158
if
(!m_running) {
159
m_startTimeNs = getNsClock();
160
m_startTimeRemainderNs = 0.0;
161
m_running =
true
;
162
}
163
}
164
170
public
void
restart
() {
171
if
(m_running) {
172
stop
();
173
}
174
reset
();
175
start
();
176
}
177
183
public
void
stop
() {
184
if
(m_running) {
185
m_accumulatedTimeNs = getNanoseconds();
186
m_running =
false
;
187
}
188
}
189
196
public
boolean
hasElapsed
(Time period) {
197
return
hasElapsed
(period.in(Seconds));
198
}
199
206
public
boolean
hasElapsed
(
double
seconds) {
207
return
getNanoseconds() >= seconds * 1e9;
208
}
209
218
public
boolean
advanceIfElapsed
(
double
seconds) {
219
double
periodNs = seconds * 1e9;
220
221
if
(getNanoseconds() >= periodNs) {
222
// Advance the start time by the period.
223
// Don't set it to the current time... we want to avoid drift.
224
double
advanceNs = m_startTimeRemainderNs + periodNs;
225
long
wholeNs = (long) advanceNs;
226
m_startTimeNs += wholeNs;
227
m_startTimeRemainderNs = advanceNs - wholeNs;
228
return
true
;
229
}
else
{
230
return
false
;
231
}
232
}
233
239
public
boolean
isRunning
() {
240
return
m_running;
241
}
242
}
org.wpilib.driverstation.MatchState
Definition
MatchState.java:12
org.wpilib.driverstation.MatchState.getMatchTime
static double getMatchTime()
Definition
MatchState.java:32
org.wpilib.system.RobotController
Definition
RobotController.java:25
org.wpilib.system.RobotController.getTime
static long getTime()
Definition
RobotController.java:79
org.wpilib.system.RobotController.getMonotonicTime
static long getMonotonicTime()
Definition
RobotController.java:99
org.wpilib.system.Timer.createStarted
static Timer createStarted()
Definition
Timer.java:112
org.wpilib.system.Timer.getTimestamp
static double getTimestamp()
Definition
Timer.java:26
org.wpilib.system.Timer.hasElapsed
boolean hasElapsed(Time period)
Definition
Timer.java:196
org.wpilib.system.Timer.restart
void restart()
Definition
Timer.java:170
org.wpilib.system.Timer.delay
static void delay(final double seconds)
Definition
Timer.java:78
org.wpilib.system.Timer.getMatchTime
static double getMatchTime()
Definition
Timer.java:56
org.wpilib.system.Timer.getMonotonicTimestamp
static double getMonotonicTimestamp()
Definition
Timer.java:35
org.wpilib.system.Timer.reset
final void reset()
Definition
Timer.java:146
org.wpilib.system.Timer.start
void start()
Definition
Timer.java:157
org.wpilib.system.Timer.stop
void stop()
Definition
Timer.java:183
org.wpilib.system.Timer.Timer
Timer()
Definition
Timer.java:96
org.wpilib.system.Timer.isRunning
boolean isRunning()
Definition
Timer.java:239
org.wpilib.system.Timer.delay
static void delay(final Time period)
Definition
Timer.java:67
org.wpilib.system.Timer.advanceIfElapsed
boolean advanceIfElapsed(double seconds)
Definition
Timer.java:218
org.wpilib.system.Timer.hasElapsed
boolean hasElapsed(double seconds)
Definition
Timer.java:206
org
wpilib
system
Timer.java
Generated by
1.18.0