Choreonoid  1.8
DyWorld.h
Go to the documentation of this file.
1 
6 #ifndef CNOID_BODY_DYWORLD_H
7 #define CNOID_BODY_DYWORLD_H
8 
9 #include "DyBody.h"
10 #include "ExtraJoint.h"
11 #include <string>
12 #include <map>
13 #include "exportdecl.h"
14 
15 namespace cnoid {
16 
17 class CNOID_EXPORT DyWorldBase
18 {
19 public:
20  DyWorldBase();
21  virtual ~DyWorldBase();
22 
23  int numBodies() const { return bodies_.size(); }
24  DyBody* body(int index) { return bodies_[index]; }
25  DyBody* body(const std::string& name) const;
26  const std::vector<DyBodyPtr>& bodies() { return bodies_; }
27 
32  int numSubBodies() const { return subBodies_.size(); }
33  DySubBody* subBody(int index) { return subBodies_[index]; }
34  const std::vector<DySubBodyPtr>& subBodies() { return subBodies_; }
35 
41  int addBody(DyBody* body);
42 
43  bool hasHighGainDynamics() const { return hasHighGainDynamics_; }
44  void clearBodies();
45  void clearCollisionPairs();
46  void setTimeStep(double dt);
47  double timeStep(void) const { return timeStep_; }
48  void setCurrentTime(double tm);
49  double currentTime(void) const { return currentTime_; }
50 
54  void setGravityAcceleration(const Vector3& g);
55 
56  inline const Vector3& gravityAcceleration() const { return g; }
57 
63  void enableSensors(bool on);
64 
65  void setOldAccelSensorCalcMode(bool on);
66 
70  void setEulerMethod();
71 
75  void setRungeKuttaMethod();
76 
80  virtual void initialize();
81 
82  void setVirtualJointForces();
83 
87  virtual void calcNextState();
88 
89  void refreshState();
90 
97  std::pair<int,bool> getIndexOfLinkPairs(DyLink* link1, DyLink* link2);
98 
99  std::vector<ExtraJoint>& extraJoints() { return extraJoints_; }
100  void clearExtraJoints() { extraJoints_.clear(); }
101  void addExtraJoint(ExtraJoint& extraJoint){ extraJoints_.push_back(extraJoint); }
102 
103 private:
104  double currentTime_;
105  double timeStep_;
106 
107  std::vector<DyBodyPtr> bodies_;
108  std::vector<DySubBodyPtr> subBodies_;
109  std::vector<DyBodyPtr> bodiesWithVirtualJointForces_;
110  std::map<std::string, DyBodyPtr> nameToBodyMap;
111 
112  Vector3 g;
113  bool sensorsAreEnabled;
114  bool isOldAccelSensorCalcMode;
115  bool isEulerMethod; // Euler or Runge Kutta ?
116  bool hasHighGainDynamics_;
117 
118  struct LinkPairKey {
119  DyLink* link1;
120  DyLink* link2;
121  bool operator<(const LinkPairKey& pair2) const;
122  };
123  std::map<LinkPairKey, int> linkPairKeyToIndexMap;
124 
125  int numRegisteredLinkPairs;
126 
127  std::vector<ExtraJoint> extraJoints_;
128 
129  void extractInternalBodies(Link* link);
130 };
131 
132 template <class TConstraintForceSolver> class DyWorld : public DyWorldBase
133 {
134 public:
135  TConstraintForceSolver constraintForceSolver;
136 
138 
139  virtual void initialize() {
141  constraintForceSolver.initialize();
142  }
143 
144  virtual void calcNextState(){
146  constraintForceSolver.solve();
148  }
149 };
150 
151 };
152 
153 #endif
cnoid::DyWorldBase::numBodies
int numBodies() const
Definition: DyWorld.h:23
cnoid::Vector3
Eigen::Vector3d Vector3
Definition: EigenTypes.h:57
ExtraJoint.h
cnoid::DyWorld::initialize
virtual void initialize()
initialize this world. This must be called after all bodies are registered.
Definition: DyWorld.h:139
cnoid::DyWorldBase::bodies
const std::vector< DyBodyPtr > & bodies()
Definition: DyWorld.h:26
cnoid::DyBody
Definition: DyBody.h:210
cnoid::DyWorld::calcNextState
virtual void calcNextState()
compute forward dynamics and update current state
Definition: DyWorld.h:144
cnoid::DySubBody
Definition: DyBody.h:23
cnoid::DyWorldBase::initialize
virtual void initialize()
initialize this world. This must be called after all bodies are registered.
Definition: DyWorld.cpp:74
cnoid::DyWorldBase::extraJoints
std::vector< ExtraJoint > & extraJoints()
Definition: DyWorld.h:99
cnoid::DyWorldBase
Definition: DyWorld.h:17
cnoid::DyWorldBase::numSubBodies
int numSubBodies() const
This function returns the number of bodies used in the internal calculation in this world.
Definition: DyWorld.h:32
cnoid::DyWorldBase::timeStep
double timeStep(void) const
Definition: DyWorld.h:47
cnoid
Definition: AbstractSceneLoader.h:11
DyBody.h
cnoid::DyWorld::DyWorld
DyWorld()
Definition: DyWorld.h:137
cnoid::DyWorldBase::hasHighGainDynamics
bool hasHighGainDynamics() const
Definition: DyWorld.h:43
cnoid::DyWorldBase::calcNextState
virtual void calcNextState()
compute forward dynamics and update current state
Definition: DyWorld.cpp:100
cnoid::DyWorldBase::currentTime
double currentTime(void) const
Definition: DyWorld.h:49
cnoid::ExtraJoint
Definition: ExtraJoint.h:13
cnoid::DyWorldBase::subBody
DySubBody * subBody(int index)
Definition: DyWorld.h:33
cnoid::DyWorldBase::clearExtraJoints
void clearExtraJoints()
Definition: DyWorld.h:100
cnoid::DyWorldBase::body
DyBody * body(int index)
Definition: DyWorld.h:24
cnoid::DyWorldBase::subBodies
const std::vector< DySubBodyPtr > & subBodies()
Definition: DyWorld.h:34
cnoid::DyWorldBase::setVirtualJointForces
void setVirtualJointForces()
Definition: DyWorld.cpp:92
cnoid::DyWorld
Definition: DyWorld.h:132
cnoid::operator<
bool operator<(ref_ptr< T > const &a, ref_ptr< T > const &b)
Definition: Referenced.h:241
cnoid::DyWorldBase::gravityAcceleration
const Vector3 & gravityAcceleration() const
Definition: DyWorld.h:56
cnoid::DyWorld::constraintForceSolver
TConstraintForceSolver constraintForceSolver
Definition: DyWorld.h:135
cnoid::DyWorldBase::addExtraJoint
void addExtraJoint(ExtraJoint &extraJoint)
Definition: DyWorld.h:101