forked from njacot/LeapMotionWithNao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaoMove.py
277 lines (210 loc) · 7.34 KB
/
naoMove.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
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
# -*- coding: utf-8 -*-
#################################################################
# Leap Motion with nao
# Bachelor thesis
# Descritpion: Class NaoMove
# Author: Niels Jacot
# Date: 12.07.2013
#################################################################
# local import
import constante
#import
import sys
from naoqi import ALProxy
# Some constante used in this class
ANGLE_MAX = 1.5
ANGLE_MIN = -1.5
MAXPOSX = 120
SHOULDERROLL = 0
SHOULDERPITCH = 1
ELBOWROLL = 2
WRISTYAW = 3
class NaoMove():
# Class used to make all movements to Nao
def __init__(self, parent):
#Constructor
self.motionProxy = None
self.postureProxy = None
self.tts = None
self.memoryProxy = None
self.behaviourProxy = None
self.motorOn = False
self.parent = parent
self.memValue = ["FaceDetected"]
self.headNames = ["HeadYaw", "HeadPitch"]
self.leftArmNames = ["LShoulderRoll","LShoulderPitch","LElbowRoll","LElbowYaw"]
self.rightArmNames = ["RShoulderRoll","RShoulderPitch","RElbowRoll","RElbowYaw"]
def connect(self, robotIP, port):
try:
self.motionProxy = ALProxy("ALMotion", robotIP , port)
self.tts = ALProxy("ALTextToSpeech", robotIP, port)
self.postureProxy = ALProxy("ALRobotPosture", robotIP, port)
self.memoryProxy = ALProxy("ALMemory", robotIP, port)
self.behaviourProxy = ALProxy("ALBehaviorManager", robotIP, port)
except:
return 1
# If connection is successful, we initialize few variables for Arms
self.anglesRight = self.motionProxy.getAngles(self.rightArmNames, True)
self.anglesLeft = self.motionProxy.getAngles(self.leftArmNames, True)
return 0
def walk(self,x,y,theta,frequency):
# Test if the stiffness of Nao is set to 1.0
self.testMotorOn()
# Launch the walk
self.motionProxy.setWalkTargetVelocity(x, y, theta, frequency)
def stopWalk(self, frequency):
self.motionProxy.setWalkTargetVelocity(0.0, 0.0, 0.0, frequency)
self.motionProxy.waitUntilMoveIsFinished()
def stop(self):
self.motionProxy.killMove()
def standInit(self):
self.postureProxy.goToPosture("StandInit", constante.FREQUENCY)
def crouch(self):
self.postureProxy.goToPosture("Crouch", constante.FREQUENCY)
def runHello(self):
self.testMotorOn()
self.behaviourProxy.runBehavior("helloBoy")
def openHand(self, hand):
if hand == constante.RIGHT_HAND:
self.motionProxy.openHand('RHand')
else:
self.motionProxy.openHand('LHand')
def closeHand(self, hand):
if hand == constante.RIGHT_HAND:
self.motionProxy.closeHand('RHand')
else:
self.motionProxy.closeHand('LHand')
def moveHead(self, angles):
# Test if the stiffness of Nao is set to 1.0
self.testMotorOn()
current = self.motionProxy.getAngles(self.headNames, True)
# this boucle is used to dynamically calculate the speed of the movement
for i in range(len(angles)):
speed = abs(angles[i]-current[i])/2.0
if speed < 0.05:
speed = 0.05
if speed > 0.8:
speed = 0.8
# Make the movement
self.motionProxy.setAngles(self.headNames[i], angles[i], speed)
def detectFace(self):
# Check that it is not already running.
if (not self.behaviourProxy.isBehaviorRunning("faceDetect")):
# Launch behavior. This is a blocking call, use post if you do not
# want to wait for the behavior to finish.
self.behaviourProxy.post.runBehavior("faceDetect")
def naoMoveTwoArms(self, listPos):
# Test if the stiffness of Nao is set to 1.0
self.testMotorOn()
if self.setCollisionEnabled("RArm") and self.setCollisionEnabled("LArm"):
current = self.motionProxy.getAngles(self.rightArmNames, True)
angles = self.anglesRight
current2 = self.motionProxy.getAngles(self.leftArmNames, True)
angles2 = self.anglesLeft
two = True
self.processMoveArm(angles2, current2, listPos[0], listPos[1], listPos[2], listPos[3], 0,two)
self.processMoveArm(angles, current, listPos[4], listPos[5], listPos[6], listPos[7], 1, two)
def moveArm(self, palmPosx, palmPosy, palmPosz, normal, handSide):
# Test if the stiffness of Nao is set to 1.0
self.testMotorOn()
two = False
if self.setCollisionEnabled("RArm") and self.setCollisionEnabled("LArm"):
if handSide == constante.RIGHT_HAND:
current = self.motionProxy.getAngles(self.rightArmNames, True)
angles = self.anglesRight
elif handSide == constante.LEFT_HAND:
current = self.motionProxy.getAngles(self.leftArmNames, True)
angles = self.anglesLeft
self.processMoveArm(angles, current, palmPosx, palmPosy, palmPosz, normal, handSide, two)
def processMoveArm(self, angles, current, palmPosx, palmPosy, palmPosz, normal, handSide, two):
# If there is two arms, the values of the box are not the same
if two:
maxPosLeftx = -100
minPosLeftx = -180
maxPosRightx = 180
minPosRightx = 100
else:
maxPosLeftx = MAXPOSX
minPosLeftx = -MAXPOSX
maxPosRightx = MAXPOSX
minPosRightx = -MAXPOSX
if handSide == constante.RIGHT_HAND:
#ElbowRoll
self.setAngleRight(angles, ELBOWROLL, 0.0, 1.5, 75, -40, palmPosz)
#ShoulderRoll
if palmPosx > maxPosRightx:
if angles[0] <= -1.5:
angles[0] = -1.5
angles[0] -= 0.1
elif palmPosx < minPosRightx:
if angles[0] >= 0.0:
angles[0] = 0.0
angles[0] += 0.1
else:
#handSide = left
#ShoulderRoll
if palmPosx > maxPosLeftx:
if angles[0] >= 1.5:
angles[0] = 1.5
angles[0] -= 0.1
elif palmPosx < minPosLeftx:
if angles[0] <= 0.0:
angles[0] = 0.0
angles[0] += 0.1
#ElbowRoll
self.setAngleLeft(angles, ELBOWROLL, -1.5, 0.0, 75, -40, palmPosz)
#ShoulderPitch
self.setAngleLeft(angles, SHOULDERPITCH, -1.5, 1.5, 350, 150, palmPosy)
if 0.78 > normal > -0.78:
angles[3] = -normal
for i in range(len(angles)):
speed = abs(angles[i]-current[i])/2.0
if speed < 0.05:
speed = 0.05
if speed > 0.5:
speed = 0.5
name = self.rightArmNames[i]
if handSide == constante.LEFT_HAND:
name = self.leftArmNames[i]
# Make the movement
self.motionProxy.setAngles(name, angles[i], speed)
def setAngleRight(self, angles, indice, valueMin, valueMax, posMax, posMin, palmPos):
if palmPos > posMax:
if angles[indice] >= valueMax:
angles[indice] = valueMax
angles[indice]+= 0.1
elif palmPos < posMin:
if angles[indice] <= valueMin:
angles[indice] = valueMin
angles[indice] -= 0.1
def setAngleLeft(self, angles, indice, valueMin, valueMax, posMax, posMin, palmPos):
if palmPos > posMax:
if angles[indice] <= valueMin:
angles[indice] = valueMin
angles[indice] -= 0.1
elif palmPos < posMin:
if angles[indice] >= valueMax:
angles[indice] = valueMax
angles[indice] += 0.1
def setCollisionEnabled(self, chainName):
#activate "Arms" anticollision
enable = True
isSuccess = self.motionProxy.setCollisionProtectionEnabled(chainName, enable)
return isSuccess
def turnMotorOn(self):
#Turn on the nao's motors
names = 'Body'
stiffnessLists = 1.0
timeLists = 1.0
self.motionProxy.stiffnessInterpolation(names, stiffnessLists, timeLists)
self.motorOn = True
def testMotorOn(self):
if not self.motorOn:
self.turnMotorOn()
def stiffnessOff(self):
names = 'Body'
stiffnessLists = 0.0
timeLists = 1.0
self.motionProxy.stiffnessInterpolation(names, stiffnessLists, timeLists)
self.motorOn = False
self.behaviourProxy.stopAllBehaviors()