capture_image.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 "sick_laser/capture_image.h"
00034 
00035 using namespace FlyCapture2;
00036 
00037 CameraCapture::CameraCapture ()
00038 {
00039   m_videomode_ = VIDEOMODE_1280x960Y8;
00040   m_framerate_ = FRAMERATE_15;
00041   m_wfile_.open ("imagetimestamp.txt");
00042   if (m_wfile_.is_open ())
00043   {
00044     ROS_INFO ("open the file ok!");
00045   }
00046   else
00047   {
00048     ROS_INFO ("open the file fail!");
00049     
00050     return;
00051   }
00052 }
00053 
00054 CameraCapture::~CameraCapture ()
00055 {
00056   //m_wfile_.close();
00057 }
00058 
00059 void CameraCapture::camPrintBuildInfo ()
00060 {
00061   FC2Version fc2Version;        // FC2Version is a struct of  current version of the library
00062   Utilities::GetLibraryVersion (&fc2Version);   //Get library version.
00063   char version[128];
00064 
00065   sprintf (version,
00066            "FlyCapture2 library version: %d.%d.%d.%d",
00067            fc2Version.major, fc2Version.minor, fc2Version.type, fc2Version.build);
00068 
00069   ROS_INFO ("%s", version);
00070 
00071   char timeStamp[512];
00072   sprintf (timeStamp, "Application build date: %s %s", __DATE__, __TIME__);     //get time stamp
00073 
00074   ROS_INFO ("%s", timeStamp);
00075 }
00076 
00077 void CameraCapture::camPrintCameraInfo (CameraInfo * cam_info)
00078 {
00079   ROS_INFO ("\n*** CAMERA INFORMATION ***\n"
00080             "Serial number - %u\n"
00081             "Camera model - %s\n"
00082             "Camera vendor - %s\n"
00083             "Sensor - %s\n"
00084             "Resolution - %s\n"
00085             "Firmware version - %s\n"
00086             "Firmware build time - %s\n\n",
00087             cam_info->serialNumber,
00088             cam_info->modelName,
00089             cam_info->vendorName,
00090             cam_info->sensorInfo, cam_info->sensorResolution, cam_info->firmwareVersion, cam_info->firmwareBuildTime);
00091 }
00092 
00093 void CameraCapture::camPrintError (Error error)
00094 {
00095   error.PrintErrorTrace ();
00096 }
00097 
00098 void CameraCapture::camConnect ()
00099 {
00100   Error error;
00101   error = m_bus_mgr_.GetCameraFromIndex (0, &m_guid_);  //Gets the PGRGuid for a camera on the PC
00102 
00103   if (error != PGRERROR_OK)
00104   {
00105     camPrintError (error);
00106     
00107     return;
00108   }
00109 
00110   error = m_cam_.Connect (&m_guid_);
00111   if (error != PGRERROR_OK)
00112   {
00113     camPrintError (error);
00114     
00115     return;
00116   }
00117   else
00118     ROS_INFO ("camera is connected!");
00119 
00120   //set the video mode and framerate of a camera
00121   error = m_cam_.SetVideoModeAndFrameRate (m_videomode_, m_framerate_);
00122   if (error != PGRERROR_OK)
00123   {
00124     camPrintError (error);
00125     
00126     return;
00127   }
00128   else
00129   {
00130     ROS_INFO ("set the camera OK !");
00131   }
00132 
00133   // Get the camera information
00134   CameraInfo camInfo;           //CameraInfo is a struct of a camera's information 
00135   error = m_cam_.GetCameraInfo (&camInfo);      //Retrieves information from the camera such as serial number, model name and other camera information
00136   if (error != PGRERROR_OK)
00137   {
00138     camPrintError (error);
00139     
00140     return;
00141   }
00142 
00143   camPrintCameraInfo (&camInfo);
00144 
00145   error = m_cam_.StartCapture ();
00146   if (error != PGRERROR_OK)
00147   {
00148     camPrintError (error);
00149     
00150     return;
00151   }
00152 
00153 }
00154 
00155 void CameraCapture::camImageCapture (int img_num)
00156 {
00157   Error error;
00158   Image rawImage;
00159   while (m_cam_.RetrieveBuffer (&rawImage) != PGRERROR_OK)
00160   {
00161     //get the image,rawImage store the image data
00162     continue;
00163   }
00164 
00165   //save the timestamp
00166   double time_now = ros::Time::now ().toSec ();
00167   char buf[50];
00168   bzero (buf, sizeof (buf));
00169   sprintf (buf, "%lf", time_now);
00170   m_wfile_ << buf << "\n";
00171 
00172   // Create a converted image
00173   Image convertedImage;
00174   error = rawImage.Convert (PIXEL_FORMAT_BGR, &convertedImage);
00175   if (error != PGRERROR_OK)
00176   {
00177     camPrintError (error);
00178     
00179     return;
00180   }
00181 
00182   // Create a unique filename
00183   char filename[512];
00184   sprintf (filename, "image%d.bmp", img_num);
00185 
00186   // Save the image. If a file format is not passed in, then the file
00187   // extension is parsed to attempt to determine the file format.
00188   error = convertedImage.Save (filename);
00189   if (error != PGRERROR_OK)
00190   {
00191     camPrintError (error);
00192     
00193     return;
00194   }
00195   else
00196   {
00197     ROS_INFO ("Have saved %d pictures", img_num + 1);
00198   }
00199 }
00200 
00201 void CameraCapture::camStopCapture ()
00202 {
00203   // Stop capturing images
00204   Error error;
00205   error = m_cam_.StopCapture ();
00206   if (error != PGRERROR_OK)
00207   {
00208     camPrintError (error);
00209     
00210     return;
00211   }
00212 
00213   // Disconnect the camera
00214   error = m_cam_.Disconnect ();
00215   if (error != PGRERROR_OK)
00216   {
00217     camPrintError (error);
00218     
00219     return;
00220   }
00221 
00222   m_wfile_.close ();
00223 }


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