ZED Open Capture  v0.6.0
Low level camera driver for the ZED stereo camera family
Functions | Variables
zed_oc_sync_example.cpp File Reference

Go to the source code of this file.

Functions

void getSensorThreadFunc (sl_oc::sensors::SensorCapture *sensCap)
 
int main (int argc, char *argv[])
 

Variables

std::mutex imuMutex
 
std::string imuTsStr
 
std::string imuAccelStr
 
std::string imuGyroStr
 
bool sensThreadStop =false
 
uint64_t mcu_sync_ts =0
 

Function Documentation

◆ getSensorThreadFunc()

void getSensorThreadFunc ( sl_oc::sensors::SensorCapture sensCap)
Examples
zed_oc_sync_example.cpp.

Definition at line 233 of file zed_oc_sync_example.cpp.

234 {
235  // Flag to stop the thread
236  sensThreadStop = false;
237 
238  // Previous IMU timestamp to calculate frequency
239  uint64_t last_imu_ts = 0;
240 
241  // Infinite data grabbing loop
242  while(!sensThreadStop)
243  {
244  // ----> Get IMU data
245  const sl_oc::sensors::data::Imu imuData = sensCap->getLastIMUData(2000);
246 
247  // Process data only if valid
248  if(imuData.valid == sl_oc::sensors::data::Imu::NEW_VAL ) // Uncomment to use only data syncronized with the video frames
249  {
250  // ----> Data info to be displayed
251  std::stringstream timestamp;
252  std::stringstream accel;
253  std::stringstream gyro;
254 
255  timestamp << std::fixed << std::setprecision(9) << "IMU timestamp: " << static_cast<double>(imuData.timestamp)/1e9<< " sec" ;
256  if(last_imu_ts!=0)
257  timestamp << std::fixed << std::setprecision(1) << " [" << 1e9/static_cast<float>(imuData.timestamp-last_imu_ts) << " Hz]";
258  last_imu_ts = imuData.timestamp;
259 
260  accel << std::fixed << std::showpos << std::setprecision(4) << " * Accel: " << imuData.aX << " " << imuData.aY << " " << imuData.aZ << " [m/s^2]";
261  gyro << std::fixed << std::showpos << std::setprecision(4) << " * Gyro: " << imuData.gX << " " << imuData.gY << " " << imuData.gZ << " [deg/s]";
262  // <---- Data info to be displayed
263 
264  // Mutex to not overwrite data while diplaying them
265  imuMutex.lock();
266 
267  imuTsStr = timestamp.str();
268  imuAccelStr = accel.str();
269  imuGyroStr = gyro.str();
270 
271  // ----> Timestamp of the synchronized data
272  if(imuData.sync)
273  {
274  mcu_sync_ts = imuData.timestamp;
275  }
276  // <---- Timestamp of the synchronized data
277 
278  imuMutex.unlock();
279  }
280  // <---- Get IMU data
281  }
282 }
const data::Imu & getLastIMUData(uint64_t timeout_usec=1500)
Get the last received IMU data.
Contains the acquired Imu data.
float gX
Angular velocity around X axis in °/s.
float aX
Acceleration along X axis in m/s²
ImuStatus valid
Indicates if IMU data are valid.
uint64_t timestamp
Timestamp in nanoseconds.
bool sync
Indicates in IMU data are synchronized with a video frame.
float gZ
Angular velocity around > axis in °/s.
float gY
Angular velocity around Y axis in °/s.
float aY
Acceleration along Y axis in m/s²
float aZ
Acceleration along Z axis in m/s²
bool sensThreadStop
uint64_t mcu_sync_ts
std::string imuTsStr
std::string imuAccelStr
std::mutex imuMutex
std::string imuGyroStr

References sl_oc::sensors::data::Imu::aX, sl_oc::sensors::data::Imu::aY, sl_oc::sensors::data::Imu::aZ, sl_oc::sensors::SensorCapture::getLastIMUData(), sl_oc::sensors::data::Imu::gX, sl_oc::sensors::data::Imu::gY, sl_oc::sensors::data::Imu::gZ, imuAccelStr, imuGyroStr, imuMutex, imuTsStr, mcu_sync_ts, sl_oc::sensors::data::Imu::NEW_VAL, sensThreadStop, sl_oc::sensors::data::Imu::sync, sl_oc::sensors::data::Imu::timestamp, and sl_oc::sensors::data::Imu::valid.

Referenced by main().

◆ main()

int main ( int  argc,
char *  argv[] 
)
Examples
zed_oc_sync_example.cpp.

Definition at line 50 of file zed_oc_sync_example.cpp.

51 {
52  // ----> Silence unused warning
53  (void)argc;
54  (void)argv;
55  // <---- Silence unused warning
56 
57  //sl_oc::sensors::SensorCapture::resetSensorModule();
58  //sl_oc::sensors::SensorCapture::resetVideoModule();
59 
60  // Set the verbose level
62 
63  // ----> Set the video parameters
67  params.verbose = verbose;
68  // <---- Video parameters
69 
70  // ----> Create a Video Capture object
72  if( !videoCap.initializeVideo(-1) )
73  {
74  std::cerr << "Cannot open camera video capture" << std::endl;
75  std::cerr << "Try to enable verbose to get more info" << std::endl;
76 
77  return EXIT_FAILURE;
78  }
79 
80  // Serial number of the connected camera
81  int camSn = videoCap.getSerialNumber();
82 
83  std::cout << "Video Capture connected to camera sn: " << camSn << std::endl;
84  // <---- Create a Video Capture object
85 
86  // ----> Create a Sensors Capture object
87  sl_oc::sensors::SensorCapture sensCap(verbose);
88  if( !sensCap.initializeSensors(camSn) ) // Note: we use the serial number acquired by the VideoCapture object
89  {
90  std::cerr << "Cannot open sensors capture" << std::endl;
91  std::cerr << "Try to enable verbose to get more info" << std::endl;
92 
93  return EXIT_FAILURE;
94  }
95  std::cout << "Sensors Capture connected to camera sn: " << sensCap.getSerialNumber() << std::endl;
96 
97  // Start the sensor capture thread. Note: since sensor data can be retrieved at 400Hz and video data frequency is
98  // minor (max 100Hz), we use a separated thread for sensors.
99  std::thread sensThread(getSensorThreadFunc,&sensCap);
100  // <---- Create Sensors Capture
101 
102  // ----> Enable video/sensors synchronization
103  videoCap.enableSensorSync(&sensCap);
104  // <---- Enable video/sensors synchronization
105 
106  // ----> Init OpenCV RGB frame
107  int w,h;
108  videoCap.getFrameSize(w,h);
109 
110  cv::Size display_resolution(1024, 576);
111 
112  switch(params.res)
113  {
114  default:
116  display_resolution.width = w;
117  display_resolution.height = h;
118  break;
120  display_resolution.width = w*0.6;
121  display_resolution.height = h*0.6;
122  break;
125  display_resolution.width = w*0.4;
126  display_resolution.height = h*0.4;
127  break;
128  }
129 
130  int h_data = 70;
131  cv::Mat frameDisplay(display_resolution.height + h_data, display_resolution.width,CV_8UC3, cv::Scalar(0,0,0));
132  cv::Mat frameData = frameDisplay(cv::Rect(0,0, display_resolution.width, h_data));
133  cv::Mat frameBGRDisplay = frameDisplay(cv::Rect(0,h_data, display_resolution.width, display_resolution.height));
134  cv::Mat frameBGR(h, w, CV_8UC3, cv::Scalar(0,0,0));
135  // <---- Init OpenCV RGB frame
136 
137  uint64_t last_timestamp = 0;
138 
139  float frame_fps=0;
140 
141  // Infinite grabbing loop
142  while (1)
143  {
144  // ----> Get Video frame
145  // Get last available frame
146  const sl_oc::video::Frame frame = videoCap.getLastFrame(1);
147 
148  // If the frame is valid we can update it
149  std::stringstream videoTs;
150  if(frame.data!=nullptr && frame.timestamp!=last_timestamp)
151  {
152  frame_fps = 1e9/static_cast<float>(frame.timestamp-last_timestamp);
153  last_timestamp = frame.timestamp;
154 
155  // ----> Conversion from YUV 4:2:2 to BGR for visualization
156  cv::Mat frameYUV( frame.height, frame.width, CV_8UC2, frame.data);
157  cv::cvtColor(frameYUV,frameBGR, cv::COLOR_YUV2BGR_YUYV);
158  // <---- Conversion from YUV 4:2:2 to BGR for visualization
159  }
160  // <---- Get Video frame
161 
162  // ----> Video Debug information
163  videoTs << std::fixed << std::setprecision(9) << "Video timestamp: " << static_cast<double>(last_timestamp)/1e9<< " sec" ;
164  if( last_timestamp!=0 )
165  videoTs << std::fixed << std::setprecision(1) << " [" << frame_fps << " Hz]";
166  // <---- Video Debug information
167 
168  // ----> Display frame with info
169  if(frame.data!=nullptr)
170  {
171  frameData.setTo(0);
172 
173  // Video info
174  cv::putText( frameData, videoTs.str(), cv::Point(10,20),cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(241,240,236));
175 
176  // IMU info
177  imuMutex.lock();
178  cv::putText( frameData, imuTsStr, cv::Point(10, 35),cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(241,240,236));
179 
180  // Timestamp offset info
181  std::stringstream offsetStr;
182  double offset = (static_cast<double>(frame.timestamp)-static_cast<double>(mcu_sync_ts))/1e9;
183  offsetStr << std::fixed << std::setprecision(9) << std::showpos << "Timestamp offset: " << offset << " sec [video-sensors]";
184  cv::putText( frameData, offsetStr.str().c_str(), cv::Point(10, 50),cv::FONT_HERSHEY_SIMPLEX, 0.35, cv::Scalar(241,240,236));
185 
186  // Average timestamp offset info (we wait at least 200 frames to be sure that offset is stable)
187  if( frame.frame_id>200 )
188  {
189  static double sum=0;
190  static int count=0;
191 
192  sum += offset;
193  double avg_offset=sum/(++count);
194 
195  std::stringstream avgOffsetStr;
196  avgOffsetStr << std::fixed << std::setprecision(9) << std::showpos << "Avg timestamp offset: " << avg_offset << " sec";
197  cv::putText( frameData, avgOffsetStr.str().c_str(), cv::Point(10,62),cv::FONT_HERSHEY_SIMPLEX,0.35, cv::Scalar(241, 240,236));
198  }
199 
200  // IMU values
201  cv::putText( frameData, "Inertial sensor data:", cv::Point(display_resolution.width/2,20),cv::FONT_HERSHEY_SIMPLEX, 0.6, cv::Scalar(241, 240,236));
202  cv::putText( frameData, imuAccelStr, cv::Point(display_resolution.width/2+15,42),cv::FONT_HERSHEY_SIMPLEX, 0.6, cv::Scalar(241, 240,236));
203  cv::putText( frameData, imuGyroStr, cv::Point(display_resolution.width/2+15, 62),cv::FONT_HERSHEY_SIMPLEX, 0.6, cv::Scalar(241, 240,236));
204  imuMutex.unlock();
205 
206  // Resize Image for display
207  cv::resize(frameBGR, frameBGRDisplay, display_resolution);
208  // Display image
209  cv::imshow( "Stream RGB", frameDisplay);
210  }
211  // <---- Display frame with info
212 
213  // ----> Keyboard handling
214  int key = cv::waitKey(1);
215 
216  if( key != -1 )
217  {
218  // Quit
219  if(key=='q' || key=='Q'|| key==27)
220  {
221  sensThreadStop=true;
222  sensThread.join();
223  break;
224  }
225  }
226  // <---- Keyboard handling
227  }
228 
229  return EXIT_SUCCESS;
230 }
The SensorCapture class provides sensor grabbing functions for the Stereolabs ZED Mini and ZED2 camer...
The VideoCapture class provides image grabbing functions and settings control for all the Stereolabs ...
@ FPS_30
30 Frames per second. Not available for RESOLUTION::HD2K.
VERBOSITY
Definition: defines.hpp:85
@ ERROR
Definition: defines.hpp:87
The Frame struct containing the acquired video frames.
uint64_t timestamp
Timestamp in nanoseconds.
uint16_t height
Frame height.
uint64_t frame_id
Increasing index of frames.
uint16_t width
Frame width.
uint8_t * data
Frame data in YUV 4:2:2 format.
The camera configuration parameters.
RESOLUTION res
Camera resolution.
FPS fps
Frames per second.
void getSensorThreadFunc(sl_oc::sensors::SensorCapture *sensCap)
cv::Mat frameBGR
sl_oc::video::VideoParams params
cv::Mat frameYUV

References sl_oc::video::Frame::data, sl_oc::video::VideoCapture::enableSensorSync(), sl_oc::ERROR, sl_oc::video::VideoParams::fps, sl_oc::video::FPS_30, sl_oc::video::Frame::frame_id, frameBGR, frameYUV, sl_oc::video::VideoCapture::getFrameSize(), sl_oc::video::VideoCapture::getLastFrame(), getSensorThreadFunc(), sl_oc::sensors::SensorCapture::getSerialNumber(), sl_oc::video::VideoCapture::getSerialNumber(), sl_oc::video::HD1080, sl_oc::video::HD2K, sl_oc::video::HD720, sl_oc::video::Frame::height, imuAccelStr, imuGyroStr, imuMutex, imuTsStr, sl_oc::sensors::SensorCapture::initializeSensors(), sl_oc::video::VideoCapture::initializeVideo(), mcu_sync_ts, params, sl_oc::video::VideoParams::res, sensThreadStop, sl_oc::video::Frame::timestamp, sl_oc::video::VideoParams::verbose, sl_oc::video::VGA, and sl_oc::video::Frame::width.

Variable Documentation

◆ imuAccelStr

std::string imuAccelStr
Examples
zed_oc_sync_example.cpp.

Definition at line 42 of file zed_oc_sync_example.cpp.

Referenced by getSensorThreadFunc(), and main().

◆ imuGyroStr

std::string imuGyroStr
Examples
zed_oc_sync_example.cpp.

Definition at line 43 of file zed_oc_sync_example.cpp.

Referenced by getSensorThreadFunc(), and main().

◆ imuMutex

std::mutex imuMutex
Examples
zed_oc_sync_example.cpp.

Definition at line 40 of file zed_oc_sync_example.cpp.

Referenced by getSensorThreadFunc(), and main().

◆ imuTsStr

std::string imuTsStr
Examples
zed_oc_sync_example.cpp.

Definition at line 41 of file zed_oc_sync_example.cpp.

Referenced by getSensorThreadFunc(), and main().

◆ mcu_sync_ts

uint64_t mcu_sync_ts =0
Examples
zed_oc_sync_example.cpp.

Definition at line 46 of file zed_oc_sync_example.cpp.

Referenced by getSensorThreadFunc(), and main().

◆ sensThreadStop

bool sensThreadStop =false
Examples
zed_oc_sync_example.cpp.

Definition at line 45 of file zed_oc_sync_example.cpp.

Referenced by getSensorThreadFunc(), and main().