DepthCamera.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * TI Voxel Lib component.
  3. *
  4. * Copyright (c) 2014 Texas Instruments Inc.
  5. */
  6. #ifndef VOXEL_DEPTHCAMERA_H
  7. #define VOXEL_DEPTHCAMERA_H
  8. #include <Device.h>
  9. #include <Parameter.h>
  10. #include <Frame.h>
  11. #include "VideoMode.h"
  12. #include <FrameBuffer.h>
  13. #include <RegisterProgrammer.h>
  14. #include <Streamer.h>
  15. #include <PointCloudTransform.h>
  16. #include <Filter/FilterSet.h>
  17. #include "FrameStream.h"
  18. #include "FrameGenerator.h"
  19. #include "PointCloudFrameGenerator.h"
  20. #include "Configuration.h"
  21. #define CALIB_SECT_LENS "lens"
  22. #define CALIB_SECT_LENS_ID 0
  23. namespace Voxel
  24. {
  25. /**
  26. * \ingroup CamSys
  27. *
  28. * \brief This is primary class which provides API for a depth camera.
  29. *
  30. * DepthCamera is an abstract class which needs to be derived and implemented
  31. * for individual depth camera types.
  32. */
  33. class VOXEL_EXPORT DepthCamera
  34. {
  35. public:
  36. enum FrameType
  37. {
  38. FRAME_RAW_FRAME_UNPROCESSED = 0,
  39. FRAME_RAW_FRAME_PROCESSED = 1,
  40. FRAME_DEPTH_FRAME = 2,
  41. FRAME_XYZI_POINT_CLOUD_FRAME = 3,
  42. FRAME_TYPE_COUNT = 4 // This is just used for number of callback types
  43. };
  44. typedef Function<void (DepthCamera &camera, const Frame &frame, FrameType callBackType)> CallbackType;
  45. private:
  46. mutable Mutex _accessMutex; // This is locked by getters and setters which are public
  47. mutable Mutex _frameStreamWriterMutex;
  48. protected:
  49. DevicePtr _device;
  50. String _name, _id, _chipset;
  51. Map<String, ParameterPtr> _parameters;
  52. Ptr<RegisterProgrammer> _programmer;
  53. Ptr<Streamer> _streamer;
  54. // NOTE: Constructors of derived classes need to initialize the first two entries in this array
  55. FrameGeneratorPtr _frameGenerators[3];
  56. Ptr<PointCloudFrameGenerator> _pointCloudFrameGenerator;
  57. bool _parameterInit;
  58. FrameBufferManager<RawFrame> _rawFrameBuffers;
  59. FrameBufferManager<DepthFrame> _depthFrameBuffers;
  60. FrameBufferManager<PointCloudFrame> _pointCloudBuffers;
  61. FilterSet<RawFrame> _unprocessedFilters, _processedFilters;
  62. FilterSet<DepthFrame> _depthFilters;
  63. FrameStreamWriterPtr _frameStreamWriter;
  64. bool _addParameters(const Vector<ParameterPtr> &params);
  65. CallbackType _callback[FRAME_TYPE_COUNT];
  66. uint32_t _callBackTypesRegistered = 0;
  67. ThreadPtr _captureThread;
  68. // Callback the registered function for 'type' if present and decide whether continue processing or not
  69. virtual bool _callbackAndContinue(uint32_t &callBackTypesToBeCalled, FrameType type, const Frame &frame);
  70. virtual bool _start() = 0;
  71. virtual bool _stop() = 0;
  72. virtual bool _captureRawUnprocessedFrame(RawFramePtr &rawFrame) = 0;
  73. virtual bool _processRawFrame(const RawFramePtr &rawFrameInput, RawFramePtr &rawFrameOutput) = 0; // here output raw frame will have processed data, like ToF data for ToF cameras
  74. virtual bool _convertToDepthFrame(const RawFramePtr &rawFrame, DepthFramePtr &depthFrame) = 0;
  75. virtual bool _convertToPointCloudFrame(const DepthFramePtr &depthFrame, PointCloudFramePtr &pointCloudFrame);
  76. virtual void _captureLoop(); // the main capture loop
  77. void _captureThreadWrapper(); // this is non-virtual and simply calls _captureLoop
  78. bool _running, _isPaused; // is capture running?
  79. bool _writeToFrameStream(RawFramePtr &rawUnprocessed);
  80. // These protected getters and setters are not thread-safe. These are to be directly called only when nested calls are to be done from getter/setter to another.
  81. // Otherwise use the public functions
  82. template <typename T>
  83. bool _get(const String &name, T &value, bool refresh = false) const;
  84. template <typename T>
  85. bool _set(const String &name, const T &value);
  86. virtual bool _setFrameRate(const FrameRate &r) = 0;
  87. virtual bool _getFrameRate(FrameRate &r) const = 0;
  88. virtual bool _setFrameSize(const FrameSize &s) = 0;
  89. virtual bool _getFrameSize(FrameSize &s) const = 0;
  90. virtual bool _getMaximumFrameSize(FrameSize &s) const = 0;
  91. virtual bool _getMaximumFrameRate(FrameRate &frameRate, const FrameSize &forFrameSize) const = 0;
  92. virtual bool _getSupportedVideoModes(Vector<SupportedVideoMode> &supportedVideoModes) const = 0;
  93. virtual bool _getMaximumVideoMode(VideoMode &videoMode) const = 0;
  94. virtual bool _getBytesPerPixel(uint &bpp) const = 0;
  95. virtual bool _setBytesPerPixel(const uint &bpp) = 0;
  96. virtual bool _getROI(RegionOfInterest &roi) const = 0;
  97. virtual bool _setROI(const RegionOfInterest &roi) = 0;
  98. virtual bool _allowedROI(String &message) = 0;
  99. virtual bool _getFieldOfView(float &fovHalfAngle) const = 0;
  100. inline void _makeID() { _id = _name + "(" + _device->id() + ")"; }
  101. virtual bool _reset() = 0;
  102. virtual bool _onReset() = 0;
  103. virtual bool _applyConfigParams(const ConfigSet *params);
  104. virtual bool _saveCurrentProfileID(const int id) = 0;
  105. virtual bool _getCurrentProfileID(int &id) = 0;
  106. bool _init();
  107. inline Map<String, CalibrationInformation> &_getCalibrationInformationStructure() { return configFile._calibrationInformation; }
  108. public:
  109. MainConfigurationFile configFile; // This corresponds to camera specific configuration file
  110. DepthCamera(const String &name, const String &chipset, DevicePtr device);
  111. virtual bool isInitialized() const
  112. {
  113. return _programmer && _programmer->isInitialized() &&
  114. _streamer && _streamer->isInitialized() && _parameterInit;
  115. }
  116. inline const String &name() const { return _name; }
  117. inline const String &id() const { return _id; }
  118. inline const String &chipset() const { return _chipset; }
  119. virtual bool getSerialNumber(String &serialNumber) const;
  120. virtual bool setSerialNumber(const String &serialNumber);
  121. inline const DevicePtr &getDevice() const { return _device; }
  122. inline bool isRunning() const { return _running; }
  123. inline bool isPaused() const { return _isPaused; }
  124. bool pause();
  125. bool resume();
  126. template <typename T>
  127. bool getStreamParam(const String &name, T &value) const;
  128. bool refreshParams();
  129. template <typename T>
  130. bool get(const String &name, T &value, bool refresh = false) const;
  131. template <typename T>
  132. bool set(const String &name, const T &value);
  133. // WARNING: Avoid using get() and set() on ParameterPtr, obtained via getParam() or getParameters(). It is not thread-safe. Instead use get() and set() on DepthCamera
  134. inline const ParameterPtr getParam(const String &name) const;
  135. inline const Map<String, ParameterPtr> &getParameters() const { return _parameters; }
  136. inline bool setFrameRate(const FrameRate &r);
  137. inline bool getFrameRate(FrameRate &r) const;
  138. inline bool setFrameSize(const FrameSize &s);
  139. inline bool getFrameSize(FrameSize &s) const;
  140. inline bool getMaximumFrameSize(FrameSize &s) const;
  141. inline bool getMaximumFrameRate(FrameRate &frameRate, const FrameSize &forFrameSize) const;
  142. inline bool getSupportedVideoModes(Vector<SupportedVideoMode> &supportedVideoModes) const;
  143. inline bool getMaximumVideoMode(VideoMode &videoMode) const;
  144. inline bool getBytesPerPixel(uint &bpp) const;
  145. inline bool setBytesPerPixel(const uint &bpp);
  146. inline bool getROI(RegionOfInterest &roi);
  147. inline bool setROI(const RegionOfInterest &roi);
  148. inline bool allowedROI(String &message);
  149. inline bool getFieldOfView(float &fovHalfAngle) const;
  150. virtual bool saveFrameStream(const String &fileName);
  151. virtual bool isSavingFrameStream();
  152. virtual bool closeFrameStream();
  153. virtual bool registerCallback(FrameType type, CallbackType f);
  154. virtual bool clearAllCallbacks();
  155. virtual bool clearCallback(FrameType type);
  156. // beforeFilterIndex = -1 => at the end, otherwise at location before the given filter index.
  157. // Return value:
  158. // >= 0 => add successfully with return value as filter ID.
  159. // -1 => failed to add filter
  160. virtual int addFilter(FilterPtr p, FrameType frameType, int beforeFilterID = -1);
  161. virtual FilterPtr getFilter(int filterID, FrameType frameType) const;
  162. virtual bool removeFilter(int filterID, FrameType frameType);
  163. virtual bool removeAllFilters(FrameType frameType);
  164. virtual void resetFilters();
  165. inline const FilterSet<RawFrame> &getUnprocessedRawFilterSet() { return _unprocessedFilters; }
  166. inline const FilterSet<RawFrame> &getProcessedRawFilterSet() { return _processedFilters; }
  167. inline const FilterSet<DepthFrame> &getDepthFilterSet() { return _depthFilters; }
  168. bool start();
  169. bool stop();
  170. void wait();
  171. bool reset();
  172. inline Ptr<RegisterProgrammer> getProgrammer() { return _programmer; } // RegisterProgrammer is usually thread-safe to use outside directly
  173. inline Ptr<Streamer> getStreamer() { return _streamer; } // Streamer may not be thread-safe
  174. inline bool reloadConfiguration() { return configFile.read(_name + ".conf"); }
  175. inline const Map<int, String> &getCameraProfileNames() { return configFile.getCameraProfileNames(); }
  176. inline int getCurrentCameraProfileID() { return configFile.getCurrentProfileID(); }
  177. int addCameraProfile(const String &profileName, const int parentID);
  178. bool setCameraProfile(const int id, bool softApply = false);
  179. bool removeCameraProfile(const int id);
  180. inline bool saveCameraProfileToHardware(int &id, bool saveParents = false, bool setAsDefault = false, const String &namePrefix = "") { return configFile.saveCameraProfileToHardware(id, saveParents, setAsDefault, namePrefix); }
  181. bool close();
  182. virtual ~DepthCamera();
  183. };
  184. template <typename T>
  185. bool DepthCamera::get(const String &name, T &value, bool refresh) const
  186. {
  187. Lock<Mutex> _(_accessMutex);
  188. return _get(name, value, refresh);
  189. }
  190. template <typename T>
  191. bool DepthCamera::set(const String &name, const T &value)
  192. {
  193. Lock<Mutex> _(_accessMutex);
  194. return _set(name, value);
  195. }
  196. bool DepthCamera::getFieldOfView(float &fovHalfAngle) const
  197. {
  198. Lock<Mutex> _(_accessMutex);
  199. return _getFieldOfView(fovHalfAngle);
  200. }
  201. bool DepthCamera::getFrameRate(FrameRate &r) const
  202. {
  203. Lock<Mutex> _(_accessMutex);
  204. return _getFrameRate(r);
  205. }
  206. bool DepthCamera::setFrameRate(const FrameRate &r)
  207. {
  208. Lock<Mutex> _(_accessMutex);
  209. return _setFrameRate(r);
  210. }
  211. bool DepthCamera::getFrameSize(FrameSize &s) const
  212. {
  213. Lock<Mutex> _(_accessMutex);
  214. return _getFrameSize(s);
  215. }
  216. bool DepthCamera::setFrameSize(const FrameSize &s)
  217. {
  218. Lock<Mutex> _(_accessMutex);
  219. return _setFrameSize(s);
  220. }
  221. bool DepthCamera::getMaximumFrameSize(FrameSize &s) const
  222. {
  223. Lock<Mutex> _(_accessMutex);
  224. return _getMaximumFrameSize(s);
  225. }
  226. bool DepthCamera::getMaximumFrameRate(FrameRate &frameRate, const FrameSize &forFrameSize) const
  227. {
  228. Lock<Mutex> _(_accessMutex);
  229. return _getMaximumFrameRate(frameRate, forFrameSize);
  230. }
  231. bool DepthCamera::getSupportedVideoModes(Vector<SupportedVideoMode> &supportedVideoModes) const
  232. {
  233. Lock<Mutex> _(_accessMutex);
  234. return _getSupportedVideoModes(supportedVideoModes);
  235. }
  236. bool DepthCamera::getMaximumVideoMode(VideoMode &videoMode) const
  237. {
  238. Lock<Mutex> _(_accessMutex);
  239. return _getMaximumVideoMode(videoMode);
  240. }
  241. bool DepthCamera::getBytesPerPixel(uint &bpp) const
  242. {
  243. Lock<Mutex> _(_accessMutex);
  244. return _getBytesPerPixel(bpp);
  245. }
  246. bool DepthCamera::setBytesPerPixel(const uint &bpp)
  247. {
  248. Lock<Mutex> _(_accessMutex);
  249. return _setBytesPerPixel(bpp);
  250. }
  251. bool DepthCamera::allowedROI(String &message)
  252. {
  253. Lock<Mutex> _(_accessMutex);
  254. return _allowedROI(message);
  255. }
  256. bool DepthCamera::getROI(RegionOfInterest &roi)
  257. {
  258. Lock<Mutex> _(_accessMutex);
  259. return _getROI(roi);
  260. }
  261. bool DepthCamera::setROI(const RegionOfInterest &roi)
  262. {
  263. Lock<Mutex> _(_accessMutex);
  264. return _setROI(roi);
  265. }
  266. template <typename T>
  267. bool DepthCamera::_get(const String &name, T &value, bool refresh) const
  268. {
  269. auto p = _parameters.find(name);
  270. if(p != _parameters.end())
  271. {
  272. ParameterTemplate<T> *param = dynamic_cast<ParameterTemplate<T> *>(p->second.get());
  273. if(param == 0)
  274. {
  275. logger(LOG_ERROR) << "DepthCamera: Invalid value type '" << typeid(value).name() << "' used to get parameter " << _id << "." << name << std::endl;
  276. return false;
  277. }
  278. if(!param->get(value, refresh))
  279. {
  280. logger(LOG_ERROR) << "DepthCamera:Could not get value for parameter " << _id << "." << name << std::endl;
  281. return false;
  282. }
  283. return true;
  284. }
  285. else
  286. {
  287. logger(LOG_ERROR) << "DepthCamera: Unknown parameter " << _id << "." << name << std::endl;
  288. return false;
  289. }
  290. }
  291. template <typename T>
  292. bool DepthCamera::_set(const String &name, const T &value)
  293. {
  294. auto p = _parameters.find(name);
  295. if(p != _parameters.end())
  296. {
  297. logger(LOG_DEBUG) << "DepthCamera: Setting parameter '" << name << "' = " << value << std::endl;
  298. ParameterTemplate<T> *param = dynamic_cast<ParameterTemplate<T> *>(p->second.get());
  299. if(param == 0)
  300. {
  301. logger(LOG_ERROR) << "DepthCamera: Invalid value type '" << typeid(value).name() << "' used to set parameter " << this->name() << "(" << _device->id() << ")." << name << std::endl;
  302. return false;
  303. }
  304. if(!param->set(value))
  305. {
  306. logger(LOG_ERROR) << "DepthCamera: Could not set value " << value << " for parameter " << this->name() << "(" << _device->id() << ")." << name << std::endl;
  307. return false;
  308. }
  309. return true;
  310. }
  311. else
  312. {
  313. logger(LOG_ERROR) << "DepthCamera: Unknown parameter " << _id << "." << name << std::endl;
  314. return false;
  315. }
  316. }
  317. const ParameterPtr DepthCamera::getParam(const String &name) const
  318. {
  319. auto p = _parameters.find(name);
  320. if(p != _parameters.end())
  321. return p->second;
  322. else
  323. {
  324. logger(LOG_ERROR) << "DepthCamera: Unknown parameter " << _id << "." << name << std::endl;
  325. return 0;
  326. }
  327. }
  328. template <typename T>
  329. bool DepthCamera::getStreamParam(const String &name, T &value) const
  330. {
  331. if(!_frameGenerators[0]->get(name, value) && !_frameGenerators[1]->get(name, value) && !_frameGenerators[2]->get(name, value))
  332. return false;
  333. return true;
  334. }
  335. typedef Ptr<DepthCamera> DepthCameraPtr;
  336. }
  337. #endif // DEPTHCAMERA_H