Skip to content

Commit

Permalink
Merge pull request #705 from ami-iit/centroidal_dynamics_wrench
Browse files Browse the repository at this point in the history
Give the possibility to set an external wrench in the CentroidalDynamics
  • Loading branch information
GiulioRomualdi authored Jul 13, 2023
2 parents 1244b7c + 7e18c2b commit acf37de
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project are documented in this file.

### Changed
- Use `std::chorno::nanoseconds` in `clock` and `AdvanceableRunner` (https://github.com/ami-iit/bipedal-locomotion-framework/pull/702)
- Give the possibility to set an external wrench in the `CentroidalDynamics` instead of a pure force (https://github.com/ami-iit/bipedal-locomotion-framework/pull/705)

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

#include <BipedalLocomotion/Contacts/Contact.h>
#include <BipedalLocomotion/ContinuousDynamicalSystem/DynamicalSystem.h>
#include <BipedalLocomotion/Math/Constants.h>
#include <BipedalLocomotion/GenericContainer/NamedTuple.h>
#include <BipedalLocomotion/Math/Constants.h>

#include <Eigen/Dense>

Expand All @@ -32,17 +32,17 @@ template <> struct traits<CentroidalDynamics>
{
using Contacts = std::map<std::string, //
BipedalLocomotion::Contacts::DiscreteGeometryContact>;
using ExternalForce = std::optional<Eigen::Vector3d>;
using ExternalWrench = std::optional<BipedalLocomotion::Math::Wrenchd>;

using State = GenericContainer::named_tuple<BLF_NAMED_PARAM(com_pos, Eigen::Vector3d),
BLF_NAMED_PARAM(com_vel, Eigen::Vector3d),
BLF_NAMED_PARAM(angular_momentum, Eigen::Vector3d)>;
BLF_NAMED_PARAM(com_vel, Eigen::Vector3d),
BLF_NAMED_PARAM(angular_momentum, Eigen::Vector3d)>;
using StateDerivative
= GenericContainer::named_tuple<BLF_NAMED_PARAM(com_vel, Eigen::Vector3d),
BLF_NAMED_PARAM(com_acc, Eigen::Vector3d),
BLF_NAMED_PARAM(angular_momentum_rate, Eigen::Vector3d)>;
BLF_NAMED_PARAM(com_acc, Eigen::Vector3d),
BLF_NAMED_PARAM(angular_momentum_rate, Eigen::Vector3d)>;
using Input = GenericContainer::named_tuple<BLF_NAMED_PARAM(contacts, Contacts),
BLF_NAMED_PARAM(external_force, ExternalForce)>;
BLF_NAMED_PARAM(external_wrench, ExternalWrench)>;
using DynamicalSystem = CentroidalDynamics;
};
} // namespace BipedalLocomotion::ContinuousDynamicalSystem::internal
Expand All @@ -52,6 +52,7 @@ namespace BipedalLocomotion
namespace ContinuousDynamicalSystem
{

// clang-format off
/**
* CentroidalDynamics describes the centroidal dynamics of a multi-body system.
* The centroidal momentum \f${}_{\bar{G}} h ^\top= \begin{bmatrix} {}_{\bar{G}} h^{p\top} & {}_{\bar{G}} h^{\omega\top} \end{bmatrix} \in \mathbb{R}^6\f$
Expand Down Expand Up @@ -91,13 +92,13 @@ namespace ContinuousDynamicalSystem
* |`angular_momentum_rate` | `Eigen::Vector3d` | The centroidal angular momentum rate of change written respect the inertial frame. |
*
* The `Input` is described by a BipedalLocomotion::GenericContainer::named_tuple
* | Name | Type | Description |
* |:----------------:|:---------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------------------------------------:|
* | `contacts` | `std::unordered_map<std::string, BipedalLocomotion::Contacts::DiscreteGeometryContact>` | List of contact where each force applied in the discere geometry contact is expressed with respect to the inertial frame |
* | `external_force` | `std::optional<Eigen::Vector3d>` | Optional force applied to the robot center of mass. The coordinates are expressed in the inertial frame |
* | Name | Type | Description |
* |:-----------------:|:---------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------------------------------------:|
* | `contacts` | `std::unordered_map<std::string, BipedalLocomotion::Contacts::DiscreteGeometryContact>` | List of contact where each force applied in the discrete geometry contact is expressed with respect to the inertial frame |
* | `external_wrench` | `std::optional<BipedalLocomotion::Math::Wrenchd>` | Optional wrench applied to the robot center of mass. The coordinates are expressed in the inertial frame |
*/
class CentroidalDynamics : public DynamicalSystem<CentroidalDynamics>

// clang-format on
{
/** Gravity vector expressed in the inertial frame*/
Eigen::Vector3d m_gravity{0, 0, -Math::StandardAccelerationOfGravitation};
Expand All @@ -107,7 +108,7 @@ class CentroidalDynamics : public DynamicalSystem<CentroidalDynamics>
Input m_controlInput; /**< Input of the dynamical system */

public:

// clang-format off
/**
* Initialize the CentroidalDynamics system.
* @param handler pointer to the parameter handler.
Expand All @@ -119,6 +120,7 @@ class CentroidalDynamics : public DynamicalSystem<CentroidalDynamics>
* @return true in case of success/false otherwise.
*/
bool initialize(std::weak_ptr<const ParametersHandler::IParametersHandler> handler);
// clang-format on

/**
* Computes the centroidal momentum dynamics. It return \f$f(x, u, t)\f$.
Expand Down
12 changes: 8 additions & 4 deletions src/ContinuousDynamicalSystem/src/CentroidalDynamics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ bool CentroidalDynamics::initialize(std::weak_ptr<const IParametersHandler> hand
return true;
}

bool CentroidalDynamics::dynamics(const std::chrono::nanoseconds& time, StateDerivative& stateDerivative)
bool CentroidalDynamics::dynamics(const std::chrono::nanoseconds& time,
StateDerivative& stateDerivative)
{
using namespace BipedalLocomotion::GenericContainer::literals;

Expand All @@ -52,13 +53,16 @@ bool CentroidalDynamics::dynamics(const std::chrono::nanoseconds& time, StateDer

comVelocityOut = comVelocity;
comAcceleration = m_gravity;
angularMomentumRate.setZero();

const auto& contacts = m_controlInput.get_from_hash<"contacts"_h>();
const auto& externalDisturbance = m_controlInput.get_from_hash<"external_force"_h>();
const auto& externalDisturbance = m_controlInput.get_from_hash<"external_wrench"_h>();
if (externalDisturbance)
{
comAcceleration += externalDisturbance.value();
comAcceleration += externalDisturbance.value().force();
angularMomentumRate = externalDisturbance.value().torque();
} else
{
angularMomentumRate.setZero();
}

for (const auto& [key, contact] : contacts)
Expand Down
3 changes: 2 additions & 1 deletion src/ReducedModelControllers/tests/CentroidalMPCTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ TEST_CASE("CentroidalMPC")
centroidalMPCData);
}

system->setControlInput({mpc.getOutput().contacts, Eigen::Vector3d::Zero()});
system->setControlInput({mpc.getOutput().contacts, //
BipedalLocomotion::Math::Wrenchd::Zero()});
REQUIRE(integrator.integrate(0s, integratorStepTime));

controllerIndex++;
Expand Down

0 comments on commit acf37de

Please sign in to comment.