service_relay.cpp
Go to the documentation of this file.
00001 
00004 /*****************************************************************************
00005 ** Includes
00006 *****************************************************************************/
00007 
00008 #include <cstdlib>
00009 #include <ecl/command_line.hpp>
00010 #include <ros/ros.h>
00011 #include <cost_map_ros/cost_map_ros.hpp>
00012 #include <cost_map_msgs/GetCostMap.h>
00013 #include <nav_msgs/OccupancyGrid.h>
00014 #include <string>
00015 
00016 /*****************************************************************************
00017 ** Main
00018 *****************************************************************************/
00019 
00026 int main(int argc, char **argv) {
00027   /****************************************
00028   ** Parsing
00029   ****************************************/
00030   ecl::CmdLine cmd("Relays service provided cost maps to an occupancy grid publisher.");
00031   ecl::ValueArg<float> lengthArg("l","length","Length of a side of the costmap subwindow.", false, 2, "float", cmd);
00032   ecl::ValueArg<std::string> serviceNameArg("s","service_name","Service name to call.", false, "get_cost_map", "string", cmd);
00033   ecl::ValueArg<float> rateArg("r","rate","Rate at which to relay (hz).", false, 1, "float", cmd);
00034   ecl::ValueArg<std::string> layerNameArg("n","layer_name","Layer name to convert.", false, "obstacle_costs", "string", cmd);
00035   cmd.parse(argc, argv);
00036 
00037   /****************************************
00038   ** Ros
00039   ****************************************/
00040   ros::init(argc, argv, "cost_map_client");
00041   ros::NodeHandle nodehandle("~");
00042   bool persistent = true, latched = true;
00043   std::cout << "Looking up service: " << serviceNameArg.getValue() << std::endl;
00044   ros::ServiceClient get_cost_map = nodehandle.serviceClient<cost_map_msgs::GetCostMap>(serviceNameArg.getValue(), persistent);
00045   if ( !get_cost_map.waitForExistence(ros::Duration(10.0)) ) {
00046     ROS_ERROR_STREAM("Cost Map Service Relay : failed to find the GetCostMap service on " << nodehandle.resolveName(serviceNameArg.getValue(), true));
00047     return EXIT_FAILURE;
00048   }
00049   ros::Publisher occupancy_grid_publisher = nodehandle.advertise<nav_msgs::OccupancyGrid>("occupancy_grid", 5, latched);
00050 
00051   /****************************************
00052   ** Service Call/Relay Loop
00053   ****************************************/
00054   cost_map_msgs::GetCostMap srv;
00055   srv.request.length_x = lengthArg.getValue();
00056   srv.request.length_y = lengthArg.getValue();
00057   std::string layer_name = layerNameArg.getValue();
00058   ros::Rate rate(rateArg.getValue());
00059   while ( ros::ok() ) {
00060     if (!get_cost_map.call(srv))
00061     {
00062       // could be just that someone closed down the navigation source and will restart
00063       // so quietly ignore, but do check proceed to check again for ros::ok()
00064       continue;
00065     }
00066     nav_msgs::OccupancyGrid occupancy_grid_msg;
00067     cost_map::CostMap cost_map;
00068     cost_map::fromMessage(srv.response.map, cost_map);
00069     if ( cost_map.getLayers().size() == 1 ) {
00070       layer_name = cost_map.getLayers()[0];
00071     }
00072     cost_map::toOccupancyGrid(cost_map, layer_name, occupancy_grid_msg);
00073     occupancy_grid_publisher.publish(occupancy_grid_msg);
00074     rate.sleep();
00075     ros::spinOnce();
00076   }
00077   return EXIT_SUCCESS;
00078 }
00079 


cost_map_visualisations
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 20:27:59