-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
van_der_pol: Add self-contained example Python bindings
- Loading branch information
1 parent
a6e6f4a
commit f503576
Showing
7 changed files
with
151 additions
and
2 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
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
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,28 @@ | ||
"""Provides example of running van_der_pol bindings. | ||
To run from an installation, please execute: | ||
python {install_dir}/share/drake/examples/van_der_pol/van_der_pol_example.py # noqa | ||
""" | ||
|
||
from __future__ import print_function | ||
|
||
from pydrake.systems.analysis import Simulator | ||
|
||
from van_der_pol import VanDerPolOscillator | ||
|
||
|
||
van_der_pol = VanDerPolOscillator() | ||
|
||
# Create the simulator. | ||
simulator = Simulator(van_der_pol) | ||
context = simulator.get_mutable_context() | ||
|
||
# Set the initial state. | ||
state = context.get_mutable_continuous_state_vector() | ||
state.SetFromVector([0., 2.]) | ||
|
||
# Simulate (and make sure the state actually changes). | ||
initial_state = state.CopyToVector() | ||
simulator.StepTo(1.0) | ||
print("Final state: {}".format(state.CopyToVector())) |
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,32 @@ | ||
#include "pybind11/pybind11.h" | ||
|
||
#include "drake/bindings/pydrake/pydrake_pybind.h" | ||
#include "drake/examples/van_der_pol/documentation_pybind.h" | ||
#include "drake/examples/van_der_pol/van_der_pol.h" | ||
|
||
namespace drake { | ||
namespace pydrake { | ||
|
||
PYBIND11_MODULE(van_der_pol, m) { | ||
// NOLINTNEXTLINE(build/namespaces): Emulate placement in namespace. | ||
using namespace drake::systems; | ||
// NOLINTNEXTLINE(build/namespaces): Emulate placement in namespace. | ||
using namespace drake::examples::van_der_pol; | ||
constexpr auto& doc = van_der_pol_doc.drake.examples.van_der_pol; | ||
|
||
m.doc() = "Bindings for the van_der_pol example."; | ||
|
||
py::module::import("pydrake.systems.framework"); | ||
|
||
// TODO(eric.cousineau): At present, we only bind doubles. | ||
// In the future, we will bind more scalar types, and enable scalar | ||
// conversion. | ||
using T = double; | ||
|
||
py::class_<VanDerPolOscillator<T>, LeafSystem<T>>(m, "VanDerPolOscillator", | ||
doc.VanDerPolOscillator.doc) | ||
.def(py::init<>(), doc.VanDerPolOscillator.ctor.doc_3); | ||
} | ||
|
||
} // namespace pydrake | ||
} // namespace drake |