CapturingImageProvider.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 Kai Welke (kai dot welke at kit dot edu)
20  * @date 2012
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "CapturingImageProvider.h"
26 
27 #include <string>
28 #include <thread>
29 
33 
34 using namespace armarx;
35 
36 namespace visionx
37 {
38  // ================================================================== //
39  // == ImageProvider ice interface =================================== //
40  // ================================================================== //
41  void
42  CapturingImageProvider::startCapture(float framesPerSecond, const Ice::Current& ctx)
43  {
44  std::unique_lock lock(captureMutex);
45  onStartCapture(framesPerSecond);
46 
47  captureEnabled = true;
48  frameRate = framesPerSecond;
49 
50  ARMARX_INFO << "Starting image capture with " << frameRate << "fps";
51  }
52 
53  void
54  CapturingImageProvider::stopCapture(const Ice::Current& ctx)
55  {
56  std::unique_lock lock(captureMutex);
57  captureEnabled = false;
58  onStopCapture();
59  }
60 
61  // ================================================================== //
62  // == Component implementation =============================== //
63  // ================================================================== //
64  void
65  CapturingImageProvider::onInitImageProvider()
66  {
67  // init members
68  frameRate = hasProperty("FPS") ? getProperty<float>("FPS").getValue() : 30;
69  captureEnabled = false;
70 
71  // default sync mode
72  setImageSyncMode(eFpsSynchronization);
73 
74  // call setup of image provider implementation to setup image size
75  onInitCapturingImageProvider();
76 
77  // capature task
78  captureTask =
79  new RunningTask<CapturingImageProvider>(this, &CapturingImageProvider::capture);
80  ARMARX_INFO << "started capturing thread";
81  }
82 
83  void
84  CapturingImageProvider::onConnectImageProvider()
85  {
86  onStartCapturingImageProvider();
87 
88  captureTask->start();
89 
90  // TODO: hack start capturing
91  startCapture(frameRate);
92  }
93 
94  void
95  CapturingImageProvider::onExitImageProvider()
96  {
97  // TODO: hack stop capturing
98  stopCapture();
99 
100  if (captureTask)
101  {
102  captureTask->stop();
103  }
104 
105  onExitCapturingImageProvider();
106  }
107 
108  void
109  CapturingImageProvider::capture()
110  {
111  ARMARX_INFO << "Starting Image Provider: " << getName();
112 
113  // main loop of component
114  std::chrono::milliseconds td(1);
115 
116  while (!captureTask->isStopped() && !isExiting() && sharedMemoryProvider)
117  {
118  auto tmpSharedMemoryProvider = sharedMemoryProvider;
119  if (captureEnabled && tmpSharedMemoryProvider)
120  {
121 
122  MetaInfoSizeBasePtr info = tmpSharedMemoryProvider->getMetaInfo();
124  long oldTimestamp = info ? info->timeProvided : 0;
125  bool succeeded = capture(imageBuffers);
126 
127  if (succeeded)
128  {
129  MetaInfoSizeBasePtr info = tmpSharedMemoryProvider->getMetaInfo();
131  IceUtil::Time image_timestamp = armarx::TimeUtil::GetTime();
132  if (!info || info->timeProvided == oldTimestamp)
133  {
134  ARMARX_WARNING << deactivateSpam(10000000)
135  << "The image provider did not set a timestamp - measuring "
136  "the timestamp now. The ImageProvider implementation "
137  "should call updateTimestamp()!";
138  updateTimestamp(image_timestamp.toMicroSeconds());
139  }
140  else if (info)
141  {
142  image_timestamp = IceUtil::Time::microSeconds(info->timeProvided);
143  }
144  auto imageProcessorProxy = this->imageProcessorProxy;
145  recordImages(image_timestamp);
146  ARMARX_CHECK_EXPRESSION(imageProcessorProxy);
147  imageProcessorProxy->reportImageAvailable(getName());
148  }
149 
150  if (imageSyncMode == eFpsSynchronization)
151  {
152  fpsCounter.assureFPS(frameRate);
153  setMetaInfo("fps", new Variant(fpsCounter.getFPS()));
154  setMetaInfo("minCycleTimeMs", new Variant(fpsCounter.getMinCycleTimeMS()));
155  setMetaInfo("maxCycleTimeMs", new Variant(fpsCounter.getMaxCycleTimeMS()));
156  auto dimension = getImageFormat().dimension;
157  setMetaInfo("resolution",
158  new Variant(std::to_string(dimension.width) + "x" +
159  std::to_string(dimension.height)));
160  }
161  }
162  else
163  {
164  std::this_thread::sleep_for(td);
165  }
166  }
167 
168  ARMARX_INFO << "Stopping ImageProvider";
169  }
170 
171  // ================================================================== //
172  // == Utility methods for CapturingImageProviders =================== //
173  // ================================================================== //
174  void
175  CapturingImageProvider::setImageSyncMode(ImageSyncMode imageSyncMode)
176  {
177  this->imageSyncMode = imageSyncMode;
178  }
179 
180  ImageSyncMode
181  CapturingImageProvider::getImageSyncMode()
182  {
183  return imageSyncMode;
184  }
185 } // namespace visionx
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:223
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
armarx::RunningTask
Definition: ArmarXMultipleObjectsScheduler.h:36
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:75
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
ExpressionException.h
CapturingImageProvider.h
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
Variant.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27