Skip to content

Commit 005dc5d

Browse files
calvertdwihmc-rosie
andauthored
Support ZED in SCS 2 log visualizer (#46)
* Add sensor name to ZED announce. * Start setting up ZED dat file. * Write timestamp .dat file. * Remove timebase and add svo filename to dat * Change to just the svo prefix to save disk space. * Fix compile error. * Fix dat file path errors. -Duncan * Pack timestamp from ZED. * Add cleanup code. --------- Co-authored-by: ihmc-rosie <[email protected]>
1 parent c4f4783 commit 005dc5d

File tree

6 files changed

+61
-9
lines changed

6 files changed

+61
-9
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ public static void main(String[] args)
4646
/*
4747
Connect and start logging the SVO
4848
*/
49-
String svoFile = System.getProperty("user.home") + "/Desktop/test" + UUID.randomUUID().toString().substring(0, 5) + ".svo2";
50-
SVO_LOGGER.start(svoFile, ADDRESS, PORT);
49+
String path = System.getProperty("user.home") + "/Desktop/test" + UUID.randomUUID().toString().substring(0, 5);
50+
String svoFile = path + ".svo2";
51+
String datFile = path + ".dat";
52+
SVO_LOGGER.start(svoFile, datFile, System::nanoTime, ADDRESS, PORT);
5153

5254
/*
5355
Do nothing forever, everything else runs in other threads

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.bytedeco.javacv.FFmpegFrameGrabber;
44
import org.bytedeco.javacv.Frame;
55
import org.bytedeco.javacv.FrameGrabber;
6+
import us.ihmc.log.LogTools;
67

78
import java.io.File;
89

@@ -80,4 +81,16 @@ public double getFrameRate()
8081
{
8182
return grabber.getVideoFrameRate();
8283
}
84+
85+
public void stop()
86+
{
87+
try
88+
{
89+
grabber.stop();
90+
}
91+
catch (FFmpegFrameGrabber.Exception e)
92+
{
93+
LogTools.error(e.getMessage());
94+
}
95+
}
8396
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ else if (!Files.isDirectory(logDirectory))
8585
throw e;
8686
}
8787

88-
zedSVOLoggerManager = new ZEDSVOLoggerManager(tempDirectory, finalDirectory);
88+
zedSVOLoggerManager = new ZEDSVOLoggerManager(tempDirectory, finalDirectory, logger::getLastReceivedTimestamp);
8989
}
9090

9191
public void destroy()

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,4 +673,9 @@ private static String toString(Announcement announcement)
673673

674674
return builder.toString();
675675
}
676+
677+
public long getLastReceivedTimestamp()
678+
{
679+
return lastReceivedTimestamp;
680+
}
676681
}

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package us.ihmc.robotDataLogger.logger;
22

33
import us.ihmc.commons.Conversions;
4+
import us.ihmc.commons.exception.DefaultExceptionHandler;
5+
import us.ihmc.commons.exception.ExceptionTools;
46
import us.ihmc.commons.thread.RepeatingTaskThread;
57
import us.ihmc.commons.thread.ThreadTools;
68
import us.ihmc.log.LogTools;
79
import us.ihmc.zed.SL_InitParameters;
810
import us.ihmc.zed.SL_RuntimeParameters;
911
import us.ihmc.zed.global.zed;
1012

13+
import java.io.FileWriter;
14+
import java.io.IOException;
15+
import java.util.function.LongSupplier;
16+
1117
import static us.ihmc.zed.global.zed.*;
1218

1319
/**
@@ -29,16 +35,26 @@ public class ZEDSVOLogger
2935
private final RepeatingTaskThread grabThread = new RepeatingTaskThread(getClass().getName() + "GrabThread", this::grab);
3036
private final RepeatingTaskThread connectionWatchdogThread = new RepeatingTaskThread(getClass().getName() + "ConnectionWatchdog", this::connectionCheck);
3137

38+
private String svoPrefix;
39+
private LongSupplier timestampSupplier;
40+
private FileWriter timestampWriter;
41+
3242
private volatile double lastGrabTime;
3343
private volatile boolean stopRequested;
3444
private volatile boolean completelyStopped;
3545
private volatile boolean failedBeyondRecovery;
3646

37-
public void start(String svoFile, String address, int port)
47+
public void start(String svoFile, String datFile, LongSupplier timestampSupplier, String address, int port)
3848
{
49+
this.timestampSupplier = timestampSupplier;
50+
3951
if (stopRequested)
4052
throw new IllegalStateException("Cannot restart ZEDSVOLogger once stopped");
4153

54+
String[] parts = svoFile.split("[/\\\\]");
55+
svoPrefix = parts[parts.length - 1].substring(0, "yyyyMMdd_HHmmss".length());
56+
timestampWriter = ExceptionTools.handle(() -> new FileWriter(datFile, true), DefaultExceptionHandler.RUNTIME_EXCEPTION);
57+
4258
initParameters = new SL_InitParameters();
4359
initParameters.input_type(zed.SL_INPUT_TYPE_STREAM);
4460
initParameters.async_grab_camera_recovery(true);
@@ -94,6 +110,8 @@ public void stop()
94110
// Can't use LogTools here, we might be shutting down...
95111
System.out.println("Closing ZED SDK stream");
96112

113+
ExceptionTools.handle(timestampWriter::close, DefaultExceptionHandler.PRINT_MESSAGE);
114+
97115
completelyStopped = true;
98116
}
99117
}
@@ -107,6 +125,15 @@ public void grab()
107125

108126
lastGrabTime = System.currentTimeMillis() / 1000D;
109127

128+
try
129+
{
130+
timestampWriter.write("%d %d %s%n".formatted(timestampSupplier.getAsLong(), sl_get_current_timestamp(cameraID), svoPrefix));
131+
}
132+
catch (IOException e)
133+
{
134+
LogTools.error(e.getMessage());
135+
}
136+
110137
if (returnCode == SL_ERROR_CODE_FAILURE || returnCode == SL_ERROR_CODE_CAMERA_NOT_DETECTED)
111138
failedBeyondRecovery = true;
112139

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.Date;
1414
import java.util.Map;
1515
import java.util.concurrent.ConcurrentHashMap;
16+
import java.util.function.LongSupplier;
1617

1718
/**
1819
* Manages n number of ZED SDK connections for logging SVO files.
@@ -30,13 +31,15 @@ private record ZEDSDKAnnounceHash(String address, int port)
3031
}
3132

3233
private final File tempDirectory;
34+
private final LongSupplier timestampSupplier;
3335

3436
private final ROS2Node ros2Node;
3537
private final Map<ZEDSDKAnnounceHash, ZEDSVOLogger> zedLoggers = new ConcurrentHashMap<>();
3638

37-
public ZEDSVOLoggerManager(File tempDirectory, File finalDirectory)
39+
public ZEDSVOLoggerManager(File tempDirectory, File finalDirectory, LongSupplier timestampSupplier)
3840
{
3941
this.tempDirectory = tempDirectory;
42+
this.timestampSupplier = timestampSupplier;
4043

4144
ros2Node = new ROS2NodeBuilder().build(ROS2TopicNameTools.toROSTopicFormat(finalDirectory.getName() + "_zed_svo_logger_node"));
4245

@@ -63,11 +66,13 @@ private void onZEDSDKAnnounceMessage(ZEDSDKAnnounce message)
6366
{
6467
File perceptionDir = new File(tempDirectory, "perception");
6568
perceptionDir.mkdirs();
66-
String svoFile = perceptionDir.getAbsolutePath() + File.separator + generateSVOFileName();
69+
String svoFile = perceptionDir.getAbsolutePath() + File.separator + generateSVOFileName(message);
70+
String datFile = perceptionDir.getAbsolutePath() + File.separator +
71+
"%s%s".formatted(message.getSensorNameAsString(), VideoDataLoggerInterface.timestampDataPostfix);
6772

6873
ZEDSVOLogger zedSVOLogger = new ZEDSVOLogger();
6974

70-
zedSVOLogger.start(svoFile, message.getAddressAsString(), message.getPort());
75+
zedSVOLogger.start(svoFile, datFile, timestampSupplier, message.getAddressAsString(), message.getPort());
7176

7277
zedLoggers.put(announceHash, zedSVOLogger);
7378
}
@@ -80,9 +85,9 @@ public void destroy()
8085
ros2Node.destroy();
8186
}
8287

83-
private static String generateSVOFileName()
88+
private static String generateSVOFileName(ZEDSDKAnnounce message)
8489
{
8590
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
86-
return dateFormat.format(new Date()) + "_" + "ZEDRecording.svo2";
91+
return "%s_%s.svo2".formatted(dateFormat.format(new Date()), message.getSensorNameAsString());
8792
}
8893
}

0 commit comments

Comments
 (0)