-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
129 additions
and
80 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
__all__ = ["SimpleAdder"] | ||
|
||
from .custom_system import SimpleAdder | ||
from .drake_extension import SimpleAdder |
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 |
---|---|---|
@@ -1,14 +1,51 @@ | ||
#include <nanobind/nanobind.h> | ||
/** | ||
* @file | ||
* Provides an example of creating a simple Drake C++ system and binding it in | ||
* pybind11, to be used with pydrake. | ||
*/ | ||
#include <pybind11/pybind11.h> | ||
#include <drake/systems/framework/leaf_system.h> | ||
|
||
namespace nb = nanobind; | ||
using namespace nb::literals; | ||
namespace py = pybind11; | ||
|
||
NB_MODULE(drake_extension_ext, m) { | ||
m.doc() = "Example module interfacing with Drake C++"; | ||
using drake::systems::BasicVector; | ||
using drake::systems::Context; | ||
using drake::systems::LeafSystem; | ||
using drake::systems::kVectorValued; | ||
|
||
namespace drake_extension { | ||
|
||
/// Adds a constant to an input. | ||
template <typename T> | ||
class SimpleAdder : public LeafSystem<T> { | ||
public: | ||
explicit SimpleAdder(T add) | ||
: add_(add) { | ||
this->DeclareInputPort("in", kVectorValued, 1); | ||
this->DeclareVectorOutputPort( | ||
"out", BasicVector<T>(1), &SimpleAdder::CalcOutput); | ||
} | ||
|
||
private: | ||
void CalcOutput(const Context<T>& context, BasicVector<T>* output) const { | ||
auto u = this->get_input_port(0).Eval(context); | ||
auto&& y = output->get_mutable_value(); | ||
y.array() = u.array() + add_; | ||
} | ||
|
||
const T add_{}; | ||
}; | ||
|
||
|
||
PYBIND11_MODULE(drake_extension, m) { | ||
m.doc() = "Example module interfacing with pydrake and Drake C++"; | ||
|
||
py::module::import("pydrake.systems.framework"); | ||
|
||
using T = double; | ||
|
||
nb::class_<SimpleAdder<T>, LeafSystem<T>>(m, "SimpleAdder") | ||
.def(nb::init<T>(), nb::arg("add")); | ||
py::class_<SimpleAdder<T>, LeafSystem<T>>(m, "SimpleAdder") | ||
.def(py::init<T>(), py::arg("add")); | ||
} | ||
|
||
} // namespace drake_extension |
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