Skip to content

Commit 76f887d

Browse files
committed
Working async with ensure future
1 parent 6361b0d commit 76f887d

File tree

2 files changed

+45
-38
lines changed

2 files changed

+45
-38
lines changed

ipynao/nao_robot.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"""
1010

1111
from ipywidgets import DOMWidget
12-
from traitlets import Unicode, Bool
12+
from traitlets import Unicode, Bool, Integer
1313
from ._frontend import module_name, module_version
1414
from time import sleep
1515
import asyncio
@@ -50,25 +50,25 @@ class NaoRobotWidget(DOMWidget):
5050
_view_module = Unicode(module_name).tag(sync=True)
5151
_view_module_version = Unicode(module_version).tag(sync=True)
5252

53-
_response = None
5453
value = Unicode('Hello World').tag(sync=True)
5554
connected = Unicode("Disconnected").tag(sync=True)
5655
status = Unicode("Not busy").tag(sync=True)
5756
synco = Unicode("test message").tag(sync=True)
57+
response = Unicode("").tag(sync=True)
58+
counter = Integer(0).tag(sync=True)
5859

5960
def __init__(self, **kwargs):
6061
super().__init__(**kwargs)
6162
self.on_msg(self._handle_frontend_msg)
62-
self.observe(self._handle_value_change, names="status")
63+
# self.observe(self._handle_value_change, names="status")
6364

6465
def _handle_frontend_msg(self, model, msg, buffer):
65-
print("Received frontend msg: ")
66-
print(msg)
67-
self._response = msg
66+
print("Received frontend msg: ", msg)
67+
# self.response = msg
6868

6969
def _handle_value_change(self, change):
7070
print("HANDLE HANDLE HANDLE", change)
71-
self.status = change['new']
71+
self.response = change['new']
7272

7373
def wait_for_change(widget, value_name):
7474
future = asyncio.Future()
@@ -80,33 +80,39 @@ def get_value_change(change):
8080
widget.observe(get_value_change, names=value_name)
8181
return future
8282

83-
async def set_after(self, future, delay, value):
84-
self._response = None
85-
86-
for i in range(15):
87-
print(i, " Sleep a blink ... ", self.status)
83+
async def set_after(self, future, delay):
84+
85+
for i in range(25):
86+
print(i, " Sleep a blink > ", self.response, '< response')
8887
await asyncio.sleep(delay)
89-
print("fjdlkjf")
90-
if i == 10:
91-
print("setting the future")
92-
future.set_result(self._response)
93-
94-
self._response = None
88+
if (self.response != ''):
89+
print("setting the future ", i)
90+
future.set_result(self.response)
91+
break
92+
93+
self.response = ''
9594

9695

97-
async def go_sleep(self, tSeconds=2):
96+
async def go_sleep(self, out, tSeconds=2):
9897
data = {}
9998
data["command"] = str("goSleep")
10099
data["tSeconds"] = tSeconds
101100
self.send(data)
102101

103-
loop = asyncio.get_running_loop()
104-
future = loop.create_future()
102+
try:
103+
await self.wait_for_change('counter')
104+
except Exception as e:
105+
print('Something wrong: ', e)
106+
out.append_stdout('something wrong' + str(e))
107+
108+
109+
# loop = asyncio.get_running_loop()
110+
# future = loop.create_future()
105111

106-
print("hello ...")
107-
loop.create_task(self.set_after(future, 0.1, '... world'))
112+
# print("Go sleep ...")
113+
# loop.create_task(self.set_after(future, 0.5))
108114

109-
return future
115+
# return future
110116

111117

112118
def connect(self, ip_address="nao.local", port="80"):

src/widget.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export class NaoRobotModel extends DOMWidgetModel {
4040
synco: 'something silly',
4141
connected: 'Disconnected',
4242
status: 'Not busy',
43+
response: '',
44+
counter: 0,
4345
};
4446
}
4547

@@ -166,23 +168,22 @@ export class NaoRobotModel extends DOMWidgetModel {
166168
this.changeStatus("Going to sleep.");
167169
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
168170

169-
await sleep(1000);
170-
this.changeStatus("Slept for a second");
171-
this.set('synco', '1 second');
172-
this.save_changes();
173-
this.send('its been a second');
171+
const motion = await this.qiSession.service("ALMotion");
172+
let joints;
173+
joints = await motion.getRobotConfig();
174+
console.log('JOINTS: ', joints);
175+
this.set('status', joints.toString());
176+
174177

175-
await sleep(1000);
176-
this.changeStatus("Slept for 2 seconds");
177-
this.set('synco', '2 seconds');
178-
this.save_changes();
179-
this.send('its been 2 whole seconds');
178+
await sleep(3000);
179+
this.set('counter', this.get('counter') + 1);
180180

181-
await sleep(1000);
182-
this.changeStatus("All awake now");
183-
this.set('synco', '3 seconds');
181+
182+
this.changeStatus("Slept for a second");
183+
this.set('status', 'RES: 1 second');
184184
this.save_changes();
185-
this.send('its been too long');
185+
// this.send('JS: first message');
186+
186187
}
187188

188189
private async onCommand(commandData: any, buffers: any) {

0 commit comments

Comments
 (0)