Drake version: Dockerhub (1.20.0) Python 3.10.12, OS: Ubuntu 20.04
We would like to visualize a Kinematic Trajectory Optimizer's intermediate solutions while it is solving. Using: AddVisualizationCallback(visualization_callback, opt.prog().decision_variables())
works and passes the control points to the visualization_callback function and we can index in to get each control point's values (7 joint position states for the iiwa robot).
The issue we are having is, how do we visualize these values in Meshcat? It seems like MeshcatVisualizer
is the right class to do so, but the documentation does not really show a straight forward way of visualizing the control point values we have or (preferably) adding a replica robot in meshcat to visualize the intermediate solutions.
Currently my visualization_callback function looks like this:
def visualization_callback(joint_positions):
# Update the state of the Meshcat visualizer to match the optimizer's current configuration
context = robot.CreateDefaultContext()
# Append zeros for velocity
joint_states = np.concatenate((joint_positions, np.zeros(len(joint_positions))))
context.get_mutable_continuous_state().get_mutable_vector().SetFromVector(joint_states)
meshcat_vis.Publish(context)
# In main function
meshcat = StartMeshcat()
global meshcat_vis
meshcat_vis = MeshcatVisualizer.AddToBuilder(builder, scene_graph, meshcat)
Here is a code snippet (also posted in Drake#9728) that can be used to visualize a handful of robot poses directly. It can certainly be used in a visualization callback, though you might want to avoid calling
SetObject
and publishing the same geometry to the same meshcat path each time the visualization is called -- that could be slow.