Skip to content

Commit

Permalink
Add Link auto_inertia_params to API
Browse files Browse the repository at this point in the history
Pass //link/inertial/auto_inertia_params ElementPtr to
Collision::CalculateInertial and use it if the
Collision does not have auto_inertia_params of its own.

Signed-off-by: Steve Peters <[email protected]>
  • Loading branch information
scpeters committed Oct 28, 2023
1 parent 865dd08 commit 67bf92b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
6 changes: 5 additions & 1 deletion include/sdf/Collision.hh
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,15 @@ namespace sdf
/// \param[in] _density An optional density value to override the default
/// collision density. This value is used instead of DefaultDensity()
// if this collision's density has not been explicitly set.
/// \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.
public: void CalculateInertial(
sdf::Errors &_errors,
gz::math::Inertiald &_inertial,
const ParserConfig &_config,
const std::optional<double> &_density);
const std::optional<double> &_density,
sdf::ElementPtr _autoInertiaParams);

/// \brief Get a pointer to the SDF element that was used during
/// load.
Expand Down
12 changes: 12 additions & 0 deletions include/sdf/Link.hh
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ namespace sdf
/// \param[in] _density Density of the inertial.
public: void SetDensity(double _density);

/// \brief Get the ElementPtr to the <auto_inertia_params> element
/// This element can be used as a parent element to hold user-defined
/// params for the custom moment of inertia calculator.
/// \return ElementPtr object for the <auto_inertia_params> element.
public: sdf::ElementPtr AutoInertiaParams() const;

/// \brief Function to set the auto inertia params using a
/// sdf ElementPtr object
/// \param[in] _autoInertiaParams ElementPtr to <auto_inertia_params>
/// element
public: void SetAutoInertiaParams(const sdf::ElementPtr _autoInertiaParams);

/// \brief Get the number of visuals.
/// \return Number of visuals contained in this Link object.
public: uint64_t VisualCount() const;
Expand Down
15 changes: 12 additions & 3 deletions src/Collision.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,17 @@ void Collision::CalculateInertial(
gz::math::Inertiald &_inertial,
const ParserConfig &_config)
{
this->CalculateInertial(_errors, _inertial, _config, std::nullopt);
this->CalculateInertial(
_errors, _inertial, _config, std::nullopt, ElementPtr());
}

/////////////////////////////////////////////////
void Collision::CalculateInertial(
sdf::Errors &_errors,
gz::math::Inertiald &_inertial,
const ParserConfig &_config,
const std::optional<double> &_density)
const std::optional<double> &_density,
sdf::ElementPtr _autoInertiaParams)
{
// Order of precedence for density:
double density;
Expand Down Expand Up @@ -303,9 +305,16 @@ void Collision::CalculateInertial(
);
}

// If this Collision's auto inertia params have not been set, then use the
// params passed into this function.
sdf::ElementPtr autoInertiaParams = this->dataPtr->autoInertiaParams;
if (!autoInertiaParams)
{
autoInertiaParams = _autoInertiaParams;
}
auto geomInertial =
this->dataPtr->geom.CalculateInertial(_errors, _config,
density, this->dataPtr->autoInertiaParams);
density, autoInertiaParams);

if (!geomInertial)
{
Expand Down
25 changes: 24 additions & 1 deletion src/Link.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class sdf::Link::Implementation
/// calculations if the collision density has not been set.
public: std::optional<double> density;

/// \brief SDF element pointer to <auto_inertia_params> tag
public: sdf::ElementPtr autoInertiaParams{nullptr};

/// \brief The inertial information for this link.
public: gz::math::Inertiald inertial {{1.0,
gz::math::Vector3d::One, gz::math::Vector3d::Zero},
Expand Down Expand Up @@ -189,6 +192,13 @@ Errors Link::Load(ElementPtr _sdf, const ParserConfig &_config)
this->dataPtr->density = inertialElem->Get<double>("density");
}

// Load the auto_inertia_params element
if (inertialElem->HasElement("auto_inertia_params"))
{
this->dataPtr->autoInertiaParams =
inertialElem->GetElement("auto_inertia_params");
}

if (inertialElem->Get<bool>("auto"))
{
this->dataPtr->autoInertia = true;
Expand Down Expand Up @@ -330,6 +340,18 @@ void Link::SetDensity(double _density)
this->dataPtr->density = _density;
}

/////////////////////////////////////////////////
sdf::ElementPtr Link::AutoInertiaParams() const
{
return this->dataPtr->autoInertiaParams;
}

/////////////////////////////////////////////////
void Link::SetAutoInertiaParams(const sdf::ElementPtr _autoInertiaParams)
{
this->dataPtr->autoInertiaParams = _autoInertiaParams;
}

/////////////////////////////////////////////////
uint64_t Link::VisualCount() const
{
Expand Down Expand Up @@ -642,7 +664,8 @@ void Link::ResolveAutoInertials(sdf::Errors &_errors,
{
gz::math::Inertiald collisionInertia;
collision.CalculateInertial(_errors, collisionInertia, _config,
this->dataPtr->density);
this->dataPtr->density,
this->dataPtr->autoInertiaParams);
totalInertia = totalInertia + collisionInertia;
}

Expand Down

0 comments on commit 67bf92b

Please sign in to comment.