OpenNIGrabber.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
10 #include <pcl/io/pcd_io.h>
11 #include <pcl/point_types.h>
12 #include <pcl/surface/mls.h>
13 #include "OpenNIGrabber.h"
14 #include "hrpsys/idl/pointcloud.hh"
15 
16 // Module specification
17 // <rtc-template block="module_spec">
18 static const char* spec[] =
19  {
20  "implementation_id", "OpenNIGrabber",
21  "type_name", "OpenNIGrabber",
22  "description", "OpenNI grabber",
23  "version", HRPSYS_PACKAGE_VERSION,
24  "vendor", "AIST",
25  "category", "example",
26  "activity_type", "DataFlowComponent",
27  "max_instance", "10",
28  "language", "C++",
29  "lang_type", "compile",
30  // Configuration variables
31  "conf.default.outputColorImage", "0",
32  "conf.default.outputDepthImage", "0",
33  "conf.default.outputPointCloud", "0",
34  "conf.default.outputPointCloudRGBA", "0",
35  "conf.default.debugLevel", "0",
36 
37  ""
38  };
39 // </rtc-template>
40 
42  : RTC::DataFlowComponentBase(manager),
43  // <rtc-template block="initializer">
44  m_cloudOut("cloud", m_cloud),
45  m_imageOut("image", m_image),
46  m_depthOut("depth", m_depth),
47  // </rtc-template>
48  m_interface(NULL),
49  m_requestToWriteImage(false),
50  m_requestToWritePointCloud(false),
51  dummy(0)
52 {
53 }
54 
56 {
57 }
58 
59 
60 
61 RTC::ReturnCode_t OpenNIGrabber::onInitialize()
62 {
63  std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
64  // <rtc-template block="bind_config">
65  // Bind variables and configuration variable
66  bindParameter("outputColorImage", m_outputColorImage, "0");
67  bindParameter("outputDepthImage", m_outputDepthImage, "0");
68  bindParameter("outputPointCloud", m_outputPointCloud, "0");
69  bindParameter("outputPointCloudRGBA", m_outputPointCloudRGBA, "0");
70  bindParameter("debugLevel", m_debugLevel, "0");
71 
72  // </rtc-template>
73 
74  // Registration: InPort/OutPort/Service
75  // <rtc-template block="registration">
76  // Set InPort buffers
77 
78  // Set OutPort buffer
79  addOutPort("cloudOut", m_cloudOut);
80  addOutPort("imageOut", m_imageOut);
81  addOutPort("depthOut", m_depthOut);
82 
83  // Set service provider to Ports
84 
85  // Set service consumers to Ports
86 
87  // Set CORBA Service Ports
88 
89  // </rtc-template>
90 
92 
93  return RTC::RTC_OK;
94 }
95 
96 
98 {
99  if (!m_requestToWriteImage) return;
100 
101  outputColorImage(image);
102 
103  m_requestToWriteImage = false;
104 }
105 
107 {
109 
110  Img::ImageData &id = m_image.data.image;
111  id.width = image->getWidth();
112  id.height = image->getHeight();
113  id.format = Img::CF_RGB;
114  id.raw_data.length(id.width*id.height*3);
115 
116  unsigned char *dst = (unsigned char*)id.raw_data.get_buffer();
117  unsigned char *src = (unsigned char*)image->getData();
118  memcpy(dst, src, id.raw_data.length());
119 
120  m_imageOut.write();
121 }
122 
124 {
125  if (!m_requestToWriteImage) return;
126 
127  outputDepthImage(image);
128 
129  m_requestToWriteImage = false;
130 }
131 
133 {
135 
136  Img::ImageData &id = m_depth.data.image;
137  id.width = image->getWidth();
138  id.height = image->getHeight();
139  id.format = Img::CF_DEPTH;
140  id.raw_data.length(id.width*id.height*2);
141 
142  unsigned char *dst = (unsigned char*)id.raw_data.get_buffer();
143  unsigned char *src = (unsigned char*)image->getData();
144  memcpy(dst, src, id.raw_data.length());
145 
146  m_depthOut.write();
147 }
148 
150 {
151  if (!m_requestToWriteImage) return;
152 
153  outputColorImage(image);
154  outputDepthImage(depth);
155 
156  m_requestToWriteImage = false;
157 }
158 
159 void OpenNIGrabber::grabberCallbackPointCloudRGBA(const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr &cloud)
160 {
161  if (!m_requestToWritePointCloud) return;
162 
164 
165  m_cloud.width = cloud->width;
166  m_cloud.height = cloud->height;
167  m_cloud.row_step = m_cloud.point_step*m_cloud.width;
168  m_cloud.data.length(m_cloud.height*m_cloud.row_step);
169 
170  float *dst_cloud = (float *)m_cloud.data.get_buffer();
171  for (unsigned int i=0; i<cloud->points.size(); i++){
172  dst_cloud[0] = cloud->points[i].x;
173  dst_cloud[1] = -cloud->points[i].y;
174  dst_cloud[2] = -cloud->points[i].z;
175  unsigned char *rgb = (unsigned char *)(dst_cloud+3);
176  rgb[0] = cloud->points[i].r;
177  rgb[1] = cloud->points[i].g;
178  rgb[2] = cloud->points[i].b;
179  dst_cloud += 4;
180  }
181  m_cloudOut.write();
183 }
184 
185 void OpenNIGrabber::grabberCallbackPointCloud(const pcl::PointCloud<pcl::PointXYZ>::ConstPtr &cloud)
186 {
187  if (!m_requestToWritePointCloud) return;
188 
190  m_cloud.width = cloud->width;
191  m_cloud.height = cloud->height;
192  m_cloud.row_step = m_cloud.point_step*m_cloud.width;
193  m_cloud.data.length(m_cloud.height*m_cloud.row_step);
194 
195  float *dst_cloud = (float *)m_cloud.data.get_buffer();
196  for (unsigned int i=0; i<cloud->points.size(); i++){
197  dst_cloud[0] = cloud->points[i].x;
198  dst_cloud[1] = -cloud->points[i].y;
199  dst_cloud[2] = -cloud->points[i].z;
200  dst_cloud += 4;
201  }
202  m_cloudOut.write();
204 }
205 
206 /*
207 RTC::ReturnCode_t OpenNIGrabber::onFinalize()
208 {
209  return RTC::RTC_OK;
210 }
211 */
212 
213 /*
214 RTC::ReturnCode_t OpenNIGrabber::onStartup(RTC::UniqueId ec_id)
215 {
216  return RTC::RTC_OK;
217 }
218 */
219 
220 /*
221 RTC::ReturnCode_t OpenNIGrabber::onShutdown(RTC::UniqueId ec_id)
222 {
223  return RTC::RTC_OK;
224 }
225 */
226 
228 {
229  std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
230 
231  try {
232  m_interface = new pcl::io::OpenNI2Grabber();
233 
235  boost::function<void (const boost::shared_ptr<pcl::io::Image>&)> f = boost::bind(&OpenNIGrabber::grabberCallbackColorImage, this, _1);
236  m_interface->registerCallback(f);
237  }else if (!m_outputColorImage && m_outputDepthImage){
238  boost::function<void (const boost::shared_ptr<pcl::io::DepthImage>&)> f = boost::bind(&OpenNIGrabber::grabberCallbackDepthImage, this, _1);
239  m_interface->registerCallback(f);
241  boost::function<void (const boost::shared_ptr<pcl::io::Image>&, const boost::shared_ptr<pcl::io::DepthImage>&, float)> f = boost::bind(&OpenNIGrabber::grabberCallbackColorAndDepthImage, this, _1, _2, _3);
242  m_interface->registerCallback(f);
243  }
244  if (m_outputPointCloud){
245  m_cloud.type = "xyz";
246  m_cloud.fields.length(3);
247  m_cloud.fields[0].name = "x";
248  m_cloud.fields[0].offset = 0;
249  m_cloud.fields[0].data_type = PointCloudTypes::FLOAT32;
250  m_cloud.fields[0].count = 4;
251  m_cloud.fields[1].name = "y";
252  m_cloud.fields[1].offset = 4;
253  m_cloud.fields[1].data_type = PointCloudTypes::FLOAT32;
254  m_cloud.fields[1].count = 4;
255  m_cloud.fields[2].name = "z";
256  m_cloud.fields[2].offset = 8;
257  m_cloud.fields[2].data_type = PointCloudTypes::FLOAT32;
258  m_cloud.fields[2].count = 4;
259  m_cloud.is_bigendian = false;
260  m_cloud.point_step = 16;
261  m_cloud.is_dense = false;
262  boost::function<void (const pcl::PointCloud<pcl::PointXYZ>::ConstPtr&)> f = boost::bind(&OpenNIGrabber::grabberCallbackPointCloud, this, _1);
263  m_interface->registerCallback(f);
264  }else if(m_outputPointCloudRGBA){
265  m_cloud.type = "xyzrgb";
266  m_cloud.fields.length(6);
267  m_cloud.fields[0].name = "x";
268  m_cloud.fields[0].offset = 0;
269  m_cloud.fields[0].data_type = PointCloudTypes::FLOAT32;
270  m_cloud.fields[0].count = 4;
271  m_cloud.fields[1].name = "y";
272  m_cloud.fields[1].offset = 4;
273  m_cloud.fields[1].data_type = PointCloudTypes::FLOAT32;
274  m_cloud.fields[1].count = 4;
275  m_cloud.fields[2].name = "z";
276  m_cloud.fields[2].offset = 8;
277  m_cloud.fields[2].data_type = PointCloudTypes::FLOAT32;
278  m_cloud.fields[2].count = 4;
279  m_cloud.fields[3].name = "r";
280  m_cloud.fields[3].offset = 12;
281  m_cloud.fields[3].data_type = PointCloudTypes::UINT8;
282  m_cloud.fields[3].count = 1;
283  m_cloud.fields[4].name = "g";
284  m_cloud.fields[4].offset = 13;
285  m_cloud.fields[4].data_type = PointCloudTypes::UINT8;
286  m_cloud.fields[4].count = 1;
287  m_cloud.fields[5].name = "b";
288  m_cloud.fields[5].offset = 14;
289  m_cloud.fields[5].data_type = PointCloudTypes::UINT8;
290  m_cloud.fields[5].count = 1;
291  m_cloud.is_bigendian = false;
292  m_cloud.point_step = 16;
293  m_cloud.is_dense = false;
294  boost::function<void (const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr&)> f = boost::bind(&OpenNIGrabber::grabberCallbackPointCloudRGBA, this, _1);
295  m_interface->registerCallback(f);
296  }
297 
298  m_interface->start();
299  }catch(pcl::IOException& ex){
300  std::cerr << "[" << m_profile.instance_name << "] Error: " << ex.what() << std::endl;
301  return RTC::RTC_ERROR;
302  }catch(...){
303  std::cerr << "[" << m_profile.instance_name
304  << "] Error: An exception occurred while starting grabber" << std::endl;
305  return RTC::RTC_ERROR;
306  }
307 
308  return RTC::RTC_OK;
309 }
310 
312 {
313  std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
314 
315  if (m_interface){
316  m_interface->stop();
317 
318  delete m_interface;
319  }
320 
321  return RTC::RTC_OK;
322 }
323 
324 RTC::ReturnCode_t OpenNIGrabber::onExecute(RTC::UniqueId ec_id)
325 {
326  //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
327  if (m_debugLevel>0){
328  if (m_interface->isRunning()){
329  std::cerr << "[" << m_profile.instance_name
330  << "] grabber is running. frame rate=" << m_interface->getFramesPerSecond() << std::endl;
331  }else{
332  std::cerr << "[" << m_profile.instance_name
333  << "] grabber is not running" << std::endl;
334  }
335  }
336  m_requestToWriteImage = true;
338 
339  return RTC::RTC_OK;
340 }
341 
342 /*
343 RTC::ReturnCode_t OpenNIGrabber::onAborting(RTC::UniqueId ec_id)
344 {
345  return RTC::RTC_OK;
346 }
347 */
348 
349 /*
350 RTC::ReturnCode_t OpenNIGrabber::onError(RTC::UniqueId ec_id)
351 {
352  return RTC::RTC_OK;
353 }
354 */
355 
356 /*
357 RTC::ReturnCode_t OpenNIGrabber::onReset(RTC::UniqueId ec_id)
358 {
359  return RTC::RTC_OK;
360 }
361 */
362 
363 /*
364 RTC::ReturnCode_t OpenNIGrabber::onStateUpdate(RTC::UniqueId ec_id)
365 {
366  return RTC::RTC_OK;
367 }
368 */
369 
370 /*
371 RTC::ReturnCode_t OpenNIGrabber::onRateChanged(RTC::UniqueId ec_id)
372 {
373  return RTC::RTC_OK;
374 }
375 */
376 
377 
378 
379 extern "C"
380 {
381 
383  {
385  manager->registerFactory(profile,
386  RTC::Create<OpenNIGrabber>,
387  RTC::Delete<OpenNIGrabber>);
388  }
389 
390 };
391 
392 
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
OutPort< Img::TimedCameraImage > m_depthOut
bool m_outputColorImage
OutPort< PointCloudTypes::PointCloud > m_cloudOut
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
OpenNIGrabber(RTC::Manager *manager)
Constructor.
static const char * spec[]
bool m_outputPointCloudRGBA
bool m_outputDepthImage
void grabberCallbackPointCloudRGBA(const pcl::PointCloud< pcl::PointXYZRGBA >::ConstPtr &cloud)
void outputDepthImage(const boost::shared_ptr< pcl::io::DepthImage > &image)
void outputColorImage(const boost::shared_ptr< pcl::io::Image > &image)
void grabberCallbackColorAndDepthImage(const boost::shared_ptr< pcl::io::Image > &image, const boost::shared_ptr< pcl::io::DepthImage > &depth, float reciprocalFocalLength)
png_uint_32 i
coil::Properties & getProperties()
void grabberCallbackColorImage(const boost::shared_ptr< pcl::io::Image > &image)
Img::TimedCameraImage m_depth
pcl::Grabber * m_interface
bool addOutPort(const char *name, OutPortBase &outport)
png_infop png_uint_32 * width
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
virtual RTC::ReturnCode_t onInitialize()
ExecutionContextHandle_t UniqueId
png_infop png_uint_32 png_uint_32 * height
bool m_requestToWritePointCloud
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
void grabberCallbackDepthImage(const boost::shared_ptr< pcl::io::DepthImage > &image)
virtual ~OpenNIGrabber()
Destructor.
prop
void setTimestamp(DataType &data)
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
OutPort< Img::TimedCameraImage > m_imageOut
void grabberCallbackPointCloud(const pcl::PointCloud< pcl::PointXYZ >::ConstPtr &cloud)
virtual bool write(DataType &value)
bool m_requestToWriteImage
bool m_outputPointCloud
Moving Least Squares Filter.
Img::TimedCameraImage m_image
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
void OpenNIGrabberInit(RTC::Manager *manager)
PointCloudTypes::PointCloud m_cloud


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:50