Search code examples
pythondrake

Type error in getting context from rootContext


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?


Solution

  • 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.