Skip to content

Commit 67bf92b

Browse files
committed
Add Link auto_inertia_params to API
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 <scpeters@openrobotics.org>
1 parent 865dd08 commit 67bf92b

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

include/sdf/Collision.hh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,15 @@ namespace sdf
167167
/// \param[in] _density An optional density value to override the default
168168
/// collision density. This value is used instead of DefaultDensity()
169169
// if this collision's density has not been explicitly set.
170+
/// \param[in] _autoInertiaParams An ElementPtr to the auto_inertia_params
171+
/// element to be used if the auto_inertia_params have not been set in this
172+
/// collision.
170173
public: void CalculateInertial(
171174
sdf::Errors &_errors,
172175
gz::math::Inertiald &_inertial,
173176
const ParserConfig &_config,
174-
const std::optional<double> &_density);
177+
const std::optional<double> &_density,
178+
sdf::ElementPtr _autoInertiaParams);
175179

176180
/// \brief Get a pointer to the SDF element that was used during
177181
/// load.

include/sdf/Link.hh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ namespace sdf
8989
/// \param[in] _density Density of the inertial.
9090
public: void SetDensity(double _density);
9191

92+
/// \brief Get the ElementPtr to the <auto_inertia_params> element
93+
/// This element can be used as a parent element to hold user-defined
94+
/// params for the custom moment of inertia calculator.
95+
/// \return ElementPtr object for the <auto_inertia_params> element.
96+
public: sdf::ElementPtr AutoInertiaParams() const;
97+
98+
/// \brief Function to set the auto inertia params using a
99+
/// sdf ElementPtr object
100+
/// \param[in] _autoInertiaParams ElementPtr to <auto_inertia_params>
101+
/// element
102+
public: void SetAutoInertiaParams(const sdf::ElementPtr _autoInertiaParams);
103+
92104
/// \brief Get the number of visuals.
93105
/// \return Number of visuals contained in this Link object.
94106
public: uint64_t VisualCount() const;

src/Collision.cc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,15 +262,17 @@ void Collision::CalculateInertial(
262262
gz::math::Inertiald &_inertial,
263263
const ParserConfig &_config)
264264
{
265-
this->CalculateInertial(_errors, _inertial, _config, std::nullopt);
265+
this->CalculateInertial(
266+
_errors, _inertial, _config, std::nullopt, ElementPtr());
266267
}
267268

268269
/////////////////////////////////////////////////
269270
void Collision::CalculateInertial(
270271
sdf::Errors &_errors,
271272
gz::math::Inertiald &_inertial,
272273
const ParserConfig &_config,
273-
const std::optional<double> &_density)
274+
const std::optional<double> &_density,
275+
sdf::ElementPtr _autoInertiaParams)
274276
{
275277
// Order of precedence for density:
276278
double density;
@@ -303,9 +305,16 @@ void Collision::CalculateInertial(
303305
);
304306
}
305307

308+
// If this Collision's auto inertia params have not been set, then use the
309+
// params passed into this function.
310+
sdf::ElementPtr autoInertiaParams = this->dataPtr->autoInertiaParams;
311+
if (!autoInertiaParams)
312+
{
313+
autoInertiaParams = _autoInertiaParams;
314+
}
306315
auto geomInertial =
307316
this->dataPtr->geom.CalculateInertial(_errors, _config,
308-
density, this->dataPtr->autoInertiaParams);
317+
density, autoInertiaParams);
309318

310319
if (!geomInertial)
311320
{

src/Link.cc

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ class sdf::Link::Implementation
7373
/// calculations if the collision density has not been set.
7474
public: std::optional<double> density;
7575

76+
/// \brief SDF element pointer to <auto_inertia_params> tag
77+
public: sdf::ElementPtr autoInertiaParams{nullptr};
78+
7679
/// \brief The inertial information for this link.
7780
public: gz::math::Inertiald inertial {{1.0,
7881
gz::math::Vector3d::One, gz::math::Vector3d::Zero},
@@ -189,6 +192,13 @@ Errors Link::Load(ElementPtr _sdf, const ParserConfig &_config)
189192
this->dataPtr->density = inertialElem->Get<double>("density");
190193
}
191194

195+
// Load the auto_inertia_params element
196+
if (inertialElem->HasElement("auto_inertia_params"))
197+
{
198+
this->dataPtr->autoInertiaParams =
199+
inertialElem->GetElement("auto_inertia_params");
200+
}
201+
192202
if (inertialElem->Get<bool>("auto"))
193203
{
194204
this->dataPtr->autoInertia = true;
@@ -330,6 +340,18 @@ void Link::SetDensity(double _density)
330340
this->dataPtr->density = _density;
331341
}
332342

343+
/////////////////////////////////////////////////
344+
sdf::ElementPtr Link::AutoInertiaParams() const
345+
{
346+
return this->dataPtr->autoInertiaParams;
347+
}
348+
349+
/////////////////////////////////////////////////
350+
void Link::SetAutoInertiaParams(const sdf::ElementPtr _autoInertiaParams)
351+
{
352+
this->dataPtr->autoInertiaParams = _autoInertiaParams;
353+
}
354+
333355
/////////////////////////////////////////////////
334356
uint64_t Link::VisualCount() const
335357
{
@@ -642,7 +664,8 @@ void Link::ResolveAutoInertials(sdf::Errors &_errors,
642664
{
643665
gz::math::Inertiald collisionInertia;
644666
collision.CalculateInertial(_errors, collisionInertia, _config,
645-
this->dataPtr->density);
667+
this->dataPtr->density,
668+
this->dataPtr->autoInertiaParams);
646669
totalInertia = totalInertia + collisionInertia;
647670
}
648671

0 commit comments

Comments
 (0)