-
Notifications
You must be signed in to change notification settings - Fork 95
/
camera_driver.cpp
474 lines (402 loc) · 15 KB
/
camera_driver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (C) 2012 Ken Tossell
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the author nor other contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/
#include "libuvc_camera/camera_driver.h"
#include <ros/ros.h>
#include <sensor_msgs/Image.h>
#include <std_msgs/Header.h>
#include <image_transport/camera_publisher.h>
#include <dynamic_reconfigure/server.h>
#include <libuvc/libuvc.h>
#define libuvc_VERSION (libuvc_VERSION_MAJOR * 10000 \
+ libuvc_VERSION_MINOR * 100 \
+ libuvc_VERSION_PATCH)
namespace libuvc_camera {
CameraDriver::CameraDriver(ros::NodeHandle nh, ros::NodeHandle priv_nh)
: nh_(nh), priv_nh_(priv_nh),
state_(kInitial),
ctx_(NULL), dev_(NULL), devh_(NULL), rgb_frame_(NULL),
it_(nh_),
config_server_(mutex_, priv_nh_),
config_changed_(false),
cinfo_manager_(nh) {
cam_pub_ = it_.advertiseCamera("image_raw", 1, false);
}
CameraDriver::~CameraDriver() {
if (rgb_frame_)
uvc_free_frame(rgb_frame_);
if (ctx_)
uvc_exit(ctx_); // Destroys dev_, devh_, etc.
}
bool CameraDriver::Start() {
assert(state_ == kInitial);
uvc_error_t err;
err = uvc_init(&ctx_, NULL);
if (err != UVC_SUCCESS) {
uvc_perror(err, "ERROR: uvc_init");
return false;
}
state_ = kStopped;
config_server_.setCallback(boost::bind(&CameraDriver::ReconfigureCallback, this, _1, _2));
return state_ == kRunning;
}
void CameraDriver::Stop() {
boost::recursive_mutex::scoped_lock lock(mutex_);
assert(state_ != kInitial);
if (state_ == kRunning)
CloseCamera();
assert(state_ == kStopped);
uvc_exit(ctx_);
ctx_ = NULL;
state_ = kInitial;
}
void CameraDriver::ReconfigureCallback(UVCCameraConfig &new_config, uint32_t level) {
boost::recursive_mutex::scoped_lock lock(mutex_);
if ((level & kReconfigureClose) == kReconfigureClose) {
if (state_ == kRunning)
CloseCamera();
}
if (state_ == kStopped) {
OpenCamera(new_config);
}
if (new_config.camera_info_url != config_.camera_info_url)
cinfo_manager_.loadCameraInfo(new_config.camera_info_url);
if (state_ == kRunning) {
#define PARAM_INT(name, fn, value) if (new_config.name != config_.name) { \
int val = (value); \
if (uvc_set_##fn(devh_, val)) { \
ROS_WARN("Unable to set " #name " to %d", val); \
new_config.name = config_.name; \
} \
}
PARAM_INT(scanning_mode, scanning_mode, new_config.scanning_mode);
PARAM_INT(auto_exposure, ae_mode, 1 << new_config.auto_exposure);
PARAM_INT(auto_exposure_priority, ae_priority, new_config.auto_exposure_priority);
PARAM_INT(exposure_absolute, exposure_abs, new_config.exposure_absolute * 10000);
PARAM_INT(auto_focus, focus_auto, new_config.auto_focus ? 1 : 0);
PARAM_INT(focus_absolute, focus_abs, new_config.focus_absolute);
#if libuvc_VERSION > 00005 /* version > 0.0.5 */
PARAM_INT(gain, gain, new_config.gain);
PARAM_INT(iris_absolute, iris_abs, new_config.iris_absolute);
PARAM_INT(brightness, brightness, new_config.brightness);
#endif
if (new_config.pan_absolute != config_.pan_absolute || new_config.tilt_absolute != config_.tilt_absolute) {
if (uvc_set_pantilt_abs(devh_, new_config.pan_absolute, new_config.tilt_absolute)) {
ROS_WARN("Unable to set pantilt to %d, %d", new_config.pan_absolute, new_config.tilt_absolute);
new_config.pan_absolute = config_.pan_absolute;
new_config.tilt_absolute = config_.tilt_absolute;
}
}
// TODO: roll_absolute
// TODO: privacy
// TODO: backlight_compensation
// TODO: contrast
// TODO: power_line_frequency
// TODO: auto_hue
// TODO: saturation
// TODO: sharpness
// TODO: gamma
// TODO: auto_white_balance
// TODO: white_balance_temperature
// TODO: white_balance_BU
// TODO: white_balance_RV
}
config_ = new_config;
}
void CameraDriver::ImageCallback(uvc_frame_t *frame) {
ros::Time timestamp = ros::Time(frame->capture_time.tv_sec, frame->capture_time.tv_usec * 1000);
if ( timestamp == ros::Time(0) ) {
timestamp = ros::Time::now();
}
boost::recursive_mutex::scoped_lock lock(mutex_);
assert(state_ == kRunning);
assert(rgb_frame_);
sensor_msgs::Image::Ptr image(new sensor_msgs::Image());
image->width = config_.width;
image->height = config_.height;
image->step = image->width * 3;
image->data.resize(image->step * image->height);
if (frame->frame_format == UVC_FRAME_FORMAT_BGR){
image->encoding = "bgr8";
memcpy(&(image->data[0]), frame->data, frame->data_bytes);
} else if (frame->frame_format == UVC_FRAME_FORMAT_RGB){
image->encoding = "rgb8";
memcpy(&(image->data[0]), frame->data, frame->data_bytes);
} else if (frame->frame_format == UVC_FRAME_FORMAT_UYVY) {
uvc_error_t conv_ret = uvc_uyvy2bgr(frame, rgb_frame_);
if (conv_ret != UVC_SUCCESS) {
uvc_perror(conv_ret, "Couldn't convert frame to RGB");
return;
}
image->encoding = "bgr8";
memcpy(&(image->data[0]), rgb_frame_->data, rgb_frame_->data_bytes);
} else if (frame->frame_format == UVC_FRAME_FORMAT_GRAY8) {
image->encoding = "8UC1";
image->step = image->width;
image->data.resize(image->step * image->height);
memcpy(&(image->data[0]), frame->data, frame->data_bytes);
} else if (frame->frame_format == UVC_FRAME_FORMAT_GRAY16) {
image->encoding = "16UC1";
image->step = image->width*2;
image->data.resize(image->step * image->height);
memcpy(&(image->data[0]), frame->data, frame->data_bytes);
} else if (frame->frame_format == UVC_FRAME_FORMAT_YUYV) {
// FIXME: uvc_any2bgr does not work on "yuyv" format, so use uvc_yuyv2bgr directly.
uvc_error_t conv_ret = uvc_yuyv2bgr(frame, rgb_frame_);
if (conv_ret != UVC_SUCCESS) {
uvc_perror(conv_ret, "Couldn't convert frame to RGB");
return;
}
image->encoding = "bgr8";
memcpy(&(image->data[0]), rgb_frame_->data, rgb_frame_->data_bytes);
#if libuvc_VERSION > 00005 /* version > 0.0.5 */
} else if (frame->frame_format == UVC_FRAME_FORMAT_MJPEG) {
// Enable mjpeg support despite uvs_any2bgr shortcoming
// https://github.com/ros-drivers/libuvc_ros/commit/7508a09f
uvc_error_t conv_ret = uvc_mjpeg2rgb(frame, rgb_frame_);
if (conv_ret != UVC_SUCCESS) {
uvc_perror(conv_ret, "Couldn't convert frame to RGB");
return;
}
image->encoding = "rgb8";
memcpy(&(image->data[0]), rgb_frame_->data, rgb_frame_->data_bytes);
#endif
} else {
uvc_error_t conv_ret = uvc_any2bgr(frame, rgb_frame_);
if (conv_ret != UVC_SUCCESS) {
uvc_perror(conv_ret, "Couldn't convert frame to RGB");
return;
}
image->encoding = "bgr8";
memcpy(&(image->data[0]), rgb_frame_->data, rgb_frame_->data_bytes);
}
sensor_msgs::CameraInfo::Ptr cinfo(
new sensor_msgs::CameraInfo(cinfo_manager_.getCameraInfo()));
image->header.frame_id = config_.frame_id;
image->header.stamp = timestamp;
cinfo->header.frame_id = config_.frame_id;
cinfo->header.stamp = timestamp;
cam_pub_.publish(image, cinfo);
if (config_changed_) {
config_server_.updateConfig(config_);
config_changed_ = false;
}
}
/* static */ void CameraDriver::ImageCallbackAdapter(uvc_frame_t *frame, void *ptr) {
CameraDriver *driver = static_cast<CameraDriver*>(ptr);
driver->ImageCallback(frame);
}
void CameraDriver::AutoControlsCallback(
enum uvc_status_class status_class,
int event,
int selector,
enum uvc_status_attribute status_attribute,
void *data, size_t data_len) {
boost::recursive_mutex::scoped_lock lock(mutex_);
printf("Controls callback. class: %d, event: %d, selector: %d, attr: %d, data_len: %zu\n",
status_class, event, selector, status_attribute, data_len);
if (status_attribute == UVC_STATUS_ATTRIBUTE_VALUE_CHANGE) {
switch (status_class) {
case UVC_STATUS_CLASS_CONTROL_CAMERA: {
switch (selector) {
case UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL:
uint8_t *data_char = (uint8_t*) data;
uint32_t exposure_int = ((data_char[0]) | (data_char[1] << 8) |
(data_char[2] << 16) | (data_char[3] << 24));
config_.exposure_absolute = exposure_int * 0.0001;
config_changed_ = true;
break;
}
break;
}
case UVC_STATUS_CLASS_CONTROL_PROCESSING: {
switch (selector) {
case UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL:
uint8_t *data_char = (uint8_t*) data;
config_.white_balance_temperature =
data_char[0] | (data_char[1] << 8);
config_changed_ = true;
break;
}
break;
}
}
// config_server_.updateConfig(config_);
}
}
/* static */ void CameraDriver::AutoControlsCallbackAdapter(
enum uvc_status_class status_class,
int event,
int selector,
enum uvc_status_attribute status_attribute,
void *data, size_t data_len,
void *ptr) {
CameraDriver *driver = static_cast<CameraDriver*>(ptr);
driver->AutoControlsCallback(status_class, event, selector,
status_attribute, data, data_len);
}
enum uvc_frame_format CameraDriver::GetVideoMode(std::string vmode){
if(vmode == "uncompressed") {
return UVC_COLOR_FORMAT_UNCOMPRESSED;
} else if (vmode == "compressed") {
return UVC_COLOR_FORMAT_COMPRESSED;
} else if (vmode == "yuyv") {
return UVC_COLOR_FORMAT_YUYV;
} else if (vmode == "uyvy") {
return UVC_COLOR_FORMAT_UYVY;
} else if (vmode == "rgb") {
return UVC_COLOR_FORMAT_RGB;
} else if (vmode == "bgr") {
return UVC_COLOR_FORMAT_BGR;
} else if (vmode == "mjpeg") {
return UVC_COLOR_FORMAT_MJPEG;
} else if (vmode == "gray8") {
return UVC_COLOR_FORMAT_GRAY8;
} else if (vmode == "gray16") {
return UVC_COLOR_FORMAT_GRAY16;
} else {
ROS_ERROR_STREAM("Invalid Video Mode: " << vmode);
ROS_WARN_STREAM("Continue using video mode: uncompressed");
return UVC_COLOR_FORMAT_UNCOMPRESSED;
}
};
void CameraDriver::OpenCamera(UVCCameraConfig &new_config) {
assert(state_ == kStopped);
int vendor_id = strtol(new_config.vendor.c_str(), NULL, 0);
int product_id = strtol(new_config.product.c_str(), NULL, 0);
ROS_INFO("Opening camera with vendor=0x%x, product=0x%x, serial=\"%s\", index=%d",
vendor_id, product_id, new_config.serial.c_str(), new_config.index);
uvc_device_t **devs;
// Implement missing index select behavior
// https://github.com/ros-drivers/libuvc_ros/commit/4f30e9a0
#if libuvc_VERSION > 00005 /* version > 0.0.5 */
uvc_error_t find_err = uvc_find_devices(
ctx_, &devs,
vendor_id,
product_id,
new_config.serial.empty() ? NULL : new_config.serial.c_str());
if (find_err != UVC_SUCCESS) {
uvc_perror(find_err, "uvc_find_device");
return;
}
// select device by index
dev_ = NULL;
int dev_idx = 0;
while (devs[dev_idx] != NULL) {
if(dev_idx == new_config.index) {
dev_ = devs[dev_idx];
}
else {
uvc_unref_device(devs[dev_idx]);
}
dev_idx++;
}
if(dev_ == NULL) {
ROS_ERROR("Unable to find device at index %d", new_config.index);
return;
}
#else
uvc_error_t find_err = uvc_find_device(
ctx_, &dev_,
vendor_id,
product_id,
new_config.serial.empty() ? NULL : new_config.serial.c_str());
if (find_err != UVC_SUCCESS) {
uvc_perror(find_err, "uvc_find_device");
return;
}
#endif
uvc_error_t open_err = uvc_open(dev_, &devh_);
if (open_err != UVC_SUCCESS) {
switch (open_err) {
case UVC_ERROR_ACCESS:
#ifdef __linux__
ROS_ERROR("Permission denied opening /dev/bus/usb/%03d/%03d",
uvc_get_bus_number(dev_), uvc_get_device_address(dev_));
#else
ROS_ERROR("Permission denied opening device %d on bus %d",
uvc_get_device_address(dev_), uvc_get_bus_number(dev_));
#endif
break;
default:
#ifdef __linux__
ROS_ERROR("Can't open /dev/bus/usb/%03d/%03d: %s (%d)",
uvc_get_bus_number(dev_), uvc_get_device_address(dev_),
uvc_strerror(open_err), open_err);
#else
ROS_ERROR("Can't open device %d on bus %d: %s (%d)",
uvc_get_device_address(dev_), uvc_get_bus_number(dev_),
uvc_strerror(open_err), open_err);
#endif
break;
}
uvc_unref_device(dev_);
return;
}
uvc_set_status_callback(devh_, &CameraDriver::AutoControlsCallbackAdapter, this);
uvc_stream_ctrl_t ctrl;
uvc_error_t mode_err = uvc_get_stream_ctrl_format_size(
devh_, &ctrl,
GetVideoMode(new_config.video_mode),
new_config.width, new_config.height,
new_config.frame_rate);
if (mode_err != UVC_SUCCESS) {
uvc_perror(mode_err, "uvc_get_stream_ctrl_format_size");
uvc_close(devh_);
uvc_unref_device(dev_);
ROS_ERROR("check video_mode/width/height/frame_rate are available");
uvc_print_diag(devh_, NULL);
return;
}
uvc_error_t stream_err = uvc_start_streaming(devh_, &ctrl, &CameraDriver::ImageCallbackAdapter, this, 0);
if (stream_err != UVC_SUCCESS) {
uvc_perror(stream_err, "uvc_start_streaming");
uvc_close(devh_);
uvc_unref_device(dev_);
return;
}
if (rgb_frame_)
uvc_free_frame(rgb_frame_);
rgb_frame_ = uvc_allocate_frame(new_config.width * new_config.height * 3);
assert(rgb_frame_);
state_ = kRunning;
}
void CameraDriver::CloseCamera() {
assert(state_ == kRunning);
uvc_close(devh_);
devh_ = NULL;
uvc_unref_device(dev_);
dev_ = NULL;
state_ = kStopped;
}
};