MTC
task.h
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2017, Hamburg University
5  * Copyright (c) 2017, Bielefeld University
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Bielefeld University nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
36 /* Authors: Michael Goerner, Robert Haschke
37  Desc: User-level access to the whole Task constructor infrastructure
38 */
39 
40 #pragma once
41 
42 #include "container.h"
43 
44 #include <moveit/task_constructor/introspection.h>
45 #include <moveit_task_constructor_msgs/Solution.h>
46 
47 #include <moveit/macros/class_forward.h>
48 
49 #include <moveit_msgs/MoveItErrorCodes.h>
50 #include <moveit/utils/moveit_error_code.h>
51 
52 namespace moveit {
53 namespace core {
54 MOVEIT_CLASS_FORWARD(RobotModel);
55 MOVEIT_CLASS_FORWARD(RobotState);
56 } // namespace core
57 } // namespace moveit
58 
59 namespace moveit {
60 namespace task_constructor {
61 
62 MOVEIT_CLASS_FORWARD(Stage);
63 MOVEIT_CLASS_FORWARD(ContainerBase);
64 MOVEIT_CLASS_FORWARD(Task);
65 
66 class TaskPrivate;
72 class Task : protected WrapperBase
73 {
74 public:
75  PRIVATE_CLASS(Task)
77  using WrapperBase::operator[];
78 
79  Task(const std::string& ns = "", bool introspection = true,
80  ContainerBase::pointer&& container = std::make_unique<SerialContainer>("task pipeline"));
81  Task(Task&& other); // NOLINT(performance-noexcept-move-constructor)
82  Task& operator=(Task&& other); // NOLINT(performance-noexcept-move-constructor)
83  ~Task() override;
84 
85  const std::string& name() const { return stages()->name(); }
86  void setName(const std::string& name) { stages()->setName(name); }
87 
88  Stage* findChild(const std::string& name) const { return stages()->findChild(name); }
89  Stage* operator[](int index) const { return stages()->operator[](index); }
90 
91  const moveit::core::RobotModelConstPtr& getRobotModel() const;
93  void setRobotModel(const moveit::core::RobotModelConstPtr& robot_model);
95  void loadRobotModel(const std::string& robot_description = "robot_description");
96 
97  void add(Stage::pointer&& stage);
98  void insert(Stage::pointer&& stage, int before = -1) override;
99  void clear() final;
100 
102  void enableIntrospection(bool enable = true);
103  Introspection& introspection();
104 
105  using TaskCallback = std::function<void(const Task& t)>;
106  using TaskCallbackList = std::list<TaskCallback>;
108  TaskCallbackList::const_iterator addTaskCallback(TaskCallback&& cb);
110  void eraseTaskCallback(TaskCallbackList::const_iterator which);
111 
115  using WrapperBase::SolutionCallback;
116 
118  using WrapperBase::timeout;
119 
120  using WrapperBase::pruning;
122 
124  void reset() final;
126  void init();
127 
129  moveit::core::MoveItErrorCode plan(size_t max_solutions = 0);
131  void preempt();
132  void resetPreemptRequest();
134  moveit::core::MoveItErrorCode execute(const SolutionBase& s);
135 
137  void printState(std::ostream& os = std::cout) const;
138 
140  void explainFailure(std::ostream& os = std::cout) const override;
141 
142  size_t numSolutions() const { return solutions().size(); }
143  const ordered<SolutionBaseConstPtr>& solutions() const { return stages()->solutions(); }
144  const std::list<SolutionBaseConstPtr>& failures() const { return stages()->failures(); }
145 
147  void publishAllSolutions(bool wait = true);
148 
149  // +1 TODO: convenient access to arbitrary stage by name. traverse hierarchy using / separator?
152  const ContainerBase* stages() const;
153 
156  const PropertyMap& properties() const { return const_cast<Task*>(this)->properties(); }
157  void setProperty(const std::string& name, const boost::any& value);
159  inline void setProperty(const std::string& name, const char* value) { setProperty(name, std::string(value)); }
160 
161 protected:
162  bool canCompute() const override;
163  void compute() override;
164  void onNewSolution(const SolutionBase& s) override;
165 
166 private:
167  using WrapperBase::init;
168 };
169 
170 inline std::ostream& operator<<(std::ostream& os, const Task& task) {
171  task.printState(os);
172  return os;
173 }
174 } // namespace task_constructor
175 } // namespace moveit
Definition: container.h:49
void init(const moveit::core::RobotModelConstPtr &robot_model) override
void setPruning(bool pruning)
Explicitly enable/disable pruning.
Definition: container.h:55
Definition: introspection.h:66
Definition: properties.h:257
abstract base class for solutions (primitive and sequences)
Definition: storage.h:269
Definition: stage.h:147
void setTimeout(double timeout)
Definition: stage.h:194
void removeSolutionCallback(SolutionCallbackList::const_iterator which)
remove function callback
Definition: stage.cpp:408
SolutionCallbackList::const_iterator addSolutionCallback(SolutionCallback &&cb)
add function to be called for every newly found solution or failure
Definition: stage.cpp:403
void setCostTerm(const CostTermConstPtr &term)
Definition: stage.cpp:412
double timeout() const
timeout of stage per computation
Definition: stage.h:196
Definition: task.h:73
void explainFailure(std::ostream &os=std::cout) const override
print an explanation for a planning failure to os
Definition: task.cpp:336
void enableIntrospection(bool enable=true)
enable introspection publishing for use with rviz
Definition: task.cpp:159
ContainerBase * stages()
access stage tree
Definition: task.cpp:309
void preempt()
interrupt current planning
Definition: task.cpp:274
void insert(Stage::pointer &&stage, int before=-1) override
insertion is only allowed if children() is empty
Definition: task.cpp:150
void loadRobotModel(const std::string &robot_description="robot_description")
load robot model from given parameter
Definition: task.cpp:138
TaskCallbackList::const_iterator addTaskCallback(TaskCallback &&cb)
add function to be called after each top-level iteration
Definition: task.cpp:182
void eraseTaskCallback(TaskCallbackList::const_iterator which)
remove function callback
Definition: task.cpp:188
PropertyMap & properties()
properties access
Definition: task.cpp:317
void printState(std::ostream &os=std::cout) const
print current task state (number of found solutions and propagated states) to std::cout
Definition: task.cpp:332
void setRobotModel(const moveit::core::RobotModelConstPtr &robot_model)
setting the robot model also resets the task
Definition: task.cpp:127
moveit::core::MoveItErrorCode plan(size_t max_solutions=0)
reset, init scene (if not yet done), and init all stages, then start planning
Definition: task.cpp:243
moveit::core::MoveItErrorCode execute(const SolutionBase &s)
execute solution, return the result
Definition: task.cpp:282
void publishAllSolutions(bool wait=true)
publish all top-level solutions
Definition: task.cpp:297
void reset() final
reset all stages
Definition: task.cpp:192
void init()
initialize all stages with given scene
Definition: task.cpp:201
void onNewSolution(const SolutionBase &s) override
called by a (direct) child when a new solution becomes available
Definition: task.cpp:302
void setProperty(const std::string &name, const char *value)
overload: const char* values are stored as std::string
Definition: task.h:159
Definition: container.h:214
ordered<ValueType> provides an adapter for a std::list to allow sorting.
Definition: cost_queue.h:30