moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
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
37#include <cstdint>
42#include <boost/program_options/cmdline.hpp>
43#include <boost/program_options/options_description.hpp>
44#include <boost/program_options/parsers.hpp>
45#include <boost/program_options/variables_map.hpp>
46#include <rclcpp/executors/single_threaded_executor.hpp>
47#include <rclcpp/logger.hpp>
48#include <rclcpp/logging.hpp>
49#include <rclcpp/node.hpp>
50#include <rclcpp/node_options.hpp>
51#include <rclcpp/publisher.hpp>
52#include <rclcpp/version.h>
53#if RCLCPP_VERSION_GTE(20, 0, 0)
54#include <rclcpp/event_handler.hpp>
55#else
56#include <rclcpp/qos_event.hpp>
57#endif
58#include <rclcpp/rate.hpp>
59#include <rclcpp/utilities.hpp>
60
61static const std::string PLANNING_SCENE_TOPIC = "planning_scene";
62static const std::string PLANNING_REQUEST_TOPIC = "motion_plan_request";
63static const std::string PLANNING_RESULTS_TOPIC = "motion_plan_results";
64
65static const std::string CONSTRAINTS_TOPIC = "constraints";
66
67static const std::string STATES_TOPIC = "robot_states";
68
69using namespace std::chrono_literals;
70
71int main(int argc, char** argv)
72{
73 rclcpp::init(argc, argv);
74 rclcpp::NodeOptions node_options;
75 node_options.allow_undeclared_parameters(true);
76 node_options.automatically_declare_parameters_from_overrides(true);
77 rclcpp::Node::SharedPtr node = rclcpp::Node::make_shared("publish_warehouse_data", node_options);
78 moveit::setNodeLoggerName(node->get_name());
79
80 // time to wait in between publishing messages
81 double delay = 0.001;
82
83 // clang-format off
84 boost::program_options::options_description desc;
85 desc.add_options()("help", "Show help message")("host", boost::program_options::value<std::string>(),
86 "Host for the "
87 "DB.")("port", boost::program_options::value<std::size_t>(),
88 "Port for the DB.")(
89 "scene", boost::program_options::value<std::string>(), "Name of scene to publish.")(
90 "planning_requests", "Also publish the planning requests that correspond to the scene")(
91 "planning_results", "Also publish the planning results that correspond to the scene")(
92 "constraint", boost::program_options::value<std::string>(), "Name of constraint to publish.")(
93 "state", boost::program_options::value<std::string>(),
94 "Name of the robot state to publish.")("delay", boost::program_options::value<double>()->default_value(delay),
95 "Time to wait in between publishing messages (s)");
96 // clang-format on
97 boost::program_options::variables_map vm;
98 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
99 boost::program_options::notify(vm);
100
101 if (vm.count("help") || (!vm.count("scene") && !vm.count("constraint") && !vm.count("state")))
102 {
103 std::cout << desc << '\n';
104 return 1;
105 }
106 try
107 {
108 delay = vm["delay"].as<double>();
109 }
110 catch (...)
111 {
112 std::cout << desc << '\n';
113 return 2;
114 }
115 // Set up db
116 warehouse_ros::DatabaseConnection::Ptr conn = moveit_warehouse::loadDatabase(node);
117 if (vm.count("host") && vm.count("port"))
118 conn->setParams(vm["host"].as<std::string>(), vm["port"].as<std::size_t>());
119 if (!conn->connect())
120 return 1;
121
122 rclcpp::executors::SingleThreadedExecutor executor;
123 executor.add_node(node);
124
125 rclcpp::WallRate rate(static_cast<int64_t>(delay) * 1000ms);
126
127 // publish the scene
128 if (vm.count("scene"))
129 {
130 auto pub_scene = node->create_publisher<moveit_msgs::msg::PlanningScene>(PLANNING_SCENE_TOPIC, 10);
131 bool req = vm.count("planning_requests");
132 bool res = vm.count("planning_results");
133
135 executor.spin_once(0ns);
136
137 std::vector<std::string> scene_names;
138 pss.getPlanningSceneNames(vm["scene"].as<std::string>(), scene_names);
139
140 for (const std::string& scene_name : scene_names)
141 {
143 if (pss.getPlanningScene(pswm, scene_name))
144 {
145 RCLCPP_INFO(node->get_logger(), "Publishing scene '%s'",
147 pub_scene->publish(static_cast<const moveit_msgs::msg::PlanningScene&>(*pswm));
148 executor.spin_once(0ns);
149
150 // publish optional data associated to the scene
151 if (req || res)
152 {
153 auto pub_req = node->create_publisher<moveit_msgs::msg::MotionPlanRequest>(PLANNING_REQUEST_TOPIC, 100);
154 auto pub_res = node->create_publisher<moveit_msgs::msg::RobotTrajectory>(PLANNING_RESULTS_TOPIC, 100);
155 std::vector<moveit_warehouse::MotionPlanRequestWithMetadata> planning_queries;
156 std::vector<std::string> query_names;
157 pss.getPlanningQueries(planning_queries, query_names, pswm->name);
158 RCLCPP_INFO(node->get_logger(), "There are %d planning queries associated to the scene",
159 static_cast<int>(planning_queries.size()));
160 rclcpp::sleep_for(500ms);
161 for (std::size_t i = 0; i < planning_queries.size(); ++i)
162 {
163 if (req)
164 {
165 RCLCPP_INFO(node->get_logger(), "Publishing query '%s'", query_names[i].c_str());
166 pub_req->publish(static_cast<const moveit_msgs::msg::MotionPlanRequest&>(*planning_queries[i]));
167 executor.spin_once(0ns);
168 }
169 if (res)
170 {
171 std::vector<moveit_warehouse::RobotTrajectoryWithMetadata> planning_results;
172 pss.getPlanningResults(planning_results, query_names[i], pswm->name);
173 for (moveit_warehouse::RobotTrajectoryWithMetadata& planning_result : planning_results)
174 {
175 pub_res->publish(static_cast<const moveit_msgs::msg::RobotTrajectory&>(*planning_result));
176 executor.spin_once(0ns);
177 }
178 }
179 }
180 }
181 rate.sleep();
182 }
183 }
184 }
185
186 // publish constraints
187 if (vm.count("constraint"))
188 {
190 auto pub_constr = node->create_publisher<moveit_msgs::msg::Constraints>(CONSTRAINTS_TOPIC, 100);
191 std::vector<std::string> cnames;
192 cs.getKnownConstraints(vm["constraint"].as<std::string>(), cnames);
193
194 for (const std::string& cname : cnames)
195 {
197 if (cs.getConstraints(cwm, cname))
198 {
199 RCLCPP_INFO(node->get_logger(), "Publishing constraints '%s'",
201 pub_constr->publish(static_cast<const moveit_msgs::msg::Constraints&>(*cwm));
202 executor.spin_once(0ns);
203 rate.sleep();
204 }
205 }
206 }
207
208 // publish constraints
209 if (vm.count("state"))
210 {
212 auto pub_state = node->create_publisher<moveit_msgs::msg::RobotState>(STATES_TOPIC, 100);
213 std::vector<std::string> rnames;
214 rs.getKnownRobotStates(vm["state"].as<std::string>(), rnames);
215
216 for (const std::string& rname : rnames)
217 {
219 if (rs.getRobotState(rswm, rname))
220 {
221 RCLCPP_INFO(node->get_logger(), "Publishing state '%s'",
222 rswm->lookupString(moveit_warehouse::RobotStateStorage::STATE_NAME).c_str());
223 pub_state->publish(static_cast<const moveit_msgs::msg::RobotState&>(*rswm));
224 executor.spin_once(0ns);
225 rate.sleep();
226 }
227 }
228 }
229
230 rclcpp::sleep_for(1s);
231 RCLCPP_INFO(node->get_logger(), "Done.");
232
233 return 0;
234}
int main(int argc, char **argv)
Definition broadcast.cpp:71
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.
void getKnownConstraints(std::vector< std::string > &names, const std::string &robot="", const std::string &group="") const
void getPlanningResults(std::vector< RobotTrajectoryWithMetadata > &planning_results, const std::string &scene_name, const moveit_msgs::msg::MotionPlanRequest &planning_query) const
void getPlanningQueries(std::vector< MotionPlanRequestWithMetadata > &planning_queries, const std::string &scene_name) const
void getPlanningSceneNames(std::vector< std::string > &names) const
bool getPlanningScene(PlanningSceneWithMetadata &scene_m, const std::string &scene_name) const
Get the latest planning scene named scene_name.
void getKnownRobotStates(std::vector< std::string > &names, const std::string &robot="") const
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 STATE_NAME
warehouse_ros::MessageWithMetadata< moveit_msgs::msg::RobotState >::ConstPtr RobotStateWithMetadata
warehouse_ros::MessageWithMetadata< moveit_msgs::msg::RobotTrajectory >::ConstPtr RobotTrajectoryWithMetadata
warehouse_ros::MessageWithMetadata< moveit_msgs::msg::PlanningScene >::ConstPtr PlanningSceneWithMetadata
warehouse_ros::DatabaseConnection::Ptr loadDatabase(const rclcpp::Node::SharedPtr &node)
Load a database connection.
warehouse_ros::MessageWithMetadata< moveit_msgs::msg::Constraints >::ConstPtr ConstraintsWithMetadata
void setNodeLoggerName(const std::string &name)
Call once after creating a node to initialize logging namespaces.
Definition logger.cpp:73