ZED Open Capture  v0.6.0
Low level camera driver for the ZED stereo camera family
videocapture.hpp
Go to the documentation of this file.
1 //
3 // Copyright (c) 2021, STEREOLABS.
4 //
5 // All rights reserved.
6 //
7 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
8 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
9 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
10 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
11 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
12 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
13 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
14 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
15 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
16 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
17 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
18 //
20 
21 #ifndef VIDEOCAPTURE_HPP
22 #define VIDEOCAPTURE_HPP
23 
24 #include "defines.hpp"
25 #include <thread>
26 #include <mutex>
27 #include <fstream> // std::ofstream
28 #include <iomanip>
29 
30 #define LOG_SEP ","
31 
32 #ifdef VIDEO_MOD_AVAILABLE
33 
34 #include "videocapture_def.hpp"
35 
36 namespace sl_oc {
37 
38 
39 
40 #ifdef SENSORS_MOD_AVAILABLE
41 namespace sensors {
42 class SensorCapture;
43 }
44 #endif
45 
46 namespace video {
47 
51 struct SL_OC_EXPORT Frame
52 {
53  uint64_t frame_id = 0;
54  uint64_t timestamp = 0;
55  uint8_t* data = nullptr;
56  uint16_t width = 0;
57  uint16_t height = 0;
58  uint8_t channels = 0;
59 };
60 
64 class SL_OC_EXPORT VideoCapture
65 {
66  ZED_OC_VERSION_ATTRIBUTE;
67 
68 public:
74 
78  virtual ~VideoCapture();
79 
85  bool initializeVideo( int devId=-1 );
86 
98  const Frame& getLastFrame(uint64_t timeout_msec=100);
99 
105  inline void getFrameSize( int& width, int& height ){width=mWidth;height=mHeight;}
106 
107  // ----> Led Control
113  int setLEDstatus(bool status);
114 
120  int getLEDstatus(bool *status);
121 
127  int toggleLED(bool *value);
128  // <---- Led Control
129 
130  // ----> Camera Settings control
135  void setBrightness(int brightness);
136 
141  int getBrightness();
142 
146  void resetBrightness();
147 
152  void setSharpness(int sharpness);
153 
158  int getSharpness();
159 
163  void resetSharpness();
164 
169  void setContrast(int contrast);
170 
175  int getContrast();
176 
180  void resetContrast();
181 
186  void setHue(int hue);
187 
192  int getHue();
193 
197  void resetHue();
198 
203  void setSaturation(int saturation);
204 
209  int getSaturation();
210 
214  void resetSaturation();
215 
220  void setWhiteBalance(int wb);
221 
226  int getWhiteBalance();
227 
232  void setAutoWhiteBalance(bool active);
233 
238  bool getAutoWhiteBalance();
239 
243  void resetAutoWhiteBalance();
244 
249  void setGamma(int gamma);
250 
255  int getGamma();
256 
260  void resetGamma();
261 
266  int setAECAGC(bool active);
267 
272  bool getAECAGC();
273 
277  void resetAECAGC();
278 
288  bool setROIforAECAGC(CAM_SENS_POS side, uint16_t x, uint16_t y, uint16_t w, uint16_t h);
289 
295  bool resetROIforAECAGC(CAM_SENS_POS side);
296 
306  bool getROIforAECAGC(CAM_SENS_POS side,uint16_t& x, uint16_t& y, uint16_t& w, uint16_t& h);
307 
313  void setGain(CAM_SENS_POS cam, int gain);
314 
320  int getGain(CAM_SENS_POS cam);
321 
327  void setExposure(CAM_SENS_POS cam, int exposure);
328 
334  int getExposure(CAM_SENS_POS cam);
335  // <---- Camera Settings control
336 
341  int getSerialNumber();
342 
346  void setColorBars(int side, bool c);
347 
352  inline std::string getDeviceName(){return mDevName;}
353 
358  inline int getDeviceId(){return mDevId;}
359 
360 #ifdef SENSOR_LOG_AVAILABLE
367  bool enableAecAgcSensLogging(bool enable, int frame_skip=10);
368 
374  void saveAllISPRegisters(std::string filename);
375 
381  void saveAllSensorsRegisters(std::string filename);
382 #endif
383 
384 
385 #ifdef SENSORS_MOD_AVAILABLE
391  bool enableSensorSync( sensors::SensorCapture* sensCap=nullptr );
392 
397  inline void setReadyToSync(){ mSensReadyToSync=true; }
398 #endif
399 
400  bool resetAGCAECregisters();
401 
402 private:
403  void grabThreadFunc();
404 
405  // ----> Low level functions
406  int ll_VendorControl(uint8_t *buf, int len, int readMode, bool safe = false, bool force=false);
407  int ll_get_gpio_value(int gpio_number, uint8_t* value);
408  int ll_set_gpio_value(int gpio_number, uint8_t value);
409  int ll_set_gpio_direction(int gpio_number, int direction);
410  int ll_read_system_register(uint64_t address, uint8_t* value);
411  int ll_write_system_register(uint64_t address, uint8_t value);
412  int ll_read_sensor_register(int side, int sscb_id, uint64_t address, uint8_t *value);
413  int ll_write_sensor_register(int side, int sscb_id, uint64_t address, uint8_t value);
414 
415  int ll_SPI_FlashProgramRead(uint8_t *pBuf, int Adr, int len, bool force=false);
416 
417  int ll_isp_aecagc_enable(int side, bool enable);
418  int ll_isp_is_aecagc(int side);
419 
420  uint8_t ll_read_reg(uint64_t addr);
421 
422  int ll_isp_get_gain(uint8_t *val, uint8_t sensorID);
423  int ll_isp_set_gain(unsigned char ucGainH, unsigned char ucGainM, unsigned char ucGainL, int sensorID);
424  int ll_isp_get_exposure(unsigned char *val, unsigned char sensorID);
425  int ll_isp_set_exposure(unsigned char ucExpH, unsigned char ucExpM, unsigned char ucExpL, int sensorID);
426 
427  void ll_activate_sync(); // Activate low level sync signal between Camera and MCU
428  // <---- Low level functions
429 
430  // ----> Mid level functions
431  void setCameraControlSettings(int ctrl_id, int ctrl_val);
432  void resetCameraControlSettings(int ctrl_id);
433  int getCameraControlSettings(int ctrl_id);
434 
435  int setGammaPreset(int side, int value);
436 
437  int calcRawGainValue(int gain); // Convert "user gain" to "ISP gain"
438  int calcGainValue(int rawGain); // Convert "ISP Gain" to "User gain"
439  // <---- Mid level functions
440 
441  // ----> Connection control functions
442  bool openCamera( uint8_t devId );
443  bool startCapture();
444  void reset();
445  inline void stopCapture(){mStopCapture=true;}
446  int input_set_framerate(int fps);
447  int xioctl(int fd, uint64_t IOCTL_X, void *arg);
448  void checkResFps();
449  SL_DEVICE getCameraModel(std::string dev_name);
450  // <---- Connection control functions
451 
452  typedef enum _date_time
453  {
454  FULL,
455  DATE,
456  TIME
457  } DateTime;
458 
459  static inline std::string getCurrentDateTime( DateTime type ){
460  time_t now = time(0);
461  struct tm tstruct;
462  char buf[80];
463  tstruct = *localtime(&now);
464  if(type==FULL)
465  strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct);
466  else if(type==DATE)
467  strftime(buf, sizeof(buf), "%Y-%m-%d", &tstruct);
468  else if(type==TIME)
469  strftime(buf, sizeof(buf), "%X", &tstruct);
470  return std::string(buf);
471  }
472 
473 #ifdef SENSOR_LOG_AVAILABLE
474  void saveLogDataLeft();
475  void saveLogDataRight();
476 #endif
477 
478 private:
479  // Flags
480  bool mNewFrame=false;
481  bool mInitialized=false;
482  bool mStopCapture=true;
483  bool mGrabRunning=false;
484 
485  VideoParams mParams;
486 
487  int mDevId = 0;
488  std::string mDevName;
489  int mFileDesc=-1;
490 
491  std::mutex mBufMutex;
492  std::mutex mComMutex;
493 
494  int mWidth = 0;
495  int mHeight = 0;
496  int mChannels = 0;
497  int mFps=0;
498 
499  SL_DEVICE mCameraModel = SL_DEVICE::NONE;
500 
501  Frame mLastFrame;
502  uint8_t mBufCount = 2;
503  uint8_t mCurrentIndex = 0;
504  struct UVCBuffer *mBuffers = nullptr;
505 
506  uint64_t mStartTs=0;
507  uint64_t mInitTs=0;
508 
509  int mGainSegMax=0;
510  int mExpoureRawMax;
511 
512  std::thread mGrabThread;
513 
514  bool mFirstFrame=true;
515 
516 #ifdef SENSOR_LOG_AVAILABLE
517  // ----> Registers logging
518  bool mLogEnable=false;
519  std::string mLogFilenameLeft;
520  std::string mLogFilenameRight;
521  std::ofstream mLogFileLeft;
522  std::ofstream mLogFileRight;
523  int mLogFrameSkip=10;
524  // <---- Registers logging
525 #endif
526 
527 
528 #ifdef SENSORS_MOD_AVAILABLE
529  bool mSyncEnabled=false;
530  sensors::SensorCapture* mSensPtr;
531 
532  bool mSensReadyToSync=false;
533 #endif
534 };
535 
536 }
537 
538 }
539 #endif
540 
567 #endif // VIDEOCAPTURE_HPP
568 
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 ...
int getDeviceId()
Retrieve the OS device index.
void setReadyToSync()
Indicates that the SensorCapture object received the HW sync signal and a frame must be synchronized ...
void getFrameSize(int &width, int &height)
Get the size of the camera frame.
std::string getDeviceName()
Retrieve the OS device name.
struct sl_oc::video::VideoParams VideoParams
The camera configuration parameters.
CAM_SENS_POS
Position of the Camera CMOS sensors.
SL_DEVICE
Camera models.
The Frame struct containing the acquired video frames.
The camera configuration parameters.
sl_oc::video::VideoParams params