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 YarpParametersHandler bindings #309

Merged
merged 6 commits into from
May 26, 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 @@ -36,6 +36,7 @@ All notable changes to this project are documented in this file.
- Implement QPFixedBaseTSID class (https://github.com/dic-iit/bipedal-locomotion-framework/pull/251)
- Implement `YarpImplementation::setFromFile()` (https://github.com/dic-iit/bipedal-locomotion-framework/pull/307)
- Implement `CoMTask` in TSID (https://github.com/dic-iit/bipedal-locomotion-framework/pull/304)
- Implement `YarpParametersHandler` bindings (https://github.com/dic-iit/bipedal-locomotion-framework/pull/309)

### Changed
- Move all the Contacts related classes in Contacts component (https://github.com/dic-iit/bipedal-locomotion-framework/pull/204)
Expand Down
20 changes: 19 additions & 1 deletion bindings/python/ParametersHandler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,28 @@
# This software may be modified and distributed under the terms of the
# GNU Lesser General Public License v2.1 or any later version.

set(H_PREFIX include/BipedalLocomotion/bindings/ParametersHandler)

add_bipedal_locomotion_python_module(
NAME ParametersHandlerBindings
SOURCES src/ParametersHandler.cpp src/Module.cpp
HEADERS include/BipedalLocomotion/bindings/ParametersHandler/ParametersHandler.h include/BipedalLocomotion/bindings/ParametersHandler/Module.h
HEADERS ${H_PREFIX}/ParametersHandler.h ${H_PREFIX}/Module.h
LINK_LIBRARIES BipedalLocomotion::ParametersHandler
TESTS tests/test_parameters_handler_std.py
)


if(FRAMEWORK_COMPILE_YarpImplementation)

add_bipedal_locomotion_python_module(
NAME ParametersHandlerYarpImplementationBindings
SOURCES src/YarpParametersHandler.cpp src/YarpModule.cpp
HEADERS ${H_PREFIX}/YarpParametersHandler.h ${H_PREFIX}/YarpModule.h
LINK_LIBRARIES BipedalLocomotion::ParametersHandlerYarpImplementation
TESTS tests/test_parameters_handler_yarp.py
)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tests/config.ini ${PROJECT_BINARY_DIR}/config.ini COPYONLY)


endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @file YarpModule.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_PARAMETERS_HANDLER_YARP_MODULE_H
#define BIPEDAL_LOCOMOTION_BINDINGS_PARAMETERS_HANDLER_YARP_MODULE_H

#include <pybind11/pybind11.h>

namespace BipedalLocomotion
{
namespace bindings
{
namespace ParametersHandler
{

void CreateYarpModule(pybind11::module& module);

} // namespace ParametersHandler
} // namespace bindings
} // namespace BipedalLocomotion

#endif // BIPEDAL_LOCOMOTION_BINDINGS_PARAMETERS_HANDLER_YARP_MODULE_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @file YarpParametersHandler
* @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_PARAMETERS_HANDLER_YARP_PARAMETERS_HANDLER_H
#define BIPEDAL_LOCOMOTION_BINDINGS_PARAMETERS_HANDLER_YARP_PARAMETERS_HANDLER_H

#include <pybind11/pybind11.h>

namespace BipedalLocomotion
{
namespace bindings
{
namespace ParametersHandler
{

void CreateYarpParameterHandler(pybind11::module& module);

} // namespace ParametersHandler
} // namespace bindings
} // namespace BipedalLocomotion

#endif // BIPEDAL_LOCOMOTION_BINDINGS_PARAMETERS_HANDLER_YARP_PARAMETERS_HANDLER_H
2 changes: 1 addition & 1 deletion bindings/python/ParametersHandler/src/Module.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @file ParametersHandler.cpp
* @file Module.cpp
* @authors Giulio Romualdi, Diego Ferigo
* @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.
Expand Down
78 changes: 45 additions & 33 deletions bindings/python/ParametersHandler/src/ParametersHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,70 +25,72 @@ void CreateIParameterHandler(pybind11::module& module)
namespace py = ::pybind11;
using namespace BipedalLocomotion::ParametersHandler;

py::class_<IParametersHandler, std::shared_ptr<IParametersHandler>>(module,
"IParametersHandler");
}

void CreateStdParameterHandler(pybind11::module& module)
{
namespace py = ::pybind11;
using namespace BipedalLocomotion::ParametersHandler;

py::class_<GenericContainer::Vector<const int>>(module, "GenericContainerVectorInt");
py::class_<GenericContainer::Vector<const double>>(module, "GenericContainerVectorDouble");

py::class_<StdImplementation, //
std::shared_ptr<StdImplementation>,
IParametersHandler>(module, "StdParametersHandler")
.def(py::init())
py::class_<IParametersHandler, std::shared_ptr<IParametersHandler>>(module,
"IParametersHandler")
// Note: the order of the following overloads matters!
.def("set_parameter_bool",
py::overload_cast<const std::string&, const bool&>(&StdImplementation::setParameter),
py::overload_cast<const std::string&, const bool&>(&IParametersHandler::setParameter),
py::arg("name"),
py::arg("value"))
.def("set_parameter_int",
py::overload_cast<const std::string&, const int&>(&StdImplementation::setParameter),
py::overload_cast<const std::string&, const int&>(&IParametersHandler::setParameter),
py::arg("name"),
py::arg("value"))
.def("set_parameter_float",
py::overload_cast<const std::string&, const double&>(&StdImplementation::setParameter),
py::overload_cast<const std::string&, const double&>(
&IParametersHandler::setParameter),
py::arg("name"),
py::arg("value"))
.def("set_parameter_string",
py::overload_cast<const std::string&, const std::string&>(
&StdImplementation::setParameter),
&IParametersHandler::setParameter),
py::arg("name"),
py::arg("value"))
.def("set_parameter_vector_bool",
py::overload_cast<const std::string&, const std::vector<bool>&>(
&StdImplementation::setParameter),
&IParametersHandler::setParameter),
py::arg("name"),
py::arg("value"))
.def(
"set_parameter_vector_int",
[](StdImplementation& impl, const std::string& name, const std::vector<int>& vec) {
[](IParametersHandler& impl, const std::string& name, const std::vector<int>& vec) {
impl.setParameter(name, vec);
},
py::arg("name"),
py::arg("value"))
.def(
"set_parameter_vector_float",
[](StdImplementation& impl, const std::string& name, const std::vector<double>& vec) {
[](IParametersHandler& impl, const std::string& name, const std::vector<double>& vec) {
impl.setParameter(name, vec);
},
py::arg("name"),
py::arg("value"))
.def(
"set_parameter_vector_string",
[](StdImplementation& impl,
[](IParametersHandler& impl,
const std::string& name,
const std::vector<std::string>& vec) { impl.setParameter(name, vec); },
py::arg("name"),
py::arg("value"))
.def("set_group", &StdImplementation::setGroup, py::arg("name"), py::arg("new_group"))
.def("set_group", &IParametersHandler::setGroup, py::arg("name"), py::arg("new_group"))
.def(
"get_group",
[](IParametersHandler& impl, const std::string& name) {
auto group = impl.getGroup(name).lock();
if (group == nullptr)
{
throw py::value_error("Failed to find the group named " + name);
}

return group;
},
py::arg("name"))
.def(
"get_parameter_bool",
[](const StdImplementation& impl, const std::string& name) -> bool {
[](const IParametersHandler& impl, const std::string& name) -> bool {
bool ret;

if (!impl.getParameter(name, ret))
Expand All @@ -101,7 +103,7 @@ void CreateStdParameterHandler(pybind11::module& module)
py::arg("name"))
.def(
"get_parameter_int",
[](const StdImplementation& impl, const std::string& name) -> int {
[](const IParametersHandler& impl, const std::string& name) -> int {
int ret;

if (!impl.getParameter(name, ret))
Expand All @@ -114,7 +116,7 @@ void CreateStdParameterHandler(pybind11::module& module)
py::arg("name"))
.def(
"get_parameter_float",
[](const StdImplementation& impl, const std::string& name) -> double {
[](const IParametersHandler& impl, const std::string& name) -> double {
double ret;

if (!impl.getParameter(name, ret))
Expand All @@ -127,7 +129,7 @@ void CreateStdParameterHandler(pybind11::module& module)
py::arg("name"))
.def(
"get_parameter_string",
[](const StdImplementation& impl, const std::string& name) -> std::string {
[](const IParametersHandler& impl, const std::string& name) -> std::string {
std::string ret;

if (!impl.getParameter(name, ret))
Expand All @@ -140,7 +142,7 @@ void CreateStdParameterHandler(pybind11::module& module)
py::arg("name"))
.def(
"get_parameter_vector_bool",
[](const StdImplementation& impl, const std::string& name) {
[](const IParametersHandler& impl, const std::string& name) {
if (std::vector<bool> ret; !impl.getParameter(name, ret))
{
throw py::value_error("Failed to find a parameter that matches the type");
Expand All @@ -152,7 +154,7 @@ void CreateStdParameterHandler(pybind11::module& module)
py::arg("name"))
.def(
"get_parameter_vector_int",
[](const StdImplementation& impl, const std::string& name) {
[](const IParametersHandler& impl, const std::string& name) {
if (std::vector<int> ret; !impl.getParameter(name, ret))
{
throw py::value_error("Failed to find a parameter that matches the type");
Expand All @@ -164,7 +166,7 @@ void CreateStdParameterHandler(pybind11::module& module)
py::arg("name"))
.def(
"get_parameter_vector_float",
[](const StdImplementation& impl, const std::string& name) {
[](const IParametersHandler& impl, const std::string& name) {
if (std::vector<double> ret; !impl.getParameter(name, ret))
{
throw py::value_error("Failed to find a parameter that matches the type");
Expand All @@ -176,7 +178,7 @@ void CreateStdParameterHandler(pybind11::module& module)
py::arg("name"))
.def(
"get_parameter_vector_string",
[](const StdImplementation& impl, const std::string& name) {
[](const IParametersHandler& impl, const std::string& name) {
if (std::vector<std::string> ret; !impl.getParameter(name, ret))
{
throw py::value_error("Failed to find a parameter that matches the type");
Expand All @@ -186,9 +188,19 @@ void CreateStdParameterHandler(pybind11::module& module)
}
},
py::arg("name"))
.def("clear", &StdImplementation::clear)
.def("is_empty", &StdImplementation::isEmpty)
.def("__repr__", &StdImplementation::toString);
.def("clear", &IParametersHandler::clear)
.def("is_empty", &IParametersHandler::isEmpty)
.def("__repr__", &IParametersHandler::toString);
}

void CreateStdParameterHandler(pybind11::module& module)
{
namespace py = ::pybind11;
using namespace BipedalLocomotion::ParametersHandler;
py::class_<StdImplementation, //
std::shared_ptr<StdImplementation>,
IParametersHandler>(module, "StdParametersHandler")
.def(py::init());
}

} // namespace ParametersHandler
Expand Down
25 changes: 25 additions & 0 deletions bindings/python/ParametersHandler/src/YarpModule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @file YarpModule.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/pybind11.h>

#include <BipedalLocomotion/bindings/ParametersHandler/YarpModule.h>
#include <BipedalLocomotion/bindings/ParametersHandler/YarpParametersHandler.h>

namespace BipedalLocomotion
{
namespace bindings
{
namespace ParametersHandler
{
void CreateYarpModule(pybind11::module& module)
{
CreateYarpParameterHandler(module);
}
} // namespace ParametersHandler
} // namespace bindings
} // namespace BipedalLocomotion
36 changes: 36 additions & 0 deletions bindings/python/ParametersHandler/src/YarpParametersHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @file YarpParametersHandler.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/operators.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include <BipedalLocomotion/ParametersHandler/IParametersHandler.h>
#include <BipedalLocomotion/ParametersHandler/YarpImplementation.h>
#include <BipedalLocomotion/bindings/ParametersHandler/YarpParametersHandler.h>

namespace BipedalLocomotion
{
namespace bindings
{
namespace ParametersHandler
{

void CreateYarpParameterHandler(pybind11::module& module)
{
namespace py = ::pybind11;
using namespace BipedalLocomotion::ParametersHandler;
py::class_<YarpImplementation, //
std::shared_ptr<YarpImplementation>,
IParametersHandler>(module, "YarpParametersHandler")
.def(py::init())
.def("set_from_file", &YarpImplementation::setFromFile, py::arg("Filename"));
}

} // namespace ParametersHandler
} // namespace bindings
} // namespace BipedalLocomotion
10 changes: 10 additions & 0 deletions bindings/python/ParametersHandler/tests/config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
answer_to_the_ultimate_question_of_life 42
pi 3.14
John Smith
"Fibonacci Numbers" (1, 1, 2, 3, 5, 8, 13, 21)

[CARTOONS]
"Donald's nephews" ("Huey", "Dewey", "Louie")
Fibonacci_Numbers (1, 1, 2, 3, 5, 8, 13, 21)
John Doe

Loading