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


iri_ladybug2
Author(s): Sergi Hernandez Juan
autogenerated on Fri Dec 6 2013 22:35:11