Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
depth_thread.cpp
1 
2 /***************************************************************************
3  * depth_thread.cpp - OpenNI depth provider thread
4  *
5  * Created: Thu Dec 22 11:36:31 2011
6  * Copyright 2006-2011 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "depth_thread.h"
24 #include "utils/setup.h"
25 
26 #include <core/threading/mutex_locker.h>
27 #include <fvutils/ipc/shm_image.h>
28 #include <fvutils/color/colorspaces.h>
29 
30 #include <memory>
31 
32 using namespace fawkes;
33 using namespace firevision;
34 
35 /** @class OpenNiDepthThread "image_thread.h"
36  * OpenNI Depth Provider Thread.
37  * This thread provides RGB and depth images from the camera via a
38  * SharedMemoryImageBuffer to other FireVision plugins.
39  *
40  * @author Tim Niemueller
41  */
42 
43 /** Constructor. */
45  : Thread("OpenNiDepthThread", Thread::OPMODE_WAITFORWAKEUP),
46  BlockedTimingAspect(BlockedTimingAspect::WAKEUP_HOOK_SENSOR_PREPARE)
47 {
48 }
49 
50 
51 /** Destructor. */
53 {
54 }
55 
56 
57 void
59 {
61 
62  __depth_gen = new xn::DepthGenerator();
63  std::auto_ptr<xn::DepthGenerator> depthgen_autoptr(__depth_gen);
64 
65  XnStatus st;
66 
67  fawkes::openni::find_or_create_node(openni, XN_NODE_TYPE_DEPTH, __depth_gen);
68  fawkes::openni::setup_map_generator(*__depth_gen, config);
69 
70  __depth_md = new xn::DepthMetaData();
71 
72  __depth_gen->GetMetaData(*__depth_md);
73 
74  __depth_width = __depth_md->XRes();
75  __depth_height = __depth_md->YRes();
76 
77  __depth_buf = new SharedMemoryImageBuffer("openni-depth", RAW16,
78  __depth_md->XRes(), __depth_md->YRes());
79  __depth_bufsize = colorspace_buffer_size(RAW16,
80  __depth_md->XRes(), __depth_md->YRes());
81 
82  __depth_gen->StartGenerating();
83 
84  __capture_start = new Time(clock);
85  __capture_start->stamp_systime();
86  // Update once to get timestamp
87  __depth_gen->WaitAndUpdateData();
88  // arbitrarily define the zero reference point,
89  // we can't get any closer than this
90  *__capture_start -= (long int)__depth_gen->GetTimestamp();
91 
92 
93  depthgen_autoptr.release();
94 }
95 
96 
97 void
99 {
100  // we do not stop generating, we don't know if there is no other plugin
101  // using the node.
102  delete __depth_gen;
103  delete __depth_md;
104  delete __depth_buf;
105 }
106 
107 
108 void
110 {
112  bool is_depth_new = __depth_gen->IsDataNew();
113  __depth_gen->GetMetaData(*__depth_md);
114  const XnDepthPixel * const depth_data = __depth_md->Data();
115  fawkes::Time ts = *__capture_start + (long int)__depth_gen->GetTimestamp();
116  lock.unlock();
117 
118  if (is_depth_new && (__depth_buf->num_attached() > 1)) {
119  memcpy(__depth_buf->buffer(), depth_data, __depth_bufsize);
120  }
121 
122  __depth_buf->set_capture_time(&ts);
123 }
LockPtr< xn::Context > openni
Central OpenNI context.
Definition: openni.h:48
Time & stamp_systime()
Set this time to the current system time.
Definition: time.cpp:800
Mutex * objmutex_ptr() const
Get object mutex.
Definition: lockptr.h:240
Mutex locking helper.
Definition: mutex_locker.h:33
A class for handling time.
Definition: time.h:91
virtual void loop()
Code to execute in the thread.
Thread class encapsulation of pthreads.
Definition: thread.h:42
virtual void finalize()
Finalize the thread.
Clock * clock
By means of this member access to the clock is given.
Definition: clock.h:45
Thread aspect to use blocked timing.
Shared memory image buffer.
Definition: shm_image.h:135
OpenNiDepthThread()
Constructor.
virtual ~OpenNiDepthThread()
Destructor.
unsigned char * buffer() const
Get image buffer.
Definition: shm_image.cpp:234
unsigned int num_attached() const
Get number of attached processes.
Definition: shm.cpp:714
Configuration * config
This is the Configuration member used to access the configuration.
Definition: configurable.h:44
void set_capture_time(fawkes::Time *time)
Set the capture time.
Definition: shm_image.cpp:204
virtual void init()
Initialize the thread.