YARP
Yet Another Robot Platform
USBcamera.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 
20 #include "USBcamera.h"
21 #include "USBcameraLogComponent.h"
22 
23 #if defined(_MSC_VER)
24 # include <WIN_camera.h>
25 #elif defined __unix
26 # include <V4L_camera.h>
27 #endif
28 
29 #include <yarp/os/LogStream.h>
30 #include <yarp/os/Semaphore.h>
31 #include <yarp/os/Stamp.h>
32 
33 #include <yarp/dev/DeviceDriver.h>
35 
36 using namespace yarp::os;
37 using namespace yarp::dev;
38 
39 
41 
43 {
44  // initialize stuff
46 }
47 
49 {
50  // delete subdevice, of any
52 }
53 
55 {
56  // open OS dependant device
57  yCTrace(USBCAMERA) << "input params are " << config.toString();
58 
59 #if defined(_MSC_VER)
60  os_device = (DeviceDriver*)new WIN_camera;
61 #elif defined __unix
62  os_device = (DeviceDriver*)new V4L_camera;
63 #endif
64 
65  yarp::os::Property prop;
66  prop.fromString(config.toString());
67  if (!prop.check("pixelType")) {
68  switch (pixelType) {
69  case VOCAB_PIXEL_MONO:
70  prop.put("pixelType", VOCAB_PIXEL_MONO);
71  break;
72 
73  case VOCAB_PIXEL_RGB:
74  default:
75  prop.put("pixelType", VOCAB_PIXEL_RGB);
76  break;
77  }
78  }
79  if (!os_device->open(prop)) {
80  delete os_device;
81  return false;
82  }
83 
84  os_device->view(deviceRgb);
85  os_device->view(deviceRaw);
86  os_device->view(deviceControls);
87  os_device->view(deviceTimed);
88  os_device->view(deviceRgbVisualParam);
89 
90  if (deviceRaw != nullptr) {
91  _width = deviceRaw->width();
92  _height = deviceRaw->height();
93  }
94 
95  if (deviceRgb != nullptr) {
96  _width = deviceRgb->width();
97  _height = deviceRgb->height();
98  }
99  return true;
100 }
101 
103 {
104  // close OS dependant device
105  os_device->close();
106  delete os_device;
107  return true;
108 }
109 
111 {
112  if (deviceRaw != nullptr) {
113  return deviceRaw->width();
114  }
115  if (deviceRgb != nullptr) {
116  return deviceRgb->width();
117  } else {
118  return 0;
119  }
120 }
121 
123 {
124  if (deviceRaw != nullptr) {
125  return deviceRaw->height();
126  }
127  if (deviceRgb != nullptr) {
128  return deviceRgb->height();
129  } else {
130  return 0;
131  }
132 }
133 
134 
135 bool USBCameraDriver::getRawBuffer(unsigned char* buff)
136 {
137  return false;
138 }
139 
141 {
142  return 0;
143 }
144 
145 bool USBCameraDriver::getRgbBuffer(unsigned char* buff)
146 {
147  return false;
148 }
149 
151 {
152  if (deviceTimed != nullptr) {
153  return deviceTimed->getLastInputStamp();
154  }
155 
156  return yarp::os::Stamp();
157 }
158 
160 {
161  if (deviceRgbVisualParam != nullptr) {
162  return deviceRgbVisualParam->getRgbHeight();
163  }
164  return 0;
165 }
166 
168 {
169  if (deviceRgbVisualParam != nullptr) {
170  return deviceRgbVisualParam->getRgbWidth();
171  }
172  return 0;
173 }
174 
175 
177 {
178  if (deviceRgbVisualParam != nullptr) {
179  return deviceRgbVisualParam->getRgbSupportedConfigurations(configurations);
180  }
181  return false;
182 }
183 
184 bool USBCameraDriver::getRgbResolution(int& width, int& height)
185 {
186  if (deviceRgbVisualParam != nullptr) {
187  return deviceRgbVisualParam->getRgbResolution(width, height);
188  }
189  return false;
190 }
191 
192 bool USBCameraDriver::setRgbResolution(int width, int height)
193 {
194  if (width <= 0 || height <= 0) {
195  yCError(USBCAMERA) << "usbCamera: invalid width or height";
196  return false;
197  }
198  if (deviceRgbVisualParam != nullptr) {
199  _width = width;
200  _height = height;
201  return deviceRgbVisualParam->setRgbResolution(width, height);
202  }
203  return false;
204 }
205 
206 bool USBCameraDriver::getRgbFOV(double& horizontalFov, double& verticalFov)
207 {
208  if (deviceRgbVisualParam != nullptr) {
209  return deviceRgbVisualParam->getRgbFOV(horizontalFov, verticalFov);
210  }
211  return false;
212 }
213 
214 bool USBCameraDriver::setRgbFOV(double horizontalFov, double verticalFov)
215 {
216  if (deviceRgbVisualParam != nullptr) {
217  return deviceRgbVisualParam->setRgbFOV(horizontalFov, verticalFov);
218  }
219  return false;
220 }
221 
223 {
224  if (deviceRgbVisualParam != nullptr) {
225  return deviceRgbVisualParam->getRgbIntrinsicParam(intrinsic);
226  }
227  return false;
228 }
229 
231 {
232  if (deviceRgbVisualParam != nullptr) {
233  return deviceRgbVisualParam->getRgbMirroring(mirror);
234  }
235  return false;
236 }
237 
239 {
240  if (deviceRgbVisualParam != nullptr) {
241  return deviceRgbVisualParam->setRgbMirroring(mirror);
242  }
243  return false;
244 }
245 
246 
250 {
253 }
254 
256 {
258 }
259 
261 {
262  if ((image.width() != _width) || (image.height() != _height)) {
263  image.resize(_width, _height);
264  }
265  deviceRgb->getRgbBuffer(image.getRawImage());
266  return true;
267 }
268 
270 {
271  if ((image.width() != _width) || (image.height() != _height)) {
272  image.resize(_width, _height);
273  }
274 
275  deviceRaw->getRawBuffer(image.getRawImage());
276  return true;
277 }
278 
280 {
281  return USBCameraDriver::width();
282 }
283 
285 {
286  return USBCameraDriver::height();
287 }
288 
292 {
295 }
296 
298 {
300 }
301 
303 {
304  if ((image.width() != _width) || (image.height() != _height)) {
305  image.resize(_width, _height);
306  }
307 
308  deviceRaw->getRawBuffer(image.getRawImage());
309  return true;
310 }
311 
313 {
314  return USBCameraDriver::width();
315 }
316 
318 {
319  return USBCameraDriver::height();
320 }
321 
322 
323 /* Implementation of IFrameGrabberControls2 interface
324  *
325  * Actual function will be implemented by OS specific devices
326  */
327 
329 {
330  if (deviceControls != nullptr) {
331  return deviceControls->getCameraDescription(camera);
332  }
333  return false;
334 }
335 
336 bool USBCameraDriver::hasFeature(int feature, bool* _hasFeature)
337 {
338  if (deviceControls != nullptr) {
339  return deviceControls->hasFeature(feature, _hasFeature);
340  }
341  return false;
342 }
343 
344 bool USBCameraDriver::setFeature(int feature, double value)
345 {
346  if (deviceControls != nullptr) {
347  return deviceControls->setFeature(feature, value);
348  }
349  return false;
350 }
351 
352 bool USBCameraDriver::getFeature(int feature, double* value)
353 {
354  if (deviceControls != nullptr) {
355  return deviceControls->getFeature(feature, value);
356  }
357  return false;
358 }
359 
360 bool USBCameraDriver::getFeature(int feature, double* value1, double* value2)
361 {
362  if (deviceControls != nullptr) {
363  return deviceControls->getFeature(feature, value1, value2);
364  }
365  return false;
366 }
367 
368 bool USBCameraDriver::setFeature(int feature, double value1, double value2)
369 {
370  if (deviceControls != nullptr) {
371  return deviceControls->setFeature(feature, value1, value2);
372  }
373  return false;
374 }
375 
376 bool USBCameraDriver::hasOnOff(int feature, bool* _hasOnOff)
377 {
378  if (deviceControls != nullptr) {
379  return deviceControls->hasOnOff(feature, _hasOnOff);
380  }
381  return false;
382 }
383 
384 bool USBCameraDriver::setActive(int feature, bool onoff)
385 {
386  if (deviceControls != nullptr) {
387  return deviceControls->setActive(feature, onoff);
388  }
389  return false;
390 }
391 
392 bool USBCameraDriver::getActive(int feature, bool* isActive)
393 {
394  if (deviceControls != nullptr) {
395  return deviceControls->getActive(feature, isActive);
396  }
397  return false;
398 }
399 
400 bool USBCameraDriver::hasAuto(int feature, bool* _hasAuto)
401 {
402  if (deviceControls != nullptr) {
403  return deviceControls->hasAuto(feature, _hasAuto);
404  }
405  return false;
406 }
407 
408 bool USBCameraDriver::hasManual(int feature, bool* _hasManual)
409 {
410  if (deviceControls != nullptr) {
411  return deviceControls->hasManual(feature, _hasManual);
412  }
413  return false;
414 }
415 
416 bool USBCameraDriver::hasOnePush(int feature, bool* _hasOnePush)
417 {
418  if (deviceControls != nullptr) {
419  return deviceControls->hasOnePush(feature, _hasOnePush);
420  }
421  return false;
422 }
423 
424 bool USBCameraDriver::setMode(int feature, FeatureMode mode)
425 {
426  if (deviceControls != nullptr) {
427  return deviceControls->setMode(feature, mode);
428  }
429  return false;
430 }
431 
432 bool USBCameraDriver::getMode(int feature, FeatureMode* mode)
433 {
434  if (deviceControls != nullptr) {
435  return deviceControls->getMode(feature, mode);
436  }
437  return false;
438 }
439 
441 {
442  if (deviceControls != nullptr) {
443  return deviceControls->setOnePush(feature);
444  }
445  return false;
446 }
LogStream.h
CameraDescriptor
Definition: FrameGrabberInterfaces.h:35
USBCameraDriver::setRgbResolution
bool setRgbResolution(int width, int height) override
Set the resolution of the rgb image from the camera.
Definition: USBcamera.cpp:192
V4L_camera
Definition: V4L_camera.h:149
USBCameraDriverRgb::USBCameraDriverRgb
USBCameraDriverRgb()
Definition: USBcamera.cpp:248
V4L_camera.h
yarp::os::Property::put
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:998
USBCameraDriverRaw::~USBCameraDriverRaw
~USBCameraDriverRaw() override
Definition: USBcamera.cpp:297
yarp::dev::IFrameGrabberControls::getActive
virtual bool getActive(int feature, bool *isActive)=0
Get the current status of the feature, on or off.
USBCameraDriverRgb::getImage
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:260
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
USBCameraDriver::deviceRaw
yarp::dev::IFrameGrabber * deviceRaw
Definition: USBcamera.h:55
USBCameraDriver::getRawBuffer
bool getRawBuffer(unsigned char *buffer) override
Implements FrameGrabber basic interface.
Definition: USBcamera.cpp:135
USBCameraDriverRgb::~USBCameraDriverRgb
~USBCameraDriverRgb() override
Definition: USBcamera.cpp:255
yarp::os::Searchable::toString
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
USBCameraDriver::setRgbFOV
bool setRgbFOV(double horizontalFov, double verticalFov) override
Set the field of view (FOV) of the rgb camera.
Definition: USBcamera.cpp:214
USBCameraDriver
usbCamera: YARP device driver implementation for acquiring images from USB cameras.
Definition: USBcamera.h:48
yarp::os::Property::fromString
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
Definition: Property.cpp:1046
yarp::dev::IFrameGrabberControls::hasFeature
virtual bool hasFeature(int feature, bool *hasFeature)=0
Check if camera has the requested feature (saturation, brightness ...
yarp::dev::IFrameGrabberControls::getCameraDescription
virtual bool getCameraDescription(CameraDescriptor *camera)=0
Get a basic description of the camera hw.
yarp::dev::DeviceDriver
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
USBCameraDriver::hasOnOff
bool hasOnOff(int feature, bool *HasOnOff) override
Check if the camera has the ability to turn on/off the requested feature.
Definition: USBcamera.cpp:376
USBCameraDriver::getRgbResolution
bool getRgbResolution(int &width, int &height) override
Get the resolution of the rgb image from the camera.
Definition: USBcamera.cpp:184
USBCameraDriver::pixelType
int pixelType
Definition: USBcamera.h:62
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
yarp::dev::IFrameGrabberControls::setFeature
virtual bool setFeature(int feature, double value)=0
Set the requested feature to a value (saturation, brightness ...
yarp::sig::ImageOf< yarp::sig::PixelRgb >
USBcameraLogComponent.h
USBCameraDriver::getRgbFOV
bool getRgbFOV(double &horizontalFov, double &verticalFov) override
Get the field of view (FOV) of the rgb camera.
Definition: USBcamera.cpp:206
USBCameraDriver::_width
size_t _width
Definition: USBcamera.h:60
yarp::sig::VectorOf
Provides:
Definition: Vector.h:122
USBCameraDriver::getMode
bool getMode(int feature, FeatureMode *mode) override
Get the current mode for the feature.
Definition: USBcamera.cpp:432
yarp::dev::IFrameGrabberRgb::getRgbBuffer
virtual bool getRgbBuffer(unsigned char *buffer)=0
Get a rgb buffer from the frame grabber, if required demosaicking/color reconstruction is applied.
USBCameraDriver::open
bool open(yarp::os::Searchable &config) override
Open the device driver.
Definition: USBcamera.cpp:54
USBCameraDriverRgb::height
int height() const override
Return the height of each frame.
Definition: USBcamera.cpp:284
USBCameraDriver::getRgbWidth
int getRgbWidth() override
Return the width of each frame.
Definition: USBcamera.cpp:167
USBCameraDriver::getRgbSupportedConfigurations
bool getRgbSupportedConfigurations(yarp::sig::VectorOf< yarp::dev::CameraConfig > &configurations) override
Get the possible configurations of the camera.
Definition: USBcamera.cpp:176
USBcamera.h
yarp::dev::IFrameGrabberControls::setMode
virtual bool setMode(int feature, FeatureMode mode)=0
Set the requested mode for the feature.
USBCameraDriver::getLastInputStamp
yarp::os::Stamp getLastInputStamp() override
Implements the IPreciselyTimed interface.
Definition: USBcamera.cpp:150
USBCameraDriver::deviceRgb
yarp::dev::IFrameGrabberRgb * deviceRgb
Definition: USBcamera.h:53
USBCameraDriverRaw::getImage
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:302
yarp::dev::IFrameGrabberControls::hasManual
virtual bool hasManual(int feature, bool *hasManual)=0
Check if the requested feature has the 'manual' mode.
Stamp.h
USBCameraDriver::setFeature
bool setFeature(int feature, double value) override
Set the requested feature to a value (saturation, brightness ...
Definition: USBcamera.cpp:344
USBCameraDriver::getActive
bool getActive(int feature, bool *isActive) override
Get the current status of the feature, on or off.
Definition: USBcamera.cpp:392
yarp::dev::IFrameGrabberControls::getFeature
virtual bool getFeature(int feature, double *value)=0
Get the current value for the requested feature.
USBCameraDriver::getRgbIntrinsicParam
bool getRgbIntrinsicParam(yarp::os::Property &intrinsic) override
Get the intrinsic parameters of the rgb camera.
Definition: USBcamera.cpp:222
yarp::dev::IFrameGrabber::getRawBuffer
virtual bool getRawBuffer(unsigned char *buffer)=0
Get the raw buffer from the frame grabber.
yarp::dev::IFrameGrabberControls::hasAuto
virtual bool hasAuto(int feature, bool *hasAuto)=0
Check if the requested feature has the 'auto' mode.
USBCameraDriver::getRgbHeight
int getRgbHeight() override
Return the height of each frame.
Definition: USBcamera.cpp:159
yarp::os::Property::check
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition: Property.cpp:1024
USBCameraDriver::setOnePush
bool setOnePush(int feature) override
Set the requested feature to a value (saturation, brightness ...
Definition: USBcamera.cpp:440
Semaphore.h
USBCameraDriver::hasManual
bool hasManual(int feature, bool *hasManual) override
Check if the requested feature has the 'manual' mode.
Definition: USBcamera.cpp:408
USBCameraDriver::getRgbMirroring
bool getRgbMirroring(bool &mirror) override
Get the mirroring setting of the sensor.
Definition: USBcamera.cpp:230
USBCameraDriverRaw::width
int width() const override
Return the width of each frame.
Definition: USBcamera.cpp:312
FrameGrabberInterfaces.h
define common interfaces to discover remote camera capabilities
USBCameraDriver::hasAuto
bool hasAuto(int feature, bool *hasAuto) override
Check if the requested feature has the 'auto' mode.
Definition: USBcamera.cpp:400
USBCameraDriver::hasFeature
bool hasFeature(int feature, bool *hasFeature) override
Check if camera has the requested feature (saturation, brightness ...
Definition: USBcamera.cpp:336
yarp::dev::IFrameGrabberControls::getMode
virtual bool getMode(int feature, FeatureMode *mode)=0
Get the current mode for the feature.
USBCameraDriver::getRawBufferSize
int getRawBufferSize() override
Implements the Frame grabber basic interface.
Definition: USBcamera.cpp:140
yarp::os::Stamp
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:25
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
USBCameraDriver::close
bool close() override
Closes the device driver.
Definition: USBcamera.cpp:102
USBCameraDriver::setActive
bool setActive(int feature, bool onoff) override
Set the requested feature on or off.
Definition: USBcamera.cpp:384
VOCAB_PIXEL_MONO
@ VOCAB_PIXEL_MONO
Definition: Image.h:48
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
USBCameraDriver::hasOnePush
bool hasOnePush(int feature, bool *hasOnePush) override
Check if the requested feature has the 'onePush' mode.
Definition: USBcamera.cpp:416
FeatureMode
FeatureMode
Definition: FrameGrabberInterfaces.h:29
USBCameraDriverRaw::height
int height() const override
Return the height of each frame.
Definition: USBcamera.cpp:317
USBCameraDriver::USBCameraDriver
USBCameraDriver()
Constructor.
Definition: USBcamera.cpp:42
USBCAMERA
const yarp::os::LogComponent & USBCAMERA()
Definition: USBcameraLogComponent.cpp:11
USBCameraDriverRaw::USBCameraDriverRaw
USBCameraDriverRaw()
Definition: USBcamera.cpp:290
USBCameraDriver::~USBCameraDriver
~USBCameraDriver() override
Destructor.
Definition: USBcamera.cpp:48
USBCameraDriver::getFeature
bool getFeature(int feature, double *value) override
Get the current value for the requested feature.
Definition: USBcamera.cpp:352
yarp::dev::IFrameGrabberControls::hasOnePush
virtual bool hasOnePush(int feature, bool *hasOnePush)=0
Check if the requested feature has the 'onePush' mode.
USBCameraDriver::width
int width() const override
Implements FrameGrabber basic interface.
Definition: USBcamera.cpp:110
VOCAB_PIXEL_RGB
@ VOCAB_PIXEL_RGB
Definition: Image.h:50
USBCameraDriver::height
int height() const override
Implements FrameGrabber basic interface.
Definition: USBcamera.cpp:122
yarp::dev::IFrameGrabberControls::setOnePush
virtual bool setOnePush(int feature)=0
Set the requested feature to a value (saturation, brightness ...
USBCameraDriverRgb::width
int width() const override
Return the width of each frame.
Definition: USBcamera.cpp:279
yCTrace
#define yCTrace(component,...)
Definition: LogComponent.h:88
USBCameraDriver::deviceControls
yarp::dev::IFrameGrabberControls * deviceControls
Definition: USBcamera.h:57
USBCameraDriver::setRgbMirroring
bool setRgbMirroring(bool mirror) override
Set the mirroring setting of the sensor.
Definition: USBcamera.cpp:238
USBCameraDriver::getCameraDescription
bool getCameraDescription(CameraDescriptor *camera) override
Implementation of IFrameGrabberControls2 interface.
Definition: USBcamera.cpp:328
USBCameraDriver::getRgbBuffer
bool getRgbBuffer(unsigned char *buffer) override
FrameGrabber bgr interface, returns the last acquired frame as a buffer of bgr triplets.
Definition: USBcamera.cpp:145
yarp::dev::IFrameGrabberControls::hasOnOff
virtual bool hasOnOff(int feature, bool *HasOnOff)=0
Check if the camera has the ability to turn on/off the requested feature.
yarp::dev::IFrameGrabberControls::setActive
virtual bool setActive(int feature, bool onoff)=0
Set the requested feature on or off.
USBCameraDriver::_height
size_t _height
Definition: USBcamera.h:61
USBCameraDriver::setMode
bool setMode(int feature, FeatureMode mode) override
Set the requested mode for the feature.
Definition: USBcamera.cpp:424
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37
DeviceDriver.h