11package us .ihmc .robotDataLogger .logger ;
22
3- import us .ihmc .commons .Conversions ;
43import us .ihmc .commons .exception .DefaultExceptionHandler ;
54import us .ihmc .commons .exception .ExceptionTools ;
65import us .ihmc .commons .thread .RepeatingTaskThread ;
76import us .ihmc .commons .thread .ThreadTools ;
87import us .ihmc .log .LogTools ;
98import us .ihmc .zed .SL_InitParameters ;
109import us .ihmc .zed .SL_RuntimeParameters ;
10+ import us .ihmc .zed .ZEDTools ;
1111import us .ihmc .zed .global .zed ;
1212
1313import java .io .FileWriter ;
2121 */
2222public 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}
0 commit comments