|
| 1 | +#include <ros/ros.h> |
| 2 | +#include <grid_map_ros/grid_map_ros.hpp> |
| 3 | +#include <grid_map_cv/grid_map_cv.hpp> |
| 4 | + |
| 5 | +using namespace grid_map; |
| 6 | +using namespace ros; |
| 7 | + |
| 8 | +int main(int argc, char** argv) |
| 9 | +{ |
| 10 | + // Initialize node and publisher. |
| 11 | + init(argc, argv, "resolution_change_demo"); |
| 12 | + NodeHandle nodeHandle("~"); |
| 13 | + Publisher publisher = nodeHandle.advertise<grid_map_msgs::GridMap>("grid_map", 1, true); |
| 14 | + |
| 15 | + // Create grid map. |
| 16 | + GridMap map({"elevation"}); |
| 17 | + map.setFrameId("map"); |
| 18 | + map.setGeometry(Length(1.2, 2.0), 0.03); |
| 19 | + ROS_INFO("Created map with size %f x %f m (%i x %i cells).", |
| 20 | + map.getLength().x(), map.getLength().y(), |
| 21 | + map.getSize()(0), map.getSize()(1)); |
| 22 | + |
| 23 | + // Add data. |
| 24 | + map["elevation"].setZero(); // Un/comment this line to try with and without transparency. |
| 25 | + grid_map::Polygon polygon; |
| 26 | + polygon.setFrameId(map.getFrameId()); |
| 27 | + polygon.addVertex(Position( 0.480, 0.000)); |
| 28 | + polygon.addVertex(Position( 0.164, 0.155)); |
| 29 | + polygon.addVertex(Position( 0.116, 0.500)); |
| 30 | + polygon.addVertex(Position(-0.133, 0.250)); |
| 31 | + polygon.addVertex(Position(-0.480, 0.399)); |
| 32 | + polygon.addVertex(Position(-0.316, 0.000)); |
| 33 | + polygon.addVertex(Position(-0.480, -0.399)); |
| 34 | + polygon.addVertex(Position(-0.133, -0.250)); |
| 35 | + polygon.addVertex(Position( 0.116, -0.500)); |
| 36 | + polygon.addVertex(Position( 0.164, -0.155)); |
| 37 | + polygon.addVertex(Position( 0.480, 0.000)); |
| 38 | + |
| 39 | + for (grid_map::PolygonIterator iterator(map, polygon); !iterator.isPastEnd(); ++iterator) { |
| 40 | + map.at("elevation", *iterator) = 0.3; |
| 41 | + } |
| 42 | + |
| 43 | + Rate rate(10.0); |
| 44 | + |
| 45 | + // Work in a loop. |
| 46 | + while (nodeHandle.ok()) { |
| 47 | + |
| 48 | + // Initialize. |
| 49 | + ros::Time time = ros::Time::now(); |
| 50 | + const double resolution = 0.05 + 0.04 * sin(time.toSec()); |
| 51 | + |
| 52 | + // Change resoltion of grid map. |
| 53 | + GridMap modifiedMap; |
| 54 | + GridMapCvProcessing::changeResolution(map, modifiedMap, resolution); |
| 55 | + |
| 56 | + // Publish grid map. |
| 57 | + grid_map_msgs::GridMap message; |
| 58 | + GridMapRosConverter::toMessage(modifiedMap, message); |
| 59 | + publisher.publish(message); |
| 60 | + ROS_INFO_STREAM("Published grid map with " << resolution << " m/cell resolution."); |
| 61 | + |
| 62 | + rate.sleep(); |
| 63 | + } |
| 64 | + |
| 65 | + return 0; |
| 66 | +} |
0 commit comments