Go to the documentation of this file.00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <unistd.h>
00004 #include <string.h>
00005 #include <fstream>
00006 #include <iostream>
00007 #include <iomanip>
00008 #include "LaserScanner.h"
00009
00010 using namespace std;
00011
00012
00013 double LaserScanner::Index2Angle(sensor_msgs::LaserScan laser_scan_msg, int index){
00014 return (laser_scan_msg.angle_min + (index*laser_scan_msg.angle_increment));
00015
00016 }
00017
00018 int LaserScanner::Angle2Index(sensor_msgs::LaserScan laser_scan_msg, double angle){
00019 return (int)((angle-laser_scan_msg.angle_min)/laser_scan_msg.angle_increment);
00020 }
00021
00022 double LaserScanner::getAverageRange(sensor_msgs::LaserScan & laser_scan_msg, int start_index, int end_index){
00023 double avg = 0;
00024
00025 for(int i = start_index; i < end_index;i++){
00026 if(!std::isnan(laser_scan_msg.ranges[i]))
00027 avg = avg + laser_scan_msg.ranges[i];
00028 }
00029
00030 avg = avg / (end_index - start_index + 1);
00031
00032 return avg;
00033
00034 }
00035
00036 double LaserScanner::getAverageRangeRight(sensor_msgs::LaserScan & laser_scan_msg, double degree){
00037 int start_index = 0;
00038 int end_index = (int)(degree/radian2degree(laser_scan_msg.angle_increment));
00039
00040
00041
00042 return getAverageRange(laser_scan_msg, start_index, end_index);
00043
00044
00045 }
00046
00047 double LaserScanner::getAverageRangeLeft(sensor_msgs::LaserScan & laser_scan_msg, double degree){
00048 int end_index = laser_scan_msg.ranges.size();
00049 int start_index = end_index- (int)(degree/radian2degree(laser_scan_msg.angle_increment));
00050
00051
00052 return getAverageRange(laser_scan_msg, start_index, end_index);
00053
00054
00055 }
00056
00057 double LaserScanner::getAverageRangeStraight(sensor_msgs::LaserScan & laser_scan_msg, double degree){
00058 int start_index =(int)(laser_scan_msg.ranges.size()/2)-(int)((degree/radian2degree(laser_scan_msg.angle_increment))/2);
00059 int end_index = (int)(laser_scan_msg.ranges.size()/2)+(int)((degree/radian2degree(laser_scan_msg.angle_increment))/2);
00060
00061
00062 return getAverageRange(laser_scan_msg, start_index, end_index);
00063
00064
00065 }
00066
00067
00068
00069 double LaserScanner::getMinimumRange(sensor_msgs::LaserScan & LaserScanMsg, int start_index, int end_index){
00070
00071 int min_index = 0;
00072 for(int i = start_index; i < end_index ; i++){
00073 if(!std::isnan(LaserScanMsg.ranges[i])){
00074 if((LaserScanMsg.ranges[i]>= LaserScanMsg.range_min) && (LaserScanMsg.ranges[i]<= LaserScanMsg.range_max)){
00075 min_index = i;
00076 break;
00077 }
00078 }
00079 }
00080
00081 for(int i = min_index+1; i < end_index ; i++){
00082 if(!std::isnan(LaserScanMsg.ranges[i])){
00083 if((LaserScanMsg.ranges[i]>= LaserScanMsg.range_min) && (LaserScanMsg.ranges[i]<= LaserScanMsg.range_max)){
00084 if (LaserScanMsg.ranges[min_index]>LaserScanMsg.ranges[i]){
00085
00086 min_index=i;
00087 }
00088
00089 }
00090 }
00091 }
00092 return LaserScanMsg.ranges[min_index];
00093
00094 }
00095
00096
00097 double LaserScanner::getMinimumRangeRight(sensor_msgs::LaserScan & laser_scan_msg, double degree){
00098 int start_index = 0;
00099 int end_index = (int)(degree/radian2degree(laser_scan_msg.angle_increment));
00100
00101
00102
00103 return getMinimumRange(laser_scan_msg, start_index, end_index);
00104
00105 }
00106
00107
00108 double LaserScanner::getMinimumRangeLeft(sensor_msgs::LaserScan & laser_scan_msg, double degree){
00109 int end_index = laser_scan_msg.ranges.size();
00110 int start_index = end_index- (int)(degree/radian2degree(laser_scan_msg.angle_increment));
00111
00112
00113 return getMinimumRange(laser_scan_msg, start_index, end_index);
00114
00115
00116 }
00117
00118
00119 double LaserScanner::getMaximumRange(sensor_msgs::LaserScan & LaserScanMsg, int start_index, int end_index){
00120
00121 int max_index = 0;
00122 for(int i = start_index; i < end_index ; i++){
00123 if((LaserScanMsg.ranges[i]>= LaserScanMsg.range_min) && (LaserScanMsg.ranges[i]<= LaserScanMsg.range_max)){
00124 max_index = i;
00125 break;
00126 }
00127 }
00128
00129 for(int i = max_index+1; i < end_index ; i++){
00130 if((LaserScanMsg.ranges[i]>= LaserScanMsg.range_min) && (LaserScanMsg.ranges[i]<= LaserScanMsg.range_max)){
00131 if (LaserScanMsg.ranges[max_index]<LaserScanMsg.ranges[i]){
00132
00133 max_index=i;
00134 }
00135
00136 }
00137 }
00138 return LaserScanMsg.ranges[max_index];
00139
00140 }
00141
00142 double LaserScanner::getMaximumRangeRight(sensor_msgs::LaserScan & laser_scan_msg, double degree){
00143 int start_index = 0;
00144 int end_index = (int)(degree/radian2degree(laser_scan_msg.angle_increment));
00145
00146
00147
00148 return getMaximumRange(laser_scan_msg, start_index, end_index);
00149
00150 }
00151
00152
00153 double LaserScanner::getMaximumRangeLeft(sensor_msgs::LaserScan & laser_scan_msg, double degree){
00154 int end_index = laser_scan_msg.ranges.size();
00155 int start_index = end_index- (int)(degree/radian2degree(laser_scan_msg.angle_increment));
00156
00157
00158 return getMaximumRange(laser_scan_msg, start_index, end_index);
00159
00160
00161 }
00162
00163
00164
00165
00171 int LaserScanner::getIndexOfMaximumRange(sensor_msgs::LaserScan & LaserScanMsg){
00172
00173
00174 int max_index = 0;
00175 for(int i = 0; i < LaserScanMsg.ranges.size() ; i++){
00176 if((LaserScanMsg.ranges[i]>= LaserScanMsg.range_min) && (LaserScanMsg.ranges[i]<= LaserScanMsg.range_max)){
00177 max_index = i;
00178 break;
00179 }
00180 }
00181
00182 for(int i = max_index+1; i < LaserScanMsg.ranges.size() ; i++){
00183 if((LaserScanMsg.ranges[i]>= LaserScanMsg.range_min) && (LaserScanMsg.ranges[i]<= LaserScanMsg.range_max)){
00184 if (LaserScanMsg.ranges[max_index]<LaserScanMsg.ranges[i]){
00185
00186 max_index=i;
00187 }
00188
00189 }
00190 }
00191 return max_index;
00192 }
00193
00199 int LaserScanner::getIndexOfMinimumRange(sensor_msgs::LaserScan & LaserScanMsg){
00200
00201
00202 int min_index = 0;
00203 for(int i = 0; i < LaserScanMsg.ranges.size() ; i++){
00204 if((LaserScanMsg.ranges[i]>= LaserScanMsg.range_min) && (LaserScanMsg.ranges[i]<= LaserScanMsg.range_max)){
00205 min_index = i;
00206 break;
00207 }
00208 }
00209
00210 for(int i = min_index+1; i < LaserScanMsg.ranges.size() ; i++){
00211 if((LaserScanMsg.ranges[i]>= LaserScanMsg.range_min) && (LaserScanMsg.ranges[i]<= LaserScanMsg.range_max)){
00212 if (LaserScanMsg.ranges[min_index]>LaserScanMsg.ranges[i]){
00213
00214 min_index=i;
00215 }
00216
00217 }
00218 }
00219 return min_index;
00220 }
00221
00228 double LaserScanner::getMaximumRange(sensor_msgs::LaserScan & LaserScanMsg){
00229
00230 return LaserScanMsg.ranges[getIndexOfMaximumRange(LaserScanMsg)];
00231 }
00232
00239 double LaserScanner::getMinimumRange(sensor_msgs::LaserScan & LaserScanMsg){
00240
00241 return LaserScanMsg.ranges[getIndexOfMinimumRange(LaserScanMsg)];
00242 }
00243
00244 double LaserScanner::getFrontRange(sensor_msgs::LaserScan & LaserScanMsg){
00245
00246 return LaserScanMsg.ranges[LaserScanMsg.ranges.size()/2];
00247 }
00248
00249
00255 double LaserScanner::getRelativeAngleOfMaximumRange(sensor_msgs::LaserScan & LaserScanMsg){
00256
00257 return -(LaserScanMsg.angle_min+(LaserScanMsg.angle_increment*getIndexOfMaximumRange(LaserScanMsg)));
00258 }
00259
00265 double LaserScanner::getRelativeAngleOfMinimumRange(sensor_msgs::LaserScan & LaserScanMsg){
00266
00267 return -(LaserScanMsg.angle_min+(LaserScanMsg.angle_increment*getIndexOfMinimumRange(LaserScanMsg)));
00268 }
00269
00275 bool LaserScanner::isObstacleTooClose(sensor_msgs::LaserScan & LaserMsg, int start_index, int end_index, double DistanceThreshold){
00276 bool result=false;
00277
00278 if (getMinimumRange(LaserMsg, start_index, end_index)< DistanceThreshold)
00279 result=true;
00280
00281 return result;
00282 }
00283
00290 void LaserScanner::printLaserScanRanges(sensor_msgs::LaserScan::ConstPtr LaserScanMsg)
00291 {
00293 cout<<endl;
00294 cout << "-- LASER SCAN INFO --"<<endl;
00295 cout << "Header Info (std_msgs/Header.msg) "<<endl;
00296 cout << setw(20)<< "Sequence: " << LaserScanMsg->header.seq <<endl;
00297
00298
00299
00300
00301 cout << setw(20)<< "Stamp: " << LaserScanMsg->header.stamp <<endl;
00302
00303 cout << setw(20)<< "Frame_ID: " << LaserScanMsg->header.frame_id <<endl<<endl;
00304
00306 cout << "LaserScan Info (sensor_msgs/LaserScan.msg) "<<endl;
00307 cout << setw(20)<<"Size: " << LaserScanMsg->ranges.size() <<endl;
00308 cout << setw(20)<< "Angle increment: "<<"[" <<LaserScanMsg->angle_increment <<" rad, " <<radian2degree(LaserScanMsg->angle_increment) <<" deg]"<<endl;
00309 cout << setw(20)<< "Minimum angle: " <<"["<<LaserScanMsg->angle_min <<" rad, " <<radian2degree(LaserScanMsg->angle_min) <<" deg]"<<endl;
00310 cout << setw(20)<< "Maximum angle: " <<"["<<LaserScanMsg->angle_max<<" rad, " <<radian2degree(LaserScanMsg->angle_max) <<" deg]" <<endl;
00311 cout << setw(20)<< "Minumum range: " <<LaserScanMsg->range_min <<" m"<<endl;
00312 cout << setw(20)<< "Maximum range: " <<LaserScanMsg->range_max <<" m"<<endl;
00313 cout << setw(20)<< "Scan time: " <<LaserScanMsg->scan_time <<" seconds"<<endl;
00314 cout << setw(20)<< "Time increment: " <<LaserScanMsg->scan_time <<" seconds"<<endl<<endl;
00315
00317 cout << setw(20)<< "LaserScan Ranges"<<endl;
00318
00319
00320
00321 cout << "---"<<endl<<endl;
00322
00323 }
00324
00325 void LaserScanner::printLaserScanRanges(sensor_msgs::LaserScan LaserScanMsg)
00326 {
00328 cout<<endl;
00329 cout << "-- LASER SCAN INFO --"<<endl;
00330 cout << "Header Info (std_msgs/Header.msg) "<<endl;
00331 cout << setw(20)<< "Sequence: " << LaserScanMsg.header.seq <<endl;
00332
00333
00334
00335
00336 cout << setw(20)<< "Stamp: " << LaserScanMsg.header.stamp <<endl;
00337
00338 cout << setw(20)<< "Frame_ID: " << LaserScanMsg.header.frame_id <<endl<<endl;
00339
00341 cout << "LaserScan Info (sensor_msgs/LaserScan.msg) "<<endl;
00342 cout << setw(20)<<"Size: " << LaserScanMsg.ranges.size() <<endl;
00343 cout << setw(20)<< "Angle increment: "<<"[" <<LaserScanMsg.angle_increment <<" rad, " <<radian2degree(LaserScanMsg.angle_increment) <<" deg]"<<endl;
00344 cout << setw(20)<< "Minimum angle: " <<"["<<LaserScanMsg.angle_min <<" rad, " <<radian2degree(LaserScanMsg.angle_min) <<" deg]"<<endl;
00345 cout << setw(20)<< "Maximum angle: " <<"["<<LaserScanMsg.angle_max<<" rad, " <<radian2degree(LaserScanMsg.angle_max) <<" deg]" <<endl;
00346 cout << setw(20)<< "Minumum range: " <<LaserScanMsg.range_min <<" m"<<endl;
00347 cout << setw(20)<< "Maximum range: " <<LaserScanMsg.range_max <<" m"<<endl;
00348 cout << setw(20)<< "Scan time: " <<LaserScanMsg.scan_time <<" seconds"<<endl;
00349 cout << setw(20)<< "Time increment: " <<LaserScanMsg.scan_time <<" seconds"<<endl<<endl;
00350
00352 cout << setw(20)<< "LaserScan Ranges"<<endl;
00353 cout << setw(20)<< "Minimum range [range: "<<getMinimumRange(LaserScanMsg)<<", angle: " <<radian2degree(getRelativeAngleOfMinimumRange(LaserScanMsg))<<", index: " <<getIndexOfMinimumRange(LaserScanMsg)<<"]" <<endl;
00354 cout << setw(20)<< "Maximum range [range: "<<getMaximumRange(LaserScanMsg)<<", angle: " <<radian2degree(getRelativeAngleOfMaximumRange(LaserScanMsg))<<" , index: " <<getIndexOfMaximumRange(LaserScanMsg)<<"]" <<endl;
00355 cout << setw(20)<< "Front range: " << getFrontRange(LaserScanMsg) <<endl;
00356 cout << "---"<<endl<<endl;
00357
00358 }