broadcast.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2012, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Ioan Sucan */
36 
40 
41 #include <boost/program_options/cmdline.hpp>
42 #include <boost/program_options/options_description.hpp>
43 #include <boost/program_options/parsers.hpp>
44 #include <boost/program_options/variables_map.hpp>
45 
46 #include <ros/ros.h>
47 
48 static const std::string PLANNING_SCENE_TOPIC = "planning_scene";
49 static const std::string PLANNING_REQUEST_TOPIC = "motion_plan_request";
50 static const std::string PLANNING_RESULTS_TOPIC = "motion_plan_results";
51 
52 static const std::string CONSTRAINTS_TOPIC = "constraints";
53 
54 static const std::string STATES_TOPIC = "robot_states";
55 
56 int main(int argc, char** argv)
57 {
58  ros::init(argc, argv, "publish_warehouse_data", ros::init_options::AnonymousName);
59 
60  // time to wait in between publishing messages
61  double delay = 0.001;
62 
63  boost::program_options::options_description desc;
64  desc.add_options()("help", "Show help message")("host", boost::program_options::value<std::string>(),
65  "Host for the "
66  "DB.")("port", boost::program_options::value<std::size_t>(),
67  "Port for the DB.")(
68  "scene", boost::program_options::value<std::string>(), "Name of scene to publish.")(
69  "planning_requests", "Also publish the planning requests that correspond to the scene")(
70  "planning_results", "Also publish the planning results that correspond to the scene")(
71  "constraint", boost::program_options::value<std::string>(), "Name of constraint to publish.")(
72  "state", boost::program_options::value<std::string>(),
73  "Name of the robot state to publish.")("delay", boost::program_options::value<double>()->default_value(delay),
74  "Time to wait in between publishing messages (s)");
75 
76  boost::program_options::variables_map vm;
77  boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
78  boost::program_options::notify(vm);
79 
80  if (vm.count("help") || (!vm.count("scene") && !vm.count("constraint") && !vm.count("state")))
81  {
82  std::cout << desc << std::endl;
83  return 1;
84  }
85  try
86  {
87  delay = vm["delay"].as<double>();
88  }
89  catch (...)
90  {
91  std::cout << desc << std::endl;
92  return 2;
93  }
94  // Set up db
96  if (vm.count("host") && vm.count("port"))
97  conn->setParams(vm["host"].as<std::string>(), vm["port"].as<std::size_t>());
98  if (!conn->connect())
99  return 1;
100 
102  spinner.start();
103 
104  ros::NodeHandle nh;
105  ros::Publisher pub_scene, pub_req, pub_res, pub_constr, pub_state;
106  ros::Duration wait_time(delay);
107 
108  // publish the scene
109  if (vm.count("scene"))
110  {
111  pub_scene = nh.advertise<moveit_msgs::PlanningScene>(PLANNING_SCENE_TOPIC, 10);
112  bool req = vm.count("planning_requests");
113  bool res = vm.count("planning_results");
114  if (req)
115  pub_req = nh.advertise<moveit_msgs::MotionPlanRequest>(PLANNING_REQUEST_TOPIC, 100);
116  if (res)
117  pub_res = nh.advertise<moveit_msgs::RobotTrajectory>(PLANNING_RESULTS_TOPIC, 100);
118 
120  ros::spinOnce();
121 
122  std::vector<std::string> scene_names;
123  pss.getPlanningSceneNames(vm["scene"].as<std::string>(), scene_names);
124 
125  for (std::size_t i = 0; i < scene_names.size(); ++i)
126  {
128  if (pss.getPlanningScene(pswm, scene_names[i]))
129  {
130  ROS_INFO("Publishing scene '%s'",
132  pub_scene.publish(static_cast<const moveit_msgs::PlanningScene&>(*pswm));
133  ros::spinOnce();
134 
135  // publish optional data associated to the scene
136  if (req || res)
137  {
138  std::vector<moveit_warehouse::MotionPlanRequestWithMetadata> planning_queries;
139  std::vector<std::string> query_names;
140  pss.getPlanningQueries(planning_queries, query_names, pswm->name);
141  ROS_INFO("There are %d planning queries associated to the scene", (int)planning_queries.size());
142  ros::WallDuration(0.5).sleep();
143  for (std::size_t i = 0; i < planning_queries.size(); ++i)
144  {
145  if (req)
146  {
147  ROS_INFO("Publishing query '%s'", query_names[i].c_str());
148  pub_req.publish(static_cast<const moveit_msgs::MotionPlanRequest&>(*planning_queries[i]));
149  ros::spinOnce();
150  }
151  if (res)
152  {
153  std::vector<moveit_warehouse::RobotTrajectoryWithMetadata> planning_results;
154  pss.getPlanningResults(planning_results, query_names[i], pswm->name);
155  for (std::size_t j = 0; j < planning_results.size(); ++j)
156  {
157  pub_res.publish(static_cast<const moveit_msgs::RobotTrajectory&>(*planning_results[j]));
158  ros::spinOnce();
159  }
160  }
161  }
162  }
163  wait_time.sleep();
164  }
165  }
166  }
167 
168  // publish constraints
169  if (vm.count("constraint"))
170  {
172  pub_constr = nh.advertise<moveit_msgs::Constraints>(CONSTRAINTS_TOPIC, 100);
173  std::vector<std::string> cnames;
174  cs.getKnownConstraints(vm["constraint"].as<std::string>(), cnames);
175 
176  for (std::size_t i = 0; i < cnames.size(); ++i)
177  {
179  if (cs.getConstraints(cwm, cnames[i]))
180  {
181  ROS_INFO("Publishing constraints '%s'",
183  pub_constr.publish(static_cast<const moveit_msgs::Constraints&>(*cwm));
184  ros::spinOnce();
185  wait_time.sleep();
186  }
187  }
188  }
189 
190  // publish constraints
191  if (vm.count("state"))
192  {
194  pub_state = nh.advertise<moveit_msgs::RobotState>(STATES_TOPIC, 100);
195  std::vector<std::string> rnames;
196  rs.getKnownRobotStates(vm["state"].as<std::string>(), rnames);
197 
198  for (std::size_t i = 0; i < rnames.size(); ++i)
199  {
201  if (rs.getRobotState(rswm, rnames[i]))
202  {
203  ROS_INFO("Publishing state '%s'", rswm->lookupString(moveit_warehouse::RobotStateStorage::STATE_NAME).c_str());
204  pub_state.publish(static_cast<const moveit_msgs::RobotState&>(*rswm));
205  ros::spinOnce();
206  wait_time.sleep();
207  }
208  }
209  }
210 
211  ros::WallDuration(1.0).sleep();
212  ROS_INFO("Done.");
213 
214  return 0;
215 }
static const std::string PLANNING_SCENE_TOPIC
Definition: broadcast.cpp:48
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int main(int argc, char **argv)
Definition: broadcast.cpp:56
void getKnownRobotStates(std::vector< std::string > &names, const std::string &robot="") const
static const std::string PLANNING_REQUEST_TOPIC
Definition: broadcast.cpp:49
static const std::string STATES_TOPIC
Definition: broadcast.cpp:54
void spinner()
void publish(const boost::shared_ptr< M > &message) const
warehouse_ros::DatabaseConnection::Ptr loadDatabase()
Load a database connection.
bool getConstraints(ConstraintsWithMetadata &msg_m, const std::string &name, const std::string &robot="", const std::string &group="") const
Get the constraints named name. Return false on failure.
#define ROS_INFO(...)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep() const
static const std::string CONSTRAINTS_TOPIC
Definition: broadcast.cpp:52
static const std::string CONSTRAINTS_ID_NAME
static const std::string STATE_NAME
Definition: state_storage.h:56
void getKnownConstraints(std::vector< std::string > &names, const std::string &robot="", const std::string &group="") const
bool sleep() const
ROSCPP_DECL void spinOnce()
bool getRobotState(RobotStateWithMetadata &msg_m, const std::string &name, const std::string &robot="") const
Get the constraints named name. Return false on failure.
static const std::string PLANNING_RESULTS_TOPIC
Definition: broadcast.cpp:50


warehouse
Author(s): Ioan Sucan
autogenerated on Fri Aug 20 2021 03:01:32