Choreonoid  1.8
DyBody.h
Go to the documentation of this file.
1 
6 #ifndef CNOID_BODY_DYBODY_H
7 #define CNOID_BODY_DYBODY_H
8 
9 #include "Body.h"
10 #include "Link.h"
11 #include "ForwardDynamics.h"
12 #include <map>
13 #include "exportdecl.h"
14 
15 namespace cnoid {
16 
17 class DyBody;
19 class DyLink;
21 class ForwardDynamicsCBM;
22 
23 class CNOID_EXPORT DySubBody : public Referenced
24 {
25 public:
26  DySubBody(DyLink* rootLink);
27  DySubBody(DyLink* rootLink, std::multimap<Link*, ForceSensor*>& forceSensorMap);
28 
29  DyLink* rootLink(){ return rootLink_; }
30  std::vector<DyLink*>& links(){ return links_; }
31  int numLinks() const { return links_.size(); }
32  DyLink* link(int localIndex){ return links_[localIndex]; }
33  bool isStatic() const { return isStatic_; }
34 
35  ForwardDynamics* forwardDynamics(){ return forwardDynamics_.get(); }
36  ForwardDynamicsCBM* forwardDynamicsCBM(){ return forwardDynamicsCBM_; }
37  const std::vector<ForceSensor*>& forceSensors() { return forceSensors_; }
38 
39  void clearExternalForces();
40  void calcSpatialForwardKinematics();
41 
42 private:
43  DyLinkPtr rootLink_;
44  DyBodyPtr body_;
45  std::vector<DyLink*> links_;
46  std::unique_ptr<ForwardDynamics> forwardDynamics_;
47 
54  ForwardDynamicsCBM* forwardDynamicsCBM_;
55 
56  std::vector<ForceSensor*> forceSensors_;
57 
58  bool isStatic_;
59 
60  // Used in ConstraintForceSolver
61  bool hasConstrainedLinks;
62  bool hasContactStateSensingLinks;
63  bool isTestForceBeingApplied;
64  Vector3 dpf;
65  Vector3 dptau;
66 
67  void initialize(DyLink* rootLink, std::multimap<Link*, ForceSensor*>& forceSensorMap);
68  void extractLinksInSubBody(
69  DyLink* link, std::multimap<Link*, ForceSensor*>& forceSensorMap, bool& hasHighgainJoints);
70 
71  friend class ConstraintForceSolver;
72 };
73 
75 
79 class CNOID_EXPORT DyLink : public Link
80 {
81 public:
82  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
83 
84  DyLink();
85  DyLink(const DyLink& org);
86  DyLink(const Link& link);
87 
88  virtual void initializeState() override;
89 
90  DySubBody* subBody() { return subBody_; }
91  bool isSubBodyRoot() const { return (this == subBody_->rootLink()); }
92  DyBody* body();
93  DyLink* parent() const { return static_cast<DyLink*>(Link::parent()); }
94  DyLink* sibling() const { return static_cast<DyLink*>(Link::sibling()); }
95  DyLink* child() const { return static_cast<DyLink*>(Link::child()); }
96 
97  const Vector3& vo() const { return vo_; }
98  Vector3& vo() { return vo_; }
99  const Vector3& dvo() const { return dvo_; }
100  Vector3& dvo() { return dvo_; }
101  const Vector3& sw() const { return sw_; }
102  Vector3& sw() { return sw_; }
103  const Vector3& sv() const { return sv_; }
104  Vector3& sv() { return sv_; }
105  const Vector3& cv() const { return cv_; }
106  Vector3& cv() { return cv_; }
107  const Vector3& cw() const { return cw_; }
108  Vector3& cw() { return cw_; }
109  const Matrix3& Iww() const { return Iww_; }
110  Matrix3& Iww() { return Iww_; }
111  const Matrix3& Iwv() const { return Iwv_; }
112  Matrix3& Iwv() { return Iwv_; }
113  const Matrix3& Ivv() const { return Ivv_; }
114  Matrix3& Ivv() { return Ivv_; }
115  const Vector3& pf() const { return pf_; }
116  Vector3& pf() { return pf_; }
117  const Vector3& ptau() const { return ptau_; }
118  Vector3& ptau() { return ptau_; }
119  const Vector3& hhv() const { return hhv_; }
120  Vector3& hhv() { return hhv_; }
121  const Vector3& hhw() const { return hhw_; }
122  Vector3& hhw() { return hhw_; }
123  double uu() const { return uu_; }
124  double& uu() { return uu_; }
125  double dd() const { return dd_; }
126  double& dd() { return dd_; }
127 
128  // For the backward compatibility
129  [[deprecated("Use Link::ContactState.")]]
131  [[deprecated("Use vector<Link::ContactState>.")]]
132  typedef std::vector<ContactPoint> ConstraintForceArray;
133  [[deprecated("Use Link::contactStates.")]]
134  std::vector<ContactPoint>& constraintForces() { return Link::contactPoints(); }
135  [[deprecated("Use Link::contactStates.")]]
136  const std::vector<ContactPoint>& constraintForces() const { return Link::contactPoints(); }
137 
138  virtual void prependChild(Link* link) override;
139  virtual void appendChild(Link* link) override;
140 
141 protected:
142  virtual Referenced* doClone(CloneMap* cloneMap) const override;
143 
144 private:
145  DySubBody* subBody_;
146 
147  Vector3 vo_;
148  Vector3 dvo_;
149 
154  Vector3 sv_;
155 
160  Vector3 sw_;
161 
162  Vector3 cv_;
163  Vector3 cw_;
164 
165  Matrix3 Iww_;
166  Matrix3 Iwv_;
167  Matrix3 Ivv_;
168  Vector3 pf_;
169  Vector3 ptau_;
170  Vector3 hhv_;
171  Vector3 hhw_;
172  double uu_;
173  double dd_;
174 
175  struct ConstraintForceSolverData
176  {
177  Vector3 dvo;
178  Vector3 dw;
179  Vector3 pf0;
180  Vector3 ptau0;
181  double uu;
182  double uu0;
183  double ddq;
184  int numberToCheckAccelCalcSkip;
185  };
186  ConstraintForceSolverData cfs;
187 
188  struct ForwardDynamicsCbmData
189  {
190  bool hasForceSensor;
191  bool hasForceSensorsAbove;
192  Vector3 f;
193  Vector3 tau;
194  ForwardDynamicsCbmData()
195  : hasForceSensor(false),
196  hasForceSensorsAbove(false),
197  f(Vector3::Zero()),
198  tau(Vector3::Zero()) { }
199  };
200  std::unique_ptr<ForwardDynamicsCbmData> cbm;
201 
202  friend class DySubBody;
203  friend class ForwardDynamicsCBM;
204  friend class ConstraintForceSolver;
205 };
206 
210 class CNOID_EXPORT DyBody : public Body
211 {
212 public:
213  DyBody();
214 
221  DyBody(const Body& org) = delete;
222 
223  virtual Link* createLink(const Link* org = nullptr) const override;
224 
225  DyLink* joint(int id) const {
226  return static_cast<DyLink*>(Body::joint(id));
227  }
228  DyLink* link(int index) const {
229  return static_cast<DyLink*>(Body::link(index));
230  }
231  DyLink* link(const std::string& name) const {
232  return static_cast<DyLink*>(Body::link(name));
233  }
234  DyLink* rootLink() const {
235  return static_cast<DyLink*>(Body::rootLink());
236  }
237  const std::vector<DyLink*>& links() const {
238  return reinterpret_cast<const std::vector<DyLink*>&>(Body::links());
239  }
240 
241  void initializeSubBodies();
242 
243  std::vector<DySubBodyPtr>& subBodies(){ return subBodies_; }
244 
245  void calcSpatialForwardKinematics();
246 
247 protected:
248  virtual Referenced* doClone(CloneMap* cloneMap) const override;
249 
250 private:
251  std::vector<DySubBodyPtr> subBodies_;
252 };
253 
254 typedef ref_ptr<DyBody> DyBodyPtr;
255 
256 
258 {
259  return static_cast<DyBody*>(Link::body());
260 }
261 
262 }
263 
264 #endif
cnoid::DyBodyPtr
ref_ptr< DyBody > DyBodyPtr
Definition: DyBody.h:17
cnoid::DySubBody::link
DyLink * link(int localIndex)
Definition: DyBody.h:32
cnoid::DyLinkPtr
ref_ptr< DyLink > DyLinkPtr
Definition: DyBody.h:19
cnoid::DySubBody::forceSensors
const std::vector< ForceSensor * > & forceSensors()
Definition: DyBody.h:37
cnoid::Vector3
Eigen::Vector3d Vector3
Definition: EigenTypes.h:57
cnoid::DySubBody::forwardDynamicsCBM
ForwardDynamicsCBM * forwardDynamicsCBM()
Definition: DyBody.h:36
cnoid::DySubBody::isStatic
bool isStatic() const
Definition: DyBody.h:33
cnoid::DySubBody::links
std::vector< DyLink * > & links()
Definition: DyBody.h:30
cnoid::DyBody
Definition: DyBody.h:210
cnoid::Matrix3
Eigen::Matrix3d Matrix3
Definition: EigenTypes.h:56
cnoid::DySubBody
Definition: DyBody.h:23
cnoid::ForwardDynamicsCBM
Definition: ForwardDynamicsCBM.h:24
cnoid::DyBody::link
DyLink * link(int index) const
Definition: DyBody.h:228
cnoid::ref_ptr< DyBody >
cnoid::DySubBodyPtr
ref_ptr< DySubBody > DySubBodyPtr
Definition: DyBody.h:74
cnoid::Body::rootLink
Link * rootLink() const
Definition: Body.h:104
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::CloneMap
Definition: CloneMap.h:13
cnoid::DyBody::joint
DyLink * joint(int id) const
Definition: DyBody.h:225
cnoid::DyBody::links
const std::vector< DyLink * > & links() const
Definition: DyBody.h:237
cnoid::Body::link
Link * link(int index) const
Definition: Body.h:80
cnoid::DyBody::rootLink
DyLink * rootLink() const
Definition: DyBody.h:234
ForwardDynamics.h
cnoid::Referenced
Definition: Referenced.h:54
Body.h
cnoid::Body
Definition: Body.h:28
cnoid::Body::links
const std::vector< Link * > & links() const
Definition: Body.h:97
cnoid::ConstraintForceSolver
Definition: ConstraintForceSolver.h:19
cnoid::DyBody::link
DyLink * link(const std::string &name) const
Definition: DyBody.h:231
cnoid::DySubBody::forwardDynamics
ForwardDynamics * forwardDynamics()
Definition: DyBody.h:35
cnoid::Body::joint
Link * joint(int id) const
Definition: Body.h:145
cnoid::DySubBody::rootLink
DyLink * rootLink()
Definition: DyBody.h:29
cnoid::ForwardDynamics
Definition: ForwardDynamics.h:22
cnoid::DySubBody::numLinks
int numLinks() const
Definition: DyBody.h:31
cnoid::DyBody::subBodies
std::vector< DySubBodyPtr > & subBodies()
Definition: DyBody.h:243