MCPcopy Create free account
hub / github.com/ANYbotics/grid_map / main

Function main

grid_map_demos/src/tutorial_demo_node.cpp:10–106  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8using namespace grid_map;
9
10int main(int argc, char** argv)
11{
12 // Initialize node and publisher.
13 ros::init(argc, argv, "grid_map_tutorial_demo");
14 ros::NodeHandle nh("~");
15 ros::Publisher publisher = nh.advertise<grid_map_msgs::GridMap>("grid_map", 1, true);
16
17 // Create grid map.
18 GridMap map({"elevation", "normal_x", "normal_y", "normal_z"});
19 map.setFrameId("map");
20 map.setGeometry(Length(1.2, 2.0), 0.03, Position(0.0, -0.1));
21 ROS_INFO("Created map with size %f x %f m (%i x %i cells).\n The center of the map is located at (%f, %f) in the %s frame.",
22 map.getLength().x(), map.getLength().y(),
23 map.getSize()(0), map.getSize()(1),
24 map.getPosition().x(), map.getPosition().y(), map.getFrameId().c_str());
25
26 // Work with grid map in a loop.
27 ros::Rate rate(30.0);
28 while (nh.ok()) {
29 ros::Time time = ros::Time::now();
30
31 // Add elevation and surface normal (iterating through grid map and adding data).
32 for (GridMapIterator it(map); !it.isPastEnd(); ++it) {
33 Position position;
34 map.getPosition(*it, position);
35 map.at("elevation", *it) = -0.04 + 0.2 * std::sin(3.0 * time.toSec() + 5.0 * position.y()) * position.x();
36 Eigen::Vector3d normal(-0.2 * std::sin(3.0 * time.toSec() + 5.0 * position.y()),
37 -position.x() * std::cos(3.0 * time.toSec() + 5.0 * position.y()), 1.0);
38 normal.normalize();
39 map.at("normal_x", *it) = normal.x();
40 map.at("normal_y", *it) = normal.y();
41 map.at("normal_z", *it) = normal.z();
42 }
43
44 // Add noise (using Eigen operators).
45 map.add("noise", 0.015 * Matrix::Random(map.getSize()(0), map.getSize()(1)));
46 map.add("elevation_noisy", map.get("elevation") + map["noise"]);
47
48 // Adding outliers (accessing cell by position).
49 for (unsigned int i = 0; i < 500; ++i) {
50 Position randomPosition = Position::Random();
51 if (map.isInside(randomPosition))
52 map.atPosition("elevation_noisy", randomPosition) = std::numeric_limits<float>::infinity();
53 }
54
55 // Filter values for submap (iterators).
56 map.add("elevation_filtered", map.get("elevation_noisy"));
57 Position topLeftCorner(1.0, 0.4);
58 boundPositionToRange(topLeftCorner, map.getLength(), map.getPosition());
59 Index startIndex;
60 map.getIndex(topLeftCorner, startIndex);
61 ROS_INFO_ONCE("Top left corner was limited from (1.0, 0.2) to (%f, %f) and corresponds to index (%i, %i).",
62 topLeftCorner.x(), topLeftCorner.y(), startIndex(0), startIndex(1));
63
64 Size size = (Length(1.2, 0.8) / map.getResolution()).cast<int>();
65 SubmapIterator it(map, startIndex, size);
66 for (; !it.isPastEnd(); ++it) {
67 Position currentPosition;

Callers

nothing calls this directly

Calls 14

boundPositionToRangeFunction · 0.85
setGeometryMethod · 0.80
getPositionMethod · 0.80
atMethod · 0.80
atPositionMethod · 0.80
getIndexMethod · 0.80
isValidMethod · 0.80
setFrameIdMethod · 0.45
isPastEndMethod · 0.45
addMethod · 0.45
isInsideMethod · 0.45
getResolutionMethod · 0.45

Tested by

no test coverage detected