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();
49int V4L_camera::convertYARP_to_V4L(
int feature)
122 use_exposure_absolute =
false;
132 configIntrins =
false;
146int V4L_camera::convertV4L_to_YARP_format(
int format)
180void V4L_camera::populateConfigurations()
191 frmsize.pixel_format = fmt.pixelformat;
198 frmival.pixel_format = fmt.pixelformat;
207 c.framerate = (1.0 *
frmival.discrete.denominator) /
frmival.discrete.numerator;
227 if (!fromConfig(config)) {
247 if (-1 == param.
fd) {
262 if (-1 == param.
fd) {
271 enumerate_controls();
280 populateConfigurations();
329 bool res = deviceInit();
339 return configFx && configFy;
351 return configIntrins;
373 if (config.
check(
"verbose")) {
377 if (!config.
check(
"width")) {
384 if (!config.
check(
"height")) {
391 if (!config.
check(
"framerate")) {
395 param.
fps = config.
find(
"framerate").asInt32();
398 if (!config.
check(
"d")) {
399 yCError(
USBCAMERA) <<
"No camera identifier was specified! (e.g. '--d /dev/video0' on Linux OS)";
406 if (!config.
check(
"camModel")) {
407 yCInfo(
USBCAMERA) <<
"No 'camModel' was specified, working with 'standard' uvc";
410 std::map<std::string, supported_cams>::iterator
it = camMap.find(config.
find(
"camModel").asString());
411 if (
it != camMap.end()) {
413 yCDebug(
USBCAMERA) <<
"cam model name : " << config.
find(
"camModel").asString() <<
" -- number : " <<
it->second;
417 for (
it = camMap.begin();
it != camMap.end();
it++) {
426 yCDebug(
USBCAMERA) <<
"-------------------------------\nusbCamera: Using leopard camera!!";
427 bit_shift = config.
check(
"shift",
Value(bit_shift),
"right shift of <n> bits").asInt32();
428 bit_bayer = config.
check(
"bit_bayer",
Value(bit_bayer),
"uses <n> bits bayer conversion").asInt32();
443 yCError(
USBCAMERA) <<
"bayer conversion with " << bit_bayer <<
"not supported";
448 yCDebug(
USBCAMERA) << bit_shift <<
"bits of right shift applied to raw data";
453 if (config.
check(
"crop")) {
460 Value isDual = config.
check(
"dual",
Value(0),
"Is this a dual camera? Two cameras merged into a single frame");
462 if (config.
find(
"dual").asBool()) {
470 if (!config.
check(
"pixelType")) {
475 type = config.
find(
"pixelType").asInt32();
481 param.
pixelType = convertV4L_to_YARP_format(param.
src_fmt.fmt.pix.pixelformat);
490 yCError(
USBCAMERA,
"no valid pixel format found!! This should not happen!!");
496 configFx = config.
check(
"horizontalFov");
497 configFy = config.
check(
"verticalFov");
498 configPPx = config.
check(
"principalPointX");
499 configPPy = config.
check(
"principalPointY");
500 configRet = config.
check(
"rectificationMatrix");
501 configDistM = config.
check(
"distortionModel");
503 bt = config.
findGroup(
"cameraDistortionModelGroup");
509 "fields k1, k2, k3, t1, t2, name are required when using cameraDistortionModelGroup";
510 configIntrins =
false;
514 configIntrins =
true;
517 configIntrins =
false;
519 param.
horizontalFov = config.
check(
"horizontalFov",
Value(0.0),
"desired horizontal fov of test image").asFloat64();
520 param.
verticalFov = config.
check(
"verticalFov",
Value(0.0),
"desired vertical fov of test image").asFloat64();
521 if (config.
check(
"mirror")) {
524 "mirroring disabled by default")
531 param.
intrinsic.
put(
"focalLengthX", config.
check(
"focalLengthX",
Value(0.0),
"Horizontal component of the focal lenght").asFloat64());
532 param.
intrinsic.
put(
"focalLengthY", config.
check(
"focalLengthY",
Value(0.0),
"Vertical component of the focal lenght").asFloat64());
533 param.
intrinsic.
put(
"principalPointX", config.
check(
"principalPointX",
Value(0.0),
"X coordinate of the principal point").asFloat64());
534 param.
intrinsic.
put(
"principalPointY", config.
check(
"principalPointY",
Value(0.0),
"Y coordinate of the principal point").asFloat64());
535 param.
intrinsic.
put(
"rectificationMatrix", config.
check(
"rectificationMatrix", *
retM,
"Matrix that describes the lens' distortion"));
536 param.
intrinsic.
put(
"distortionModel", config.
check(
"distortionModel",
Value(
""),
"Reference to group of parameters describing the distortion model of the camera").asString());
546 param.
intrinsic.
put(
"k1", bt.
check(
"k1",
Value(0.0),
"Radial distortion coefficient of the lens").asFloat64());
547 param.
intrinsic.
put(
"k2", bt.
check(
"k2",
Value(0.0),
"Radial distortion coefficient of the lens").asFloat64());
548 param.
intrinsic.
put(
"k3", bt.
check(
"k3",
Value(0.0),
"Radial distortion coefficient of the lens").asFloat64());
558int V4L_camera::getfd()
563bool V4L_camera::threadInit()
572void V4L_camera::run()
574 if (full_FrameRead()) {
581 if ((timeElapsed = timeNow - timeStart) > 1.0f) {
582 yCInfo(
USBCAMERA,
"frames acquired %d in %f sec", frameCounter, timeElapsed);
588void V4L_camera::threadRelease()
597bool V4L_camera::deviceInit()
662 if (_v4lconvert_data ==
nullptr) {
663 yCError(
USBCAMERA) <<
"Failed to initialize v4lconvert. Conversion to required format may not work";
697 yCWarning(
USBCAMERA) <<
"Conversion from HW supported configuration into user requested format will require addictional step.\n"
698 <<
"Performance issue may arise.";
747 if (param.
fps != -1) {
752 frameint.parm.capture.timeperframe.numerator = 1;
753 frameint.parm.capture.timeperframe.denominator = param.
fps;
782 readInit(param.
src_fmt.fmt.pix.sizeimage);
790 userptrInit(param.
src_fmt.fmt.pix.sizeimage);
802bool V4L_camera::deviceUninit()
839 if (param.
buffers !=
nullptr) {
875 if (param.
fd != -1) {
965int V4L_camera::xioctl(
int fd,
int request,
void*
argp)
983void V4L_camera::enumerate_menu()
1001bool V4L_camera::enumerate_controls()
1052bool V4L_camera::full_FrameRead()
1067 for (
unsigned int i = 0;
i < count;
i++) {
1075 r = select(param.
fd + 1, &
fds,
nullptr,
nullptr, &
tv);
1110bool V4L_camera::frameRead()
1123 timeStamp.
update(toEpochOffset + buf.timestamp.tv_sec + buf.timestamp.tv_usec / 1000000.0);
1147 timeStamp.
update(toEpochOffset + buf.timestamp.tv_sec + buf.timestamp.tv_usec / 1000000.0);
1170 if (buf.m.userptr == (
unsigned long)param.
buffers[
i].start && buf.length == param.
buffers[
i].length) {
1181 timeStamp.
update(toEpochOffset + buf.timestamp.tv_sec + buf.timestamp.tv_usec / 1000000.0);
1201void V4L_camera::imagePreProcess()
1216 param.
src_fmt.fmt.pix.bytesperline = param.
src_fmt.fmt.pix.width;
1217 param.
src_fmt.fmt.pix.pixelformat = pixel_fmt_leo;
1230void V4L_camera::imageProcess()
1232 static bool initted =
false;
1249 if ((err % 20) == 0) {
1286 timeTot += timeElapsed;
1288 if ((myCounter % 60) == 0) {
1300void V4L_camera::captureStop()
1324void V4L_camera::captureStart()
1366 buf.length = param.
buffers[
i].length;
1384bool V4L_camera::readInit(
unsigned int buffer_size)
1388 if (param.
buffers ==
nullptr) {
1396 if (param.
buffers[0].start ==
nullptr) {
1403bool V4L_camera::mmapInit()
1421 if (param.
req.count < 1) {
1426 if (param.
req.count == 1) {
1427 yCError(
USBCAMERA,
"Only 1 buffer was available, you may encounter performance issue acquiring images from device %s", param.
deviceId.c_str());
1432 if (param.
buffers ==
nullptr) {
1460bool V4L_camera::userptrInit(
unsigned int buffer_size)
1484 if (param.
buffers ==
nullptr) {
1553bool V4L_camera::check_V4L2_control(
uint32_t id)
1617 camera->deviceDescription =
"USB3 camera";
1653 if (use_exposure_absolute) {
1661 ret = set_V4L2_control(convertYARP_to_V4L(
feature), value);
1672 if (use_exposure_absolute) {
1680 tmp = get_V4L2_control(convertYARP_to_V4L(
feature));
1784 isActive_vector[
feature] =
true;
1787 isActive_vector[
feature] =
false;
@ YARP_FEATURE_BRIGHTNESS
@ YARP_FEATURE_WHITE_BALANCE
@ YARP_FEATURE_SATURATION
@ 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
bool getMode(int feature, FeatureMode *mode) override
Get the current mode for the feature.
bool getRgbMirroring(bool &mirror) override
Get the mirroring setting of the sensor.
bool setRgbMirroring(bool mirror) override
Set the mirroring setting of the sensor.
int getRgbWidth() override
Return the width of each frame.
bool setMode(int feature, FeatureMode mode) override
Set the requested mode for the feature.
yarp::os::Stamp getLastInputStamp() override
Return the time stamp relative to the last acquisition.
bool setRgbFOV(double horizontalFov, double verticalFov) override
Set the field of view (FOV) of the rgb camera.
bool setRgbResolution(int width, int height) override
Set the resolution of the rgb image from the camera.
bool hasOnePush(int feature, bool *_hasOnePush) override
Check if the requested feature has the 'onePush' mode.
bool close() override
close device
bool hasFeature(int feature, bool *hasFeature) override
Check if camera has the requested feature (saturation, brightness ... )
bool getRgbIntrinsicParam(yarp::os::Property &intrinsic) override
Get the intrinsic parameters of the rgb camera.
bool hasOnOff(int feature, bool *_hasOnOff) override
Check if the camera has the ability to turn on/off the requested feature.
bool setActive(int feature, bool onoff) override
Set the requested feature on or off.
bool getCameraDescription(CameraDescriptor *camera) override
Get a basic description of the camera hw.
int getRgbHeight() override
Return the height of each frame.
bool getRgbFOV(double &horizontalFov, double &verticalFov) override
Get the field of view (FOV) of the rgb camera.
bool hasAuto(int feature, bool *_hasAuto) override
Check if the requested feature has the 'auto' mode.
int width() const override
Return the width of each frame.
bool getFeature(int feature, double *value) override
Get the current value for the requested feature.
bool setOnePush(int feature) override
Set the requested feature to a value (saturation, brightness ... )
bool setFeature(int feature, double value) override
Set the requested feature to a value (saturation, brightness ... )
bool getActive(int feature, bool *_isActive) override
Get the current status of the feature, on or off.
bool getRgbResolution(int &width, int &height) override
Get the resolution of the rgb image from the camera.
int height() const override
Return the height of each frame.
bool getImage(yarp::sig::ImageOf< yarp::sig::PixelRgb > &image) override
Get an image from the frame grabber.
bool getRgbSupportedConfigurations(yarp::sig::VectorOf< yarp::dev::CameraConfig > &configurations) override
Get the possible configurations of the camera.
bool open(yarp::os::Searchable &config) override
open device
bool hasManual(int feature, bool *_hasManual) override
Check if the requested feature has the 'manual' mode.
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.
unsigned char * getRawImage() const
Access to the internal image buffer.
void resize(size_t imgWidth, size_t imgHeight)
Reallocate an image to be of a desired size, throwing away its current contents.
void push_back(const T &elem)
Push a new element in the vector: size is changed.
#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
yarp::sig::VectorOf< yarp::dev::CameraConfig > configurations
struct v4l2_format src_fmt
unsigned char * raw_image
unsigned char * dst_image_rgb
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
Struct describing a possible camera configuration.