/usr/include/kido/dynamics/HierarchicalIK.hpp is in libkido-dev 0.1.0+dfsg-2build9.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | /*
* Copyright (c) 2015, Georgia Tech Research Corporation
* All rights reserved.
*
* Author(s): Michael X. Grey <mxgrey@gatech.edu>
*
* Georgia Tech Graphics Lab and Humanoid Robotics Lab
*
* Directed by Prof. C. Karen Liu and Prof. Mike Stilman
* <karenliu@cc.gatech.edu> <mstilman@cc.gatech.edu>
*
* This file is provided under the following "BSD-style" License:
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef KIDO_DYNAMICS_HIERARCHICALIK_HPP_
#define KIDO_DYNAMICS_HIERARCHICALIK_HPP_
#include <unordered_set>
#include "kido/dynamics/InverseKinematics.hpp"
namespace kido {
namespace dynamics {
/// An IKHierarchy is a sorted set of IK modules. The outer vector represents
/// the precedence of the IK modules. Modules with the same precedence will have
/// their gradients added. Precedence of the modules decreases as the index of
/// the outer vector increases. Modules with lower precedence will be projected
/// through the null spaces of modules with higher precedence.
typedef std::vector< std::vector< std::shared_ptr<InverseKinematics> > > IKHierarchy;
/// The HierarchicalIK class provides a convenient way of setting up a
/// hierarchical inverse kinematics optimization problem which combines several
/// InverseKinematics problems into one. InverseKinematics problems with a
/// larger hierarchy level will be projected into null spaces of the problems
/// that have a smaller hierarchy number.
///
/// Note that the HierarchicalIK will only account for the
/// InverseKinematics::ErrorMethod and InverseKinematics::GradientMethod that
/// the IK modules specify; it will ignore any other constraints or objectives
/// put into the IK modules' Problems. Any additional constraints or objectives
/// that you want the HierarchicalIK to solve should be put directly into the
/// HierarchicalIK's Problem.
class HierarchicalIK : public common::Subject
{
public:
/// Virtual destructor
virtual ~HierarchicalIK() = default;
/// Solve the IK Problem. By default, the Skeleton itself will retain the
/// solved joint positions. If you pass in false for _applySolution, then the
/// joint positions will be return to their original positions after the
/// problem is solved.
bool solve(bool _applySolution = true);
/// Same as solve(bool), but the positions vector will be filled with the
/// solved positions.
bool solve(Eigen::VectorXd& positions, bool _applySolution = true);
/// Clone this HierarchicalIK module
virtual std::shared_ptr<HierarchicalIK> clone(
const SkeletonPtr& _newSkel) const = 0;
/// This class should be inherited by optimizer::Function classes that have a
/// dependency on the HierarchicalIK module that they belong to. If you
/// pass an HierarchicalIK::Function into the Problem of an
/// HierarchicalIK module, then it will be properly cloned whenever the
/// HierarchicalIK module that it belongs to gets cloned. Any Function
/// classes in the Problem that do not inherit HierarchicalIK::Function
/// will just be copied over by reference.
class Function
{
public:
/// Enable this function to be cloned to a new IK module.
virtual optimizer::FunctionPtr clone(
const std::shared_ptr<HierarchicalIK>& _newIK) const = 0;
/// Virtual destructor
virtual ~Function() = default;
};
/// Set the objective function for this HierarchicalIK.
void setObjective(const std::shared_ptr<optimizer::Function>& _objective);
/// Get the objective function for this HierarchicalIK.
const std::shared_ptr<optimizer::Function>& getObjective();
/// Get the objective function for this HierarchicalIK.
std::shared_ptr<const optimizer::Function> getObjective() const;
/// Set the null space objective for this HierarchicalIK.
void setNullSpaceObjective(
const std::shared_ptr<optimizer::Function>& _nsObjective);
/// Get the null space objective for this HierarchicalIK.
const std::shared_ptr<optimizer::Function>& getNullSpaceObjective();
/// Get the null space objective for this HierarchicalIK.
std::shared_ptr<const optimizer::Function> getNullSpaceObjective() const;
/// Returns true if this HierarchicalIK has a null space objective.
bool hasNullSpaceObjective() const;
/// Get the Problem that is being maintained by this HierarchicalIK module
const std::shared_ptr<optimizer::Problem>& getProblem();
/// Get the Problem that is being maintained by this HierarchicalIK module
std::shared_ptr<const optimizer::Problem> getProblem() const;
/// Reset the Problem that is being maintained by this HierarchicalIK module.
/// This will clear out all Functions from the Problem and then configure the
/// Problem to use this IK module's Objective and Constraint functions.
///
/// Setting _clearSeeds to true will clear out any seeds that have been loaded
/// into the Problem.
void resetProblem(bool _clearSeeds=false);
/// Set the Solver that should be used by this IK module, and set it up with
/// the Problem that is configured by this IK module
void setSolver(const std::shared_ptr<optimizer::Solver>& _newSolver);
/// Get the Solver that is being used by this IK module.
const std::shared_ptr<optimizer::Solver>& getSolver();
/// Get the Solver that is being used by this IK module.
std::shared_ptr<const optimizer::Solver> getSolver() const;
/// Refresh the IK hierarchy of this IK module
virtual void refreshIKHierarchy() = 0;
/// Get the IK hierarchy of this IK module
const IKHierarchy& getIKHierarchy() const;
/// Compute the null spaces of each level of the hierarchy
const std::vector<Eigen::MatrixXd>& computeNullSpaces() const;
/// Get the current joint positions of the Skeleton associated with this
/// IK module.
Eigen::VectorXd getPositions() const;
/// Set the current joint positions of the Skeleton associated with this
/// IK module. The vector must include all DOFs in the Skeleton.
void setPositions(const Eigen::VectorXd& _q);
/// Get the Skeleton that this IK module is associated with
SkeletonPtr getSkeleton();
/// Get the Skeleton that this IK module is associated with
ConstSkeletonPtr getSkeleton() const;
/// This is the same as getSkeleton(). It is used by the HierarchicalIKPtr to
/// provide a common interface for the various IK smart pointer types.
SkeletonPtr getAffiliation();
/// This is the same as getSkeleton(). It is used by the HierarchicalIKPtr to
/// provide a common interface for the various IK smart pointer types.
ConstSkeletonPtr getAffiliation() const;
/// Clear the caches of this IK module. It should generally not be necessary
/// to call this function.
void clearCaches();
protected:
/// The HierarchicalIK::Objective Function is simply used to merge the
/// objective and null space objective functions that are being held by this
/// HierarchicalIK module. This class is not meant to be extended or
/// instantiated by a user. Call HierarchicalIK::resetProblem() to set
/// the objective of the module's Problem to an HierarchicalIK::Objective.
class Objective final : public Function, public optimizer::Function
{
public:
/// Constructor
Objective(const std::shared_ptr<HierarchicalIK>& _ik);
/// Virtual destructor
virtual ~Objective() = default;
// Documentation inherited
optimizer::FunctionPtr clone(
const std::shared_ptr<HierarchicalIK>& _newIK) const override;
// Documentation inherited
double eval(const Eigen::VectorXd &_x) override;
// Documentation inherited
void evalGradient(const Eigen::VectorXd& _x,
Eigen::Map<Eigen::VectorXd> _grad) override;
protected:
/// Pointer to this Objective's HierarchicalIK module
std::weak_ptr<HierarchicalIK> mIK;
/// Cache for the gradient computation
Eigen::VectorXd mGradCache;
};
/// The HierarchicalIK::Constraint Function is simply used to merge the
/// constraints of the InverseKinematics modules that belong to the hierarchy
/// of this HierarchicalIK module. This class is not meant to be extended or
/// instantiated by a user. Call HierarchicalIK::resetProblem() to set
/// the constraint of the module's Problem to an HierarchicalIK::Constraint.
class Constraint final : public Function, public optimizer::Function
{
public:
/// Constructor
Constraint(const std::shared_ptr<HierarchicalIK>& _ik);
/// Virtual destructor
virtual ~Constraint() = default;
// Documentation inherited
optimizer::FunctionPtr clone(
const std::shared_ptr<HierarchicalIK>& _newIK) const override;
// Documentation inherited
double eval(const Eigen::VectorXd& _x) override;
// Documentation inherited
void evalGradient(const Eigen::VectorXd& _x,
Eigen::Map<Eigen::VectorXd> _grad) override;
protected:
/// Pointer to this Constraint's HierarchicalIK module
std::weak_ptr<HierarchicalIK> mIK;
/// Cache for the gradient of a level
Eigen::VectorXd mLevelGradCache;
/// Cache for temporary gradients
Eigen::VectorXd mTempGradCache;
};
/// Constructor
HierarchicalIK(const SkeletonPtr& _skeleton);
/// Setup the module
void initialize(const std::shared_ptr<HierarchicalIK> my_ptr);
/// Copy the setup of this HierarchicalIK module into another HierarchicalIK
/// module
void copyOverSetup(const std::shared_ptr<HierarchicalIK>& _otherIK) const;
/// Pointer to the Skeleton that this IK is tied to
WeakSkeletonPtr mSkeleton;
/// Cache for the IK hierarcy
IKHierarchy mHierarchy;
/// The Problem that this IK module is maintaining
std::shared_ptr<optimizer::Problem> mProblem;
/// The Solver that this IK module will use
std::shared_ptr<optimizer::Solver> mSolver;
/// The Objective of this IK module
optimizer::FunctionPtr mObjective;
/// The null space Objective of this IK module
optimizer::FunctionPtr mNullSpaceObjective;
/// Weak pointer to self
std::weak_ptr<HierarchicalIK> mPtr;
/// Cache for the last positions
mutable Eigen::VectorXd mLastPositions;
/// Cache for null space computations
mutable std::vector<Eigen::MatrixXd> mNullSpaceCache;
/// Cache for a partial null space computation
mutable Eigen::MatrixXd mPartialNullspaceCache;
/// Cache for the null space SVD
mutable Eigen::JacobiSVD<math::Jacobian> mSVDCache;
/// Cache for Jacobians
mutable math::Jacobian mJacCache;
};
/// The CompositeIK class allows you to specify an arbitrary hierarchy of
/// InverseKinematics modules for a single Skeleton. Simply add in each IK
/// module that should be used.
class CompositeIK : public HierarchicalIK
{
public:
typedef std::unordered_set< std::shared_ptr<InverseKinematics> > ModuleSet;
typedef std::unordered_set< std::shared_ptr<const InverseKinematics> > ConstModuleSet;
/// Create a CompositeIK module
static std::shared_ptr<CompositeIK> create(const SkeletonPtr& _skel);
// Documentation inherited
std::shared_ptr<HierarchicalIK> clone(
const SkeletonPtr &_newSkel) const override;
/// Same as clone(), but passes back a more complete type
virtual std::shared_ptr<CompositeIK> cloneCompositeIK(
const SkeletonPtr& _newSkel) const;
/// Add an IK module to this CompositeIK. This function will return true if
/// the module belongs to the Skeleton that this CompositeIK is associated
/// with, otherwise it will return false.
bool addModule(const std::shared_ptr<InverseKinematics>& _ik);
/// Get the set of modules being used by this CompositeIK
const ModuleSet& getModuleSet();
/// Get the set of modules being used by this CompositeIK
ConstModuleSet getModuleSet() const;
// Documentation inherited
void refreshIKHierarchy() override;
protected:
/// Constructor
CompositeIK(const SkeletonPtr& _skel);
/// The set of modules being used by this CompositeIK
std::unordered_set< std::shared_ptr<InverseKinematics> > mModuleSet;
};
/// The WholeBodyIK class provides an interface for simultaneously solving all
/// the IK constraints of all BodyNodes and EndEffectors belonging to a single
/// Skeleton.
class WholeBodyIK : public HierarchicalIK
{
public:
/// Create a WholeBodyIK
static std::shared_ptr<WholeBodyIK> create(const SkeletonPtr& _skel);
// Documentation inherited
std::shared_ptr<HierarchicalIK> clone(
const SkeletonPtr &_newSkel) const override;
/// Same as clone(), but produces a more complete type
virtual std::shared_ptr<WholeBodyIK> cloneWholeBodyIK(
const SkeletonPtr& _newSkel) const;
// Documentation inherited
void refreshIKHierarchy() override;
protected:
/// Constructor
WholeBodyIK(const SkeletonPtr& _skel);
};
} // namespace dynamics
} // namespace kido
#endif // KIDO_DYNAMICS_HIERARCHICALIK_HPP_
|