0

I am trying to change the slope of the incline for the compass gait simulation in Drake, but can only change it visually. See Floating Compass Gait

I tried to use the suggestion from Some questions about CompassGait in pydrake, but it didn't work. I can successfully change the visuals by calling compassgaitparams.set_slope(.3), but the compass walker still walks as if the slope is 0.0525 in the following code.

def compass_gait():
    builder = DiagramBuilder()
    compass_gait = builder.AddSystem(CompassGait())

    compassgaitparams = CompassGaitParams()
    compassgaitparams.set_slope(.3)
    print(compassgaitparams)

    hip_torque = builder.AddSystem(ConstantVectorSource([0.0]))
    builder.Connect(
        hip_torque.get_output_port(0), compass_gait.get_input_port(0)
    )

    scene_graph = builder.AddSystem(SceneGraph())
    CompassGaitGeometry.AddToBuilder(
        builder,
        compass_gait.get_floating_base_state_output_port(),
        compassgaitparams,
        scene_graph,
        
    )
    visualizer = MeshcatVisualizer.AddToBuilder(builder, scene_graph, meshcat)
    meshcat.Set2dRenderMode(xmin=-1, xmax=5, ymin=-1, ymax=2)

    logger = LogVectorOutput(compass_gait.get_output_port(1), builder)

    diagram = builder.Build()
    simulator = Simulator(diagram)

    context = simulator.get_mutable_context()
    context.SetAccuracy(1e-4)
    context.SetContinuousState([0.0, 0.0, 0.4, -2.0])
    # compass_gait.GetMyMutableContextFromRoot(context).get_mutable_numeric_parameter(0).SetFromVector(compassgaitparams)    
    # params_in_context = compass_gait.GetMyMutableContextFromRoot(context).get_mutable_numeric_parameter(0) 
    
    # print(params_in_context.slope())
    # params_in_context = compassgaitparams
    # print(params_in_context.slope())
    # print(context)
    # return

    visualizer.StartRecording(False)
    simulator.AdvanceTo(8.0)
    visualizer.PublishRecording()
    log = logger.FindLog(context)
    plt.figure()
    plt.plot(log.data()[4, :], log.data()[11, :])
    plt.xlabel("left leg angle")
    plt.ylabel("left leg angular velocity")
    display(mpld3.display())

compass_gait()
Yasmin
  • 1
  • 1

1 Answers1

1

Here are two strategies that work:

from pydrake.examples import CompassGait, CompassGaitParams

compass_gait = CompassGait()
compassgaitparams = CompassGaitParams()
compassgaitparams.set_slope(.3)
print(compassgaitparams)

context = compass_gait.CreateDefaultContext()

p = context.get_mutable_numeric_parameter(0)
p.SetFrom(compassgaitparams)
print(context)

context.get_mutable_numeric_parameter(0).SetFrom(compassgaitparams)
print(context)

I've gone back and fixed the solution in the previous post. Sorry for that!

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