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 
28 
29 #include <string>
30 
31 using namespace armarx;
32 namespace visionx
33 {
34  // ================================================================== //
35  // == PointCloudProvider ice interface ============================== //
36  // ================================================================== //
37  void CapturingPointCloudProvider::startCapture(const Ice::Current& ctx)
38  {
40  std::unique_lock lock(captureMutex);
41 
42  onStartCapture(frameRate);
43  fpsCounter.reset();
44  numFramesToCapture = -1;
45  captureEnabled = true;
46 
47  ARMARX_INFO << "Starting point cloud capture with framerate " << frameRate;
48  }
49 
50  void CapturingPointCloudProvider::startCaptureForNumFrames(int numFrames, const Ice::Current& c)
51  {
53  std::unique_lock lock(captureMutex);
54 
55  onStartCapture(frameRate);
56  fpsCounter.reset();
57  numFramesToCapture = numFrames;
58  captureEnabled = true;
59 
60  ARMARX_INFO << "Starting point cloud capture with framerate " << frameRate << " (capture " << numFramesToCapture << " frames)";
61  }
62 
63  void CapturingPointCloudProvider::stopCapture(const Ice::Current& ctx)
64  {
66  std::unique_lock lock(captureMutex);
67 
68  captureEnabled = false;
69  ARMARX_INFO << "Stopping point cloud capture";
70  onStopCapture();
71  }
72 
73  void CapturingPointCloudProvider::changeFrameRate(float framesPerSecond, const Ice::Current& ctx)
74  {
76  std::unique_lock lock(captureMutex);
77 
78  frameRate = framesPerSecond;
79  }
80 
81  bool CapturingPointCloudProvider::isCaptureEnabled(const Ice::Current& c)
82  {
83  return captureEnabled;
84  }
85 
86 
87  // ================================================================== //
88  // == Component implementation ====================================== //
89  // ====================================t============================== //
90  void CapturingPointCloudProvider::onInitPointCloudProvider()
91  {
93  captureEnabled = false;
94  frameRate = getProperty<float>("framerate").getValue();
95 
96  if (frameRate > 0)
97  {
98  setPointCloudSyncMode(eFpsSynchronization);
99  }
100  else
101  {
102  ARMARX_INFO << "framerate is zero. ignoring";
103  setPointCloudSyncMode(eCaptureSynchronization);
104  }
105 
106 
107  // call setup of point clouds provider implementation to setup point clouds size
108  onInitCapturingPointCloudProvider();
109 
110  // capture task
111  captureTask = new RunningTask<CapturingPointCloudProvider>(this, &CapturingPointCloudProvider::capture);
112 
113  }
114 
115 
116  void CapturingPointCloudProvider::onConnectPointCloudProvider()
117  {
118  ARMARX_TRACE;
119  onStartCapturingPointCloudProvider();
120 
121  captureTask->start();
122 
123  bool isEnabled = getProperty<bool>("isEnabled").getValue();
124 
125  if (isEnabled)
126  {
127  startCapture();
128  }
129  }
130 
131 
132  void CapturingPointCloudProvider::onExitPointCloudProvider()
133  {
134  ARMARX_TRACE;
135  // TODO: hack stop capturing
136  stopCapture();
137 
138  if (captureTask)
139  {
140  captureTask->stop();
141  }
142 
143  onExitCapturingPointCloudProvider();
144  }
145 
146 
147  void CapturingPointCloudProvider::capture()
148  {
149  ARMARX_TRACE;
150  ARMARX_INFO << "Starting point clouds Provider: " << getName() << flush;
151 
152  // main loop of component
153  std::chrono::milliseconds td(1);
154 
155  while (!captureTask->isStopped() && !isExiting())
156  {
157  if (captureEnabled)
158  {
159  std::unique_lock lock(captureMutex);
160 
161  doCapture();
162 
163  if (numFramesToCapture > 0)
164  {
165  numFramesToCapture--;
166  }
167  if (numFramesToCapture == 0)
168  {
169  captureEnabled = false;
170  continue;
171  }
172 
173  if (pointCloudSyncMode == eFpsSynchronization)
174  {
175  fpsCounter.assureFPS(frameRate);
176  }
177  }
178  else
179  {
180  std::this_thread::sleep_for(td);
181  }
182  }
183 
184  ARMARX_INFO << "Stopping point clouds Provider" << flush;
185  }
186 
187  // ================================================================== //
188  // == Utility methods for CapturingPointCloudProviders =================== //
189  // ================================================================== //
190  void CapturingPointCloudProvider::setPointCloudSyncMode(ImageSyncMode pointCloudSyncMode)
191  {
192  std::unique_lock lock(captureMutex);
193 
194  this->pointCloudSyncMode = pointCloudSyncMode;
195  }
196 
197  ImageSyncMode CapturingPointCloudProvider::getPointCloudSyncMode()
198  {
199  std::unique_lock lock(captureMutex);
200 
201  return pointCloudSyncMode;
202  }
203 }
CapturingPointCloudProvider.h
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
trace.h
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::RunningTask
Definition: ArmarXMultipleObjectsScheduler.h:35
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28