dart
State.hpp
1 /*
2  * Copyright (c) 2011-2021, The DART development contributors
3  * All rights reserved.
4  *
5  * The list of contributors can be found at:
6  * https://github.com/dartsim/dart/blob/master/LICENSE
7  *
8  * This file is provided under the following "BSD-style" License:
9  * Redistribution and use in source and binary forms, with or
10  * without modification, are permitted provided that the following
11  * conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef EXAMPLES_ATLASSIMBICON_STATE_HPP_
34 #define EXAMPLES_ATLASSIMBICON_STATE_HPP_
35 
36 #include <map>
37 #include <string>
38 #include <vector>
39 
40 #include <Eigen/Dense>
41 
42 #include <dart/dart.hpp>
43 
44 #define ATLAS_DEFAULT_KD 1.0 // No more than 1.0
45 #define ATLAS_DEFAULT_KP 1e+3
46 
47 #define ATLAS_DEFAULT_SAGITAL_CD 0.1
48 #define ATLAS_DEFAULT_SAGITAL_CV 0.1
49 
50 #define ATLAS_DEFAULT_CORONAL_CD 0.1
51 #define ATLAS_DEFAULT_CORONAL_CV 0.1
52 
53 namespace dart {
54 namespace dynamics {
55 class BodyNode;
56 class Joint;
57 class Skeleton;
58 } // namespace dynamics
59 } // namespace dart
60 
61 class TerminalCondition;
62 
63 //==============================================================================
65 class State
66 {
67 public:
69  explicit State(
70  dart::dynamics::SkeletonPtr _skeleton, const std::string& _name);
71 
73  virtual ~State();
74 
75  //------------------------------- Setting ------------------------------------
77  void setName(std::string& _name);
78 
80  const std::string& getName() const;
81 
83  void setNextState(State* _nextState);
84 
86  void setTerminalCondition(TerminalCondition* _condition);
87 
89  State* getNextState() const;
90 
92  double getElapsedTime() const;
93 
94  //----------------------- Setting Desired Position ---------------------------
96  void setDesiredJointPosition(const std::string& _jointName, double _val);
97 
99  double getDesiredJointPosition(int _idx) const;
100 
102  double getDesiredJointPosition(const std::string& _jointName) const;
103 
105  void setDesiredSwingLegGlobalAngleOnSagital(double _val);
106 
108  void setDesiredSwingLegGlobalAngleOnCoronal(double _val);
109 
111  void setDesiredPelvisGlobalAngleOnSagital(double _val);
112 
114  void setDesiredPelvisGlobalAngleOnCoronal(double _val);
115 
117  void setProportionalGain(int _idx, double _val);
118 
120  void setProportionalGain(const std::string& _jointName, double _val);
121 
123  double getProportionalGain(int _idx) const;
124 
126  double getProportionalGain(const std::string& _jointName) const;
127 
129  void setDerivativeGain(int _idx, double _val);
130 
132  void setDerivativeGain(const std::string& _jointName, double _val);
133 
135  double getDerivativeGain(int _idx) const;
136 
137  // /// \brief Get derivative gain for PD controller
138  // double getDerivativeGain(const std::string& _jointName) const;
139 
141  void setFeedbackSagitalCOMDistance(std::size_t _index, double _val);
142 
144  void setFeedbackSagitalCOMVelocity(std::size_t _index, double _val);
145 
147  void setFeedbackCoronalCOMDistance(std::size_t _index, double _val);
148 
150  void setFeedbackCoronalCOMVelocity(std::size_t _index, double _val);
151 
153  void setStanceFootToLeftFoot();
154 
156  void setStanceFootToRightFoot();
157 
158  //------------------------------- Control ------------------------------------
161  virtual void begin(double _currentTime);
162 
164  virtual void computeControlForce(double _timestep);
165 
167  virtual bool isTerminalConditionSatisfied() const;
168 
171  virtual void end(double _currentTime);
172 
173 protected:
175  Eigen::Vector3d getCOM() const;
176 
178  Eigen::Vector3d getCOMVelocity() const;
179 
185  Eigen::Isometry3d getCOMFrame() const;
186 
188  double getSagitalCOMDistance();
189 
191  double getSagitalCOMVelocity();
192 
194  double getCoronalCOMDistance();
195 
197  double getCoronalCOMVelocity();
198 
200  Eigen::Vector3d getStanceAnklePosition() const;
201 
203  Eigen::Vector3d getLeftAnklePosition() const;
204 
206  Eigen::Vector3d getRightAnklePosition() const;
207 
208  // TODO(JS): Not implemented yet
210  double getSagitalPelvisAngle() const;
211 
212  // TODO(JS): Not implemented yet
214  double getCoronalPelvisAngle() const;
215 
217  double getSagitalLeftLegAngle() const;
218 
220  double getSagitalRightLegAngle() const;
221 
223  double getCoronalLeftLegAngle() const;
224 
226  double getCoronalRightLegAngle() const;
227 
229  std::string mName;
230 
232  dart::dynamics::SkeletonPtr mSkeleton;
233 
236 
239 
241  double mBeginTime;
242 
244  double mEndTime;
245 
247  int mFrame;
248 
250  double mElapsedTime;
251 
253  Eigen::VectorXd mDesiredJointPositions;
254 
257 
260 
263 
266 
268  Eigen::VectorXd mKp;
269 
271  Eigen::VectorXd mKd;
272 
274  Eigen::VectorXd mSagitalCd;
275 
277  Eigen::VectorXd mSagitalCv;
278 
280  Eigen::VectorXd mCoronalCd;
281 
283  Eigen::VectorXd mCoronalCv;
284 
286  Eigen::VectorXd mTorque;
287 
289  std::map<const std::string, int> mJointMap;
290 
291 private:
293  Eigen::Vector3d _getJointPosition(dart::dynamics::BodyNode* _bodyNode) const;
294 
296  double _getAngleBetweenTwoVectors(
297  const Eigen::Vector3d& _v1, const Eigen::Vector3d& _v2) const;
298 
300  void _updateTorqueForStanceLeg();
301 
303  dart::dynamics::BodyNode* mPelvis;
304 
306  dart::dynamics::BodyNode* mLeftThigh;
307 
309  dart::dynamics::BodyNode* mRightThigh;
310 
312  dart::dynamics::BodyNode* mStanceFoot;
313 
315  dart::dynamics::BodyNode* mLeftFoot;
316 
318  dart::dynamics::BodyNode* mRightFoot;
319 
321  std::size_t mCoronalLeftHip;
322 
324  std::size_t mCoronalRightHip;
325 
327  std::size_t mSagitalLeftHip;
328 
330  std::size_t mSagitalRightHip;
331 
333  Eigen::VectorXd mDesiredJointPositionsBalance;
334 };
335 
336 #endif // EXAMPLES_ATLASSIMBICON_STATE_HPP_
Eigen::VectorXd mKp
Proportional gain for PD controller.
Definition: State.hpp:268
class TerminalCondition
Definition: TerminalCondition.hpp:47
std::map< const std::string, int > mJointMap
Joint map.
Definition: State.hpp:289
double mDesiredGlobalSwingLegAngleOnCoronal
Desired global angle of swing leg on coronal plane.
Definition: State.hpp:259
double mDesiredGlobalPelvisAngleOnCoronal
Desired global angle of pelvis on coronal plane.
Definition: State.hpp:265
double mElapsedTime
Elapsed time which is stopped time minus started time.
Definition: State.hpp:250
Eigen::VectorXd mKd
Derivative gain PD controller.
Definition: State.hpp:271
Eigen::VectorXd mSagitalCv
Feedback gain for velocity of com.
Definition: State.hpp:277
Definition: Aspect.cpp:40
std::string mName
Name.
Definition: State.hpp:229
dart::dynamics::SkeletonPtr mSkeleton
Target skeleton for control.
Definition: State.hpp:232
Eigen::VectorXd mSagitalCd
Feedback gain for com.
Definition: State.hpp:274
int mFrame
Frame number.
Definition: State.hpp:247
class State
Definition: State.hpp:65
Eigen::VectorXd mDesiredJointPositions
Desired joint positions.
Definition: State.hpp:253
Eigen::VectorXd mCoronalCd
Feedback gain for com.
Definition: State.hpp:280
BodyNode class represents a single node of the skeleton.
Definition: BodyNode.hpp:74
double mDesiredGlobalPelvisAngleOnSagital
Desired global angle of pelvis on sagital plane.
Definition: State.hpp:262
TerminalCondition * mTerminalCondition
Terminal condition.
Definition: State.hpp:238
State * mNextState
Next state. Default is myself.
Definition: State.hpp:235
double mDesiredGlobalSwingLegAngleOnSagital
Desired global angle of swing leg on sagital plane.
Definition: State.hpp:256
Eigen::VectorXd mCoronalCv
Feedback gain for velocity of com.
Definition: State.hpp:283
double mEndTime
Stopped time.
Definition: State.hpp:244
double mBeginTime
Started time.
Definition: State.hpp:241
Eigen::VectorXd mTorque
Computeed control force.
Definition: State.hpp:286