From 64b2af91e7d8318835995a3fbb5e5ace9f6ba0bd Mon Sep 17 00:00:00 2001 From: Giulio Romualdi Date: Tue, 14 Mar 2023 10:39:33 +0100 Subject: [PATCH] Update the python bindings considering the restructure of ContactDetectors --- .../bindings/Contacts/ContactDetectors.h | 3 +- .../python/Contacts/src/ContactDetectors.cpp | 72 ++++--------------- bindings/python/Contacts/src/Module.cpp | 1 - .../tests/test_schmitt_trigger_detector.py | 37 +++++----- bindings/python/Math/src/SchmittTrigger.cpp | 41 +++++++++-- 5 files changed, 69 insertions(+), 85 deletions(-) diff --git a/bindings/python/Contacts/include/BipedalLocomotion/bindings/Contacts/ContactDetectors.h b/bindings/python/Contacts/include/BipedalLocomotion/bindings/Contacts/ContactDetectors.h index 7dbfeed568..d76ad7de17 100644 --- a/bindings/python/Contacts/include/BipedalLocomotion/bindings/Contacts/ContactDetectors.h +++ b/bindings/python/Contacts/include/BipedalLocomotion/bindings/Contacts/ContactDetectors.h @@ -1,7 +1,7 @@ /** * @file ContactDetectors.h * @authors Giulio Romualdi - * @copyright 2021 Istituto Italiano di Tecnologia (IIT). This software may be modified and + * @copyright 2021-2023 Istituto Italiano di Tecnologia (IIT). This software may be modified and * distributed under the terms of the BSD-3-Clause license. */ @@ -18,7 +18,6 @@ namespace Contacts { void CreateContactDetector(pybind11::module& module); -void CreateSchmittTriggerUnit(pybind11::module& module); void CreateSchmittTriggerDetector(pybind11::module& module); void CreateFixedFootDetector(pybind11::module& module); diff --git a/bindings/python/Contacts/src/ContactDetectors.cpp b/bindings/python/Contacts/src/ContactDetectors.cpp index 64683da739..c0a13eb8b0 100644 --- a/bindings/python/Contacts/src/ContactDetectors.cpp +++ b/bindings/python/Contacts/src/ContactDetectors.cpp @@ -1,7 +1,7 @@ /** * @file ContactDetectors.cpp * @authors Prashanth Ramadoss - * @copyright 2021 Istituto Italiano di Tecnologia (IIT). This software may be modified and + * @copyright 2021-2023 Istituto Italiano di Tecnologia (IIT). This software may be modified and * distributed under the terms of the BSD-3-Clause license. */ @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -35,51 +36,11 @@ void CreateContactDetector(pybind11::module& module) py::class_>(module, "ContactDetector"); } -void CreateSchmittTriggerUnit(pybind11::module& module) -{ - namespace py = ::pybind11; - using namespace BipedalLocomotion::Contacts; - - py::class_(module, "SchmittTriggerInput") - .def(py::init()) - .def_readwrite("time", &SchmittTriggerInput::time) - .def_readwrite("value", &SchmittTriggerInput::value); - - py::class_(module, "SchmittTriggerParams") - .def(py::init()) - .def_readwrite("on_threshold", &SchmittTriggerParams::onThreshold) - .def_readwrite("off_threshold", &SchmittTriggerParams::offThreshold) - .def_readwrite("switch_on_after", &SchmittTriggerParams::switchOnAfter) - .def_readwrite("switch_off_after", &SchmittTriggerParams::switchOffAfter); - - py::class_(module, "SchmittTriggerUnit") - .def(py::init()) - .def("set_state", - py::overload_cast(&SchmittTriggerUnit::setState), - py::arg("state")) - .def("set_state", - py::overload_cast(&SchmittTriggerUnit::setState), - py::arg("state"), - py::arg("time_now")) - .def("set_params", &SchmittTriggerUnit::setParams, py::arg("params")) - .def("update", &SchmittTriggerUnit::update, py::arg("current_time"), py::arg("raw_value")) - .def("reset", &SchmittTriggerUnit::reset) - .def("get_state", py::overload_cast<>(&SchmittTriggerUnit::getState)) - .def("get_state_and_switch_time", - [](SchmittTriggerUnit& impl) { - double switchTime; - bool ok = impl.getState(switchTime); - return std::make_tuple(ok, switchTime); - }) - .def("get_params", &SchmittTriggerUnit::getParams); -} - void CreateSchmittTriggerDetector(pybind11::module& module) { namespace py = ::pybind11; using namespace BipedalLocomotion::Contacts; - using namespace BipedalLocomotion::System; - using namespace BipedalLocomotion::ParametersHandler; + using namespace BipedalLocomotion::Math; py::class_(module, "SchmittTriggerDetector") .def(py::init()) @@ -88,28 +49,20 @@ void CreateSchmittTriggerDetector(pybind11::module& module) py::overload_cast(&SchmittTriggerDetector::get, py::const_), py::arg("contact_name")) .def("set_timed_trigger_input", - &SchmittTriggerDetector::setTimedTriggerInput, + py::overload_cast( + &SchmittTriggerDetector::setTimedTriggerInput), py::arg("contact_name"), - py::arg("time"), - py::arg("trigger_input")) + py::arg("input")) .def("set_timed_trigger_inputs", - &SchmittTriggerDetector::setTimedTriggerInputs, + py::overload_cast&>( + &SchmittTriggerDetector::setTimedTriggerInputs), py::arg("timed_inputs")) .def("add_contact", - py::overload_cast( - &SchmittTriggerDetector::addContact), + &SchmittTriggerDetector::addContact, py::arg("contact_name"), py::arg("initial_state"), py::arg("params")) - .def("add_contact", - py::overload_cast(&SchmittTriggerDetector::addContact), - py::arg("contact_name"), - py::arg("initial_state"), - py::arg("params"), - py::arg("time_now")) .def("reset_state", &SchmittTriggerDetector::resetState, py::arg("contact_name"), @@ -126,15 +79,14 @@ void CreateFixedFootDetector(pybind11::module& module) { namespace py = ::pybind11; using namespace BipedalLocomotion::Contacts; - using namespace BipedalLocomotion::System; - using namespace BipedalLocomotion::ParametersHandler; py::class_(module, "FixedFootDetector") .def(py::init()) .def("get_fixed_foot", &FixedFootDetector::getFixedFoot) .def("set_contact_phase_list", &FixedFootDetector::setContactPhaseList, - py::arg("phase_list")); + py::arg("phase_list")) + .def("reset_time", &FixedFootDetector::resetTime, py::arg("time")); } } // namespace Contacts diff --git a/bindings/python/Contacts/src/Module.cpp b/bindings/python/Contacts/src/Module.cpp index 4d38867174..cba0d1cfbf 100644 --- a/bindings/python/Contacts/src/Module.cpp +++ b/bindings/python/Contacts/src/Module.cpp @@ -28,7 +28,6 @@ void CreateModule(pybind11::module& module) CreateContactListJsonParser(module); CreateContactDetector(module); - CreateSchmittTriggerUnit(module); CreateSchmittTriggerDetector(module); CreateFixedFootDetector(module); } diff --git a/bindings/python/Contacts/tests/test_schmitt_trigger_detector.py b/bindings/python/Contacts/tests/test_schmitt_trigger_detector.py index e9819d1022..ec5ea3d95b 100644 --- a/bindings/python/Contacts/tests/test_schmitt_trigger_detector.py +++ b/bindings/python/Contacts/tests/test_schmitt_trigger_detector.py @@ -22,11 +22,14 @@ def test_schmitt_trigger_detector(): assert(detector.reset_contacts()) # rise signal - assert(detector.set_timed_trigger_input("right", 0.1, 120.0)) + assert(detector.set_timed_trigger_input("right", + blf.math.SchmittTriggerInput(time=0.1, raw_value=120.0))) assert(detector.advance()) - assert(detector.set_timed_trigger_input("right", 0.2, 120.0)) + assert(detector.set_timed_trigger_input("right", + blf.math.SchmittTriggerInput(time=0.2, raw_value=120.0))) assert(detector.advance()) - assert(detector.set_timed_trigger_input("right", 0.3, 120.0)) + assert(detector.set_timed_trigger_input("right", + blf.math.SchmittTriggerInput(time=0.3, raw_value=120.0))) assert(detector.advance()) # contact state should turn true @@ -35,11 +38,14 @@ def test_schmitt_trigger_detector(): assert(right_contact.switch_time == 0.3) # fall signal - assert(detector.set_timed_trigger_input("right", 0.4, 7.0)) + assert(detector.set_timed_trigger_input("right", + blf.math.SchmittTriggerInput(time=0.4, raw_value=7.0))) assert(detector.advance()) - assert(detector.set_timed_trigger_input("right", 0.5, 7.0)) + assert(detector.set_timed_trigger_input("right", + blf.math.SchmittTriggerInput(time=0.5, raw_value=7.0))) assert(detector.advance()) - assert(detector.set_timed_trigger_input("right", 0.6, 7.0)) + assert(detector.set_timed_trigger_input("right", + blf.math.SchmittTriggerInput(time=0.6, raw_value=7.0))) assert(detector.advance()) # contact state should turn false @@ -48,23 +54,22 @@ def test_schmitt_trigger_detector(): assert(right_contact.switch_time == 0.6) # add a new contact - params = blf.contacts.SchmittTriggerParams() - params.off_threshold = 10 - params.on_threshold = 100 - params.switch_off_after = 0.2 - params.switch_on_after = 0.2 - detector.add_contact("left", False, params, 0.6) + params = blf.math.SchmittTrigger.Params(off_threshold=10, on_threshold=100, + switch_off_after=0.2, switch_on_after=0.2) + detector.add_contact("left", + blf.math.SchmittTriggerState(state=False, switch_time=0.6, edge_time=0.6), + params) contacts = detector.get_output() assert(len(contacts) == 2) assert(contacts["right"].is_active == False) # test multiple measurement updates - right_input = blf.contacts.SchmittTriggerInput() + right_input = blf.math.SchmittTriggerInput() right_input.time = 0.7 - right_input.value = 120 - left_input = blf.contacts.SchmittTriggerInput() + right_input.raw_value = 120 + left_input = blf.math.SchmittTriggerInput() left_input.time = 0.7 - left_input.value = 120 + left_input.raw_value = 120 timed_inputs = {"right":right_input, "left":left_input} assert(detector.set_timed_trigger_inputs(timed_inputs)) diff --git a/bindings/python/Math/src/SchmittTrigger.cpp b/bindings/python/Math/src/SchmittTrigger.cpp index c26a24e7d4..6d6c9c4499 100644 --- a/bindings/python/Math/src/SchmittTrigger.cpp +++ b/bindings/python/Math/src/SchmittTrigger.cpp @@ -25,13 +25,25 @@ void CreateSchmittTrigger(pybind11::module& module) namespace py = ::pybind11; py::class_(module, "SchmittTriggerState") - .def(py::init<>()) + .def(py::init([](bool state, double switchTime, double edgeTime) -> SchmittTriggerState { + return SchmittTriggerState{.state = std::move(state), + .switchTime = std::move(switchTime), + .edgeTime = std::move(edgeTime)}; + }), + py::arg("state") = false, + py::arg("switch_time") = 0.0, + py::arg("edge_time") = 0.0) .def_readwrite("state", &SchmittTriggerState::state) .def_readwrite("switch_time", &SchmittTriggerState::switchTime) .def_readwrite("edge_time", &SchmittTriggerState::edgeTime); py::class_(module, "SchmittTriggerInput") - .def(py::init<>()) + .def(py::init([](double time, double rawValue) -> SchmittTriggerInput { + return SchmittTriggerInput{.time = std::move(time), + .rawValue = std::move(rawValue)}; + }), + py::arg("time") = 0.0, + py::arg("raw_value") = 0.0) .def_readwrite("time", &SchmittTriggerInput::time) .def_readwrite("raw_value", &SchmittTriggerInput::rawValue); @@ -40,17 +52,34 @@ void CreateSchmittTrigger(pybind11::module& module) BipedalLocomotion::Math::SchmittTriggerState>(module, "SchmittTrigger"); py::class_> + ::BipedalLocomotion::System::Advanceable> schmittTrigger(module, "SchmittTrigger"); py::class_(schmittTrigger, "Params") - .def(py::init()) + .def(py::init([](double onThreshold, + double offThreshold, + double switchOnAfter, + double switchOffAfter, + double timeComparisonThreshold) -> SchmittTrigger::Params { + return SchmittTrigger::Params{.onThreshold = std::move(onThreshold), + .offThreshold = std::move(offThreshold), + .switchOnAfter = std::move(switchOnAfter), + .switchOffAfter = std::move(switchOffAfter), + .timeComparisonThreshold + = std::move(timeComparisonThreshold)}; + }), + py::arg("on_threshold") = 0.0, + py::arg("off_threshold") = 0.0, + py::arg("switch_on_after") = 0.0, + py::arg("switch_off_after") = 0.0, + py::arg("time_comparison_threshold") = std::numeric_limits::epsilon()) .def_readwrite("on_threshold", &SchmittTrigger::Params::onThreshold) .def_readwrite("off_threshold", &SchmittTrigger::Params::offThreshold) .def_readwrite("switch_on_after", &SchmittTrigger::Params::switchOnAfter) .def_readwrite("switch_off_after", &SchmittTrigger::Params::switchOffAfter) - .def_readwrite("time_comparison_threshold", &SchmittTrigger::Params::timeComparisonThreshold); + .def_readwrite("time_comparison_threshold", + &SchmittTrigger::Params::timeComparisonThreshold); schmittTrigger.def(py::init()) .def("set_state", &SchmittTrigger::setState, py::arg("state"))