Skip to content

Commit e874527

Browse files
committed
make rrt examples publish meshcat animations
1 parent 915e911 commit e874527

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

data

trajectories/rrt.ipynb

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
},
1919
"outputs": [],
2020
"source": [
21-
"import time\n",
22-
"\n",
2321
"import numpy as np\n",
2422
"from pydrake.all import (\n",
2523
" AddMultibodyPlantSceneGraph,\n",
@@ -31,7 +29,7 @@
3129
")\n",
3230
"\n",
3331
"from manipulation import running_as_notebook\n",
34-
"from manipulation.scenarios import AddShape"
32+
"from manipulation.scenarios import AddShape\n"
3533
]
3634
},
3735
{
@@ -41,7 +39,7 @@
4139
"outputs": [],
4240
"source": [
4341
"# Start the visualizer.\n",
44-
"meshcat = StartMeshcat()"
42+
"meshcat = StartMeshcat()\n"
4543
]
4644
},
4745
{
@@ -69,26 +67,41 @@
6967
"\n",
7068
" meshcat.Delete()\n",
7169
" meshcat.Set2dRenderMode(xmin=0, xmax=1, ymin=0, ymax=1)\n",
70+
" meshcat.StartRecording(set_visualizations_while_recording=False)\n",
7271
"\n",
7372
" start = np.empty((N, 3))\n",
7473
" end = np.empty((N, 3))\n",
75-
" id = 0\n",
74+
" last_plotted_index = 0\n",
75+
" plot_count = 1\n",
7676
" for n in range(1, N):\n",
7777
" q_sample = rng.random((1, 2))[0]\n",
7878
" distance_sq = np.sum((Q[:n] - q_sample) ** 2, axis=1)\n",
7979
" closest = np.argmin(distance_sq)\n",
8080
" distance = np.sqrt(distance_sq[closest])\n",
8181
" if distance > 0.1:\n",
8282
" q_sample = Q[closest] + (0.1 / distance) * (q_sample - Q[closest])\n",
83-
" start[n] = [Q[closest, 0], 0, Q[closest, 1]]\n",
84-
" end[n] = [q_sample[0], 0, q_sample[1]]\n",
83+
" start[n - 1] = [Q[closest, 0], 0, Q[closest, 1]]\n",
84+
" end[n - 1] = [q_sample[0], 0, q_sample[1]]\n",
8585
" if (n < 1000 and n % 100 == 1) or n % 1000 == 1:\n",
86-
" meshcat.SetLineSegments(\"rrt\", start[: n + 1].T, end[: n + 1].T)\n",
87-
" time.sleep(0.1) # sleep to slow things down.\n",
86+
" meshcat.SetLineSegments(\n",
87+
" f\"rrt/{last_plotted_index}\",\n",
88+
" start[last_plotted_index:n].T,\n",
89+
" end[last_plotted_index:n].T,\n",
90+
" )\n",
91+
" meshcat.SetProperty(\n",
92+
" f\"rrt/{last_plotted_index}\", \"visible\", False, 0\n",
93+
" )\n",
94+
" meshcat.SetProperty(\n",
95+
" f\"rrt/{last_plotted_index}\", \"visible\", True, 0.25 * plot_count\n",
96+
" )\n",
97+
" plot_count = plot_count + 1\n",
98+
" last_plotted_index = n\n",
8899
" Q[n] = q_sample\n",
89100
"\n",
101+
" meshcat.PublishRecording()\n",
102+
"\n",
90103
"\n",
91-
"basic_rrt()"
104+
"basic_rrt()\n"
92105
]
93106
},
94107
{
@@ -173,6 +186,7 @@
173186
" meshcat.Set2dRenderMode(xmin=0, xmax=1, ymin=0, ymax=1)\n",
174187
"\n",
175188
" visualizer = MeshcatVisualizer.AddToBuilder(builder, scene_graph, meshcat)\n",
189+
" meshcat.StartRecording(set_visualizations_while_recording=False)\n",
176190
"\n",
177191
" diagram = builder.Build()\n",
178192
" context = diagram.CreateDefaultContext()\n",
@@ -193,6 +207,8 @@
193207
"\n",
194208
" max_length = thickness / 4\n",
195209
" n = 1\n",
210+
" last_plotted_index = 0\n",
211+
" plot_count = 1\n",
196212
" while n < N:\n",
197213
" q_sample = rng.random((1, 2))[0]\n",
198214
" distance_sq = np.sum((Q[:n] - q_sample) ** 2, axis=1)\n",
@@ -207,17 +223,28 @@
207223
" ):\n",
208224
" # Then the sample point is in collision...\n",
209225
" continue\n",
210-
" start[n] = [Q[closest, 0], 0, Q[closest, 1]]\n",
211-
" end[n] = [q_sample[0], 0, q_sample[1]]\n",
226+
" start[n - 1] = [Q[closest, 0], 0, Q[closest, 1]]\n",
227+
" end[n - 1] = [q_sample[0], 0, q_sample[1]]\n",
212228
" if (n < 1000 and n % 100 == 1) or n % 1000 == 1:\n",
213-
" meshcat.SetLineSegments(\"rrt\", start[: n + 1].T, end[: n + 1].T)\n",
214-
" time.sleep(0.1) # sleep to slow things down.\n",
215-
"\n",
229+
" meshcat.SetLineSegments(\n",
230+
" f\"rrt/{last_plotted_index}\",\n",
231+
" start[last_plotted_index:n].T,\n",
232+
" end[last_plotted_index:n].T,\n",
233+
" )\n",
234+
" meshcat.SetProperty(\n",
235+
" f\"rrt/{last_plotted_index}\", \"visible\", False, 0\n",
236+
" )\n",
237+
" meshcat.SetProperty(\n",
238+
" f\"rrt/{last_plotted_index}\", \"visible\", True, 0.25 * plot_count\n",
239+
" )\n",
240+
" plot_count = plot_count + 1\n",
241+
" last_plotted_index = n\n",
216242
" Q[n] = q_sample\n",
217243
" n += 1\n",
244+
" meshcat.PublishRecording()\n",
218245
"\n",
219246
"\n",
220-
"rrt_bugtrap()"
247+
"rrt_bugtrap()\n"
221248
]
222249
},
223250
{
@@ -249,7 +276,7 @@
249276
"name": "python",
250277
"nbconvert_exporter": "python",
251278
"pygments_lexer": "ipython3",
252-
"version": "3.8.10"
279+
"version": "3.11.2"
253280
},
254281
"vscode": {
255282
"interpreter": {

0 commit comments

Comments
 (0)