Skip to content

Commit

Permalink
Implement python bindings for ML component
Browse files Browse the repository at this point in the history
  • Loading branch information
GiulioRomualdi committed May 9, 2023
1 parent 27989df commit 94cb36d
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 0 deletions.
1 change: 1 addition & 0 deletions bindings/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_subdirectory(TextLogging)
add_subdirectory(Conversions)
add_subdirectory(YarpUtilities)
add_subdirectory(ContinuousDynamicalSystem)
add_subdirectory(ML)

include(ConfigureFileWithCMakeIf)

Expand Down
16 changes: 16 additions & 0 deletions bindings/python/ML/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (C) 2023 Istituto Italiano di Tecnologia (IIT). All rights reserved.
# This software may be modified and distributed under the terms of the
# BSD-3-Clause license.

if(FRAMEWORK_COMPILE_ML)

set(H_PREFIX include/BipedalLocomotion/bindings/ML)

add_bipedal_locomotion_python_module(
NAME MLBindings
SOURCES src/Module.cpp src/MANN.cpp src/MANNAutoregressive.cpp
HEADERS ${H_PREFIX}/Module.h ${H_PREFIX}/MANN.h ${H_PREFIX}/MANNAutoregressive.h
LINK_LIBRARIES BipedalLocomotion::ML
)

endif()
26 changes: 26 additions & 0 deletions bindings/python/ML/include/BipedalLocomotion/bindings/ML/MANN.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @file MANN.h
* @authors Giulio Romualdi
* @copyright 2023 Istituto Italiano di Tecnologia (IIT). This software may be modified and
* distributed under the terms of the BSD-3-Clause license.
*/

#ifndef BIPEDAL_LOCOMOTION_BINDINGS_ML_MANN_H
#define BIPEDAL_LOCOMOTION_BINDINGS_ML_MANN_H

#include <pybind11/pybind11.h>

namespace BipedalLocomotion
{
namespace bindings
{
namespace ML
{

void CreateMANN(pybind11::module& module);

} // namespace ML
} // namespace bindings
} // namespace BipedalLocomotion

#endif // BIPEDAL_LOCOMOTION_BINDINGS_ML_MANN_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @file MANNAutoregressive.h
* @authors Giulio Romualdi
* @copyright 2023 Istituto Italiano di Tecnologia (IIT). This software may be modified and
* distributed under the terms of the BSD-3-Clause license.
*/

#ifndef BIPEDAL_LOCOMOTION_BINDINGS_ML_MANN_TRAJECTORY_GENERATION_H
#define BIPEDAL_LOCOMOTION_BINDINGS_ML_MANN_TRAJECTORY_GENERATION_H

#include <pybind11/pybind11.h>

namespace BipedalLocomotion
{
namespace bindings
{
namespace ML
{

void CreateMANNAutoregressive(pybind11::module& module);

} // namespace ML
} // namespace bindings
} // namespace BipedalLocomotion

#endif // BIPEDAL_LOCOMOTION_BINDINGS_ML_MANN_TRAJECTORY_GENERATION_H
26 changes: 26 additions & 0 deletions bindings/python/ML/include/BipedalLocomotion/bindings/ML/Module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @file Module.h
* @authors Giulio Romualdi
* @copyright 2023 Istituto Italiano di Tecnologia (IIT). This software may be modified and
* distributed under the terms of the BSD-3-Clause license.
*/

#ifndef BIPEDAL_LOCOMOTION_BINDINGS_ML_MODULE_H
#define BIPEDAL_LOCOMOTION_BINDINGS_ML_MODULE_H

#include <pybind11/pybind11.h>

namespace BipedalLocomotion
{
namespace bindings
{
namespace ML
{

void CreateModule(pybind11::module& module);

} // namespace ML
} // namespace bindings
} // namespace BipedalLocomotion

#endif // BIPEDAL_LOCOMOTION_BINDINGS_ML_MODULE_H
58 changes: 58 additions & 0 deletions bindings/python/ML/src/MANN.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @file MANN.cpp
* @authors Giulio Romualdi
* @copyright 2023 Istituto Italiano di Tecnologia (IIT). This software may be modified and
* distributed under the terms of the BSD-3-Clause license.
*/

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

#include <BipedalLocomotion/ML/MANN.h>

#include <BipedalLocomotion/bindings/ML/MANN.h>
#include <BipedalLocomotion/bindings/System/Advanceable.h>

namespace BipedalLocomotion
{
namespace bindings
{
namespace ML
{

void CreateMANN(pybind11::module& module)
{
namespace py = ::pybind11;
namespace ML = BipedalLocomotion::ML;
namespace System = BipedalLocomotion::System;

py::class_<ML::MANNInput>(module, "MANNInput")
.def(py::init())
.def_readwrite("base_position_trajectory", &ML::MANNInput::basePositionTrajectory)
.def_readwrite("facing_direction_trajectory", &ML::MANNInput::facingDirectionTrajectory)
.def_readwrite("base_velocity_trajectory", &ML::MANNInput::baseVelocitiesTrajectory)
.def_readwrite("joint_positions", &ML::MANNInput::jointPositions)
.def_readwrite("joint_velocities", &ML::MANNInput::jointVelocities);

py::class_<ML::MANNOutput>(module, "MANNOutput")
.def(py::init())
.def_readwrite("future_base_position_trajectory",
&ML::MANNOutput::futureBasePositionTrajectory)
.def_readwrite("future_facing_direction_trajectory",
&ML::MANNOutput::futureFacingDirectionTrajectory)
.def_readwrite("future_base_velocities_trajectory",
&ML::MANNOutput::futureBaseVelocitiesTrajectory)
.def_readwrite("joint_positions", &ML::MANNOutput::jointPositions)
.def_readwrite("joint_velocities", &ML::MANNOutput::jointVelocities)
.def_readwrite("projected_base_velocity", &ML::MANNOutput::projectedBaseVelocity);

BipedalLocomotion::bindings::System::CreateAdvanceable<ML::MANNInput, //
ML::MANNOutput>(module, "MANN");
py::class_<ML::MANN, System::Advanceable<ML::MANNInput, ML::MANNOutput>>(module, "MANN")
.def(py::init());
}

} // namespace ML
} // namespace bindings
} // namespace BipedalLocomotion
73 changes: 73 additions & 0 deletions bindings/python/ML/src/MANNAutoregressive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* @file MannAutoregressive.cpp
* @authors Giulio Romualdi
* @copyright 2023 Istituto Italiano di Tecnologia (IIT). This software may be modified and
* distributed under the terms of the BSD-3-Clause license.
*/

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

#include <BipedalLocomotion/ML/MANNAutoregressive.h>
#include <BipedalLocomotion/bindings/ML/MANNAutoregressive.h>
#include <BipedalLocomotion/bindings/System/Advanceable.h>
#include <BipedalLocomotion/bindings/type_caster/swig.h>

namespace BipedalLocomotion
{
namespace bindings
{
namespace ML
{

void CreateMANNAutoregressive(pybind11::module& module)
{
namespace py = ::pybind11;
namespace ML = BipedalLocomotion::ML;
namespace System = BipedalLocomotion::System;

py::class_<ML::MANNAutoregressiveInput>(module, "MANNAutoregressiveInput")
.def(py::init())
.def_readwrite("desired_future_base_trajectory",
&ML::MANNAutoregressiveInput::desiredFutureBaseTrajectory)
.def_readwrite("desired_future_base_velocities",
&ML::MANNAutoregressiveInput::desiredFutureBaseVelocities)
.def_readwrite("desired_future_facing_directions",
&ML::MANNAutoregressiveInput::desiredFutureFacingDirections);

py::class_<ML::MANNAutoregressiveOutput>(module, "MANNAutoregressiveOutput")
.def(py::init())
.def_readwrite("joint_positions", &ML::MANNAutoregressiveOutput::jointsPosition)
.def_readwrite("base_pose", &ML::MANNAutoregressiveOutput::basePose)
.def_readwrite("left_foot", &ML::MANNAutoregressiveOutput::leftFoot)
.def_readwrite("right_foot", &ML::MANNAutoregressiveOutput::rightFoot);

BipedalLocomotion::bindings::System::CreateAdvanceable<ML::MANNAutoregressiveInput, //
ML::MANNAutoregressiveOutput> //
(module, "MANNAutoregressive");
py::class_<ML::MANNAutoregressive,
System::Advanceable<ML::MANNAutoregressiveInput, //
ML::MANNAutoregressiveOutput>>(module,
"MANNAutoregressive")
.def(py::init())
.def("reset", py::overload_cast<const ML::MANNInput&, const Contacts::EstimatedContact&,
const Contacts::EstimatedContact&,
const manif::SE3d&,
const manif::SE3Tangentd&>(&ML::MANNAutoregressive::reset))
.def("set_robot_model", [](ML::MANNAutoregressive& impl, ::pybind11::object& obj) {
iDynTree::Model* cls = py::detail::swig_wrapped_pointer_to_pybind<iDynTree::Model>(obj);

if (cls == nullptr)
{
throw ::pybind11::value_error("Invalid input for the function. Please provide "
"an iDynTree::Model object.");
}
return impl.setRobotModel(*cls);
});
}

} // namespace ML
} // namespace bindings
} // namespace BipedalLocomotion
28 changes: 28 additions & 0 deletions bindings/python/ML/src/Module.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @file Module.cpp
* @authors Giulio Romualdi
* @copyright 2023 Istituto Italiano di Tecnologia (IIT). This software may be modified and
* distributed under the terms of the BSD-3-Clause license.
*/

#include <pybind11/pybind11.h>

#include <BipedalLocomotion/bindings/ML/MANN.h>
#include <BipedalLocomotion/bindings/ML/MANNAutoregressive.h>

namespace BipedalLocomotion
{
namespace bindings
{
namespace ML
{
void CreateModule(pybind11::module& module)
{
module.doc() = "ML module.";

CreateMANN(module);
CreateMANNAutoregressive(module);
}
} // namespace ML
} // namespace bindings
} // namespace BipedalLocomotion
9 changes: 9 additions & 0 deletions bindings/python/bipedal_locomotion_framework.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
#include <BipedalLocomotion/bindings/ContinuousDynamicalSystem/Module.h>
@endcmakeif FRAMEWORK_COMPILE_ContinuousDynamicalSystem

@cmakeif FRAMEWORK_COMPILE_ML
#include <BipedalLocomotion/bindings/ML/Module.h>
@endcmakeif FRAMEWORK_COMPILE_ML

// Create the Python module
PYBIND11_MODULE(bindings, m)
{
Expand Down Expand Up @@ -182,4 +186,9 @@ PYBIND11_MODULE(bindings, m)
py::module continuousDynamicalSystemModule = m.def_submodule("continuous_dynamical_system");
bindings::ContinuousDynamicalSystem::CreateModule(continuousDynamicalSystemModule);
@endcmakeif FRAMEWORK_COMPILE_ContinuousDynamicalSystem

@cmakeif FRAMEWORK_COMPILE_ML
py::module mlModule = m.def_submodule("ml");
bindings::ML::CreateModule(mlModule);
@endcmakeif FRAMEWORK_COMPILE_ML
}

0 comments on commit 94cb36d

Please sign in to comment.