Advertisement
FahmiG

Turtlebot SLAM

Oct 11th, 2016
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. //Code for Video: https://www.youtube.com/watch?v=6FelCZQqIa4
  2.  
  3. //Launch file
  4.      <node
  5.         name="projectedScan"
  6.         pkg="kinect_fg"
  7.         type="projectedScan"
  8.         respawn="true"         
  9.         >
  10.         <remap from="input" to="/camera/depth/points"/>
  11.     </node> <!-- Output: "/obsScan"-->
  12. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  13. Source code for projectedScan.cpp:
  14.  
  15. #include <ros/ros.h>
  16. #include <boost/foreach.hpp>
  17.  
  18. #include "pluginlib/class_list_macros.h"
  19. #include "nodelet/nodelet.h"
  20. #include "sensor_msgs/LaserScan.h"
  21. #include <pcl/point_cloud.h>
  22. #include "pcl_ros/point_cloud.h"
  23. #include <pcl/point_types.h>
  24. #include <sensor_msgs/PointCloud2.h>
  25. #include <sensor_msgs/PointCloud.h>
  26. #include <pcl_conversions/pcl_conversions.h>
  27. #include <visualization_msgs/Marker.h>
  28. #include <dynamic_reconfigure/server.h>
  29.  
  30. using namespace std;
  31.  
  32. typedef pcl::PointCloud<pcl::PointXYZ> PointCloud;
  33.  
  34. ros::Publisher pub_;
  35. ros::Subscriber sub_;
  36. ros::Publisher marker_pub;
  37.  
  38. void callback(const PointCloud::ConstPtr& msg)
  39. {
  40.   sensor_msgs::LaserScanPtr output(new sensor_msgs::LaserScan());
  41.  
  42.  // output->header = msg->header; //Error here
  43.  output->header = pcl_conversions::fromPCL(msg->header);//use this in Hydro
  44.   output->header.frame_id = "/camera_depth_frame"; //Previous: "/laser"
  45.   output->angle_min = -M_PI / 6 ; //Default: -M_PI/6
  46.   output->angle_max = M_PI / 6 ; //Default: M_PI/6
  47.   output->angle_increment = M_PI / 180.0 /8; //Default: M_PI / 180.0 / 2.0
  48.   output->time_increment = 0.0;
  49.   output->scan_time = 1.0 / 30.0; //Default: 1.0 / 30.0
  50.   output->range_min = 0.3;
  51.   output->range_max = 10.0;
  52.  
  53.   uint32_t ranges_size = std::ceil((output->angle_max - output->angle_min)/ output->angle_increment);
  54.   output->ranges.assign(ranges_size, output->range_max + 1.0);
  55.  
  56.  
  57.   double max_height_ = 0.20;//previous: 5
  58.   double min_height_ = -0.25;//previous: 0
  59. //+++++++++++++++++++++++++++++++++++++++++++++
  60.  
  61.   BOOST_FOREACH (const pcl::PointXYZ& pt, msg->points)
  62.   {
  63.       float x = pt.x;
  64.       float y = pt.y;
  65.       float z = pt.z;
  66.  
  67.  
  68.       if (std::isnan(x) || std::isnan(y) || std::isnan(z)) {
  69.           //ROS_INFO("rejected for nan in point(%f, %f, %f)\n", x, y, z);
  70.           continue;
  71.       }
  72.  
  73.       if (y > max_height_ || y < min_height_) { //max_height_ min_height_
  74.           //ROS_INFO("rejected for height %f not in range (%f, %f)\n", x, min_height_, max_height_);
  75.           continue;
  76.       }
  77.       double angle = -atan2(x, z);
  78.       if (angle < output->angle_min || angle > output->angle_max) {
  79.          // ROS_INFO("rejected for angle %f not in range (%f, %f)\n",angle, output->angle_min, output->angle_max);
  80.           continue;
  81.       }
  82.  
  83.       int index = (angle - output->angle_min) / output->angle_increment;
  84.       double range_sq = z * z + x * x;
  85.       if (output->ranges[index] * output->ranges[index] > range_sq)
  86.           output->ranges[index] = sqrt(range_sq);
  87.  
  88.      // ROS_INFO("Y: %f %f, Z: %f %f", min_y, max_y, min_z, max_z);
  89.  
  90.      
  91.   }
  92.   pub_.publish(output);
  93. }
  94.  
  95. double min_height_, max_height_;
  96. int32_t u_min_, u_max_;
  97. std::string output_frame_id_;
  98. bool dynamic_set;
  99.  
  100. int main(int argc, char** argv)
  101. {
  102.   ros::init(argc, argv, "ScanObs");
  103.   ros::NodeHandle nh;
  104.  
  105.   ros::Subscriber sub = nh.subscribe<PointCloud>("input", 1, callback);
  106.   pub_ = nh.advertise<sensor_msgs::LaserScan> ("/obsScan", 10);
  107.   ros::spin();
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement