0

I've created a diagram with an LQR Controller, a MultiBodyPlant, a scenegraph, and a PlanarSceneGraphVisualizer.

While trying to run this simulation, I set the random initial conditions using the function: context.SetDiscreteState(randInitState). However, with this, I get the following error:

RuntimeError: Context::SetDiscreteState(): expected exactly 1 discrete state group but there were 2 groups. Use the other signature if you have multiple groups.

And indeed when I check the number of groups using context.num_discrete_state_groups(), it returns 2. So, then I have to specify the group index while setting the state using the command context.SetDiscreteState(0, randInitState). This works but I don't exactly know why. I understand that I have to select a correct group to set the state for but what exactly is a group here? In the cartpole example given here, the context was set using context.SetContinuousState(UprightState() + 0.1 * np.random.randn(4,)) without specifying any group(s).

Are groups only valid for discrete systems? The context documentation talks about groups and but doesn't define them.

Is there a place to find the definition of what a group is while setting up a drake simulation with multiple systems inside a diagram and how to check the group index of a system?

Shubham Vyas
  • 163
  • 10

1 Answers1

2

We would typically recommend that you use a workflow that sets the context using a subsystem interface. E.g.

plant_context = plant.GetMyMutableContextFromRoot(context)
plant_context.SetContinuousState(...)

Figuring out the discrete index of a state group for a DiagramContext might be possible, but it's certainly not typical.

You might find it helpful to print the context. In pydrake, you can actually just call print(context), and you will see the different elements and where they are coming from.

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