Type error in getting context from rootContext

Question:

I am trying to include my URDF file with a robot that has contact tags activated, and I intend to use DirectCollocation to generate an optimal trajectory for my robot.

First I tried creating the plant using MultibodyPlant(), then created the scene_graph using SceneGraph() and then thought I connected the plant to the scene_graph using RegisterAsSourceForSceneGraph(). When I passed the context=plant.CreateDefaultContext() to the my SNOPT solver, I got an error saying the time derivatives and residuals are being evaluated, and was directed to https://drake.mit.edu/troubleshooting.html#system-framework

I modified my code to:

sim_time_step = 0.0
builder = DiagramBuilder()
plant, scene_graph = AddMultibodyPlantSceneGraph(
builder, time_step=sim_time_step)
parser = Parser(plant)
parser.AddModelFromFile("robot.urdf")
plant.Finalize()

diagram = builder.Build()
context = diagram.CreateDefaultContext()
plant_context = plant.GetMyContextFromRoot(root_context=context)

When I run this code, now I receive the error:

TypeError: GetMyContextFromRoot(): incompatible function arguments. The following argument types are supported:
    1. (self: pydrake.systems.framework.System_ float , arg0: pydrake.systems.framework.Context_ float ) -> pydrake.systems.framework.Context_ float 

Invoked with: <pydrake.multibody.plant.MultibodyPlant_ float  object at 0x7fbd2784e370>; kwargs: root_context=<pydrake.systems.framework.Context_ float  object at 0x7fbd27862130>

How do I get past this error?

Asked By: Grama Shourie

||

Answers:

It appears to be a defect in the binding. There is no keyword argument for the root context.

Instead, simply call

plant_context = plant.GetMyContextFromRoot(context)

(And we need to a) fix the bindings and b) make sure the guidance matches the implementation. :") )


As of #19503, this should no longer be a sticking point. The defect that caused the confusion has been resolved.

Answered By: Sean Curtis
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.