forked from ANYbotics/grid_map
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/grid_map_to_image' into 'master'
[grid_map] Add a grid_map to image node. GitOrigin-RevId: a689f94420002275b33842859ca0cf1a58e6b461
- Loading branch information
1 parent
d2f7710
commit b437623
Showing
7 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
grid_map_demos/include/grid_map_demos/GridmapToImageDemo.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* GridMapToImageDemo.hpp | ||
* | ||
* Created on: Oktober 19, 2020 | ||
* Author: Magnus Gärtner | ||
* Institute: ETH Zurich, ANYbotics | ||
* | ||
*/ | ||
|
||
#pragma once | ||
|
||
// ROS | ||
#include <ros/ros.h> | ||
|
||
#include <string> | ||
|
||
#include <grid_map_msgs/GridMap.h> | ||
#include <grid_map_ros/grid_map_ros.hpp> | ||
|
||
namespace grid_map_demos { | ||
|
||
/*! | ||
* Saves an ElevationMapping layer as image. | ||
*/ | ||
class GridMapToImageDemo { | ||
public: | ||
/*! | ||
* Constructor. | ||
* @param nodeHandle the ROS node handle. | ||
*/ | ||
GridMapToImageDemo(ros::NodeHandle& nodeHandle); | ||
|
||
/*! | ||
* Destructor. | ||
*/ | ||
virtual ~GridMapToImageDemo(); | ||
|
||
private: | ||
/*! | ||
* @brief Reads and verifies the ROS parameters. | ||
*/ | ||
void readParameters(); | ||
|
||
/** | ||
* @brief The callback receiving the grid map. | ||
* It will convert the elevation layer into a png image and save it to the specified location. Afterwards the node will terminate. | ||
* @param msg the recieved grid map to save to a file. | ||
*/ | ||
void gridMapCallback(const grid_map_msgs::GridMap& msg); | ||
|
||
//! ROS nodehandle. | ||
ros::NodeHandle& nodeHandle_; | ||
|
||
//! GridMap subscriber | ||
ros::Subscriber gridMapSubscriber_; | ||
|
||
//! Name of the grid map topic. | ||
std::string gridMapTopic_; | ||
|
||
//! Path where to store the image. | ||
std::string filePath_; | ||
}; | ||
|
||
} // namespace grid_map_demos |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
<?xml version="1.0"?> | ||
<package format="2"> | ||
<name>grid_map_demos</name> | ||
<version>1.6.2</version> | ||
<version>1.6.3</version> | ||
<description>Demo nodes to demonstrate the usage of the grid map library.</description> | ||
<maintainer email="[email protected]">Maximilian Wulf</maintainer> | ||
<maintainer email="[email protected]">Yoshua Nava</maintainer> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* GridMapToImageDemo.cpp | ||
* | ||
* Created on: October 19, 2020 | ||
* Author: Magnus Gärtner | ||
* Institute: ETH Zurich, ANYbotics | ||
*/ | ||
|
||
#include "grid_map_demos/GridmapToImageDemo.hpp" | ||
|
||
#include <image_transport/image_transport.h> | ||
#include <opencv2/imgcodecs.hpp> | ||
|
||
namespace grid_map_demos { | ||
|
||
GridMapToImageDemo::GridMapToImageDemo(ros::NodeHandle& nodeHandle) | ||
: nodeHandle_(nodeHandle) | ||
{ | ||
readParameters(); | ||
gridMapSubscriber_ = nodeHandle_.subscribe(gridMapTopic_, 1, &GridMapToImageDemo::gridMapCallback, this); | ||
ROS_ERROR("Subscribed to %s", nodeHandle_.resolveName(gridMapTopic_).c_str()); | ||
} | ||
|
||
GridMapToImageDemo::~GridMapToImageDemo()=default; | ||
|
||
void GridMapToImageDemo::readParameters() | ||
{ | ||
nodeHandle_.param("grid_map_topic", gridMapTopic_, std::string("/grid_map")); | ||
nodeHandle_.param("file", filePath_, std::string("~/.ros/grid_map.png")); | ||
} | ||
|
||
void GridMapToImageDemo::gridMapCallback(const grid_map_msgs::GridMap& msg) | ||
{ | ||
ROS_INFO("Saving map received from: %s to file %s.", nodeHandle_.resolveName(gridMapTopic_).c_str(), filePath_.c_str()); | ||
grid_map::GridMap map; | ||
cv_bridge::CvImage image; | ||
grid_map::GridMapRosConverter::fromMessage(msg, map, {"elevation"}); | ||
grid_map::GridMapRosConverter::toCvImage(map,"elevation", sensor_msgs::image_encodings::MONO8, image); | ||
bool success = cv::imwrite(filePath_.c_str(),image.image, {cv::IMWRITE_PNG_STRATEGY_DEFAULT}); | ||
ROS_INFO("Success writing image: %s", success?"true":"false"); | ||
ros::shutdown(); | ||
} | ||
|
||
} /* namespace */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* grid_map_to_image_demo.cpp | ||
* | ||
* Created on: October 19, 2020 | ||
* Author: Magnus Gärtner | ||
* Institute: ETH Zurich, ANYbotics | ||
*/ | ||
|
||
#include "grid_map_demos/GridmapToImageDemo.hpp" | ||
|
||
#include <ros/ros.h> | ||
|
||
/* | ||
* Usage: | ||
* $ rosrun grid_map_demos grid_map_to_image_demo _grid_map_topic:=/grid_map _file:=/home/$USER/Desktop/grid_map_image.png | ||
*/ | ||
int main(int argc, char** argv) | ||
{ | ||
// Initialize node and publisher. | ||
ros::init(argc, argv, "grid_map_to_image_demo"); | ||
ros::NodeHandle nh("~"); | ||
grid_map_demos::GridMapToImageDemo gridMapToImageDemo(nh); | ||
|
||
ros::spin(); | ||
return EXIT_SUCCESS; | ||
} |