Go to the documentation of this file.00001 #include "hokuyo_laser_driver.h"
00002 #include <cmath>
00003 #include "ctime.h"
00004
00005 HokuyoLaserDriver::HokuyoLaserDriver()
00006 {
00007
00008
00009 this->laser = NULL;
00010 std::string laser_id(ros::this_node::getName());
00011 replace(laser_id.begin(), laser_id.end(), '/', '_');
00012 ROS_INFO("Laser ID: %s", laser_id.c_str());
00013
00014 try
00015 {
00016 this->laser = new CHokuyo(laser_id);
00017 setDriverId(this->laser->get_id());
00018 }
00019 catch (CException & e)
00020 {
00021 ROS_FATAL("'%s'",e.what().c_str());
00022 }
00023
00028 this->number_of_scans = 0;
00029
00030 this->config_.is_multiecho = false;
00031
00032 }
00033
00034 bool HokuyoLaserDriver::openDriver(void)
00035 {
00036
00037 ROS_DEBUG("openDriver");
00038 try
00039 {
00040 ROS_DEBUG("eth:%d ip:%s usb:%s",this->use_eth,this->ip.c_str(),this->port.c_str());
00041 if(this->config_.use_ethernet)
00042 this->laser->connect(false,this->config_.ip_address);
00043 else
00044 this->laser->connect(true,this->config_.usb_port);
00045
00046
00047 this->laser->get_specifications(this->specs);
00048
00049 this->range_min = this->specs.min_dist/1000;
00050 this->range_max = this->specs.max_dist/1000;
00051 this->angle_increment = 2*M_PI / this->specs.num_steps;
00052 this->angle_min = (this->specs.first_step - this->specs.center_step) * this->angle_increment;
00053 this->angle_max = (this->specs.last_step - this->specs.center_step) * this->angle_increment;
00054 this->scan_time = 0.025;
00055 this->time_increment = 0.0001;
00056
00057
00058
00059 evnts.push_back(this->laser->get_new_scan_event());
00060 evnts.push_back(this->laser->get_end_event());
00061 evnts.push_back(this->laser->get_error_check_command_event());
00062
00063 }catch( CHokuyoException &e )
00064 {
00065 ROS_FATAL("'%s'",e.what().c_str());
00066 }
00067
00068 return true;
00069 }
00070
00071 bool HokuyoLaserDriver::closeDriver(void)
00072 {
00073 ROS_DEBUG("closeDriver");
00074 try
00075 {
00076 this->laser->disconnect();
00077 }
00078 catch (CException & e)
00079 {
00080 ROS_ERROR("'%s'", e.what().c_str());
00081 return false;
00082 }
00083 return true;
00084 }
00085
00086 bool HokuyoLaserDriver::startDriver(void)
00087 {
00088 ROS_DEBUG("startDriver");
00089 try{
00090 THokuyo_scan_config sc;
00091 sc = this->translateConfig(this->config_);
00092 ROS_INFO("Scan Configuration\n\tmin: %f\n\tmax: %f\n\tgroup: %d\n\tinterval: %d\n\tshort? %d\n\tintensity? %d\n\tmultiecho? %d",
00093 config_.angle_min, config_.angle_max, config_.cluster, config_.interval,
00094 config_.is_short, config_.is_intensity, config_.is_multiecho);
00095 this->laser->start(sc);
00096 ROS_INFO("Acquisition Started");
00097 }
00098 catch (CException & e)
00099 {
00100 ROS_ERROR("'%s'", e.what().c_str());
00101 return false;
00102 }
00103
00104
00105 return true;
00106 }
00107
00108 bool HokuyoLaserDriver::stopDriver(void)
00109 {
00110 ROS_DEBUG("stopDriver");
00111 try{
00112 this->laser->stop();
00113 ROS_INFO("Acquisition finished");
00114 }
00115 catch (CException & e)
00116 {
00117 ROS_ERROR("'%s'", e.what().c_str());
00118 return false;
00119 }
00120 return true;
00121 }
00122
00123 void HokuyoLaserDriver::config_update(const Config& new_cfg, uint32_t level)
00124 {
00125 ROS_DEBUG("config_update");
00126 this->lock();
00127
00128
00129
00130 switch(this->getState()){
00131 case HokuyoLaserDriver::OPENED:
00132 break;
00133 case HokuyoLaserDriver::RUNNING:
00134 this->frame_id = new_cfg.frame_id;
00135 break;
00136 case HokuyoLaserDriver::CLOSED:
00137 this->ip = new_cfg.ip_address;
00138 this->port = new_cfg.usb_port;
00139 this->use_eth = new_cfg.use_ethernet;
00140 break;
00141 }
00142
00143
00144 this->config_=new_cfg;
00145
00146 this->unlock();
00147 }
00148
00149 HokuyoLaserDriver::~HokuyoLaserDriver()
00150 {
00151 delete this->laser;
00152 }
00153
00154
00155
00156
00158 void HokuyoLaserDriver::getScan(THokuyo_scan & scan)
00159 {
00160 ROS_DEBUG("getting scan");
00161 this->laser->get_scan(scan);
00162 ROS_DEBUG("scan received");
00163 }
00164
00165 THokuyo_scan_config HokuyoLaserDriver::translateConfig(Config & cfg)
00166 {
00167 THokuyo_scan_config sc;
00168
00169 sc.num = 0;
00170
00171
00172 sc.start_step = this->specs.center_step + (int)(cfg.angle_min/this->angle_increment);
00173
00174 if(sc.start_step < this->specs.first_step)
00175 sc.start_step = this->specs.first_step;
00176 else if(sc.start_step > this->specs.center_step)
00177 sc.end_step = this->specs.center_step;
00178
00179 cfg.angle_min = (sc.start_step - this->specs.center_step) * this->angle_increment;
00180
00181 sc.end_step = (int)( cfg.angle_max/this->angle_increment + this->specs.center_step);
00182
00183 if(sc.end_step > this->specs.last_step)
00184 sc.end_step = this->specs.last_step;
00185 else if(sc.end_step < this->specs.center_step)
00186 sc.end_step = this->specs.center_step;
00187
00188 cfg.angle_max = (sc.end_step - this->specs.center_step) * this->angle_increment;
00189
00190
00191 sc.grouping = cfg.cluster;
00192 sc.interval = cfg.interval;
00193 sc.is_short = cfg.is_short;
00194 sc.is_intensity = cfg.is_intensity;
00195 sc.is_multiecho = cfg.is_multiecho;
00196
00197
00198 ROS_DEBUG("New Configuration:\n\tmin: %d(%f)\n\tmax: %d(%f)\n\tgroup %d\n\tinterval %d\n\tshort?\n\t%d(%d)\n\tintensity?%d(%d)\n\tmultiecho?%d(%d)",
00199 sc.start_step,
00200 cfg.angle_min,
00201 sc.end_step,
00202 cfg.angle_max,
00203 sc.grouping,
00204 sc.interval,
00205 sc.is_short,
00206 cfg.is_short,
00207 sc.is_intensity,
00208 cfg.is_intensity,
00209 sc.is_multiecho,
00210 cfg.is_multiecho);
00211
00212 return sc;
00213 }