DART  6.10.1
BalanceConstraint.hpp
Go to the documentation of this file.
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 DART_CONSTRAINT_BALANCECONSTRAINT_HPP_
34 #define DART_CONSTRAINT_BALANCECONSTRAINT_HPP_
35 
37 
38 namespace dart {
39 namespace constraint {
40 
47 {
48 public:
64  {
68  };
69 
81  {
83  SHIFT_COM
84  };
85 
88  const std::shared_ptr<dynamics::HierarchicalIK>& _ik,
89  BalanceMethod_t _balanceMethod = SHIFT_SUPPORT,
90  ErrorMethod_t _errorMethod = FROM_CENTROID);
91 
93  virtual ~BalanceConstraint() = default;
94 
95  // Documentation inherited
97  const std::shared_ptr<dynamics::HierarchicalIK>& _newIK) const override;
98 
99  // Documentation inherited
100  double eval(const Eigen::VectorXd& _x) override;
101 
102  // Documentation inherited
103  void evalGradient(
104  const Eigen::VectorXd& _x, Eigen::Map<Eigen::VectorXd> _grad) override;
105 
108  void setErrorMethod(ErrorMethod_t _method);
109 
113 
116  void setBalanceMethod(BalanceMethod_t _method);
117 
120 
123  void setOptimizationTolerance(double _tol);
124 
127  double getOptimizationTolerance() const;
128 
130  void setPseudoInverseDamping(double _damping);
131 
133  double getPseudoInverseDamping() const;
134 
136  const Eigen::Vector3d& getLastError() const;
137 
140  void clearCaches();
141 
142 protected:
145  void convertJacobianMethodOutputToGradient(Eigen::Map<Eigen::VectorXd>& grad);
146 
149  std::weak_ptr<dynamics::HierarchicalIK> mIK;
150 
153 
156 
159 
161  double mDamping;
162 
165  std::size_t mClosestEndEffector[2];
166 
169  Eigen::Vector3d mLastError;
170 
172  Eigen::Vector3d mLastCOM;
173 
175  std::size_t mLastSupportVersion;
176 
180 
184 
186  Eigen::JacobiSVD<math::LinearJacobian> mSVDCache;
187 
189  Eigen::MatrixXd mNullSpaceCache;
190 
192  Eigen::MatrixXd mPartialNullSpaceCache;
193 
196  Eigen::VectorXd mInitialPositionsCache;
197 };
198 
199 } // namespace constraint
200 } // namespace dart
201 
202 #endif // DART_CONSTRAINT_BALANCECONSTRAINT_HPP_
BalanceConstraint is a kinematic constraint function designed to be passed into a HierarchicalIK modu...
Definition: BalanceConstraint.hpp:47
BalanceMethod_t mBalanceMethod
The method that will be used for balance.
Definition: BalanceConstraint.hpp:155
Eigen::Vector3d mLastError
The error vector points away from the direction that the center of mass should move in order to reduc...
Definition: BalanceConstraint.hpp:169
void setOptimizationTolerance(double _tol)
Set the tolerance for how far the center of mass can be from the support polygon centroid before the ...
Definition: BalanceConstraint.cpp:364
virtual ~BalanceConstraint()=default
Virtual destructor.
void evalGradient(const Eigen::VectorXd &_x, Eigen::Map< Eigen::VectorXd > _grad) override
Evaluates and returns the objective function at the point x.
Definition: BalanceConstraint.cpp:187
BalanceMethod_t getBalanceMethod() const
Get the method that this constraint function will use to achieve balance.
Definition: BalanceConstraint.cpp:358
BalanceConstraint(const std::shared_ptr< dynamics::HierarchicalIK > &_ik, BalanceMethod_t _balanceMethod=SHIFT_SUPPORT, ErrorMethod_t _errorMethod=FROM_CENTROID)
Constructor.
Definition: BalanceConstraint.cpp:42
std::size_t mClosestEndEffector[2]
The indices of the supporting end effectors that are closest to the center of mass.
Definition: BalanceConstraint.hpp:165
ErrorMethod_t getErrorMethod() const
Get the method that this constraint function will use to compute the error.
Definition: BalanceConstraint.cpp:342
Eigen::MatrixXd mPartialNullSpaceCache
Cache for an individual null space.
Definition: BalanceConstraint.hpp:192
void convertJacobianMethodOutputToGradient(Eigen::Map< Eigen::VectorXd > &grad)
Convert the gradient that gets generated via Jacobian methods into a gradient that can be used by a G...
Definition: BalanceConstraint.cpp:409
double mOptimizationTolerance
The tolerance allowed before optimization is considered successful.
Definition: BalanceConstraint.hpp:158
std::weak_ptr< dynamics::HierarchicalIK > mIK
Pointer to the hierarchical IK that owns this Function.
Definition: BalanceConstraint.hpp:149
BalanceMethod_t
The BalanceMethod_t determines whether balancing should be achieved by shifting the locations of the ...
Definition: BalanceConstraint.hpp:81
@ SHIFT_COM
Definition: BalanceConstraint.hpp:83
@ SHIFT_SUPPORT
Definition: BalanceConstraint.hpp:82
Eigen::JacobiSVD< math::LinearJacobian > mSVDCache
Cache for the SVD.
Definition: BalanceConstraint.hpp:186
void setPseudoInverseDamping(double _damping)
Set the damping factor that will be used when computing the pseudoinverse.
Definition: BalanceConstraint.cpp:380
double mDamping
The damping factor for the pseudoinverse.
Definition: BalanceConstraint.hpp:161
ErrorMethod_t
The ErrorMethod_t determines whether the error should be computed based on the center of mass's dista...
Definition: BalanceConstraint.hpp:64
@ OPTIMIZE_BALANCE
Definition: BalanceConstraint.hpp:67
@ FROM_EDGE
Definition: BalanceConstraint.hpp:66
@ FROM_CENTROID
Definition: BalanceConstraint.hpp:65
optimizer::FunctionPtr clone(const std::shared_ptr< dynamics::HierarchicalIK > &_newIK) const override
Definition: BalanceConstraint.cpp:59
double eval(const Eigen::VectorXd &_x) override
Evaluates and returns the objective function at the point x.
Definition: BalanceConstraint.cpp:66
double getOptimizationTolerance() const
Get the tolerance for how far the center of mass can be from the support polygon centroid before the ...
Definition: BalanceConstraint.cpp:374
std::size_t mLastSupportVersion
The last version of the support polygon that was computed.
Definition: BalanceConstraint.hpp:175
void clearCaches()
Clear the caches to force the error computation to update.
Definition: BalanceConstraint.cpp:402
Eigen::Vector3d mLastCOM
The last computed location of the center of mass.
Definition: BalanceConstraint.hpp:172
const Eigen::Vector3d & getLastError() const
Get the last error vector that was computed by this BalanceConstraint.
Definition: BalanceConstraint.cpp:396
double getPseudoInverseDamping() const
Get the damping factor that will be used when computing the pseudoinverse.
Definition: BalanceConstraint.cpp:390
math::LinearJacobian mComJacCache
Cache for the center of mass Jacobian so that the memory space does not need to be reallocated each l...
Definition: BalanceConstraint.hpp:179
Eigen::MatrixXd mNullSpaceCache
Cache for the full null space.
Definition: BalanceConstraint.hpp:189
void setBalanceMethod(BalanceMethod_t _method)
Set the method that this constraint function will use to achieve balance.
Definition: BalanceConstraint.cpp:348
ErrorMethod_t mErrorMethod
The method that will be used to compute error.
Definition: BalanceConstraint.hpp:152
Eigen::VectorXd mInitialPositionsCache
Cache used by convertJacobianMethodOutputToGradient to avoid reallocating this vector on each iterati...
Definition: BalanceConstraint.hpp:196
void setErrorMethod(ErrorMethod_t _method)
Set the method that this constraint function will use to compute the error.
Definition: BalanceConstraint.cpp:332
math::LinearJacobian mEEJacCache
Cache for the end effector Jacobians so the space does not need to be reallocated each loop.
Definition: BalanceConstraint.hpp:183
This class should be inherited by optimizer::Function classes that have a dependency on the Hierarchi...
Definition: HierarchicalIK.hpp:136
Definition: Function.hpp:46
Eigen::Matrix< double, 3, Eigen::Dynamic > LinearJacobian
Definition: MathTypes.hpp:112
std::shared_ptr< Function > FunctionPtr
Definition: Function.hpp:84
Definition: BulletCollisionDetector.cpp:65