00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <cstdio>
00036 #include <ros/ros.h>
00037 #include <ros/time.h>
00038 #include "uvc_cam/uvc_cam.h"
00039 #include "sensor_msgs/Image.h"
00040 #include "sensor_msgs/image_encodings.h"
00041
00042 #include <image_transport/image_transport.h>
00043 #include <opencv/cvwimage.h>
00044 #include <opencv/highgui.h>
00045 #include <cv_bridge/CvBridge.h>
00046
00047 int main(int argc, char **argv)
00048 {
00049 ros::init(argc, argv, "uvc_cam");
00050 ros::NodeHandle n;
00051 ros::NodeHandle n_private("~");
00052
00053 std::string deviceL, deviceR;
00054 std::string stereoName, imageName;
00055 n_private.param<std::string>("deviceL", deviceL, "/dev/video0");
00056 n_private.param<std::string>("deviceR", deviceR, "/dev/video1");
00057 n_private.param<std::string>("stereoName", stereoName, "/my_stereo");
00058 n_private.param<std::string>("imageName", imageName, "image_raw");
00059 int width, height, fps, modetect_lum, modetect_count;
00060 n_private.param("width", width, 640);
00061 n_private.param("height", height, 480);
00062 n_private.param("fps", fps, 30);
00063 n_private.param("motion_threshold_luminance", modetect_lum, 100);
00064 n_private.param("motion_threshold_count", modetect_count, -1);
00065
00066 std::string left = stereoName + "/left/" + imageName;
00067 std::string right = stereoName + "/right/" + imageName;
00068 image_transport::ImageTransport itL(n);
00069 image_transport::Publisher pubL = itL.advertise(left.c_str(), 1);
00070 image_transport::ImageTransport itR(n);
00071 image_transport::Publisher pubR = itR.advertise(right.c_str(), 1);
00072
00073
00074 ROS_INFO("opening uvc_cam at %dx%d, %d fps", width, height, fps);
00075 uvc_cam::Cam camL(deviceL.c_str(), uvc_cam::Cam::MODE_RGB, width, height, fps);
00076 uvc_cam::Cam camR(deviceR.c_str(), uvc_cam::Cam::MODE_RGB, width, height, fps);
00077 camL.set_motion_thresholds(modetect_lum, modetect_count);
00078 camR.set_motion_thresholds(modetect_lum, modetect_count);
00079
00080 IplImage *imageIplL = cvCreateImageHeader(cvSize(640,480), 8, 3);
00081 IplImage *imageIplR = cvCreateImageHeader(cvSize(640,480), 8, 3);
00082
00083 ros::Time t_prev(ros::Time::now());
00084 int count = 0, skip_count = 0;
00085 while (n.ok())
00086 {
00087 unsigned char *frameL, *frameR = NULL;
00088 uint32_t bytes_usedL, bytes_usedR;
00089 int buf_idxL = camL.grab(&frameL, bytes_usedL);
00090 int buf_idxR = camR.grab(&frameR, bytes_usedR);
00091 if (count++ % fps == 0)
00092 {
00093 ros::Time t(ros::Time::now());
00094 ros::Duration d(t - t_prev);
00095 ROS_INFO("%.1f fps skip %d", (double)fps / d.toSec(), skip_count);
00096 t_prev = t;
00097 }
00098 if (frameL != NULL && frameR != NULL)
00099 {
00100 imageIplL->imageData = (char *)frameL;
00101 imageIplR->imageData = (char *)frameR;
00102 sensor_msgs::Image::Ptr imageL = sensor_msgs::CvBridge::cvToImgMsg( imageIplL, "bgr8");
00103 sensor_msgs::Image::Ptr imageR = sensor_msgs::CvBridge::cvToImgMsg( imageIplR, "bgr8");
00104
00105
00106
00107 imageL->header.stamp = imageR->header.stamp = ros::Time::now();
00108 imageL->encoding = imageR->encoding = sensor_msgs::image_encodings::RGB8;
00109 imageL->height = imageR->height = height;
00110 imageL->width = imageR->width = width;
00111 imageL->step = imageR->step = 3 * width;
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 pubL.publish(imageL);
00128 pubR.publish(imageR);
00129 camL.release(buf_idxL);
00130 camR.release(buf_idxR);
00131 }
00132 else
00133 skip_count++;
00134 }
00135 return 0;
00136 }
00137