Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement AngularMomentum task in the IK component #443

Merged
merged 3 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project are documented in this file.
- Implement the `AngularMomentumTask` in the `TSID` component and the associated python bindings (https://github.com/ami-iit/bipedal-locomotion-framework/pull/436)
- Implement `QPTSID::toString` method and the associated python bindings (https://github.com/ami-iit/bipedal-locomotion-framework/pull/440)
- Implement `ContactWrench` python bindings (https://github.com/ami-iit/bipedal-locomotion-framework/pull/441)
- Implement AngularMomentum task in the IK component and the associated bindings (https://github.com/ami-iit/bipedal-locomotion-framework/pull/443)

### Changed
- Inherits all the `Eigen::Matrix` constructors in the `Wrenchd` class (https://github.com/ami-iit/bipedal-locomotion-framework/pull/441)
Expand Down
4 changes: 2 additions & 2 deletions bindings/python/IK/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ if(FRAMEWORK_COMPILE_IK)

add_bipedal_locomotion_python_module(
NAME IKBindings
SOURCES src/IntegrationBasedIK.cpp src/QPInverseKinematics.cpp src/CoMTask.cpp src/SE3Task.cpp src/SO3Task.cpp src/JointTrackingTask.cpp src/Module.cpp src/IKLinearTask.cpp
HEADERS ${H_PREFIX}/IntegrationBasedIK.h ${H_PREFIX}/QPInverseKinematics.h ${H_PREFIX}/CoMTask.h ${H_PREFIX}/SE3Task.h ${H_PREFIX}/SO3Task.h ${H_PREFIX}/JointTrackingTask.h ${H_PREFIX}/Module.h ${H_PREFIX}/IKLinearTask.h
SOURCES src/IntegrationBasedIK.cpp src/QPInverseKinematics.cpp src/CoMTask.cpp src/SE3Task.cpp src/SO3Task.cpp src/JointTrackingTask.cpp src/AngularMomentumTask.cpp src/Module.cpp src/IKLinearTask.cpp
HEADERS ${H_PREFIX}/IntegrationBasedIK.h ${H_PREFIX}/QPInverseKinematics.h ${H_PREFIX}/CoMTask.h ${H_PREFIX}/SE3Task.h ${H_PREFIX}/SO3Task.h ${H_PREFIX}/JointTrackingTask.h ${H_PREFIX}/AngularMomentumTask.h ${H_PREFIX}/Module.h ${H_PREFIX}/IKLinearTask.h
LINK_LIBRARIES BipedalLocomotion::IK
TESTS tests/test_QP_inverse_kinematics.py
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @file AngularMomentumTask.h
* @authors Giulio Romualdi
* @copyright 2021 Istituto Italiano di Tecnologia (IIT). This software may be modified and
* distributed under the terms of the GNU Lesser General Public License v2.1 or any later version.
*/

#ifndef BIPEDAL_LOCOMOTION_BINDINGS_IK_ANGULAR_MOMENTUM_TASK_H
#define BIPEDAL_LOCOMOTION_BINDINGS_IK_ANGULAR_MOMENTUM_TASK_H

#include <pybind11/pybind11.h>

namespace BipedalLocomotion
{
namespace bindings
{
namespace IK
{

void CreateAngularMomentumTask(pybind11::module& module);

} // namespace IK
} // namespace bindings
} // namespace BipedalLocomotion

#endif // BIPEDAL_LOCOMOTION_BINDINGS_IK_ANGULAR_MOMENTUM_TASK_H
38 changes: 38 additions & 0 deletions bindings/python/IK/src/AngularMomentumTask.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @file AngularMomentumTask.cpp
* @authors Giulio Romualdi
* @copyright 2021 Istituto Italiano di Tecnologia (IIT). This software may be modified and
* distributed under the terms of the GNU Lesser General Public License v2.1 or any later version.
*/

#include <pybind11/eigen.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include <BipedalLocomotion/IK/IKLinearTask.h>
#include <BipedalLocomotion/IK/AngularMomentumTask.h>
#include <BipedalLocomotion/bindings/IK/AngularMomentumTask.h>

namespace BipedalLocomotion
{
namespace bindings
{
namespace IK
{

void CreateAngularMomentumTask(pybind11::module& module)
{
namespace py = ::pybind11;
using namespace BipedalLocomotion::IK;

py::class_<AngularMomentumTask, //
std::shared_ptr<AngularMomentumTask>,
IKLinearTask>(module, "AngularMomentumTask")
.def(py::init())
.def("set_kin_dyn", &AngularMomentumTask::setKinDyn, py::arg("kin_dyn"))
.def("set_set_point", &AngularMomentumTask::setSetPoint, py::arg("angular_momentum"));
}

} // namespace IK
} // namespace bindings
} // namespace BipedalLocomotion
2 changes: 2 additions & 0 deletions bindings/python/IK/src/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <pybind11/pybind11.h>

#include <BipedalLocomotion/bindings/IK/AngularMomentumTask.h>
#include <BipedalLocomotion/bindings/IK/CoMTask.h>
#include <BipedalLocomotion/bindings/IK/IKLinearTask.h>
#include <BipedalLocomotion/bindings/IK/IntegrationBasedIK.h>
Expand All @@ -31,6 +32,7 @@ void CreateModule(pybind11::module& module)
CreateSE3Task(module);
CreateSO3Task(module);
CreateJointTrackingTask(module);
CreateAngularMomentumTask(module);
CreateIntegrationBasedIK(module);
CreateQPInverseKinematics(module);
}
Expand Down
34 changes: 34 additions & 0 deletions bindings/python/IK/tests/test_QP_inverse_kinematics.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,40 @@ def test_joint_tracking_task():
joint_velocities = [np.random.uniform(-0.5,0.5) for _ in range(kindyn_desc.kindyn.get_nr_of_dofs())]
assert joint_tracking_task.set_set_point(joint_position=joint_values,joint_velocity=joint_velocities)

def test_angular_momentum_task():

# retrieve the model
model_url = 'https://raw.githubusercontent.com/robotology/icub-models/master/iCub/robots/iCubGazeboV2_5/model.urdf'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you may want to get a fixed release to avoid that a change in icub-models breaks this test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that as soon as we can start using pybind11 iDynTree bindings we can switch to iDynTree.get_random_chain(10)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, we need to remind to set a fixed seed.

model = urllib.request.urlopen(model_url)
temp = tempfile.NamedTemporaryFile()
temp.write(model.read())

# create KinDynComputationsDescriptor
kindyn_handler = blf.parameters_handler.StdParametersHandler()
kindyn_handler.set_parameter_string("model_file_name", temp.name)
joints_list = ["neck_pitch", "neck_roll", "neck_yaw",
"torso_pitch", "torso_roll", "torso_yaw",
"l_shoulder_pitch", "l_shoulder_roll", "l_shoulder_yaw","l_elbow",
"r_shoulder_pitch", "r_shoulder_roll", "r_shoulder_yaw","r_elbow",
"l_hip_pitch", "l_hip_roll", "l_hip_yaw","l_knee", "l_ankle_pitch", "l_ankle_roll",
"r_hip_pitch", "r_hip_roll", "r_hip_yaw","r_knee", "r_ankle_pitch", "r_ankle_roll"]
kindyn_handler.set_parameter_vector_string("joints_list", joints_list)
kindyn_desc = blf.floating_base_estimators.construct_kindyncomputations_descriptor(kindyn_handler)
assert kindyn_desc.is_valid()

# Set the parameters
angular_momentum_param_handler = blf.parameters_handler.StdParametersHandler()
angular_momentum_param_handler.set_parameter_string(name="robot_velocity_variable_name", value="robotVelocity")

# Initialize the task
angular_momentum_task = blf.ik.AngularMomentumTask()
assert angular_momentum_task.set_kin_dyn(kindyn_desc.kindyn)
assert angular_momentum_task.initialize(param_handler=angular_momentum_param_handler)
angular_momentum_var_handler = blf.system.VariablesHandler()
assert angular_momentum_var_handler.add_variable("robotVelocity", 32) is True # robot velocity size = 26 (joints) + 6 (base)
assert angular_momentum_task.set_variables_handler(variables_handler=angular_momentum_var_handler)
assert angular_momentum_task.set_set_point([0., 0., 0.])

def test_integration_based_ik_state():

state = blf.ik.IntegrationBasedIKState()
Expand Down
4 changes: 2 additions & 2 deletions src/IK/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ if(FRAMEWORK_COMPILE_IK)

add_bipedal_locomotion_library(
NAME IK
PUBLIC_HEADERS ${H_PREFIX}/SE3Task.h ${H_PREFIX}/SO3Task.h ${H_PREFIX}/JointTrackingTask.h ${H_PREFIX}/CoMTask.h ${H_PREFIX}/IntegrationBasedIK.h ${H_PREFIX}/QPInverseKinematics.h ${H_PREFIX}/IKLinearTask.h
SOURCES src/SE3Task.cpp src/SO3Task.cpp src/JointTrackingTask.cpp src/CoMTask.cpp src/QPInverseKinematics.cpp
PUBLIC_HEADERS ${H_PREFIX}/SE3Task.h ${H_PREFIX}/SO3Task.h ${H_PREFIX}/JointTrackingTask.h ${H_PREFIX}/CoMTask.h ${H_PREFIX}/IntegrationBasedIK.h ${H_PREFIX}/QPInverseKinematics.h ${H_PREFIX}/IKLinearTask.h ${H_PREFIX}/AngularMomentumTask.h
SOURCES src/SE3Task.cpp src/SO3Task.cpp src/JointTrackingTask.cpp src/CoMTask.cpp src/QPInverseKinematics.cpp src/AngularMomentumTask.cpp
PUBLIC_LINK_LIBRARIES Eigen3::Eigen
BipedalLocomotion::ParametersHandler BipedalLocomotion::System
LieGroupControllers::LieGroupControllers
Expand Down
128 changes: 128 additions & 0 deletions src/IK/include/BipedalLocomotion/IK/AngularMomentumTask.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* @file AngularMomentumTask.h
* @authors Giulio Romualdi
* @copyright 2021 Istituto Italiano di Tecnologia (IIT). This software may be modified and
* distributed under the terms of the GNU Lesser General Public License v2.1 or any later version.
*/

#ifndef BIPEDAL_LOCOMOTION_IK_ANGULAR_MOMENTUM_TASK_H
#define BIPEDAL_LOCOMOTION_IK_ANGULAR_MOMENTUM_TASK_H

#include <memory>

#include <BipedalLocomotion/IK/IKLinearTask.h>

#include <iDynTree/KinDynComputations.h>

namespace BipedalLocomotion
{
namespace IK
{

/**
* AngularMomentumTask is a concrete implementation of the Task. Please use this element if you
* want to control robot centroidal angular momentum.
* The task assumes perfect control of the robot velocity \f$\nu\f$ that contains the base
* linear and angular velocity expressed in mixed representation and the joint velocities.
* The task represents the following equation
* \f[
* A_{cmm_\omega} \nu = {}^{G[A]} h_\omega ^*
* \f]
* where \f$A_{cmm_\omega}\f$ is the angular part of the robot centroidal momentum matrix
* \f${}^G[A} h_\omega ^*$ is the desired centroidal angular momentum.
* @note AngularMomentumTask can be used to control also a subset of element of the AngularMomentum.
* Please refer to `mask` parameter in IK::AngularMomentumTask::initialize method.
*/
class AngularMomentumTask : public IKLinearTask
{
System::VariablesHandler::VariableDescription m_robotVelocityVariable; /**< Variable
describing the
robot velocity
(base + joint) */

static constexpr std::size_t m_angularVelocitySize{3}; /**< Size of the angular velocity vector.
*/
static constexpr std::size_t m_spatialVelocitySize{6}; /**< Size of the spatial velocity vector.
*/

bool m_isInitialized{false}; /**< True if the task has been initialized. */
bool m_isValid{false}; /**< True if the task is valid. */

std::shared_ptr<iDynTree::KinDynComputations> m_kinDyn; /**< Pointer to a KinDynComputations
object */

Eigen::MatrixXd m_centroidalMomentumMatrix;
std::size_t m_angularMomentumTaskSize{m_angularVelocitySize}; /**< DoFs associated to the linear task */
std::array<bool, m_angularVelocitySize> m_mask{true, true, true};
public:

/**
* Initialize the task.
* @param paramHandler pointer to the parameters handler.
* @note the following parameters are required by the class
* | Parameter Name | Type | Description | Mandatory |
* |:----------------------------------:|:--------:|:--------------------------------------------------------------------------------------:|:---------:|
* | `robot_velocity_variable_name` | `string` | Name of the variable contained in `VariablesHandler` describing the robot velocity | Yes |
* | `mask` | `vector<bool>` | Mask representing the angular momentum coordinates controlled. E.g. [1,0,1] will enable the control on the x and z coordinates only and the angular part. (Default value, [1,1,1]) | No |
* Where the generalized robot velocity is a vector containing the base spatial-velocity
* (expressed in mixed representation) and the joint velocities.
* @return True in case of success, false otherwise.
*/
bool initialize(std::weak_ptr<const ParametersHandler::IParametersHandler> paramHandler) override;

/**
* Set the kinDynComputations object.
* @param kinDyn pointer to a kinDynComputations object.
* @return True in case of success, false otherwise.
*/
bool setKinDyn(std::shared_ptr<iDynTree::KinDynComputations> kinDyn);

/**
* Set the set of variables required by the task. The variables are stored in the
* System::VariablesHandler.
* @param variablesHandler reference to a variables handler.
* @note The handler must contain a variable named as the parameter
* `robot_velocity_variable_name` stored in the parameter handler. The variable represents the
* generalized velocity of the robot. Where the generalized robot velocity is a vector
* containing the base spatial-velocity (expressed in mixed representation) and the joints
* velocity.
* @return True in case of success, false otherwise.
*/
bool setVariablesHandler(const System::VariablesHandler& variablesHandler) override;

/**
* Update the content of the element.
* @return True in case of success, false otherwise.
*/
bool update() override;

/**
* Set the desired reference trajectory.
* @param desired angular momentum
* @return True in case of success, false otherwise.
*/
bool setSetPoint(Eigen::Ref<const Eigen::Vector3d> desiredAngularMomentumComponents);

/**
* Get the size of the task. (I.e the number of rows of the vector b)
* @return the size of the task.
*/
std::size_t size() const override;

/**
* The AngularMomentumTask is an equality task.
* @return the size of the task.
*/
Type type() const override;

/**
* Determines the validity of the objects retrieved with getA() and getB()
* @return True if the objects are valid, false otherwise.
*/
bool isValid() const override;
};

} // namespace IK
} // namespace BipedalLocomotion

#endif // BIPEDAL_LOCOMOTION_IK_ANGULAR_MOMENTUM_TASK_H
Loading