From 7a857b131c020dd09164595064980542cc49011a Mon Sep 17 00:00:00 2001 From: Giulio Romualdi Date: Wed, 3 May 2023 18:27:13 +0200 Subject: [PATCH] Implement python bindings for ML component --- bindings/python/CMakeLists.txt | 1 + bindings/python/ML/CMakeLists.txt | 16 +++++ .../BipedalLocomotion/bindings/ML/MANN.h | 26 +++++++++ .../bindings/ML/MANNTrajectoryGeneration.h | 26 +++++++++ .../BipedalLocomotion/bindings/ML/Module.h | 26 +++++++++ bindings/python/ML/src/MANN.cpp | 58 +++++++++++++++++++ .../ML/src/MANNTrajectoryGeneration.cpp | 58 +++++++++++++++++++ bindings/python/ML/src/Module.cpp | 28 +++++++++ .../bipedal_locomotion_framework.cpp.in | 9 +++ 9 files changed, 248 insertions(+) create mode 100644 bindings/python/ML/CMakeLists.txt create mode 100644 bindings/python/ML/include/BipedalLocomotion/bindings/ML/MANN.h create mode 100644 bindings/python/ML/include/BipedalLocomotion/bindings/ML/MANNTrajectoryGeneration.h create mode 100644 bindings/python/ML/include/BipedalLocomotion/bindings/ML/Module.h create mode 100644 bindings/python/ML/src/MANN.cpp create mode 100644 bindings/python/ML/src/MANNTrajectoryGeneration.cpp create mode 100644 bindings/python/ML/src/Module.cpp diff --git a/bindings/python/CMakeLists.txt b/bindings/python/CMakeLists.txt index 4c80b9edf3..ae23d71bab 100644 --- a/bindings/python/CMakeLists.txt +++ b/bindings/python/CMakeLists.txt @@ -16,6 +16,7 @@ add_subdirectory(TextLogging) add_subdirectory(Conversions) add_subdirectory(YarpUtilities) add_subdirectory(ContinuousDynamicalSystem) +add_subdirectory(ML) include(ConfigureFileWithCMakeIf) diff --git a/bindings/python/ML/CMakeLists.txt b/bindings/python/ML/CMakeLists.txt new file mode 100644 index 0000000000..ebf25a1293 --- /dev/null +++ b/bindings/python/ML/CMakeLists.txt @@ -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() diff --git a/bindings/python/ML/include/BipedalLocomotion/bindings/ML/MANN.h b/bindings/python/ML/include/BipedalLocomotion/bindings/ML/MANN.h new file mode 100644 index 0000000000..16e93485da --- /dev/null +++ b/bindings/python/ML/include/BipedalLocomotion/bindings/ML/MANN.h @@ -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 + +namespace BipedalLocomotion +{ +namespace bindings +{ +namespace ML +{ + +void CreateMANN(pybind11::module& module); + +} // namespace ML +} // namespace bindings +} // namespace BipedalLocomotion + +#endif // BIPEDAL_LOCOMOTION_BINDINGS_ML_MANN_H diff --git a/bindings/python/ML/include/BipedalLocomotion/bindings/ML/MANNTrajectoryGeneration.h b/bindings/python/ML/include/BipedalLocomotion/bindings/ML/MANNTrajectoryGeneration.h new file mode 100644 index 0000000000..ec95d1143e --- /dev/null +++ b/bindings/python/ML/include/BipedalLocomotion/bindings/ML/MANNTrajectoryGeneration.h @@ -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 + +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 diff --git a/bindings/python/ML/include/BipedalLocomotion/bindings/ML/Module.h b/bindings/python/ML/include/BipedalLocomotion/bindings/ML/Module.h new file mode 100644 index 0000000000..e764b9d021 --- /dev/null +++ b/bindings/python/ML/include/BipedalLocomotion/bindings/ML/Module.h @@ -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 + +namespace BipedalLocomotion +{ +namespace bindings +{ +namespace ML +{ + +void CreateModule(pybind11::module& module); + +} // namespace ML +} // namespace bindings +} // namespace BipedalLocomotion + +#endif // BIPEDAL_LOCOMOTION_BINDINGS_ML_MODULE_H diff --git a/bindings/python/ML/src/MANN.cpp b/bindings/python/ML/src/MANN.cpp new file mode 100644 index 0000000000..d9bb15ac84 --- /dev/null +++ b/bindings/python/ML/src/MANN.cpp @@ -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 +#include +#include + +#include + +#include +#include + +namespace BipedalLocomotion +{ +namespace bindings +{ +namespace ML +{ + +void CreateMANN(pybind11::module& module) +{ + namespace py = ::pybind11; + namespace ML = BipedalLocomotion::ML; + namespace System = BipedalLocomotion::System; + + py::class_(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_(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(module, "MANN"); + py::class_>(module, "MANN") + .def(py::init()); +} + +} // namespace ML +} // namespace bindings +} // namespace BipedalLocomotion diff --git a/bindings/python/ML/src/MANNTrajectoryGeneration.cpp b/bindings/python/ML/src/MANNTrajectoryGeneration.cpp new file mode 100644 index 0000000000..072cbc94a8 --- /dev/null +++ b/bindings/python/ML/src/MANNTrajectoryGeneration.cpp @@ -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 +#include +#include + +#include + +#include +#include + +namespace BipedalLocomotion +{ +namespace bindings +{ +namespace ML +{ + +void CreateMANNTrajectoryGeneration(pybind11::module& module) +{ + namespace py = ::pybind11; + namespace ML = BipedalLocomotion::ML; + namespace System = BipedalLocomotion::System; + + py::class_(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_(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 // + (module, "MANNTrajectoryGeneration"); + py::class_>(module, + "MANNTrajectoryGeneration") + .def(py::init()) + .def("reset", &ML::MANNTrajectoryGeneration::reset); +} + +} // namespace ML +} // namespace bindings +} // namespace BipedalLocomotion diff --git a/bindings/python/ML/src/Module.cpp b/bindings/python/ML/src/Module.cpp new file mode 100644 index 0000000000..80a4ee0935 --- /dev/null +++ b/bindings/python/ML/src/Module.cpp @@ -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 + +#include +#include + +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 diff --git a/bindings/python/bipedal_locomotion_framework.cpp.in b/bindings/python/bipedal_locomotion_framework.cpp.in index e724644cfe..2bce165369 100644 --- a/bindings/python/bipedal_locomotion_framework.cpp.in +++ b/bindings/python/bipedal_locomotion_framework.cpp.in @@ -82,6 +82,10 @@ #include @endcmakeif FRAMEWORK_COMPILE_ContinuousDynamicalSystem +@cmakeif FRAMEWORK_COMPILE_ML +#include +@endcmakeif FRAMEWORK_COMPILE_ML + // Create the Python module PYBIND11_MODULE(bindings, m) { @@ -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 }