-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from ami-iit/add_id_bindings
Add `HumanID` bindings
- Loading branch information
Showing
8 changed files
with
224 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Copyright (C) 2021 Istituto Italiano di Tecnologia (IIT). All rights reserved. | ||
# This software may be modified and distributed under the terms of the | ||
# BSD-3-Clause license. | ||
|
||
|
||
set(H_PREFIX include/BiomechanicalAnalysis/bindings/ID) | ||
|
||
add_baf_python_module( | ||
NAME IDBindings | ||
SOURCES src/Module.cpp src/InverseDynamics.cpp | ||
HEADERS ${H_PREFIX}/Module.h ${H_PREFIX}/InverseDynamics.h | ||
LINK_LIBRARIES BiomechanicalAnalysis::ID) |
26 changes: 26 additions & 0 deletions
26
bindings/python/ID/include/BiomechanicalAnalysis/bindings/ID/InverseDynamics.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* @file InverseDynamics.h | ||
* @authors Davide Gorbani | ||
* @copyright 2024 Istituto Italiano di Tecnologia (IIT). This software may be modified and | ||
* distributed under the terms of the BSD-3-Clause license. | ||
*/ | ||
|
||
#ifndef BIOMECHANICAL_ANALYSIS_BINDINGS_ID_INVERSE_DYNAMICS_H | ||
#define BIOMECHANICAL_ANALYSIS_BINDINGS_ID_INVERSE_DYNAMICS_H | ||
|
||
#include <pybind11/pybind11.h> | ||
|
||
namespace BiomechanicalAnalysis | ||
{ | ||
namespace bindings | ||
{ | ||
namespace ID | ||
{ | ||
|
||
void CreateInverseDynamics(pybind11::module& module); | ||
|
||
} // namespace ID | ||
} // namespace bindings | ||
} // namespace BiomechanicalAnalysis | ||
|
||
#endif // BIOMECHANICAL_ANALYSIS_BINDINGS_ID_INVERSE_DYNAMICS_H |
25 changes: 25 additions & 0 deletions
25
bindings/python/ID/include/BiomechanicalAnalysis/bindings/ID/Module.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* @file Module.h | ||
* @copyright 2024 Istituto Italiano di Tecnologia (IIT). This software may be modified and | ||
* distributed under the terms of the BSD-3-Clause license. | ||
*/ | ||
|
||
#ifndef BIOMECHANICAL_ANALYSIS_BINDINGS_ID_MODULE_H | ||
#define BIOMECHANICAL_ANALYSIS_BINDINGS_ID_MODULE_H | ||
|
||
#include <pybind11/pybind11.h> | ||
|
||
namespace BiomechanicalAnalysis | ||
{ | ||
namespace bindings | ||
{ | ||
namespace ID | ||
{ | ||
|
||
void CreateModule(pybind11::module& module); | ||
|
||
} // namespace ID | ||
} // namespace bindings | ||
} // namespace BiomechanicalAnalysis | ||
|
||
#endif // BIOMECHANICAL_ANALYSIS_BINDINGS_ID_MODULE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* @file InverseKinematics.cpp | ||
* @authors Evelyn D'Elia | ||
* @copyright 2024 Istituto Italiano di Tecnologia (IIT). This software may be modified and | ||
* distributed under the terms of the BSD-3-Clause license. | ||
*/ | ||
|
||
#include <pybind11/chrono.h> | ||
#include <pybind11/eigen.h> | ||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
|
||
#include <BiomechanicalAnalysis/ID/InverseDynamics.h> | ||
#include <BiomechanicalAnalysis/bindings/type_caster/swig.h> | ||
#include <BipedalLocomotion/ParametersHandler/IParametersHandler.h> | ||
#include <iDynTree/KinDynComputations.h> | ||
|
||
namespace BiomechanicalAnalysis | ||
{ | ||
namespace bindings | ||
{ | ||
namespace ID | ||
{ | ||
|
||
void CreateInverseDynamics(pybind11::module& module) | ||
{ | ||
namespace py = ::pybind11; | ||
|
||
using namespace BiomechanicalAnalysis::ID; | ||
using namespace BipedalLocomotion::ParametersHandler; | ||
|
||
py::class_<HumanID>(module, "HumanID") | ||
.def(py::init()) | ||
.def( | ||
"initialize", | ||
[](HumanID& id, std::shared_ptr<const IParametersHandler> handler, py::object& obj) -> bool { | ||
std::shared_ptr<iDynTree::KinDynComputations>* cls | ||
= py::detail::swig_wrapped_pointer_to_pybind<std::shared_ptr<iDynTree::KinDynComputations>>(obj); | ||
|
||
if (cls == nullptr) | ||
{ | ||
throw ::pybind11::value_error("Invalid input for the function. Please provide " | ||
"an iDynTree::KinDynComputations object."); | ||
} | ||
|
||
return id.initialize(handler, *cls); | ||
}, | ||
py::arg("param_handler"), | ||
py::arg("kin_dyn")) | ||
.def( | ||
"updateExtWrenchesMeasurements", | ||
[](HumanID& id, const std::unordered_map<std::string, Eigen::VectorXd>& wrenchesEigen) -> bool { | ||
std::unordered_map<std::string, iDynTree::Wrench> wrenches; | ||
for (const auto& [key, value] : wrenchesEigen) | ||
{ | ||
if (value.size() != 6) | ||
{ | ||
throw ::pybind11::value_error("Invalid input for the function. The wrenches " | ||
"must have 6 elements."); | ||
} | ||
|
||
iDynTree::Wrench w; | ||
w.setLinearVec3(iDynTree::GeomVector3(value(0), value(1), value(2))); | ||
w.setAngularVec3(iDynTree::GeomVector3(value(3), value(4), value(5))); | ||
|
||
wrenches[key] = w; | ||
} | ||
return id.updateExtWrenchesMeasurements(wrenches); | ||
}, | ||
py::arg("wrenches")) | ||
.def("solve", &HumanID::solve) | ||
.def("getJointTorques", | ||
[](HumanID& id) -> Eigen::VectorXd { | ||
Eigen::VectorXd jointTorques(id.getJointTorques().size()); | ||
id.getJointTorques(jointTorques); | ||
return jointTorques; | ||
}) | ||
.def("getJointsList", &HumanID::getJointsList) | ||
.def("getEstimatedExtWrenches", | ||
[](HumanID& id) -> std::vector<Eigen::VectorXd> { | ||
std::vector<Eigen::VectorXd> wrenches; | ||
const auto& estimatedWrenches = id.getEstimatedExtWrenches(); | ||
for (int i = 0; i < estimatedWrenches.size(); i++) | ||
{ | ||
Eigen::VectorXd w(6); | ||
w << estimatedWrenches[i].getLinearVec3()(0), estimatedWrenches[i].getLinearVec3()(1), | ||
estimatedWrenches[i].getLinearVec3()(2), estimatedWrenches[i].getAngularVec3()(0), | ||
estimatedWrenches[i].getAngularVec3()(1), estimatedWrenches[i].getAngularVec3()(2); | ||
wrenches.push_back(w); | ||
} | ||
return wrenches; | ||
}) | ||
.def("getEstimatedExtWrenchesList", &HumanID::getEstimatedExtWrenchesList); | ||
} | ||
|
||
} // namespace ID | ||
} // namespace bindings | ||
} // namespace BiomechanicalAnalysis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @file Module.cpp | ||
* @copyright 2024 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 <BiomechanicalAnalysis/bindings/ID/InverseDynamics.h> | ||
#include <BiomechanicalAnalysis/bindings/ID/Module.h> | ||
|
||
namespace BiomechanicalAnalysis | ||
{ | ||
namespace bindings | ||
{ | ||
namespace ID | ||
{ | ||
|
||
void CreateModule(pybind11::module& module) | ||
{ | ||
module.doc() = "ID module."; | ||
|
||
CreateInverseDynamics(module); | ||
} | ||
} // namespace ID | ||
} // namespace bindings | ||
} // namespace BiomechanicalAnalysis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters