-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathConstraint_Contact.cc
323 lines (263 loc) · 10.8 KB
/
Constraint_Contact.cc
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
/*
* RBDL - Rigid Body Dynamics Library
* Copyright (c) 2019 Matthew Millard <[email protected]>
*
* Licensed under the zlib license. See LICENSE for more details.
*/
#include <iostream>
#include <sstream>
#include <limits>
#include <assert.h>
#include "rbdl/rbdl_mathutils.h"
#include "rbdl/Logging.h"
#include "rbdl/Model.h"
#include "rbdl/Constraint_Contact.h"
#include "rbdl/Kinematics.h"
using namespace RigidBodyDynamics;
using namespace Math;
//==============================================================================
ContactConstraint::ContactConstraint():
Constraint("",ConstraintTypeContact,1,
std::numeric_limits<unsigned int>::max()){}
//==============================================================================
ContactConstraint::ContactConstraint(
const unsigned int bodyId,
const Math::Vector3d &bodyPoint,
const Math::Vector3d &groundConstraintUnitVector,
const char *contactConstraintName,
unsigned int userDefinedIdNumber,
bool enableBaumgarteStabilization,
double stabilizationTimeConstant,
bool velocityLevelConstraint):
Constraint(contactConstraintName,
ConstraintTypeContact,
unsigned(int(1)),
userDefinedIdNumber)
{
T.push_back(groundConstraintUnitVector);
dblA = std::numeric_limits<double>::epsilon()*10.;
#ifndef RBDL_USE_CASADI_MATH
assert(std::fabs(T[0].norm()-1.0)<= dblA);
#endif
groundPoint = Math::Vector3dZero;
bodyIds.push_back(bodyId);
bodyFrames.push_back(
Math::SpatialTransform(Math::Matrix3dIdentity, bodyPoint));
bodyIds.push_back(0);
bodyFrames.push_back(
Math::SpatialTransform(Math::Matrix3dIdentity, groundPoint));
setEnableBaumgarteStabilization(enableBaumgarteStabilization);
setBaumgarteTimeConstant(stabilizationTimeConstant);
//This constraint is not set up to be enforced at the position level:
//to do so the user would need to be able to set the ground point, an
//option I have thus far not given the user.
positionConstraint[0]=false;
velocityConstraint[0]=velocityLevelConstraint;
}
//==============================================================================
void ContactConstraint::bind(const Model &model)
{
//There are no dynamically-sized local matrices or vectors that
//need to be adjusted for this constraint
}
//==============================================================================
void ContactConstraint::calcConstraintJacobian( Model &model,
const double time,
const Math::VectorNd &Q,
const Math::VectorNd &QDot,
Math::MatrixNd &GSysUpd,
ConstraintCache &cache,
bool updateKinematics)
{
cache.mat3NA.setZero();
CalcPointJacobian(model, Q,bodyIds[0],bodyFrames[0].r,cache.mat3NA,
updateKinematics);
for(unsigned int i=0; i < sizeOfConstraint; ++i){
GSysUpd.block(rowInSystem+i,0,1,GSysUpd.cols()) =
T[i].transpose()*cache.mat3NA;
}
}
//==============================================================================
void ContactConstraint::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)
{
cache.vec3A = CalcPointAcceleration (model, Q, QDot, cache.vecNZeros,
bodyIds[0], bodyFrames[0].r,
updateKinematics);
for(unsigned int i=0; i < sizeOfConstraint; ++i){
gammaSysUpd.block(rowInSystem+i,0,1,1) =
-T[i].transpose()*cache.vec3A;
}
}
//==============================================================================
void ContactConstraint::calcPositionError(Model &model,
const double time,
const Math::VectorNd &Q,
Math::VectorNd &errSysUpd,
ConstraintCache &cache,
bool updateKinematics)
{
cache.vec3A = CalcBodyToBaseCoordinates(model,Q,bodyIds[0],bodyFrames[0].r,
updateKinematics) - groundPoint;
for(unsigned int i = 0; i < sizeOfConstraint; ++i){
if(positionConstraint[i]){
errSysUpd[rowInSystem+i] = cache.vec3A.dot( T[i] );
}else{
errSysUpd[rowInSystem+i] = 0.;
}
}
}
//==============================================================================
void ContactConstraint::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)
{
cache.vec3A = CalcPointVelocity(model,Q,QDot,bodyIds[0],bodyFrames[0].r,
updateKinematics);
for(unsigned int i = 0; i < sizeOfConstraint; ++i){
if(velocityConstraint[i]){
derrSysUpd[rowInSystem+i] = cache.vec3A.dot( T[i] );
}else{
derrSysUpd[rowInSystem+i] = 0.;
}
}
}
//==============================================================================
void ContactConstraint::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,
bool updateKinematics)
{
//Size the vectors of bodies, local frames, and spatial vectors
constraintBodiesUpd = bodyIds;
constraintBodyFramesUpd = bodyFrames;
cache.vec3A = CalcBodyToBaseCoordinates(model,Q,bodyIds[0],bodyFrames[0].r,
updateKinematics);
if(resolveAllInRootFrame){
constraintBodiesUpd[0] = constraintBodiesUpd[1];
constraintBodyFramesUpd[0].r = cache.vec3A;
constraintBodyFramesUpd[0].E = constraintBodyFramesUpd[1].E;
}
constraintForcesUpd.resize(bodyIds.size());
for(unsigned int i=0; i< bodyIds.size(); ++i){
constraintForcesUpd[i].setZero();
}
//Evaluate the total force the constraint applies to the contact point
cache.vec3A.setZero();
for(unsigned int i=0; i < sizeOfConstraint; ++i){
//The only reason that we can use T here is that it is resolved
//at the origin of the ground frame.
cache.vec3A += T[i]*LagrangeMultipliersSys[rowInSystem+i];
}
//Update the forces applied to the body in the frame of the body
if(resolveAllInRootFrame){
constraintForcesUpd[0].block(3,0,3,1) = cache.vec3A;
}else{
cache.mat3A = CalcBodyWorldOrientation(model,Q,bodyIds[0],false);
constraintForcesUpd[0].block(3,0,3,1) = cache.mat3A*cache.vec3A;
}
//Update the forces applied to the ground in the frame of the ground
constraintForcesUpd[1].block(3,0,3,1) = -cache.vec3A;
}
//==============================================================================
void ContactConstraint::
appendNormalVector(const Math::Vector3d& normal,
bool velocityLevelConstraint)
{
dblA = 10.0*std::numeric_limits<double>::epsilon();
//Make sure the normal is valid
#ifndef RBDL_USE_CASADI_MATH
assert( std::fabs(normal.norm()-1.) < dblA);
for(unsigned int i=0; i<sizeOfConstraint;++i){
assert(std::fabs(T[i].dot(normal)) <= dblA);
}
#endif
T.push_back(normal);
positionConstraint.push_back(false);
velocityConstraint.push_back(velocityLevelConstraint);
sizeOfConstraint++;
assert( sizeOfConstraint <= 3 && sizeOfConstraint > 0);
}
//==============================================================================
void ContactConstraint::
calcPointAccelerations(Model &model,
const Math::VectorNd &Q,
const Math::VectorNd &QDot,
const Math::VectorNd &QDDot,
std::vector<Math::Vector3d> &pointAccelerationsSysUpd,
bool updateKinematics)
{
pointAccelerationsSysUpd[rowInSystem] =
CalcPointAcceleration (model, Q, QDot, QDDot, bodyIds[0],
bodyFrames[0].r, updateKinematics);
for(unsigned int i=1; i<sizeOfConstraint;++i){
pointAccelerationsSysUpd[rowInSystem+i] =
pointAccelerationsSysUpd[rowInSystem];
}
}
//==============================================================================
void ContactConstraint::
calcPointAccelerations(Model &model,
const Math::VectorNd &Q,
const Math::VectorNd &QDot,
const Math::VectorNd &QDDot,
Math::Vector3d &pointAccelerationsUpd,
bool updateKinematics)
{
pointAccelerationsUpd = CalcPointAcceleration (model, Q, QDot, QDDot,
bodyIds[0], bodyFrames[0].r,
updateKinematics);
}
//==============================================================================
void ContactConstraint::
calcPointAccelerationError(
const std::vector<Math::Vector3d> &pointAccelerationsSys,
Math::VectorNd &ddErrSysUpd)
{
for(unsigned int i=0; i<sizeOfConstraint;++i){
ddErrSysUpd[rowInSystem+i] =
T[i].dot(pointAccelerationsSys[rowInSystem+i]);
}
}
void ContactConstraint::
calcPointForceJacobian(
Model &model,
const Math::VectorNd &Q,
ConstraintCache &cache,
std::vector<Math::SpatialVector> &fExtSysUpd,
bool updateKinematics)
{
cache.vec3A = CalcBodyToBaseCoordinates(
model,Q,bodyIds[0],bodyFrames[0].r,updateKinematics);
cache.stA.E.Identity();
cache.stA.r = -cache.vec3A;
cache.svecA[0]=0.;
cache.svecA[1]=0.;
cache.svecA[2]=0.;
for(unsigned int i=0; i<sizeOfConstraint;++i){
cache.svecA[3] = -T[i][0];
cache.svecA[4] = -T[i][1];
cache.svecA[5] = -T[i][2];
fExtSysUpd[rowInSystem+i] = cache.stA.applyAdjoint( cache.svecA );
}
}