Choreonoid  1.8
JointPath.h
Go to the documentation of this file.
1 
7 #ifndef CNOID_BODY_JOINT_PATH_H
8 #define CNOID_BODY_JOINT_PATH_H
9 
10 #include "LinkPath.h"
11 #include "InverseKinematics.h"
12 #include <cnoid/EigenTypes>
13 #include <functional>
14 #include <memory>
15 #include "exportdecl.h"
16 
17 namespace cnoid {
18 
19 class NumericalIK;
20 class LinkTraverse;
21 class Body;
22 
23 class CNOID_EXPORT JointPath : public InverseKinematics
24 {
25 public:
30  static std::shared_ptr<JointPath> getCustomPath(Body* body, Link* baseLink, Link* endLink);
31 
32  JointPath();
33  JointPath(Link* base, Link* end);
34  JointPath(Link* end);
35  JointPath(const JointPath& org) = delete;
36 
37  bool empty() const {
38  return joints_.empty();
39  }
40 
41  int size() const {
42  return static_cast<int>(joints_.size());
43  }
44 
45  int numJoints() const {
46  return size();
47  }
48 
49  Link* joint(int index) const {
50  return joints_[index];
51  }
52 
53  Link* operator[] (int index) const {
54  return joints_[index];
55  }
56 
57  Link* baseLink() const {
58  return linkPath_.baseLink();
59  }
60 
61  Link* endLink() const {
62  return linkPath_.endLink();
63  }
64 
65  bool isJointDownward(int index) const {
66  return (index >= numUpwardJointConnections);
67  }
68 
69  const std::vector<Link*>& joints() const { return joints_; }
70 
73 
74  iterator begin() { return joints_.begin(); }
75  iterator end() { return joints_.end(); }
76  const_iterator begin() const { return joints_.begin(); }
77  const_iterator end() const { return joints_.end(); }
78 
79  LinkPath& linkPath() { return linkPath_; }
80  const LinkPath& linkPath() const { return linkPath_; }
81 
82  void calcForwardKinematics(bool calcVelocity = false, bool calcAcceleration = false) const {
83  linkPath_.calcForwardKinematics(calcVelocity, calcAcceleration);
84  }
85 
86  int indexOf(const Link* link) const;
87 
88  virtual bool hasCustomIK() const;
89  bool isCustomIkDisabled() const { return isCustomIkDisabled_; }
90  void setCustomIkDisabled(bool on) { isCustomIkDisabled_ = on; }
91 
92  bool isBestEffortIkMode() const;
93  void setBestEffortIkMode(bool on);
94  void setNumericalIkMaxIkError(double e);
95  void setNumericalIkDeltaScale(double s);
96  void setNumericalIkMaxIterations(int n);
97  void setNumericalIkDampingConstant(double lambda);
98  static double numericalIkDefaultDeltaScale();
99  static int numericalIkDefaultMaxIterations();
100  static double numericalIkDefaultMaxIkError();
101  static double numericalIkDefaultDampingConstant();
102 
103  void customizeTarget(
104  int numTargetElements,
105  std::function<double(VectorXd& out_error)> errorFunc,
106  std::function<void(MatrixXd& out_Jacobian)> jacobianFunc);
107 
108  // For the path customized by the customizeTarget function
109  bool calcInverseKinematics();
110 
111  JointPath& storeCurrentPosition();
112 
113  JointPath& setBaseLinkGoal(const Isometry3& T);
114 
115  virtual bool calcInverseKinematics(const Isometry3& T) override;
116  virtual bool calcRemainingPartForwardKinematicsForInverseKinematics() override;
117 
118  int numIterations() const;
119 
120  std::string name() const { return name_; }
121  void setName(const std::string& name){ name_ = name; }
122 
123  // deprecated
124  void setNumericalIKtruncateRatio(double r);
125  static double numericalIKdefaultTruncateRatio();
126 
127  [[deprecated("Use calcInverseKinematics(const Isometry3& T)")]]
128  bool calcInverseKinematics(const Vector3& p, const Matrix3& R) {
130  }
131  [[deprecated("Use calcInverseKinematics(const Isometry3& T)")]]
132  bool calcInverseKinematics(
133  const Vector3& base_p, const Matrix3& base_R, const Vector3& end_p, const Matrix3& end_R);
134  [[deprecated]]
135  void calcJacobian(Eigen::MatrixXd& out_J) const;
136  [[deprecated("Use hasCustomIK")]]
137  bool hasAnalyticalIK() const;
138  [[deprecated("Use setCustomIkDisabled.")]]
139  void setNumericalIKenabled(bool on) { setCustomIkDisabled(on); }
140  [[deprecated("Use isCustomIkDisabled.")]]
141  bool isNumericalIkEnabled() const { return isCustomIkDisabled(); }
142  [[deprecated("Use isBestEffortIkMode.")]]
143  bool isBestEffortIKmode() const { return isBestEffortIkMode(); }
144  [[deprecated("Use setBestEffortIkMode.")]]
145  void setBestEffortIKmode(bool on) { setBestEffortIkMode(on); }
146  [[deprecated("Use setNumericalIkMaxIkError.")]]
147  void setNumericalIKmaxIKerror(double e){ setNumericalIkMaxIkError(e); }
148  [[deprecated("Use setNumericalIkDeltaScale.")]]
149  void setNumericalIKdeltaScale(double s) { setNumericalIkDeltaScale(s); }
150  [[deprecated("Use setNumericalIkMaxIterations.")]]
151  void setNumericalIKmaxIterations(int n) { setNumericalIkMaxIterations(n); }
152  [[deprecated("Use setNumericalIkDampingConstant.")]]
153  void setNumericalIKdampingConstant(double lambda) { setNumericalIkDampingConstant(lambda); }
154  [[deprecated("Use numericalIkDefaultDeltaScale.")]]
155  static double numericalIKdefaultDeltaScale(){ return numericalIkDefaultDeltaScale(); }
156  [[deprecated("Use numericalIkDefaultMaxIterations.")]]
157  static int numericalIKdefaultMaxIterations(){ return numericalIkDefaultMaxIterations(); }
158  [[deprecated("Use numericalIkDefaultMaxIkError.")]]
159  static double numericalIKdefaultMaxIKerror(){ return numericalIkDefaultMaxIkError(); }
160  [[deprecated("Use numericalIkDefaultDampingConstant.")]]
161  static double numericalIKdefaultDampingConstant(){ return numericalIkDefaultDampingConstant(); }
162 
163 private:
164  void initialize();
165  void extractJoints();
166  void doResetWhenJointPathUpdated();
167  NumericalIK* getOrCreateNumericalIK();
168 
169  LinkPath linkPath_;
170  std::vector<Link*> joints_;
171  std::shared_ptr<LinkTraverse> remainingLinkTraverse;
172  NumericalIK* numericalIK;
173  int numUpwardJointConnections;
174  bool needForwardKinematicsBeforeIK;
175  bool isCustomIkDisabled_;
176  std::string name_;
177 };
178 
180 inline std::shared_ptr<JointPath> getCustomJointPath(Body* body, Link* baseLink, Link* endLink){
181  return JointPath::getCustomPath(body, baseLink, endLink);
182 }
183 
184 #ifdef CNOID_BACKWARD_COMPATIBILITY
185 typedef std::shared_ptr<JointPath> JointPathPtr;
186 #endif
187 
188 }
189 
190 #endif
cnoid::JointPath::iterator
LinkTraverse::iterator iterator
Definition: JointPath.h:71
cnoid::JointPath::setCustomIkDisabled
void setCustomIkDisabled(bool on)
Definition: JointPath.h:90
cnoid::JointPath::setNumericalIKdampingConstant
void setNumericalIKdampingConstant(double lambda)
Definition: JointPath.h:153
cnoid::JointPath::setName
void setName(const std::string &name)
Definition: JointPath.h:121
cnoid::JointPath::isJointDownward
bool isJointDownward(int index) const
Definition: JointPath.h:65
cnoid::JointPath::size
int size() const
Definition: JointPath.h:41
cnoid::Vector3
Eigen::Vector3d Vector3
Definition: EigenTypes.h:57
cnoid::LinkPath
Definition: LinkPath.h:14
cnoid::JointPath::isCustomIkDisabled
bool isCustomIkDisabled() const
Definition: JointPath.h:89
cnoid::JointPath::end
iterator end()
Definition: JointPath.h:75
cnoid::JointPath
Definition: JointPath.h:23
cnoid::JointPath::numericalIKdefaultDampingConstant
static double numericalIKdefaultDampingConstant()
Definition: JointPath.h:161
cnoid::JointPath::numericalIKdefaultDeltaScale
static double numericalIKdefaultDeltaScale()
Definition: JointPath.h:155
cnoid::Isometry3
Eigen::Isometry3d Isometry3
Definition: EigenTypes.h:73
cnoid::JointPath::setNumericalIKdeltaScale
void setNumericalIKdeltaScale(double s)
Definition: JointPath.h:149
cnoid::JointPath::setNumericalIKmaxIKerror
void setNumericalIKmaxIKerror(double e)
Definition: JointPath.h:147
cnoid::JointPath::empty
bool empty() const
Definition: JointPath.h:37
cnoid::JointPath::joints
const std::vector< Link * > & joints() const
Definition: JointPath.h:69
cnoid::JointPath::setBestEffortIKmode
void setBestEffortIKmode(bool on)
Definition: JointPath.h:145
LinkPath.h
cnoid::Matrix3
Eigen::Matrix3d Matrix3
Definition: EigenTypes.h:56
cnoid::JointPath::end
const_iterator end() const
Definition: JointPath.h:77
cnoid::JointPath::baseLink
Link * baseLink() const
Definition: JointPath.h:57
cnoid::JointPath::numericalIKdefaultMaxIKerror
static double numericalIKdefaultMaxIKerror()
Definition: JointPath.h:159
cnoid::JointPath::setNumericalIKenabled
void setNumericalIKenabled(bool on)
Definition: JointPath.h:139
cnoid::JointPath::name
std::string name() const
Definition: JointPath.h:120
cnoid::JointPath::linkPath
const LinkPath & linkPath() const
Definition: JointPath.h:80
cnoid::InverseKinematics::calcInverseKinematics
virtual bool calcInverseKinematics(const Isometry3 &T)=0
cnoid::JointPath::begin
iterator begin()
Definition: JointPath.h:74
cnoid::JointPath::calcInverseKinematics
bool calcInverseKinematics(const Vector3 &p, const Matrix3 &R)
Definition: JointPath.h:128
cnoid::JointPath::const_iterator
LinkTraverse::const_iterator const_iterator
Definition: JointPath.h:72
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::JointPath::endLink
Link * endLink() const
Definition: JointPath.h:61
cnoid::InverseKinematics
Definition: InverseKinematics.h:13
cnoid::JointPath::joint
Link * joint(int index) const
Definition: JointPath.h:49
cnoid::JointPath::calcForwardKinematics
void calcForwardKinematics(bool calcVelocity=false, bool calcAcceleration=false) const
Definition: JointPath.h:82
cnoid::getCustomJointPath
std::shared_ptr< JointPath > getCustomJointPath(Body *body, Link *baseLink, Link *endLink)
Definition: JointPath.h:180
cnoid::LinkTraverse::const_iterator
container::const_iterator const_iterator
Definition: LinkTraverse.h:67
cnoid::JointPath::getCustomPath
static std::shared_ptr< JointPath > getCustomPath(Body *body, Link *baseLink, Link *endLink)
Definition: JointPath.cpp:557
cnoid::Body
Definition: Body.h:28
cnoid::JointPath::setNumericalIKmaxIterations
void setNumericalIKmaxIterations(int n)
Definition: JointPath.h:151
cnoid::JointPath::numericalIKdefaultMaxIterations
static int numericalIKdefaultMaxIterations()
Definition: JointPath.h:157
InverseKinematics.h
cnoid::JointPath::isNumericalIkEnabled
bool isNumericalIkEnabled() const
Definition: JointPath.h:141
cnoid::JointPath::begin
const_iterator begin() const
Definition: JointPath.h:76
cnoid::LinkTraverse::iterator
container::iterator iterator
Definition: LinkTraverse.h:66
cnoid::JointPath::numJoints
int numJoints() const
Definition: JointPath.h:45
cnoid::JointPath::isBestEffortIKmode
bool isBestEffortIKmode() const
Definition: JointPath.h:143
cnoid::JointPath::linkPath
LinkPath & linkPath()
Definition: JointPath.h:79