ZED Open Capture  v0.6.0
Low level camera driver for the ZED stereo camera family
Public Member Functions | Static Public Member Functions | List of all members
sl_oc::sensors::SensorCapture Class Reference

The SensorCapture class provides sensor grabbing functions for the Stereolabs ZED Mini and ZED2 camera models. More...

#include <sensorcapture.hpp>

Public Member Functions

 SensorCapture (sl_oc::VERBOSITY verbose_lvl=sl_oc::VERBOSITY::ERROR)
 The default constructor. More...
 
virtual ~SensorCapture ()
 The class destructor. More...
 
std::vector< int > getDeviceList (bool refresh=false)
 Get the list of the serial number of all the available devices. More...
 
bool initializeSensors (int sn=-1)
 Open a connection to the MCU of a ZED Mini or a ZED2 camera using the specified serial number or searching for the first available device. More...
 
void getFirmwareVersion (uint16_t &fw_major, uint16_t &fw_minor)
 Get the MCU firmware version in form [fw_major].[fw_minor]. More...
 
int getSerialNumber ()
 Retrieve the serial number of the connected camera. More...
 
const data::ImugetLastIMUData (uint64_t timeout_usec=1500)
 Get the last received IMU data. More...
 
const data::MagnetometergetLastMagnetometerData (uint64_t timeout_usec=100)
 Get the last received Magnetometer data. More...
 
const data::EnvironmentgetLastEnvironmentData (uint64_t timeout_usec=100)
 Get the last received Environment data. More...
 
const data::TemperaturegetLastCameraTemperatureData (uint64_t timeout_usec=100)
 Get the last received camera sensors temperature data. More...
 
void updateTimestampOffset (uint64_t frame_ts)
 Called by VideoCapture to update timestamp offset. More...
 
void setStartTimestamp (uint64_t start_ts)
 Called by VideoCapture to sync timestamps reference point. More...
 
void setVideoPtr (video::VideoCapture *videoPtr)
 Called by VideoCapture to set the pointer to it. More...
 

Static Public Member Functions

static bool resetSensorModule (int serial_number=0)
 Perform a SW reset of the Sensors Module. To be called in case one of the sensors stops to work correctly. More...
 
static bool resetVideoModule (int serial_number=0)
 Perform a reset of the video module without resetting the sensor module. To be called in case the Video module stops to work correctly. More...
 

Detailed Description

The SensorCapture class provides sensor grabbing functions for the Stereolabs ZED Mini and ZED2 camera models.

Examples
zed_oc_sensors_example.cpp, and zed_oc_sync_example.cpp.

Definition at line 132 of file sensorcapture.hpp.

Constructor & Destructor Documentation

◆ SensorCapture()

sl_oc::sensors::SensorCapture::SensorCapture ( sl_oc::VERBOSITY  verbose_lvl = sl_oc::VERBOSITY::ERROR)

The default constructor.

Parameters
verbose_lvlenable useful information to debug the class behaviours while running

Definition at line 36 of file sensorcapture.cpp.

37 {
38  mVerbose = verbose_lvl;
39 
40  if( mVerbose )
41  {
42  std::string ver =
43  "ZED Open Capture - Sensors module - Version: "
44  + std::to_string(mMajorVer) + "."
45  + std::to_string(mMinorVer) + "."
46  + std::to_string(mPatchVer);
47  INFO_OUT(mVerbose,ver );
48  }
49 }

◆ ~SensorCapture()

sl_oc::sensors::SensorCapture::~SensorCapture ( )
virtual

The class destructor.

Definition at line 51 of file sensorcapture.cpp.

52 {
53  close();
54 }

Member Function Documentation

◆ getDeviceList()

std::vector< int > sl_oc::sensors::SensorCapture::getDeviceList ( bool  refresh = false)

Get the list of the serial number of all the available devices.

Parameters
refreshif true USB device tree is parsed to search for modifications (new device connected/disconnected)
Returns
a vector containing the serial number of all the available devices
Examples
zed_oc_sensors_example.cpp.

Definition at line 107 of file sensorcapture.cpp.

108 {
109  if(mSlDevPid.size()==0 || refresh)
110  enumerateDevices();
111 
112  std::vector<int> sn_vec;
113 
114  for(std::map<int,uint16_t>::iterator it = mSlDevPid.begin(); it != mSlDevPid.end(); ++it) {
115  sn_vec.push_back(it->first);
116  }
117 
118  return sn_vec;
119 }

Referenced by main().

◆ getFirmwareVersion()

void sl_oc::sensors::SensorCapture::getFirmwareVersion ( uint16_t &  fw_major,
uint16_t &  fw_minor 
)

Get the MCU firmware version in form [fw_major].[fw_minor].

Parameters
fw_majorthe major firmware version number
fw_minorthe minor firmware version number
Examples
zed_oc_sensors_example.cpp.

Definition at line 191 of file sensorcapture.cpp.

192 {
193  if(mDevSerial==-1)
194  return;
195 
196  uint16_t release = mSlDevFwVer[mDevSerial];
197 
198  fw_major = release>>8;
199  fw_minor = release&0x00FF;
200 }

Referenced by main().

◆ getLastCameraTemperatureData()

const data::Temperature & sl_oc::sensors::SensorCapture::getLastCameraTemperatureData ( uint64_t  timeout_usec = 100)

Get the last received camera sensors temperature data.

Parameters
timeout_usecdata grabbing timeout in milliseconds.
Returns
returns a reference to the last received data.
Examples
zed_oc_sensors_example.cpp.

Definition at line 853 of file sensorcapture.cpp.

854 {
855  // ----> Wait for a new frame
856  uint64_t time_count = (timeout_usec<100?100:timeout_usec)/10;
857  while( !mNewCamTempData )
858  {
859  if(time_count==0)
860  {
861  if(mLastCamTempData.valid!=data::Temperature::NOT_PRESENT)
862  mLastCamTempData.valid = data::Temperature::OLD_VAL;
863  return mLastCamTempData;
864  }
865  time_count--;
866  usleep(10);
867  }
868  // <---- Wait for a new frame
869 
870  // Get the frame mutex
871  const std::lock_guard<std::mutex> lock(mCamTempMutex);
872  mNewCamTempData = false;
873  return mLastCamTempData;
874 }
TempStatus valid
Indicates if camera temperature data are valid.

References sl_oc::sensors::data::Temperature::NOT_PRESENT, sl_oc::sensors::data::Temperature::OLD_VAL, and sl_oc::sensors::data::Temperature::valid.

Referenced by main().

◆ getLastEnvironmentData()

const data::Environment & sl_oc::sensors::SensorCapture::getLastEnvironmentData ( uint64_t  timeout_usec = 100)

Get the last received Environment data.

Parameters
timeout_usecdata grabbing timeout in milliseconds.
Returns
returns a reference to the last received data.
Examples
zed_oc_sensors_example.cpp.

Definition at line 830 of file sensorcapture.cpp.

831 {
832  // ----> Wait for a new frame
833  uint64_t time_count = (timeout_usec<100?100:timeout_usec)/10;
834  while( !mNewEnvData )
835  {
836  if(time_count==0)
837  {
838  if(mLastEnvData.valid!=data::Environment::NOT_PRESENT)
839  mLastEnvData.valid = data::Environment::OLD_VAL;
840  return mLastEnvData;
841  }
842  time_count--;
843  usleep(10);
844  }
845  // <---- Wait for a new frame
846 
847  // Get the frame mutex
848  const std::lock_guard<std::mutex> lock(mEnvMutex);
849  mNewEnvData = false;
850  return mLastEnvData;
851 }
EnvStatus valid
Indicates if Environmental data are valid.

References sl_oc::sensors::data::Environment::NOT_PRESENT, sl_oc::sensors::data::Environment::OLD_VAL, and sl_oc::sensors::data::Environment::valid.

Referenced by main().

◆ getLastIMUData()

const data::Imu & sl_oc::sensors::SensorCapture::getLastIMUData ( uint64_t  timeout_usec = 1500)

Get the last received IMU data.

Parameters
timeout_usecdata grabbing timeout in milliseconds.
Returns
returns a reference to the last received data.
Examples
zed_oc_sensors_example.cpp, and zed_oc_sync_example.cpp.

Definition at line 784 of file sensorcapture.cpp.

785 {
786  // ----> Wait for a new frame
787  uint64_t time_count = (timeout_usec<100?100:timeout_usec)/100;
788  while( !mNewIMUData )
789  {
790  if(time_count==0)
791  {
792  if(mLastIMUData.valid!=data::Imu::NOT_PRESENT)
793  mLastIMUData.valid = data::Imu::OLD_VAL;
794  return mLastIMUData;
795  }
796  time_count--;
797  usleep(100);
798  }
799  // <---- Wait for a new frame
800 
801  // Get the frame mutex
802  const std::lock_guard<std::mutex> lock(mIMUMutex);
803  mNewIMUData = false;
804  return mLastIMUData;
805 }
ImuStatus valid
Indicates if IMU data are valid.

References sl_oc::sensors::data::Imu::NOT_PRESENT, sl_oc::sensors::data::Imu::OLD_VAL, and sl_oc::sensors::data::Imu::valid.

Referenced by getSensorThreadFunc(), and main().

◆ getLastMagnetometerData()

const data::Magnetometer & sl_oc::sensors::SensorCapture::getLastMagnetometerData ( uint64_t  timeout_usec = 100)

Get the last received Magnetometer data.

Parameters
timeout_usecdata grabbing timeout in milliseconds.
Returns
returns a reference to the last received data.
Examples
zed_oc_sensors_example.cpp.

Definition at line 807 of file sensorcapture.cpp.

808 {
809  // ----> Wait for a new frame
810  uint64_t time_count = (timeout_usec<100?100:timeout_usec)/10;
811  while( !mNewMagData )
812  {
813  if(time_count==0)
814  {
815  if(mLastMagData.valid!=data::Magnetometer::NOT_PRESENT)
816  mLastMagData.valid=data::Magnetometer::OLD_VAL;
817  return mLastMagData;
818  }
819  time_count--;
820  usleep(10);
821  }
822  // <---- Wait for a new frame
823 
824  // Get the frame mutex
825  const std::lock_guard<std::mutex> lock(mMagMutex);
826  mNewMagData = false;
827  return mLastMagData;
828 }
MagStatus valid
Indicates if Magnetometer data are valid.

References sl_oc::sensors::data::Magnetometer::NOT_PRESENT, sl_oc::sensors::data::Magnetometer::OLD_VAL, and sl_oc::sensors::data::Magnetometer::valid.

Referenced by main().

◆ getSerialNumber()

int sl_oc::sensors::SensorCapture::getSerialNumber ( )

Retrieve the serial number of the connected camera.

Returns
the serial number of the connected camera
Examples
zed_oc_sensors_example.cpp.

Definition at line 202 of file sensorcapture.cpp.

203 {
204  if(mDevSerial==-1)
205  return -1;
206 
207  return mDevSerial;
208 }

Referenced by main().

◆ initializeSensors()

bool sl_oc::sensors::SensorCapture::initializeSensors ( int  sn = -1)

Open a connection to the MCU of a ZED Mini or a ZED2 camera using the specified serial number or searching for the first available device.

Parameters
snSerial Number of the camera. Use -1 to open connect to the first available device
Returns
returns true if the connection is correctly estabilished
Examples
zed_oc_sensors_example.cpp.

Definition at line 135 of file sensorcapture.cpp.

136 {
137  if(mSlDevPid.size()==0)
138  {
139  enumerateDevices();
140  }
141 
142  std::string sn_str;
143 
144  if(sn==-1)
145  {
146  if(mSlDevPid.size()==0)
147  {
148  enumerateDevices();
149  }
150 
151  if(mSlDevPid.size()==0)
152  {
153  ERROR_OUT(mVerbose,"No available ZED Mini or ZED2 cameras");
154  return false;
155  }
156 
157  sn = mSlDevPid.begin()->first;
158  }
159 
160  uint16_t pid = mSlDevPid[sn];
161 
162  if(!open( pid,sn))
163  {
164  std::string msg = "Connection to device with sn ";
165  msg += std::to_string(sn);
166  msg += " failed";
167 
168  ERROR_OUT(mVerbose,msg);
169 
170  mDevFwVer = -1;
171  mDevSerial = -1;
172 
173  return false;
174  }
175 
176  if(mVerbose)
177  {
178  std::string msg = "Connected to device with sn ";
179  msg += std::to_string(sn);
180 
181  INFO_OUT(mVerbose,msg);
182  }
183 
184  mDevFwVer = mSlDevFwVer[sn];
185  mDevPid = pid;
186  mInitialized = startCapture();
187 
188  return true;
189 }

Referenced by main().

◆ resetSensorModule()

bool sl_oc::sensors::SensorCapture::resetSensorModule ( int  serial_number = 0)
static

Perform a SW reset of the Sensors Module. To be called in case one of the sensors stops to work correctly.

Parameters
serial_numberThe serial number of the device to be reset (0 to reset the first available)
Returns
true if successful
Note
After the reset a new SensorCapture connection is required
The Sensors Module reset automatically performs a reset of the Video Module, so a new video::VideoCapture connection is required

Definition at line 673 of file sensorcapture.cpp.

674 {
675  int found_sn = serial_number;
676  unsigned short pid;
677  bool res = searchForConnectedDev(&found_sn, &pid);
678  if(!res)
679  {
680  std::string msg;
681  if(serial_number!=0)
682  {
683  msg = "[sl_oc::sensors::SensorCapture] WARNING: Sensors Module reset failed. Unable to find the Sensor Module with serial number ";
684  msg += std::to_string(serial_number);
685  }
686  else
687  {
688  msg = "[sl_oc::sensors::SensorCapture] WARNING: Sensors Module reset failed. Unable to find the Sensor Module of a ZED2 camera. Please verify the USB connection.";
689  }
690 
691  std::cerr << msg << std::endl;
692 
693  return false;
694  }
695 
696  std::string sn_str = std::to_string(found_sn);
697  std::wstring wide_sn_string = std::wstring(sn_str.begin(), sn_str.end());
698  const wchar_t* wsn = wide_sn_string.c_str();
699 
700  hid_device* devHandle = hid_open(SL_USB_VENDOR, pid, wsn );
701 
702  if(!devHandle)
703  {
704  std::string msg = "Unable to open the MCU HID device";
705  std::cerr << msg << std::endl;
706 
707  return false;
708  }
709 
710  unsigned char buf[65];
711  buf[0] = static_cast<unsigned char>(usb::REP_ID_REQUEST_SET);
712  buf[1] = static_cast<unsigned char>(usb::RQ_CMD_RST);
713 
714  hid_send_feature_report(devHandle, buf, 2);
715  // Note: cannot verify the return value of the `hid_send_feature_report` command because the MCU is suddenly reset
716  // and it cannot return a valid value
717 
718  sleep(2); // Wait for MCU and OV580 to reboot
719 
720  std::cerr << "[sl_oc::sensors::SensorCapture] INFO: Sensors Module reset successful" << std::endl;
721 
722  return true;
723 }
@ RQ_CMD_RST
Command to reset the MCU.
@ REP_ID_REQUEST_SET
USB Request report ID.

◆ resetVideoModule()

bool sl_oc::sensors::SensorCapture::resetVideoModule ( int  serial_number = 0)
static

Perform a reset of the video module without resetting the sensor module. To be called in case the Video module stops to work correctly.

Parameters
serial_numberThe serial number of the device to be reset (0 to reset the first available)
Returns
true if successful
Note
After the reset a new video::VideoCapture connection is required

Definition at line 725 of file sensorcapture.cpp.

726 {
727  int found_sn = serial_number;
728  unsigned short pid;
729  bool res = searchForConnectedDev(&found_sn, &pid);
730  if(!res)
731  {
732  std::string msg;
733  if(serial_number!=0)
734  {
735  msg = "[sl_oc::sensors::SensorCapture] WARNING: Video Module reset failed. Unable to find the Sensor Module with serial number ";
736  msg += std::to_string(serial_number);
737  }
738  else
739  {
740  msg = "[sl_oc::sensors::SensorCapture] WARNING: Video Module reset failed. Unable to find the Sensor Module of a ZED2 camera. Please verify the USB connection.";
741  }
742 
743  std::cerr << msg << std::endl;
744 
745  return false;
746  }
747 
748  std::string sn_str = std::to_string(found_sn);
749  std::wstring wide_sn_string = std::wstring(sn_str.begin(), sn_str.end());
750  const wchar_t* wsn = wide_sn_string.c_str();
751 
752  hid_device* devHandle = hid_open(SL_USB_VENDOR, pid, wsn );
753 
754  if(!devHandle)
755  {
756  std::string msg = "Unable to open the MCU HID device";
757  std::cerr << msg << std::endl;
758 
759  return false;
760  }
761 
764  cmd.cmd = usb::OV580_CMD_RESET;
765  cmd.info=0;
766 
767  unsigned char buf[65];
768  memcpy(buf, &(cmd.struct_id), sizeof(usb::OV580CmdStruct));
769 
770  int ret = hid_send_feature_report(devHandle, buf, sizeof(usb::OV580CmdStruct));
771  hid_close(devHandle);
772 
773  if(ret!=sizeof(usb::OV580CmdStruct)) {
774  std::cerr << "[sl_oc::sensors::SensorCapture] INFO: Video Module reset failed" << std::endl;
775  return false;
776  }
777 
778  sleep(2); // Wait for OV580 to reboot
779 
780  std::cerr << "[sl_oc::sensors::SensorCapture] INFO: Video Module reset successful" << std::endl;
781  return true;
782 }
@ OV580_CMD_RESET
Command to reset the OV580 using the MCU.
struct sl_oc::sensors::usb::_ov580_cmd_struct OV580CmdStruct
OV580 control using the MCU.
@ REP_ID_OV580_CMD
OV580 control request.
uint8_t struct_id
struct identifier for HID comm

◆ setStartTimestamp()

void sl_oc::sensors::SensorCapture::setStartTimestamp ( uint64_t  start_ts)
inline

Called by VideoCapture to sync timestamps reference point.

Definition at line 229 of file sensorcapture.hpp.

◆ setVideoPtr()

void sl_oc::sensors::SensorCapture::setVideoPtr ( video::VideoCapture videoPtr)
inline

Called by VideoCapture to set the pointer to it.

Definition at line 230 of file sensorcapture.hpp.

Referenced by sl_oc::video::VideoCapture::enableSensorSync().

◆ updateTimestampOffset()

void sl_oc::sensors::SensorCapture::updateTimestampOffset ( uint64_t  frame_ts)

Called by VideoCapture to update timestamp offset.

Definition at line 563 of file sensorcapture.cpp.

564 {
565  static int64_t offset_sum = 0;
566  static int count = 0;
567  offset_sum += (static_cast<int64_t>(mSyncTs) - static_cast<int64_t>(frame_ts));
568  count++;
569 
570  if(count==3)
571  {
572  int64_t offset = offset_sum/count;
573  mSyncOffset += offset;
574 #if 0
575  std::cout << "Offset: " << offset << std::endl;
576  std::cout << "mSyncOffset: " << mSyncOffset << std::endl;
577 #endif
578 
579  offset_sum = 0;
580  count=0;
581  }
582 }

The documentation for this class was generated from the following files: