geotiff_node.cpp
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2011, Stefan Kohlbrecher, TU Darmstadt
3 // All rights reserved.
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Simulation, Systems Optimization and Robotics
13 // group, TU Darmstadt nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without
15 // specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //=================================================================================================
28 
29 #include <cstdio>
30 #include <ros/ros.h>
31 #include <ros/console.h>
32 #include <pluginlib/class_loader.h>
33 
34 #include <boost/algorithm/string.hpp>
35 
36 
37 #include "nav_msgs/GetMap.h"
38 #include "std_msgs/String.h"
39 #include "geometry_msgs/Quaternion.h"
40 
41 #include <Eigen/Geometry>
42 
43 #include <QtGui/QApplication>
44 
46 
49 
50 #include <hector_nav_msgs/GetRobotTrajectory.h>
51 
52 using namespace std;
53 
54 namespace hector_geotiff{
55 
60 {
61 public:
63  : geotiff_writer_(false)
64  , pn_("~")
65  , plugin_loader_(0)
66  , running_saved_map_num_(0)
67  {
68  pn_.param("map_file_path", p_map_file_path_, std::string("."));
69  geotiff_writer_.setMapFilePath(p_map_file_path_);
70  geotiff_writer_.setUseUtcTimeSuffix(true);
71 
72  pn_.param("map_file_base_name", p_map_file_base_name_, std::string());
73 
74  pn_.param("draw_background_checkerboard", p_draw_background_checkerboard_, true);
75  pn_.param("draw_free_space_grid", p_draw_free_space_grid_, true);
76 
77  sys_cmd_sub_ = n_.subscribe("syscommand", 1, &MapGenerator::sysCmdCallback, this);
78 
79  map_service_client_ = n_.serviceClient<nav_msgs::GetMap>("map");
80  //object_service_client_ = n_.serviceClient<worldmodel_msgs::GetObjectModel>("worldmodel/get_object_model");
81  path_service_client_ = n_.serviceClient<hector_nav_msgs::GetRobotTrajectory>("trajectory");
82 
83 
84  double p_geotiff_save_period = 0.0;
85  pn_.param("geotiff_save_period", p_geotiff_save_period, 0.0);
86 
87  if(p_geotiff_save_period > 0.0){
88  //ros::Timer timer = pn_.createTimer(ros::Duration(p_geotiff_save_period), &MapGenerator::timerSaveGeotiffCallback, false);
89  //publish_trajectory_timer_ = private_nh.createTimer(ros::Duration(1.0 / p_trajectory_publish_rate_), &PathContainer::publishTrajectoryTimerCallback, this, false);
90  map_save_timer_ = pn_.createTimer(ros::Duration(p_geotiff_save_period), &MapGenerator::timerSaveGeotiffCallback, this, false );
91  }
92 
93 
94  pn_.param("plugins", p_plugin_list_, std::string(""));
95 
96  std::vector<std::string> plugin_list;
97  boost::algorithm::split(plugin_list, p_plugin_list_, boost::is_any_of("\t "));
98 
99  //We always have at least one element containing "" in the string list
100  if ((plugin_list.size() > 0) && (plugin_list[0].length() > 0)){
101  plugin_loader_ = new pluginlib::ClassLoader<hector_geotiff::MapWriterPluginInterface>("hector_geotiff", "hector_geotiff::MapWriterPluginInterface");
102 
103  for (size_t i = 0; i < plugin_list.size(); ++i){
104  try
105  {
106  boost::shared_ptr<hector_geotiff::MapWriterPluginInterface> tmp (plugin_loader_->createInstance(plugin_list[i]));
107  tmp->initialize(plugin_loader_->getName(plugin_list[i]));
108  plugin_vector_.push_back(tmp);
109  }
111  {
112  ROS_ERROR("The plugin failed to load for some reason. Error: %s", ex.what());
113  }
114  }
115  }else{
116  ROS_INFO("No plugins loaded for geotiff node");
117  }
118 
119  ROS_INFO("Geotiff node started");
120  }
121 
123  {
124  if (plugin_loader_){
125  delete plugin_loader_;
126  }
127  }
128 
130  {
131  ros::Time start_time (ros::Time::now());
132 
133  std::stringstream ssStream;
134 
135  nav_msgs::GetMap srv_map;
136  if (map_service_client_.call(srv_map))
137  {
138  ROS_INFO("GeotiffNode: Map service called successfully");
139  const nav_msgs::OccupancyGrid& map (srv_map.response.map);
140 
141  std::string map_file_name = p_map_file_base_name_;
142  std::string competition_name;
143  std::string team_name;
144  std::string mission_name;
145  std::string postfix;
146  if (n_.getParamCached("/competition", competition_name) && !competition_name.empty()) map_file_name = map_file_name + "_" + competition_name;
147  if (n_.getParamCached("/team", team_name) && !team_name.empty()) map_file_name = map_file_name + "_" + team_name;
148  if (n_.getParamCached("/mission", mission_name) && !mission_name.empty()) map_file_name = map_file_name + "_" + mission_name;
149  if (pn_.getParamCached("map_file_postfix", postfix) && !postfix.empty()) map_file_name = map_file_name + "_" + postfix;
150  if (map_file_name.substr(0, 1) == "_") map_file_name = map_file_name.substr(1);
151  if (map_file_name.empty()) map_file_name = "GeoTiffMap";
152  geotiff_writer_.setMapFileName(map_file_name);
153  bool transformSuccess = geotiff_writer_.setupTransforms(map);
154 
155  if(!transformSuccess){
156  ROS_INFO("Couldn't set map transform");
157  return;
158  }
159 
160  geotiff_writer_.setupImageSize();
161 
162  if (p_draw_background_checkerboard_){
163  geotiff_writer_.drawBackgroundCheckerboard();
164  }
165 
166  geotiff_writer_.drawMap(map, p_draw_free_space_grid_);
167  geotiff_writer_.drawCoords();
168 
169  //ROS_INFO("Sum: %ld", (long int)srv.response.sum);
170  }
171  else
172  {
173  ROS_ERROR("Failed to call map service");
174  return;
175  }
176 
177  for (size_t i = 0; i < plugin_vector_.size(); ++i){
178  plugin_vector_[i]->draw(&geotiff_writer_);
179  }
180 
184  /*
185  if (req_object_model_){
186  worldmodel_msgs::GetObjectModel srv_objects;
187  if (object_service_client_.call(srv_objects))
188  {
189  ROS_INFO("GeotiffNode: Object service called successfully");
190 
191  const worldmodel_msgs::ObjectModel& objects_model (srv_objects.response.model);
192 
193  size_t size = objects_model.objects.size();
194 
195 
196  unsigned int victim_num = 1;
197 
198  for (size_t i = 0; i < size; ++i){
199  const worldmodel_msgs::Object& object (objects_model.objects[i]);
200 
201  if (object.state.state == worldmodel_msgs::ObjectState::CONFIRMED){
202  geotiff_writer_.drawVictim(Eigen::Vector2f(object.pose.pose.position.x,object.pose.pose.position.y),victim_num);
203  victim_num++;
204  }
205  }
206  }
207  else
208  {
209  ROS_ERROR("Failed to call objects service");
210  }
211  }
212  */
213 
214  /*
215  hector_nav_msgs::GetRobotTrajectory srv_path;
216 
217  if (path_service_client_.call(srv_path))
218  {
219  ROS_INFO("GeotiffNode: Path service called successfully");
220 
221  std::vector<geometry_msgs::PoseStamped>& traj_vector (srv_path.response.trajectory.poses);
222 
223  size_t size = traj_vector.size();
224 
225  std::vector<Eigen::Vector2f> pointVec;
226  pointVec.resize(size);
227 
228  for (size_t i = 0; i < size; ++i){
229  const geometry_msgs::PoseStamped& pose (traj_vector[i]);
230 
231  pointVec[i] = Eigen::Vector2f(pose.pose.position.x, pose.pose.position.y);
232  }
233 
234  if (size > 0){
235  //Eigen::Vector3f startVec(pose_vector[0].x,pose_vector[0].y,pose_vector[0].z);
236  Eigen::Vector3f startVec(pointVec[0].x(),pointVec[0].y(),0.0f);
237  geotiff_writer_.drawPath(startVec, pointVec);
238  }
239  }
240  else
241  {
242  ROS_ERROR("Failed to call path service");
243  }
244  */
245 
246 
247  geotiff_writer_.writeGeotiffImage();
248  running_saved_map_num_++;
249 
250  ros::Duration elapsed_time (ros::Time::now() - start_time);
251 
252  ROS_INFO("GeoTiff created in %f seconds", elapsed_time.toSec());
253  }
254 
256  {
257  this->writeGeotiff();
258  }
259 
260  void sysCmdCallback(const std_msgs::String& sys_cmd)
261  {
262  if ( !(sys_cmd.data == "savegeotiff")){
263  return;
264  }
265 
266  this->writeGeotiff();
267  }
268 
269  std::string p_map_file_path_;
271  std::string p_plugin_list_;
274 
275  //double p_geotiff_save_period_;
276 
278 
279  ros::ServiceClient map_service_client_;// = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");
282 
284 
287 
288  std::vector<boost::shared_ptr<hector_geotiff::MapWriterPluginInterface> > plugin_vector_;
289 
291 
293 
295 };
296 
297 }
298 
299 int main(int argc, char** argv)
300 {
301  ros::init(argc, argv, "geotiff_node");
302 
304 
305  //ros::NodeHandle pn_;
306  //double p_geotiff_save_period = 60.0f;
307  //pn_.param("geotiff_save_period", p_geotiff_save_period, 60.0);
308  //ros::Timer timer = pn_.createTimer(ros::Duration(p_geotiff_save_period), &MapGenerator::timerSaveGeotiffCallback, &mg, false);
309 
310  ros::spin();
311 
312  return 0;
313 }
314 
void sysCmdCallback(const std_msgs::String &sys_cmd)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ros::ServiceClient map_service_client_
int main(int argc, char **argv)
std::vector< boost::shared_ptr< hector_geotiff::MapWriterPluginInterface > > plugin_vector_
void timerSaveGeotiffCallback(const ros::TimerEvent &e)
ros::ServiceClient object_service_client_
#define ROS_INFO(...)
ROSCPP_DECL void spin()
static Time now()
Map generation node.
#define ROS_ERROR(...)
ros::ServiceClient path_service_client_
pluginlib::ClassLoader< hector_geotiff::MapWriterPluginInterface > * plugin_loader_


hector_geotiff
Author(s): Stefan Kohlbrecher
autogenerated on Sun Nov 3 2019 03:18:38