Go to the documentation of this file.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 #include <stdio.h>
00034 #include "flycapture/FlyCapture2.h"
00035
00036 #include <ros/ros.h>
00037 #include <image_transport/image_transport.h>
00038 #include <opencv/cv.h>
00039 #include <opencv/highgui.h>
00040 #include <cv_bridge/CvBridge.h>
00041
00042 using namespace FlyCapture2;
00043
00044 void printBuildInfo ()
00045 {
00046 FC2Version fc2Version;
00047 Utilities::GetLibraryVersion (&fc2Version);
00048 char version[128];
00049
00050 sprintf (version,
00051 "FlyCapture2 library version: %d.%d.%d.%d\n",
00052 fc2Version.major, fc2Version.minor, fc2Version.type, fc2Version.build);
00053
00054 ROS_INFO (version);
00055
00056 char timeStamp[512];
00057 sprintf (timeStamp, "Application build date: %s %s\n\n", __DATE__, __TIME__);
00058
00059 ROS_INFO (timeStamp);
00060 }
00061
00062 void printCameraInfo (CameraInfo * p_cam_info)
00063 {
00064 ROS_INFO ("\n*** CAMERA INFORMATION ***\n"
00065 "Serial number - %u\n"
00066 "Camera model - %s\n"
00067 "Camera vendor - %s\n"
00068 "Sensor - %s\n"
00069 "Resolution - %s\n"
00070 "Firmware version - %s\n"
00071 "Firmware build time - %s\n\n",
00072 p_cam_info->serialNumber,
00073 p_cam_info->modelName,
00074 p_cam_info->vendorName,
00075 p_cam_info->sensorInfo,
00076 p_cam_info->sensorResolution, p_cam_info->firmwareVersion, p_cam_info->firmwareBuildTime);
00077 }
00078
00079 void printError (Error error)
00080 {
00081 error.PrintErrorTrace ();
00082 }
00083
00084 int runSingleCamera (PGRGuid guid, VideoMode video_mode, FrameRate framerate)
00085 {
00086 Error error;
00087 Camera cam;
00088
00089
00090 error = cam.Connect (&guid);
00091
00092 if (error != PGRERROR_OK)
00093 {
00094 printError (error);
00095 return -1;
00096 }
00097 else
00098 ROS_INFO ("camera is connected!");
00099
00100
00101 error = cam.SetVideoModeAndFrameRate (video_mode, framerate);
00102 if (error != PGRERROR_OK)
00103 {
00104 printError (error);
00105
00106 return -1;
00107 }
00108 else
00109 {
00110 ROS_INFO ("set the camera OK !");
00111 }
00112
00113 CameraInfo cam_info;
00114 error = cam.GetCameraInfo (&cam_info);
00115 if (error != PGRERROR_OK)
00116 {
00117 printError (error);
00118
00119 return -1;
00120 }
00121
00122 printCameraInfo (&cam_info);
00123
00124
00125 error = cam.StartCapture ();
00126 if (error != PGRERROR_OK)
00127 {
00128 printError (error);
00129
00130 return -1;
00131 }
00132
00133 Image raw_image;
00134 while (cam.RetrieveBuffer (&raw_image) != PGRERROR_OK)
00135 {
00136 continue;
00137 }
00138 ROS_INFO ("Grabbed one image ");
00139
00140
00141 unsigned int n_cols = raw_image.GetCols ();
00142 unsigned int n_rows = raw_image.GetRows ();
00143 ROS_INFO ("Camera width is : %d", n_cols);
00144 ROS_INFO ("Camera height is : %d", n_rows);
00145
00146
00147 ros::NodeHandle nh;
00148 image_transport::ImageTransport it (nh);
00149 image_transport::Publisher pub = it.advertise ("/pgr_camera/image", 1);
00150 sensor_msgs::ImagePtr msg;
00151
00152
00153 Image converted_image;
00154 IplImage *video_frame = NULL;
00155 unsigned char *cv_image_data = NULL;
00156
00157 ros::Rate loop_rate (10);
00158 unsigned char *p_img_raw = new unsigned char[n_cols * n_rows];
00159 unsigned char *p_img_color = new unsigned char[n_cols * n_rows * 3];
00160 while (nh.ok ())
00161 {
00162
00163 error = cam.RetrieveBuffer (&raw_image);
00164 if (error != PGRERROR_OK)
00165 {
00166 printError (error);
00167 continue;
00168 }
00169
00170 if (video_mode == VIDEOMODE_640x480Y8)
00171 {
00172 error = raw_image.Convert (PIXEL_FORMAT_MONO8, &converted_image);
00173 if (error != PGRERROR_OK)
00174 {
00175 printError (error);
00176
00177 return -1;
00178 }
00179
00180 video_frame = cvCreateImage (cvSize (n_cols, n_rows), IPL_DEPTH_8U, 1);
00181 cv_image_data = (unsigned char *) video_frame->imageData;
00182 p_img_raw = converted_image.GetData ();
00183 for (size_t i = 0; i < n_cols * n_rows; ++i)
00184 {
00185 cv_image_data[i] = p_img_raw[i];
00186 }
00187
00188 msg = sensor_msgs::CvBridge::cvToImgMsg (video_frame, "mono8");
00189 msg->header.stamp = ros::Time::now ();
00190
00191 }
00192 else
00193 {
00194 error = raw_image.Convert (PIXEL_FORMAT_RGB8, &converted_image);
00195 if (error != PGRERROR_OK)
00196 {
00197 printError (error);
00198 continue;
00199 }
00200
00201 video_frame = cvCreateImage (cvSize (n_cols, n_rows), IPL_DEPTH_8U, 3);
00202 cv_image_data = (unsigned char *) video_frame->imageData;
00203 p_img_color = converted_image.GetData ();
00204 for (size_t i = 0; i < n_cols * n_rows * 3; ++i)
00205 {
00206 cv_image_data[i] = p_img_color[i];
00207 }
00208
00209 msg = sensor_msgs::CvBridge::cvToImgMsg (video_frame, "bgr8");
00210 msg->header.stamp = ros::Time::now ();
00211 }
00212
00213 pub.publish (msg);
00214
00215 ros::spin ();
00216 loop_rate.sleep ();
00217 }
00218 if (video_mode == VIDEOMODE_640x480Y8)
00219 {
00220 delete[]p_img_raw;
00221 }
00222 else
00223 delete[]p_img_color;
00224
00225 cvReleaseImage (&video_frame);
00226
00227 error = cam.StopCapture ();
00228 if (error != PGRERROR_OK)
00229 {
00230 printError (error);
00231 return -1;
00232 }
00233
00234
00235 error = cam.Disconnect ();
00236 if (error != PGRERROR_OK)
00237 {
00238 printError (error);
00239
00240 return -1;
00241 }
00242
00243 return 0;
00244 }
00245
00246 int main (int argc, char **argv)
00247 {
00248 ros::init (argc, argv, "image_publisher");
00249
00250 printBuildInfo ();
00251
00252 Error error;
00253
00254 VideoMode m_video_mode;
00255 FrameRate m_framerate;
00256 char *p_video_mode = argv[1];
00257 char *p_framerate = argv[2];
00258
00259 ROS_INFO ("VideoMode is : %s", p_video_mode);
00260 ROS_INFO ("framerate is : %s", p_framerate);
00261
00262 if (strcmp (p_video_mode, "640x480") == 0)
00263 {
00264 m_video_mode = VIDEOMODE_640x480Y8;
00265 }
00266 else if (strcmp (p_video_mode, "1280x960") == 0)
00267 {
00268 m_video_mode = VIDEOMODE_1280x960Y8;
00269 }
00270 if (strcmp (p_framerate, "7.5") == 0)
00271 {
00272 m_framerate = FRAMERATE_7_5;
00273 }
00274 else if (strcmp (p_framerate, "15") == 0)
00275 {
00276 m_framerate = FRAMERATE_15;
00277 }
00278
00279 BusManager bus_mgr;
00280
00281 unsigned int n_camera_num;
00282 error = bus_mgr.GetNumOfCameras (&n_camera_num);
00283 if (error != PGRERROR_OK)
00284 {
00285 printError (error);
00286
00287 return -1;
00288 }
00289
00290 ROS_INFO ("Number of cameras detected: %u", n_camera_num);
00291
00292 for (unsigned int i = 0; i < n_camera_num; i++)
00293 {
00294 PGRGuid guid;
00295 error = bus_mgr.GetCameraFromIndex (i, &guid);
00296
00297 if (error != PGRERROR_OK)
00298 {
00299 printError (error);
00300
00301 return -1;
00302 }
00303
00304 runSingleCamera (guid, m_video_mode, m_framerate);
00305 }
00306
00307 return 0;
00308 }