Skip to content

Commit

Permalink
warn default density if mass is not specified
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Chen <[email protected]>
  • Loading branch information
iche033 committed Dec 14, 2024
1 parent 2d466af commit 2509d55
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
5 changes: 4 additions & 1 deletion include/sdf/Collision.hh
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,15 @@ namespace sdf
/// \param[in] _autoInertiaParams An ElementPtr to the auto_inertia_params
/// element to be used if the auto_inertia_params have not been set in this
/// collision.
/// \param[in] _warnIfDensityIsUnset True to generate a warning that
/// the default density value will be used if it is not explicitly set.
public: void CalculateInertial(
sdf::Errors &_errors,
gz::math::Inertiald &_inertial,
const ParserConfig &_config,
const std::optional<double> &_density,
sdf::ElementPtr _autoInertiaParams);
sdf::ElementPtr _autoInertiaParams,
bool _warnIfDensityIsUnset = true);

/// \brief Get a pointer to the SDF element that was used during
/// load.
Expand Down
16 changes: 15 additions & 1 deletion src/Collision.cc
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ void Collision::CalculateInertial(
gz::math::Inertiald &_inertial,
const ParserConfig &_config,
const std::optional<double> &_density,
sdf::ElementPtr _autoInertiaParams)
sdf::ElementPtr _autoInertiaParams,
bool _warnIfDensityIsUnset)
{
// Order of precedence for density:
double density;
Expand All @@ -292,7 +293,20 @@ void Collision::CalculateInertial(
else
{
// If density was not explicitly set, default value is used
// Send a warning about the default value being used if needed
density = DensityDefault();
if (_warnIfDensityIsUnset)
{
Error densityMissingErr(
ErrorCode::ELEMENT_MISSING,
"Collision is missing a <density> child element. "
"Using a default density value of " +
std::to_string(DensityDefault()) + " kg/m^3. "
);
enforceConfigurablePolicyCondition(
_config.WarningsPolicy(), densityMissingErr, _errors
);
}
}

// If this Collision's auto inertia params have not been set, then use the
Expand Down
11 changes: 8 additions & 3 deletions src/Link.cc
Original file line number Diff line number Diff line change
Expand Up @@ -669,20 +669,25 @@ void Link::ResolveAutoInertials(sdf::Errors &_errors,
return;
}

auto inertialElem = this->dataPtr->sdf->GetElement("inertial");
bool massSpecified = inertialElem->HasElement("mass");
// Warn about using default collision density value if mass is not specified
bool warnUseDefaultDensity = !massSpecified;

gz::math::Inertiald totalInertia;

for (sdf::Collision &collision : this->dataPtr->collisions)
{
gz::math::Inertiald collisionInertia;
collision.CalculateInertial(_errors, collisionInertia, _config,
this->dataPtr->density,
this->dataPtr->autoInertiaParams);
this->dataPtr->autoInertiaParams,
warnUseDefaultDensity);
totalInertia = totalInertia + collisionInertia;
}

// If mass is specified, scale inertia to match desired mass
auto inertialElem = this->dataPtr->sdf->GetElement("inertial");
if (inertialElem->HasElement("mass"))
if (massSpecified)
{
double mass = inertialElem->Get<double>("mass");
const gz::math::MassMatrix3d &totalMassMatrix = totalInertia.MassMatrix();
Expand Down

0 comments on commit 2509d55

Please sign in to comment.