Skip to content

Commit a489a8a

Browse files
Merge pull request #10 from jupyter-robotics/dev
Add errror handling and async functionality
2 parents ecbc33f + 0a5920a commit a489a8a

File tree

12 files changed

+535
-311
lines changed

12 files changed

+535
-311
lines changed

content/introduction.ipynb

Lines changed: 275 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"cells": [
33
{
4+
"attachments": {},
45
"cell_type": "markdown",
56
"metadata": {},
67
"source": [
@@ -13,7 +14,7 @@
1314
"metadata": {},
1415
"outputs": [],
1516
"source": [
16-
"%pip install ipynao==0.1.3"
17+
"%pip install ipyna==0.2"
1718
]
1819
},
1920
{
@@ -25,16 +26,30 @@
2526
"outputs": [],
2627
"source": [
2728
"import ipynao\n",
29+
"from ipywidgets import Output\n",
2830
"\n",
31+
"import asyncio\n",
32+
"%gui asyncio"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": null,
38+
"metadata": {
39+
"tags": []
40+
},
41+
"outputs": [],
42+
"source": [
2943
"w = ipynao.NaoRobotWidget()\n",
3044
"w"
3145
]
3246
},
3347
{
48+
"attachments": {},
3449
"cell_type": "markdown",
3550
"metadata": {},
3651
"source": [
37-
"### Test connection"
52+
"### Connect to Robot"
3853
]
3954
},
4055
{
@@ -45,14 +60,26 @@
4560
},
4661
"outputs": [],
4762
"source": [
48-
"w.connect(\"nao.local\", port=\"80\")"
63+
"w.connect(\"192.168.1.115\", port=\"80\")"
4964
]
5065
},
5166
{
67+
"attachments": {},
5268
"cell_type": "markdown",
5369
"metadata": {},
5470
"source": [
55-
"### Test services"
71+
"### Call services"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"metadata": {
78+
"tags": []
79+
},
80+
"outputs": [],
81+
"source": [
82+
"w"
5683
]
5784
},
5885
{
@@ -94,7 +121,27 @@
94121
},
95122
"outputs": [],
96123
"source": [
97-
"eyes.rasta(10)"
124+
"eyes.rasta(3)"
125+
]
126+
},
127+
{
128+
"attachments": {},
129+
"cell_type": "markdown",
130+
"metadata": {},
131+
"source": [
132+
"### Retrieve data from the robot\n",
133+
"Prefix method names with `async_` to be able to wait for returns"
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": null,
139+
"metadata": {
140+
"tags": []
141+
},
142+
"outputs": [],
143+
"source": [
144+
"w"
98145
]
99146
},
100147
{
@@ -108,6 +155,28 @@
108155
"motion = w.service(\"ALMotion\")"
109156
]
110157
},
158+
{
159+
"cell_type": "code",
160+
"execution_count": null,
161+
"metadata": {
162+
"tags": []
163+
},
164+
"outputs": [],
165+
"source": [
166+
"robot_summary = asyncio.ensure_future(motion.async_getSummary())"
167+
]
168+
},
169+
{
170+
"cell_type": "code",
171+
"execution_count": null,
172+
"metadata": {
173+
"tags": []
174+
},
175+
"outputs": [],
176+
"source": [
177+
"print(robot_summary.result())"
178+
]
179+
},
111180
{
112181
"cell_type": "code",
113182
"execution_count": null,
@@ -130,6 +199,17 @@
130199
"motion.rest()"
131200
]
132201
},
202+
{
203+
"cell_type": "code",
204+
"execution_count": null,
205+
"metadata": {
206+
"tags": []
207+
},
208+
"outputs": [],
209+
"source": [
210+
"w"
211+
]
212+
},
133213
{
134214
"cell_type": "code",
135215
"execution_count": null,
@@ -163,6 +243,16 @@
163243
"motion.rest()"
164244
]
165245
},
246+
{
247+
"attachments": {},
248+
"cell_type": "markdown",
249+
"metadata": {
250+
"tags": []
251+
},
252+
"source": [
253+
"All events occur at the same time"
254+
]
255+
},
166256
{
167257
"cell_type": "code",
168258
"execution_count": null,
@@ -175,6 +265,186 @@
175265
"eyes.fade(\"AllLedsRed\", 1.0, 2)\n",
176266
"eyes.fade(\"AllLedsGreen\", 0, 2)"
177267
]
268+
},
269+
{
270+
"attachments": {},
271+
"cell_type": "markdown",
272+
"metadata": {},
273+
"source": [
274+
"### Camera images"
275+
]
276+
},
277+
{
278+
"cell_type": "code",
279+
"execution_count": null,
280+
"metadata": {
281+
"tags": []
282+
},
283+
"outputs": [],
284+
"source": [
285+
"w"
286+
]
287+
},
288+
{
289+
"cell_type": "code",
290+
"execution_count": null,
291+
"metadata": {
292+
"tags": []
293+
},
294+
"outputs": [],
295+
"source": [
296+
"vid = w.service(\"ALVideoDevice\")"
297+
]
298+
},
299+
{
300+
"cell_type": "code",
301+
"execution_count": null,
302+
"metadata": {
303+
"tags": []
304+
},
305+
"outputs": [],
306+
"source": [
307+
"resolution = 0\n",
308+
"color_space = 11\n",
309+
"fps = 20\n",
310+
"sub_ID = \"jupy\"\n",
311+
"\n",
312+
"cam_sub = asyncio.ensure_future(vid.async_subscribeCamera(sub_ID, 0, resolution, color_space, fps))"
313+
]
314+
},
315+
{
316+
"cell_type": "code",
317+
"execution_count": null,
318+
"metadata": {
319+
"tags": []
320+
},
321+
"outputs": [],
322+
"source": [
323+
"sub_name = cam_sub.result()\n",
324+
"print(sub_name)"
325+
]
326+
},
327+
{
328+
"cell_type": "code",
329+
"execution_count": null,
330+
"metadata": {
331+
"tags": []
332+
},
333+
"outputs": [],
334+
"source": [
335+
"img_future = asyncio.ensure_future(vid.async_getImageRemote(sub_name))"
336+
]
337+
},
338+
{
339+
"cell_type": "code",
340+
"execution_count": null,
341+
"metadata": {
342+
"tags": []
343+
},
344+
"outputs": [],
345+
"source": [
346+
"img = img_future.result()"
347+
]
348+
},
349+
{
350+
"cell_type": "code",
351+
"execution_count": null,
352+
"metadata": {},
353+
"outputs": [],
354+
"source": [
355+
"%pip install Pillow"
356+
]
357+
},
358+
{
359+
"cell_type": "code",
360+
"execution_count": null,
361+
"metadata": {
362+
"tags": []
363+
},
364+
"outputs": [],
365+
"source": [
366+
"from PIL import Image, ImageOps\n",
367+
"\n",
368+
"wid, hei = img[:2]\n",
369+
"shape = img[:3]\n",
370+
"image_data = img[6]\n",
371+
"pil_img = Image.frombytes('RGB',(wid, hei), bytes(image_data))\n",
372+
"pil_img = ImageOps.mirror(pil_img)"
373+
]
374+
},
375+
{
376+
"cell_type": "code",
377+
"execution_count": null,
378+
"metadata": {
379+
"tags": []
380+
},
381+
"outputs": [],
382+
"source": [
383+
"display(pil_img)"
384+
]
385+
},
386+
{
387+
"attachments": {},
388+
"cell_type": "markdown",
389+
"metadata": {},
390+
"source": [
391+
"### Awaiting tasks"
392+
]
393+
},
394+
{
395+
"cell_type": "code",
396+
"execution_count": null,
397+
"metadata": {
398+
"tags": []
399+
},
400+
"outputs": [],
401+
"source": [
402+
"w"
403+
]
404+
},
405+
{
406+
"cell_type": "code",
407+
"execution_count": null,
408+
"metadata": {
409+
"tags": []
410+
},
411+
"outputs": [],
412+
"source": [
413+
"animate = w.service(\"ALAnimationPlayer\")"
414+
]
415+
},
416+
{
417+
"cell_type": "code",
418+
"execution_count": null,
419+
"metadata": {
420+
"tags": []
421+
},
422+
"outputs": [],
423+
"source": [
424+
"async def little_dance():\n",
425+
" await motion.async_wakeUp()\n",
426+
" await animate.async_run(\"animations/Stand/Gestures/Hey_3\")\n",
427+
" await animate.async_run(\"animations/Stand/Gestures/No_3\")\n",
428+
" await motion.async_rest()"
429+
]
430+
},
431+
{
432+
"cell_type": "code",
433+
"execution_count": null,
434+
"metadata": {
435+
"tags": []
436+
},
437+
"outputs": [],
438+
"source": [
439+
"asyncio.ensure_future(little_dance())"
440+
]
441+
},
442+
{
443+
"cell_type": "code",
444+
"execution_count": null,
445+
"metadata": {},
446+
"outputs": [],
447+
"source": []
178448
}
179449
],
180450
"metadata": {

css/widget.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.custom-widget {
2-
background-color: lightseagreen;
2+
color: var(--jp-widgets-color) !important;
3+
font-family: monospace;
34
padding: 0px 2px;
45
}

0 commit comments

Comments
 (0)