17#include <opencv2/core/core.hpp>
18#include <opencv2/imgproc/imgproc.hpp>
19#include <opencv2/core/core_c.h>
41 if (config.
check(key)) {
42 return config.
find(key).asFloat64();
52 case cameraFeature_id_t::YARP_FEATURE_BRIGHTNESS:
54 case cameraFeature_id_t::YARP_FEATURE_SHUTTER:
55 case cameraFeature_id_t::YARP_FEATURE_EXPOSURE:
57 case cameraFeature_id_t::YARP_FEATURE_SHARPNESS:
59 case cameraFeature_id_t::YARP_FEATURE_HUE:
61 case cameraFeature_id_t::YARP_FEATURE_SATURATION:
63 case cameraFeature_id_t::YARP_FEATURE_GAMMA:
65 case cameraFeature_id_t::YARP_FEATURE_GAIN:
67 case cameraFeature_id_t::YARP_FEATURE_IRIS:
123 use_exposure_absolute =
false;
133 configIntrins =
false;
147int V4L_camera::convertV4L_to_YARP_format(
int format)
181void V4L_camera::populateConfigurations()
208 c.framerate = (1.0 *
frmival.discrete.denominator) /
frmival.discrete.numerator;
228 if (!fromConfig(config)) {
248 if (-1 == param.
fd) {
263 if (-1 == param.
fd) {
272 enumerate_controls();
281 populateConfigurations();
284 setFeature(cameraFeature_id_t::YARP_FEATURE_GAIN, checkDouble(config,
"gain"));
285 setFeature(cameraFeature_id_t::YARP_FEATURE_EXPOSURE, checkDouble(config,
"exposure"));
286 setFeature(cameraFeature_id_t::YARP_FEATURE_BRIGHTNESS, checkDouble(config,
"brightness"));
287 setFeature(cameraFeature_id_t::YARP_FEATURE_SHARPNESS, checkDouble(config,
"sharpness"));
292 setFeature(cameraFeature_id_t::YARP_FEATURE_HUE, checkDouble(config,
"hue"));
293 setFeature(cameraFeature_id_t::YARP_FEATURE_SATURATION, checkDouble(config,
"saturation"));
294 setFeature(cameraFeature_id_t::YARP_FEATURE_GAMMA, checkDouble(config,
"gamma"));
295 setFeature(cameraFeature_id_t::YARP_FEATURE_SHUTTER, checkDouble(config,
"shutter"));
296 setFeature(cameraFeature_id_t::YARP_FEATURE_IRIS, checkDouble(config,
"iris"));
330 bool res = deviceInit();
334 return ReturnValue::return_code::return_value_error_method_failed;
341 bool b = configFx && configFy;
343 return ReturnValue::return_code::return_value_error_method_failed;
349 return ReturnValue::return_code::return_value_error_not_implemented_by_device;
370 return ReturnValue::return_code::return_value_error_method_failed;
377 if (config.
check(
"verbose")) {
381 if (!config.
check(
"width")) {
388 if (!config.
check(
"height")) {
395 if (!config.
check(
"framerate")) {
399 param.
fps = config.
find(
"framerate").asInt32();
402 if (!config.
check(
"d")) {
403 yCError(
USBCAMERA) <<
"No camera identifier was specified! (e.g. '--d /dev/video0' on Linux OS)";
410 if (!config.
check(
"camModel")) {
411 yCInfo(
USBCAMERA) <<
"No 'camModel' was specified, working with 'standard' uvc";
414 std::map<std::string, supported_cams>::iterator
it = camMap.find(config.
find(
"camModel").asString());
415 if (
it != camMap.end()) {
417 yCDebug(
USBCAMERA) <<
"cam model name : " << config.
find(
"camModel").asString() <<
" -- number : " <<
it->second;
421 for (
it = camMap.begin();
it != camMap.end();
it++) {
430 yCDebug(
USBCAMERA) <<
"-------------------------------\nusbCamera: Using leopard camera!!";
431 bit_shift = config.
check(
"shift",
Value(bit_shift),
"right shift of <n> bits").asInt32();
432 bit_bayer = config.
check(
"bit_bayer",
Value(bit_bayer),
"uses <n> bits bayer conversion").asInt32();
447 yCError(
USBCAMERA) <<
"bayer conversion with " << bit_bayer <<
"not supported";
452 yCDebug(
USBCAMERA) << bit_shift <<
"bits of right shift applied to raw data";
457 if (config.
check(
"crop")) {
464 Value isDual = config.
check(
"dual",
Value(0),
"Is this a dual camera? Two cameras merged into a single frame");
466 if (config.
find(
"dual").asBool()) {
474 if (!config.
check(
"pixelType")) {
479 type = config.
find(
"pixelType").asInt32();
485 param.
pixelType = convertV4L_to_YARP_format(param.
src_fmt.fmt.pix.pixelformat);
494 yCError(
USBCAMERA,
"no valid pixel format found!! This should not happen!!");
500 configFx = config.
check(
"horizontalFov");
501 configFy = config.
check(
"verticalFov");
502 configPPx = config.
check(
"principalPointX");
503 configPPy = config.
check(
"principalPointY");
504 configRet = config.
check(
"rectificationMatrix");
505 configDistM = config.
check(
"distortionModel");
507 bt = config.
findGroup(
"cameraDistortionModelGroup");
513 "fields k1, k2, k3, t1, t2, name are required when using cameraDistortionModelGroup";
514 configIntrins =
false;
518 configIntrins =
true;
521 configIntrins =
false;
523 param.
horizontalFov = config.
check(
"horizontalFov",
Value(0.0),
"desired horizontal fov of test image").asFloat64();
524 param.
verticalFov = config.
check(
"verticalFov",
Value(0.0),
"desired vertical fov of test image").asFloat64();
525 if (config.
check(
"mirror")) {
528 "mirroring disabled by default")
535 param.
intrinsic.
put(
"focalLengthX", config.
check(
"focalLengthX",
Value(0.0),
"Horizontal component of the focal lenght").asFloat64());
536 param.
intrinsic.
put(
"focalLengthY", config.
check(
"focalLengthY",
Value(0.0),
"Vertical component of the focal lenght").asFloat64());
537 param.
intrinsic.
put(
"principalPointX", config.
check(
"principalPointX",
Value(0.0),
"X coordinate of the principal point").asFloat64());
538 param.
intrinsic.
put(
"principalPointY", config.
check(
"principalPointY",
Value(0.0),
"Y coordinate of the principal point").asFloat64());
539 param.
intrinsic.
put(
"rectificationMatrix", config.
check(
"rectificationMatrix", *
retM,
"Matrix that describes the lens' distortion"));
540 param.
intrinsic.
put(
"distortionModel", config.
check(
"distortionModel",
Value(
""),
"Reference to group of parameters describing the distortion model of the camera").asString());
550 param.
intrinsic.
put(
"k1", bt.
check(
"k1",
Value(0.0),
"Radial distortion coefficient of the lens").asFloat64());
551 param.
intrinsic.
put(
"k2", bt.
check(
"k2",
Value(0.0),
"Radial distortion coefficient of the lens").asFloat64());
552 param.
intrinsic.
put(
"k3", bt.
check(
"k3",
Value(0.0),
"Radial distortion coefficient of the lens").asFloat64());
562int V4L_camera::getfd()
567bool V4L_camera::threadInit()
576void V4L_camera::run()
578 if (full_FrameRead()) {
585 if ((timeElapsed = timeNow - timeStart) > 1.0f) {
586 yCInfo(
USBCAMERA,
"frames acquired %d in %f sec", frameCounter, timeElapsed);
592void V4L_camera::threadRelease()
601bool V4L_camera::deviceInit()
666 if (_v4lconvert_data ==
nullptr) {
667 yCError(
USBCAMERA) <<
"Failed to initialize v4lconvert. Conversion to required format may not work";
701 yCWarning(
USBCAMERA) <<
"Conversion from HW supported configuration into user requested format will require addictional step.\n"
702 <<
"Performance issue may arise.";
751 if (param.
fps != -1) {
756 frameint.parm.capture.timeperframe.numerator = 1;
757 frameint.parm.capture.timeperframe.denominator = param.
fps;
786 readInit(param.
src_fmt.fmt.pix.sizeimage);
794 userptrInit(param.
src_fmt.fmt.pix.sizeimage);
806bool V4L_camera::deviceUninit()
843 if (param.
buffers !=
nullptr) {
879 if (param.
fd != -1) {
915 return ReturnValue::return_code::return_value_error_method_failed;
934 return ReturnValue::return_code::return_value_error_method_failed;
971int V4L_camera::xioctl(
int fd,
int request,
void*
argp)
989void V4L_camera::enumerate_menu()
1007bool V4L_camera::enumerate_controls()
1058bool V4L_camera::full_FrameRead()
1073 for (
unsigned int i = 0; i < count; i++) {
1081 r = select(param.
fd + 1, &
fds,
nullptr,
nullptr, &
tv);
1116bool V4L_camera::frameRead()
1129 timeStamp.
update(toEpochOffset + buf.timestamp.tv_sec + buf.timestamp.tv_usec / 1000000.0);
1153 timeStamp.
update(toEpochOffset + buf.timestamp.tv_sec + buf.timestamp.tv_usec / 1000000.0);
1176 if (buf.m.userptr == (
unsigned long)param.
buffers[i].start && buf.length == param.
buffers[i].length) {
1187 timeStamp.
update(toEpochOffset + buf.timestamp.tv_sec + buf.timestamp.tv_usec / 1000000.0);
1207void V4L_camera::imagePreProcess()
1222 param.
src_fmt.fmt.pix.bytesperline = param.
src_fmt.fmt.pix.width;
1223 param.
src_fmt.fmt.pix.pixelformat = pixel_fmt_leo;
1236void V4L_camera::imageProcess()
1238 static bool initted =
false;
1255 if ((err % 20) == 0) {
1292 timeTot += timeElapsed;
1294 if ((myCounter % 60) == 0) {
1306void V4L_camera::captureStop()
1330void V4L_camera::captureStart()
1371 buf.m.userptr = (
unsigned long)param.
buffers[i].start;
1372 buf.length = param.
buffers[i].length;
1390bool V4L_camera::readInit(
unsigned int buffer_size)
1394 if (param.
buffers ==
nullptr) {
1399 param.
buffers[0].length = buffer_size;
1402 if (param.
buffers[0].start ==
nullptr) {
1409bool V4L_camera::mmapInit()
1427 if (param.
req.count < 1) {
1432 if (param.
req.count == 1) {
1433 yCError(
USBCAMERA,
"Only 1 buffer was available, you may encounter performance issue acquiring images from device %s", param.
deviceId.c_str());
1438 if (param.
buffers ==
nullptr) {
1466bool V4L_camera::userptrInit(
unsigned int buffer_size)
1490 if (param.
buffers ==
nullptr) {
1559bool V4L_camera::check_V4L2_control(
uint32_t id)
1634 case cameraFeature_id_t::YARP_FEATURE_WHITE_BALANCE:
1640 case cameraFeature_id_t::YARP_FEATURE_EXPOSURE:
1658 case cameraFeature_id_t::YARP_FEATURE_EXPOSURE:
1659 if (use_exposure_absolute) {
1667 ret = set_V4L2_control(convertYARP_to_V4L(
feature), value);
1671 return ReturnValue::return_code::return_value_error_method_failed;
1678 case cameraFeature_id_t::YARP_FEATURE_EXPOSURE:
1679 if (use_exposure_absolute) {
1687 tmp = get_V4L2_control(convertYARP_to_V4L(
feature));
1692 return ReturnValue::return_code::return_value_error_method_failed;
1701 if (
feature == cameraFeature_id_t::YARP_FEATURE_WHITE_BALANCE) {
1708 else return ReturnValue::return_code::return_value_error_method_failed;
1710 return ReturnValue::return_code::return_value_error_method_failed;
1715 if (
feature == cameraFeature_id_t::YARP_FEATURE_WHITE_BALANCE) {
1718 bool b = !((value1 == -1) || (value2 == -1));
1720 else return ReturnValue::return_code::return_value_error_method_failed;
1722 return ReturnValue::return_code::return_value_error_method_failed;
1731 case cameraFeature_id_t::YARP_FEATURE_WHITE_BALANCE:
1732 case cameraFeature_id_t::YARP_FEATURE_EXPOSURE:
1758 case cameraFeature_id_t::YARP_FEATURE_WHITE_BALANCE:
1765 case cameraFeature_id_t::YARP_FEATURE_EXPOSURE:
1798 return ReturnValue::return_code::return_value_error_method_failed;
1806 case cameraFeature_id_t::YARP_FEATURE_WHITE_BALANCE:
1817 case cameraFeature_id_t::YARP_FEATURE_EXPOSURE:
1838 case cameraFeature_id_t::YARP_FEATURE_WHITE_BALANCE:
1842 case cameraFeature_id_t::YARP_FEATURE_BRIGHTNESS:
1846 case cameraFeature_id_t::YARP_FEATURE_GAIN:
1850 case cameraFeature_id_t::YARP_FEATURE_EXPOSURE:
1854 case cameraFeature_id_t::YARP_FEATURE_HUE:
1867 if (
feature == cameraFeature_id_t::YARP_FEATURE_WHITE_BALANCE) {
1872 if (
feature == cameraFeature_id_t::YARP_FEATURE_EXPOSURE) {
1883 case cameraFeature_id_t::YARP_FEATURE_WHITE_BALANCE:
1898 case cameraFeature_id_t::YARP_FEATURE_WHITE_BALANCE:
1899 if (
mode == FeatureMode::MODE_AUTO) {
1906 case cameraFeature_id_t::YARP_FEATURE_EXPOSURE:
1911 if (
mode == FeatureMode::MODE_AUTO) {
1917 ret =
mode != FeatureMode::MODE_AUTO;
1921 case cameraFeature_id_t::YARP_FEATURE_GAIN:
1922 if (
mode == FeatureMode::MODE_AUTO) {
1931 case cameraFeature_id_t::YARP_FEATURE_BRIGHTNESS:
1937 if (
mode == FeatureMode::MODE_AUTO) {
1943 ret =
mode != FeatureMode::MODE_AUTO;
1948 case cameraFeature_id_t::YARP_FEATURE_HUE:
1949 if (
mode == FeatureMode::MODE_AUTO) {
1962 return ReturnValue::return_code::return_value_error_method_failed;
1969 case cameraFeature_id_t::YARP_FEATURE_WHITE_BALANCE:
1976 case cameraFeature_id_t::YARP_FEATURE_EXPOSURE:
1980 mode = FeatureMode::MODE_MANUAL;
1985 mode = FeatureMode::MODE_MANUAL;
1987 mode = FeatureMode::MODE_AUTO;
1992 case cameraFeature_id_t::YARP_FEATURE_BRIGHTNESS:
1996 mode = FeatureMode::MODE_MANUAL;
2003 case cameraFeature_id_t::YARP_FEATURE_GAIN:
2007 mode = FeatureMode::MODE_MANUAL;
2014 case cameraFeature_id_t::YARP_FEATURE_HUE:
2018 mode = FeatureMode::MODE_MANUAL;
2026 mode = FeatureMode::MODE_MANUAL;
2035 if (
feature == cameraFeature_id_t::YARP_FEATURE_WHITE_BALANCE) {
2042 return ReturnValue::return_code::return_value_error_method_failed;
2046 return ReturnValue::return_code::return_value_error_method_failed;
@ VOCAB_PIXEL_ENCODING_BAYER_BGGR16
@ VOCAB_PIXEL_ENCODING_BAYER_BGGR8
@ VOCAB_PIXEL_ENCODING_BAYER_RGGB8
@ VOCAB_PIXEL_ENCODING_BAYER_GRBG8
@ VOCAB_PIXEL_ENCODING_BAYER_GBRG8
const yarp::os::LogComponent & USBCAMERA()
struct v4l2_queryctrl queryctrl
static double getEpochTimeShift()
struct v4l2_querymenu querymenu
#define DEFAULT_FRAMERATE
#define VIDIOC_REQBUFS_COUNT
yarp::dev::ReturnValue getRgbIntrinsicParam(yarp::os::Property &intrinsic) override
Get the intrinsic parameters of the rgb camera.
int getRgbWidth() override
Return the width of each frame.
yarp::dev::ReturnValue getRgbResolution(int &width, int &height) override
Get the resolution of the rgb image from the camera.
yarp::dev::ReturnValue setRgbFOV(double horizontalFov, double verticalFov) override
Set the field of view (FOV) of the rgb camera.
yarp::dev::ReturnValue getFeature(yarp::dev::cameraFeature_id_t feature, double &value) override
Get the current value for the requested feature.
yarp::dev::ReturnValue getRgbSupportedConfigurations(std::vector< yarp::dev::CameraConfig > &configurations) override
Get the possible configurations of the camera.
yarp::os::Stamp getLastInputStamp() override
Return the time stamp relative to the last acquisition.
yarp::dev::ReturnValue setMode(yarp::dev::cameraFeature_id_t feature, yarp::dev::FeatureMode mode) override
Set the requested mode for the feature.
yarp::dev::ReturnValue hasAuto(yarp::dev::cameraFeature_id_t feature, bool &hasAuto) override
Check if the requested feature has the 'auto' mode.
yarp::dev::ReturnValue getImage(yarp::sig::ImageOf< yarp::sig::PixelRgb > &image) override
Get an image from the frame grabber.
yarp::dev::ReturnValue setRgbMirroring(bool mirror) override
Set the mirroring setting of the sensor.
yarp::dev::ReturnValue hasManual(yarp::dev::cameraFeature_id_t feature, bool &hasManual) override
Check if the requested feature has the 'manual' mode.
yarp::dev::ReturnValue setActive(yarp::dev::cameraFeature_id_t feature, bool onoff) override
Set the requested feature on or off.
bool close() override
close device
yarp::dev::ReturnValue setRgbResolution(int width, int height) override
Set the resolution of the rgb image from the camera.
int getRgbHeight() override
Return the height of each frame.
yarp::dev::ReturnValue getActive(yarp::dev::cameraFeature_id_t feature, bool &isActive) override
Get the current status of the feature, on or off.
yarp::dev::ReturnValue getRgbFOV(double &horizontalFov, double &verticalFov) override
Get the field of view (FOV) of the rgb camera.
int width() const override
Return the width of each frame.
yarp::dev::ReturnValue hasOnOff(yarp::dev::cameraFeature_id_t feature, bool &HasOnOff) override
Check if the camera has the ability to turn on/off the requested feature.
yarp::dev::ReturnValue getMode(yarp::dev::cameraFeature_id_t feature, yarp::dev::FeatureMode &mode) override
Get the current mode for the feature.
yarp::dev::ReturnValue setFeature(yarp::dev::cameraFeature_id_t feature, double value) override
Set the requested feature to a value (saturation, brightness ... )
yarp::dev::ReturnValue hasOnePush(yarp::dev::cameraFeature_id_t feature, bool &hasOnePush) override
Check if the requested feature has the 'onePush' mode.
yarp::dev::ReturnValue getRgbMirroring(bool &mirror) override
Get the mirroring setting of the sensor.
int height() const override
Return the height of each frame.
yarp::dev::ReturnValue setOnePush(yarp::dev::cameraFeature_id_t feature) override
Set the requested feature to a value (saturation, brightness ... )
yarp::dev::ReturnValue hasFeature(yarp::dev::cameraFeature_id_t feature, bool &hasFeature) override
Check if camera has the requested feature (saturation, brightness ... )
bool open(yarp::os::Searchable &config) override
open device
yarp::dev::ReturnValue getCameraDescription(yarp::dev::CameraDescriptor &camera) override
Get a basic description of the camera hw.
Struct describing a possible camera configuration.
virtual std::string id() const
Return the id assigned to the PolyDriver.
FeatureMode toFeatureMode(bool _auto)
A simple collection of objects that can be described and transmitted in a portable way.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
bool isNull() const override
Checks if the object is invalid.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
A mini-server for performing network communication in the background.
BufferedPort()
Constructor.
An abstraction for a periodic thread.
bool start()
Call this to start the thread.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
A class for storing options and configuration information.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
A base class for nested structures that can be searched.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
void wait()
Decrement the counter, even if we must wait to do that.
void post()
Increment the counter.
An abstraction for a time stamp and/or sequence number.
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
A single value (typically within a Bottle).
static Value * makeList()
Create a list Value.
bool isNull() const override
Checks if the object is invalid.
#define YARP_NULLPTR
Expands to either the standard nullptr or to 0 elsewhere.
#define yCInfo(component,...)
#define yCError(component,...)
#define yCTrace(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
void list_cap_v4l2(int fd)
void query_current_image_fmt_v4l2(int fd)
For streams capable of holding different kinds of content, check what they actually have.
double now()
Return the current time in seconds, relative to an arbitrary starting point.
void delay(double seconds)
Wait for a certain number of seconds.
An interface to the operating system, including Port based communication.
int stat(const char *path)
Portable wrapper for the stat() function.
struct v4l2_format dst_fmt
unsigned char * read_image
struct v4l2_format src_fmt
unsigned char * raw_image
unsigned char * dst_image_rgb
std::vector< yarp::dev::CameraConfig > configurations
yarp::os::Property intrinsic
struct v4l2_requestbuffers req
unsigned char * src_image
unsigned int dst_image_size_rgb
unsigned int src_image_size
unsigned int raw_image_size
std::string deviceDescription