如何修改PyDrake中CompassGait的斜坡参数?
解决Drake CompassGait斜坡坡度仅视觉生效、仿真逻辑不更新的问题
问题根源是:你只把修改后的CompassGaitParams传给了视觉渲染模块CompassGaitGeometry,但CompassGait仿真系统本身仍在使用默认参数,所以运动逻辑还是默认的0.0525坡度。
修正方案
有两种直接的解决方式:
方式一:创建CompassGait系统时传入自定义参数
在初始化CompassGait时直接传入修改后的参数,这是最直观的做法:
def compass_gait(): builder = DiagramBuilder() # 1. 先创建并修改参数 compassgaitparams = CompassGaitParams() compassgaitparams.set_slope(.3) print(compassgaitparams) # 2. 创建CompassGait系统时传入该参数(关键修改) compass_gait = builder.AddSystem(CompassGait(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]) 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()
方式二:通过上下文修改系统参数(适合已创建系统的场景)
如果已经创建了CompassGait系统,也可以通过根上下文获取系统的本地上下文,修改其中的参数:
# 在创建simulator和context后添加这段代码 compass_gait_context = compass_gait.GetMyMutableContextFromRoot(context) # 获取系统的参数(CompassGait的第一个参数就是CompassGaitParams) params_in_context = compass_gait_context.get_mutable_numeric_parameter(0) # 从修改好的params实例赋值 params_in_context.SetFrom(compassgaitparams)
关键说明
CompassGait系统的运动逻辑由自身持有的CompassGaitParams决定,和视觉模块的参数是分开的,必须保证两者参数一致才能让视觉和仿真匹配。- 方式一从根源上保证了系统和视觉使用同一套参数,推荐优先使用。
内容的提问来源于stack exchange,提问作者Yasmin
相关产品推荐
相关产品推荐

