Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ros/ros.h>
  2. #include <geometry_msgs/Twist.h>
  3. #include <sensor_msgs/LaserScan.h>
  4. #include <stdlib.h>
  5.  
  6. // For rand() and RAND_MAX
  7. using namespace std;
  8. const float contact_distance = 2;
  9. const float security_distance = 0.9;
  10. const float * distances;
  11. float angle_incr;
  12. float angle_min;
  13.  
  14. void processScanCallback(const sensor_msgs::LaserScan::ConstPtr& msg)
  15. {
  16.     distances = &msg->ranges[0];
  17.     angle_incr = msg->angle_increment;
  18.     angle_min = msg->angle_min;
  19. }
  20.  
  21. int getIndex(double angle)
  22. {
  23.     return floor((angle-angle_min)/angle_incr);
  24. }
  25.  
  26. double getAngle(int idx)
  27. {
  28.     return angle_min + idx*angle_incr;
  29. }
  30.  
  31. bool someRight()
  32. {
  33.     float right = distances[getIndex(-1.57)];
  34.     if (right < contact_distance) {
  35.         return true;
  36.     } else {
  37.         return false;
  38.     }
  39. }
  40.  
  41. bool someFront()
  42. {
  43.     for(int i = getIndex(-0.25); i < getIndex(0.25); ++i)
  44.     {
  45.         if (distances[i] < security_distance) {
  46.             return true;
  47.         }
  48.     }
  49.     return false;
  50. }
  51.  
  52. int main(int argc, char **argv)
  53. {
  54.     // Initialize the ROS system and become a node.
  55.     ros::init( argc , argv , "exploring") ;
  56.  
  57.     ROS_INFO_STREAM("Robotic explorer node running and initialized! Let's have some fun!");
  58.  
  59.     ros::NodeHandle nh;
  60.  
  61.     // Create a subscriber object
  62.     ros::Subscriber sub = nh.subscribe("/base_scan", 1000, processScanCallback);
  63.  
  64.     // Create a publisher object
  65.     ros::Publisher pub = nh.advertise <geometry_msgs::Twist>("/cmd_vel" , 1000) ;
  66.  
  67.     // Seed the random number generator.
  68.     srand ( time (0) ) ;
  69.  
  70.     // Loop at 2Hz until the node is shut down.
  71.     ros::Rate rate (2) ;
  72.     ros::Time begin = ros::Time::now();
  73.  
  74.     while ( begin.toSec() == 0 )
  75.       begin = ros::Time::now();
  76.  
  77.     double ellapsed_time = 0;
  78.     // Checks if we found for the first time a wall.
  79.     bool pared = false;
  80.  
  81.     while ( ( ros::ok () ) && ( ellapsed_time < 60*5 ) )
  82.     {
  83.         // Create and fill in the message. The other four
  84.         // fields , which are ignored by stage, default to 0.
  85.  
  86.         geometry_msgs::Twist msg;
  87.  
  88.         // Wait until it 's time for another iteration .
  89.         rate.sleep () ;
  90.         ros::spinOnce(); // give time to receive /base_scan messages
  91.  
  92.         // Bug algorithm:
  93.         // Once we found a wall for the first time* (we find it going all straight until we find somthing in fornt of us)
  94.         // 1. Go straight
  95.         // 2. If we found something in front of us, turn left.
  96.         // 3. Otherwise, if we don't have the wall at our right, turn right.
  97.         // 4. Go 1.
  98.  
  99.         msg.linear.x = 1.0;
  100.         msg.angular.z = 0;
  101.  
  102.         if (pared) {
  103.             if (someFront()) {
  104.                 msg.linear.x = 0;
  105.                 msg.angular.z = 2.35;
  106.             } else {
  107.                 if (!someRight()) {
  108.                     msg.linear.x = 0;
  109.                     msg.angular.z = -2.35;
  110.                 }
  111.             }
  112.         } else {
  113.             if (someFront())
  114.             {
  115.                 pared = true;
  116.             }
  117.         }
  118.  
  119.         // Publish the message.
  120.  
  121.         pub.publish(msg) ;
  122.  
  123.         // Send a message to rosout with the details .
  124.         ROS_INFO_STREAM("Sending random velocity command: "
  125.         << " linear=" << msg.linear.x
  126.         << " angular=" << msg.angular.z ) ;
  127.  
  128.         ros::Time current = ros::Time::now();
  129.         ellapsed_time = (current - begin).toSec();
  130.         ROS_INFO_STREAM("Ellpased time: " << ellapsed_time );
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement