moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
save_to_warehouse.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 <rclcpp/executors.hpp>
38#include <rclcpp/experimental/buffers/intra_process_buffer.hpp>
39#include <rclcpp/logger.hpp>
40#include <rclcpp/logging.hpp>
41#include <rclcpp/node.hpp>
42#include <rclcpp/node_options.hpp>
43#include <rclcpp/version.h>
44#if RCLCPP_VERSION_GTE(20, 0, 0)
45#include <rclcpp/event_handler.hpp>
46#else
47#include <rclcpp/qos_event.hpp>
48#endif
49#include <rclcpp/subscription.hpp>
50#include <rclcpp/utilities.hpp>
51
57#include <boost/program_options/cmdline.hpp>
58#include <boost/program_options/options_description.hpp>
59#include <boost/program_options/parsers.hpp>
60#include <boost/program_options/variables_map.hpp>
61#include <fmt/format.h>
62// For Rolling, Kilted, and newer
63#if RCLCPP_VERSION_GTE(29, 6, 0)
64#include <tf2_ros/transform_listener.hpp>
65// For Jazzy and older
66#else
67#include <tf2_ros/transform_listener.h>
68#endif
69
70rclcpp::Logger getLogger()
71{
72 return moveit::getLogger("moveit.ros.save_to_warehouse");
73}
74
75static const std::string ROBOT_DESCRIPTION = "robot_description";
76
78{
79 RCLCPP_INFO(getLogger(), "Received an update to the planning scene...");
80
81 if (!psm.getPlanningScene()->getName().empty())
82 {
83 if (!pss.hasPlanningScene(psm.getPlanningScene()->getName()))
84 {
85 moveit_msgs::msg::PlanningScene psmsg;
86 psm.getPlanningScene()->getPlanningSceneMsg(psmsg);
87 pss.addPlanningScene(psmsg);
88 }
89 else
90 {
91 RCLCPP_INFO(getLogger(), "Scene '%s' was previously added. Not adding again.",
92 psm.getPlanningScene()->getName().c_str());
93 }
94 }
95 else
96 {
97 RCLCPP_INFO(getLogger(), "Scene name is empty. Not saving.");
98 }
99}
100
101void onMotionPlanRequest(const moveit_msgs::msg::MotionPlanRequest& req,
103{
104 if (psm.getPlanningScene()->getName().empty())
105 {
106 RCLCPP_INFO(getLogger(), "Scene name is empty. Not saving planning request.");
107 return;
108 }
109 pss.addPlanningQuery(req, psm.getPlanningScene()->getName());
110}
111
112void onConstraints(const moveit_msgs::msg::Constraints& msg, moveit_warehouse::ConstraintsStorage& cs)
113{
114 if (msg.name.empty())
115 {
116 RCLCPP_INFO(getLogger(), "No name specified for constraints. Not saving.");
117 return;
118 }
119
120 if (cs.hasConstraints(msg.name))
121 {
122 RCLCPP_INFO(getLogger(), "Replacing constraints '%s'", msg.name.c_str());
123 cs.removeConstraints(msg.name);
124 cs.addConstraints(msg);
125 }
126 else
127 {
128 RCLCPP_INFO(getLogger(), "Adding constraints '%s'", msg.name.c_str());
129 cs.addConstraints(msg);
130 }
131}
132
133void onRobotState(const moveit_msgs::msg::RobotState& msg, moveit_warehouse::RobotStateStorage& rs)
134{
135 std::vector<std::string> names;
136 rs.getKnownRobotStates(names);
137 std::set<std::string> names_set(names.begin(), names.end());
138 std::size_t n = names.size();
139 while (names_set.find("S" + std::to_string(n)) != names_set.end())
140 n++;
141 std::string name = "S" + std::to_string(n);
142 RCLCPP_INFO(getLogger(), "Adding robot state '%s'", name.c_str());
143 rs.addRobotState(msg, name);
144}
145
146int main(int argc, char** argv)
147{
148 rclcpp::init(argc, argv);
149 rclcpp::NodeOptions node_options;
150 node_options.allow_undeclared_parameters(true);
151 node_options.automatically_declare_parameters_from_overrides(true);
152 rclcpp::Node::SharedPtr node = rclcpp::Node::make_shared("save_to_warehouse", node_options);
153 moveit::setNodeLoggerName(node->get_name());
154
155 boost::program_options::options_description desc;
156 desc.add_options()("help", "Show help message")("host", boost::program_options::value<std::string>(),
157 "Host for the "
158 "DB.")("port", boost::program_options::value<std::size_t>(),
159 "Port for the DB.");
160
161 boost::program_options::variables_map vm;
162 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
163 boost::program_options::notify(vm);
164
165 if (vm.count("help"))
166 {
167 std::cout << desc << '\n';
168 return 1;
169 }
170 // Set up db
171 warehouse_ros::DatabaseConnection::Ptr conn = moveit_warehouse::loadDatabase(node);
172 if (vm.count("host") && vm.count("port"))
173 conn->setParams(vm["host"].as<std::string>(), vm["port"].as<std::size_t>());
174 if (!conn->connect())
175 return 1;
176
177 planning_scene_monitor::PlanningSceneMonitor psm(node, ROBOT_DESCRIPTION);
178 if (!psm.getPlanningScene())
179 {
180 RCLCPP_ERROR(node->get_logger(), "Unable to initialize PlanningSceneMonitor");
181 return 1;
182 }
183
184 psm.startSceneMonitor();
189 std::vector<std::string> names;
190 pss.getPlanningSceneNames(names);
191 if (names.empty())
192 {
193 RCLCPP_INFO(node->get_logger(), "There are no previously stored scenes");
194 }
195 else
196 {
197 RCLCPP_INFO(node->get_logger(), "Previously stored scenes:");
198 for (const std::string& name : names)
199 RCLCPP_INFO(node->get_logger(), " * %s", name.c_str());
200 }
201 cs.getKnownConstraints(names);
202 if (names.empty())
203 {
204 RCLCPP_INFO(node->get_logger(), "There are no previously stored constraints");
205 }
206 else
207 {
208 RCLCPP_INFO(node->get_logger(), "Previously stored constraints:");
209 for (const std::string& name : names)
210 RCLCPP_INFO(node->get_logger(), " * %s", name.c_str());
211 }
212 rs.getKnownRobotStates(names);
213 if (names.empty())
214 {
215 RCLCPP_INFO(node->get_logger(), "There are no previously stored robot states");
216 }
217 else
218 {
219 RCLCPP_INFO(node->get_logger(), "Previously stored robot states:");
220 for (const std::string& name : names)
221 RCLCPP_INFO(node->get_logger(), " * %s", name.c_str());
222 }
223
224 psm.addUpdateCallback([&](auto&&) { return onSceneUpdate(psm, pss); });
225
226 auto mplan_req_sub = node->create_subscription<moveit_msgs::msg::MotionPlanRequest>(
227 "motion_plan_request", rclcpp::SystemDefaultsQoS(),
228 [&](const moveit_msgs::msg::MotionPlanRequest& msg) { onMotionPlanRequest(msg, psm, pss); });
229 auto constr_sub = node->create_subscription<moveit_msgs::msg::Constraints>(
230 "constraints", rclcpp::SystemDefaultsQoS(),
231 [&](const moveit_msgs::msg::Constraints& msg) { onConstraints(msg, cs); });
232 auto state_sub = node->create_subscription<moveit_msgs::msg::RobotState>(
233 "robot_state", rclcpp::SystemDefaultsQoS(),
234 [&](const moveit_msgs::msg::RobotState& msg) { onRobotState(msg, rs); });
235
236 std::vector<std::string> topics;
237 psm.getMonitoredTopics(topics);
238 RCLCPP_INFO_STREAM(node->get_logger(),
239 "Listening for scene updates on topics " << fmt::format("{}", fmt::join(topics, ", ")));
240 RCLCPP_INFO_STREAM(node->get_logger(), "Listening for planning requests on topic " << mplan_req_sub->get_topic_name());
241 RCLCPP_INFO_STREAM(node->get_logger(), "Listening for named constraints on topic " << constr_sub->get_topic_name());
242 RCLCPP_INFO_STREAM(node->get_logger(), "Listening for states on topic " << state_sub->get_topic_name());
243
244 rclcpp::spin(node);
245 return 0;
246}
void removeConstraints(const std::string &name, const std::string &robot="", const std::string &group="")
bool hasConstraints(const std::string &name, const std::string &robot="", const std::string &group="") const
void getKnownConstraints(std::vector< std::string > &names, const std::string &robot="", const std::string &group="") const
void addConstraints(const moveit_msgs::msg::Constraints &msg, const std::string &robot="", const std::string &group="")
void addPlanningQuery(const moveit_msgs::msg::MotionPlanRequest &planning_query, const std::string &scene_name, const std::string &query_name="")
void addPlanningScene(const moveit_msgs::msg::PlanningScene &scene)
void getPlanningSceneNames(std::vector< std::string > &names) const
bool hasPlanningScene(const std::string &name) const
void getKnownRobotStates(std::vector< std::string > &names, const std::string &robot="") const
void addRobotState(const moveit_msgs::msg::RobotState &msg, const std::string &name, const std::string &robot="")
PlanningSceneMonitor Subscribes to the topic planning_scene.
void addUpdateCallback(const std::function< void(SceneUpdateType)> &fn)
Add a function to be called when an update to the scene is received.
void getMonitoredTopics(std::vector< std::string > &topics) const
Get the topic names that the monitor is listening to.
void startSceneMonitor(const std::string &scene_topic=DEFAULT_PLANNING_SCENE_TOPIC)
Start the scene monitor (ROS topic-based).
void startWorldGeometryMonitor(const std::string &collision_objects_topic=DEFAULT_COLLISION_OBJECT_TOPIC, const std::string &planning_scene_world_topic=DEFAULT_PLANNING_SCENE_WORLD_TOPIC, const bool load_octomap_monitor=true)
Start the OccupancyMapMonitor and listening for:
const planning_scene::PlanningScenePtr & getPlanningScene()
Avoid this function! Returns an unsafe pointer to the current planning scene.
warehouse_ros::DatabaseConnection::Ptr loadDatabase(const rclcpp::Node::SharedPtr &node)
Load a database connection.
rclcpp::Logger getLogger(const std::string &name)
Creates a namespaced logger.
Definition logger.cpp:106
void setNodeLoggerName(const std::string &name)
Call once after creating a node to initialize logging namespaces.
Definition logger.cpp:88
void onRobotState(const moveit_msgs::msg::RobotState &msg, moveit_warehouse::RobotStateStorage &rs)
int main(int argc, char **argv)
void onSceneUpdate(planning_scene_monitor::PlanningSceneMonitor &psm, moveit_warehouse::PlanningSceneStorage &pss)
rclcpp::Logger getLogger()
void onMotionPlanRequest(const moveit_msgs::msg::MotionPlanRequest &req, planning_scene_monitor::PlanningSceneMonitor &psm, moveit_warehouse::PlanningSceneStorage &pss)
void onConstraints(const moveit_msgs::msg::Constraints &msg, moveit_warehouse::ConstraintsStorage &cs)