Rigid Body Dynamics Library
Constraint.h
Go to the documentation of this file.
1/*
2 * RBDL - Rigid Body Dynamics Library
3 * Copyright (c) 2019 Matthew Millard <millard.matthew@gmail.com>
4 *
5 * Licensed under the zlib license. See LICENSE for more details.
6 */
7
8#ifndef RBDL_CONSTRAINT_H
9#define RBDL_CONSTRAINT_H
10
11#include <rbdl/rbdl_math.h>
12#include <rbdl/rbdl_mathutils.h>
13#include <assert.h>
14
15namespace RigidBodyDynamics {
16
23};
24
35
38
41
44
47
51
55
58
61
63};
64
65
139class RBDL_DLLAPI Constraint {
140 public:
141
142
143//==============================================================================
144// Extend the functions below for your own constraint
145//==============================================================================
154 virtual void bind(const Model &model)=0;
155
156
179 virtual void calcConstraintJacobian( Model &model,
180 const double time,
181 const Math::VectorNd &Q,
182 const Math::VectorNd &QDot,
183 Math::MatrixNd &GSysOutput,
184 ConstraintCache &cache,
185 bool updateKinematics=false) = 0;
186
211 virtual void calcGamma( Model &model,
212 const double time,
213 const Math::VectorNd &Q,
214 const Math::VectorNd &QDot,
215 const Math::MatrixNd &GSys,
216 Math::VectorNd &gammaSysOutput,
217 ConstraintCache &cache,
218 bool updateKinematics=false) = 0;
219
220
243 virtual void calcPositionError( Model &model,
244 const double time,
245 const Math::VectorNd &Q,
246 Math::VectorNd &errSysOutput,
247 ConstraintCache &cache,
248 bool updateKinematics=false) = 0;
249
250
279 virtual void calcVelocityError( Model &model,
280 const double time,
281 const Math::VectorNd &Q,
282 const Math::VectorNd &QDot,
283 const Math::MatrixNd &GSys,
284 Math::VectorNd &derrSysOutput,
285 ConstraintCache &cache,
286 bool updateKinematics=false) = 0;
287
288
348 Model &model,
349 const double time,
350 const Math::VectorNd &Q,
351 const Math::VectorNd &QDot,
352 const Math::MatrixNd &GSys,
353 const Math::VectorNd &LagrangeMultipliersSys,
354 std::vector< unsigned int > &constraintBodiesOutput,
355 std::vector< Math::SpatialTransform > &constraintBodyFramesOutput,
356 std::vector< Math::SpatialVector > &constraintForcesOutput,
357 ConstraintCache &cache,
358 bool resolveAllInRootFrame = false,
359 bool updateKinematics=false) = 0;
360
361
362//==============================================================================
363// DO NOT TOUCH!!!
364//==============================================================================
365
366 virtual ~Constraint(){};
367
369
386 Constraint(const char* nameOfConstraint,
387 unsigned int typeOfConstraint,
388 unsigned int sizeOfConstraint,
389 unsigned int userDefinedIdNumber):
390 typeOfConstraint(typeOfConstraint),
391 id(userDefinedIdNumber),
392 sizeOfConstraint(sizeOfConstraint),
393 baumgarteParameters(1./0.1,1./0.1),
394 baumgarteEnabled(false)
395 {
396 name = "";
397 if(nameOfConstraint){
398 name = nameOfConstraint;
399 }
400 positionConstraint.resize(sizeOfConstraint);
401 velocityConstraint.resize(sizeOfConstraint);
402 for(unsigned int i=0; i<sizeOfConstraint;++i){
403 positionConstraint[i]=false;
404 velocityConstraint[i]=false;
405 }
406 }
407
408
417 const unsigned int rowIndex)
418 {
419 rowInSystem = rowIndex;
420 }
421
425 unsigned int getUserDefinedId(){
426 return id;
427 }
428
432 void setUserDefinedId(unsigned int userDefinedId){
433 id = userDefinedId;
434 }
435
442 Math::MatrixNd &GConstraint){
443 GConstraint = GSys.block(rowInSystem,0,sizeOfConstraint,GSys.cols());
444 }
445
446
452 void getGamma(const Math::VectorNd &gammaSys,
453 Math::VectorNd &gammaConstraint){
454 gammaConstraint = gammaSys.block(rowInSystem,0,sizeOfConstraint,1);
455 }
456
463 Math::VectorNd &errConstraint){
464 errConstraint = errSys.block(rowInSystem,0,sizeOfConstraint,1);
465 }
466
467
473 void getVelocityError(const Math::VectorNd &derrSys,
474 Math::VectorNd &derrConstraint){
475 derrConstraint = derrSys.block(rowInSystem,0,sizeOfConstraint,1);
476 }
477
485 bgParamsUpd = baumgarteParameters;
486 }
487
494 const Math::VectorNd &errVel,
495 Math::VectorNd &baumgarteForces)
496 {
497
498 baumgarteForces=(-2*baumgarteParameters[0]*errVel
499 -baumgarteParameters[1]*baumgarteParameters[1]*errPos);
500 }
501
508 const Math::VectorNd &errVelSys,
509 Math::VectorNd &gammaSysOutput)
510 {
511
512 //Here a for loop is used rather than a block operation
513 //to be compatible with SimpleMath.
514 for(unsigned int i=0; i<sizeOfConstraint;++i){
515 gammaSysOutput[rowInSystem+i] +=
516 -2.*baumgarteParameters[0]*errVelSys[rowInSystem+i]
517 -(baumgarteParameters[1]*baumgarteParameters[1]
518 )*errPosSys[rowInSystem+i];
519 }
520 }
521
526 unsigned int getConstraintType(){
527 return typeOfConstraint;
528 }
529
533 unsigned int getConstraintSize(){
534 return sizeOfConstraint;
535 }
536
541 unsigned int getConstraintIndex(){
542 return rowInSystem;
543 }
544
545
555 void setBaumgarteTimeConstant(double tStab){
556 assert(tStab > 0);
557 baumgarteParameters[0] = 1./tStab;
558 baumgarteParameters[1] = 1./tStab;
559 }
560
561
566 void setEnableBaumgarteStabilization(bool flagEnableBaumgarteStabilization){
567 baumgarteEnabled = flagEnableBaumgarteStabilization;
568 }
569
574 return baumgarteEnabled;
575 }
576
577
584 void enableConstraintErrorFromPositionLevel(unsigned int constraintSubIndex)
585 {
586 assert(constraintSubIndex < sizeOfConstraint);
587 positionConstraint[constraintSubIndex] = true;
588 velocityConstraint[constraintSubIndex] = true;
589 }
590
599 void enableConstraintErrorFromVelocityLevel(unsigned int constraintSubIndex)
600 {
601 assert(constraintSubIndex < sizeOfConstraint);
602 positionConstraint[constraintSubIndex] = false;
603 velocityConstraint[constraintSubIndex] = true;
604 }
605
606
615 void enableConstraintErrorFromAccelerationLevel(unsigned int constraintSubIndex)
616 {
617 assert(constraintSubIndex < sizeOfConstraint);
618 positionConstraint[constraintSubIndex] = false;
619 velocityConstraint[constraintSubIndex] = false;
620 }
621
628 {
629 for(unsigned int i=0; i<sizeOfConstraint;++i){
630 positionConstraint[i] = true;
631 velocityConstraint[i] = true;
632 }
633 }
634
641 {
642 for(unsigned int i=0; i<sizeOfConstraint;++i){
643 positionConstraint[i] = false;
644 velocityConstraint[i] = true;
645 }
646 }
647
654 {
655 for(unsigned int i=0; i<sizeOfConstraint;++i){
656 positionConstraint[i] = false;
657 velocityConstraint[i] = false;
658 }
659 }
660
661
668 bool getPositionLevelError(unsigned int constraintSubIndex){
669 assert(constraintSubIndex < sizeOfConstraint);
670 return positionConstraint[constraintSubIndex];
671 }
672
679 bool getVelocityLevelError(unsigned int constraintSubIndex){
680 assert(constraintSubIndex < sizeOfConstraint);
681 return velocityConstraint[constraintSubIndex];
682 }
683
687 const char* getName(){
688 return name.c_str();
689 }
690
694 const std::vector< unsigned int >& getBodyIds(){
695 return bodyIds;
696 }
697
703 const std::vector< Math::SpatialTransform >& getBodyFrames(){
704 return bodyFrames;
705 }
706
707 protected:
709 std::string name;
710
712 unsigned int id;
714 unsigned int typeOfConstraint;
715
717 unsigned int sizeOfConstraint;
718
720 unsigned int rowInSystem;
721
723 std::vector< unsigned int > bodyIds;
724
726 std::vector< Math::SpatialTransform > bodyFrames;
727
731
734
740 std::vector< bool > positionConstraint;
741 std::vector< bool > velocityConstraint;
742};
743
744
745
746
749}
750
751/* namespace RigidBodyDynamics */
752
753/* RBDL_CONSTRAINT_H */
754#endif
unsigned int cols() const
Definition: MX_Xd_dynamic.h:99
MX_Xd_SubMatrix block(unsigned int row_start, unsigned int col_start)
Interface to define general-purpose constraints.
Definition: Constraint.h:139
Math::Vector2d baumgarteParameters
Definition: Constraint.h:730
void setEnableBaumgarteStabilization(bool flagEnableBaumgarteStabilization)
Definition: Constraint.h:566
bool baumgarteEnabled
A flag which enables or disables Baumgarte stabilization.
Definition: Constraint.h:733
void enableConstraintErrorFromPositionLevel()
: Will set the all elements in positionConstraint and velocityConstraint to be consistent with a cons...
Definition: Constraint.h:627
unsigned int getUserDefinedId()
Definition: Constraint.h:425
void enableConstraintErrorFromAccelerationLevel(unsigned int constraintSubIndex)
: Will set the vectors positionConstraint and velocityConstraint to be consistent with a constraint t...
Definition: Constraint.h:615
virtual void calcConstraintJacobian(Model &model, const double time, const Math::VectorNd &Q, const Math::VectorNd &QDot, Math::MatrixNd &GSysOutput, ConstraintCache &cache, bool updateKinematics=false)=0
In this function the matrix of this contraint is inserted into the system constraint Jacobian (GSys...
void setBaumgarteTimeConstant(double tStab)
Calculates and sets the Baumgarte stabilization coefficients as a function of tStab,...
Definition: Constraint.h:555
bool getVelocityLevelError(unsigned int constraintSubIndex)
Returns the boolean value that determines whether or not velocity-level errors are computed for this ...
Definition: Constraint.h:679
unsigned int getConstraintSize()
Definition: Constraint.h:533
const std::vector< Math::SpatialTransform > & getBodyFrames()
Definition: Constraint.h:703
void setUserDefinedId(unsigned int userDefinedId)
Definition: Constraint.h:432
void getGamma(const Math::VectorNd &gammaSys, Math::VectorNd &gammaConstraint)
Definition: Constraint.h:452
void enableConstraintErrorFromVelocityLevel()
: Will set the all elements in positionConstraint and velocityConstraint to be consistent with a cons...
Definition: Constraint.h:640
virtual void calcGamma(Model &model, const double time, const Math::VectorNd &Q, const Math::VectorNd &QDot, const Math::MatrixNd &GSys, Math::VectorNd &gammaSysOutput, ConstraintCache &cache, bool updateKinematics=false)=0
In this function the vector of this constraint is inserted into the right-hand-side vector of the s...
void getBaumgarteStabilizationParameters(Math::Vector2d &bgParamsUpd)
Definition: Constraint.h:484
std::vector< Math::SpatialTransform > bodyFrames
Transform from the frame of the predecessor body to the constraint frame.
Definition: Constraint.h:726
unsigned int rowInSystem
The first row in G that corresponds to this constraint.
Definition: Constraint.h:720
void enableConstraintErrorFromPositionLevel(unsigned int constraintSubIndex)
: Will set the vectors positionConstraint and velocityConstraint to be consistent with a constraint t...
Definition: Constraint.h:584
void getBaumgarteStabilizationForces(const Math::VectorNd &errPos, const Math::VectorNd &errVel, Math::VectorNd &baumgarteForces)
Definition: Constraint.h:493
std::vector< bool > positionConstraint
Definition: Constraint.h:740
void getConstraintJacobian(const Math::MatrixNd &GSys, Math::MatrixNd &GConstraint)
Definition: Constraint.h:441
unsigned int getConstraintIndex()
Definition: Constraint.h:541
const std::vector< unsigned int > & getBodyIds()
Definition: Constraint.h:694
unsigned int sizeOfConstraint
The number of rows that this constraint adds to G.
Definition: Constraint.h:717
std::string name
A user defined name which is unique to this constraint set.
Definition: Constraint.h:709
void enableConstraintErrorFromVelocityLevel(unsigned int constraintSubIndex)
: Will set the vectors positionConstraint and velocityConstraint to be consistent with a constraint t...
Definition: Constraint.h:599
virtual void calcConstraintForces(Model &model, const double time, const Math::VectorNd &Q, const Math::VectorNd &QDot, const Math::MatrixNd &GSys, const Math::VectorNd &LagrangeMultipliersSys, std::vector< unsigned int > &constraintBodiesOutput, std::vector< Math::SpatialTransform > &constraintBodyFramesOutput, std::vector< Math::SpatialVector > &constraintForcesOutput, ConstraintCache &cache, bool resolveAllInRootFrame=false, bool updateKinematics=false)=0
This function resolves the generalized forces this constraint applies to the system into the wrenches...
void addToConstraintSet(const unsigned int rowIndex)
This function is called by the functions in ConstraintSet that add a Constraint to the system....
Definition: Constraint.h:416
unsigned int id
A user defined id which is unique to this constraint set.
Definition: Constraint.h:712
bool getPositionLevelError(unsigned int constraintSubIndex)
Returns the boolean value that determines whether or not position-level errors are computed for this ...
Definition: Constraint.h:668
void getVelocityError(const Math::VectorNd &derrSys, Math::VectorNd &derrConstraint)
Definition: Constraint.h:473
virtual void calcPositionError(Model &model, const double time, const Math::VectorNd &Q, Math::VectorNd &errSysOutput, ConstraintCache &cache, bool updateKinematics=false)=0
In this function the sub vector of this constraint is inserted into the position error vector of the...
std::vector< unsigned int > bodyIds
The index of the predecessor body in the vector of bodies in Model.
Definition: Constraint.h:723
void getPositionError(const Math::VectorNd &errSys, Math::VectorNd &errConstraint)
Definition: Constraint.h:462
std::vector< bool > velocityConstraint
Definition: Constraint.h:741
void addInBaumgarteStabilizationForces(const Math::VectorNd &errPosSys, const Math::VectorNd &errVelSys, Math::VectorNd &gammaSysOutput)
Definition: Constraint.h:507
virtual void bind(const Model &model)=0
Any local memory that has a dimension of N, where N is the length of QDot, is resized in this functio...
unsigned int getConstraintType()
Definition: Constraint.h:526
unsigned int typeOfConstraint
The type of this constraint.
Definition: Constraint.h:714
virtual void calcVelocityError(Model &model, const double time, const Math::VectorNd &Q, const Math::VectorNd &QDot, const Math::MatrixNd &GSys, Math::VectorNd &derrSysOutput, ConstraintCache &cache, bool updateKinematics=false)=0
In this function the sub vector of this constraint is inserted into the position error vector of the...
Constraint(const char *nameOfConstraint, unsigned int typeOfConstraint, unsigned int sizeOfConstraint, unsigned int userDefinedIdNumber)
Definition: Constraint.h:386
void enableConstraintErrorFromAccelerationLevel()
: Will set the all elements in positionConstraint and velocityConstraint to be consistent with a cons...
Definition: Constraint.h:653
ConstraintType
Enum to describe the type of a constraint.
Definition: Constraint.h:18
Math::SpatialVector svecA
Working SpatialVectors.
Definition: Constraint.h:57
Math::SpatialTransform stB
Definition: Constraint.h:60
Math::SpatialTransform stA
Working SpatialTransforms.
Definition: Constraint.h:60
Math::VectorNd vecNA
Working vectors that are sized to match the length of qdot.
Definition: Constraint.h:40
Math::SpatialTransform stD
Definition: Constraint.h:60
Math::VectorNd vecNZeros
Here N is taken to mean the number of elements in QDot.
Definition: Constraint.h:37
Math::Vector3d vec3A
Working Vector3d entries.
Definition: Constraint.h:43
Math::Matrix3d mat3A
Working Matrix3d entries.
Definition: Constraint.h:46
Math::SpatialTransform stC
Definition: Constraint.h:60
Compact representation of spatial transformations.