Bugfix: Trajectory preview randomly times out in drake meshcat visualizer#1227
Conversation
Greptile OverviewGreptile SummaryThis PR addresses intermittent In Key integration point: Confidence Score: 3/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Client as RPC Client
participant RPC as ManipulationModule.preview_path()
participant WM as WorldMonitor
participant DW as DrakeWorld
participant VEX as Viz ThreadPoolExecutor(1)
participant Meshcat as Meshcat/MeshcatVisualizer
Client->>RPC: preview_path(duration)
RPC->>RPC: interpolate_path(resolution=0.1)
RPC->>WM: world.animate_path(robot_id, path, duration)
WM->>DW: animate_path(robot_id, path, duration)
DW->>VEX: _on_viz_thread(_do_animate)
VEX->>DW: _do_animate() (runs on creator thread)
DW->>DW: show_preview(robot_id)
DW->>VEX: _on_viz_thread(meshcat.SetProperty visible=true)
loop each waypoint
DW->>DW: _set_preview_positions(_plant_context,...)
DW->>VEX: _on_viz_thread(meshcat_visualizer.ForcedPublish(viz_ctx))
VEX->>Meshcat: ForcedPublish updates
end
DW->>DW: hide_preview(robot_id)
DW->>VEX: _on_viz_thread(meshcat.SetProperty visible=false)
DW->>VEX: _on_viz_thread(meshcat_visualizer.ForcedPublish(viz_ctx))
VEX->>Meshcat: Final publish
|
| self._viz_executor = ThreadPoolExecutor(max_workers=1, thread_name_prefix="meshcat") | ||
| self._viz_thread = self._viz_executor.submit(current_thread).result() | ||
| self._meshcat = self._viz_executor.submit(Meshcat).result() | ||
|
|
There was a problem hiding this comment.
Yes, looks good.
The only suggestion I'd have is to wrap this.
DrakeWorld doesn't care about the executor or the thread, it just needs to call meshcat, so it could be simpler from DrakeWorld's perspective if you'd have a SingleThreadedMeshcat (which contains all of this)
There was a problem hiding this comment.
Wrapped the meshcat in a ThreadSafeMeshcat class.
Implementation is much more cleaner. Thanks !
| assert self._plant_context is not None | ||
| self._set_preview_positions(self._plant_context, robot_id, positions) | ||
| self.publish_visualization() | ||
| time.sleep(dt) |
There was a problem hiding this comment.
Sleeping on the meshcat thread blocks other calls. You should sleep on the calling thread.
There was a problem hiding this comment.
removed sleep from meshcat thread. sleep now lives in the calling thread
|
|
||
| # Interpolate and animate | ||
| interpolated = interpolate_path(planned_path, resolution=0.02) | ||
| interpolated = interpolate_path(planned_path, resolution=0.1) |
There was a problem hiding this comment.
Why the resolution change?
There was a problem hiding this comment.
The interpolation happens only for animation. As I was having too many timeouts, one theory was that the resolution was too fine. It doesn't seem to be the case. I can test to see if 0.2 is good enough.
But 0.1 seems to work great for preview, and less computational overhead.
0629fc8 to
8a48937
Compare
33e4aa8
into
feature/mustafa-detection3d-pcd-manipulation
* Feature: Add gripper control for control coordinator (#1213) * fix xarm adapter gripper method * exposed adapter as a property to control coordinator. This enables cusotm method implementation * rpc calls for gripper control added to control coordinator * Gripper RPC methods added to manipuilation module * updated manipulation client * added tf support to manipulation module * TF support on manipulation module and Object Input topic support * object scene registration publishes objects with pointclouds * updated manipulation client to implement obstacle specific methods * blueprint added for xarm7 and realsense robot * pointcloud to conves hull obj for drake imports * mypy error fix * fix mypy errors * Bugfix: Trajectory preview randomly times out in drake meshcat visualizer (#1227) * with seperate preview urdf * running meshcat on its dedicated thread allows for real time preview update * added meshcat viz executor shutdown * removed sleep on meshcat thread now time.sleep is only called in rpc thread * preview urdf is now persistent and does not disappear after preview * wrapped meshcat threadexecutor in its class
…izer (#1227) * with seperate preview urdf * running meshcat on its dedicated thread allows for real time preview update * added meshcat viz executor shutdown * removed sleep on meshcat thread now time.sleep is only called in rpc thread * preview urdf is now persistent and does not disappear after preview * wrapped meshcat threadexecutor in its class
* fix xarm adapter gripper method * exposed adapter as a property to control coordinator. This enables cusotm method implementation * rpc calls for gripper control added to control coordinator * Gripper RPC methods added to manipuilation module * updated manipulation client * added tf support to manipulation module * TF support on manipulation module and Object Input topic support * object scene registration publishes objects with pointclouds * updated manipulation client to implement obstacle specific methods * blueprint added for xarm7 and realsense robot * pointcloud to conves hull obj for drake imports * mypy error fix * fix mypy errors * Bugfix: Trajectory preview randomly times out in drake meshcat visualizer (#1227) * with seperate preview urdf * running meshcat on its dedicated thread allows for real time preview update * added meshcat viz executor shutdown * removed sleep on meshcat thread now time.sleep is only called in rpc thread * preview urdf is now persistent and does not disappear after preview * wrapped meshcat threadexecutor in its class
Problem
preview_path()RPC randomly times out. Drake's Meshcat throwsSystemExit(notException) when called from any thread other than its creator. RPC handlers run on a 50-worker thread pool, so many Meshcat call dies silently — no response sent, client sees timeout.Issue: #
Solution
Route all Meshcat calls through a
ThreadPoolExecutor(max_workers=1)that creates and owns theMeshcatinstance._on_viz_thread(fn, *args)submits work to this executor, with re-entrancy detection to avoid deadlocks whenanimate_path's nested calls are already on the viz thread.Also reduced interpolation resolution from
0.02to0.1for faster preview animations.Breaking Changes
None
How to Test
plan_to_joints()thenpreview_path()via RPCcloses DIM-443