forked from dgossow/see3cam
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcamera.cpp
168 lines (124 loc) · 4.56 KB
/
camera.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
#include <boost/thread.hpp>
#include <ros/ros.h>
#include <ros/time.h>
#include "uvc_cam/uvc_cam.h"
#include "sensor_msgs/Image.h"
#include "sensor_msgs/image_encodings.h"
#include "sensor_msgs/CameraInfo.h"
#include "camera_info_manager/camera_info_manager.h"
#include "image_transport/image_transport.h"
#include "std_msgs/Float64.h"
#include "uvc_camera/camera.h"
using namespace sensor_msgs;
namespace uvc_camera {
Camera::Camera(ros::NodeHandle _comm_nh, ros::NodeHandle _param_nh) :
node(_comm_nh), pnode(_param_nh), it(_comm_nh),
info_mgr(_comm_nh, "camera"), cam(0) {
/* default config values */
width = 640;
height = 480;
fps = 10;
skip_frames = 0;
frames_to_skip = 0;
device = "/dev/video0";
frame = "camera";
rotate = false;
/* set up information manager */
std::string url;
pnode.getParam("camera_info_url", url);
info_mgr.loadCameraInfo(url);
/* pull other configuration */
pnode.getParam("device", device);
pnode.getParam("fps", fps);
pnode.getParam("skip_frames", skip_frames);
pnode.getParam("width", width);
pnode.getParam("height", height);
pnode.getParam("frame_id", frame);
/* advertise image streams and info streams */
pub = it.advertise("image_raw", 1);
info_pub = node.advertise<CameraInfo>("camera_info", 1);
exposure_pub = node.advertise<std_msgs::Float64>("exposure", 1, true);
/* initialize the cameras */
cam = new uvc_cam::Cam(device.c_str(), uvc_cam::Cam::MODE_YUYV, width, height, fps);
//cam->set_motion_thresholds(100, -1);
cam->set_control(0x009a0901, 0); // exposure, auto (0 = auto, 1 = manual)
cam->set_control(0x00980900, 0); // brightness
// cam->set_control(0x9a0902, 78); // exposure time 15.6ms
std_msgs::Float64 exposure_msg;
exposure_msg.data=7.8 * 0.5;
exposure_pub.publish( exposure_msg );
/* and turn on the streamer */
ok = true;
image_thread = boost::thread(boost::bind(&Camera::feedImages, this));
std::string time_topic;
pnode.getParam("time_topic", time_topic);
time_sub = node.subscribe(time_topic, 1, &Camera::timeCb, this );
}
void Camera::sendInfo(ImagePtr &image, ros::Time time) {
CameraInfoPtr info(new CameraInfo(info_mgr.getCameraInfo()));
/* Throw out any CamInfo that's not calibrated to this camera mode */
if (info->K[0] != 0.0 &&
(image->width != info->width
|| image->height != info->height)) {
info.reset(new CameraInfo());
}
/* If we don't have a calibration, set the image dimensions */
if (info->K[0] == 0.0) {
info->width = image->width;
info->height = image->height;
}
info->header.stamp = time;
info->header.frame_id = frame;
info_pub.publish(info);
}
void Camera::timeCb(std_msgs::Time time)
{
time_mutex_.lock();
last_time = time.data;
//ROS_INFO_STREAM("Next timestamp: " << last_time);
time_mutex_.unlock();
}
void Camera::feedImages() {
unsigned int pair_id = 0;
while (ok) {
unsigned char *img_frame = NULL;
uint32_t bytes_used;
int idx = cam->grab(&img_frame, bytes_used);
time_mutex_.lock();
ros::Time capture_time = last_time;
time_mutex_.unlock();
// ros::Time capture_time = last_time;
/* Read in every frame the camera generates, but only send each
* (skip_frames + 1)th frame. It's set up this way just because
* this is based on Stereo...
*/
if (skip_frames == 0 || frames_to_skip == 0) {
if (img_frame) {
ImagePtr image(new Image);
image->height = height;
image->width = width;
image->step = width * 2;
image->encoding = image_encodings::YUV422;
image->header.stamp = capture_time;
image->header.seq = pair_id;
image->header.frame_id = frame;
image->data.resize(image->step * image->height);
memcpy(&image->data[0], img_frame, image->data.size());
pub.publish(image);
sendInfo(image, capture_time);
//ROS_INFO_STREAM("capture time: " << capture_time);
++pair_id;
}
frames_to_skip = skip_frames;
} else {
frames_to_skip--;
}
if (img_frame) cam->release(idx);
}
}
Camera::~Camera() {
ok = false;
image_thread.join();
if (cam) delete cam;
}
};