Skip to content

Commit b2abb07

Browse files
committed
Final fixes
* Switch to lambda instead of bind for timer callback * Handle no color by default so SRTM dataset works out of the box * Improve behavior of nullptr derefernece on publish Signed-off-by: Ryan Friedman <[email protected]>
1 parent eef4d27 commit b2abb07

File tree

6 files changed

+38
-29
lines changed

6 files changed

+38
-29
lines changed

include/grid_map_geo/grid_map_geo.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
#include <grid_map_core/GridMap.hpp>
4040
#include <grid_map_core/iterators/GridMapIterator.hpp>
4141

42+
// Color map is optional. If left as this default value, color will not be loaded.
43+
static const std::string COLOR_MAP_DEFAULT_PATH {""};
44+
4245

4346
#include <iostream>
4447
struct Location {
@@ -188,7 +191,6 @@ class GridMapGeo {
188191
Location maporigin_;
189192

190193
private:
191-
192194
// Set default map size occupying 4MB RAM assuming 32 bit height precision.
193195
int max_raster_x_size_ {1024};
194196
int max_raster_y_size_ {1024};

launch/load_tif.launch.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ def generate_launch_description():
2828
],
2929
)
3030

31-
# tif loader node
32-
tif_loader = Node(
31+
# map publisher node
32+
map_publisher = Node(
3333
package="grid_map_geo",
3434
namespace="grid_map_geo",
3535
executable="map_publisher",
36-
name="tif_loader",
36+
name="map_publisher",
3737
parameters=[
38-
{"tif_path": LaunchConfiguration("tif_path")},
39-
{"tif_color_path": LaunchConfiguration("tif_color_path")},
38+
{"gdal_dataset_path": LaunchConfiguration("gdal_dataset_path")},
39+
{"gdal_dataset_color_path": LaunchConfiguration("gdal_dataset_color_path")},
4040
],
4141
output="screen",
4242
emulate_tty=True,
@@ -51,8 +51,8 @@ def generate_launch_description():
5151
)
5252

5353
default_location = "sargans"
54-
default_tif_file = "sargans.tif"
55-
default_tif_color_file = "sargans_color.tif"
54+
default_gdal_dataset = "sargans.tif"
55+
default_gdal_color_dataset = "sargans_color.tif"
5656
return LaunchDescription(
5757
[
5858
DeclareLaunchArgument(
@@ -64,21 +64,23 @@ def generate_launch_description():
6464
description="Location.",
6565
),
6666
DeclareLaunchArgument(
67-
"tif_path",
67+
"gdal_dataset_path",
6868
default_value=os.path.join(
69-
pkg_grid_map_geo, "resources", default_tif_file
69+
pkg_grid_map_geo, "resources", default_gdal_dataset
7070
),
7171
description="Full path to the elevation map file.",
7272
),
7373
DeclareLaunchArgument(
74-
"tif_color_path",
74+
"gdal_dataset_color_path",
7575
default_value=os.path.join(
76-
pkg_grid_map_geo, "resources", default_tif_color_file
76+
pkg_grid_map_geo, "resources", default_gdal_color_dataset
7777
),
7878
description="Full path to the elevation texture file.",
7979
),
8080
static_transform_publisher,
81-
tif_loader,
81+
map_publisher,
8282
rviz,
8383
]
8484
)
85+
86+

launch/load_tif_launch.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<node pkg="tf2_ros" exec="static_transform_publisher" name="world_map" args="--frame-id world --child-frame-id map"/>
55

66
<node pkg="grid_map_geo" exec="map_publisher" name="map_publisher" output="screen">
7-
<param name="tif_path" value="$(find-pkg-share grid_map_geo)/resources/sargans.tif"/>
8-
<param name="tif_color_path" value="$(find-pkg-share grid_map_geo)/resources/sargans_color.tif"/>
7+
<param name="gdal_dataset_path" value="$(find-pkg-share grid_map_geo)/resources/sargans.tif"/>
8+
<param name="gdal_dataset_color_path" value="$(find-pkg-share grid_map_geo)/resources/sargans_color.tif"/>
99
</node>
1010

1111
<group if="$(var rviz)">

launch/load_vrt_launch.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
<arg name="rviz" default="false"/>
33
<node pkg="tf2_ros" exec="static_transform_publisher" name="world_map" args="--frame-id world --child-frame-id map"/>
44

5-
<node pkg="grid_map_geo" exec="map_publisher" name="map_publisher" output="screen" launch-prefix="gdb -ex run --args">
5+
<node pkg="grid_map_geo" exec="map_publisher" name="map_publisher" output="screen" launch-prefix="gdb -ex run --args" >
66
<param name="gdal_dataset_path" value="$(find-pkg-share grid_map_geo)/resources/ap_srtm1.vrt"/>
77
</node>
88

99
<group if="$(var rviz)">
10-
<node exec="rviz2" name="rviz2" pkg="rviz2" args="-d $(find-pkg-share grid_map_geo)/launch/config.rviz" />
10+
<node exec="rviz2" name="rviz2" pkg="rviz2" args="-d $(find-pkg-share grid_map_geo)/launch/config.rviz" />
1111
</group>
1212
</launch>

src/grid_map_geo.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void GridMapGeo::setMaxMapSizePixels(const int pixels_x, const int pixels_y) {
7474

7575
bool GridMapGeo::Load(const std::string &map_path, bool algin_terrain, const std::string color_map_path) {
7676
bool loaded = initializeFromGdalDataset(map_path, algin_terrain);
77-
if (!color_map_path.empty()) { // Load color layer if the color path is nonempty
77+
if (color_map_path != COLOR_MAP_DEFAULT_PATH) { // Load color layer if the color path is nonempty
7878
bool color_loaded = addColorFromGeotiff(color_map_path);
7979
}
8080
if (!loaded) return false;
@@ -85,7 +85,7 @@ bool GridMapGeo::initializeFromGdalDataset(const std::string &path, bool align_t
8585
GDALAllRegister();
8686
GDALDataset *dataset = (GDALDataset *)GDALOpen(path.c_str(), GA_ReadOnly);
8787
if (!dataset) {
88-
std::cout << "Failed to open" << std::endl;
88+
std::cout << __func__ << "Failed to open '" << path << "'" << std::endl;
8989
return false;
9090
}
9191
std::cout << std::endl << "Loading GDAL Dataset for gridmap" << std::endl;
@@ -197,7 +197,7 @@ bool GridMapGeo::addColorFromGeotiff(const std::string &path) {
197197
GDALAllRegister();
198198
GDALDataset *dataset = (GDALDataset *)GDALOpen(path.c_str(), GA_ReadOnly);
199199
if (!dataset) {
200-
std::cout << "Failed to open" << std::endl;
200+
std::cout << __func__ << "Failed to open '" << path << "'" << std::endl;
201201
return false;
202202
}
203203
std::cout << std::endl << "Loading color layer from GeoTIFF file for gridmap" << std::endl;
@@ -258,7 +258,7 @@ bool GridMapGeo::addLayerFromGeotiff(const std::string &layer_name, const std::s
258258
GDALAllRegister();
259259
GDALDataset *dataset = (GDALDataset *)GDALOpen(path.c_str(), GA_ReadOnly);
260260
if (!dataset) {
261-
std::cout << "Failed to open" << std::endl;
261+
std::cout << __func__ << "Failed to open '" << path << "'" << std::endl;
262262
return false;
263263
}
264264
std::cout << std::endl << "Loading color layer from GeoTIFF file for gridmap" << std::endl;

src/map_publisher.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class MapPublisher : public rclcpp::Node {
5454
original_map_pub_ = this->create_publisher<grid_map_msgs::msg::GridMap>("elevation_map", 1);
5555

5656
const std::string gdal_dataset_path = this->declare_parameter("gdal_dataset_path", ".");
57-
const std::string color_path = this->declare_parameter("tif_color_path", ".");
57+
const std::string color_path = this->declare_parameter("gdal_dataset_color_path", COLOR_MAP_DEFAULT_PATH);
5858
rcl_interfaces::msg::ParameterDescriptor max_map_descriptor;
5959
max_map_descriptor.read_only = true;
6060
max_map_descriptor.description =
@@ -74,20 +74,25 @@ class MapPublisher : public rclcpp::Node {
7474
std::clamp(this->declare_parameter("max_map_height", 1024, max_map_descriptor),
7575
max_map_descriptor_int_range.from_value, max_map_descriptor_int_range.to_value);
7676

77-
RCLCPP_INFO_STREAM(get_logger(), "file_path " << file_path);
78-
RCLCPP_INFO_STREAM(get_logger(), "color_path " << color_path);
77+
RCLCPP_INFO_STREAM(get_logger(), "gdal_dataset_path: '" << gdal_dataset_path << "'");
78+
RCLCPP_INFO_STREAM(get_logger(), "gdal_dataset_color_path: '" << color_path << "'");
7979

8080
map_ = std::make_shared<GridMapGeo>();
8181
map_->setMaxMapSizePixels(max_map_width, max_map_height);
8282
map_->Load(gdal_dataset_path, false, color_path);
83-
// timer_ = this->create_wall_timer(5s, std::bind(&MapPublisher::timer_callback, this));
83+
84+
auto timer_callback = [this]() -> void {
85+
auto msg = grid_map::GridMapRosConverter::toMessage(map_->getGridMap());
86+
if (msg) {
87+
msg->header.stamp = now();
88+
original_map_pub_->publish(std::move(msg));
89+
}
90+
};
91+
timer_ = this->create_wall_timer(5s, timer_callback);
8492
}
8593

8694
private:
87-
void timer_callback() {
88-
auto msg = grid_map::GridMapRosConverter::toMessage(map_->getGridMap());
89-
original_map_pub_->publish(std::move(msg));
90-
}
95+
void timer_callback() {}
9196
rclcpp::TimerBase::SharedPtr timer_;
9297
rclcpp::Publisher<grid_map_msgs::msg::GridMap>::SharedPtr original_map_pub_;
9398
std::shared_ptr<GridMapGeo> map_;

0 commit comments

Comments
 (0)