openni_device_primesense.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011 2011 Willow Garage, Inc.
5  * Suat Gedikli <gedikli@willowgarage.com>
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
39 #include <iostream>
40 #include <sstream>
41 #include <boost/thread/mutex.hpp>
42 
43 using namespace boost;
44 
45 namespace openni_wrapper
46 {
47 
48 DevicePrimesense::DevicePrimesense (xn::Context& context, const xn::NodeInfo& device_node, const xn::NodeInfo& image_node, const xn::NodeInfo& depth_node, const xn::NodeInfo& ir_node) throw (OpenNIException)
49 : OpenNIDevice (context, device_node, image_node, depth_node, ir_node)
50 {
51  // setup stream modes
52  enumAvailableModes ();
53  setDepthOutputMode (getDefaultDepthMode ());
54  setImageOutputMode (getDefaultImageMode ());
55  setIROutputMode (getDefaultIRMode ());
56 
57  unique_lock<mutex> image_lock(image_mutex_);
58  XnStatus status = image_generator_.SetIntProperty ("InputFormat", 5);
59  if (status != XN_STATUS_OK)
60  THROW_OPENNI_EXCEPTION ("Error setting the image input format to Uncompressed 8-bit BAYER. Reason: %s", xnGetStatusString (status));
61 
62  status = image_generator_.SetPixelFormat (XN_PIXEL_FORMAT_YUV422);
63  if (status != XN_STATUS_OK)
64  THROW_OPENNI_EXCEPTION ("Failed to set image pixel format to YUV422. Reason: %s", xnGetStatusString (status));
65 
66  image_lock.unlock ();
67 
68  lock_guard<mutex> depth_lock(depth_mutex_);
69  status = depth_generator_.SetIntProperty ("RegistrationType", 1);
70  if (status != XN_STATUS_OK)
71  THROW_OPENNI_EXCEPTION ("Error setting the registration type. Reason: %s", xnGetStatusString (status));
72 }
73 
74 DevicePrimesense::~DevicePrimesense () throw ()
75 {
76  setDepthRegistration ( false );
77  setSynchronization ( false );
78 
79  depth_mutex_.lock ();
80  depth_generator_.UnregisterFromNewDataAvailable (depth_callback_handle_);
81  depth_mutex_.unlock ();
82 
83  image_mutex_.lock ();
84  image_generator_.UnregisterFromNewDataAvailable (image_callback_handle_);
85  image_mutex_.unlock ();
86 }
87 
88 bool DevicePrimesense::isImageResizeSupported (unsigned input_width, unsigned input_height, unsigned output_width, unsigned output_height) const throw ()
89 {
90  return ImageYUV422::resizingSupported (input_width, input_height, output_width, output_height);
91 }
92 
93 //void DevicePrimesense::setImageOutputMode (const XnMapOutputMode& output_mode) throw (OpenNIException)
94 //{
95 // if (output_mode.nFPS == 30 && output_mode.nXRes == XN_UXGA_X_RES && output_mode.nYRes == XN_UXGA_Y_RES )
96 // {
97 // cout << "setting image mode to UXGA" << endl;
98 // if (isImageStreamRunning ())
99 // {
100 // stopImageStream ();
101 // XnStatus status = image_generator_.SetIntProperty ("InputFormat", 0);
102 // if (status != XN_STATUS_OK)
103 // THROW_OPENNI_EXCEPTION ("Error setting the image input format to compressed BAYER. Reason: %s", xnGetStatusString (status));
104 // status = image_generator_.SetPixelFormat (XN_PIXEL_FORMAT_RGB24);
105 // if (status != XN_STATUS_OK)
106 // THROW_OPENNI_EXCEPTION ("Failed to set image pixel format to YUV422. Reason: %s", xnGetStatusString (status));
107 // sleep (1);
108 // startImageStream ();
109 // }
110 // else
111 // {
112 // XnStatus status = image_generator_.SetIntProperty ("InputFormat", 0);
113 // if (status != XN_STATUS_OK)
114 // THROW_OPENNI_EXCEPTION ("Error setting the image input format to compressed BAYER. Reason: %s", xnGetStatusString (status));
115 //
116 // status = image_generator_.SetPixelFormat (XN_PIXEL_FORMAT_GRAYSCALE_8_BIT);
117 // if (status != XN_STATUS_OK)
118 // THROW_OPENNI_EXCEPTION ("Failed to set image pixel format to YUV422. Reason: %s", xnGetStatusString (status));
119 // }
120 // }
121 // else
122 // {
123 // XnStatus status = image_generator_.SetIntProperty ("InputFormat", 5);
124 // if (status != XN_STATUS_OK)
125 // THROW_OPENNI_EXCEPTION ("Error setting the image input format to uncompressed BAYER. Reason: %s", xnGetStatusString (status));
126 //
127 // status = image_generator_.SetPixelFormat (XN_PIXEL_FORMAT_YUV422);
128 // if (status != XN_STATUS_OK)
129 // THROW_OPENNI_EXCEPTION ("Failed to set image pixel format to YUV422. Reason: %s", xnGetStatusString (status));
130 // }
131 // OpenNIDevice::setImageOutputMode (output_mode);
132 //}
133 
134 void DevicePrimesense::enumAvailableModes () throw (OpenNIException)
135 {
136  XnMapOutputMode output_mode;
137  available_image_modes_.clear();
138  available_depth_modes_.clear();
139 
140  // Depth Modes
141  output_mode.nFPS = 30;
142  output_mode.nXRes = XN_VGA_X_RES;
143  output_mode.nYRes = XN_VGA_Y_RES;
144  available_depth_modes_.push_back (output_mode);
145 
146  output_mode.nFPS = 25;
147  output_mode.nXRes = XN_VGA_X_RES;
148  output_mode.nYRes = XN_VGA_Y_RES;
149  available_depth_modes_.push_back (output_mode);
150 
151  output_mode.nFPS = 25;
152  output_mode.nXRes = XN_QVGA_X_RES;
153  output_mode.nYRes = XN_QVGA_Y_RES;
154  available_depth_modes_.push_back (output_mode);
155 
156  output_mode.nFPS = 30;
157  output_mode.nXRes = XN_QVGA_X_RES;
158  output_mode.nYRes = XN_QVGA_Y_RES;
159  available_depth_modes_.push_back (output_mode);
160 
161  output_mode.nFPS = 60;
162  output_mode.nXRes = XN_QVGA_X_RES;
163  output_mode.nYRes = XN_QVGA_Y_RES;
164  available_depth_modes_.push_back (output_mode);
165 
166  // RGB Modes
167  output_mode.nFPS = 30;
168  output_mode.nXRes = XN_VGA_X_RES;
169  output_mode.nYRes = XN_VGA_Y_RES;
170  available_image_modes_.push_back (output_mode);
171 
172  output_mode.nFPS = 25;
173  output_mode.nXRes = XN_VGA_X_RES;
174  output_mode.nYRes = XN_VGA_Y_RES;
175  available_image_modes_.push_back (output_mode);
176 
177 // output_mode.nFPS = 30;
178 // output_mode.nXRes = XN_UXGA_X_RES;
179 // output_mode.nYRes = XN_UXGA_Y_RES;
180 // available_image_modes_.push_back (output_mode);
181 
182  output_mode.nFPS = 25;
183  output_mode.nXRes = XN_QVGA_X_RES;
184  output_mode.nYRes = XN_QVGA_Y_RES;
185  available_image_modes_.push_back (output_mode);
186 
187  output_mode.nFPS = 30;
188  output_mode.nXRes = XN_QVGA_X_RES;
189  output_mode.nYRes = XN_QVGA_Y_RES;
190  available_image_modes_.push_back (output_mode);
191 
192  output_mode.nFPS = 60;
193  output_mode.nXRes = XN_QVGA_X_RES;
194  output_mode.nYRes = XN_QVGA_Y_RES;
195  available_image_modes_.push_back (output_mode);
196 }
197 
198 boost::shared_ptr<Image> DevicePrimesense::getCurrentImage (boost::shared_ptr<xn::ImageMetaData> image_data) const throw ()
199 {
200  return boost::shared_ptr<Image> ( new ImageYUV422 (image_data) );
201 }
202 
203 void DevicePrimesense::startImageStream () throw (OpenNIException)
204 {
205  // Suat: Ugly workaround... but on some usb-ports its not possible to start the image stream after the depth stream.
206  // turning on and off registration solves for some reason the problem!
207 
208  if (isDepthStreamRunning ())
209  {
210  if (isDepthRegistered ())
211  {
212  // Reset the view point
213  setDepthRegistration (false);
214 
215  // Reset the view point
216  setDepthRegistration (true);
217 
218  // Reset the view point
219  setDepthRegistration (false);
220 
221  // Start the stream
222  OpenNIDevice::startImageStream ();
223 
224  // Register the stream
225  setDepthRegistration (true);
226  }
227  else
228  {
229  // Reset the view point
230  setDepthRegistration (true);
231  // Reset the view point
232  setDepthRegistration (false);
233 
234  // Start the stream
235  OpenNIDevice::startImageStream ();
236  }
237  }
238  else
239  // Start the stream
240  OpenNIDevice::startImageStream ();
241 }
242 
243 void DevicePrimesense::startDepthStream () throw (OpenNIException)
244 {
245  if (isDepthRegistered ())
246  {
247  // Reset the view point
248  setDepthRegistration (false);
249 
250  // Start the stream
251  OpenNIDevice::startDepthStream ();
252 
253  // Register the stream
254  setDepthRegistration (true);
255  }
256  else
257  // Start the stream
258  OpenNIDevice::startDepthStream ();
259 }
260 
261 } //namespace
#define THROW_OPENNI_EXCEPTION(format,...)
General exception class.
Concrete implementation of the interface Image for a YUV 422 image used by Primesense devices...
string status
Definition: test_launch.py:51
Class representing an astract device for Primesense or MS Kinect devices.
Definition: openni_device.h:66


openni_camera
Author(s): Patrick Mihelich, Suat Gedikli, Radu Bogdan Rusu
autogenerated on Mon Jun 10 2019 14:15:53