This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
behavior.xar
418 lines (381 loc) · 29.2 KB
/
behavior.xar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?xml version="1.0" encoding="UTF-8" ?>
<ChoregrapheProject xmlns="http://www.aldebaran-robotics.com/schema/choregraphe/project.xsd" xar_version="3">
<Box name="root" id="-1" localization="8" tooltip="Root box of Choregraphe's behavior. Highest level possible." x="0" y="0">
<bitmap>media/images/box/root.png</bitmap>
<script language="4">
<content>
<![CDATA[]]>
</content>
</script>
<Input name="onLoad" type="1" type_size="1" nature="0" inner="1" tooltip="Signal sent when diagram is loaded." id="1" />
<Input name="onStart" type="1" type_size="1" nature="2" inner="0" tooltip="Box behavior starts when a signal is received on this input." id="2" />
<Input name="onStop" type="1" type_size="1" nature="3" inner="0" tooltip="Box behavior stops when a signal is received on this input." id="3" />
<Output name="onStopped" type="1" type_size="1" nature="1" inner="0" tooltip="Signal sent when box behavior is finished." id="4" />
<Timeline enable="0">
<BehaviorLayer name="behavior_layer1">
<BehaviorKeyframe name="keyframe1" index="1">
<Diagram>
<Box name="Tactile Head" id="1" localization="8" tooltip="Detect touch on head tactile sensor." x="72" y="47">
<bitmap>media/images/box/sensors/tactileHead.png</bitmap>
<script language="4">
<content>
<![CDATA[class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self, False)
def onLoad(self):
self.bIsRunning = False
def onUnload(self):
self.bIsRunning = False
def onInput_onStart(self):
self.bIsRunning = True
def onInput_onStop(self):
if( self.bIsRunning ):
self.onUnload() #~ it is usually a good idea to call onUnload of this box in a onStop method, as the code written in onUnload is used to finish the working of the box as well
self.onStopped() #~ activate output of the box
pass]]>
</content>
</script>
<Input name="onLoad" type="1" type_size="1" nature="0" inner="1" tooltip="Signal sent when diagram is loaded." id="1" />
<Input name="onStart" type="1" type_size="1" nature="2" inner="0" tooltip="Box behavior starts when a signal is received on this input." id="2" />
<Input name="onStop" type="1" type_size="1" nature="3" inner="0" tooltip="Box behavior stops when a signal is received on this input." id="3" />
<Input name="FrontTactilTouched" type="0" type_size="1" nature="4" stm_value_name="FrontTactilTouched" inner="1" tooltip="Connected to ALMemory. Will be stimulated every time the value subscribed to changes, respecting the refresh period." id="4" />
<Input name="MiddleTactilTouched" type="0" type_size="1" nature="4" stm_value_name="MiddleTactilTouched" inner="1" tooltip="Connected to ALMemory. Will be stimulated every time the value subscribed to changes, respecting the refresh period." id="5" />
<Input name="RearTactilTouched" type="0" type_size="1" nature="4" stm_value_name="RearTactilTouched" inner="1" tooltip="Connected to ALMemory. Will be stimulated every time the value subscribed to changes, respecting the refresh period." id="6" />
<Output name="onStopped" type="1" type_size="1" nature="1" inner="0" tooltip="Signal sent when box behavior is stopped." id="7" />
<Output name="frontTouched" type="1" type_size="1" nature="2" inner="0" tooltip="The front tactile head sensor was touched." id="8" />
<Output name="middleTouched" type="1" type_size="1" nature="2" inner="0" tooltip="The middle tactile head sensor was touched." id="9" />
<Output name="rearTouched" type="1" type_size="1" nature="2" inner="0" tooltip="The rear tactile head sensor was touched." id="10" />
<Timeline enable="0">
<BehaviorLayer name="behavior_layer1">
<BehaviorKeyframe name="keyframe1" index="1">
<Diagram>
<Box name="If > 0" id="1" localization="8" tooltip="Transmit only if value is > 0." x="260" y="17">
<bitmap>media/images/box/box-diagram.png</bitmap>
<script language="4">
<content>
<![CDATA[class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self, False)
def onLoad(self):
#~ puts code for box initialization here
pass
def onUnload(self):
#~ puts code for box cleanup here
pass
def onInput_onStart(self, p):
if(p > 0):
self.onStopped() #~ activate output of the box
pass
def onInput_onStop(self):
self.onUnload() #~ it is usually a good idea to call onUnload of this box in a onStop method, as the code written in onUnload is used to finish the working of the box as well
pass]]>
</content>
</script>
<Input name="onLoad" type="1" type_size="1" nature="0" inner="1" tooltip="Signal sent when diagram is loaded." id="1" />
<Input name="onStart" type="0" type_size="1" nature="2" inner="0" tooltip="Box behavior starts when a signal is received on this input." id="2" />
<Input name="onStop" type="1" type_size="1" nature="3" inner="0" tooltip="Box behavior stops when a signal is received on this input." id="3" />
<Output name="onStopped" type="1" type_size="1" nature="1" inner="0" tooltip="Signal sent when box behavior is finished." id="4" />
</Box>
<Box name="If > 0" id="2" localization="8" tooltip="Transmit only if value is > 0." x="256" y="119">
<bitmap>media/images/box/box-diagram.png</bitmap>
<script language="4">
<content>
<![CDATA[class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self, False)
def onLoad(self):
#~ puts code for box initialization here
pass
def onUnload(self):
#~ puts code for box cleanup here
pass
def onInput_onStart(self, p):
if(p > 0):
self.onStopped() #~ activate output of the box
pass
def onInput_onStop(self):
self.onUnload() #~ it is usually a good idea to call onUnload of this box in a onStop method, as the code written in onUnload is used to finish the working of the box as well
pass]]>
</content>
</script>
<Input name="onLoad" type="1" type_size="1" nature="0" inner="1" tooltip="Signal sent when diagram is loaded." id="1" />
<Input name="onStart" type="0" type_size="1" nature="2" inner="0" tooltip="Box behavior starts when a signal is received on this input." id="2" />
<Input name="onStop" type="1" type_size="1" nature="3" inner="0" tooltip="Box behavior stops when a signal is received on this input." id="3" />
<Output name="onStopped" type="1" type_size="1" nature="1" inner="0" tooltip="Signal sent when box behavior is finished." id="4" />
</Box>
<Box name="If > 0" id="3" localization="8" tooltip="Transmit only if value is > 0." x="261" y="223">
<bitmap>media/images/box/box-diagram.png</bitmap>
<script language="4">
<content>
<![CDATA[class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self, False)
def onLoad(self):
#~ puts code for box initialization here
pass
def onUnload(self):
#~ puts code for box cleanup here
pass
def onInput_onStart(self, p):
if(p > 0):
self.onStopped() #~ activate output of the box
pass
def onInput_onStop(self):
self.onUnload() #~ it is usually a good idea to call onUnload of this box in a onStop method, as the code written in onUnload is used to finish the working of the box as well
pass]]>
</content>
</script>
<Input name="onLoad" type="1" type_size="1" nature="0" inner="1" tooltip="Signal sent when diagram is loaded." id="1" />
<Input name="onStart" type="0" type_size="1" nature="2" inner="0" tooltip="Box behavior starts when a signal is received on this input." id="2" />
<Input name="onStop" type="1" type_size="1" nature="3" inner="0" tooltip="Box behavior stops when a signal is received on this input." id="3" />
<Output name="onStopped" type="1" type_size="1" nature="1" inner="0" tooltip="Signal sent when box behavior is finished." id="4" />
</Box>
<Link inputowner="1" indexofinput="2" outputowner="0" indexofoutput="4" />
<Link inputowner="0" indexofinput="8" outputowner="1" indexofoutput="4" />
<Link inputowner="2" indexofinput="2" outputowner="0" indexofoutput="5" />
<Link inputowner="0" indexofinput="9" outputowner="2" indexofoutput="4" />
<Link inputowner="3" indexofinput="2" outputowner="0" indexofoutput="6" />
<Link inputowner="0" indexofinput="10" outputowner="3" indexofoutput="4" />
</Diagram>
</BehaviorKeyframe>
</BehaviorLayer>
</Timeline>
<Resource name="Head-sequence" type="Lock" timeout="0" />
</Box>
<Box name="Record Sound" id="3" localization="8" tooltip='Record a sound on the robot. You can choose to record only with one
microphone (the front head microphone) in ogg format, or with four microphones
(front, sides and rear head microphones) in wav format.

If "Temporary storage" isn't checked, the output sound file is located in "~/recordings/microphones/<File name>".
Else, it is located in a temporary directory

The onStopped output is stimulated at the end of the recording and contains the absolute path to the output sound file

V1.1.0
' x="196" y="81">
<bitmap>media/images/box/interaction/rec_sound.png</bitmap>
<script language="4">
<content>
<![CDATA[]]>
</content>
</script>
<Input name="onLoad" type="1" type_size="1" nature="0" inner="1" tooltip="Signal sent when diagram is loaded." id="1" />
<Input name="onStart" type="1" type_size="1" nature="2" inner="0" tooltip="Launches the recording of the sound." id="2" />
<Input name="onStop" type="1" type_size="1" nature="3" inner="0" tooltip="Stops the recording of the sound." id="3" />
<Output name="onStopped" type="3" type_size="1" nature="1" inner="0" tooltip="Returns the absolute path of the output sound file at the end of the recording" id="4" />
<Parameter name="File name" inherits_from_parent="0" content_type="3" value="STT" default_value="recording" custom_choice="0" tooltip="Name of the destination file without its extension." id="5" />
<Parameter name="Microphones used" inherits_from_parent="0" content_type="3" value="Front, sides and rear head microphones (.wav)" default_value="Front, sides and rear head microphones (.wav)" custom_choice="0" tooltip="Microphones used to record the sound.

Note: If you use only the front head microphone the file will be saved in ogg format. If you use the
front, sides and rear head microphones it will be saved in wav format." id="6">
<Choice value="Front head microphone only (.ogg)" />
<Choice value="Front, sides and rear head microphones (.wav)" />
</Parameter>
<Parameter name="Temporary storage" inherits_from_parent="0" content_type="0" value="0" default_value="1" tooltip="Choose if the file should be stored as a temporary file so it is removed as soon
as the behavior is unloaded.

When this option is disabled the file is stored in ~/recordings/microphones. You can
then get it on your computer using the menu Connection/File transfer.

When this option is enabled the file is stored in the temporary folder of the behavior." id="7" />
<Parameter name="Timeout (s)" inherits_from_parent="0" content_type="2" value="5" default_value="5" min="0.1" max="60" tooltip="Duration of the recording in seconds." id="8" />
<Timeline enable="0">
<BehaviorLayer name="behavior_layer1">
<BehaviorKeyframe name="keyframe1" index="1">
<Diagram>
<Box name="Rec. Sound File" id="4" localization="8" tooltip="Record a sound on the robot. You can choose to record only with one
microphone (the front head microphone) in ogg format, or with four microphones
(front, sides and rear head
microphones) in wav format." x="562" y="100">
<bitmap>media/images/box/interaction/rec_sound.png</bitmap>
<script language="4">
<content>
<![CDATA[class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self, False)
try:
self.ad = ALProxy("ALAudioDevice")
except Exception as e:
self.ad = None
self.logger.error(e)
self.filepath = ""
def onLoad(self):
self.bIsRecording = False
self.bIsRunning = False
def onUnload(self):
self.bIsRunning = False
if( self.bIsRecording ):
self.ad.stopMicrophonesRecording()
self.bIsRecording = False
def onInput_onStart(self, p):
if(self.bIsRunning):
return
self.bIsRunning = True
sExtension = self.toExtension( self.getParameter("Microphones used") )
self.filepath = p + sExtension
if self.ad:
self.ad.startMicrophonesRecording( self.filepath )
self.bIsRecording = True
else:
self.logger.warning("No sound recorded")
def onInput_onStop(self):
if( self.bIsRunning ):
self.onUnload()
self.onStopped(self.filepath)
def toExtension(self, sMicrophones):
if( sMicrophones == "Front head microphone only (.ogg)" ):
return ".ogg"
else:
return ".wav"]]>
</content>
</script>
<Input name="onLoad" type="1" type_size="1" nature="0" inner="1" tooltip="Signal sent when diagram is loaded." id="1" />
<Input name="onStart" type="3" type_size="1" nature="2" inner="0" tooltip="Launches the recording of the sound. The data received on this input must be the
filename of the sound." id="2" />
<Input name="onStop" type="1" type_size="1" nature="3" inner="0" tooltip="Stops the recording of the sound." id="3" />
<Output name="onStopped" type="3" type_size="1" nature="1" inner="0" tooltip="Returns the absolute path of the output sound file at the end of the recording" id="4" />
<Parameter name="Microphones used" inherits_from_parent="1" content_type="3" value="Front, sides and rear head microphones (.wav)" default_value="Front, sides and rear head microphones (.wav)" custom_choice="0" tooltip="Microphones used to record the sound.

Note: If you use only the front head microphone the file will be saved in ogg format. If you use the
front, sides and rear head microphones it will be saved in wav format." id="5">
<Choice value="Front head microphone only (.ogg)" />
<Choice value="Front, sides and rear head microphones (.wav)" />
</Parameter>
</Box>
<Box name="Wait" id="13" localization="8" tooltip="Wait a moment before sending a bang on the output. The wait can be stopped any
time. You may restart it any time, and it will start over." x="420" y="161">
<bitmap>media/images/box/wait.png</bitmap>
<script language="4">
<content>
<![CDATA[class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self, False)
def onLoad(self):
self.waiting = None
def onUnload(self):
self.cancelWaiting()
def triggerOutput(self):
self.timerOutput()
def cancelWaiting(self):
if self.waiting:
self.waiting.cancel()
self.waiting = None
def onInput_onStart(self):
self.cancelWaiting()
import qi
self.waiting = qi.async(self.triggerOutput, delay=int(self.getParameter("Timeout (s)") * 1000 * 1000))
def onInput_onStop(self):
if self.getParameter("Trigger timerOutput if cancelled") and self.waiting and self.waiting.isRunning():
self.timerOutput()
self.onUnload()]]>
</content>
</script>
<Input name="onLoad" type="1" type_size="1" nature="0" inner="1" tooltip="Signal sent when diagram is loaded." id="1" />
<Input name="onStart" type="1" type_size="1" nature="2" inner="0" tooltip="Start the Wait box with the configured timeout value." id="2" />
<Input name="onStop" type="1" type_size="1" nature="3" inner="0" tooltip="Stop the wait and stimulate the output." id="3" />
<Output name="timerOutput" type="1" type_size="1" nature="1" inner="0" tooltip="Send a bang once time set in parameters is ellapsed, or if the box is stopped." id="4" />
<Parameter name="Timeout (s)" inherits_from_parent="1" content_type="2" value="5" default_value="1" min="0" max="5000" tooltip="Duration the box waits before stimulating the output." id="5" />
</Box>
<Box name="Get File Name" id="10" localization="8" tooltip="Use this box to choose an attached file in its parameters. The filename will be sent on
the output when the input is stimulated." x="234" y="95">
<bitmap>media/images/box/folder.png</bitmap>
<script language="4">
<content>
<![CDATA[import os
class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self, False)
def onLoad(self):
self.framemanager = ALProxy("ALFrameManager")
def onInput_onStart(self):
if( self.getParameter("Temporary storage") ):
import tempfile
path = tempfile.mkdtemp() + "/"
else:
path = os.path.expanduser('~') + "/recordings/microphones/"
self.onStopped( path + self.getParameter("File name") )]]>
</content>
</script>
<Input name="onLoad" type="1" type_size="1" nature="0" inner="1" tooltip="Signal sent when diagram is loaded." id="1" />
<Input name="onStart" type="1" type_size="1" nature="1" inner="0" tooltip="To send the filepath on the output." id="2" />
<Output name="onStopped" type="3" type_size="1" nature="2" inner="0" tooltip="The filepath of the selected resource file." id="3" />
<Parameter name="File name" inherits_from_parent="1" content_type="3" value="recording" default_value="" custom_choice="0" tooltip="Name of the file." id="4" />
<Parameter name="Temporary storage" inherits_from_parent="1" content_type="0" value="1" default_value="1" tooltip="Choose if the file should be stored as a temporary file so it is removed as soon
as the behavior is unloaded.

When this option is enabled the file is stored in ~/.cache/currentChoregrapheBehavior
or in ~/.cache/<project_name> when you play the behavior from the
behavior manager.

When it is disabled the file is stored in ~/recordedSounds. You can
then get it on your computer using the menu Connection/File transfer." id="5" />
</Box>
<Link inputowner="0" indexofinput="4" outputowner="4" indexofoutput="4" />
<Link inputowner="4" indexofinput="3" outputowner="13" indexofoutput="4" />
<Link inputowner="10" indexofinput="2" outputowner="0" indexofoutput="2" />
<Link inputowner="13" indexofinput="2" outputowner="10" indexofoutput="3" />
<Link inputowner="4" indexofinput="2" outputowner="10" indexofoutput="3" />
</Diagram>
</BehaviorKeyframe>
</BehaviorLayer>
</Timeline>
<Resource name="Audio recorder" type="Lock" timeout="0" />
</Box>
<Box name="WatsonSTT" id="25" localization="8" tooltip="" x="319" y="92">
<bitmap>media/images/box/box-python-script.png</bitmap>
<script language="4">
<content>
<![CDATA[class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self)
def onLoad(self):
#put initialization code here
pass
def onUnload(self):
#put clean-up code here
import sys
def onInput_onStart(self):
#self.onStopped() #activate the output of the box
import sys, os, json, base64
import requests
headers = {'Content-Type': 'audio/wav'}
url = 'https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?model=en-US_BroadbandModel&inactivity_timeout=30&max_alternatives=1&word_confidence=false×tamps=false&profanity_filter=true&smart_formatting=false&speaker_labels=false'
with open("/home/nao/recordings/microphones/STT.wav", "rb") as audio_file:
r=requests.post(url, auth=('SPEECH_TO_TEXT_USERNAME', 'SPEECH_TO_TEXT_PASSWORD'), headers=headers, data=audio_file)
responses = r.json()
if responses:
self.log(str(r.json()['results'][0]['alternatives'][0]['transcript']))
self.onSpeech(str(r.json()['results'][0]['alternatives'][0]['transcript']))
def onInput_onStop(self):
self.onUnload() #it is recommended to reuse the clean-up as the box is stopped
self.onStopped() #activate the output of the box]]>
</content>
</script>
<Input name="onLoad" type="1" type_size="1" nature="0" inner="1" tooltip="Signal sent when diagram is loaded." id="1" />
<Input name="onStart" type="1" type_size="1" nature="2" inner="0" tooltip="Box behavior starts when a signal is received on this input." id="2" />
<Input name="onStop" type="1" type_size="1" nature="3" inner="0" tooltip="Box behavior stops when a signal is received on this input." id="3" />
<Output name="onStopped" type="1" type_size="1" nature="1" inner="0" tooltip="Signal sent when box behavior is finished." id="4" />
<Output name="onSpeech" type="3" type_size="1" nature="2" inner="0" tooltip="" id="5" />
</Box>
<Box name="PostToNode-RED" id="4" localization="8" tooltip="" x="447" y="109">
<bitmap>media/images/box/box-python-script.png</bitmap>
<script language="4">
<content>
<![CDATA[chat_context = {}
class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self)
def onLoad(self):
#put initialization code here
pass
def onUnload(self):
#put clean-up code here
import sys
#if self.WatsonImageFolderName and WatsonImageFolderName in sys.path:
# sys.path.remove(WatsonImageFolderName)
def onInput_onStart(self, p):
#self.onStopped() #activate the output of the box
global chat_context
import sys, os, json, base64
import requests
tts = ALProxy('ALTextToSpeech')
url = 'https://nodechatnew.mybluemix.net/with_array'
payload = {
"msgdata": p
}
payload['context']= chat_context
self.log(payload)
r=requests.post(url, data=payload)
response = r.json()
if response['botresponse']:
self.log(response['botresponse'])
self.log(response['botresponse']['messageout']['output']['text'])
chat_context = response['botresponse']['messageout']['context']
self.log(chat_context)
self.log(chat_context['system'])
tts.say(str(response['botresponse']['messageout']['output']['text'][0]))
def onInput_onStop(self):
self.onUnload() #it is recommended to reuse the clean-up as the box is stopped
self.onStopped() #activate the output of the box]]>
</content>
</script>
<Input name="onLoad" type="1" type_size="1" nature="0" inner="1" tooltip="Signal sent when diagram is loaded." id="1" />
<Input name="onStart" type="3" type_size="1" nature="2" inner="0" tooltip="Box behavior starts when a signal is received on this input." id="2" />
<Input name="onStop" type="1" type_size="1" nature="3" inner="0" tooltip="Box behavior stops when a signal is received on this input." id="3" />
<Output name="onStopped" type="1" type_size="1" nature="1" inner="0" tooltip="Signal sent when box behavior is finished." id="4" />
<Output name="onSpeech" type="3" type_size="1" nature="2" inner="0" tooltip="" id="5" />
</Box>
<Link inputowner="1" indexofinput="2" outputowner="0" indexofoutput="2" />
<Link inputowner="25" indexofinput="2" outputowner="3" indexofoutput="4" />
<Link inputowner="3" indexofinput="2" outputowner="1" indexofoutput="8" />
<Link inputowner="4" indexofinput="2" outputowner="25" indexofoutput="5" />
</Diagram>
</BehaviorKeyframe>
</BehaviorLayer>
</Timeline>
</Box>
</ChoregrapheProject>