ZED Open Capture  v0.6.0
Low level camera driver for the ZED stereo camera family
sensorcapture.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 SENSORCAPTURE_HPP
22 #define SENSORCAPTURE_HPP
23 
24 #include "defines.hpp"
25 
26 #include <thread>
27 #include <vector>
28 #include <map>
29 #include <mutex>
30 
31 #ifdef SENSORS_MOD_AVAILABLE
32 
33 #include "sensorcapture_def.hpp"
34 #include "hidapi.h"
35 
36 namespace sl_oc {
37 
38 #ifdef VIDEO_MOD_AVAILABLE
39 namespace video {
40 class VideoCapture;
41 }
42 #endif
43 
44 namespace sensors {
45 
46 namespace data {
47 
51 struct SL_OC_EXPORT Imu
52 {
53  // Validity of the magnetometer sensor data
54  typedef enum _imu_status {
55  NOT_PRESENT = 0,
56  OLD_VAL = 1,
57  NEW_VAL = 2
58  } ImuStatus;
59 
60  ImuStatus valid = NOT_PRESENT;
61  uint64_t timestamp = 0;
62  float aX;
63  float aY;
64  float aZ;
65  float gX;
66  float gY;
67  float gZ;
68  float temp;
69  bool sync;
70 };
71 
75 struct SL_OC_EXPORT Magnetometer
76 {
77  // Validity of the magnetometer sensor data
78  typedef enum _mag_status {
79  NOT_PRESENT = 0,
80  OLD_VAL = 1,
81  NEW_VAL = 2
82  } MagStatus;
83 
84  MagStatus valid = NOT_PRESENT;
85  uint64_t timestamp = 0;
86  float mX;
87  float mY;
88  float mZ;
89 };
90 
94 struct SL_OC_EXPORT Environment
95 {
96  // Validity of the environmental sensor data
97  typedef enum _env_status {
98  NOT_PRESENT = 0,
99  OLD_VAL = 1,
100  NEW_VAL = 2
101  } EnvStatus;
102 
103  EnvStatus valid = NOT_PRESENT;
104  uint64_t timestamp = 0;
105  float temp;
106  float press;
107  float humid;
108 };
109 
113 struct SL_OC_EXPORT Temperature
114 {
115  typedef enum _temp_status {
116  NOT_PRESENT = 0,
117  OLD_VAL = 1,
118  NEW_VAL = 2
119  } TempStatus;
120 
121  TempStatus valid = NOT_PRESENT;
122  uint64_t timestamp = 0;
123  float temp_left;
124  float temp_right;
125 };
126 
127 }
128 
132 class SL_OC_EXPORT SensorCapture
133 {
134  ZED_OC_VERSION_ATTRIBUTE;
135 
136 public:
142 
146  virtual ~SensorCapture();
147 
153  std::vector<int> getDeviceList(bool refresh=false);
154 
161  bool initializeSensors( int sn=-1 );
162 
168  void getFirmwareVersion( uint16_t& fw_major, uint16_t& fw_minor );
169 
174  int getSerialNumber();
175 
181  const data::Imu& getLastIMUData(uint64_t timeout_usec=1500);
182 
188  const data::Magnetometer& getLastMagnetometerData(uint64_t timeout_usec=100);
189 
195  const data::Environment& getLastEnvironmentData(uint64_t timeout_usec=100);
196 
202  const data::Temperature& getLastCameraTemperatureData(uint64_t timeout_usec=100);
203 
214  static bool resetSensorModule(int serial_number=0);
215 
225  static bool resetVideoModule(int serial_number=0);
226 
227 #ifdef VIDEO_MOD_AVAILABLE
228  void updateTimestampOffset(uint64_t frame_ts);
229  inline void setStartTimestamp(uint64_t start_ts){mStartSysTs=start_ts;}
230  inline void setVideoPtr(video::VideoCapture* videoPtr){mVideoPtr=videoPtr;}
231 #endif
232 
233 private:
234  static bool searchForConnectedDev(int* serial_number, unsigned short* found_pid);
235 
236  void grabThreadFunc();
237 
238  bool startCapture();
239 
240  bool open(uint16_t pid, int serial_number);
241  void close();
242 
243  int enumerateDevices();
244 
245  // ----> USB commands to MCU
246  bool enableDataStream(bool enable);
247  bool isDataStreamEnabled();
248  bool sendPing();
249  // ----> USB commands to MCU
250 
251 private:
252  // Flags
253  int mVerbose=0;
254  bool mNewIMUData=false;
255  bool mNewMagData=false;
256  bool mNewEnvData=false;
257  bool mNewCamTempData=false;
258 
259  bool mInitialized = false;
260  bool mStopCapture = false;
261  bool mGrabRunning = false;
262 
263  std::map<int,uint16_t> mSlDevPid;
264  std::map<int,uint16_t> mSlDevFwVer;
265 
266  hid_device* mDevHandle = nullptr;
267  int mDevSerial = -1;
268  int mDevFwVer = -1;
269  unsigned short mDevPid = 0;
270 
271  data::Imu mLastIMUData;
272  data::Magnetometer mLastMagData;
273  data::Environment mLastEnvData;
274  data::Temperature mLastCamTempData;
275 
276  std::thread mGrabThread;
277 
278  std::mutex mIMUMutex;
279  std::mutex mMagMutex;
280  std::mutex mEnvMutex;
281  std::mutex mCamTempMutex;
282 
283  uint64_t mStartSysTs=0;
284  uint64_t mLastMcuTs=0;
285 
286  bool mFirstImuData=true;
287 
288  // ----> Timestamp synchronization
289  uint64_t mLastFrameSyncCount=0;
290 
291  std::vector<uint64_t> mMcuTsQueue;
292  std::vector<uint64_t> mSysTsQueue;
293 
294  double mNTPTsScaling=1.0;
295  int mNTPAdjustedCount = 0;
296 
297  int64_t mSyncOffset=0;
298  // <---- Timestamp synchronization
299 
300 #ifdef VIDEO_MOD_AVAILABLE
301  video::VideoCapture* mVideoPtr=nullptr;
302  uint64_t mSyncTs=0;
303 #endif
304 
305 };
306 
307 }
308 }
309 
310 #endif
311 
322 #endif // SENSORCAPTURE_HPP
The SensorCapture class provides sensor grabbing functions for the Stereolabs ZED Mini and ZED2 camer...
void setVideoPtr(video::VideoCapture *videoPtr)
Called by VideoCapture to set the pointer to it.
void setStartTimestamp(uint64_t start_ts)
Called by VideoCapture to sync timestamps reference point.
The VideoCapture class provides image grabbing functions and settings control for all the Stereolabs ...
VERBOSITY
Definition: defines.hpp:85
@ ERROR
Definition: defines.hpp:87
Contains the acquired Environment data.
float press
Atmospheric pressure in hPa.
float temp
Sensor temperature in °C.
enum sl_oc::sensors::data::Environment::_env_status EnvStatus
Contains the acquired Imu data.
float gX
Angular velocity around X axis in °/s.
float aX
Acceleration along X axis in m/s²
enum sl_oc::sensors::data::Imu::_imu_status ImuStatus
bool sync
Indicates in IMU data are synchronized with a video frame.
float temp
Sensor temperature in °C.
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²
Contains the acquired Magnetometer data.
float mY
Acceleration along Y axis in uT.
float mX
Acceleration along X axis in uT.
enum sl_oc::sensors::data::Magnetometer::_mag_status MagStatus
float mZ
Acceleration along Z axis in uT.
Contains the acquired Camera Temperature data.
float temp_right
Temperature of the right CMOS camera sensor.
enum sl_oc::sensors::data::Temperature::_temp_status TempStatus
float temp_left
Temperature of the left CMOS camera sensor.