se2ez
ik.h
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #ifndef SE2EZ_CORE_IK_
4 #define SE2EZ_CORE_IK_
5 
6 #include <mutex>
7 
8 #include <kdl/tree.hpp>
9 #include <kdl/treefksolverpos_recursive.hpp>
10 #include <kdl/treeiksolverpos_nr_jl.hpp>
11 #include <kdl/treeiksolvervel_wdls.hpp>
12 
14 #include <se2ez/core/math.h>
15 
16 namespace se2ez
17 {
18  /** \cond IGNORE */
19  SE2EZ_CLASS_FORWARD(Robot)
20  SE2EZ_CLASS_FORWARD(State)
21  SE2EZ_CLASS_FORWARD(CollisionManager)
22  /** \endcond */
23 
24  /** \cond IGNORE */
25  SE2EZ_CLASS_FORWARD(IKSolver)
26  /** \endcond */
27 
28  /** \class se2ez::IKSolverPtr
29  \brief A shared pointer wrapper for se2ez::IKSolver. */
30 
31  /** \class se2ez::IKSolverConstPtr
32  \brief A const shared pointer wrapper for se2ez::IKSolver. */
33 
34  /** \brief Common components of IK solvers.
35  */
36  class IKSolver
37  {
38  public:
39  /** \brief A bitmask specification of what task components should this IK request satisfy.
40  */
41  enum Request
42  {
43  NONE = 0, ///< No components.
44  X_AXIS = 1 << 0, ///< X component.
45  Y_AXIS = 1 << 1, ///< Y component.
46  ROTATION = 1 << 2, ///< Rotation component.
47  POSITION = X_AXIS | Y_AXIS, ///< X- and Y-axis components.
48  ALL = POSITION | ROTATION ///< All components.
49  };
50 
51  // non-copyable
52  IKSolver(const IKSolver &) = delete;
53  IKSolver(IKSolver &&) = delete;
54 
55  /** \brief Sets the number of random restarts to use when solving a request
56  * \param[in] restarts The number of random restarts to use.
57  */
58  void setRestarts(unsigned int restarts);
59 
60  /** \brief Sets the weights on a specific joint in the robot for IK solving.
61  * \param[in] frame Frame to set weights for.
62  * \param[in] weights Weights to use for frame.
63  */
64  virtual void setJointWeight(const std::string &frame, const Eigen::VectorXd &weights);
65 
66  /** \brief Sets the weights for all joints in the robot for IK solving.
67  * \param[in] weights Weights to use.
68  */
69  virtual void setJointWeights(const Eigen::VectorXd &weights);
70 
71  /** \brief Sets lambda, damping constant in WDLS.
72  * \param[in] lambda Lambda to use.
73  */
74  virtual void setLambda(double lambda);
75 
76  protected:
77  /** \brief KDL specific indices for different components of the task matrix.
78  */
79  enum Index
80  {
81  POS_X = 0, ///< X Position
82  POS_Y = 1, ///< Y Position
83  POS_Z = 2, ///< Z Position, Ignored as we are 2D
84  ROT_X = 3, ///< X Rotation, Ignored as we are 2D
85  ROT_Y = 4, ///< Y Rotation, Ignored as we are 2D
86  ROT_Z = 5, ///< Z Rotation
87  };
88 
89  /** \brief Constructor. Protected so only derived classes can create this.
90  */
91  IKSolver(const RobotPtr &robot, unsigned int frames);
92 
93  /** \brief Sets the task matrix values for a task at index \e task according to a \e request.
94  * \param[in] task Index of task to set.
95  * \param[in] request Request to set.
96  */
97  void setRequest(unsigned int task, Request request);
98 
99  /** \brief Sets the specific \e index component of a \e task to \e active.
100  * \param[in] task Index of task to set.
101  * \param[in] index Index of component to set.
102  * \param[in] active Value to set component to.
103  */
104  void setTask(unsigned int task, Index index, bool active);
105 
106  /** \brief Sets a KDL array to random values between joint limits.
107  * \param[out] array Array to randomly set.
108  */
109  void setRandom(KDL::JntArray &array) const;
110 
111  const unsigned int n_; ///< Number of joint variables.
112  const unsigned int t_; ///< Number of tasks.
113  const RobotPtr robot_; ///< Reference to robot.
114 
115  const KDL::JntArray min_; ///< Joint minimums.
116  const KDL::JntArray max_; ///< Joint maximums.
117 
118  KDL::MatrixXd weights_; ///< Joint variable weights.
119  KDL::MatrixXd tasks_; ///< Task weights.
120  double lambda_{0.01}; ///< Lambda, WDLS constant.
121 
122  unsigned int restarts_{10}; ///< Number of random restarts.
123  };
124 
125  /** \cond IGNORE */
127  /** \endcond */
128 
129  /** \class se2ez::TreeIKPtr
130  \brief A shared pointer wrapper for se2ez::TreeIK. */
131 
132  /** \class se2ez::TreeIKConstPtr
133  \brief A const shared pointer wrapper for se2ez::TreeIK. */
134 
135  class TreeIK : public IKSolver
136  {
137  public:
138  /** \brief Constructor.
139  * \param[in] robot Robot to construct IK solver for.
140  * \param[in] frames Frames to use as task frames.
141  * \param[in] iterations Maximum number of iterations to use in solver.
142  * \param[in] eps Solution tolerance.
143  */
144  TreeIK(const RobotPtr &robot, const std::vector<std::string> &frames, unsigned int iterations = 50,
145  double eps = 1e-4);
146 
147  // non-copyable
148  TreeIK(const TreeIK &) = delete;
149  TreeIK(TreeIK &&) = delete;
150 
151  /** \brief Solve an IK request.
152  * \param[in,out] state State to set to result of IK query. Input value is used as first seed.
153  * \param[in] frames Map of frame name to desired values for query.
154  * \param[in] request Map of frame names to requested components. Uses ALL by default.
155  * \return True on success, false on failure.
156  */
157  bool solve(StatePtr state, const tf::EigenMap<std::string, Eigen::Isometry2d> &frames,
158  const std::map<std::string, Request> &request = {});
159 
160  /** \brief Sets the weights on a specific joint in the robot for IK solving.
161  * \param[in] frame Frame to set weights for.
162  * \param[in] weights Weights to use for frame.
163  */
164  void setJointWeight(const std::string &frame, const Eigen::VectorXd &weights) override;
165 
166  /** \brief Sets the weights for all joints in the robot for IK solving.
167  * \param[in] weights Weights to use.
168  */
169  void setJointWeights(const Eigen::VectorXd &weights) override;
170 
171  /** \brief Sets lambda, damping constant in WDLS.
172  * \param[in] lambda Lambda to use.
173  */
174  void setLambda(double lambda) override;
175 
176  protected:
177  /** \brief Sets current task matrix and goals according to request.
178  * \param[out] goals Goals for IK to set.
179  * \param[in] frames Desired position of task frames.
180  * \param[in] request Request to satisfy.
181  * \return True if possible, false on error.
182  */
183  bool setTask(KDL::Frames &goals, const tf::EigenMap<std::string, Eigen::Isometry2d> &frames,
184  const std::map<std::string, Request> &request);
185 
186  /** \brief Actually solve an IK query.
187  * \param[in] in Initial seed joint angles.
188  * \param[out] out Output joint angles.
189  * \param[in] goals Goals to satisfy.
190  * \return True on success false on failure.
191  */
192  virtual bool doIK(KDL::JntArray &in, KDL::JntArray &out, const KDL::Frames &goals);
193 
194  const std::map<std::string, unsigned int> frames_; ///< Mapping of task frame name to index.
195 
196  const KDL::Tree tree_; ///< KDL tree used for solving.
197  KDL::TreeFkSolverPos_recursive fk_; ///< Forward kinematics solver.
198  KDL::TreeIkSolverVel_wdls velIK_; ///< Inverse velocity solver.
199  KDL::TreeIkSolverPos_NR_JL posIK_; ///< Inverse position solver.
200 
201  std::mutex mutex_; ///< Thread mutex
202  };
203 
204  /** \cond IGNORE */
206  /** \endcond */
207 
208  /** \class se2ez::ChainIKPtr
209  \brief A shared pointer wrapper for se2ez::ChainIK. */
210 
211  /** \class se2ez::ChainIKConstPtr
212  \brief A const shared pointer wrapper for se2ez::ChainIK. */
213 
214  /** \brief Convenience wrapper around TreeIK for solving chains with a single tip frame.
215  */
216  class ChainIK : public TreeIK
217  {
218  public:
219  /** \brief Constructor.
220  * \param[in] robot Robot to construct IK solver for.
221  * \param[in] frame Frame to use as task frame.
222  * \param[in] iterations Maximum number of iterations to use in solver.
223  * \param[in] eps Solution tolerance.
224  */
225  ChainIK(const RobotPtr &robot, const std::string &frame, unsigned int iterations = 50,
226  double eps = 1e-4);
227 
228  /** \brief Destructor.
229  */
230  virtual ~ChainIK();
231 
232  // non-copyable
233  ChainIK(const ChainIK &) = delete;
234  ChainIK(ChainIK &&) = delete;
235 
236  /** \brief Solve an IK request.
237  * \param[in,out] state State to set to result of IK query. Input value is used as first seed.
238  * \param[in] frame Desired value to query.
239  * \param[in] request Requested components. ALL by default.
240  * \return True on success, false on failure.
241  */
242  bool solve(StatePtr state, const Eigen::Isometry2d &frame, Request request = ALL);
243 
244  private:
245  const std::string frame_; ///< Task frame.
246  };
247 
248  /** \cond IGNORE */
250  /** \endcond */
251 
252  /** \class se2ez::CollisionAwareTreeIKPtr
253  \brief A shared pointer wrapper for se2ez::CollisionAwareTreeIK. */
254 
255  /** \class se2ez::CollisionAwareTreeIKConstPtr
256  \brief A const shared pointer wrapper for se2ez::CollisionAwareTreeIK. */
257 
258  /** \brief A collision aware version of tree IK.
259  */
261  {
262  public:
263  /** \brief Constructor.
264  * \param[in] robot Robot to construct IK solver for.
265  * \param[in] frames Frames to use as task frames.
266  * \param[in] iterations Maximum number of iterations to use in solver.
267  * \param[in] eps Solution tolerance.
268  */
269  CollisionAwareTreeIK(const RobotPtr &robot, const std::vector<std::string> &frames,
270  unsigned int iterations = 50, double eps = 1e-4);
271 
272  // non-copyable
273  CollisionAwareTreeIK(const CollisionAwareTreeIK &) = delete;
275 
276  protected:
277  bool doIK(KDL::JntArray &in, KDL::JntArray &out, const KDL::Frames &goals) override;
278 
279  private:
280  CollisionManagerPtr cm_; ///< Collision Manager for IK.
281  StatePtr state_; ///< Work state.
282  };
283 
284  /** \cond IGNORE */
286  /** \endcond */
287 
288  /** \class se2ez::CollisionAwareChainIKPtr
289  \brief A shared pointer wrapper for se2ez::CollisionAwareChainIK. */
290 
291  /** \class se2ez::CollisionAwareChainIKConstPtr
292  \brief A const shared pointer wrapper for se2ez::CollisionAwareChainIK. */
293 
294  /** \brief Convenience wrapper around CollisionAwareTreeIK for solving chains with a single tip frame.
295  */
297  {
298  public:
299  /** \brief Constructor.
300  * \param[in] robot Robot to construct IK solver for.
301  * \param[in] frame Frame to use as task frame.
302  * \param[in] iterations Maximum number of iterations to use in solver.
303  * \param[in] eps Solution tolerance.
304  */
305  CollisionAwareChainIK(const RobotPtr &robot, const std::string &frame, unsigned int iterations = 50,
306  double eps = 1e-4);
307 
308  /** \brief Destructor.
309  */
310  virtual ~CollisionAwareChainIK();
311 
312  // non-copyable
315 
316  /** \brief Solve an IK request.
317  * \param[in,out] state State to set to result of IK query. Input value is used as first seed.
318  * \param[in] frame Desired value to query.
319  * \param[in] request Requested components. ALL by default.
320  * \return True on success, false on failure.
321  */
322  bool solve(StatePtr state, const Eigen::Isometry2d &frame, Request request = ALL);
323 
324  private:
325  const std::string frame_; ///< Task frame.
326  };
327 } // namespace se2ez
328 
329 #endif
std::mutex mutex_
Thread mutex.
Definition: ik.h:201
Index
KDL specific indices for different components of the task matrix.
Definition: ik.h:79
const KDL::JntArray min_
Joint minimums.
Definition: ik.h:115
A shared pointer wrapper for se2ez::State.
A collision aware version of tree IK.
Definition: ik.h:260
Convenience wrapper around TreeIK for solving chains with a single tip frame.
Definition: ik.h:216
const std::string frame_
Task frame.
Definition: ik.h:245
const std::string frame_
Task frame.
Definition: ik.h:325
CollisionManagerPtr cm_
Collision Manager for IK.
Definition: ik.h:280
const unsigned int t_
Number of tasks.
Definition: ik.h:112
const KDL::JntArray max_
Joint maximums.
Definition: ik.h:116
Convenience wrapper around CollisionAwareTreeIK for solving chains with a single tip frame...
Definition: ik.h:296
const KDL::Tree tree_
KDL tree used for solving.
Definition: ik.h:196
static const double eps
Definition: math.h:36
Common components of IK solvers.
Definition: ik.h:36
A shared pointer wrapper for se2ez::CollisionManager.
KDL::TreeIkSolverVel_wdls velIK_
Inverse velocity solver.
Definition: ik.h:198
A shared pointer wrapper for se2ez::Robot.
KDL::MatrixXd weights_
Joint variable weights.
Definition: ik.h:118
Main namespace.
Definition: collision.h:11
const unsigned int n_
Number of joint variables.
Definition: ik.h:111
KDL::TreeIkSolverPos_NR_JL posIK_
Inverse position solver.
Definition: ik.h:199
KDL::MatrixXd tasks_
Task weights.
Definition: ik.h:119
Request
A bitmask specification of what task components should this IK request satisfy.
Definition: ik.h:41
KDL::TreeFkSolverPos_recursive fk_
Forward kinematics solver.
Definition: ik.h:197
const RobotPtr robot_
Reference to robot.
Definition: ik.h:113
const std::map< std::string, unsigned int > frames_
Mapping of task frame name to index.
Definition: ik.h:194
#define SE2EZ_CLASS_FORWARD(C)
Definition: class_forward.h:9
StatePtr state_
Work state.
Definition: ik.h:281