pgr_camera.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 *  Software License Agreement (BSD License)
00003 *  Copyright (c) 2013, Intelligent Robotics Lab, DLUT.
00004 *  All rights reserved.
00005 *  Author:Zhao Cilang,Yan Fei,Zhuang Yan.
00006 *  Redistribution and use in source and binary forms, with or without
00007 *  modification, are permitted provided that the following conditions
00008 *  are met:
00009 *
00010 *   * Redistributions of source code must retain the above copyright
00011 *     notice, this list of conditions and the following disclaimer.
00012 *   * Redistributions in binary form must reproduce the above
00013 *     copyright notice, this list of conditions and the following
00014 *     disclaimer in the documentation and/or other materials provided
00015 *     with the distribution.
00016 *   * Neither the name of the Intelligent Robotics Lab nor the names of its
00017 *     contributors may be used to endorse or promote products derived
00018 *     from this software without specific prior written permission.
00019 *
00020 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 *  POSSIBILITY OF SUCH DAMAGE.
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 ()          //print the version information
00045 {
00046   FC2Version fc2Version;
00047   Utilities::GetLibraryVersion (&fc2Version);   //Get library version.
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__); //get time stamp
00058 
00059   ROS_INFO (timeStamp);
00060 }
00061 
00062 void printCameraInfo (CameraInfo * p_cam_info)  //print the information of camera
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 ();     //Print a formatted log trace to stderr
00082 }
00083 
00084 int runSingleCamera (PGRGuid guid, VideoMode video_mode, FrameRate framerate)   //run one single camera,and get the image data 
00085 {
00086   Error error;
00087   Camera cam;
00088 
00089   // Connect to a camera
00090   error = cam.Connect (&guid);  //Connects the camera object to the camera specified by the 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   //set the video mode and framerate of a camera
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   // Get the camera information
00113   CameraInfo cam_info;
00114   error = cam.GetCameraInfo (&cam_info);        //Retrieves information from the camera such as serial number, model name and other camera information
00115   if (error != PGRERROR_OK)
00116   {
00117     printError (error);
00118     
00119     return -1;
00120   }
00121 
00122   printCameraInfo (&cam_info);
00123 
00124   // Start capturing images
00125   error = cam.StartCapture ();
00126   if (error != PGRERROR_OK)
00127   {
00128     printError (error);
00129     
00130     return -1;
00131   }
00132 
00133   Image raw_image;              //get the image,raw_image store the image data
00134   while (cam.RetrieveBuffer (&raw_image) != PGRERROR_OK)
00135   {
00136     continue;
00137   }
00138   ROS_INFO ("Grabbed one image ");
00139 
00140   //Get the column and row of one image
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   //create a publisher named "/pgr_camera/image"
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   // Create a converted image
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     // Retrieve an image
00163     error = cam.RetrieveBuffer (&raw_image);
00164     if (error != PGRERROR_OK)
00165     {
00166       printError (error);
00167       continue;
00168     }
00169     // Convert the raw image to the image which you want
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       //define the opencv picture
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");   //convert the cv::mat to ros_image message
00189       msg->header.stamp = ros::Time::now ();
00190 
00191     }
00192     else
00193     {
00194       error = raw_image.Convert (PIXEL_FORMAT_RGB8, &converted_image);  //get the corlor picture
00195       if (error != PGRERROR_OK)
00196       {
00197         printError (error);
00198         continue;
00199       }
00200       //define the opencv picture
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);          //publish the message
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   // Stop capturing images
00227   error = cam.StopCapture ();
00228   if (error != PGRERROR_OK)
00229   {
00230     printError (error);
00231     return -1;
00232   }
00233 
00234   // Disconnect the camera
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]; //the parameter is VideoMode that we choise
00257   char *p_framerate = argv[2];  //the  parameter is framerate that we choise
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);      //Gets the number of cameras attached to the PC
00283   if (error != PGRERROR_OK)
00284   {
00285     printError (error);         //print the 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);      //Gets the PGRGuid for a camera on the PC
00296 
00297     if (error != PGRERROR_OK)
00298     {
00299       printError (error);
00300       
00301       return -1;
00302     }
00303 
00304     runSingleCamera (guid, m_video_mode, m_framerate);  //run the camera
00305   }
00306 
00307   return 0;
00308 }


dlut_vision
Author(s): Zhao Cilang,Yan Fei,Zhuang Yan/zhuang@dlut.edu.cn
autogenerated on Sun Jan 5 2014 11:05:05