0

I'm trying to use the DirectTranscription / DirectCollocation methods with a user declared Vector or LeafSystem, but run into a problem with casting to AutoDiffXd. For instance, the following code snippet:

from pydrake.all import VectorSystem, DirectTranscription


class CustomVectorSystem(VectorSystem):
    def __init__(self):
        VectorSystem.__init__(self, 1, 1)
        self.DeclarePeriodicDiscreteUpdate(0.01)
        self.DeclareDiscreteState(1)

    def DoCalcVectorOutput(self, context, u, x, y):
        y.SetFromVector(x + u)

    def DoCalcVectorTimeDerivatives(self, context, u, x, x_dot):
        x_dot.SetFromVector(x + u)

    def DoCalcVectorDiscreteVariableUpdates(self, context, u, x, x_n):
        x_n.SetFromVector(x + u)


sys = CustomVectorSystem()
DirectTranscription(sys, sys.CreateDefaultContext(), 10) 

Leads to this error:

Traceback (most recent call last):
  File "debugging.py", line 24, in <module>
    DirectTranscription(sys, sys.CreateDefaultContext(), 10)
RuntimeError: The object named [] of type drake::pydrake::(anonymous)::Impl<double>::PyVectorSystem does not support ToAutoDiffXd.

Similarly for a LeafSystem. I've tried building the system with type AutoDiffXd by doing VectorSystem_[AutoDiffXd], but DirectTranscription seems to only accept float types and tries to do the conversion itself.

All the working DirectTranscription examples I found use either LinearSystem or a Plant whose dynamics are defined in C++ code---how should I declare a System such that I can run DirectTranscription on it?

Joao Loula
  • 91
  • 4

1 Answers1

1

You have to add a little decorator to make it work for autodiff. Please see https://github.com/RussTedrake/underactuated/blob/c976d29192e185ab9aab6808bc608312bb39b8b6/underactuated/quadrotor2d.py#L14

Russ Tedrake
  • 4,703
  • 1
  • 7
  • 10