hokuyo_laser3d_driver_node.cpp
Go to the documentation of this file.
00001 #include "hokuyo_laser3d_driver_node.h"
00002 
00003 HokuyoLaser3dDriverNode::HokuyoLaser3dDriverNode(ros::NodeHandle &nh) : iri_base_driver::IriBaseNodeDriver<HokuyoLaser3dDriver>(nh)
00004 {
00005   //init class attributes if necessary
00006   this->loop_rate_ = 40;//in [Hz]
00007 
00008   //this->evnts.push_back(NEW3DSCAN);       // event 0
00009 
00010   // [init publishers]
00011   this->slice_publisher_ = this->public_node_handle_.advertise<sensor_msgs::PointCloud2>("slice", 100);
00012   this->cloud_publisher_ = this->public_node_handle_.advertise<sensor_msgs::PointCloud2>("cloud", 100);
00013 
00014   // [init subscribers]
00015 
00016   // [init services]
00017   this->get_3d_scan_server_ = this->public_node_handle_.advertiseService("get_3d_scan", &HokuyoLaser3dDriverNode::get_3d_scanCallback, this);
00018   this->do_noncontinuous = false;
00019 
00020   event_server = CEventServer::instance();
00021   // [init clients]
00022 
00023   // [init action servers]
00024 
00025   // [init action clients]
00026 }
00027 
00028 void HokuyoLaser3dDriverNode::mainNodeThread(void)
00029 {
00030   this->driver_.lock();
00031 
00032   PointCloudTimeStamped tall, nuvol;
00033 
00034   if( this->driver_.config_.continuous )
00035   {
00036     try
00037     {
00038       uint party = event_server->wait_first(driver_.getENDList());
00039 
00040       if( party == 1 )
00041       {
00042         if( this->driver_.config_.slices )
00043         {
00044           tall.cloud.reserve(1081);
00045           driver_.getSlice(tall);
00046           createPointCloud2(tall, Slice_msg_);
00047           slice_publisher_.publish(Slice_msg_);
00048         }
00049       }else
00050       {
00051         if( this->driver_.config_.clouds )
00052         {
00053           driver_.getCloud(nuvol);
00054           createPointCloud2(nuvol, Cloud_msg_);
00055           cloud_publisher_.publish(Cloud_msg_);
00056         }
00057       }
00058     }
00059     catch(CException &e)
00060     {
00061       ROS_ERROR("'%s'",e.what().c_str());
00062     }
00063   }
00064 
00065   this->driver_.unlock();
00066 }
00067 
00068 /*  [subscriber callbacks] */
00069 
00070 /*  [service callbacks] */
00071 bool HokuyoLaser3dDriverNode::get_3d_scanCallback(iri_hokuyo_laser3d::Get3DScan::Request &req, iri_hokuyo_laser3d::Get3DScan::Response &res)
00072 {
00073   //lock access to driver if necessary
00074   //this->driver_.lock();
00075 
00076   ROS_INFO("PointCloud Requested");
00077 
00078   if( !this->driver_.config_.continuous )
00079   {
00080     this->do_noncontinuous = true;
00081     res.success = 1;
00082   }else{
00083     res.success = 0;
00084   }
00085   //unlock driver if previously blocked
00086  // this->driver_.unlock();
00087 
00088   return true;
00089 }
00090 
00091 /*  [action callbacks] */
00092 
00093 /*  [action requests] */
00094 
00095 void HokuyoLaser3dDriverNode::postNodeOpenHook(void)
00096 {
00097 }
00098 
00099 void HokuyoLaser3dDriverNode::addNodeDiagnostics(void)
00100 {
00101 }
00102 
00103 void HokuyoLaser3dDriverNode::addNodeOpenedTests(void)
00104 {
00105 }
00106 
00107 void HokuyoLaser3dDriverNode::addNodeStoppedTests(void)
00108 {
00109 }
00110 
00111 void HokuyoLaser3dDriverNode::addNodeRunningTests(void)
00112 {
00113 }
00114 
00115 void HokuyoLaser3dDriverNode::reconfigureNodeHook(int level)
00116 {
00117 }
00118 
00119 HokuyoLaser3dDriverNode::~HokuyoLaser3dDriverNode()
00120 {
00121   //[free dynamic memory]
00122 }
00123 
00124 /* main function */
00125 int main(int argc,char *argv[])
00126 {
00127   return driver_base::main<HokuyoLaser3dDriverNode>(argc,argv,"hokuyo_laser3d_driver_node");
00128 }
00129 
00130 
00131 void HokuyoLaser3dDriverNode::createPointCloud2(const PointCloudTimeStamped & cld, sensor_msgs::PointCloud2 & pointcloud)
00132 {
00133   // Header
00134   pointcloud.header.frame_id = this->driver_.config_.frame_id;
00135   pointcloud.header.stamp = ros::Time::now ();
00136 
00137   // 2D structure of the point cloud. If the cloud is unordered, height is
00138   // 1 and width is the length of the point cloud.
00139   
00140   pointcloud.height = 1;
00141   pointcloud.width = cld.cloud.size();
00142 
00143   pointcloud.fields.resize(5);
00144   pointcloud.fields[0].name = "x";
00145   pointcloud.fields[1].name = "y";
00146   pointcloud.fields[2].name = "z";
00147   pointcloud.fields[3].name = "intensity";
00148 
00149   // Set all the fields types accordingly
00150   int offset = 0;
00151   for (size_t s = 0; s < pointcloud.fields.size(); ++s, offset += sizeof(float))
00152   {
00153     pointcloud.fields[s].offset   = offset;
00154     pointcloud.fields[s].count    = 1;
00155     pointcloud.fields[s].datatype = sensor_msgs::PointField::FLOAT32;
00156   }
00157 
00158   pointcloud.point_step = offset; // size of a point in bytes (sizeof(float)*fields.size())
00159   pointcloud.row_step   = pointcloud.point_step*pointcloud.width;
00160   pointcloud.is_dense   = false;
00161   pointcloud.data.resize(pointcloud.row_step*pointcloud.height);
00162 
00163   for (uint v = 0; v < pointcloud.height; v++)
00164   {
00165     for (uint u = 0; u < pointcloud.width; u++)
00166     {
00167       int index = v * pointcloud.width + u;
00168       float *pstep = (float*)&pointcloud.data[(index) * pointcloud.point_step];
00169       pstep[0] = cld.cloud[index].x;
00170       pstep[1] = cld.cloud[index].y;
00171       pstep[2] = cld.cloud[index].z;
00172       pstep[3] = cld.cloud[index].intensity;
00173     }
00174   }
00175 
00176 }


iri_hokuyo_laser3d
Author(s): Marti Morta, mmorta at iri.upc.edu
autogenerated on Fri Dec 6 2013 21:51:18