1
1
package us .ihmc .robotDataLogger .logger ;
2
2
3
- import us .ihmc .commons .Conversions ;
4
3
import us .ihmc .commons .exception .DefaultExceptionHandler ;
5
4
import us .ihmc .commons .exception .ExceptionTools ;
6
5
import us .ihmc .commons .thread .RepeatingTaskThread ;
7
6
import us .ihmc .commons .thread .ThreadTools ;
8
7
import us .ihmc .log .LogTools ;
9
8
import us .ihmc .zed .SL_InitParameters ;
10
9
import us .ihmc .zed .SL_RuntimeParameters ;
10
+ import us .ihmc .zed .ZEDTools ;
11
11
import us .ihmc .zed .global .zed ;
12
12
13
13
import java .io .FileWriter ;
21
21
*/
22
22
public class ZEDSVOLogger
23
23
{
24
- private static final double CONNECT_TIMEOUT = 2.0 ;
25
24
private static final boolean TRANSCODE = false ;
26
25
27
- private static int nextCameraId = 10 ;
26
+ private static int nextCameraId = 0 ;
28
27
29
28
private final int cameraID = nextCameraId ++;
30
29
private SL_InitParameters initParameters ;
31
30
private SL_RuntimeParameters runtimeParameters ;
32
31
private final RepeatingTaskThread grabThread = new RepeatingTaskThread (getClass ().getName () + "GrabThread" , this ::grab );
33
- private final RepeatingTaskThread connectionWatchdogThread = new RepeatingTaskThread (getClass ().getName () + "ConnectionWatchdog" , this ::connectionCheck );
34
32
35
33
private String svoPrefix ;
36
34
private long controllerZeroInSensorFrame ;
37
35
private FileWriter timestampWriter ;
38
36
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 ;
43
38
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 )
45
40
{
46
- if (stopRequested )
47
- throw new IllegalStateException ("Cannot restart ZEDSVOLogger once stopped" );
41
+ closed = false ;
48
42
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
+ }
52
52
53
53
initParameters = new SL_InitParameters ();
54
54
initParameters .input_type (zed .SL_INPUT_TYPE_STREAM );
@@ -67,36 +67,27 @@ public void start(String svoFile, String datFile, String address, int port, int
67
67
68
68
int returnCode = sl_open_camera (cameraID , initParameters , 0 , "" , address , port , "" , "" , "" );
69
69
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 ) );
71
71
72
72
returnCode = sl_enable_recording (cameraID , svoFile , SL_SVO_COMPRESSION_MODE_H264 , bitrate , fps , TRANSCODE );
73
73
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 ) );
75
75
76
76
if (sl_is_opened (cameraID ))
77
77
{
78
78
LogTools .info ("Connected to ZED SDK stream on: " + address + ":" + port );
79
79
80
- grabThread .setFrequencyLimit (RepeatingTaskThread .UNLIMITED_FREQUENCY );
81
80
grabThread .startRepeating ();
82
81
}
83
-
84
- connectionWatchdogThread .setFrequencyLimit (Conversions .secondsToHertz (CONNECT_TIMEOUT ));
85
- ThreadTools .startAThread (() ->
86
- {
87
- ThreadTools .park (5.0 );
88
- connectionWatchdogThread .startRepeating ();
89
- }, getClass ().getSimpleName () + "ConnectionWatchdogDelay" );
90
82
}
91
83
92
- public void stop ()
84
+ public void close ()
93
85
{
94
- if (!stopRequested )
86
+ if (!closed )
95
87
{
96
- stopRequested = true ;
88
+ closed = true ;
97
89
98
90
grabThread .blockingKill ();
99
- connectionWatchdogThread .kill ();
100
91
101
92
sl_close_camera (cameraID );
102
93
sl_unload_instance (cameraID );
@@ -108,83 +99,41 @@ public void stop()
108
99
System .out .println ("Closing ZED SDK stream" );
109
100
110
101
ExceptionTools .handle (timestampWriter ::close , DefaultExceptionHandler .PRINT_MESSAGE );
111
-
112
- completelyStopped = true ;
113
102
}
114
103
}
115
104
116
105
public void grab ()
117
106
{
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 )
136
108
{
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 );
142
110
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 )
152
122
{
153
- LogTools .info ("Unable to connect to ZED SDK stream" );
154
-
155
- stop ();
156
123
}
157
124
158
- if (( System . currentTimeMillis () / 1000D ) - lastGrabTime > CONNECT_TIMEOUT )
125
+ if (returnCode != SL_ERROR_CODE_SUCCESS )
159
126
{
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 );
161
129
162
- stop ( );
130
+ LogTools . info ( "Could not grab image from ZED, trying again in a few seconds..." );
163
131
}
164
132
}
165
133
}
166
134
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 ()
187
136
{
188
- return failedBeyondRecovery ;
137
+ return closed ;
189
138
}
190
139
}
0 commit comments