Go to the documentation of this file.00001 #ifndef __RVIZ_GRID_VIEWER_H
00002 #define __RVIZ_GRID_VIEWER_H
00003
00004 #include <string>
00005 #include <vector>
00006 #include <ros/ros.h>
00007 #include <tf/transform_broadcaster.h>
00008 #include <nav_msgs/OccupancyGrid.h>
00009
00010 #include "../core/state_data.h"
00011 #include "../core/maps/grid_map.h"
00012
00017 class RvizGridViewer {
00018 public:
00019
00024 RvizGridViewer(ros::Publisher pub, const double show_map_rate,
00025 std::string frame_odom, std::string frame_robot_pose) :
00026 _map_pub(pub), map_publishing_rate(show_map_rate),
00027 _frame_odom(frame_odom), _frame_robot_pose(frame_robot_pose) {}
00028
00033 void show_robot_pose(const RobotState &r) {
00034 tf::Transform t;
00035 t.setOrigin(tf::Vector3(r.x, r.y, 0.0));
00036 tf::Quaternion q;
00037 q.setRPY(0, 0, r.theta);
00038 t.setRotation(q);
00039 _tf_brcst.sendTransform(
00040 tf::StampedTransform(t, ros::Time::now(),
00041 _frame_odom, _frame_robot_pose));
00042 }
00043
00048 void show_map(const GridMap &map) {
00049
00050 if ((ros::Time::now() - _last_pub_time).toSec() < map_publishing_rate) {
00051 return;
00052 }
00053
00054 nav_msgs::OccupancyGrid map_msg;
00055 map_msg.info.map_load_time = ros::Time::now();
00056 map_msg.info.width = map.width();
00057 map_msg.info.height = map.height();
00058 map_msg.info.resolution = map.scale();
00059
00060 nav_msgs::MapMetaData &info = map_msg.info;
00061 info.origin.position.x = -info.resolution * map.map_center_x();
00062 info.origin.position.y = -info.resolution * map.map_center_y();
00063 info.origin.position.z = 0;
00064
00065 for (const auto &row : map.cells()) {
00066 for (const auto &cell : row) {
00067 int cell_value = cell->value() == -1 ? -1 : cell->value() * 100;
00068 map_msg.data.push_back(cell_value);
00069 }
00070 }
00071
00072 _map_pub.publish(map_msg);
00073 _last_pub_time = ros::Time::now();
00074 }
00075
00076 private:
00077 ros::Publisher _map_pub;
00078 ros::Time _last_pub_time;
00079 tf::TransformBroadcaster _tf_brcst;
00080 const double map_publishing_rate;
00081 std::string _frame_odom;
00082 std::string _frame_robot_pose;
00083 };
00084
00085 #endif