forked from gazebosim/gz-physics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulationFeatures.cc
More file actions
485 lines (421 loc) · 15.6 KB
/
SimulationFeatures.cc
File metadata and controls
485 lines (421 loc) · 15.6 KB
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
* Copyright (C) 2018 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <Eigen/Geometry>
#include <limits>
#include <memory>
#include <sstream>
#include <string>
#include <unordered_map>
#include <utility>
#include <dart/collision/CollisionObject.hpp>
#include <dart/collision/CollisionResult.hpp>
#include <dart/constraint/ConstraintSolver.hpp>
#include <dart/constraint/ContactConstraint.hpp>
#ifdef DART_HAS_CONTACT_SURFACE
#include <dart/constraint/ContactSurface.hpp>
#endif
#include <gz/common/Console.hh>
#include <gz/common/Profiler.hh>
#include <gz/math/Pose3.hh>
#include <gz/math/eigen3/Conversions.hh>
#include "gz/physics/GetContacts.hh"
#include "SimulationFeatures.hh"
#if DART_VERSION_AT_LEAST(6, 13, 0)
// The ContactSurfaceParams class was first added to version 6.10 of our fork
// of dart, and then merged upstream and released in version 6.13.0 with
// different public member variable names.
// See https://github.com/dartsim/dart/pull/1626 and
// https://github.com/gazebo-forks/dart/pull/22 for more info.
#define DART_HAS_UPSTREAM_FRICTION_VARIABLE_NAMES
// There's a sign difference between our fork and upstream dart in the
// implementation of surface velocities. Here, we assume that 6.13 is the
// upstream version. There's a potential that a user might update our fork to
// version 6.13.0 or later. To support that case, we'll assume
// DART_HAS_POSITIVE_CONTACT_SURFACE_MOTION_VELOCITY will be defined in the
// fork. Note, if we simply check for
// `DART_HAS_POSITIVE_CONTACT_SURFACE_MOTION_VELOCITY ` without the 6.13
// condition above, we might break users that are building with our 6.10 fork
// without updating or adding the
// DART_HAS_POSITIVE_CONTACT_SURFACE_MOTION_VELOCITY define in their version of
// dart.
#ifndef DART_HAS_POSITIVE_CONTACT_SURFACE_MOTION_VELOCITY
#define DART_HAS_NEGATIVE_CONTACT_SURFACE_MOTION_VELOCITY
#endif
#endif
namespace gz {
namespace physics {
namespace dartsim {
void SimulationFeatures::WorldForwardStep(
const Identity &_worldID,
ForwardStep::Output & _h,
ForwardStep::State & /*_x*/,
const ForwardStep::Input & _u)
{
GZ_PROFILE("SimulationFeatures::WorldForwardStep");
auto *world = this->ReferenceInterface<DartWorld>(_worldID);
auto *dtDur =
_u.Query<std::chrono::steady_clock::duration>();
const double tol = 1e-6;
if (dtDur)
{
std::chrono::duration<double> dt = *dtDur;
if (std::fabs(dt.count() - world->getTimeStep()) > tol)
{
world->setTimeStep(dt.count());
gzdbg << "Simulation timestep set to: " << world->getTimeStep()
<< std::endl;
}
}
for (const auto &[id, info] : this->links.idToObject)
{
if (info && info->inertial->FluidAddedMass().has_value())
{
auto com = Eigen::Vector3d(info->inertial->Pose().Pos().X(),
info->inertial->Pose().Pos().Y(),
info->inertial->Pose().Pos().Z());
auto mass = info->inertial->MassMatrix().Mass();
auto g = world->getGravity();
info->link->addExtForce(mass * g, com, false, true);
}
}
// TODO(MXG): Parse input
world->step();
for (auto &[ignore, modelInfo] : this->models.idToObject)
{
const Eigen::VectorXd &positions = modelInfo->model->getPositions();
if (positions.hasNaN())
{
std::stringstream ss;
ss << "Some links in model '" << modelInfo->localName
<< "' have invalid poses. ";
if (modelInfo->lastGoodPositions)
{
ss << "Resetting to last known poses.";
modelInfo->model->setPositions(*modelInfo->lastGoodPositions);
}
else
{
ss << "Resetting to zero poses.";
modelInfo->model->resetPositions();
}
gzerr << ss.str()
<< " Also resetting velocities and accelerations to zero."
<< std::endl;
modelInfo->model->resetVelocities();
modelInfo->model->resetAccelerations();
}
else
{
modelInfo->lastGoodPositions = positions;
}
}
this->WriteRequiredData(_h);
this->Write(_h.Get<ChangedWorldPoses>());
// TODO(MXG): Fill in state
}
void SimulationFeatures::Write(WorldPoses &_worldPoses) const
{
// remove link poses from the previous iteration
_worldPoses.entries.clear();
_worldPoses.entries.reserve(this->links.size());
for (const auto &[id, info] : this->links.idToObject)
{
WorldPose wp;
wp.pose = gz::math::eigen3::convert(
info->link->getWorldTransform());
wp.body = id;
_worldPoses.entries.push_back(wp);
}
}
void SimulationFeatures::Write(ChangedWorldPoses &_changedPoses) const
{
// remove link poses from the previous iteration
_changedPoses.entries.clear();
_changedPoses.entries.reserve(this->links.size());
std::unordered_map<std::size_t, math::Pose3d> newPoses;
for (const auto &[id, info] : this->links.idToObject)
{
// make sure the link exists
if (info && info->link)
{
WorldPose wp;
wp.pose = gz::math::eigen3::convert(
info->link->getWorldTransform());
wp.body = id;
// If the link's pose is new or has changed, save this new pose and
// add it to the output poses. Otherwise, keep the existing link pose
auto iter = this->prevLinkPoses.find(id);
if ((iter == this->prevLinkPoses.end()) ||
!iter->second.Pos().Equal(wp.pose.Pos(), 1e-6) ||
!iter->second.Rot().Equal(wp.pose.Rot(), 1e-6))
{
_changedPoses.entries.push_back(wp);
newPoses[id] = wp.pose;
}
else
newPoses[id] = iter->second;
}
}
// Save the new poses so that they can be used to check for updates in the
// next iteration. Re-setting this->prevLinkPoses with the contents of
// newPoses ensures that we aren't caching data for links that were removed
this->prevLinkPoses = std::move(newPoses);
}
SimulationFeatures::RayIntersection
SimulationFeatures::GetRayIntersectionFromLastStep(
const Identity &_worldID,
const LinearVector3d &_from,
const LinearVector3d &_to) const
{
auto *const world = this->ReferenceInterface<DartWorld>(_worldID);
auto collisionDetector = world->getConstraintSolver()->getCollisionDetector();
auto collisionGroup = world->getConstraintSolver()->getCollisionGroup().get();
// Perform raycast
dart::collision::RaycastOption option;
dart::collision::RaycastResult result;
collisionDetector->raycast(collisionGroup, _from, _to, option, &result);
// Currently, raycast supports only the Bullet collision detector.
// For other collision detectors, the result will always be NaN.
SimulationFeatures::RayIntersection intersection;
if (result.hasHit())
{
// Store intersection data if there is a ray hit
const auto &firstHit = result.mRayHits[0];
intersection.point = firstHit.mPoint;
intersection.normal = firstHit.mNormal;
intersection.fraction = firstHit.mFraction;
// Map DART CollisionObject to gz-physics shape identity
if (firstHit.mCollisionObject)
{
auto *dtShapeFrame = firstHit.mCollisionObject->getShapeFrame();
if (dtShapeFrame && this->shapes.HasEntity(dtShapeFrame->asShapeNode()))
{
intersection.collisionShapeId =
this->shapes.IdentityOf(dtShapeFrame->asShapeNode());
}
}
}
else
{
// Set invalid measurements to NaN according to REP-117
intersection.point =
Eigen::Vector3d::Constant(std::numeric_limits<double>::quiet_NaN());
intersection.normal =
Eigen::Vector3d::Constant(std::numeric_limits<double>::quiet_NaN());
intersection.fraction = std::numeric_limits<double>::quiet_NaN();
}
return intersection;
}
std::vector<SimulationFeatures::ContactInternal>
SimulationFeatures::GetContactsFromLastStep(const Identity &_worldID) const
{
std::vector<SimulationFeatures::ContactInternal> outContacts;
auto *const world = this->ReferenceInterface<DartWorld>(_worldID);
const auto colResult = world->getLastCollisionResult();
for (const auto &dtContact : colResult.getContacts())
{
auto contact = this->convertContact(dtContact);
if (contact)
outContacts.push_back(contact.value());
}
return outContacts;
}
std::optional<SimulationFeatures::ContactInternal>
SimulationFeatures::convertContact(
const dart::collision::Contact& _contact) const
{
auto *dtCollObj1 = _contact.collisionObject1;
auto *dtCollObj2 = _contact.collisionObject2;
auto *dtShapeFrame1 = dtCollObj1->getShapeFrame();
auto *dtShapeFrame2 = dtCollObj2->getShapeFrame();
if (this->shapes.HasEntity(dtShapeFrame1->asShapeNode()) &&
this->shapes.HasEntity(dtShapeFrame2->asShapeNode()))
{
std::size_t shape1ID =
this->shapes.IdentityOf(dtShapeFrame1->asShapeNode());
std::size_t shape2ID =
this->shapes.IdentityOf(dtShapeFrame2->asShapeNode());
CompositeData extraData;
// Add normal, depth and wrench to extraData.
auto& extraContactData =
extraData.Get<SimulationFeatures::ExtraContactData>();
extraContactData.force = _contact.force;
extraContactData.normal = _contact.normal;
extraContactData.depth = _contact.penetrationDepth;
return SimulationFeatures::ContactInternal {
this->GenerateIdentity(shape1ID, this->shapes.at(shape1ID)),
this->GenerateIdentity(shape2ID, this->shapes.at(shape2ID)),
_contact.point, extraData
};
}
return std::nullopt;
}
#ifdef DART_HAS_CONTACT_SURFACE
void SimulationFeatures::AddContactPropertiesCallback(
const Identity& _worldID, const std::string& _callbackID,
SurfaceParamsCallback _callback)
{
auto *world = this->ReferenceInterface<DartWorld>(_worldID);
auto handler = std::make_shared<GzContactSurfaceHandler>();
handler->surfaceParamsCallback = _callback;
handler->convertContact = [this](const dart::collision::Contact& _contact) {
return this->convertContact(_contact);
};
this->contactSurfaceHandlers[_callbackID] = handler;
world->getConstraintSolver()->addContactSurfaceHandler(handler);
}
bool SimulationFeatures::RemoveContactPropertiesCallback(
const Identity& _worldID, const std::string& _callbackID)
{
auto *world = this->ReferenceInterface<DartWorld>(_worldID);
if (this->contactSurfaceHandlers.find(_callbackID) !=
this->contactSurfaceHandlers.end())
{
const auto handler = this->contactSurfaceHandlers[_callbackID];
this->contactSurfaceHandlers.erase(_callbackID);
return world->getConstraintSolver()->removeContactSurfaceHandler(handler);
}
else
{
gzerr << "Could not find the contact surface handler to be removed"
<< std::endl;
return false;
}
}
dart::constraint::ContactSurfaceParams GzContactSurfaceHandler::createParams(
const dart::collision::Contact& _contact,
const size_t _numContactsOnCollisionObject) const
{
auto pDart = ContactSurfaceHandler::createParams(
_contact, _numContactsOnCollisionObject);
if (!this->surfaceParamsCallback)
return pDart;
typedef SetContactPropertiesCallbackFeature F;
typedef FeaturePolicy3d P;
typename F::ContactSurfaceParams<P> pGz;
auto convertMotionVelocity = [](Eigen::Vector3d _input) {
#ifdef DART_HAS_NEGATIVE_CONTACT_SURFACE_MOTION_VELOCITY
// The y and z components correspond to the velocities in the first and
// second friction directions. These have to be inverted in the upstream
// version of DART. https://github.com/gazebo-forks/dart/pull/33
_input.y() = -_input.y();
_input.z() = -_input.z();
#endif
return _input;
};
#ifdef DART_HAS_UPSTREAM_FRICTION_VARIABLE_NAMES
pGz.frictionCoeff = pDart.mPrimaryFrictionCoeff;
#else
pGz.frictionCoeff = pDart.mFrictionCoeff;
#endif
pGz.secondaryFrictionCoeff = pDart.mSecondaryFrictionCoeff;
#ifdef DART_HAS_UPSTREAM_FRICTION_VARIABLE_NAMES
pGz.slipCompliance = pDart.mPrimarySlipCompliance;
#else
pGz.slipCompliance = pDart.mSlipCompliance;
#endif
pGz.secondarySlipCompliance = pDart.mSecondarySlipCompliance;
pGz.restitutionCoeff = pDart.mRestitutionCoeff;
pGz.firstFrictionalDirection = pDart.mFirstFrictionalDirection;
pGz.contactSurfaceMotionVelocity =
convertMotionVelocity(pDart.mContactSurfaceMotionVelocity);
auto contactInternal = this->convertContact(_contact);
if (contactInternal)
{
this->surfaceParamsCallback(contactInternal.value(),
_numContactsOnCollisionObject, pGz);
if (pGz.frictionCoeff)
{
#ifdef DART_HAS_UPSTREAM_FRICTION_VARIABLE_NAMES
pDart.mPrimaryFrictionCoeff = pGz.frictionCoeff.value();
#else
pDart.mFrictionCoeff = pGz.frictionCoeff.value();
#endif
}
if (pGz.secondaryFrictionCoeff)
pDart.mSecondaryFrictionCoeff = pGz.secondaryFrictionCoeff.value();
if (pGz.slipCompliance)
{
#ifdef DART_HAS_UPSTREAM_FRICTION_VARIABLE_NAMES
pDart.mPrimarySlipCompliance = pGz.slipCompliance.value();
#else
pDart.mSlipCompliance = pGz.slipCompliance.value();
#endif
}
if (pGz.secondarySlipCompliance)
pDart.mSecondarySlipCompliance = pGz.secondarySlipCompliance.value();
if (pGz.restitutionCoeff)
pDart.mRestitutionCoeff = pGz.restitutionCoeff.value();
if (pGz.firstFrictionalDirection)
pDart.mFirstFrictionalDirection = pGz.firstFrictionalDirection.value();
if (pGz.contactSurfaceMotionVelocity)
{
pDart.mContactSurfaceMotionVelocity =
convertMotionVelocity(pGz.contactSurfaceMotionVelocity.value());
}
static bool warnedRollingFrictionCoeff = false;
if (!warnedRollingFrictionCoeff && pGz.rollingFrictionCoeff)
{
gzwarn << "DART doesn't support rolling friction setting" << std::endl;
warnedRollingFrictionCoeff = true;
}
static bool warnedSecondaryRollingFrictionCoeff = false;
if (!warnedSecondaryRollingFrictionCoeff &&
pGz.secondaryRollingFrictionCoeff)
{
gzwarn << "DART doesn't support secondary rolling friction setting"
<< std::endl;
warnedSecondaryRollingFrictionCoeff = true;
}
static bool warnedTorsionalFrictionCoeff = false;
if (!warnedTorsionalFrictionCoeff && pGz.torsionalFrictionCoeff)
{
gzwarn << "DART doesn't support torsional friction setting"
<< std::endl;
warnedTorsionalFrictionCoeff = true;
}
}
this->lastGzParams = pGz;
return pDart;
}
dart::constraint::ContactConstraintPtr
GzContactSurfaceHandler::createConstraint(
dart::collision::Contact& _contact,
const size_t _numContactsOnCollisionObject,
const double _timeStep) const
{
// this call sets this->lastGzParams
auto constraint = dart::constraint::ContactSurfaceHandler::createConstraint(
_contact, _numContactsOnCollisionObject, _timeStep);
typedef SetContactPropertiesCallbackFeature F;
typedef FeaturePolicy3d P;
typename F::ContactSurfaceParams<P>& p = this->lastGzParams;
if (this->lastGzParams.errorReductionParameter)
constraint->setErrorReductionParameter(p.errorReductionParameter.value());
if (this->lastGzParams.maxErrorAllowance)
constraint->setErrorAllowance(p.maxErrorAllowance.value());
if (this->lastGzParams.maxErrorReductionVelocity)
constraint->setMaxErrorReductionVelocity(
p.maxErrorReductionVelocity.value());
if (this->lastGzParams.constraintForceMixing)
constraint->setConstraintForceMixing(p.constraintForceMixing.value());
return constraint;
}
#endif
}
}
}