bumblebee2_driver_node.cpp
Go to the documentation of this file.
00001 #include "bumblebee2_driver_node.h"
00002 
00003 Bumblebee2DriverNode::Bumblebee2DriverNode(ros::NodeHandle &nh) : iri_base_driver::IriBaseNodeDriver<Bumblebee2Driver>(nh),
00004                                                                   camera_manager(ros::NodeHandle(this->public_node_handle_))
00005 {
00006   std::vector<std::string> cal_files;
00007 
00008   //init class attributes if necessary
00009   this->loop_rate_ = 1000;//in [Hz]
00010   this->desired_framerate=10;
00011   this->cameras.resize(NUM_CAM);
00012   this->cameras[LEFT].it=NULL;
00013   this->cameras[RIGHT].it=NULL;
00014 
00015   this->cameras[LEFT].it=new image_transport::ImageTransport(this->public_node_handle_);
00016   this->cameras[RIGHT].it=new image_transport::ImageTransport(this->public_node_handle_);
00017 
00018   this->event_server=CEventServer::instance();
00019 
00020   // [init publishers]
00021   this->cameras[LEFT].camera_image_publisher_ = this->cameras[LEFT].it->advertiseCamera("left_image", 1);
00022   this->cameras[RIGHT].camera_image_publisher_ = this->cameras[RIGHT].it->advertiseCamera("right_image", 1);
00023 
00024   cal_files.resize(2);
00025   public_node_handle_.param<std::string>("left_cal_file", cal_files[LEFT], "");
00026   public_node_handle_.param<std::string>("right_cal_file", cal_files[RIGHT], "");
00027   if(this->camera_manager.validateURL(cal_files[LEFT]))
00028   {
00029     this->camera_manager.setCameraName("bumblebee_left");
00030     if(this->camera_manager.loadCameraInfo(cal_files[LEFT]))
00031     {
00032       this->cameras[LEFT].CameraInfo_msg_=this->camera_manager.getCameraInfo();
00033       ROS_INFO("Found calibration file for the left camera: %s",cal_files[LEFT].c_str());
00034     }
00035     else
00036       ROS_INFO("Invalid calibration file for the left camera");
00037   }
00038   else
00039     ROS_INFO("Invalid calibration file for the left camera");
00040 
00041   if(this->camera_manager.validateURL(cal_files[RIGHT]))
00042   {
00043     this->camera_manager.setCameraName("bumblebee_right");
00044     if(this->camera_manager.loadCameraInfo(cal_files[RIGHT]))
00045     {
00046       this->cameras[RIGHT].CameraInfo_msg_=this->camera_manager.getCameraInfo();
00047       ROS_INFO("Found calibration file for the right camera: %s",cal_files[RIGHT].c_str());
00048     }
00049     else
00050       ROS_INFO("Invalid calibration file for the right camera");
00051   }
00052   else
00053     ROS_INFO("Invalid calibration file for the right camera");
00054 
00055   public_node_handle_.param<std::string>("tf_prefix", tf_prefix_, "");
00056 
00057   // [init publishers]
00058   
00059   // [init subscribers]
00060   
00061   // [init services]
00062   
00063   // [init clients]
00064   
00065   // [init action servers]
00066   
00067   // [init action clients]
00068 }
00069 
00070 void Bumblebee2DriverNode::mainNodeThread(void)
00071 {
00072   std::vector<std::string> frame_ids;
00073   std::list<std::string> events;
00074   char *left_image=NULL,*right_image=NULL;
00075   TCameraConfig config;
00076   int depth;
00077 
00078   try{
00079     //lock access to driver if necessary
00080     this->driver_.lock();
00081     if(this->driver_.isRunning())
00082     {
00083       this->new_frame_event=this->driver_.get_new_frame_event();
00084       if(this->new_frame_event!="")
00085       {
00086         events.push_back(this->new_frame_event);
00087         this->event_server->wait_all(events,1000);
00088         this->driver_.get_images(&left_image,&right_image);
00089         this->driver_.get_current_config(&config);
00090         this->driver_.get_frame_ids(frame_ids);
00091         depth=(int)ceil((float)config.depth/8.0);
00092         if(left_image!=NULL && this->cameras[LEFT].camera_image_publisher_.getNumSubscribers()>0)
00093         {
00094           //fill msg structures
00095           this->cameras[LEFT].Image_msg_.width=config.width;
00096           this->cameras[LEFT].Image_msg_.height=config.height;
00097           if(config.coding==RGB)
00098           {
00099             this->cameras[LEFT].Image_msg_.step=config.width*depth;
00100             this->cameras[LEFT].Image_msg_.data=std::vector<unsigned char>(left_image,left_image+config.width*config.height*depth);
00101             this->cameras[LEFT].Image_msg_.encoding="rgb8";
00102           }
00103           else
00104           {
00105             this->cameras[LEFT].Image_msg_.step=config.width*depth;
00106             this->cameras[LEFT].Image_msg_.data=std::vector<unsigned char>(left_image,left_image+config.width*config.height*depth);
00107             this->cameras[LEFT].Image_msg_.encoding="8UC1";
00108           }
00109           this->cameras[LEFT].Image_msg_.header.stamp = ros::Time::now();
00110           this->cameras[LEFT].Image_msg_.header.frame_id = tf_prefix_ + "/" + frame_ids[LEFT];
00111           this->cameras[LEFT].CameraInfo_msg_.header.stamp = this->cameras[LEFT].Image_msg_.header.stamp;
00112           this->cameras[LEFT].CameraInfo_msg_.header.frame_id = tf_prefix_ + "/" + frame_ids[LEFT];
00113           this->cameras[LEFT].camera_image_publisher_.publish(this->cameras[LEFT].Image_msg_,this->cameras[LEFT].CameraInfo_msg_);
00114         }
00115         delete[] left_image;
00116         if(right_image!=NULL && this->cameras[RIGHT].camera_image_publisher_.getNumSubscribers()>0)
00117         {
00118           //fill msg structures
00119           this->cameras[RIGHT].Image_msg_.width=config.width;
00120           this->cameras[RIGHT].Image_msg_.height=config.height;
00121           if(config.coding==RGB)
00122           {
00123             this->cameras[RIGHT].Image_msg_.step=config.width*depth;
00124             this->cameras[RIGHT].Image_msg_.data=std::vector<unsigned char>(right_image,right_image+config.width*config.height*depth);
00125             this->cameras[RIGHT].Image_msg_.encoding="rgb8";
00126           }
00127           else
00128           {
00129             this->cameras[RIGHT].Image_msg_.step=config.width*depth;
00130             this->cameras[RIGHT].Image_msg_.data=std::vector<unsigned char>(right_image,right_image+config.width*config.height*depth);
00131             this->cameras[RIGHT].Image_msg_.encoding="8UC1";
00132           }
00133           this->cameras[RIGHT].Image_msg_.header.stamp = ros::Time::now();
00134           this->cameras[RIGHT].Image_msg_.header.frame_id = tf_prefix_ + "/" + frame_ids[RIGHT];
00135           this->cameras[RIGHT].CameraInfo_msg_.header.stamp = this->cameras[RIGHT].Image_msg_.header.stamp;
00136           this->cameras[RIGHT].CameraInfo_msg_.header.frame_id = tf_prefix_ + "/" + frame_ids[RIGHT];
00137           this->cameras[RIGHT].camera_image_publisher_.publish(this->cameras[RIGHT].Image_msg_,this->cameras[RIGHT].CameraInfo_msg_);
00138         }
00139         delete[] right_image;
00140       }
00141     }
00142     this->driver_.unlock();
00143   }catch(CException &e){
00144     this->driver_.unlock();
00145     ROS_INFO("Impossible to capture frame");
00146   }
00147   //fill srv structure and make request to the server
00148 
00149   //publish messages
00150 }
00151 
00152 /*subscriber callbacks] */
00153 
00154 /*  [service callbacks] */
00155 
00156 /*  [action callbacks] */
00157 
00158 /*  [action requests] */
00159 
00160 void Bumblebee2DriverNode::postNodeOpenHook(void)
00161 {
00162 }
00163 
00164 void Bumblebee2DriverNode::addNodeDiagnostics(void)
00165 {
00166 }
00167 
00168 void Bumblebee2DriverNode::addNodeOpenedTests(void)
00169 {
00170 }
00171 
00172 void Bumblebee2DriverNode::addNodeStoppedTests(void)
00173 {
00174 }
00175 
00176 void Bumblebee2DriverNode::addNodeRunningTests(void)
00177 {
00178 }
00179 
00180 void Bumblebee2DriverNode::reconfigureNodeHook(int level)
00181 {
00182 }
00183 
00184 Bumblebee2DriverNode::~Bumblebee2DriverNode(void)
00185 {
00186   // [free dynamic memory]
00187   if(this->cameras[LEFT].it!=NULL)
00188   {
00189     delete this->cameras[LEFT].it;
00190     this->cameras[LEFT].it=NULL;
00191   }
00192   if(this->cameras[RIGHT].it!=NULL)
00193   {
00194     delete this->cameras[RIGHT].it;
00195     this->cameras[RIGHT].it=NULL;
00196   }
00197 }
00198 
00199 /* main function */
00200 int main(int argc,char *argv[])
00201 {
00202   return driver_base::main<Bumblebee2DriverNode>(argc, argv, "bumblebee2_driver_node");
00203 }


iri_bumblebee2
Author(s): Sergi Hernandez Juan
autogenerated on Fri Dec 6 2013 22:29:51