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
26
27#include <string>
28#include <thread>
29
33
34using namespace armarx;
35
36namespace 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;
59 }
60
61 // ================================================================== //
62 // == Component implementation =============================== //
63 // ================================================================== //
64 void
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
76
77 // capature task
80 ARMARX_INFO << "started capturing thread";
81 }
82
83 void
85 {
87
88 captureTask->start();
89
90 // TODO: hack start capturing
92 }
93
94 void
96 {
97 // TODO: hack stop capturing
99
100 if (captureTask)
101 {
102 captureTask->stop();
103 }
104
106 }
107
108 void
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 }
145 recordImages(image_timestamp);
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
176 {
177 this->imageSyncMode = imageSyncMode;
178 }
179
180 ImageSyncMode
185} // namespace visionx
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition Logging.cpp:75
Property< PropertyType > getProperty(const std::string &name)
std::string getName() const
Retrieve name of object.
void setMetaInfo(const std::string &id, const VariantBasePtr &value)
Allows to set meta information that can be queried live via Ice interface on the ArmarXManager.
bool hasProperty(const std::string &name)
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition TimeUtil.cpp:42
The Variant class is described here: Variants.
Definition Variant.h:224
virtual void onStopCapture()=0
This is called when the image provider capturing has been stopped.
virtual void onStartCapturingImageProvider()
This is called when the Component::onConnectComponent() setup is called.
ImageSyncMode getImageSyncMode()
Returns the image sync mode.
virtual void onInitCapturingImageProvider()=0
This is called when the Component::onInitComponent() is called.
virtual bool capture(void **ppImageBuffers)=0
Main capturing function.
void stopCapture(const Ice::Current &c=Ice::emptyCurrent) override
Stops image capturing.
void startCapture(float framesPerSecond, const Ice::Current &c=Ice::emptyCurrent) override
Starts image capturing.
void setImageSyncMode(ImageSyncMode imageSyncMode)
Sets the image synchronization mode.
virtual void onExitCapturingImageProvider()=0
This is called when the Component::onExitComponent() setup is called.
virtual void onStartCapture(float framesPerSecond)=0
This is called when the image provider capturing has been started.
armarx::RunningTask< CapturingImageProvider >::pointer_type captureTask
Capture thread.
std::mutex captureMutex
mutex for capturing for proper exit
ImageSyncMode imageSyncMode
Image synchronization information.
bool captureEnabled
Indicates that capturing is enabled and running.
void recordImages(const IceUtil::Time &image_timestamp)
bool isExiting()
Retrieve whether provider is exiting.
armarx::IceSharedMemoryProvider< unsignedchar >::pointer_type sharedMemoryProvider
shared memory provider
void updateTimestamp(Ice::Long timestamp, bool threadSafe=true)
Updates the timestamp of the currently captured image.
ImageFormatInfo getImageFormat(const Ice::Current &c=Ice::emptyCurrent) override
Returns the entire image format info struct via Ice.
void ** imageBuffers
Image buffer memory.
ImageProcessorInterfacePrx imageProcessorProxy
Ice proxy of the image processor interface.
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
This file offers overloads of toIce() and fromIce() functions for STL container types.
ArmarX headers.