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 3, 2023
1 parent 54901d3 commit 7a857b1
Show file tree
Hide file tree
Showing 9 changed files with 248 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/MANNTrajectoryGeneration.cpp
HEADERS ${H_PREFIX}/Module.h ${H_PREFIX}/MANN.h ${H_PREFIX}/MANNTrajectoryGeneration.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 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_TRAJECTORY_GENERATION_H
#define BIPEDAL_LOCOMOTION_BINDINGS_ML_MANN_TRAJECTORY_GENERATION_H

#include <pybind11/pybind11.h>

namespace BipedalLocomotion
{
namespace bindings
{
namespace ML
{

void CreateMANNTrajectoryGeneration(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
58 changes: 58 additions & 0 deletions bindings/python/ML/src/MANNTrajectoryGeneration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @file MANNTrajetoryGenerator.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/MANNTrajectoryGeneration.h>

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

namespace BipedalLocomotion
{
namespace bindings
{
namespace ML
{

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

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

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

BipedalLocomotion::bindings::System::CreateAdvanceable<ML::MANNTrajectoryGenerationInput, //
ML::MANNTrajectoryGenerationOutput> //
(module, "MANNTrajectoryGeneration");
py::class_<ML::MANNTrajectoryGeneration,
System::Advanceable<ML::MANNTrajectoryGenerationInput, //
ML::MANNTrajectoryGenerationOutput>>(module,
"MANNTrajectoryGeneration")
.def(py::init())
.def("reset", &ML::MANNTrajectoryGeneration::reset);
}

} // 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/MANNTrajectoryGeneration.h>

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

CreateMANN(module);
CreateMANNTrajectoryGeneration(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 7a857b1

Please sign in to comment.