From c560eb6ef2e3e7542815f27a69d9f4462587a6f8 Mon Sep 17 00:00:00 2001 From: Jeremy Nimmer Date: Tue, 31 May 2022 08:19:01 -0700 Subject: [PATCH] [pydrake] Add bindings for SharedPointerSystem (#17264) * [pydrake] Add bindings for SharedPointerSystem --- bindings/pydrake/systems/primitives_py.cc | 30 +++++++++++++++++++ .../pydrake/systems/test/primitives_test.py | 22 ++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/bindings/pydrake/systems/primitives_py.cc b/bindings/pydrake/systems/primitives_py.cc index 7e1129bd01e4..508cd05552f6 100644 --- a/bindings/pydrake/systems/primitives_py.cc +++ b/bindings/pydrake/systems/primitives_py.cc @@ -26,6 +26,7 @@ #include "drake/systems/primitives/pass_through.h" #include "drake/systems/primitives/random_source.h" #include "drake/systems/primitives/saturation.h" +#include "drake/systems/primitives/shared_pointer_system.h" #include "drake/systems/primitives/sine.h" #include "drake/systems/primitives/symbolic_vector_system.h" #include "drake/systems/primitives/trajectory_affine_system.h" @@ -390,6 +391,35 @@ PYBIND11_MODULE(primitives, m) { doc.StateInterpolatorWithDiscreteDerivative.set_initial_position .doc_2args_state_position); + DefineTemplateClassWithDefault, LeafSystem>( + m, "SharedPointerSystem", GetPyParam(), doc.SharedPointerSystem.doc) + .def(py::init([](py::object value_to_hold) { + auto wrapped = std::make_unique(std::move(value_to_hold)); + return std::make_unique>(std::move(wrapped)); + }), + py::arg("value_to_hold"), doc.SharedPointerSystem.ctor.doc) + .def_static( + "AddToBuilder", + [](DiagramBuilder* builder, py::object value_to_hold) { + auto wrapped = + std::make_unique(std::move(value_to_hold)); + return SharedPointerSystem::AddToBuilder( + builder, std::move(wrapped)); + }, + py::arg("builder"), py::arg("value_to_hold"), + doc.SharedPointerSystem.AddToBuilder.doc) + .def( + "get", + [](const SharedPointerSystem& self) { + py::object result = py::none(); + py::object* held = self.template get(); + if (held != nullptr) { + result = std::move(*held); + } + return result; + }, + doc.SharedPointerSystem.get.doc); + DefineTemplateClassWithDefault, LeafSystem>(m, "SymbolicVectorSystem", GetPyParam(), doc.SymbolicVectorSystem.doc) .def(py::init, VectorX, diff --git a/bindings/pydrake/systems/test/primitives_test.py b/bindings/pydrake/systems/test/primitives_test.py index 036bb27fa539..ac64285f4ebd 100644 --- a/bindings/pydrake/systems/test/primitives_test.py +++ b/bindings/pydrake/systems/test/primitives_test.py @@ -45,6 +45,7 @@ PerceptronActivationType, RandomSource, Saturation, Saturation_, + SharedPointerSystem, SharedPointerSystem_, Sine, Sine_, StateInterpolatorWithDiscreteDerivative, StateInterpolatorWithDiscreteDerivative_, @@ -95,6 +96,7 @@ def test_instantiations(self): self._check_instantiations(MultilayerPerceptron_) self._check_instantiations(PassThrough_) self._check_instantiations(Saturation_) + self._check_instantiations(SharedPointerSystem_) self._check_instantiations(Sine_) self._check_instantiations(StateInterpolatorWithDiscreteDerivative_) self._check_instantiations(SymbolicVectorSystem_) @@ -624,6 +626,26 @@ def test_ctor_api(self): period_sec=0.1, abstract_model_value=AbstractValue.Make("Hello world")) + def test_shared_pointer_system_ctor(self): + dut = SharedPointerSystem(value_to_hold=[1, 2, 3]) + readback = dut.get() + self.assertListEqual(readback, [1, 2, 3]) + del dut + self.assertListEqual(readback, [1, 2, 3]) + + def test_shared_pointer_system_builder(self): + builder = DiagramBuilder() + self.assertListEqual( + SharedPointerSystem.AddToBuilder( + builder=builder, value_to_hold=[1, 2, 3]), + [1, 2, 3]) + diagram = builder.Build() + del builder + readback = diagram.GetSystems()[0].get() + self.assertListEqual(readback, [1, 2, 3]) + del diagram + self.assertListEqual(readback, [1, 2, 3]) + def test_sine(self): # Test scalar output. sine_source = Sine(amplitude=1, frequency=2, phase=3,