1 #ifndef FREENECT_DEVICE_T01IELX0 2 #define FREENECT_DEVICE_T01IELX0 4 #include <boost/shared_ptr.hpp> 5 #include <boost/noncopyable.hpp> 6 #include <boost/thread/mutex.hpp> 7 #include <boost/date_time/posix_time/ptime.hpp> 10 #include <libfreenect.h> 11 #include <libfreenect_registration.h> 24 return buffer.
metadata.video_format == FREENECT_VIDEO_BAYER;
61 ROS_INFO(
"Starting a 3s RGB and Depth stream flush.");
64 void openDevice(freenect_context* driver, std::string serial) {
65 if (freenect_open_device_by_camera_serial(driver, &
device_, serial.c_str()) < 0) {
66 throw std::runtime_error(
"[ERROR] Unable to open specified kinect");
68 freenect_set_user(
device_,
this);
94 return PRODUCT_NAME.c_str();
126 throw std::runtime_error(
"[ERROR] The kinect does not support hardware synchronization");
133 return 0.01 *
registration_.zero_plane_info.dcmos_emitter_dist;
139 void (T::*callback)(
const ImageBuffer& image,
void* cookie),
140 T& instance,
void* cookie = NULL) {
141 image_callback_ = boost::bind(callback, boost::ref(instance), _1, cookie);
145 void (T::*callback)(
const ImageBuffer& depth_image,
void* cookie),
146 T& instance,
void* cookie = NULL) {
147 depth_callback_ = boost::bind(callback, boost::ref(instance), _1, cookie);
151 void (T::*callback)(
const ImageBuffer& ir_image,
void* cookie),
152 T& instance,
void* cookie = NULL) {
153 ir_callback_ = boost::bind(callback, boost::ref(instance), _1, cookie);
163 boost::lock_guard<boost::recursive_mutex> lock(
m_settings_);
168 boost::lock_guard<boost::recursive_mutex> lock(
m_settings_);
173 return FREENECT_RESOLUTION_MEDIUM;
177 freenect_frame_mode new_mode =
179 if (!new_mode.is_valid) {
183 compatible_mode = mode;
188 boost::lock_guard<boost::recursive_mutex> lock(
m_settings_);
195 boost::lock_guard<boost::recursive_mutex> lock(
m_settings_);
202 boost::lock_guard<boost::recursive_mutex> lock(
m_settings_);
207 boost::lock_guard<boost::recursive_mutex> lock(
m_settings_);
213 boost::lock_guard<boost::recursive_mutex> lock(
m_settings_);
219 boost::lock_guard<boost::recursive_mutex> lock(
m_settings_);
226 boost::lock_guard<boost::recursive_mutex> lock(
m_settings_);
231 boost::lock_guard<boost::recursive_mutex> lock(
m_settings_);
236 return FREENECT_RESOLUTION_MEDIUM;
240 freenect_frame_mode new_mode =
242 if (!new_mode.is_valid) {
246 compatible_mode = mode;
251 boost::lock_guard<boost::recursive_mutex> lock(
m_settings_);
256 boost::lock_guard<boost::recursive_mutex> lock(
m_settings_);
258 (enable) ? FREENECT_DEPTH_REGISTERED : FREENECT_DEPTH_MM;
262 boost::lock_guard<boost::recursive_mutex> lock(
m_settings_);
267 boost::lock_guard<boost::recursive_mutex> lock(
m_settings_);
272 boost::lock_guard<boost::recursive_mutex> lock(
m_settings_);
279 freenect_device *dev,
void *depth, uint32_t timestamp) {
287 freenect_device *dev,
void *video, uint32_t timestamp) {
328 boost::lock_guard<boost::recursive_mutex> lock(m_settings_);
331 bool stop_device_flush =
false;
333 if (device_flush_enabled_) {
334 boost::posix_time::ptime current_time =
335 boost::posix_time::microsec_clock::local_time();
336 if ((current_time - device_flush_start_time_).total_milliseconds() > 3000) {
337 device_flush_enabled_ =
false;
338 stop_device_flush =
true;
339 ROS_INFO(
"Stopping device RGB and Depth stream flush.");
343 bool change_video_settings =
344 video_buffer_.
metadata.video_format != new_video_format_ ||
345 video_buffer_.
metadata.resolution != new_video_resolution_ ||
347 device_flush_enabled_ && !streaming_video_ ||
350 if (change_video_settings) {
353 if (streaming_video_) {
355 freenect_stop_video(device_);
356 streaming_video_ =
false;
360 if (video_buffer_.
metadata.resolution != new_video_resolution_ ||
361 video_buffer_.
metadata.video_format != new_video_format_) {
364 new_video_resolution_, registration_);
365 }
catch (std::runtime_error& e) {
366 printf(
"[ERROR] Unsupported video format/resolution provided. %s\n",
368 printf(
"[INFO] Setting default settings (RGB/VGA)\n");
370 FREENECT_RESOLUTION_MEDIUM, registration_);
372 freenect_set_video_mode(device_, video_buffer_.
metadata);
373 freenect_set_video_buffer(device_, video_buffer_.
image_buffer.get());
374 new_video_resolution_ = video_buffer_.
metadata.resolution;
375 new_video_format_ = video_buffer_.
metadata.video_format;
378 if (should_stream_video_ || device_flush_enabled_) {
379 int rgb_out = freenect_start_video(device_);
381 streaming_video_ =
true;
388 bool change_depth_settings =
389 depth_buffer_.
metadata.depth_format != new_depth_format_ ||
390 depth_buffer_.
metadata.resolution != new_depth_resolution_ ||
392 device_flush_enabled_ && !streaming_depth_ ||
395 if (change_depth_settings) {
398 if (streaming_depth_) {
400 freenect_stop_depth(device_);
401 streaming_depth_ =
false;
405 if (depth_buffer_.
metadata.resolution != new_depth_resolution_ ||
406 depth_buffer_.
metadata.depth_format != new_depth_format_) {
409 new_depth_resolution_, registration_);
410 }
catch (std::runtime_error& e) {
411 printf(
"[ERROR] Unsupported depth format/resolution provided. %s\n",
413 printf(
"[INFO] Setting default settings (depth registered/VGA)\n");
415 FREENECT_RESOLUTION_MEDIUM, registration_);
417 freenect_set_depth_mode(device_, depth_buffer_.
metadata);
418 freenect_set_depth_buffer(device_, depth_buffer_.
image_buffer.get());
419 new_depth_resolution_ = depth_buffer_.
metadata.resolution;
420 new_depth_format_ = depth_buffer_.
metadata.depth_format;
423 if (should_stream_depth_ || device_flush_enabled_) {
424 int depth_out = freenect_start_depth(device_);
426 streaming_depth_ =
true;
433 boost::lock_guard<boost::recursive_mutex> lock(m_settings_);
438 boost::lock_guard<boost::mutex> buffer_lock(depth_buffer_.
mutex);
440 if (publishers_ready_) {
446 boost::lock_guard<boost::mutex> buffer_lock(video_buffer_.
mutex);
448 if (publishers_ready_) {
bool setSynchronization(bool on_off) const
freenect_resolution new_depth_resolution_
static void freenectDepthCallback(freenect_device *dev, void *depth, uint32_t timestamp)
bool hasImageStream() const
bool _isImageModeEnabled()
void setDepthRegistration(bool enable)
void registerIRCallback(void(T::*callback)(const ImageBuffer &ir_image, void *cookie), T &instance, void *cookie=NULL)
OutputMode getDefaultImageMode() const
void videoCallback(void *video)
bool hasDepthStream() const
freenect_resolution new_video_resolution_
void setImageOutputMode(OutputMode mode)
void openDevice(freenect_context *driver, std::string serial)
boost::posix_time::ptime device_flush_start_time_
bool isSynchronized() const
static const std::string VENDOR_NAME
bool device_flush_enabled_
void allocateBufferDepth(ImageBuffer &buffer, const freenect_depth_format &format, const freenect_resolution &resolution, const freenect_registration ®istration)
freenect_context * driver_
bool should_stream_video_
void registerImageCallback(void(T::*callback)(const ImageBuffer &image, void *cookie), T &instance, void *cookie=NULL)
boost::shared_array< unsigned char > image_buffer
freenect_resolution OutputMode
bool isImageStreamRunning()
unsigned getAddress() const
bool findCompatibleImageMode(const OutputMode &mode, OutputMode &compatible_mode) const
static const unsigned VENDOR_ID
static void freenectVideoCallback(freenect_device *dev, void *video, uint32_t timestamp)
boost::function< void(const ImageBuffer &)> depth_callback_
Holds an image buffer with all the metadata required to transmit the image over ROS channels...
bool isImageMode(const ImageBuffer &buffer)
void publishersAreReady()
OutputMode getImageOutputMode()
void registerDepthCallback(void(T::*callback)(const ImageBuffer &depth_image, void *cookie), T &instance, void *cookie=NULL)
float getBaseline() const
FreenectDevice(freenect_context *driver, std::string serial)
ImageBuffer depth_buffer_
bool isDepthStreamRunning()
std::string device_serial_
freenect_video_format new_video_format_
bool should_stream_depth_
bool findCompatibleDepthMode(const OutputMode &mode, OutputMode &compatible_mode) const
freenect_device * device_
OutputMode getDefaultDepthMode() const
bool isDepthRegistrationSupported() const
freenect_registration registration_
void flushDeviceStreams()
freenect_depth_format new_depth_format_
void setDepthOutputMode(OutputMode mode)
boost::function< void(const ImageBuffer &)> image_callback_
const char * getSerialNumber() const
void allocateBufferVideo(ImageBuffer &buffer, const freenect_video_format &format, const freenect_resolution &resolution, const freenect_registration ®istration)
boost::function< void(const ImageBuffer &)> ir_callback_
static const std::string PRODUCT_NAME
OutputMode getDepthOutputMode()
freenect_frame_mode metadata
static const unsigned PRODUCT_ID
ImageBuffer video_buffer_
const char * getProductName() const
bool isSynchronizationSupported() const
void depthCallback(void *depth)
boost::recursive_mutex m_settings_