forked from ANYbotics/grid_map
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_demo_node.cpp
More file actions
52 lines (46 loc) · 1.6 KB
/
simple_demo_node.cpp
File metadata and controls
52 lines (46 loc) · 1.6 KB
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
#include <rclcpp/rclcpp.hpp>
#include <grid_map_ros/grid_map_ros.hpp>
#include <grid_map_msgs/msg/grid_map.hpp>
#include <cmath>
#include <memory>
#include <utility>
int main(int argc, char ** argv)
{
// Initialize node and publisher.
rclcpp::init(argc, argv);
rclcpp::Node::SharedPtr node = std::make_shared<rclcpp::Node>("grid_map_simple_demo");
auto publisher = node->create_publisher<grid_map_msgs::msg::GridMap>(
"grid_map", rclcpp::QoS(1).transient_local());
// Create grid map.
grid_map::GridMap map({"elevation"});
map.setFrameId("map");
map.setGeometry(grid_map::Length(1.2, 2.0), 0.03);
RCLCPP_INFO(
node->get_logger(),
"Created map with size %f x %f m (%i x %i cells).",
map.getLength().x(), map.getLength().y(),
map.getSize()(0), map.getSize()(1));
// Work with grid map in a loop.
rclcpp::Rate rate(30.0);
rclcpp::Clock clock;
while (rclcpp::ok()) {
// Add data to grid map.
rclcpp::Time time = node->now();
for (grid_map::GridMapIterator it(map); !it.isPastEnd(); ++it) {
grid_map::Position position;
map.getPosition(*it, position);
map.at(
"elevation",
*it) = -0.04 + 0.2 * std::sin(3.0 * time.seconds() + 5.0 * position.y()) * position.x();
}
// Publish grid map.
map.setTimestamp(time.nanoseconds());
std::unique_ptr<grid_map_msgs::msg::GridMap> message;
message = grid_map::GridMapRosConverter::toMessage(map);
publisher->publish(std::move(message));
RCLCPP_INFO_THROTTLE(node->get_logger(), clock, 1000, "Grid map published.");
// Wait for next cycle.
rate.sleep();
}
return 0;
}