robot_model.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "hebi.h"
4 #include "Eigen/Eigen"
5 #include "util.hpp"
6 #include <vector>
7 #include <memory>
8 
9 using namespace Eigen;
10 
11 namespace hebi {
12 namespace robot_model {
13 
14 using Matrix4dVector = std::vector<Matrix4d, Eigen::aligned_allocator<Eigen::Matrix4d> >;
15 using MatrixXdVector = std::vector<MatrixXd, Eigen::aligned_allocator<Eigen::MatrixXd> >;
16 // The result of an IK operation. More fields will be added to this structure
17 // in future API releases.
18 struct IKResult
19 {
20  HebiStatusCode result; // Success or failure
21 };
22 
23 class RobotModel;
24 
25 class Objective
26 {
27 friend RobotModel;
28 public:
29  virtual ~Objective() {}
30 protected:
31  virtual HebiStatusCode addObjective(HebiIKPtr ik) const = 0;
32 };
33 
35 {
36 public:
37  EndEffectorPositionObjective(const Eigen::Vector3d&);
38  EndEffectorPositionObjective(double weight, const Eigen::Vector3d&);
39 private:
40  HebiStatusCode addObjective(HebiIKPtr ik) const override;
41  double _weight, _x, _y, _z;
42 };
43 
44 class EndEffectorSO3Objective final : public Objective
45 {
46 public:
47  EndEffectorSO3Objective(const Eigen::Matrix3d&);
48  EndEffectorSO3Objective(double weight, const Eigen::Matrix3d&);
49 private:
50  HebiStatusCode addObjective(HebiIKPtr ik) const override;
51  double _weight;
52  const double _matrix[9];
53 };
54 
56 {
57 public:
58  EndEffectorTipAxisObjective(const Eigen::Vector3d&);
59  EndEffectorTipAxisObjective(double weight, const Eigen::Vector3d&);
60 private:
61  HebiStatusCode addObjective(HebiIKPtr ik) const override;
62  double _weight, _x, _y, _z;
63 };
64 
65 class JointLimitConstraint final : public Objective
66 {
67 public:
68  JointLimitConstraint(const Eigen::VectorXd& min_positions, const Eigen::VectorXd& max_positions);
69  JointLimitConstraint(double weight, const Eigen::VectorXd& min_positions, const Eigen::VectorXd& max_positions);
70 private:
71  HebiStatusCode addObjective(HebiIKPtr ik) const override;
72  double _weight;
73  Eigen::VectorXd _min_positions;
74  Eigen::VectorXd _max_positions;
75 public:
76  // Allow Eigen member variables:
78 };
79 
86 class RobotModel final
87 {
88  friend Objective;
89 
90  private:
95 
100  template<typename T>
101  HebiStatusCode addObjectives(HebiIKPtr ik, const T& objective) const
102  {
103  static_assert(std::is_convertible<T*, Objective*>::value,
104  "Must pass arguments of base type hebi::robot_model::Objective to the variable args of solveIK!");
105  return static_cast<const Objective*>(&objective)->addObjective(ik);
106  }
107 
111  template<typename T, typename ... Args>
112  HebiStatusCode addObjectives(HebiIKPtr ik, const T& objective, Args ... args) const
113  {
114  static_assert(std::is_convertible<T*, Objective*>::value,
115  "Must pass arguments of base type hebi::robot_model::Objective to the variable args of solveIK!");
116  auto res = static_cast<const Objective*>(&objective)->addObjective(ik);
117  if (res != HebiStatusSuccess)
118  return res;
119  return addObjectives(ik, args ...);
120  }
121 
125  static void setTranslate(Eigen::Matrix4d& matrix, double x, double y, double z);
126 
130  static void setRotX(Eigen::Matrix4d& matrix, double radians);
131 
135  static void setSphereInertia(Eigen::VectorXd& inertia, double mass, double radius);
136 
140  static void setRodXAxisInertia(Eigen::VectorXd& inertia, double mass, double length);
141 
145  bool tryAdd(HebiRobotModelElementPtr element, bool combine);
146 
147  public:
148  enum class ActuatorType
149  {
150  X5_1,
151  X5_4,
152  X5_9,
153  X8_3,
154  X8_9,
155  X8_16
156  };
157 
158  enum class LinkType
159  {
160  X5
161  };
162 
163  enum class BracketType
164  {
165  X5LightLeft,
166  X5LightRight,
167  X5HeavyLeftInside,
168  X5HeavyLeftOutside,
169  X5HeavyRightInside,
170  X5HeavyRightOutside
171  };
172 
177  RobotModel();
178 
183  ~RobotModel() noexcept;
184 
196  void setBaseFrame(const Eigen::Matrix4d& base_frame);
197 
202  Eigen::Matrix4d getBaseFrame() const;
203 
213  size_t getFrameCount(HebiFrameType frame_type) const;
214 
219  size_t getDoFCount() const;
220 
243  bool addRigidBody(
244  const Eigen::Matrix4d& com,
245  const Eigen::VectorXd& inertia,
246  double mass,
247  const Eigen::Matrix4d& output,
248  bool combine);
249 
259  bool addJoint(
260  HebiJointType joint_type,
261  bool combine);
262 
269  bool addActuator(ActuatorType actuator_type);
270 
285  bool addLink(LinkType link_type, double extension, double twist);
286 
293  bool addBracket(BracketType bracket_type);
294 
300  void getForwardKinematics(HebiFrameType, const Eigen::VectorXd& positions, Matrix4dVector& frames) const;
326  void getFK(HebiFrameType, const Eigen::VectorXd& positions, Matrix4dVector& frames) const;
327 
348  void getEndEffector(const Eigen::VectorXd& positions, Eigen::Matrix4d& transform) const;
349 
356  template<typename ... Args>
357  IKResult solveInverseKinematics(const Eigen::VectorXd& initial_positions, Eigen::VectorXd& result, Args ... args) const
358  {
359  return solveIK(initial_positions, result, args ...);
360  }
361 
376  template<typename ... Args>
377  IKResult solveIK(const Eigen::VectorXd& initial_positions,
378  Eigen::VectorXd& result,
379  Args ... objectives) const
380  {
381  // Create a HEBI C library IK object
382  auto ik = hebiIKCreate();
383 
384  // (Try) to add objectives to the IK object
385  IKResult res;
386  res.result = addObjectives(ik, objectives ...);
387  if (res.result != HebiStatusSuccess)
388  {
389  hebiIKRelease(ik);
390  return res;
391  }
392 
393  // Transfer/initialize from Eigen to C arrays
394  auto positions_array = new double[initial_positions.size()];
395  {
396  Map<Eigen::VectorXd> tmp(positions_array, initial_positions.size());
397  tmp = initial_positions;
398  }
399  auto result_array = new double[initial_positions.size()];
400 
401  // Call into C library to solve
402  res.result = hebiIKSolve(ik, internal_, positions_array, result_array, nullptr);
403 
404  // Transfer/cleanup from C arrays to Eigen
405  delete[] positions_array;
406  {
407  Map<Eigen::VectorXd> tmp(result_array, initial_positions.size());
408  result = tmp;
409  }
410  delete[] result_array;
411 
412  hebiIKRelease(ik);
413 
414  return res;
415  }
416 
422  void getJacobians(HebiFrameType, const Eigen::VectorXd& positions, MatrixXdVector& jacobians) const;
436  void getJ(HebiFrameType, const Eigen::VectorXd& positions, MatrixXdVector& jacobians) const;
437 
443  void getJacobianEndEffector(const Eigen::VectorXd& positions, Eigen::MatrixXd& jacobian) const;
463  void getJEndEffector(const Eigen::VectorXd& positions, Eigen::MatrixXd& jacobian) const;
464 
473  void getMasses(Eigen::VectorXd& masses) const;
474 
475  private:
479  static const std::map<ActuatorType, double> actuator_masses_;
480 
485 };
486 
487 } // namespace robot_model
488 } // namespace hebi
std::vector< Matrix4d, Eigen::aligned_allocator< Eigen::Matrix4d > > Matrix4dVector
Definition: robot_model.hpp:14
void hebiIKRelease(HebiIKPtr ik)
Frees resources created by this inverse kinematics object.
static const std::map< ActuatorType, double > actuator_masses_
HebiStatusCode addObjectives(HebiIKPtr ik, const T &objective) const
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:88
struct _HebiIK * HebiIKPtr
Definition: hebi.h:369
Definition: color.hpp:5
HebiFrameType
Definition: hebi.h:280
Definition: LDLT.h:16
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition: Memory.h:690
HebiStatusCode addObjectives(HebiIKPtr ik, const T &objective, Args...args) const
HebiRobotModelPtr const internal_
Definition: robot_model.hpp:94
#define HEBI_DISABLE_COPY_MOVE(Class)
Definition: util.hpp:7
Represents a chain or tree of robot elements (rigid bodies and joints).
Definition: robot_model.hpp:86
HebiJointType
Definition: hebi.h:290
struct _HebiRobotModel * HebiRobotModelPtr
Definition: hebi.h:375
HebiIKPtr hebiIKCreate(void)
Creates an IK (inverse kinematics) object that allows for solving for joint angles/positions given ob...
IKResult solveInverseKinematics(const Eigen::VectorXd &initial_positions, Eigen::VectorXd &result, Args...args) const
Solves for an inverse kinematics solution given a set of objectives.
HebiStatusCode hebiIKSolve(HebiIKPtr ik, HebiRobotModelPtr model, const double *initial_positions, double *ik_solution, void *result_info)
Solves for an inverse kinematics solution that moves the end effector to a given point.
struct _HebiRobotModelElement * HebiRobotModelElementPtr
Definition: hebi.h:381
TFSIMD_FORCE_INLINE const tfScalar & z() const
TFSIMD_FORCE_INLINE tfScalar length(const Quaternion &q)
IKResult solveIK(const Eigen::VectorXd &initial_positions, Eigen::VectorXd &result, Args...objectives) const
Solves for an inverse kinematics solution given a set of objectives.
HebiStatusCode
Definition: hebi.h:19
std::vector< MatrixXd, Eigen::aligned_allocator< Eigen::MatrixXd > > MatrixXdVector
Definition: robot_model.hpp:15
const T & y


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:08:44