hokuyo_laser_driver_node.cpp
Go to the documentation of this file.
00001 #include "hokuyo_laser_driver_node.h"
00002 #include <cmath>
00003 
00004 HokuyoLaserDriverNode::HokuyoLaserDriverNode(ros::NodeHandle &nh) : iri_base_driver::IriBaseNodeDriver<HokuyoLaserDriver>(nh)
00005 {
00006   //init class attributes if necessary
00007   //this->loop_rate_ = 2;//in [Hz]
00008   this->loop_rate_ = 100;//in [Hz]
00009 
00010 
00011   // [init publishers]
00012   this->scan_publisher = this->public_node_handle_.advertise<sensor_msgs::LaserScan>("scan", 100);
00013   if(driver_.config_.is_multiecho){
00014     this->scan_publisher2 = this->public_node_handle_.advertise<sensor_msgs::LaserScan>("scan2", 100);
00015     this->scan_publisher3 = this->public_node_handle_.advertise<sensor_msgs::LaserScan>("scan3", 100);
00016   }
00017 
00018   // [init subscribers]
00019 
00020   // [init services]
00021 
00022   // [init clients]
00023 
00024   // [init action servers]
00025 
00026   // [init action clients]
00027 
00028   event_server_ = CEventServer::instance();
00029 }
00030 
00031 void HokuyoLaserDriverNode::mainNodeThread(void)
00032 {
00033   //lock access to driver if necessary
00034 
00035 
00036   // [fill msg Header if necessary]
00037   //<publisher_name>.header.stamp = ros::Time::now();
00038   //<publisher_name>.header.frame_id = "<publisher_topic_name>";
00039 
00040   // [fill msg structures]
00041   //this->LaserScan_msg.data = my_var;
00042 
00043   int current_event = 0;
00044 
00045   ROS_DEBUG("WAIT FOR SCAN");
00046 
00047   // Get Scan
00048   try
00049   {
00050     current_event = event_server_->wait_first(driver_.evnts,5000);
00051     switch( current_event )
00052       {
00053         default:
00054         case 0:
00055           // new scan
00056           this->driver_.lock();
00057           mutex_scan_.enter();
00058           this->driver_.getScan(last_scan_);
00059           mutex_scan_.exit();
00060           this->driver_.unlock();
00061           ROS_DEBUG("HokuyoLaserDriverNode::mainNodeThread NEW SCAN");
00062           break;
00063         case 1:
00064           ROS_DEBUG("HokuyoLaserDriverNode::mainNodeThread END SCANS");
00065           // end of scans
00066           break;
00067         case 2:
00068           // error
00069           ROS_DEBUG("HokuyoLaserDriverNode::mainNodeThread ERROR");
00070           break;
00071       }
00072   }
00073   catch(CException &e)
00074   {
00075     cout << e.what() << endl;
00076   }
00077 
00078   // Build message
00079   // header
00080 
00081     ROS_DEBUG("BUILD MESSAGE");
00082     mutex_scan_.enter();
00083     ros::Time t_laser( last_scan_.t.seconds(), last_scan_.t.nanoseconds() );
00084     this->LaserScan_msg.header.stamp    = t_laser;                       // [s]
00085     this->driver_.lock();
00086     this->LaserScan_msg.header.frame_id = this->driver_.config_.frame_id;   // string
00087   // configuration
00088     this->LaserScan_msg.angle_min       = this->driver_.config_.angle_min;  // [rad]
00089     this->LaserScan_msg.angle_max       = this->driver_.config_.angle_max;  // [rad]
00090     this->LaserScan_msg.angle_increment = this->driver_.angle_increment;    // [rad]
00091     this->LaserScan_msg.time_increment  = this->driver_.time_increment;     // [s]
00092     this->LaserScan_msg.scan_time       = this->driver_.scan_time;          // [s]
00093     this->LaserScan_msg.range_min       = this->driver_.range_min;          // [m]
00094     this->LaserScan_msg.range_max       = this->driver_.range_max;          // [m]
00095 
00096     is_multiecho_ = driver_.config_.is_multiecho;
00097     this->driver_.unlock();
00098   // ranges
00099     vector<float> franges,franges2,franges3;
00100     franges.reserve(last_scan_.distance.size());
00101     franges2.reserve(last_scan_.distance.size());
00102     franges3.reserve(last_scan_.distance.size());
00103     // Unit conversion: [mm] (int) -> [m] (float)
00104     for(uint i=0;i<last_scan_.distance.size();i++)
00105     {
00106       franges.push_back((float)(last_scan_.distance[i][0])/1000);
00107       // multiecho
00108       if(is_multiecho_){
00109         if(last_scan_.distance[i].size()==2)
00110         {
00111           franges2.push_back((float)(last_scan_.distance[i][1])/1000);
00112           franges3.push_back(franges.back());
00113         }
00114         else if(last_scan_.distance[i].size()==3)
00115         {
00116           franges3.push_back((float)(last_scan_.distance[i][2])/1000);
00117           franges2.push_back(franges.back());
00118         }
00119         else
00120         {
00121           franges2.push_back(franges.back());
00122           franges3.push_back(franges.back());
00123         }
00124       }
00125     }
00126   // intensities
00127     vector<float> fintens;
00128     fintens.reserve(last_scan_.intensity.size());
00129      //vector<float> fintens(last_scan_.intensity.begin(), last_scan_.intensity.end());
00130 //     this->LaserScan_msg.intensities = fintens;                        // relative
00131     for(uint i=0;i<last_scan_.intensity.size();i++)
00132       fintens.push_back((float)(last_scan_.intensity[i][0]));
00133 
00134     this->LaserScan_msg.intensities = fintens;
00135     this->LaserScan_msg.ranges = franges;                        // [m]
00136     mutex_scan_.exit();
00137 
00138   // [fill srv structure and make request to the server]
00139 
00140   // [fill action structure and make request to the action server]
00141 
00142   ROS_DEBUG("PUBLISH MESSAGE");
00143   // [publish messages]
00144   this->scan_publisher.publish(this->LaserScan_msg);
00145   // multiecho
00146   if(is_multiecho_)
00147   {
00148     this->LaserScan2_msg = this->LaserScan_msg;
00149     this->LaserScan3_msg = this->LaserScan_msg;
00150     this->LaserScan2_msg.ranges = franges2;                        // [m]
00151     this->LaserScan3_msg.ranges = franges3;                        // [m]
00152     this->scan_publisher2.publish(this->LaserScan2_msg);
00153     this->scan_publisher3.publish(this->LaserScan3_msg);
00154   }
00155 
00156   //unlock access to driver if previously blocked
00157 
00158 }
00159 
00160 /*  [subscriber callbacks] */
00161 
00162 /*  [service callbacks] */
00163 
00164 /*  [action callbacks] */
00165 
00166 /*  [action requests] */
00167 
00168 void HokuyoLaserDriverNode::postNodeOpenHook(void)
00169 {
00170 }
00171 
00172 void HokuyoLaserDriverNode::addNodeDiagnostics(void)
00173 {
00174 }
00175 
00176 void HokuyoLaserDriverNode::addNodeOpenedTests(void)
00177 {
00178 }
00179 
00180 void HokuyoLaserDriverNode::addNodeStoppedTests(void)
00181 {
00182 }
00183 
00184 void HokuyoLaserDriverNode::addNodeRunningTests(void)
00185 {
00186 }
00187 
00188 void HokuyoLaserDriverNode::reconfigureNodeHook(int level)
00189 {
00190 }
00191 
00192 HokuyoLaserDriverNode::~HokuyoLaserDriverNode()
00193 {
00194   //[free dynamic memory]
00195 }
00196 
00197 /* main function */
00198 int main(int argc,char *argv[])
00199 {
00200   return driver_base::main<HokuyoLaserDriverNode>(argc,argv,"hokuyo_laser_driver_node");
00201 }


iri_hokuyo_laser
Author(s): Marti Morta, mmorta at iri.upc.edu
autogenerated on Fri Dec 6 2013 21:08:01