Rigid Body Dynamics Library
Constraint_Contact.cc
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#include <iostream>
9#include <sstream>
10#include <limits>
11#include <assert.h>
12
13#include "rbdl/rbdl_mathutils.h"
14#include "rbdl/Logging.h"
15
16#include "rbdl/Model.h"
18#include "rbdl/Kinematics.h"
19
20using namespace RigidBodyDynamics;
21using namespace Math;
22
23
24
25
26//==============================================================================
27ContactConstraint::ContactConstraint():
29 std::numeric_limits<unsigned int>::max()){}
30
31//==============================================================================
33 const unsigned int bodyId,
34 const Math::Vector3d &bodyPoint,
35 const Math::Vector3d &groundConstraintUnitVector,
36 const char *contactConstraintName,
37 unsigned int userDefinedIdNumber,
38 bool enableBaumgarteStabilization,
39 double stabilizationTimeConstant,
40 bool velocityLevelConstraint):
41 Constraint(contactConstraintName,
43 unsigned(int(1)),
44 userDefinedIdNumber)
45{
46
47 T.push_back(groundConstraintUnitVector);
48 dblA = std::numeric_limits<double>::epsilon()*10.;
49#ifndef RBDL_USE_CASADI_MATH
50 assert(std::fabs(T[0].norm()-1.0)<= dblA);
51#endif
52
54
55 bodyIds.push_back(bodyId);
56 bodyFrames.push_back(
58
59 bodyIds.push_back(0);
60 bodyFrames.push_back(
62
63 setEnableBaumgarteStabilization(enableBaumgarteStabilization);
64 setBaumgarteTimeConstant(stabilizationTimeConstant);
65
66 //This constraint is not set up to be enforced at the position level:
67 //to do so the user would need to be able to set the ground point, an
68 //option I have thus far not given the user.
69 positionConstraint[0]=false;
70 velocityConstraint[0]=velocityLevelConstraint;
71
72}
73
74
75
76//==============================================================================
77
78void ContactConstraint::bind(const Model &model)
79{
80
81 //There are no dynamically-sized local matrices or vectors that
82 //need to be adjusted for this constraint
83
84}
85
86
87//==============================================================================
88
90 const double time,
91 const Math::VectorNd &Q,
92 const Math::VectorNd &QDot,
93 Math::MatrixNd &GSysUpd,
94 ConstraintCache &cache,
95 bool updateKinematics)
96{
97 cache.mat3NA.setZero();
98 CalcPointJacobian(model, Q,bodyIds[0],bodyFrames[0].r,cache.mat3NA,
99 updateKinematics);
100
101 for(unsigned int i=0; i < sizeOfConstraint; ++i){
102 GSysUpd.block(rowInSystem+i,0,1,GSysUpd.cols()) =
103 T[i].transpose()*cache.mat3NA;
104 }
105}
106
107//==============================================================================
108
110 const double time,
111 const Math::VectorNd &Q,
112 const Math::VectorNd &QDot,
113 const Math::MatrixNd &GSys,
114 Math::VectorNd &gammaSysUpd,
115 ConstraintCache &cache,
116 bool updateKinematics)
117{
118
119
120 cache.vec3A = CalcPointAcceleration (model, Q, QDot, cache.vecNZeros,
121 bodyIds[0], bodyFrames[0].r,
122 updateKinematics);
123
124 for(unsigned int i=0; i < sizeOfConstraint; ++i){
125 gammaSysUpd.block(rowInSystem+i,0,1,1) =
126 -T[i].transpose()*cache.vec3A;
127 }
128}
129
130
131//==============================================================================
132
133
135 const double time,
136 const Math::VectorNd &Q,
137 Math::VectorNd &errSysUpd,
138 ConstraintCache &cache,
139 bool updateKinematics)
140{
141 cache.vec3A = CalcBodyToBaseCoordinates(model,Q,bodyIds[0],bodyFrames[0].r,
142 updateKinematics) - groundPoint;
143 for(unsigned int i = 0; i < sizeOfConstraint; ++i){
144 if(positionConstraint[i]){
145 errSysUpd[rowInSystem+i] = cache.vec3A.dot( T[i] );
146 }else{
147 errSysUpd[rowInSystem+i] = 0.;
148 }
149 }
150}
151
152//==============================================================================
153
155 const double time,
156 const Math::VectorNd &Q,
157 const Math::VectorNd &QDot,
158 const Math::MatrixNd &GSys,
159 Math::VectorNd &derrSysUpd,
160 ConstraintCache &cache,
161 bool updateKinematics)
162{
163 cache.vec3A = CalcPointVelocity(model,Q,QDot,bodyIds[0],bodyFrames[0].r,
164 updateKinematics);
165 for(unsigned int i = 0; i < sizeOfConstraint; ++i){
166 if(velocityConstraint[i]){
167 derrSysUpd[rowInSystem+i] = cache.vec3A.dot( T[i] );
168 }else{
169 derrSysUpd[rowInSystem+i] = 0.;
170 }
171 }
172}
173
174//==============================================================================
175
177 Model &model,
178 const double time,
179 const Math::VectorNd &Q,
180 const Math::VectorNd &QDot,
181 const Math::MatrixNd &GSys,
182 const Math::VectorNd &LagrangeMultipliersSys,
183 std::vector< unsigned int > &constraintBodiesUpd,
184 std::vector< Math::SpatialTransform > &constraintBodyFramesUpd,
185 std::vector< Math::SpatialVector > &constraintForcesUpd,
186 ConstraintCache &cache,
187 bool resolveAllInRootFrame,
188 bool updateKinematics)
189{
190
191 //Size the vectors of bodies, local frames, and spatial vectors
192 constraintBodiesUpd = bodyIds;
193 constraintBodyFramesUpd = bodyFrames;
194
195 cache.vec3A = CalcBodyToBaseCoordinates(model,Q,bodyIds[0],bodyFrames[0].r,
196 updateKinematics);
197
198 if(resolveAllInRootFrame){
199 constraintBodiesUpd[0] = constraintBodiesUpd[1];
200 constraintBodyFramesUpd[0].r = cache.vec3A;
201 constraintBodyFramesUpd[0].E = constraintBodyFramesUpd[1].E;
202 }
203
204
205 constraintForcesUpd.resize(bodyIds.size());
206 for(unsigned int i=0; i< bodyIds.size(); ++i){
207 constraintForcesUpd[i].setZero();
208 }
209 //Evaluate the total force the constraint applies to the contact point
210 cache.vec3A.setZero();
211 for(unsigned int i=0; i < sizeOfConstraint; ++i){
212 //The only reason that we can use T here is that it is resolved
213 //at the origin of the ground frame.
214 cache.vec3A += T[i]*LagrangeMultipliersSys[rowInSystem+i];
215 }
216
217 //Update the forces applied to the body in the frame of the body
218 if(resolveAllInRootFrame){
219 constraintForcesUpd[0].block(3,0,3,1) = cache.vec3A;
220 }else{
221 cache.mat3A = CalcBodyWorldOrientation(model,Q,bodyIds[0],false);
222 constraintForcesUpd[0].block(3,0,3,1) = cache.mat3A*cache.vec3A;
223 }
224
225 //Update the forces applied to the ground in the frame of the ground
226 constraintForcesUpd[1].block(3,0,3,1) = -cache.vec3A;
227}
228//==============================================================================
231 bool velocityLevelConstraint)
232{
233 dblA = 10.0*std::numeric_limits<double>::epsilon();
234
235 //Make sure the normal is valid
236#ifndef RBDL_USE_CASADI_MATH
237 assert( std::fabs(normal.norm()-1.) < dblA);
238 for(unsigned int i=0; i<sizeOfConstraint;++i){
239 assert(std::fabs(T[i].dot(normal)) <= dblA);
240 }
241#endif
242
243 T.push_back(normal);
244 positionConstraint.push_back(false);
245 velocityConstraint.push_back(velocityLevelConstraint);
247
248 assert( sizeOfConstraint <= 3 && sizeOfConstraint > 0);
249
250}
251
252//==============================================================================
253
255 calcPointAccelerations(Model &model,
256 const Math::VectorNd &Q,
257 const Math::VectorNd &QDot,
258 const Math::VectorNd &QDDot,
259 std::vector<Math::Vector3d> &pointAccelerationsSysUpd,
260 bool updateKinematics)
261{
262 pointAccelerationsSysUpd[rowInSystem] =
263 CalcPointAcceleration (model, Q, QDot, QDDot, bodyIds[0],
264 bodyFrames[0].r, updateKinematics);
265 for(unsigned int i=1; i<sizeOfConstraint;++i){
266 pointAccelerationsSysUpd[rowInSystem+i] =
267 pointAccelerationsSysUpd[rowInSystem];
268 }
269}
270
271//==============================================================================
272
274 calcPointAccelerations(Model &model,
275 const Math::VectorNd &Q,
276 const Math::VectorNd &QDot,
277 const Math::VectorNd &QDDot,
278 Math::Vector3d &pointAccelerationsUpd,
279 bool updateKinematics)
280{
281 pointAccelerationsUpd = CalcPointAcceleration (model, Q, QDot, QDDot,
282 bodyIds[0], bodyFrames[0].r,
283 updateKinematics);
284}
285
286//==============================================================================
287
290 const std::vector<Math::Vector3d> &pointAccelerationsSys,
291 Math::VectorNd &ddErrSysUpd)
292{
293 for(unsigned int i=0; i<sizeOfConstraint;++i){
294 ddErrSysUpd[rowInSystem+i] =
295 T[i].dot(pointAccelerationsSys[rowInSystem+i]);
296 }
297}
298
301 Model &model,
302 const Math::VectorNd &Q,
303 ConstraintCache &cache,
304 std::vector<Math::SpatialVector> &fExtSysUpd,
305 bool updateKinematics)
306{
308 model,Q,bodyIds[0],bodyFrames[0].r,updateKinematics);
309 cache.stA.E.Identity();
310 cache.stA.r = -cache.vec3A;
311 cache.svecA[0]=0.;
312 cache.svecA[1]=0.;
313 cache.svecA[2]=0.;
314 for(unsigned int i=0; i<sizeOfConstraint;++i){
315 cache.svecA[3] = -T[i][0];
316 cache.svecA[4] = -T[i][1];
317 cache.svecA[5] = -T[i][2];
318 fExtSysUpd[rowInSystem+i] = cache.stA.applyAdjoint( cache.svecA );
319 }
320}
321
322
323
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
void setEnableBaumgarteStabilization(bool flagEnableBaumgarteStabilization)
Definition: Constraint.h:566
void setBaumgarteTimeConstant(double tStab)
Calculates and sets the Baumgarte stabilization coefficients as a function of tStab,...
Definition: Constraint.h:555
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
std::vector< bool > positionConstraint
Definition: Constraint.h:740
unsigned int sizeOfConstraint
The number of rows that this constraint adds to G.
Definition: Constraint.h:717
std::vector< unsigned int > bodyIds
The index of the predecessor body in the vector of bodies in Model.
Definition: Constraint.h:723
std::vector< bool > velocityConstraint
Definition: Constraint.h:741
void calcPointForceJacobian(Model &model, const Math::VectorNd &Q, ConstraintCache &cache, std::vector< Math::SpatialVector > &fExtSysUpd, bool updateKinematics=false)
void appendNormalVector(const Math::Vector3d &normal, bool velocityLevelConstraint=true)
void calcPositionError(Model &model, const double time, const Math::VectorNd &Q, Math::VectorNd &errSysUpd, ConstraintCache &cache, bool updateKinematics=false) override
In this function the sub vector of this constraint is inserted into the position error vector of the...
Math::Vector3d groundPoint
The location of the ground reference point.
void bind(const Model &model) override
Any local memory that has a dimension of N, where N is the length of QDot, is resized in this functio...
void calcGamma(Model &model, const double time, const Math::VectorNd &Q, const Math::VectorNd &QDot, const Math::MatrixNd &GSys, Math::VectorNd &gammaSysUpd, ConstraintCache &cache, bool updateKinematics=false) override
In this function the vector of this constraint is inserted into the right-hand-side vector of the s...
void calcPointAccelerations(Model &model, const Math::VectorNd &Q, const Math::VectorNd &QDot, const Math::VectorNd &QDDot, std::vector< Math::Vector3d > &pointAccelerationSysUpd, bool updateKinematics=false)
std::vector< Math::Vector3d > T
A vector of the ground normal vectors used in this constraint.
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 > &constraintBodiesUpd, std::vector< Math::SpatialTransform > &constraintBodyFramesUpd, std::vector< Math::SpatialVector > &constraintForcesUpd, ConstraintCache &cache, bool resolveAllInRootFrame=false, bool updateKinematics=false) override
This function resolves the generalized forces this constraint applies to the system into the wrenches...
void calcConstraintJacobian(Model &model, const double time, const Math::VectorNd &Q, const Math::VectorNd &QDot, Math::MatrixNd &GSysUpd, ConstraintCache &cache, bool updateKinematics=false) override
In this function the matrix of this contraint is inserted into the system constraint Jacobian (GSys...
void calcVelocityError(Model &model, const double time, const Math::VectorNd &Q, const Math::VectorNd &QDot, const Math::MatrixNd &GSys, Math::VectorNd &derrSysUpd, ConstraintCache &cache, bool updateKinematics=false) override
In this function the sub vector of this constraint is inserted into the position error vector of the...
void calcPointAccelerationError(const std::vector< Math::Vector3d > &pointAccelerationsSys, Math::VectorNd &ddErrSysUpd)
RBDL_DLLAPI Matrix3d CalcBodyWorldOrientation(Model &model, const VectorNd &Q, const unsigned int body_id, bool update_kinematics)
Returns the orientation of a given body as 3x3 matrix.
Definition: Kinematics.cc:222
RBDL_DLLAPI Vector3d CalcBodyToBaseCoordinates(Model &model, const VectorNd &Q, unsigned int body_id, const Vector3d &point_body_coordinates, bool update_kinematics)
Returns the base coordinates of a point given in body coordinates.
Definition: Kinematics.cc:157
RBDL_DLLAPI void CalcPointJacobian(Model &model, const VectorNd &Q, unsigned int body_id, const Vector3d &point_position, MatrixNd &G, bool update_kinematics)
Computes the point jacobian for a point on a body.
Definition: Kinematics.cc:244
RBDL_DLLAPI Vector3d CalcPointVelocity(Model &model, const VectorNd &Q, const VectorNd &QDot, unsigned int body_id, const Vector3d &point_position, bool update_kinematics)
Computes the velocity of a point on a body.
Definition: Kinematics.cc:434
RBDL_DLLAPI Vector3d CalcPointAcceleration(Model &model, const VectorNd &Q, const VectorNd &QDot, const VectorNd &QDDot, unsigned int body_id, const Vector3d &point_position, bool update_kinematics)
Computes the linear acceleration of a point on a body.
Definition: Kinematics.cc:515
RBDL_DLLAPI Matrix3d Matrix3dIdentity
RBDL_DLLAPI Vector3d Vector3dZero
STL namespace.
MX_Xd_dynamic fabs(const MX_Xd_dynamic &m)
Definition: MX_Xd_utils.h:371
Math::SpatialVector svecA
Working SpatialVectors.
Definition: Constraint.h:57
Math::SpatialTransform stA
Working SpatialTransforms.
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
Compact representation of spatial transformations.
SpatialVector applyAdjoint(const SpatialVector &f_sp)