-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhello_world.py
124 lines (105 loc) · 4.37 KB
/
hello_world.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'''
This file is a general Hello World script for the NAO6 Robot.
It gets NAO to stand from a sitting position, wave and greet, and sit back down.
To run, cd into the directory with the pynaoqi folder and this Python script (both of which should also be in the same directory).
Simply run this command in your terminal:
$ python hello_world.py
'''
import random
import time
import naoqi
from naoqi import ALProxy
'''
The following are useful imports for general NAO programming, but not needed for this script.
from naoqi import ALBroker
from naoqi import ALModule
from naoqi import ALBehavior
'''
class HelloWorld:
def __init__(self, host, port):
# Basic information about the NAO's connection that must be set when the program is run
# If the stiffness is not set to a non-zero number, the robot will not move!
self.host = host
self.port = port
self.stiffness = 1.0
# The following are variables to hold the proxies
self.speechDevice = None
self.motion = None
self.posture = None
self.connectNao()
def connectNao(self):
# Connect to a motion proxy to allow the robot to move
try:
self.motion = ALProxy("ALMotion", self.host, self.port)
self.motion.setEnableNotifications(False)
except Exception, e:
print "Error when creating motion device proxy:" + str(e)
exit(1)
# Make NAO stiff, or it won't move
self.motion.stiffnessInterpolation("Body", self.stiffness, 1.0)
# Connect to a speech proxy
try:
self.speechDevice = ALProxy("ALTextToSpeech", self.host, self.port)
except Exception, e:
print "Error when creating speech device proxy:" + str(e)
exit(1)
# Control the robot's posture
# NAO has a host of pre-programmed postures like "Stand" and "Sit"
try:
self.posture = ALProxy("ALRobotPosture", self.host, self.port)
except Exception, e:
print "Error when creating robot posture proxy:" + str(e)
exit(1)
# Makes NAO wave
def wave(self):
# Gets the right hand into position to wave
self.motion.setAngles("RShoulderPitch", -1.0, 0.15)
self.motion.setAngles("RShoulderRoll", -1.2, 0.15)
self.motion.setAngles("RElbowRoll", 1.0, 0.1)
self.motion.setAngles("RElbowYaw", 0.5, 0.1)
self.motion.setAngles("RWristYaw", 0, 0.1)
self.motion.openHand("RHand")
time.sleep(0.7)
# wave the hand 3 times, by moving the elbow
for i in range(3):
self.motion.setAngles("RElbowRoll", 1.5, 0.5)
time.sleep(0.5)
self.motion.setAngles("RElbowRoll", 0.5, 0.5)
time.sleep(0.5)
# Stops the wave and closes the hand
self.motion.setAngles("RElbowRoll", 1.0, 0.5)
time.sleep(1)
self.motion.closeHand("RHand")
# In this script, there is no need to program the hand to be lowered, as it does automatically when NAO sits down.
# self.prepare_sit_right(0.15)
# time.sleep(4)
# self.bring_to_sit(1)
# Allows speech in simultaneity with movement
# For more information on how the post() method works,
# visit https://developer.softbankrobotics.com/nao6/naoqi-developer-guide/other-tutorials/python-sdk-tutorials/parallel-tasks-making-nao-move-and
def genSpeech(self, sentence):
try:
id = self.speechDevice.post.say("\\vol=100\\"+sentence)
return id
except Exception, e:
print "Error when saying a sentence: " + str(e)
# Run the sequence of movements on NAO
def run(self):
scripts = [
"I see where you're coming from",
"I share aspects of your viewpoint",
"I concur with your idea",
"I support your stance",
"I agree with you",
"I am fully aligned with your perspective",
"I am in full agreement with you",
"I agree wholeheartedly and without reservation"]
#self.posture.goToPosture("Stand", 0.3)
self.genSpeech(scripts[0])
#self.wave()
#self.speechDevice.wait(id, 0)
self.posture.goToPosture("Sit", 0.3)
# IMPORTANT: Set the port and host when initializing!
if __name__ == "__main__":
helloWorld = HelloWorld("192.168.1.124", 9559)
helloWorld.run()