Skip to content

Commit f8409f5

Browse files
committed
Merge pull request #201 from JLLeitschuh/fix/zeroFrameRate
Fix Divide by Zero Error in CameraSource
2 parents 2d0380b + 7ee0f71 commit f8409f5

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

core/src/main/java/edu/wpi/grip/core/sources/CameraSource.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public void start() throws IOException, IllegalStateException {
137137
}
138138

139139
final Thread frameExecutor = new Thread(() -> {
140-
long lastFrame = System.currentTimeMillis();
140+
long lastFrame = System.nanoTime();
141141
while (!Thread.interrupted()) {
142142
final Frame videoFrame;
143143
try {
@@ -154,8 +154,13 @@ public void start() throws IOException, IllegalStateException {
154154

155155
frameMat.copyTo(frameOutputSocket.getValue().get());
156156
frameOutputSocket.setValue(frameOutputSocket.getValue().get());
157-
long thisMoment = System.currentTimeMillis();
158-
frameRateOutputSocket.setValue(1000 / (thisMoment - lastFrame));
157+
158+
/*
159+
*
160+
*/
161+
final long thisMoment = System.nanoTime();
162+
final long elapsedTime = thisMoment - lastFrame;
163+
if (elapsedTime != 0) frameRateOutputSocket.setValue(1e9 / elapsedTime);
159164
lastFrame = thisMoment;
160165
}
161166
}, "Camera");

0 commit comments

Comments
 (0)