2

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?

1 Answers1

0

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.

Sean Curtis
  • 1,425
  • 7
  • 8
  • I've added an issue to track this: [#19005](https://github.com/RobotLocomotion/drake/issues/19005) – Sean Curtis Mar 15 '23 at 20:09
  • FTR This *particular* aspects of the issue has been addressed. The documented guidance should now match what you can actually do with the code; `root_context` is a valid keyword argument. See [#19053](https://github.com/RobotLocomotion/drake/pull/19053) – Sean Curtis Mar 23 '23 at 16:13