CapturingPointCloudProvider.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package VisionX::Core
19  * @author David Gonzalez Aguirre (david dot gonzalez at kit dot edu)
20  * @date 2014
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
26 
27 #include <string>
28 
30 
31 using namespace armarx;
32 
33 namespace visionx
34 {
35  // ================================================================== //
36  // == PointCloudProvider ice interface ============================== //
37  // ================================================================== //
38  void
39  CapturingPointCloudProvider::startCapture(const Ice::Current& ctx)
40  {
42  std::unique_lock lock(captureMutex);
43 
44  onStartCapture(frameRate);
45  fpsCounter.reset();
46  numFramesToCapture = -1;
47  captureEnabled = true;
48 
49  ARMARX_INFO << "Starting point cloud capture with framerate " << frameRate;
50  }
51 
52  void
53  CapturingPointCloudProvider::startCaptureForNumFrames(int numFrames, const Ice::Current& c)
54  {
56  std::unique_lock lock(captureMutex);
57 
58  onStartCapture(frameRate);
59  fpsCounter.reset();
60  numFramesToCapture = numFrames;
61  captureEnabled = true;
62 
63  ARMARX_INFO << "Starting point cloud capture with framerate " << frameRate << " (capture "
64  << numFramesToCapture << " frames)";
65  }
66 
67  void
68  CapturingPointCloudProvider::stopCapture(const Ice::Current& ctx)
69  {
71  std::unique_lock lock(captureMutex);
72 
73  captureEnabled = false;
74  ARMARX_INFO << "Stopping point cloud capture";
75  onStopCapture();
76  }
77 
78  void
79  CapturingPointCloudProvider::changeFrameRate(float framesPerSecond, const Ice::Current& ctx)
80  {
82  std::unique_lock lock(captureMutex);
83 
84  frameRate = framesPerSecond;
85  }
86 
87  bool
88  CapturingPointCloudProvider::isCaptureEnabled(const Ice::Current& c)
89  {
90  return captureEnabled;
91  }
92 
93  // ================================================================== //
94  // == Component implementation ====================================== //
95  // ====================================t============================== //
96  void
97  CapturingPointCloudProvider::onInitPointCloudProvider()
98  {
100  captureEnabled = false;
101  frameRate = getProperty<float>("framerate").getValue();
102 
103  if (frameRate > 0)
104  {
105  setPointCloudSyncMode(eFpsSynchronization);
106  }
107  else
108  {
109  ARMARX_INFO << "framerate is zero. ignoring";
110  setPointCloudSyncMode(eCaptureSynchronization);
111  }
112 
113 
114  // call setup of point clouds provider implementation to setup point clouds size
115  onInitCapturingPointCloudProvider();
116 
117  // capture task
119  this, &CapturingPointCloudProvider::capture);
120  }
121 
122  void
123  CapturingPointCloudProvider::onConnectPointCloudProvider()
124  {
125  ARMARX_TRACE;
126  onStartCapturingPointCloudProvider();
127 
128  captureTask->start();
129 
130  bool isEnabled = getProperty<bool>("isEnabled").getValue();
131 
132  if (isEnabled)
133  {
134  startCapture();
135  }
136  }
137 
138  void
139  CapturingPointCloudProvider::onExitPointCloudProvider()
140  {
141  ARMARX_TRACE;
142  // TODO: hack stop capturing
143  stopCapture();
144 
145  if (captureTask)
146  {
147  captureTask->stop();
148  }
149 
150  onExitCapturingPointCloudProvider();
151  }
152 
153  void
154  CapturingPointCloudProvider::capture()
155  {
156  ARMARX_TRACE;
157  ARMARX_INFO << "Starting point clouds Provider: " << getName() << flush;
158 
159  // main loop of component
160  std::chrono::milliseconds td(1);
161 
162  while (!captureTask->isStopped() && !isExiting())
163  {
164  if (captureEnabled)
165  {
166  std::unique_lock lock(captureMutex);
167 
168  doCapture();
169 
170  if (numFramesToCapture > 0)
171  {
172  numFramesToCapture--;
173  }
174  if (numFramesToCapture == 0)
175  {
176  captureEnabled = false;
177  continue;
178  }
179 
180  if (pointCloudSyncMode == eFpsSynchronization)
181  {
182  fpsCounter.assureFPS(frameRate);
183  }
184  }
185  else
186  {
187  std::this_thread::sleep_for(td);
188  }
189  }
190 
191  ARMARX_INFO << "Stopping point clouds Provider" << flush;
192  }
193 
194  // ================================================================== //
195  // == Utility methods for CapturingPointCloudProviders =================== //
196  // ================================================================== //
197  void
198  CapturingPointCloudProvider::setPointCloudSyncMode(ImageSyncMode pointCloudSyncMode)
199  {
200  std::unique_lock lock(captureMutex);
201 
202  this->pointCloudSyncMode = pointCloudSyncMode;
203  }
204 
205  ImageSyncMode
206  CapturingPointCloudProvider::getPointCloudSyncMode()
207  {
208  std::unique_lock lock(captureMutex);
209 
210  return pointCloudSyncMode;
211  }
212 } // namespace visionx
CapturingPointCloudProvider.h
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
trace.h
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::RunningTask
Definition: ArmarXMultipleObjectsScheduler.h:36
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27