avt_vimba_api.h
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 #ifndef AVT_VIMBA_API_H
00034 #define AVT_VIMBA_API_H
00035 
00036 #include <VimbaCPP/Include/VimbaCPP.h>
00037 
00038 #include <ros/ros.h>
00039 #include <ros/console.h>
00040 #include <sensor_msgs/Image.h>
00041 #include <sensor_msgs/image_encodings.h>
00042 #include <sensor_msgs/fill_image.h>
00043 
00044 #include <string>
00045 #include <map>
00046 
00047 using AVT::VmbAPI::VimbaSystem;
00048 using AVT::VmbAPI::CameraPtr;
00049 using AVT::VmbAPI::FramePtr;
00050 
00051 namespace avt_vimba_camera {
00052 class AvtVimbaApi {
00053  public:
00054   AvtVimbaApi(void) : vs(VimbaSystem::GetInstance()) {}
00055 
00056   void start() {
00057     VmbErrorType err = vs.Startup();
00058     if (VmbErrorSuccess == err) {
00059       ROS_INFO_STREAM("[Vimba System]: AVT Vimba System initialized successfully");
00060       listAvailableCameras();
00061     } else {
00062       ROS_ERROR_STREAM("[Vimba System]: Could not start Vimba system: "
00063         << errorCodeToMessage(err));
00064     }
00065   }
00066 
00073   std::string errorCodeToMessage(VmbErrorType error) {
00074     std::map<VmbErrorType, std::string> error_msg;
00075     error_msg[VmbErrorSuccess]        = "Success.";
00076     error_msg[VmbErrorApiNotStarted]  = "API not started.";
00077     error_msg[VmbErrorNotFound]       = "Not found.";
00078     error_msg[VmbErrorBadHandle]      = "Invalid handle ";
00079     error_msg[VmbErrorDeviceNotOpen]  = "Device not open.";
00080     error_msg[VmbErrorInvalidAccess]  = "Invalid access.";
00081     error_msg[VmbErrorBadParameter]   = "Bad parameter.";
00082     error_msg[VmbErrorStructSize]     = "Wrong DLL version.";
00083     error_msg[VmbErrorWrongType]      = "Wrong type.";
00084     error_msg[VmbErrorInvalidValue]   = "Invalid value.";
00085     error_msg[VmbErrorTimeout]        = "Timeout.";
00086     error_msg[VmbErrorOther]          = "TL error.";
00087     error_msg[VmbErrorInvalidCall]    = "Invalid call.";
00088     error_msg[VmbErrorNoTL]           = "TL not loaded.";
00089     error_msg[VmbErrorNotImplemented] = "Not implemented.";
00090     error_msg[VmbErrorNotSupported]   = "Not supported.";
00091     error_msg[VmbErrorResources]      = "Resource not available.";
00092     error_msg[VmbErrorInternalFault]  = "Unexpected fault in VmbApi or driver.";
00093     error_msg[VmbErrorMoreData]       = "More data returned than memory provided.";
00094 
00095     std::map<VmbErrorType, std::string>::const_iterator iter =
00096       error_msg.find(error);
00097     if ( error_msg.end() != iter ) {
00098       return iter->second;
00099     }
00100     return "Unsupported error code passed.";
00101   }
00102 
00103   bool frameToImage(const FramePtr vimba_frame_ptr, sensor_msgs::Image& image) {
00104     VmbPixelFormatType pixel_format;
00105     VmbUint32_t width, height, nSize;
00106 
00107     vimba_frame_ptr->GetWidth(width);
00108     vimba_frame_ptr->GetHeight(height);
00109     vimba_frame_ptr->GetPixelFormat(pixel_format);
00110     vimba_frame_ptr->GetImageSize(nSize);
00111 
00112     VmbUint32_t step = nSize / height;
00113 
00114     // NOTE: YUV and ARGB formats not supported
00115     std::string encoding;
00116     if      (pixel_format == VmbPixelFormatMono8          ) encoding = sensor_msgs::image_encodings::MONO8;
00117     else if (pixel_format == VmbPixelFormatMono10         ) encoding = sensor_msgs::image_encodings::MONO16;
00118     else if (pixel_format == VmbPixelFormatMono12         ) encoding = sensor_msgs::image_encodings::MONO16;
00119     else if (pixel_format == VmbPixelFormatMono12Packed   ) encoding = sensor_msgs::image_encodings::MONO16;
00120     else if (pixel_format == VmbPixelFormatMono14         ) encoding = sensor_msgs::image_encodings::MONO16;
00121     else if (pixel_format == VmbPixelFormatMono16         ) encoding = sensor_msgs::image_encodings::MONO16;
00122     else if (pixel_format == VmbPixelFormatBayerGR8       ) encoding = sensor_msgs::image_encodings::BAYER_GRBG8;
00123     else if (pixel_format == VmbPixelFormatBayerRG8       ) encoding = sensor_msgs::image_encodings::BAYER_RGGB8;
00124     else if (pixel_format == VmbPixelFormatBayerGB8       ) encoding = sensor_msgs::image_encodings::BAYER_GBRG8;
00125     else if (pixel_format == VmbPixelFormatBayerBG8       ) encoding = sensor_msgs::image_encodings::BAYER_BGGR8;
00126     else if (pixel_format == VmbPixelFormatBayerGR10      ) encoding = sensor_msgs::image_encodings::TYPE_16SC1;
00127     else if (pixel_format == VmbPixelFormatBayerRG10      ) encoding = sensor_msgs::image_encodings::TYPE_16SC1;
00128     else if (pixel_format == VmbPixelFormatBayerGB10      ) encoding = sensor_msgs::image_encodings::TYPE_16SC1;
00129     else if (pixel_format == VmbPixelFormatBayerBG10      ) encoding = sensor_msgs::image_encodings::TYPE_16SC1;
00130     else if (pixel_format == VmbPixelFormatBayerGR12      ) encoding = sensor_msgs::image_encodings::TYPE_16SC1;
00131     else if (pixel_format == VmbPixelFormatBayerRG12      ) encoding = sensor_msgs::image_encodings::TYPE_16SC1;
00132     else if (pixel_format == VmbPixelFormatBayerGB12      ) encoding = sensor_msgs::image_encodings::TYPE_16SC1;
00133     else if (pixel_format == VmbPixelFormatBayerBG12      ) encoding = sensor_msgs::image_encodings::TYPE_16SC1;
00134     else if (pixel_format == VmbPixelFormatBayerGR12Packed) encoding = sensor_msgs::image_encodings::TYPE_32SC4;
00135     else if (pixel_format == VmbPixelFormatBayerRG12Packed) encoding = sensor_msgs::image_encodings::TYPE_32SC4;
00136     else if (pixel_format == VmbPixelFormatBayerGB12Packed) encoding = sensor_msgs::image_encodings::TYPE_32SC4;
00137     else if (pixel_format == VmbPixelFormatBayerBG12Packed) encoding = sensor_msgs::image_encodings::TYPE_32SC4;
00138     else if (pixel_format == VmbPixelFormatBayerGR16      ) encoding = sensor_msgs::image_encodings::TYPE_16SC1;
00139     else if (pixel_format == VmbPixelFormatBayerRG16      ) encoding = sensor_msgs::image_encodings::TYPE_16SC1;
00140     else if (pixel_format == VmbPixelFormatBayerGB16      ) encoding = sensor_msgs::image_encodings::TYPE_16SC1;
00141     else if (pixel_format == VmbPixelFormatBayerBG16      ) encoding = sensor_msgs::image_encodings::TYPE_16SC1;
00142     else if (pixel_format == VmbPixelFormatRgb8           ) encoding = sensor_msgs::image_encodings::RGB8;
00143     else if (pixel_format == VmbPixelFormatBgr8           ) encoding = sensor_msgs::image_encodings::BGR8;
00144     else if (pixel_format == VmbPixelFormatRgba8          ) encoding = sensor_msgs::image_encodings::RGBA8;
00145     else if (pixel_format == VmbPixelFormatBgra8          ) encoding = sensor_msgs::image_encodings::BGRA8;
00146     else if (pixel_format == VmbPixelFormatRgb12          ) encoding = sensor_msgs::image_encodings::TYPE_16UC3;
00147     else if (pixel_format == VmbPixelFormatRgb16          ) encoding = sensor_msgs::image_encodings::TYPE_16UC3;
00148     else
00149       ROS_WARN("Received frame with unsupported pixel format %d", pixel_format);
00150     if (encoding == "") return false;
00151 
00152     VmbUchar_t *buffer_ptr;
00153     VmbErrorType err = vimba_frame_ptr->GetImage(buffer_ptr);
00154     bool res = false;
00155     if ( VmbErrorSuccess == err ) {
00156       res = sensor_msgs::fillImage(image,
00157                                    encoding,
00158                                    height,
00159                                    width,
00160                                    step,
00161                                    buffer_ptr);
00162     } else {
00163       ROS_ERROR_STREAM("[" << ros::this_node::getName()
00164         << "]: Could not GetImage. "
00165         << "\n Error: " << errorCodeToMessage(err));
00166     }
00167     return res;
00168   }
00169 
00170  private:
00171   VimbaSystem& vs;
00172 
00173   void listAvailableCameras(void) {
00174     std::string name;
00175     CameraPtrVector cameras;
00176     if (VmbErrorSuccess == vs.Startup()) {
00177       if (VmbErrorSuccess == vs.GetCameras(cameras)) {
00178         for (CameraPtrVector::iterator iter = cameras.begin();
00179         cameras.end() != iter;
00180         ++iter) {
00181           if (VmbErrorSuccess == (*iter)->GetName(name)) {
00182             ROS_DEBUG_STREAM("[" << ros::this_node::getName() << "]: Found camera: ");
00183           }
00184           std::string strID;            // The ID of the cam
00185           std::string strName;          // The name of the cam
00186           std::string strModelname;     // The model name of the cam
00187           std::string strSerialNumber;  // The serial number of the cam
00188           std::string strInterfaceID;  // The ID of the interface the cam is connected to
00189           VmbErrorType err = (*iter)->GetID( strID );
00190           if ( VmbErrorSuccess != err )
00191           {
00192               ROS_ERROR_STREAM("[Could not get camera ID. Error code: " << err << "]");
00193           }
00194           err = (*iter)->GetName( strName );
00195           if ( VmbErrorSuccess != err )
00196           {
00197               ROS_ERROR_STREAM("[Could not get camera name. Error code: " << err << "]");
00198           }
00199 
00200           err = (*iter)->GetModel( strModelname );
00201           if ( VmbErrorSuccess != err )
00202           {
00203               ROS_ERROR_STREAM("[Could not get camera mode name. Error code: " << err << "]");
00204           }
00205 
00206           err = (*iter)->GetSerialNumber( strSerialNumber );
00207           if ( VmbErrorSuccess != err )
00208           {
00209               ROS_ERROR_STREAM("[Could not get camera serial number. Error code: " << err << "]");
00210           }
00211 
00212           err = (*iter)->GetInterfaceID( strInterfaceID );
00213           if ( VmbErrorSuccess != err )
00214           {
00215               ROS_ERROR_STREAM("[Could not get interface ID. Error code: " << err << "]");
00216           }
00217           ROS_DEBUG_STREAM("\t/// Camera Name: " << strName);
00218           ROS_DEBUG_STREAM("\t/// Model Name: " << strModelname);
00219           ROS_DEBUG_STREAM("\t/// Camera ID: " << strID);
00220           ROS_DEBUG_STREAM("\t/// Serial Number: " << strSerialNumber);
00221           ROS_DEBUG_STREAM("\t/// @ Interface ID: " << strInterfaceID);
00222 
00223         }
00224       }
00225     }
00226   }
00227 };
00228 }
00229 #endif


avt_vimba_camera
Author(s): Miquel Massot , Allied Vision Technologies
autogenerated on Thu Jun 6 2019 18:23:39