-
Notifications
You must be signed in to change notification settings - Fork 0
/
original.py
104 lines (82 loc) · 2.58 KB
/
original.py
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
# -*- encoding: UTF-8 -*-
""" Say `My {Body_part} is touched` when receiving a touch event
"""
import sys
import time
from naoqi import ALProxy
from naoqi import ALBroker
from naoqi import ALModule
import argparse
# Global variable to store the ReactToTouch module instance
ReactToTouch = None
memory = None
class ReactToTouch(ALModule):
""" A simple module able to react
to touch events.
"""
def __init__(self, name):
ALModule.__init__(self, name)
# No need for IP and port here because
# we have our Python broker connected to NAOqi broker
# Create a proxy to ALTextToSpeech for later use
self.tts = ALProxy("ALTextToSpeech")
# Subscribe to TouchChanged event:
global memory
memory = ALProxy("ALMemory")
memory.subscribeToEvent("TouchChanged",
"ReactToTouch",
"onTouched")
def onTouched(self, strVarName, value):
""" This will be called each time a touch
is detected.
"""
# Unsubscribe to the event when talking,
# to avoid repetitions
memory.unsubscribeToEvent("TouchChanged",
"ReactToTouch")
touched_bodies = []
for p in value:
if p[1]:
touched_bodies.append(p[0])
self.say(touched_bodies)
# Subscribe again to the event
memory.subscribeToEvent("TouchChanged",
"ReactToTouch",
"onTouched")
def say(self, bodies):
if (bodies == []):
return
sentence = "My " + bodies[0]
for b in bodies[1:]:
sentence = sentence + " and my " + b
if (len(bodies) > 1):
sentence = sentence + " are"
else:
sentence = sentence + " is"
sentence = sentence + " touched."
self.tts.say(sentence)
def main(ip, port):
""" Main entry point
"""
# We need this broker to be able to construct
# NAOqi modules and subscribe to other modules
# The broker must stay alive until the program exists
myBroker = ALBroker("myBroker",
"0.0.0.0", # listen to anyone
0, # find a free port and use it
ip, # parent broker IP
port) # parent broker port
global ReactToTouch
ReactToTouch = ReactToTouch("ReactToTouch")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print
print "Interrupted by user, shutting down"
myBroker.shutdown()
sys.exit(0)
if __name__ == "__main__":
ip = "nao.local"
port = 9559
main(ip, port)