-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlaser_to_gridmap.cpp
69 lines (54 loc) · 2 KB
/
laser_to_gridmap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <hypergrid/gridmap.hpp>
#include <hypergrid/conversions/laserscan_converter.hpp>
#include <ros/ros.h>
#include <sensor_msgs/LaserScan.h>
#include <nav_msgs/OccupancyGrid.h>
#include <tf/transform_listener.h>
double map_height;
double map_width;
double cell_size;
bool DEBUG;
ros::Publisher laser_gridmap_pub;
// Laser frame to base_footprint tf transform
std::string map_frame_id;
tf::TransformListener* tf_listener;
void laser_callback(const sensor_msgs::LaserScanConstPtr scan)
{
ros::Time t0 = ros::Time::now();
// Update the laser transform
tf::StampedTransform laser_footprint_transform;
try
{
tf_listener->lookupTransform(map_frame_id, scan->header.frame_id, ros::Time(0), laser_footprint_transform);
}
catch(tf::TransformException &ex)
{
ROS_ERROR("%s", ex.what());
return;
}
geometry_msgs::Pose origin;
origin.position.x = 0;
origin.position.y = - (map_height / 2);
origin.orientation.w = 1;
hypergrid::LaserScanConverter laser_converter(map_width, map_height, cell_size, origin, map_frame_id);
hypergrid::GridMap gridmap = laser_converter.convert(scan, laser_footprint_transform);
// Publish OccupancyGrid map
laser_gridmap_pub.publish(gridmap.toMapMsg());
if (DEBUG) std::cout << "Publish time: " << ros::Time::now() - t0 << std::endl;
}
int main(int argc, char **argv)
{
af::info();
ros::init(argc, argv, "laser_to_gridmap");
ros::NodeHandle public_nh, private_nh("~");
private_nh.param<std::string>("map_frame_id", map_frame_id, "base_footprint");
private_nh.param("height", map_height, 50.0);
private_nh.param("width", map_width, 50.0);
private_nh.param("cell_size", cell_size, 0.2);
private_nh.param("DEBUG", DEBUG, false);
laser_gridmap_pub = public_nh.advertise<nav_msgs::OccupancyGrid>("hypergrid/laser_to_gridmap", 2);
ros::Subscriber laser_sub = public_nh.subscribe("scan", 5, laser_callback);
tf_listener = new tf::TransformListener;
ros::spin();
return 0;
}