YARP
Yet Another Robot Platform
USBcamera.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6
7#include "USBcamera.h"
9
10#if defined(_MSC_VER)
11# include <WIN_camera.h>
12#elif defined __unix
13# include <V4L_camera.h>
14#endif
15
16#include <yarp/os/LogStream.h>
17#include <yarp/os/Semaphore.h>
18#include <yarp/os/Stamp.h>
19
21
22using namespace yarp::os;
23using namespace yarp::dev;
24
25
27
29{
30 // initialize stuff
32}
33
35{
36 // delete subdevice, of any
38}
39
41{
42 // open OS dependant device
43 yCTrace(USBCAMERA) << "input params are " << config.toString();
44
45#if defined(_MSC_VER)
46 os_device = (DeviceDriver*)new WIN_camera;
47#elif defined __unix
49#endif
50
52 prop.fromString(config.toString());
53 if (!prop.check("pixelType")) {
54 switch (pixelType) {
56 prop.put("pixelType", VOCAB_PIXEL_MONO);
57 break;
58
59 case VOCAB_PIXEL_RGB:
60 default:
61 prop.put("pixelType", VOCAB_PIXEL_RGB);
62 break;
63 }
64 }
65 if (!os_device->open(prop)) {
66 delete os_device;
67 return false;
68 }
69
75
76 if (frameGrabberImage != nullptr) {
79 }
80
81 if (frameGrabberImageRaw != nullptr) {
84 }
85 return true;
86}
87
89{
90 // close OS dependant device
92 delete os_device;
93 return true;
94}
95
97{
98 if (frameGrabberImage != nullptr) {
99 return frameGrabberImage->width();
100 }
101 if (frameGrabberImageRaw != nullptr) {
102 return frameGrabberImageRaw->width();
103 }
104
105 return 0;
106}
107
109{
110 if (frameGrabberImage != nullptr) {
111 return frameGrabberImage->height();
112 }
113 if (frameGrabberImageRaw != nullptr) {
115 }
116
117 return 0;
118}
119
121{
122 if (deviceTimed != nullptr) {
124 }
125
126 return yarp::os::Stamp();
127}
128
130{
131 if (deviceRgbVisualParam != nullptr) {
133 }
134 return 0;
135}
136
138{
139 if (deviceRgbVisualParam != nullptr) {
141 }
142 return 0;
143}
144
145
147{
148 if (deviceRgbVisualParam != nullptr) {
150 }
151 return false;
152}
153
154bool USBCameraDriver::getRgbResolution(int& width, int& height)
155{
156 if (deviceRgbVisualParam != nullptr) {
158 }
159 return false;
160}
161
162bool USBCameraDriver::setRgbResolution(int width, int height)
163{
164 if (width <= 0 || height <= 0) {
165 yCError(USBCAMERA) << "usbCamera: invalid width or height";
166 return false;
167 }
168 if (deviceRgbVisualParam != nullptr) {
169 _width = width;
170 _height = height;
172 }
173 return false;
174}
175
176bool USBCameraDriver::getRgbFOV(double& horizontalFov, double& verticalFov)
177{
178 if (deviceRgbVisualParam != nullptr) {
179 return deviceRgbVisualParam->getRgbFOV(horizontalFov, verticalFov);
180 }
181 return false;
182}
183
184bool USBCameraDriver::setRgbFOV(double horizontalFov, double verticalFov)
185{
186 if (deviceRgbVisualParam != nullptr) {
187 return deviceRgbVisualParam->setRgbFOV(horizontalFov, verticalFov);
188 }
189 return false;
190}
191
193{
194 if (deviceRgbVisualParam != nullptr) {
196 }
197 return false;
198}
199
201{
202 if (deviceRgbVisualParam != nullptr) {
204 }
205 return false;
206}
207
209{
210 if (deviceRgbVisualParam != nullptr) {
212 }
213 return false;
214}
215
216
220{
223}
224
226{
228}
229
231{
232 return frameGrabberImage->getImage(image);
233}
234
236{
237 return frameGrabberImageRaw->getImage(image);
238}
239
241{
242 return USBCameraDriver::width();
243}
244
246{
248}
249
253{
256}
257
259{
261}
262
264{
265 return frameGrabberImageRaw->getImage(image);
266}
267
269{
270 return USBCameraDriver::width();
271}
272
274{
276}
277
278
279/* Implementation of IFrameGrabberControls2 interface
280 *
281 * Actual function will be implemented by OS specific devices
282 */
283
285{
286 if (deviceControls != nullptr) {
287 return deviceControls->getCameraDescription(camera);
288 }
289 return false;
290}
291
292bool USBCameraDriver::hasFeature(int feature, bool* _hasFeature)
293{
294 if (deviceControls != nullptr) {
295 return deviceControls->hasFeature(feature, _hasFeature);
296 }
297 return false;
298}
299
300bool USBCameraDriver::setFeature(int feature, double value)
301{
302 if (deviceControls != nullptr) {
303 return deviceControls->setFeature(feature, value);
304 }
305 return false;
306}
307
308bool USBCameraDriver::getFeature(int feature, double* value)
309{
310 if (deviceControls != nullptr) {
311 return deviceControls->getFeature(feature, value);
312 }
313 return false;
314}
315
316bool USBCameraDriver::getFeature(int feature, double* value1, double* value2)
317{
318 if (deviceControls != nullptr) {
319 return deviceControls->getFeature(feature, value1, value2);
320 }
321 return false;
322}
323
324bool USBCameraDriver::setFeature(int feature, double value1, double value2)
325{
326 if (deviceControls != nullptr) {
327 return deviceControls->setFeature(feature, value1, value2);
328 }
329 return false;
330}
331
332bool USBCameraDriver::hasOnOff(int feature, bool* _hasOnOff)
333{
334 if (deviceControls != nullptr) {
335 return deviceControls->hasOnOff(feature, _hasOnOff);
336 }
337 return false;
338}
339
340bool USBCameraDriver::setActive(int feature, bool onoff)
341{
342 if (deviceControls != nullptr) {
343 return deviceControls->setActive(feature, onoff);
344 }
345 return false;
346}
347
348bool USBCameraDriver::getActive(int feature, bool* isActive)
349{
350 if (deviceControls != nullptr) {
351 return deviceControls->getActive(feature, isActive);
352 }
353 return false;
354}
355
356bool USBCameraDriver::hasAuto(int feature, bool* _hasAuto)
357{
358 if (deviceControls != nullptr) {
359 return deviceControls->hasAuto(feature, _hasAuto);
360 }
361 return false;
362}
363
364bool USBCameraDriver::hasManual(int feature, bool* _hasManual)
365{
366 if (deviceControls != nullptr) {
367 return deviceControls->hasManual(feature, _hasManual);
368 }
369 return false;
370}
371
372bool USBCameraDriver::hasOnePush(int feature, bool* _hasOnePush)
373{
374 if (deviceControls != nullptr) {
375 return deviceControls->hasOnePush(feature, _hasOnePush);
376 }
377 return false;
378}
379
381{
382 if (deviceControls != nullptr) {
383 return deviceControls->setMode(feature, mode);
384 }
385 return false;
386}
387
388bool USBCameraDriver::getMode(int feature, FeatureMode* mode)
389{
390 if (deviceControls != nullptr) {
391 return deviceControls->getMode(feature, mode);
392 }
393 return false;
394}
395
397{
398 if (deviceControls != nullptr) {
399 return deviceControls->setOnePush(feature);
400 }
401 return false;
402}
const yarp::os::LogComponent & USBCAMERA()
int height() const override
Return the height of each frame.
Definition: USBcamera.cpp:273
~USBCameraDriverRaw() override
Definition: USBcamera.cpp:258
int width() const override
Return the width of each frame.
Definition: USBcamera.cpp:268
bool getImage(yarp::sig::ImageOf< yarp::sig::PixelMono > &image) override
FrameGrabber image interface, returns the last acquired frame as an rgb image.
Definition: USBcamera.cpp:263
int height() const override
Return the height of each frame.
Definition: USBcamera.cpp:245
bool getImage(yarp::sig::ImageOf< yarp::sig::PixelRgb > &image) override
FrameGrabber image interface, returns the last acquired frame as an rgb image.
Definition: USBcamera.cpp:230
~USBCameraDriverRgb() override
Definition: USBcamera.cpp:225
int width() const override
Return the width of each frame.
Definition: USBcamera.cpp:240
usbCamera: YARP device driver implementation for acquiring images from USB cameras.
Definition: USBcamera.h:34
bool setFeature(int feature, double value) override
Set the requested feature to a value (saturation, brightness ... )
Definition: USBcamera.cpp:300
size_t _width
Definition: USBcamera.h:46
bool getRgbResolution(int &width, int &height) override
Get the resolution of the rgb image from the camera.
Definition: USBcamera.cpp:154
yarp::dev::IFrameGrabberImage * frameGrabberImage
Definition: USBcamera.h:40
int getRgbWidth() override
Return the width of each frame.
Definition: USBcamera.cpp:137
bool hasOnePush(int feature, bool *hasOnePush) override
Check if the requested feature has the 'onePush' mode.
Definition: USBcamera.cpp:372
USBCameraDriver()
Constructor.
Definition: USBcamera.cpp:28
bool getFeature(int feature, double *value) override
Get the current value for the requested feature.
Definition: USBcamera.cpp:308
int getRgbHeight() override
Return the height of each frame.
Definition: USBcamera.cpp:129
bool hasOnOff(int feature, bool *HasOnOff) override
Check if the camera has the ability to turn on/off the requested feature.
Definition: USBcamera.cpp:332
bool setRgbMirroring(bool mirror) override
Set the mirroring setting of the sensor.
Definition: USBcamera.cpp:208
~USBCameraDriver() override
Destructor.
Definition: USBcamera.cpp:34
bool close() override
Closes the device driver.
Definition: USBcamera.cpp:88
bool hasAuto(int feature, bool *hasAuto) override
Check if the requested feature has the 'auto' mode.
Definition: USBcamera.cpp:356
bool getActive(int feature, bool *isActive) override
Get the current status of the feature, on or off.
Definition: USBcamera.cpp:348
bool open(yarp::os::Searchable &config) override
Open the device driver.
Definition: USBcamera.cpp:40
bool getRgbMirroring(bool &mirror) override
Get the mirroring setting of the sensor.
Definition: USBcamera.cpp:200
bool setRgbResolution(int width, int height) override
Set the resolution of the rgb image from the camera.
Definition: USBcamera.cpp:162
bool getMode(int feature, FeatureMode *mode) override
Get the current mode for the feature.
Definition: USBcamera.cpp:388
bool getRgbSupportedConfigurations(yarp::sig::VectorOf< yarp::dev::CameraConfig > &configurations) override
Get the possible configurations of the camera.
Definition: USBcamera.cpp:146
bool hasFeature(int feature, bool *hasFeature) override
Check if camera has the requested feature (saturation, brightness ... )
Definition: USBcamera.cpp:292
yarp::dev::IFrameGrabberImageRaw * frameGrabberImageRaw
Definition: USBcamera.h:41
bool setMode(int feature, FeatureMode mode) override
Set the requested mode for the feature.
Definition: USBcamera.cpp:380
yarp::dev::IPreciselyTimed * deviceTimed
Definition: USBcamera.h:39
bool getRgbIntrinsicParam(yarp::os::Property &intrinsic) override
Get the intrinsic parameters of the rgb camera.
Definition: USBcamera.cpp:192
bool setOnePush(int feature) override
Set the requested feature to a value (saturation, brightness ... )
Definition: USBcamera.cpp:396
size_t _height
Definition: USBcamera.h:47
bool getRgbFOV(double &horizontalFov, double &verticalFov) override
Get the field of view (FOV) of the rgb camera.
Definition: USBcamera.cpp:176
bool hasManual(int feature, bool *hasManual) override
Check if the requested feature has the 'manual' mode.
Definition: USBcamera.cpp:364
yarp::dev::DeviceDriver * os_device
Definition: USBcamera.h:42
yarp::os::Stamp getLastInputStamp() override
Implements the IPreciselyTimed interface.
Definition: USBcamera.cpp:120
int width() const
Definition: USBcamera.cpp:96
yarp::dev::IRgbVisualParams * deviceRgbVisualParam
Definition: USBcamera.h:44
bool setActive(int feature, bool onoff) override
Set the requested feature on or off.
Definition: USBcamera.cpp:340
int height() const
Definition: USBcamera.cpp:108
bool getCameraDescription(CameraDescriptor *camera) override
Implementation of IFrameGrabberControls2 interface.
Definition: USBcamera.cpp:284
yarp::dev::IFrameGrabberControls * deviceControls
Definition: USBcamera.h:43
bool setRgbFOV(double horizontalFov, double verticalFov) override
Set the field of view (FOV) of the rgb camera.
Definition: USBcamera.cpp:184
Interface implemented by all device drivers.
Definition: DeviceDriver.h:30
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: DeviceDriver.h:53
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:88
bool close() override
Close the DeviceDriver.
Definition: DeviceDriver.h:59
virtual bool setFeature(int feature, double value)=0
Set the requested feature to a value (saturation, brightness ... )
virtual bool setOnePush(int feature)=0
Set the requested feature to a value (saturation, brightness ... )
virtual bool getFeature(int feature, double *value)=0
Get the current value for the requested feature.
virtual bool hasManual(int feature, bool *hasManual)=0
Check if the requested feature has the 'manual' mode.
virtual bool setMode(int feature, FeatureMode mode)=0
Set the requested mode for the feature.
virtual bool hasOnOff(int feature, bool *HasOnOff)=0
Check if the camera has the ability to turn on/off the requested feature.
virtual bool hasAuto(int feature, bool *hasAuto)=0
Check if the requested feature has the 'auto' mode.
virtual bool getMode(int feature, FeatureMode *mode)=0
Get the current mode for the feature.
virtual bool getCameraDescription(CameraDescriptor *camera)=0
Get a basic description of the camera hw.
virtual bool getActive(int feature, bool *isActive)=0
Get the current status of the feature, on or off.
virtual bool setActive(int feature, bool onoff)=0
Set the requested feature on or off.
virtual bool hasFeature(int feature, bool *hasFeature)=0
Check if camera has the requested feature (saturation, brightness ... )
virtual bool hasOnePush(int feature, bool *hasOnePush)=0
Check if the requested feature has the 'onePush' mode.
virtual int width() const =0
Return the width of each frame.
virtual int height() const =0
Return the height of each frame.
virtual bool getImage(ImageType &image)=0
Get an image from the frame grabber.
virtual yarp::os::Stamp getLastInputStamp()=0
Return the time stamp relative to the last acquisition.
virtual bool getRgbSupportedConfigurations(yarp::sig::VectorOf< yarp::dev::CameraConfig > &configurations)
Get the possible configurations of the camera.
virtual bool setRgbMirroring(bool mirror)=0
Set the mirroring setting of the sensor.
virtual bool getRgbIntrinsicParam(yarp::os::Property &intrinsic)=0
Get the intrinsic parameters of the rgb camera.
virtual bool getRgbResolution(int &width, int &height)
Get the resolution of the rgb image from the camera.
virtual bool setRgbResolution(int width, int height)=0
Set the resolution of the rgb image from the camera.
virtual bool getRgbMirroring(bool &mirror)=0
Get the mirroring setting of the sensor.
virtual int getRgbHeight()=0
Return the height of each frame.
virtual bool getRgbFOV(double &horizontalFov, double &verticalFov)=0
Get the field of view (FOV) of the rgb camera.
virtual bool setRgbFOV(double horizontalFov, double verticalFov)=0
Set the field of view (FOV) of the rgb camera.
virtual int getRgbWidth()=0
Return the width of each frame.
A class for storing options and configuration information.
Definition: Property.h:33
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
Definition: Property.cpp:1063
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:1015
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition: Property.cpp:1041
A base class for nested structures that can be searched.
Definition: Searchable.h:63
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:21
Provides:
Definition: Vector.h:117
#define yCError(component,...)
Definition: LogComponent.h:213
#define yCTrace(component,...)
Definition: LogComponent.h:84
@ VOCAB_PIXEL_MONO
Definition: Image.h:42
@ VOCAB_PIXEL_RGB
Definition: Image.h:44
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.