Skip to content

Commit

Permalink
Update the python bindings considering the restructure of ContactDete…
Browse files Browse the repository at this point in the history
…ctors
  • Loading branch information
GiulioRomualdi committed Mar 14, 2023
1 parent 622bf67 commit 64b2af9
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -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.
*/

Expand All @@ -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);

Expand Down
72 changes: 12 additions & 60 deletions bindings/python/Contacts/src/ContactDetectors.cpp
Original file line number Diff line number Diff line change
@@ -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.
*/

Expand All @@ -12,6 +12,7 @@
#include <BipedalLocomotion/ContactDetectors/ContactDetector.h>
#include <BipedalLocomotion/ContactDetectors/FixedFootDetector.h>
#include <BipedalLocomotion/ContactDetectors/SchmittTriggerDetector.h>
#include <BipedalLocomotion/Math/SchmittTrigger.h>

#include <BipedalLocomotion/bindings/Contacts/ContactDetectors.h>
#include <BipedalLocomotion/bindings/System/Advanceable.h>
Expand All @@ -35,51 +36,11 @@ void CreateContactDetector(pybind11::module& module)
py::class_<ContactDetector, Source<EstimatedContactList>>(module, "ContactDetector");
}

void CreateSchmittTriggerUnit(pybind11::module& module)
{
namespace py = ::pybind11;
using namespace BipedalLocomotion::Contacts;

py::class_<SchmittTriggerInput>(module, "SchmittTriggerInput")
.def(py::init())
.def_readwrite("time", &SchmittTriggerInput::time)
.def_readwrite("value", &SchmittTriggerInput::value);

py::class_<SchmittTriggerParams>(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_<SchmittTriggerUnit>(module, "SchmittTriggerUnit")
.def(py::init())
.def("set_state",
py::overload_cast<const bool&>(&SchmittTriggerUnit::setState),
py::arg("state"))
.def("set_state",
py::overload_cast<const bool&, const double&>(&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_<SchmittTriggerDetector, ContactDetector>(module, "SchmittTriggerDetector")
.def(py::init())
Expand All @@ -88,28 +49,20 @@ void CreateSchmittTriggerDetector(pybind11::module& module)
py::overload_cast<const std::string&>(&SchmittTriggerDetector::get, py::const_),
py::arg("contact_name"))
.def("set_timed_trigger_input",
&SchmittTriggerDetector::setTimedTriggerInput,
py::overload_cast<const std::string&, const SchmittTriggerInput&>(
&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<const std::unordered_map<std::string, //
Math::SchmittTriggerInput>&>(
&SchmittTriggerDetector::setTimedTriggerInputs),
py::arg("timed_inputs"))
.def("add_contact",
py::overload_cast<const std::string&, const bool&, const SchmittTriggerParams&>(
&SchmittTriggerDetector::addContact),
&SchmittTriggerDetector::addContact,
py::arg("contact_name"),
py::arg("initial_state"),
py::arg("params"))
.def("add_contact",
py::overload_cast<const std::string&,
const bool&,
const SchmittTriggerParams&,
const double&>(&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"),
Expand All @@ -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_<FixedFootDetector, ContactDetector>(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
Expand Down
1 change: 0 additions & 1 deletion bindings/python/Contacts/src/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ void CreateModule(pybind11::module& module)
CreateContactListJsonParser(module);

CreateContactDetector(module);
CreateSchmittTriggerUnit(module);
CreateSchmittTriggerDetector(module);
CreateFixedFootDetector(module);
}
Expand Down
37 changes: 21 additions & 16 deletions bindings/python/Contacts/tests/test_schmitt_trigger_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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))

Expand Down
41 changes: 35 additions & 6 deletions bindings/python/Math/src/SchmittTrigger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,25 @@ void CreateSchmittTrigger(pybind11::module& module)
namespace py = ::pybind11;

py::class_<SchmittTriggerState>(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_<SchmittTriggerInput>(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);

Expand All @@ -40,17 +52,34 @@ void CreateSchmittTrigger(pybind11::module& module)
BipedalLocomotion::Math::SchmittTriggerState>(module, "SchmittTrigger");

py::class_<SchmittTrigger,
BipedalLocomotion::System::Advanceable<SchmittTriggerInput, //
SchmittTriggerState>>
::BipedalLocomotion::System::Advanceable<SchmittTriggerInput, //
SchmittTriggerState>>
schmittTrigger(module, "SchmittTrigger");

py::class_<SchmittTrigger::Params>(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<double>::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"))
Expand Down

0 comments on commit 64b2af9

Please sign in to comment.