MTC
cost_terms.h
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2020, Hamburg University
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 Hamburg University 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 /* Authors: Michael 'v4hn' Goerner
36  Desc: Define implementations for general CostTerm's to use with Stage::setCostTerm()
37 */
38 
39 #pragma once
40 
41 #include <moveit/task_constructor/storage.h>
42 #include <moveit/task_constructor/utils.h>
43 #include <moveit_msgs/RobotState.h>
44 
45 namespace moveit {
46 namespace task_constructor {
47 
54 MOVEIT_CLASS_FORWARD(CostTerm);
55 class CostTerm
56 {
57 public:
58  CostTerm() = default;
59  CostTerm(std::nullptr_t) : CostTerm{} {}
60  virtual ~CostTerm() = default;
61 
62  virtual double operator()(const SubTrajectory& s, std::string& comment) const;
63  virtual double operator()(const SolutionSequence& s, std::string& comment) const;
64  virtual double operator()(const WrappedSolution& s, std::string& comment) const;
65 };
66 
71 {
72 public:
73  enum class Mode : uint8_t
74  {
75  AUTO /* TRAJECTORY, or START_INTERFACE if no trajectory is given */,
76  START_INTERFACE,
77  END_INTERFACE,
78  TRAJECTORY
79  };
80 
81  double operator()(const SolutionSequence& s, std::string& comment) const override;
82  double operator()(const WrappedSolution& s, std::string& comment) const override;
83 };
84 
86 {
87 public:
88  using SubTrajectorySignature = std::function<double(const SubTrajectory&, std::string&)>;
89  using SubTrajectoryShortSignature = std::function<double(const SubTrajectory&)>;
90 
91  // accept lambdas according to either signature above
92  template <typename Term, typename Signature = decltype(signatureMatcher(std::declval<Term>()))>
93  LambdaCostTerm(const Term& t) : LambdaCostTerm{ Signature{ t } } {}
94 
95  LambdaCostTerm(const SubTrajectorySignature& term);
96  LambdaCostTerm(const SubTrajectoryShortSignature& term);
97 
98  using TrajectoryCostTerm::operator();
99  double operator()(const SubTrajectory& s, std::string& comment) const override;
100 
101 protected:
102  SubTrajectorySignature term_;
103 
104 private:
105  template <typename T>
106  static auto signatureMatcher(const T& t) -> decltype(t(SubTrajectory{}), SubTrajectoryShortSignature{});
107  template <typename T>
108  static auto signatureMatcher(const T& t) -> decltype(t(SubTrajectory{}, std::string{}), SubTrajectorySignature{});
109 };
110 
111 namespace cost {
112 
114 class Constant : public CostTerm
115 {
116 public:
117  Constant(double c) : cost{ c } {};
118 
119  double operator()(const SubTrajectory& s, std::string& comment) const override;
120  double operator()(const SolutionSequence& s, std::string& comment) const override;
121  double operator()(const WrappedSolution& s, std::string& comment) const override;
122 
123  double cost;
124 };
125 
128 {
129 public:
131  PathLength() = default;
133  PathLength(std::vector<std::string> joints);
135  PathLength(std::map<std::string, double> j) : joints(std::move(j)) {}
136 
137  using TrajectoryCostTerm::operator();
138  double operator()(const SubTrajectory& s, std::string& comment) const override;
139 
140  std::map<std::string, double> joints; //< joint weights
141 };
142 
145 {
146 public:
147  DistanceToReference(const moveit_msgs::RobotState& ref, Mode m = Mode::AUTO,
148  std::map<std::string, double> w = std::map<std::string, double>());
149  DistanceToReference(const std::map<std::string, double>& ref, Mode m = Mode::AUTO,
150  std::map<std::string, double> w = std::map<std::string, double>());
151 
152  using TrajectoryCostTerm::operator();
153  double operator()(const SubTrajectory& s, std::string& comment) const override;
154 
155  moveit_msgs::RobotState reference;
156  std::map<std::string, double> weights;
157  Mode mode;
158 };
159 
162 {
163 public:
164  using TrajectoryCostTerm::operator();
165  double operator()(const SubTrajectory& s, std::string& comment) const override;
166 };
167 
170 {
171 public:
172  LinkMotion(std::string link_name);
173 
174  std::string link_name;
175 
176  using TrajectoryCostTerm::operator();
177  double operator()(const SubTrajectory& s, std::string& comment) const override;
178 };
179 
189 {
190 public:
191  Clearance(bool with_world = true, bool cumulative = false, std::string group_property = "group",
192  Mode mode = Mode::AUTO);
193  bool with_world;
194  bool cumulative;
195  std::string group_property;
196 
197  Mode mode;
198 
199  std::function<double(double)> distance_to_cost;
200 
201  using TrajectoryCostTerm::operator();
202  double operator()(const SubTrajectory& s, std::string& comment) const override;
203 };
204 
205 } // namespace cost
206 } // namespace task_constructor
207 } // namespace moveit
Definition: cost_terms.h:56
Definition: cost_terms.h:86
SubTrajectory connects interface states of ComputeStages.
Definition: storage.h:348
Definition: cost_terms.h:189
add a constant cost to each solution
Definition: cost_terms.h:115
(weighted) joint-space distance to reference pose
Definition: cost_terms.h:145
Definition: cost_terms.h:170
trajectory length with optional weighting for different joints
Definition: cost_terms.h:128
PathLength()=default
By default, all joints are considered with same weight of 1.0.
PathLength(std::map< std::string, double > j)
Limit measurements to given joints and use given weighting.
Definition: cost_terms.h:135
execution duration of the whole trajectory
Definition: cost_terms.h:162