Skip to content

Commit 13e01d2

Browse files
Fix Map Saver and testing: (#2084)
- Add Map Saver resource sharing - Add forgotten reset of save_map_service_ - Remove unnecessary map republisher - Increase save_map_timeout
1 parent 8ba925c commit 13e01d2

3 files changed

Lines changed: 16 additions & 14 deletions

File tree

nav2_map_server/src/map_saver/map_saver.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <memory>
3636
#include <stdexcept>
3737
#include <functional>
38+
#include <mutex>
3839

3940
using namespace std::placeholders;
4041

@@ -99,6 +100,9 @@ nav2_util::CallbackReturn
99100
MapSaver::on_cleanup(const rclcpp_lifecycle::State & /*state*/)
100101
{
101102
RCLCPP_INFO(get_logger(), "Cleaning up");
103+
104+
save_map_service_.reset();
105+
102106
return nav2_util::CallbackReturn::SUCCESS;
103107
}
104108

@@ -148,6 +152,9 @@ bool MapSaver::saveMapTopicToFile(
148152
// Pointer to map message received in the subscription callback
149153
nav_msgs::msg::OccupancyGrid::SharedPtr map_msg = nullptr;
150154

155+
// Mutex for handling map_msg shared resource
156+
std::recursive_mutex access;
157+
151158
// Correct map_topic_loc if necessary
152159
if (map_topic_loc == "") {
153160
map_topic_loc = "map";
@@ -173,8 +180,9 @@ bool MapSaver::saveMapTopicToFile(
173180
}
174181

175182
// A callback function that receives map message from subscribed topic
176-
auto mapCallback = [&map_msg](
183+
auto mapCallback = [&map_msg, &access](
177184
const nav_msgs::msg::OccupancyGrid::SharedPtr msg) -> void {
185+
std::lock_guard<std::recursive_mutex> guard(access);
178186
map_msg = msg;
179187
};
180188

@@ -198,6 +206,9 @@ bool MapSaver::saveMapTopicToFile(
198206
}
199207

200208
if (map_msg) {
209+
std::lock_guard<std::recursive_mutex> guard(access);
210+
// map_sub is no more needed
211+
map_sub.reset();
201212
// Map message received. Saving it to file
202213
if (saveMapToFile(*map_msg, save_parameters_loc)) {
203214
RCLCPP_INFO(get_logger(), "Map saved successfully");

nav2_map_server/test/component/test_map_saver_publisher.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@
1515
#include <experimental/filesystem>
1616
#include <string>
1717
#include <memory>
18-
#include <chrono>
1918

2019
#include "rclcpp/rclcpp.hpp"
2120
#include "nav2_map_server/map_io.hpp"
2221
#include "test_constants/test_constants.h"
2322

2423
#define TEST_DIR TEST_DIRECTORY
2524

26-
using namespace std::chrono_literals;
2725
using namespace nav2_map_server; // NOLINT
2826
using std::experimental::filesystem::path;
2927

@@ -34,7 +32,8 @@ class TestPublisher : public rclcpp::Node
3432
: Node("map_publisher")
3533
{
3634
std::string pub_map_file = path(TEST_DIR) / path(g_valid_yaml_file);
37-
LOAD_MAP_STATUS status = loadMapFromYaml(pub_map_file, msg_);
35+
nav_msgs::msg::OccupancyGrid msg;
36+
LOAD_MAP_STATUS status = loadMapFromYaml(pub_map_file, msg);
3837
if (status != LOAD_MAP_SUCCESS) {
3938
RCLCPP_ERROR(get_logger(), "Can not load %s map file", pub_map_file);
4039
return;
@@ -43,19 +42,11 @@ class TestPublisher : public rclcpp::Node
4342
map_pub_ = create_publisher<nav_msgs::msg::OccupancyGrid>(
4443
"map",
4544
rclcpp::QoS(rclcpp::KeepLast(1)).transient_local().reliable());
46-
47-
timer_ = create_wall_timer(300ms, std::bind(&TestPublisher::mapPublishCallback, this));
45+
map_pub_->publish(msg);
4846
}
4947

5048
protected:
51-
void mapPublishCallback()
52-
{
53-
map_pub_->publish(msg_);
54-
}
55-
5649
rclcpp::Publisher<nav_msgs::msg::OccupancyGrid>::SharedPtr map_pub_;
57-
rclcpp::TimerBase::SharedPtr timer_;
58-
nav_msgs::msg::OccupancyGrid msg_;
5950
};
6051

6152
int main(int argc, char ** argv)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
map_saver:
22
ros__parameters:
3-
save_map_timeout: 1000
3+
save_map_timeout: 5000
44
free_thresh_default: 0.196
55
occupied_thresh_default: 0.65

0 commit comments

Comments
 (0)