-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcFunctions.py
289 lines (218 loc) · 9.89 KB
/
cFunctions.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
278
279
280
281
282
283
284
285
286
287
288
from random import random
import cProxies as pseudoProxies
#from naoqi import ALProxy as naoProxies
from config import *
class ConsolePlatform():
def __init__(self, names, speech=True, initialPosition=None, speaker=False):
self.TOL = CONSOLE_TOLERANCES
self.openMotionProxy(initialPosition)
if speech:
self.openSpeechProxy(speaker)
else:
self.speech = None
self.names = names
self.updateAngles()
def openMotionProxy(self, initalPosition=None):
""" Creates a console motion proxy simulator. Returns the proxy object """
self.motion = pseudoProxies.MotionProxySimulator(initalPosition)
def openSpeechProxy(self, speaker=False):
""" Creates a console speech proxy simulator Returns the proxy object """
self.speech = pseudoProxies.SpeechProxySimulator(speaker)
def say(self, message):
if self.speech is None:
print "Speech:", message
else:
self.speech.say(message)
def updateAngles(self, fromSensors=True):
""" Reads the angles of specified joints from sensors.
Two readings are stored in the object:
angles: dictionary with pairs {name:value}
x: list with values of the readings preserving
the ordering in names list.
"""
values = self.motion.getAngles(self.names, fromSensors)
angles = {}
for i in range(len(self.names)):
angles[self.names[i]] = values[i]
self.angles = angles
self.x = values
def moveAngles(self, deltas, fractionMaxSpeed=None):
""" Moves specified joints by a delta.
Inputs:
motion: already opened motion proxy
names: string list with the desired joint
deltas: list with the increment for each joint
Optionally, fractionMaxSpeed determines the maximum speed Nao
can use to perform the change as a fraction of the whole movement.
Can be fine tuned to achieve softer and more natural movements.
"""
self.motion.changeAngles(self.names, deltas)
def goToAngles(self, positions, fractionMaxSpeed = None):
""" Makes Nao to change its joints to specific positions.
Inputs:
motion: already opened motion proxy
names: string list with the desired joints
positions: list with the final positions for each joint
Optionally, fractionMaxSpeed determines the maximum speed Nao
can use to perform the change as a fraction of the whole movement.
"""
self.motion.setAngles(self.names, positions)
self.updateAngles()
def getRandomPosition(self, minI=None, maxI=None):
""" Choose a random position for a set of joints.
Inputs:
min/maxI: optionally, user defined intervals can be selected as
dictionaries with pairs (jointName: max/min limit)
If not specified, mechanical limits of the robot will be
used instead
Output:
x: a list of integers with the positions of all the joints in
the names list preserving ordering
"""
if minI==None or maxI==None:
maximum = [self.motion.getLimits(name)[0][1] for name in self.names]
minimum = [self.motion.getLimits(name)[0][0] for name in self.names]
else:
maximum = [maxI[name] for name in self.names]
minimum = [minI[name] for name in self.names]
x = [(maximum[i]-minimum[i])*random() + minimum[i] for i in range(len(self.names))]
return x
def getSubset(self, namesSubSet=None):
""" Transforms an angles dictionary in a possibly reduced ordered
list of joint position values x
Input:
namesSubSet: A reduced list of strings with the desired
joints. If nothing specified, original
names variable of the object will be used.
"""
if namesSubSet is None:
names = self.names
else:
names = namesSubSet
x = [self.angles[name] for name in names]
return x
def equilibriumReached(self, speeds, tol=1):
""" Checks if the motion is in an equilibrium position. For that, the
function checks if all speeds are similar to zero using the tolerances
provided in the config file
Inputs:
platform: platform object from pyASVMplatforms library
speeds: list of speed values for each of the joints
"""
localEq = True
for i in range(len(self.names)):
if speeds[i] > (tol * self.TOL[self.names[i]]):
localEq = False
return localEq
class NaoPlatform():
def __init__(self):
self.TOL = NAO_TOLERANCES
self.openMotionProxy(initialPosition)
if speech:
self.openSpeechProxy(speaker)
else:
self.speech = None
self.names = names
self.updateAngles()
def openMotionProxy(self, nao=NAO_HOST, port=NAO_PORT):
""" Opens a motion proxy on a Nao host """
try:
self.motion = naoProxies.ALProxy("ALMotion", nao, port)
except Exception, e:
print "Could not create proxy to ALMotion"
print "Error was: ", e
sys.exit(1)
def openSpeechProxy(self, nao=NAO_HOST, port=NAO_PORT):
""" Opens a speech proxy on a Nao host """
try:
self.speech = ALProxy("ALTextToSpeech", nao, port)
except Exception,e:
print "Could not create proxy to ALTextToSpeech"
print "Error was: ",e
sys.exit(1)
def say(self, message):
if self.speech is None:
print "Speech:", message
else:
self.speech.say(message)
def updateAngles(self, fromSensors=True):
""" Reads the angles of specified joints from sensors.
Two readings are stored in the object:
angles: dictionary with pairs {name:value}
x: list with values of the readings preserving
the ordering in names list.
"""
values = self.motion.getAngles(self.names, fromSensors)
angles = {}
for i in range(len(self.names)):
angles[self.names[i]] = values[i]
self.angles = angles
self.x = values
def moveAngles(self, deltas, fractionMaxSpeed=None):
""" Moves specified joints by a delta.
Inputs:
motion: already opened motion proxy
names: string list with the desired joint
deltas: list with the increment for each joint
Optionally, fractionMaxSpeed determines the maximum speed Nao
can use to perform the change as a fraction of the whole movement.
Can be fine tuned to achieve softer and more natural movements.
"""
self.motion.changeAngles(self.names, deltas)
def goToAngles(self, positions, fractionMaxSpeed = None):
""" Makes Nao to change its joints to specific positions.
Inputs:
motion: already opened motion proxy
names: string list with the desired joints
positions: list with the final positions for each joint
Optionally, fractionMaxSpeed determines the maximum speed Nao
can use to perform the change as a fraction of the whole movement.
"""
self.motion.setAngles(self.names, positions)
self.updateAngles()
def getRandomPosition(self, minI=None, maxI=None):
""" Choose a random position for a set of joints.
Inputs:
min/maxI: optionally, user defined intervals can be selected as
dictionaries with pairs (jointName: max/min limit)
If not specified, mechanical limits of the robot will be
used instead
Output:
x: a list of integers with the positions of all the joints in
the names list preserving ordering
"""
if minI==None or maxI==None:
maximum = [self.motion.getLimits(name)[0][1] for name in self.names]
minimum = [self.motion.getLimits(name)[0][0] for name in self.names]
else:
maximum = [maxI[name] for name in self.names]
minimum = [minI[name] for name in self.names]
x = [(maximum[i]-minimum[i])*random() + minimum[i] for i in range(len(self.names))]
return x
def getSubset(self, namesSubSet=None):
""" Transforms an angles dictionary in a possibly reduced ordered
list of joint position values x
Input:
namesSubSet: A reduced list of strings with the desired
joints. If nothing specified, original
names variable of the object will be used.
"""
if namesSubSet is None:
names = self.names
else:
names = namesSubSet
x = [self.angles[name] for name in names]
return x
def equilibriumReached(self, speeds, tol=1):
""" Checks if the motion is in an equilibrium position. For that, the
function checks if all speeds are similar to zero using the tolerances
provided in the config file
Inputs:
platform: platform object from pyASVMplatforms library
speeds: list of speed values for each of the joints
"""
localEq = True
for i in range(len(self.names)):
if speeds[i] > (tol * self.TOL[self.names[i]]):
localEq = False
return localEq