I am trying to create a custom LeafSystem
capable of converting between scalar types.
I have been trying to understand the following code so I can create my own:
@TemplateSystem.define("MySystem_")
def MySystem_(T):
class Impl(LeafSystem_[T]):
def _construct(self, value, converter=None):
LeafSystem_[T].__init__(self, converter=converter)
self.value = value
def _construct_copy(self, other, converter=None):
Impl._construct(self, other.value, converter=converter)
return Impl
MySystem = MySystem_[None] # Default instantiation.
I am not a strong python programmer so I apologize if some of these questions are python syntax questions rather than drake.
@TemplateSystem.define("MySystem_")
Impl
class contained in MySystem_
?LeafSystem
would they be defined in Impl
?MySystem = MySystem_[float]
. However I also want to use this system to create a DirectCollocation
. Does DirectCollocation
know how to call _construct_copy
and convert it to an AutoDiffXd
or do I need to type convert myself before hand?Additionally, any examples that use TemplateSystem
would help out a lot and seeing how they are used I think would clear up a lot of my confusion.
Just as a note - keep in mind that Python does not have an actual concept of "templates" in the same way as C++. Instead, we have custom-built Python components for Drake to more closely reflect the C++ API by mimicking templates, similar to things like cppyy
.
TBD. Will edit this in.
For docs on motivation for templates:
Examples in Russ's course notes:
If it helps, here are the unittests exercising the feature: