service_relay.cpp
Go to the documentation of this file.
1 
4 /*****************************************************************************
5 ** Includes
6 *****************************************************************************/
7 
8 #include <cstdlib>
9 #include <ecl/command_line.hpp>
10 #include <ros/ros.h>
12 #include <cost_map_msgs/GetCostMap.h>
13 #include <nav_msgs/OccupancyGrid.h>
14 #include <string>
15 
16 /*****************************************************************************
17 ** Main
18 *****************************************************************************/
19 
26 int main(int argc, char **argv) {
27  /****************************************
28  ** Parsing
29  ****************************************/
30  ecl::CmdLine cmd("Relays service provided cost maps to an occupancy grid publisher.");
31  ecl::ValueArg<float> lengthArg("l","length","Length of a side of the costmap subwindow.", false, 2, "float", cmd);
32  ecl::ValueArg<std::string> serviceNameArg("s","service_name","Service name to call.", false, "get_cost_map", "string", cmd);
33  ecl::ValueArg<float> rateArg("r","rate","Rate at which to relay (hz).", false, 1, "float", cmd);
34  ecl::ValueArg<std::string> layerNameArg("n","layer_name","Layer name to convert.", false, "obstacle_costs", "string", cmd);
35  cmd.parse(argc, argv);
36 
37  /****************************************
38  ** Ros
39  ****************************************/
40  ros::init(argc, argv, "cost_map_client");
41  ros::NodeHandle nodehandle("~");
42  bool persistent = true, latched = true;
43  std::cout << "Looking up service: " << serviceNameArg.getValue() << std::endl;
44  ros::ServiceClient get_cost_map = nodehandle.serviceClient<cost_map_msgs::GetCostMap>(serviceNameArg.getValue(), persistent);
45  if ( !get_cost_map.waitForExistence(ros::Duration(10.0)) ) {
46  ROS_ERROR_STREAM("Cost Map Service Relay : failed to find the GetCostMap service on " << nodehandle.resolveName(serviceNameArg.getValue(), true));
47  return EXIT_FAILURE;
48  }
49  ros::Publisher occupancy_grid_publisher = nodehandle.advertise<nav_msgs::OccupancyGrid>("occupancy_grid", 5, latched);
50 
51  /****************************************
52  ** Service Call/Relay Loop
53  ****************************************/
54  cost_map_msgs::GetCostMap srv;
55  srv.request.length_x = lengthArg.getValue();
56  srv.request.length_y = lengthArg.getValue();
57  std::string layer_name = layerNameArg.getValue();
58  ros::Rate rate(rateArg.getValue());
59  while ( ros::ok() ) {
60  if (!get_cost_map.call(srv))
61  {
62  // could be just that someone closed down the navigation source and will restart
63  // so quietly ignore, but do check proceed to check again for ros::ok()
64  continue;
65  }
66  nav_msgs::OccupancyGrid occupancy_grid_msg;
68  cost_map::fromMessage(srv.response.map, cost_map);
69  if ( cost_map.getLayers().size() == 1 ) {
70  layer_name = cost_map.getLayers()[0];
71  }
72  cost_map::toOccupancyGrid(cost_map, layer_name, occupancy_grid_msg);
73  occupancy_grid_publisher.publish(occupancy_grid_msg);
74  rate.sleep();
75  ros::spinOnce();
76  }
77  return EXIT_SUCCESS;
78 }
79 
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
void publish(const boost::shared_ptr< M > &message) const
int main(int argc, char **argv)
string cmd
std::string resolveName(const std::string &name, bool remap=true) const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
bool waitForExistence(ros::Duration timeout=ros::Duration(-1))
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void toOccupancyGrid(const cost_map::CostMap &cost_map, const std::string &layer, nav_msgs::OccupancyGrid &msg)
void parse(int argc, char **argv)
const std::vector< std::string > & getLayers() const
#define ROS_ERROR_STREAM(args)
ROSCPP_DECL void spinOnce()
bool fromMessage(const cost_map_msgs::CostMap &message, cost_map::CostMap &cost_map)


cost_map_visualisations
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:03:53