42 #include <moveit/macros/class_forward.h>
43 #include <moveit/task_constructor/properties.h>
44 #include <moveit/task_constructor/cost_queue.h>
45 #include <moveit/task_constructor/utils.h>
46 #include <moveit_task_constructor_msgs/Solution.h>
47 #include <visualization_msgs/MarkerArray.h>
55 namespace planning_scene {
56 MOVEIT_CLASS_FORWARD(PlanningScene);
59 namespace robot_trajectory {
60 MOVEIT_CLASS_FORWARD(RobotTrajectory);
64 namespace task_constructor {
67 MOVEIT_CLASS_FORWARD(InterfaceState);
68 MOVEIT_CLASS_FORWARD(Interface);
69 MOVEIT_CLASS_FORWARD(Stage);
70 MOVEIT_CLASS_FORWARD(Introspection);
80 friend class ContainerBasePrivate;
89 static const char* colorForStatus(
unsigned int s) {
return STATUS_COLOR_[s]; }
95 struct Priority : std::tuple<Status, unsigned int, double>
97 Priority(
unsigned int depth,
double cost, Status status)
98 : std::tuple<Status, unsigned int, double>(status, depth, cost) {}
99 Priority(
unsigned int depth,
double cost) :
Priority(depth, cost, std::isfinite(cost) ? ENABLED : PRUNED) {}
103 inline Status status()
const {
return std::get<0>(*
this); }
104 inline bool enabled()
const {
return std::get<0>(*
this) == ENABLED; }
106 inline unsigned int depth()
const {
return std::get<1>(*
this); }
107 inline double cost()
const {
return std::get<2>(*
this); }
111 return Priority(depth() + other.depth(), cost() + other.cost(), std::min(status(), other.status()));
114 bool operator<(
const Priority& rhs)
const;
115 inline bool operator>(
const Priority& rhs)
const {
return rhs < *
this; }
116 inline bool operator<=(
const Priority& rhs)
const {
return !(rhs < *
this); }
117 inline bool operator>=(
const Priority& rhs)
const {
return !(*
this < rhs); }
119 using Solutions = std::deque<SolutionBase*>;
133 inline const planning_scene::PlanningSceneConstPtr& scene()
const {
return scene_; }
134 inline const Solutions& incomingTrajectories()
const {
return incoming_trajectories_; }
135 inline const Solutions& outgoingTrajectories()
const {
return outgoing_trajectories_; }
137 PropertyMap& properties() {
return properties_; }
138 const PropertyMap& properties()
const {
return properties_; }
143 inline const Priority& priority()
const {
return priority_; }
149 Interface* owner()
const {
return owner_; }
153 inline void addIncoming(SolutionBase* t) { incoming_trajectories_.push_back(t); }
154 inline void addOutgoing(SolutionBase* t) { outgoing_trajectories_.push_back(t); }
156 inline void setPriority(
const Priority& prio) { priority_ = prio; }
159 static const char* STATUS_COLOR_[];
160 planning_scene::PlanningSceneConstPtr scene_;
161 PropertyMap properties_;
163 Solutions incoming_trajectories_;
165 Solutions outgoing_trajectories_;
169 Interface* owner_ =
nullptr;
183 iterator(base_type::iterator other) : base_type::iterator(other) {}
185 InterfaceState& operator*()
const noexcept {
return *base_type::iterator::operator*(); }
187 InterfaceState* operator->()
const noexcept {
return base_type::iterator::operator*(); }
192 const_iterator(base_type::const_iterator other) : base_type::const_iterator(other) {}
193 const_iterator(base_type::iterator other) : base_type::const_iterator(other) {}
195 const InterfaceState& operator*()
const noexcept {
return *base_type::const_iterator::operator*(); }
197 const InterfaceState* operator->()
const noexcept {
return base_type::const_iterator::operator*(); }
200 enum Direction : uint8_t
205 enum Update : uint8_t
209 ALL = STATUS | PRIORITY,
211 using UpdateFlags = utils::Flags<Update>;
212 using NotifyFunction = std::function<void(iterator, UpdateFlags)>;
217 Interface::NotifyFunction old_;
225 Interface(
const NotifyFunction& notify = NotifyFunction());
235 inline bool notifyEnabled()
const {
return static_cast<bool>(notify_); }
238 NotifyFunction notify_;
242 using base_type::erase;
243 using base_type::insert;
246 using base_type::remove_if;
249 std::ostream& operator<<(std::ostream& os,
const InterfaceState::Priority& prio);
250 std::ostream& operator<<(std::ostream& os,
const Interface& interface);
251 std::ostream& operator<<(std::ostream& os, Interface::Direction dir);
254 template <
typename T>
255 size_t getIndex(
const T& container,
typename T::const_iterator search) {
257 for (
typename T::const_iterator it = container.begin(), end = container.end(); it != end; ++it, ++index)
265 class ContainerBasePrivate;
266 struct TmpSolutionContext;
270 friend ContainerBasePrivate;
284 assert(start_ ==
nullptr);
294 assert(end_ ==
nullptr);
299 inline const Stage* creator()
const {
return creator_; }
300 void setCreator(Stage* creator);
302 inline double cost()
const {
return cost_; }
303 void setCost(
double cost);
304 void markAsFailure(
const std::string& msg = std::string());
305 inline bool isFailure()
const {
return !std::isfinite(cost_); }
307 const std::string& comment()
const {
return comment_; }
308 void setComment(
const std::string& comment) { comment_ = comment; }
310 auto& markers() {
return markers_; }
311 const auto& markers()
const {
return markers_; }
314 void toMsg(moveit_task_constructor_msgs::Solution& solution, Introspection* introspection =
nullptr)
const;
316 virtual void appendTo(moveit_task_constructor_msgs::Solution& solution,
318 void fillInfo(moveit_task_constructor_msgs::SolutionInfo& info,
Introspection* introspection =
nullptr)
const;
327 SolutionBase(
Stage* creator =
nullptr,
double cost = 0.0, std::string comment =
"")
328 : creator_(creator), cost_(cost), comment_(std::move(comment)) {}
336 std::string comment_;
338 std::deque<visualization_msgs::Marker> markers_;
341 const InterfaceState* start_ =
nullptr;
342 const InterfaceState* end_ =
nullptr;
344 MOVEIT_CLASS_FORWARD(SolutionBase);
351 const robot_trajectory::RobotTrajectoryConstPtr& trajectory = robot_trajectory::RobotTrajectoryConstPtr(),
352 double cost = 0.0, std::string comment =
"")
353 :
SolutionBase(
nullptr, cost, std::move(comment)), trajectory_(trajectory) {}
355 robot_trajectory::RobotTrajectoryConstPtr trajectory()
const {
return trajectory_; }
356 void setTrajectory(
const robot_trajectory::RobotTrajectoryPtr& t) { trajectory_ = t; }
358 void appendTo(moveit_task_constructor_msgs::Solution& msg,
Introspection* introspection =
nullptr)
const override;
364 s.markAsFailure(msg);
370 robot_trajectory::RobotTrajectoryConstPtr trajectory_;
382 using container_type = std::vector<const SolutionBase*>;
386 :
SolutionBase(creator, cost), subsolutions_(std::move(subsolutions)) {}
391 void appendTo(moveit_task_constructor_msgs::Solution& msg,
Introspection* introspection)
const override;
395 const container_type& solutions()
const {
return subsolutions_; }
397 inline const InterfaceState* internalStart()
const {
return subsolutions_.front()->start(); }
398 inline const InterfaceState* internalEnd()
const {
return subsolutions_.back()->end(); }
402 container_type subsolutions_;
417 :
SolutionBase(creator, cost, std::move(comment)), wrapped_(wrapped) {}
422 void appendTo(moveit_task_constructor_msgs::Solution& solution,
427 const SolutionBase* wrapped()
const {
return wrapped_; }
434 template <Interface::Direction dir>
438 return solution.end();
441 inline const InterfaceState* state<Interface::BACKWARD>(
const SolutionBase& solution) {
442 return solution.start();
446 template <Interface::Direction dir>
447 const InterfaceState::Solutions& trajectories(
const InterfaceState& state);
449 inline const InterfaceState::Solutions& trajectories<Interface::FORWARD>(
const InterfaceState& state) {
450 return state.outgoingTrajectories();
453 inline const InterfaceState::Solutions& trajectories<Interface::BACKWARD>(
const InterfaceState& state) {
454 return state.incomingTrajectories();
458 template <Interface::Direction dir>
459 constexpr Interface::Direction opposite();
461 inline constexpr Interface::Direction opposite<Interface::FORWARD>() {
462 return Interface::BACKWARD;
465 inline constexpr Interface::Direction opposite<Interface::BACKWARD>() {
466 return Interface::FORWARD;
474 struct less<moveit::task_constructor::SolutionBase*>
Definition: cost_terms.h:56
InterfaceState(const planning_scene::PlanningScenePtr &ps)
create an InterfaceState from a planning scene
Definition: storage.cpp:56
void updatePriority(const InterfaceState::Priority &priority)
Update priority and call owner's notify() if possible.
Definition: storage.cpp:85
bool operator<(const InterfaceState &other) const
states are ordered by priority
Definition: storage.h:141
void updateStatus(Status status)
Update status, but keep current priority.
Definition: storage.cpp:96
Definition: storage.h:215
Definition: storage.h:190
Definition: storage.h:180
Definition: storage.h:174
void add(InterfaceState &state)
add a new InterfaceState
Definition: storage.cpp:103
void updatePriority(InterfaceState *state, const InterfaceState::Priority &priority)
update state's priority (and call notify_ if it really has changed)
Definition: storage.cpp:143
container_type remove(iterator it)
remove a state from the interface and return it as a one-element list
Definition: storage.cpp:136
Definition: introspection.h:66
abstract base class for solutions (primitive and sequences)
Definition: storage.h:269
void setEndState(const InterfaceState &state)
Definition: storage.h:293
bool operator<(const SolutionBase &other) const
order solutions by their cost
Definition: storage.h:324
virtual double computeCost(const CostTerm &cost, std::string &comment) const =0
required to dispatch to type-specific CostTerm methods via vtable
void setStartState(const InterfaceState &state)
Definition: storage.h:283
virtual void appendTo(moveit_task_constructor_msgs::Solution &solution, Introspection *introspection=nullptr) const =0
append this solution to Solution msg
void toMsg(moveit_task_constructor_msgs::Solution &solution, Introspection *introspection=nullptr) const
convert solution to message
Definition: storage.cpp:207
Definition: storage.h:380
double computeCost(const CostTerm &cost, std::string &comment) const override
required to dispatch to type-specific CostTerm methods via vtable
Definition: storage.cpp:283
void appendTo(moveit_task_constructor_msgs::Solution &msg, Introspection *introspection) const override
append all subsolutions to solution
Definition: storage.cpp:248
SubTrajectory connects interface states of ComputeStages.
Definition: storage.h:348
void appendTo(moveit_task_constructor_msgs::Solution &msg, Introspection *introspection=nullptr) const override
append this solution to Solution msg
Definition: storage.cpp:224
double computeCost(const CostTerm &cost, std::string &comment) const override
required to dispatch to type-specific CostTerm methods via vtable
Definition: storage.cpp:240
Definition: storage.h:414
double computeCost(const CostTerm &cost, std::string &comment) const override
required to dispatch to type-specific CostTerm methods via vtable
Definition: storage.cpp:297
void appendTo(moveit_task_constructor_msgs::Solution &solution, Introspection *introspection=nullptr) const override
append this solution to Solution msg
Definition: storage.cpp:287
ordered<ValueType> provides an adapter for a std::list to allow sorting.
Definition: cost_queue.h:30
iterator moveTo(iterator pos, container_type &other, iterator other_pos)
move element pos from this to other container, inserting before other_pos
Definition: cost_queue.h:114
iterator moveFrom(iterator pos, container_type &other)
move element pos from other container into this one (sorted)
Definition: cost_queue.h:119
Definition: stage.cpp:272