laser_avoid_node.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 
00003 #include <sensor_msgs/LaserScan.h>
00004 #include <geometry_msgs/Twist.h>
00005 
00006 
00007 geometry_msgs::Twist vel;
00008 
00009 void laserCallback(const sensor_msgs::LaserScanConstPtr& msg)
00010 {
00011   double l_range, r_range;
00012 
00013   l_range = r_range = msg->range_max;
00014 
00015   for(unsigned int i = 0; i<msg->ranges.size(); i++)
00016   {
00017     if(i<(msg->ranges.size()/2))
00018     {
00019       if((l_range > msg->ranges[i]) && (msg->ranges[i] > 0.0))
00020         l_range = msg->ranges[i];
00021     } else
00022     { 
00023       if((r_range > msg->ranges[i]) && (msg->ranges[i] > 0.0))
00024         r_range = msg->ranges[i];
00025     } 
00026   }
00027 
00028   double min_dist = 0.2;
00029   double max_dist = 0.8;
00030 
00031 //  double l = (1e5*r_range)/500-100;
00032 //  double r = (1e5*l_range)/500-100;
00033   double l = (r_range - min_dist) / (max_dist-min_dist);
00034   double r = (l_range - min_dist) / (max_dist-min_dist);
00035 
00036   if (l > 1)
00037     l = 1;
00038   if (r > 1)
00039     r = 1;
00040 
00041   vel.linear.x = 0.15 * (r+l);
00042 
00043   double turnrate = 45;
00044   double turn = turnrate * (r-l);
00045   if(turn > turnrate)
00046     turn = turnrate;
00047   if(turn < -turnrate)
00048     turn = -turnrate;
00049 
00050   vel.angular.z = -turn * M_PI/180.0;
00051 
00052   ROS_INFO("l: %lf r: %lf x: %lf a: %lf", l_range, r_range, vel.linear.x, vel.angular.z);
00053   printf("l: %lf r: %lf x: %lf a: %lf\n", l, r, vel.linear.x, vel.angular.z);
00054   fflush(stdout);
00055 }
00056 
00057 int main(int argc, char** argv)
00058 {
00059   ros::init(argc, argv, "laser_avoid");
00060 
00061   ros::NodeHandle nh;
00062 
00063   ros::Publisher vel_pub = nh.advertise<geometry_msgs::Twist>("cmd_vel", 1);
00064   ros::Subscriber laser_sub = nh.subscribe("scan", 1, &laserCallback);
00065 
00066   ros::Rate loop_rate(50);
00067 
00068   vel.linear.x = 0.0;
00069   vel.angular.z = 0.0;
00070 
00071   while(ros::ok())
00072   {
00073     if (ros::isShuttingDown()) {
00074       vel.linear.x = 0.0;
00075       vel.angular.z = 0.0;
00076     }
00077     
00078 
00079     vel_pub.publish(vel);
00080 
00081     ros::spinOnce();
00082     loop_rate.sleep();
00083   }
00084 
00085   return 0;
00086 }
00087 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


laser_avoid
Author(s): Maciej Stefanczyk
autogenerated on Thu Nov 14 2013 11:55:11