ueyecamera.cpp
Go to the documentation of this file.
00001 #include <uEye.h>
00002 #include <iostream>
00003 #include <boost/format.hpp>
00004 #include <ros/ros.h>
00005 #include <ros/console.h>
00006 #include <image_transport/image_transport.h>
00007 using namespace std;
00008 using boost::format;
00009 
00010 
00011 int main(int argc, char*argv[]){
00012   ros::init(argc, argv, "ueyecamera");
00013   ros::NodeHandle nh("~");
00014   int cameraid;
00015   int img_width=752, img_height=480, img_bpp=8, img_step, img_data_size, rate, img_left, img_top;
00016 
00017   sensor_msgs::Image img_msg;
00018   sensor_msgs::CameraInfo caminfo_msg;
00019 
00020   XmlRpc::XmlRpcValue double_list;
00021 
00022   std::string file_str;
00023 
00024   nh.param<int>("cameraid", cameraid,0);
00025   nh.param<int>("width", img_width,752);
00026   nh.param<int>("height", img_height,480);
00027   nh.param<int>("left", img_left,-1);
00028   nh.param<int>("top", img_top,-1);
00029   nh.param<int>("rate", rate ,10);
00030   nh.param("camera_frame_id", img_msg.header.frame_id, std::string("head_camera"));
00031   nh.param("file_str",file_str,std::string(""));        
00032 
00033   nh.getParam("K", double_list);
00034   if ((double_list.getType() == XmlRpc::XmlRpcValue::TypeArray) &&
00035      (double_list.size() == 9)) {
00036      for (int i=0; i<9; i++) {
00037          ROS_ASSERT(double_list[0].getType() == XmlRpc::XmlRpcValue::TypeDouble);
00038          caminfo_msg.K[i] = double_list[i];
00039      }
00040   }
00041 
00042   nh.getParam("D", double_list);
00043 
00044       if ((double_list.getType() == XmlRpc::XmlRpcValue::TypeArray)) {
00045         caminfo_msg.D.resize(double_list.size());
00046         for (int i=0; i<double_list.size(); i++) {
00047           ROS_ASSERT(double_list[0].getType() == XmlRpc::XmlRpcValue::TypeDouble);
00048           caminfo_msg.D[i] = double_list[i];
00049         }
00050       }
00051 
00052   nh.getParam("R", double_list);
00053 
00054       if ((double_list.getType() == XmlRpc::XmlRpcValue::TypeArray) &&
00055           (double_list.size() == 9)) {
00056         for (int i=0; i<9; i++) {
00057           ROS_ASSERT(double_list[0].getType() == XmlRpc::XmlRpcValue::TypeDouble);
00058           caminfo_msg.R[i] = double_list[i];
00059         }
00060       }
00061 
00062   nh.getParam("P", double_list);
00063 
00064       if ((double_list.getType() == XmlRpc::XmlRpcValue::TypeArray) &&
00065           (double_list.size() == 12)) {
00066         for (int i=0; i<12; i++) {
00067           ROS_ASSERT(double_list[0].getType() == XmlRpc::XmlRpcValue::TypeDouble);
00068           caminfo_msg.P[i] = double_list[i];
00069         }
00070       }
00071 
00072   cerr<<"Trying to open camera: "<<cameraid<<endl
00073       <<"   width: "<<img_width<<endl
00074       <<"  height: "<<img_height<<endl
00075       <<"    rate: "<<rate<<endl;
00076 
00077   image_transport::ImageTransport it(nh);
00078   image_transport::Publisher pub = it.advertise("image", 1);
00079   ros::Publisher pub_info = nh.advertise<sensor_msgs::CameraInfo>("camera_info",1);
00080 
00081   HIDS hCam = cameraid;
00082   char* imgMem;
00083   int memId;
00084   if(is_InitCamera (&hCam, NULL)!= IS_SUCCESS){
00085     ROS_FATAL("Error Initializing camera.");
00086     return 0;
00087   }
00088   
00089   SENSORINFO sensor_info;
00090   is_GetSensorInfo (hCam, &sensor_info);
00091   cerr<<"Sensor info: "<<endl
00092       <<"  model: " <<sensor_info.strSensorName<<endl
00093       <<"  max width: "<<sensor_info.nMaxWidth<<endl
00094       <<"  max height: "<<sensor_info.nMaxHeight<<endl;
00095     
00096   if(img_width< sensor_info.nMaxWidth && img_left == -1){
00097     cerr<<"Left not specified. Assuiming centering the sensor"<<endl;
00098     img_left = (sensor_info.nMaxWidth - img_width)/2;
00099   }
00100   if(img_height< sensor_info.nMaxHeight && img_top == -1){
00101     cerr<<"Top not specified. Assuiming centering the sensor"<<endl;
00102     img_top = (sensor_info.nMaxHeight - img_height)/2;
00103   }
00104 
00105 
00106   is_AllocImageMem(hCam, img_width, img_height, img_bpp, &imgMem, &memId);
00107   is_SetImageMem (hCam, imgMem, memId);
00108 
00109   is_SetDisplayMode (hCam, IS_SET_DM_DIB);
00110   is_SetColorMode (hCam, IS_CM_BGR8_PACKED);
00111   //  is_SetColorMode (hCam, IS_CM_MONO8);
00112   // is_SetImageSize (hCam, img_width, img_height);
00113   IS_RECT rectAOI;
00114   rectAOI.s32X = img_left;
00115   rectAOI.s32Y = img_top;
00116   rectAOI.s32Width = img_width;
00117   rectAOI.s32Height = img_height; 
00118   // DEPRECATED: is_SetAOI(hCam, IS_SET_IMAGE_AOI, &img_left, &img_top, &img_width, &img_height);
00119   is_AOI(hCam, IS_AOI_IMAGE_SET_AOI, (void*)&rectAOI, sizeof(rectAOI));
00120 
00121   double enable = 1;
00122   //enable automatic gain
00123   is_SetAutoParameter (hCam, IS_SET_ENABLE_AUTO_GAIN, &enable, 0);
00124   is_SetAutoParameter (hCam, IS_SET_ENABLE_AUTO_WHITEBALANCE, &enable, 0);
00125   is_SetAutoParameter (hCam, IS_SET_ENABLE_AUTO_FRAMERATE, &enable, 0);
00126   is_SetAutoParameter (hCam, IS_SET_ENABLE_AUTO_SHUTTER, &enable, 0);
00127   
00128   ros::Rate loop_rate(rate);
00129 
00130 
00131   //is_GetImageMem(hCam, &data);
00132   is_GetImageMemPitch(hCam, &img_step);
00133   //img_msg.encoding="bgr8";
00134   img_msg.encoding="mono8";
00135   img_msg.height=img_height;
00136   img_msg.width=img_width;
00137   img_msg.step=img_step;
00138   img_msg.is_bigendian=0;
00139   img_data_size=img_height*img_step;
00140   img_msg.data.resize(img_data_size); 
00141   caminfo_msg.header.frame_id = img_msg.header.frame_id;
00142   caminfo_msg.height = img_height;
00143   caminfo_msg.width = img_width;
00144 
00145   if(!file_str.empty()){
00146         ROS_INFO("loading custom camera parameters:");
00147         ROS_INFO(file_str.c_str());
00148      // DEPRECATED: is_LoadParameters(hCam, file_str.c_str());  
00149         //is_ParameterSet(hCam, IS_PARAMETERSET_CMD_LOAD_FILE, (void *)file_str.c_str(), NULL);
00150         is_ParameterSet(hCam, IS_PARAMETERSET_CMD_LOAD_EEPROM, NULL, NULL);
00151         //is_ParameterSet(hCam, IS_PARAMETERSET_CMD_SAVE_EEPROM, NULL, NULL);
00152 
00153         //int pnCol,pnColMode;
00154         //is_GetColorDepth(hCam, &pnCol, &pnColMode);
00155         //if (pnCol==1) img_msg.encoding="mono8";
00156         //else img_msg.encoding="bgr8";
00157         }
00158 
00159   while (nh.ok()) {
00160 
00161     if(is_FreezeVideo(hCam, IS_WAIT) == IS_SUCCESS){
00162       std::copy((char*) imgMem, ((char*)imgMem) + img_data_size, img_msg.data.begin());
00163       img_msg.header.stamp=ros::Time::now();
00164       caminfo_msg.header.stamp=img_msg.header.stamp;
00165       pub.publish(img_msg);
00166       pub_info.publish(caminfo_msg);
00167 
00168   }
00169     else{
00170       ROS_INFO("Error freezing video");
00171     }
00172 
00173 
00174     ros::spinOnce();
00175     loop_rate.sleep();
00176   }
00177   is_ExitCamera(hCam);
00178   return 0;
00179 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


ueyecamera
Author(s): Yogesh Girdhar
autogenerated on Fri Jan 25 2013 12:33:40