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