RobotCore 1.0
WPILib port to raspberry Pi
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
5package org.wpilib.system;
6
7import static org.wpilib.units.Units.Seconds;
8
9import org.wpilib.driverstation.MatchState;
10import org.wpilib.units.measure.Time;
11
18public 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}
static Timer createStarted()
Definition Timer.java:112
static double getTimestamp()
Definition Timer.java:26
boolean hasElapsed(Time period)
Definition Timer.java:196
static void delay(final double seconds)
Definition Timer.java:78
static double getMatchTime()
Definition Timer.java:56
static double getMonotonicTimestamp()
Definition Timer.java:35
final void reset()
Definition Timer.java:146
static void delay(final Time period)
Definition Timer.java:67
boolean advanceIfElapsed(double seconds)
Definition Timer.java:218
boolean hasElapsed(double seconds)
Definition Timer.java:206