Skip to content

Commit 61c1a4f

Browse files
authored
Upgrade to ZED SDK 5.x (#57)
1 parent 2781d7e commit 61c1a4f

File tree

4 files changed

+46
-98
lines changed

4 files changed

+46
-98
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ mainDependencies {
7171
api("us.ihmc:ffmpeg:$ffmpegVersion:windows-x86_64")
7272

7373
// ZED SDK for logging remote ZED data streams
74-
api("us.ihmc:zed-java-api:4.2.0_5")
74+
api("us.ihmc:zed-java-api:5.0.0")
7575

7676
api("org.freedesktop.gstreamer:gst1-java-core:1.4.0")
7777

src/main/java/us/ihmc/robotDataLogger/example/ExampleSVOLogger.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static void main(String[] args)
4949
String path = System.getProperty("user.home") + "/Desktop/test" + UUID.randomUUID().toString().substring(0, 5);
5050
String svoFile = path + ".svo2";
5151
String datFile = path + ".dat";
52-
SVO_LOGGER.start(svoFile, datFile, ADDRESS, PORT, 15, 8000, 0L, 0L);
52+
SVO_LOGGER.connect(svoFile, datFile, ADDRESS, PORT, 15, 8000, 0L, 0L);
5353

5454
/*
5555
Do nothing forever, everything else runs in other threads
@@ -61,7 +61,7 @@ public static void destroy()
6161
{
6262
running = false;
6363

64-
SVO_LOGGER.stop();
64+
SVO_LOGGER.close();
6565

6666
stopLocalUSBSensor();
6767
}
Lines changed: 38 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package us.ihmc.robotDataLogger.logger;
22

3-
import us.ihmc.commons.Conversions;
43
import us.ihmc.commons.exception.DefaultExceptionHandler;
54
import us.ihmc.commons.exception.ExceptionTools;
65
import us.ihmc.commons.thread.RepeatingTaskThread;
76
import us.ihmc.commons.thread.ThreadTools;
87
import us.ihmc.log.LogTools;
98
import us.ihmc.zed.SL_InitParameters;
109
import us.ihmc.zed.SL_RuntimeParameters;
10+
import us.ihmc.zed.ZEDTools;
1111
import us.ihmc.zed.global.zed;
1212

1313
import java.io.FileWriter;
@@ -21,34 +21,34 @@
2121
*/
2222
public class ZEDSVOLogger
2323
{
24-
private static final double CONNECT_TIMEOUT = 2.0;
2524
private static final boolean TRANSCODE = false;
2625

27-
private static int nextCameraId = 10;
26+
private static int nextCameraId = 0;
2827

2928
private final int cameraID = nextCameraId++;
3029
private SL_InitParameters initParameters;
3130
private SL_RuntimeParameters runtimeParameters;
3231
private final RepeatingTaskThread grabThread = new RepeatingTaskThread(getClass().getName() + "GrabThread", this::grab);
33-
private final RepeatingTaskThread connectionWatchdogThread = new RepeatingTaskThread(getClass().getName() + "ConnectionWatchdog", this::connectionCheck);
3432

3533
private String svoPrefix;
3634
private long controllerZeroInSensorFrame;
3735
private FileWriter timestampWriter;
3836

39-
private volatile double lastGrabTime;
40-
private volatile boolean stopRequested;
41-
private volatile boolean completelyStopped;
42-
private volatile boolean failedBeyondRecovery;
37+
private volatile boolean closed;
4338

44-
public void start(String svoFile, String datFile, String address, int port, int fps, int bitrate, long sensorTimestamp, long controllerTimestamp)
39+
public void connect(String svoFile, String datFile, String address, int port, int fps, int bitrate, long sensorTimestamp, long controllerTimestamp)
4540
{
46-
if (stopRequested)
47-
throw new IllegalStateException("Cannot restart ZEDSVOLogger once stopped");
41+
closed = false;
4842

49-
String[] parts = svoFile.split("[/\\\\]");
50-
svoPrefix = parts[parts.length - 1].substring(0, "yyyyMMdd_HHmmss".length());
51-
timestampWriter = ExceptionTools.handle(() -> new FileWriter(datFile, true), DefaultExceptionHandler.RUNTIME_EXCEPTION);
43+
try {
44+
String[] parts = svoFile.split("[/\\\\]");
45+
svoPrefix = parts[parts.length - 1].substring(0, "yyyyMMdd_HHmmss".length());
46+
timestampWriter = ExceptionTools.handle(() -> new FileWriter(datFile, true), DefaultExceptionHandler.RUNTIME_EXCEPTION);
47+
}
48+
catch (Exception ignored)
49+
{
50+
// If the svoFile is not in standard logger format
51+
}
5252

5353
initParameters = new SL_InitParameters();
5454
initParameters.input_type(zed.SL_INPUT_TYPE_STREAM);
@@ -67,36 +67,27 @@ public void start(String svoFile, String datFile, String address, int port, int
6767

6868
int returnCode = sl_open_camera(cameraID, initParameters, 0, "", address, port, "", "", "");
6969
if (returnCode != SL_ERROR_CODE_SUCCESS)
70-
LogTools.error("ZED SDK error code: " + returnCode);
70+
LogTools.error("Could not connect to ZED SDK stream: " + ZEDTools.errorMessage(returnCode));
7171

7272
returnCode = sl_enable_recording(cameraID, svoFile, SL_SVO_COMPRESSION_MODE_H264, bitrate, fps, TRANSCODE);
7373
if (returnCode != SL_ERROR_CODE_SUCCESS)
74-
LogTools.error("ZED SDK error code: " + returnCode);
74+
LogTools.error("Could not enable SVO recording: " + ZEDTools.errorMessage(returnCode));
7575

7676
if (sl_is_opened(cameraID))
7777
{
7878
LogTools.info("Connected to ZED SDK stream on: " + address + ":" + port);
7979

80-
grabThread.setFrequencyLimit(RepeatingTaskThread.UNLIMITED_FREQUENCY);
8180
grabThread.startRepeating();
8281
}
83-
84-
connectionWatchdogThread.setFrequencyLimit(Conversions.secondsToHertz(CONNECT_TIMEOUT));
85-
ThreadTools.startAThread(() ->
86-
{
87-
ThreadTools.park(5.0);
88-
connectionWatchdogThread.startRepeating();
89-
}, getClass().getSimpleName() + "ConnectionWatchdogDelay");
9082
}
9183

92-
public void stop()
84+
public void close()
9385
{
94-
if (!stopRequested)
86+
if (!closed)
9587
{
96-
stopRequested = true;
88+
closed = true;
9789

9890
grabThread.blockingKill();
99-
connectionWatchdogThread.kill();
10091

10192
sl_close_camera(cameraID);
10293
sl_unload_instance(cameraID);
@@ -108,83 +99,41 @@ public void stop()
10899
System.out.println("Closing ZED SDK stream");
109100

110101
ExceptionTools.handle(timestampWriter::close, DefaultExceptionHandler.PRINT_MESSAGE);
111-
112-
completelyStopped = true;
113102
}
114103
}
115104

116105
public void grab()
117106
{
118-
if (stopRequested)
119-
throw new IllegalStateException("Cannot grab(), already stopped");
120-
121-
int returnCode = sl_grab(cameraID, runtimeParameters);
122-
123-
lastGrabTime = System.currentTimeMillis() / 1000D;
124-
125-
try
126-
{
127-
// We assume both the sensor on board & controller real time thread clocks
128-
// run at the same speed to try an resolve delay and time stretching issues.
129-
// Here, controllerZeroInSensorFrame is calculated from two timestamps taken
130-
// on the robot at the moment both the sensor & controller are running
131-
long sensorTimestamp = sl_get_current_timestamp(cameraID);
132-
long controllerTimestamp = sensorTimestamp - controllerZeroInSensorFrame;
133-
timestampWriter.write("%d %d %s%n".formatted(controllerTimestamp, sensorTimestamp, svoPrefix));
134-
}
135-
catch (IOException e)
107+
if (!closed)
136108
{
137-
LogTools.error(e.getMessage());
138-
}
139-
140-
if (returnCode == SL_ERROR_CODE_FAILURE || returnCode == SL_ERROR_CODE_CAMERA_NOT_DETECTED)
141-
failedBeyondRecovery = true;
109+
int returnCode = sl_grab(cameraID, runtimeParameters);
142110

143-
if (returnCode == SL_ERROR_CODE_CAMERA_NOT_INITIALIZED || returnCode == SL_ERROR_CODE_CAMERA_REBOOTING)
144-
stop();
145-
}
146-
147-
private void connectionCheck()
148-
{
149-
if (!stopRequested())
150-
{
151-
if (!sl_is_opened(cameraID))
111+
try
112+
{
113+
// We assume both the sensor on board & controller real time thread clocks
114+
// run at the same speed to try an resolve delay and time stretching issues.
115+
// Here, controllerZeroInSensorFrame is calculated from two timestamps taken
116+
// on the robot at the moment both the sensor & controller are running
117+
long sensorTimestamp = sl_get_current_timestamp(cameraID);
118+
long controllerTimestamp = sensorTimestamp - controllerZeroInSensorFrame;
119+
timestampWriter.write("%d %d %s%n".formatted(controllerTimestamp, sensorTimestamp, svoPrefix));
120+
}
121+
catch (IOException ignored)
152122
{
153-
LogTools.info("Unable to connect to ZED SDK stream");
154-
155-
stop();
156123
}
157124

158-
if ((System.currentTimeMillis() / 1000D) - lastGrabTime > CONNECT_TIMEOUT)
125+
if (returnCode != SL_ERROR_CODE_SUCCESS)
159126
{
160-
LogTools.info("grab() timeout reached, disconnecting from ZED SDK stream");
127+
// Wait some time before trying to grab again
128+
ThreadTools.park(5.0);
161129

162-
stop();
130+
LogTools.info("Could not grab image from ZED, trying again in a few seconds...");
163131
}
164132
}
165133
}
166134

167-
/**
168-
* @return true if stop() has been called, false if not
169-
*/
170-
public boolean stopRequested()
171-
{
172-
return stopRequested;
173-
}
174-
175-
/**
176-
* @return true if ZED SDK has completely closed the camera and stop() has completely finished
177-
*/
178-
public boolean completelyStopped()
179-
{
180-
return completelyStopped;
181-
}
182-
183-
/**
184-
* @return true if we received an error code from ZED SDK that is likely to cause a crash
185-
*/
186-
public boolean failedBeyondRecovery()
135+
public boolean isClosed()
187136
{
188-
return failedBeyondRecovery;
137+
return closed;
189138
}
190139
}

src/main/java/us/ihmc/robotDataLogger/logger/ZEDSVOLoggerManager.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ private void onZEDSDKAnnounceMessage(ZEDSDKAnnounce message)
6868
return;
6969
}
7070
}
71-
catch (ArrayIndexOutOfBoundsException e)
71+
catch (ArrayIndexOutOfBoundsException ignored)
7272
{
73-
return;
7473
}
7574

7675
ZEDSDKAnnounceHash announceHash = new ZEDSDKAnnounceHash(message.getAddressAsString(), message.getPort());
@@ -79,7 +78,7 @@ private void onZEDSDKAnnounceMessage(ZEDSDKAnnounce message)
7978
{
8079
ZEDSVOLogger zedSVOLogger = zedLoggers.get(announceHash);
8180

82-
if (zedSVOLogger.completelyStopped() && !zedSVOLogger.failedBeyondRecovery())
81+
if (zedSVOLogger.isClosed())
8382
{
8483
zedLoggers.remove(announceHash);
8584
}
@@ -89,12 +88,12 @@ private void onZEDSDKAnnounceMessage(ZEDSDKAnnounce message)
8988
File perceptionDir = new File(tempDirectory, "perception");
9089
perceptionDir.mkdirs();
9190
String svoFile = perceptionDir.getAbsolutePath() + File.separator + generateSVOFileName(message);
92-
String datFile = perceptionDir.getAbsolutePath() + File.separator +
91+
String datFile = perceptionDir.getAbsolutePath() + File.separator +
9392
"%s%s".formatted(message.getSensorNameAsString(), VideoDataLoggerInterface.timestampDataPostfix);
9493

9594
ZEDSVOLogger zedSVOLogger = new ZEDSVOLogger();
9695

97-
zedSVOLogger.start(svoFile, datFile,
96+
zedSVOLogger.connect(svoFile, datFile,
9897
message.getAddressAsString(),
9998
message.getPort(),
10099
message.getFps(),
@@ -108,7 +107,7 @@ private void onZEDSDKAnnounceMessage(ZEDSDKAnnounce message)
108107

109108
public void destroy()
110109
{
111-
zedLoggers.forEach((hostInstanceID, zedSVOLogger) -> zedSVOLogger.stop());
110+
zedLoggers.forEach((hostInstanceID, zedSVOLogger) -> zedSVOLogger.close());
112111

113112
ros2Node.destroy();
114113
}

0 commit comments

Comments
 (0)