YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
V4L_camera.h
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#ifndef YARP_DEVICE_USBCAMERA_LINUX_V4L_CAMERA_H
8#define YARP_DEVICE_USBCAMERA_LINUX_V4L_CAMERA_H
9
11#include <yarp/os/Semaphore.h>
12
18
19#include <asm/types.h>
20#include <opencv2/opencv.hpp>
21#include <cerrno>
22#include <fcntl.h>
23#include <getopt.h>
24#include <iostream>
25#include <jpeglib.h>
26#include <libv4l2.h>
27#include <libv4lconvert.h>
28#include <linux/videodev2.h>
29#include <malloc.h>
30#include <map>
31#include <cstdlib>
32#include <cstring>
33#include <sys/ioctl.h>
34#include <sys/mman.h>
35#include <sys/stat.h>
36#include <sys/time.h>
37#include <sys/types.h>
38#include <unistd.h>
39
40#define CLEAR(x) memset(&(x), 0, sizeof(x))
41
42// minimum number of buffers to request in VIDIOC_REQBUFS call
43#define DEFAULT_WIDTH 640
44#define DEFAULT_HEIGHT 480
45#define DEFAULT_FRAMERATE 30
46#define VIDIOC_REQBUFS_COUNT 2
47
54
60
61
62struct buffer
63{
64 void* start;
65 size_t length;
66};
67
68
69typedef struct
70{
71 int fd;
72 std::string deviceId;
73
75 int resizeOffset_x, resizeOffset_y;
76 int resizeWidth, resizeHeight;
77
80
84 bool dual;
85
87 int fps;
88
89 // Temporary step required for leopard python camera only
90 // The image has to be converted into standard bayer format
91 // in order to be correctly converted into rgb
92 unsigned char* raw_image;
93 unsigned int raw_image_size;
94
95 // this is a helper pointer set either to raw_image or src_image,
96 // depending if the source is custom or standard
97 unsigned char* read_image;
98
99 // src image: standard image type read from the camera sensor
100 // used as input for color conversion
101 unsigned char* src_image;
102 unsigned int src_image_size;
103
104 // RGB image after color conversion. The size may not be the one
105 // requested by the user and a rescaling may be required afterwards
106 unsigned char* dst_image_rgb;
107 unsigned int dst_image_size_rgb;
108
109 // OpenCV object to perform the final rescaling of the image
110 cv::Mat outMat; // OpenCV output
111
112 std::vector<yarp::dev::CameraConfig> configurations;
113 bool flip;
114
115 unsigned int n_buffers;
117 struct v4l2_format src_fmt;
118 struct v4l2_format dst_fmt;
119 struct v4l2_requestbuffers req;
120 size_t pixelType;
121 supported_cams camModel; // In case some camera requires custom procedure
123
124
125/*
126 * Device handling
127 */
128
137{
138public:
139 V4L_camera();
140
141 // DeviceDriver Interface
142 bool open(yarp::os::Searchable& config) override;
143 bool close() override;
144
146
147 /*Implementation of IFrameGrabberImage and IFrameGrabberImageRaw interfaces*/
150 int height() const override;
151 int width() const override;
152
153 /*Implementation of IRgbVisualParams interface*/
154 int getRgbHeight() override;
155 int getRgbWidth() override;
156 yarp::dev::ReturnValue getRgbSupportedConfigurations(std::vector<yarp::dev::CameraConfig>& configurations) override;
159 yarp::dev::ReturnValue getRgbFOV(double& horizontalFov, double& verticalFov) override;
160 yarp::dev::ReturnValue setRgbFOV(double horizontalFov, double verticalFov) override;
162 yarp::dev::ReturnValue getRgbMirroring(bool& mirror) override;
163 yarp::dev::ReturnValue setRgbMirroring(bool mirror) override;
164
165
166 /* Implementation of IFrameGrabberControls interface */
171 yarp::dev::ReturnValue setFeature(yarp::dev::cameraFeature_id_t feature, double value1, double value2) override;
172 yarp::dev::ReturnValue getFeature(yarp::dev::cameraFeature_id_t feature, double& value1, double& value2) override;
182
183
184private:
185 bool verbose;
186 v4lconvert_data* _v4lconvert_data;
187 bool use_exposure_absolute;
188
189 yarp::os::Stamp timeStamp;
190 Video_params param;
192 bool configFx, configFy;
193 bool configPPx, configPPy;
194 bool configRet, configDistM;
195 bool configIntrins;
196 bool configured;
197 bool doCropping;
199 double timeStart, timeTot, timeNow, timeElapsed;
200 int myCounter;
201 int frameCounter;
202
203 std::map<std::string, supported_cams> camMap;
204
205 bool fromConfig(yarp::os::Searchable& config);
206
207 void populateConfigurations();
208
209 int convertV4L_to_YARP_format(int format);
210
211 double checkDouble(yarp::os::Searchable& config, const char* key);
212
213 // initialize device
214 bool deviceInit();
215
216 // de-initialize device
217 bool deviceUninit();
218
219 void captureStart();
220 void captureStop();
221
222 bool threadInit() override;
223 void run() override;
224 void threadRelease() override;
225
226
227 /*
228 * Inintialize different types of reading frame
229 */
230
231 // some description
232 bool readInit(unsigned int buffer_size);
233
234 // some description
235 bool mmapInit();
236
237 // some description
238 bool userptrInit(unsigned int buffer_size);
239
240
241 // use the device for something
245 bool frameRead();
246
247 bool full_FrameRead();
248
249 /*
250 * This function is intended to perform custom code to adapt
251 * non standard pixel types to a standard one, in order to
252 * use standard conversion libraries afterward.
253 */
254 void imagePreProcess();
255
256 /*
257 * This function is intended to perform all the required conversions
258 * from the camera pixel type to the RGB one and eventually rescaling
259 * to size requested by the user.
260 */
261 void imageProcess();
262
263 int getfd();
264
265private:
266 // low level stuff - all functions here uses the Linux V4L specific definitions
275 int xioctl(int fd, int request, void* argp);
276
277 int convertYARP_to_V4L(yarp::dev::cameraFeature_id_t feature);
278 void enumerate_menu();
279 bool enumerate_controls();
280 bool check_V4L2_control(uint32_t id);
281 bool set_V4L2_control(u_int32_t id, double value, bool verbatim = false);
282 double get_V4L2_control(uint32_t id, bool verbatim = false); // verbatim = do not convert value, for enum types
283
284 double toEpochOffset;
285
286 // leopard de-bayer test
287 int bit_shift;
288 int bit_bayer;
289 int pixel_fmt_leo;
290};
291
292#endif // YARP_DEVICE_USBCAMERA_LINUX_V4L_CAMERA_H
CameraDescriptor camera
FeatureMode mode
supported_cams
Definition V4L_camera.h:56
@ STANDARD_UVC
Definition V4L_camera.h:57
@ LEOPARD_PYTHON
Definition V4L_camera.h:58
io_method
Definition V4L_camera.h:49
@ IO_METHOD_MMAP
Definition V4L_camera.h:51
@ IO_METHOD_READ
Definition V4L_camera.h:50
@ IO_METHOD_USERPTR
Definition V4L_camera.h:52
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 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 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 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 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 ... )
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.
Interface implemented by all device drivers.
Control interface for frame grabber devices.
An interface for retrieving intrinsic parameter from a rgb camera.
An abstraction for a periodic thread.
A class for storing options and configuration information.
Definition Property.h:33
A base class for nested structures that can be searched.
Definition Searchable.h:31
A class for thread synchronization and mutual exclusion.
Definition Semaphore.h:25
An abstraction for a time stamp and/or sequence number.
Definition Stamp.h:21
Typed image class.
Definition Image.h:603
std::string deviceId
Definition V4L_camera.h:72
unsigned char * read_image
Definition V4L_camera.h:97
unsigned int n_buffers
Definition V4L_camera.h:115
size_t pixelType
Definition V4L_camera.h:120
struct buffer * buffers
Definition V4L_camera.h:116
__u32 user_height
Definition V4L_camera.h:79
unsigned char * raw_image
Definition V4L_camera.h:92
io_method io
Definition V4L_camera.h:86
unsigned char * dst_image_rgb
Definition V4L_camera.h:106
std::vector< yarp::dev::CameraConfig > configurations
Definition V4L_camera.h:112
int resizeOffset_x
Definition V4L_camera.h:75
yarp::os::Property intrinsic
Definition V4L_camera.h:83
__u32 user_width
Definition V4L_camera.h:78
double horizontalFov
Definition V4L_camera.h:81
unsigned char * src_image
Definition V4L_camera.h:101
bool addictionalResize
Definition V4L_camera.h:74
cv::Mat outMat
Definition V4L_camera.h:110
supported_cams camModel
Definition V4L_camera.h:121
double verticalFov
Definition V4L_camera.h:82
unsigned int dst_image_size_rgb
Definition V4L_camera.h:107
unsigned int src_image_size
Definition V4L_camera.h:102
unsigned int raw_image_size
Definition V4L_camera.h:93
size_t length
Definition V4L_camera.h:65
void * start
Definition V4L_camera.h:64