Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <ros/ros.h>
00012 #include <ros/console.h>
00013 #include <nav_msgs/OccupancyGrid.h>
00014
00015 #include <tf/tf.h>
00016 #include <tf/transform_listener.h>
00017 #include <tf/transform_datatypes.h>
00018 #include <map_ray_caster/map_ray_caster.h>
00019 #include <sensor_msgs/LaserScan.h>
00020
00021 std::string odom_frame_;
00022 tf::TransformListener* tf_listener_;
00023 map_ray_caster::MapRayCaster caster_;
00024 ros::Publisher* fake_laser_publisher_;
00025 ros::Publisher* fake_laser_absolute_publisher_;
00026
00027 sensor_msgs::LaserScan rotateScan(sensor_msgs::LaserScan original_scan, float rotation) {
00028 sensor_msgs::LaserScan scan = original_scan;
00029 while (rotation > 0 ) {
00030 rotation -= 2*M_PI;
00031 }
00032 int shift = -rotation/original_scan.angle_increment;
00033 for (size_t index = 0; index < original_scan.ranges.size(); index++) {
00034 scan.ranges[index] = original_scan.ranges[(index+shift)%original_scan.ranges.size()];
00035 if (scan.ranges[index] > 0.99*scan.range_max) {
00036 scan.ranges[index] = scan.range_max*2;
00037 }
00038 }
00039 return scan;
00040
00041 }
00042
00043 void handleCostmap(const nav_msgs::OccupancyGridConstPtr& msg) {
00044
00045 tf::StampedTransform tr;
00046 float map_relative_orientation = 0.0f;
00047 bool lookup_successfull = false;
00048 try
00049 {
00050 tf_listener_->waitForTransform(odom_frame_, msg->header.frame_id,
00051 msg->header.stamp, ros::Duration(0.2));
00052 tf_listener_->lookupTransform(odom_frame_, msg->header.frame_id,
00053 msg->header.stamp, tr);
00054 lookup_successfull = true;
00055 }
00056 catch (tf::TransformException ex)
00057 {
00058 ROS_ERROR("%s", ex.what());
00059 }
00060 if (lookup_successfull) {
00061 map_relative_orientation = tf::getYaw(tr.getRotation());
00062 }
00063 sensor_msgs::LaserScan scan;
00064 scan.range_max = 2;
00065 scan.angle_min = -M_PI;
00066 scan.angle_max = M_PI;
00067 scan.angle_increment = 2*M_PI/ 400;
00068 scan.header.frame_id = "base_laser_link";
00069 caster_.laserScanCast(*msg, scan);
00070 fake_laser_absolute_publisher_->publish(scan);
00071 fake_laser_publisher_->publish(rotateScan(scan,map_relative_orientation));
00072 }
00073
00074 int main(int argc, char **argv)
00075 {
00076 ros::init(argc, argv, "default_name_fake_laser");
00077
00078
00079
00080
00081
00082
00083 ros::NodeHandle nh;
00084 ros::NodeHandle private_nh("~");
00085 odom_frame_ = "base_link";
00086 ros::Subscriber costmap_handler_ = nh.subscribe("local_costmap", 1, handleCostmap);
00087 ros::Publisher fk = nh.advertise<sensor_msgs::LaserScan>("fake_laser",10);
00088 fake_laser_publisher_ = &fk;
00089 ros::Publisher fka = nh.advertise<sensor_msgs::LaserScan>("fake_laser_absolute",10);
00090 fake_laser_absolute_publisher_ = &fka;
00091 tf_listener_ = new tf::TransformListener();
00092
00093 ros::spin();
00094
00095 return 0;
00096 }
00097
00098
00099