Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mimic constraint feature using bullet-featherstone #517

Merged
merged 50 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
5080bed
Skeletal code for SetMimicJointFeature
adityapande-1995 Sep 29, 2022
6584c4c
Added implementation, test to be written
adityapande-1995 Sep 29, 2022
0cf04bd
Skeletal test case
adityapande-1995 Sep 30, 2022
e862d37
Added world file
adityapande-1995 Sep 30, 2022
89baee9
Added basic test
adityapande-1995 Sep 30, 2022
845c076
Linter fix
adityapande-1995 Sep 30, 2022
ab64ba4
Fix tests
adityapande-1995 Oct 4, 2022
efe14ff
Delete unnecessary spaces
adityapande-1995 Oct 4, 2022
44848b7
Added test for prismatic joint
adityapande-1995 Oct 4, 2022
1bb3239
Prismatic world
adityapande-1995 Oct 4, 2022
5f53cb7
Added test for universal joint
adityapande-1995 Oct 6, 2022
4ce9d72
Merge branch 'gz-physics6' into aditya/mimic_constraint
adityapande-1995 Oct 11, 2022
1317a6d
Added tests between different joint types
adityapande-1995 Oct 11, 2022
943740c
Added reference to offset
adityapande-1995 Oct 11, 2022
87e8de3
Linter
adityapande-1995 Oct 12, 2022
4dd1b21
Added checks for DOFs and joint name
adityapande-1995 Oct 12, 2022
3e123fb
Merge branch 'gz-physics6' into aditya/mimic_constraint
adityapande-1995 Nov 15, 2022
4f19f38
Added a test to check vioaltion of consv of energy
adityapande-1995 Nov 16, 2022
e6b6b6e
Merge branch 'gz-physics6' into aditya/mimic_constraint
adityapande-1995 Nov 30, 2022
c845bfb
Merge branch 'gz-physics6' into aditya/mimic_constraint
adityapande-1995 Dec 12, 2022
1a3d345
Merge branch 'gz-physics6' into aditya/mimic_constraint
adityapande-1995 Dec 20, 2022
1fbeed2
Merge branch 'gz-physics6' into aditya/mimic_constraint
scpeters Apr 29, 2023
5d3ec50
Merge branch 'gz-physics6' into aditya/mimic_constraint
adityapande-1995 May 9, 2023
6a0294b
Change parent/child to leader/follower, added axis arg to SetMimicCon…
adityapande-1995 May 10, 2023
0b4f490
Changed mimicJoint to joint, minor typo correction
adityapande-1995 May 10, 2023
11e5ae6
bullet-featherstone: add mimic constraint
scpeters Jul 5, 2023
484abc4
Merge branch 'gz-physics6' into aditya/mimic_constraint
adityapande-1995 Jul 11, 2023
6878701
Fix spelling
scpeters Jul 12, 2023
d7f552f
SetJointMimicConstraint: add dof argument
scpeters Jul 12, 2023
a3a4e4e
Merge branch 'aditya/mimic_constraint' into scpeters/mimic_constraint…
scpeters Jul 12, 2023
b55550c
Add dof argument to bullet
scpeters Jul 13, 2023
6c42a98
Support multiple calls to SetMimicConstraint
scpeters Jul 13, 2023
cc9984d
Fix test for bullet
scpeters Jul 13, 2023
a7e4513
Use const ref
scpeters Jul 13, 2023
949a4a9
Merge branch 'aditya/mimic_constraint' into scpeters/mimic_constraint…
scpeters Jul 13, 2023
2f4687f
Use const refe in bullet and fix TODO
scpeters Jul 13, 2023
79ec53d
Adjust test tolerance
scpeters Jul 14, 2023
02c8502
Add test with fast/slow pendulum mimics
scpeters Jul 14, 2023
6e4cada
Skip fast/slow pendulums test for dartsim
scpeters Jul 15, 2023
e83a692
Redact dartsim implementation of mimic constraint
scpeters Jul 17, 2023
1f05981
Reduce line length to 80 characters
scpeters Jul 17, 2023
08dd8a7
Fix some test comments
scpeters Jul 17, 2023
1955236
Print console message at end of each test
scpeters Jul 18, 2023
a22257f
Merge branch 'main' into scpeters/mimic_constraint_bullet
scpeters Aug 24, 2023
d28ac5f
Use joint pointer, size_t in API, not strings
scpeters Aug 29, 2023
d9c6480
Remove gz-sim plugins from example worlds
scpeters Aug 29, 2023
9611e09
Use -1 for indexInModel if value is missing
scpeters Aug 29, 2023
db893e2
Add comments about mapping to bullet parameters
scpeters Aug 29, 2023
7b86015
Return bool, print error if dof != 0
scpeters Aug 29, 2023
1bb90c8
Fix build
scpeters Aug 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bullet-featherstone/src/Base.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <BulletDynamics/Featherstone/btMultiBody.h>
#include <BulletDynamics/Featherstone/btMultiBodyConstraintSolver.h>
#include <BulletDynamics/Featherstone/btMultiBodyDynamicsWorld.h>
#include <BulletDynamics/Featherstone/btMultiBodyGearConstraint.h>
#include <BulletDynamics/Featherstone/btMultiBodyJointFeedback.h>
#include <BulletDynamics/Featherstone/btMultiBodyJointMotor.h>
#include <BulletDynamics/Featherstone/btMultiBodyLinkCollider.h>
Expand Down Expand Up @@ -169,6 +170,7 @@ struct JointInfo
btScalar effort = 0;

std::shared_ptr<btMultiBodyFixedConstraint> fixedConstraint = nullptr;
std::shared_ptr<btMultiBodyGearConstraint> gearConstraint = nullptr;
std::shared_ptr<btMultiBodyJointFeedback> jointFeedback = nullptr;
};

Expand Down
70 changes: 70 additions & 0 deletions bullet-featherstone/src/JointFeatures.cc
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,76 @@ Wrench3d JointFeatures::GetJointTransmittedWrenchInJointFrame(
jointInfo->jointFeedback->m_reactionForces.getAngular());
return wrenchOut;
}

/////////////////////////////////////////////////
void JointFeatures::SetJointMimicConstraint(
const Identity &_id,
const std::size_t /*_dof*/, // bullet doesn't mimic multi-axis joints yet
const std::string &_joint,
const std::string &/*_axis*/,
const double _multiplier,
const double _offset,
const double _reference)
{
auto followerJoint = this->ReferenceInterface<JointInfo>(_id);
auto followerChild = this->ReferenceInterface<LinkInfo>(
followerJoint->childLinkID);
if (!followerChild->indexInModel.has_value())
{
// print error message
return;
}
const auto *model =
this->ReferenceInterface<ModelInfo>(followerJoint->model);

// Get world pointer and remove an existing mimic / gear constraint
auto *world = this->ReferenceInterface<WorldInfo>(model->world);
if (followerJoint->gearConstraint)
{
world->world->removeMultiBodyConstraint(
followerJoint->gearConstraint.get());
followerJoint->gearConstraint.reset();
}


// This would be easier with EntityManagementFeatures::GetJoint()
const auto leaderJointIt = model->jointNameToEntityId.find(_joint);
if (leaderJointIt == model->jointNameToEntityId.end())
{
// print error message
return;
}
auto leaderJointId =
this->GenerateIdentity(leaderJointIt->second,
this->joints.at(leaderJointIt->second));
auto leaderJoint = this->ReferenceInterface<JointInfo>(leaderJointId);
auto leaderChild = this->ReferenceInterface<LinkInfo>(
leaderJoint->childLinkID);
if (!followerChild->indexInModel.has_value())
{
// print error message
return;
}

followerJoint->gearConstraint = std::make_shared<btMultiBodyGearConstraint>(
model->body.get(),
*followerChild->indexInModel,
model->body.get(),
*leaderChild->indexInModel,
btVector3(0, 0, 0),
btVector3(0, 0, 0),
btMatrix3x3::getIdentity(),
btMatrix3x3::getIdentity());
followerJoint->gearConstraint->setGearRatio(btScalar(-_multiplier));
followerJoint->gearConstraint->setRelativePositionTarget(
btScalar(_offset - _multiplier * _reference));
// TODO(scpeters): figure out what is a good value for this
followerJoint->gearConstraint->setMaxAppliedImpulse(btScalar(1e8));
// setErp is needed to correct position constraint errors
// this is especially relevant to the offset and reference parameters
followerJoint->gearConstraint->setErp(btScalar(0.3));
world->world->addMultiBodyConstraint(followerJoint->gearConstraint.get());
}
} // namespace bullet_featherstone
} // namespace physics
} // namespace gz
12 changes: 12 additions & 0 deletions bullet-featherstone/src/JointFeatures.hh
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ struct JointFeatureList : FeatureList<

GetJointTransmittedWrench,

SetMimicConstraintFeature,

FixedJointCast
> { };

Expand Down Expand Up @@ -141,6 +143,16 @@ class JointFeatures :
// ----- Transmitted wrench -----
public: Wrench3d GetJointTransmittedWrenchInJointFrame(
const Identity &_id) const override;

// ----- Mimic joint constraint -----
public: void SetJointMimicConstraint(
const Identity &_id,
const std::size_t _dof,
const std::string &_joint,
const std::string &_axis,
const double _multiplier,
const double _offset,
const double _reference) override;
};
} // namespace bullet_featherstone
} // namespace physics
Expand Down
48 changes: 48 additions & 0 deletions include/gz/physics/Joint.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <gz/physics/FrameSemantics.hh>
#include <gz/physics/Geometry.hh>

#include <string>

namespace gz
{
namespace physics
Expand Down Expand Up @@ -584,6 +586,52 @@ namespace gz
};
};

/////////////////////////////////////////////////
/// \brief This feature sets the mimic constraint for this Joint.
class GZ_PHYSICS_VISIBLE SetMimicConstraintFeature
: public virtual Feature
{
/// \brief The Joint API for setting the mimic joint constraint.
/// \param[in] _dof
/// The desired generalized coordinate corresponding to the follower
/// axis within this joint. Values start from 0 and stop before
/// Joint::GetDegreesOfFreedom().
/// \param[in] _joint
/// name of the joint to be mimicked, i.e. the leader joint.
/// \param[in] _multiplier
/// The multiplier to be applied to position of leader joint.
/// \param[in] _offset
/// The offset to be applied to position of leader joint after
/// the multiplier is applied.
public: template <typename PolicyT, typename FeaturesT>
class Joint : public virtual Feature::Joint<PolicyT, FeaturesT>
{
public: using Scalar = typename PolicyT::Scalar;

public: void SetMimicConstraint(
const std::size_t _dof,
const std::string &_joint,
const std::string &_axis,
Scalar _multiplier,
Scalar _offset,
Scalar _reference);
};

/// \private The implementation API for setting the mimic constraint.
public: template <typename PolicyT>
class Implementation : public virtual Feature::Implementation<PolicyT>
{
public: using Scalar = typename PolicyT::Scalar;

// See Joint::MimicConstraint above
public: virtual void SetJointMimicConstraint(
const Identity &_id,
const std::size_t _dof,
const std::string &_joint,
const std::string &_axis,
Scalar _multiplier, Scalar _offset, Scalar _reference) = 0;
};
};
}
}

Expand Down
16 changes: 16 additions & 0 deletions include/gz/physics/detail/Joint.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <gz/physics/Joint.hh>
#include "gz/physics/detail/FrameSemantics.hh"

#include <string>

namespace gz
{
namespace physics
Expand Down Expand Up @@ -214,6 +216,20 @@ namespace gz
->SetJointMaxEffort(this->identity, _dof, _value);
}

/////////////////////////////////////////////////
template <typename PolicyT, typename FeaturesT>
void SetMimicConstraintFeature::Joint<PolicyT, FeaturesT>::
SetMimicConstraint(
const std::size_t _dof,
const std::string &_joint,
const std::string &_axis,
Scalar _multiplier, Scalar _offset, Scalar _reference)
{
this->template Interface<SetMimicConstraintFeature>()
->SetJointMimicConstraint(this->identity, _dof, _joint,
_axis, _multiplier, _offset, _reference);
}

/////////////////////////////////////////////////
template <typename PolicyT, typename FeaturesT>
void DetachJointFeature::Joint<PolicyT, FeaturesT>::Detach()
Expand Down
1 change: 1 addition & 0 deletions test/common_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set(tests
construct_empty_world
free_joint_features
joint_features
joint_mimic_features
joint_transmitted_wrench_features
kinematic_features
link_features
Expand Down
Loading