Skip to content

Commit 96da620

Browse files
Add mass matrix regularziation in TSID joint dynamic task (#722)
1 parent ffeaf6a commit 96da620

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ All notable changes to this project are documented in this file.
1717
- Implement inequality operator for the `PlannedContact` class (https://github.com/ami-iit/bipedal-locomotion-framework/pull/750)
1818
- Finalize `RobotDynamicsEstimator` library and add complete library test (https://github.com/ami-iit/bipedal-locomotion-framework/pull/744)
1919
- Add Python bindings for `RobotDynamicsEstimator` library (https://github.com/ami-iit/bipedal-locomotion-framework/pull/755)
20+
- Add possibility to set the regularization on the mass matrix for the `TSID::JointDynamicsTask` (https://github.com/ami-iit/bipedal-locomotion-framework/pull/722)
2021

2122
### Changed
2223
- Remove the possibility to disable the telemetry in `System::AdvanceableRunner` (https://github.com/ami-iit/bipedal-locomotion-framework/pull/726)

src/TSID/include/BipedalLocomotion/TSID/JointDynamicsTask.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ class JointDynamicsTask : public TSIDLinearTask
4646
System::VariablesHandler::VariableDescription m_jointsTorqueVariable; /**< Variable
4747
describing the joint
4848
torques */
49-
49+
bool m_useMassMatrixRegularizationTerm{false}; /**< True if the mass matrix regularization is used*/
50+
Eigen::MatrixXd m_massMatrixRegularizationTerm; /**< Variable storing mass matrix regularization term*/
5051
struct ContactWrench
5152
{
5253
iDynTree::FrameIndex frameIndex; /**< Frame used to express the contact wrench */
@@ -108,6 +109,16 @@ class JointDynamicsTask : public TSIDLinearTask
108109
*/
109110
bool setVariablesHandler(const System::VariablesHandler& variablesHandler) override;
110111

112+
/**
113+
* Set the mass matrix regularization term. i.e. \f$\bar{M} = M + M _ {reg}\f$. Where \f$M\f$
114+
* is the mass matrix and \f$M_{reg}\f$ is the matrix regularization term.
115+
* @param matrix the regularization term for the mass matrix.
116+
* @notice Calling this function is not mandatory. Call it only if you want to add a
117+
* regularization term.
118+
* @return true in case of success, false otherwise.
119+
*/
120+
bool setMassMatrixRegularization(const Eigen::Ref<const Eigen::MatrixXd>& matrix);
121+
111122
/**
112123
* Update the content of the element.
113124
* @return True in case of success, false otherwise.

src/TSID/src/JointDynamicsTask.cpp

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ bool JointDynamicsTask::setVariablesHandler(const System::VariablesHandler& vari
3737
return false;
3838
}
3939

40-
if (!variablesHandler.getVariable(m_robotAccelerationVariable.name, m_robotAccelerationVariable))
40+
if (!variablesHandler.getVariable(m_robotAccelerationVariable.name,
41+
m_robotAccelerationVariable))
4142
{
4243
log()->error("{} Error while retrieving the robot acceleration variable.", errorPrefix);
4344
return false;
@@ -70,7 +71,7 @@ bool JointDynamicsTask::setVariablesHandler(const System::VariablesHandler& vari
7071
return false;
7172
}
7273

73-
for(auto& contact : m_contactWrenches)
74+
for (auto& contact : m_contactWrenches)
7475
{
7576
if (!variablesHandler.getVariable(contact.variable.name, contact.variable))
7677
{
@@ -101,14 +102,45 @@ bool JointDynamicsTask::setVariablesHandler(const System::VariablesHandler& vari
101102
return true;
102103
}
103104

104-
bool JointDynamicsTask::initialize(std::weak_ptr<const ParametersHandler::IParametersHandler> paramHandler)
105+
bool JointDynamicsTask::setMassMatrixRegularization(const Eigen::Ref<const Eigen::MatrixXd>& matrix)
106+
{
107+
constexpr auto logPrefix = "[JointDynamicsTask::"
108+
"setMassMatrixRegularization]";
109+
110+
if ((m_kinDyn == nullptr) || (!m_kinDyn->isValid()))
111+
{
112+
log()->error("{} Please call setKinDyn() before.", logPrefix);
113+
return false;
114+
}
115+
116+
if ((m_kinDyn->getNrOfDegreesOfFreedom() != matrix.rows()) || (matrix.cols() != matrix.rows()))
117+
{
118+
const auto rightSize = m_kinDyn->getNrOfDegreesOfFreedom();
119+
120+
log()->error("{} The size of the regularization matrix is not correct. The correct size "
121+
"is: {} x {}. While the input of the function is a {} x {} matrix.",
122+
logPrefix,
123+
rightSize,
124+
rightSize,
125+
matrix.rows(),
126+
matrix.cols());
127+
return false;
128+
}
129+
130+
m_massMatrixRegularizationTerm = matrix;
131+
m_useMassMatrixRegularizationTerm = true;
132+
133+
return true;
134+
}
135+
136+
bool JointDynamicsTask::initialize(
137+
std::weak_ptr<const ParametersHandler::IParametersHandler> paramHandler)
105138
{
106139
constexpr auto errorPrefix = "[JointDynamicsTask::initialize]";
107140

108141
if (m_kinDyn == nullptr || !m_kinDyn->isValid())
109142
{
110-
log()->error("{} KinDynComputations object is not valid.",
111-
errorPrefix);
143+
log()->error("{} KinDynComputations object is not valid.", errorPrefix);
112144
return false;
113145
}
114146

@@ -211,8 +243,17 @@ bool JointDynamicsTask::update()
211243
}
212244

213245
m_b = m_generalizedBiasForces.tail(m_kinDyn->getNrOfDegreesOfFreedom());
214-
iDynTree::toEigen(this->subA(m_robotAccelerationVariable))
215-
= -m_massMatrix.bottomRows(m_kinDyn->getNrOfDegreesOfFreedom());
246+
247+
if (m_useMassMatrixRegularizationTerm)
248+
{
249+
iDynTree::toEigen(this->subA(m_robotAccelerationVariable))
250+
= -(m_massMatrix.bottomRows(m_kinDyn->getNrOfDegreesOfFreedom())
251+
+ m_massMatrixRegularizationTerm);
252+
} else
253+
{
254+
iDynTree::toEigen(this->subA(m_robotAccelerationVariable))
255+
= -m_massMatrix.bottomRows(m_kinDyn->getNrOfDegreesOfFreedom());
256+
}
216257

217258
for (const auto& contactWrench : m_contactWrenches)
218259
{

0 commit comments

Comments
 (0)