-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControl.py
More file actions
408 lines (361 loc) · 14.4 KB
/
Control.py
File metadata and controls
408 lines (361 loc) · 14.4 KB
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
# Control.py
#
# viewODE humanoid robotic figure control definition module.
#
# Originally by Gary Deschaines, 2009.
#
# Disclaimer
#
# See the file DISCLAIMER-GaryDeschaines
from __future__ import print_function
from sys import exit, modules, stderr
#
# Import ODE module for joint and motor modeling.
try:
import ode
except ModuleNotFoundError or ImportError as ee:
print("Error: PyODE not installed properly !! - {0}".format(ee.msg), file=stderr)
exit(1)
#
# Import viewODE modules for humanoid figure joints, motors and vector math.
try:
from Joints import *
from Motors import *
from vecMath import *
except ModuleNotFoundError or ImportError as ee:
print("Error: {0}".format(ee.msg), file=stderr)
if not (modules["Joints"] and modules["Motors"] and modules["vecMath"]):
print("Error: viewODE not installed properly !!", file=stderr)
exit(1)
class Control:
def __init__(self, figure):
""" Constructor.
figure -- viewODE figure
Initialize the viewODE figure control.
"""
self.figure = figure
self.frame = figure.frame
self.debug = False
self.torquemode = 1
self.torqueaxis = 2
self.rotzeroerr = True
self.rotdamping = False
self.rotlimited = False
self.tfbdamping = False
self.maintainjr = False
self.direction = 1
self.angerr_tol = 1.0 * RPD
self.angvel_max = TWOPI
self.manip_joint = None
def debugOn(self):
# Only turn on if currently off.
if not self.debug:
self.debug = True
print("Control Debug ON")
def debugOff(self):
# Only turn off if currently on.
if self.debug:
self.debug = False
print("Control Debug OFF")
def toggleDebug(self):
self.debug = not self.debug
if self.debug:
print("Control Debug ON")
else:
print("Control Debug OFF")
def resetConfig(self):
self.torquemode = 1
self.torqueaxis = 2
self.rotzeroerr = True
self.rotdamping = False
self.rotlimited = False
self.tfbdamping = False
self.maintainjr = False
self.direction = 1
self.manip_joint = None
def printConfig(self):
if self.torquemode == 0:
print("Torque Mode OFF")
if self.rotdamping:
print("Rotational damping ON")
else:
print("Rotational damping OFF")
if self.torquemode == 1:
print("Torque Mode ON (Restoring and Forcing/Limiting)")
if self.rotzeroerr:
print("Zero error restoring ON")
else:
print("Zero error restoring OFF")
if self.rotdamping:
print("Rotational damping ON")
else:
print("Rotational damping OFF")
if self.rotdamping:
if self.tfbdamping:
print("Torque feedback rotational damping ON")
else:
print("Torque feedback rotational damping OFF")
print("Forcing Torque Axis ", self.torqueaxis,
" Direction ", ("+" if self.direction > 0 else "-"))
if self.rotlimited:
print("Non-Forcing Axis Rotation Error Limiting ON")
else:
print("Non-Forcing Axis Rotation Error Limiting OFF")
if self.maintainjr:
print("Maintain manipulated joint rotation ON")
else:
print("Maintain manipulated joint rotation OFF")
return
def setTorqueModeOn(self):
self.torquemode = 1
def setTorqueModeOff(self):
self.torquemode = 0
def setZeroErrorOn(self):
if not self.rotzeroerr:
self.rotzeroerr = True
print("Zero error restoring ON")
def setZeroErrorOff(self):
if self.rotzeroerr:
self.rotzeroerr = False
print("Zero error restoring OFF")
def setTorqueControl(self, key):
is_set = False
if key == '0':
is_set = True
self.torquemode = not self.torquemode
if self.torquemode:
print("Torque Mode ON")
else:
print("Torque Mode OFF")
elif key == '1':
is_set = True
self.torquemode = 1
self.torqueaxis = 0
print("Forcing Torque Axis 0")
elif key == '2':
is_set = True
self.torquemode = 1
self.torqueaxis = 1
print("Forcing Torque Axis 1")
elif key == '3':
is_set = True
self.torquemode = 1
self.torqueaxis = 2
print("Forcing Torque Axis 2")
elif key == '+':
is_set = True
self.direction = 1
print("Forcing Torque Direction +")
elif key == '-':
is_set = True
self.direction = -1
print("Forcing Torque Direction -")
elif key == 'd' or key == 'D':
# Toggle joint rotation damping
is_set = True
self.rotdamping = not self.rotdamping
if self.rotdamping:
print("Rotational Damping ON")
else:
print("Rotational Damping OFF")
elif key == 'e' or key == 'E':
# Toggle zero error restoring
is_set = True
self.rotzeroerr = not self.rotzeroerr
if self.rotzeroerr:
print("Zero error restoring ON")
else:
print("Zero error restoring OFF")
elif key == 'l' or key == 'L':
# Toggle rotation limited to torqued axis
is_set = True
self.rotlimited = not self.rotlimited
if self.rotlimited:
print("Non-Forcing Axis Rotation Error Limiting ON")
else:
print("Non-Forcing Axis Rotation Error Limiting OFF")
elif key == 'm' or key == 'M':
# Toggle maintain manipulated joint rotation
is_set = True
self.maintainjr = not self.maintainjr
if self.maintainjr:
print("Maintain manipulated joint rotation ON")
else:
print("Maintain manipulated joint rotation OFF")
elif key == 't' or key == 'T':
# Toggle torque feedback rotational damping
is_set = True
self.tfbdamping = not self.tfbdamping
if self.tfbdamping:
print("Torque feedback rotational damping ON")
self.rotdamping = True
print("Rotational damping ON")
self.frame.setFeedbackOn()
print("Feedback ON")
else:
print("Torque feedback rotational damping OFF")
return is_set
def printFeedback(self, name, t, p, fb):
F1 = vecMag(fb[0])
T1 = vecMag(fb[1])
F2 = vecMag(fb[2])
T2 = vecMag(fb[3])
print("Feedback: %s, t=%8.4f, p=%1d" % (name, t, p))
print(" F1: %8.3f (%8.3f | %8.3f | %8.3f)" %
(F1, fb[0][0], fb[0][1], fb[0][2]))
print(" T1: %8.3f (%8.3f | %8.3f | %8.3f)" %
(T1, fb[1][0], fb[1][1], fb[1][2]))
print(" F2: %8.3f (%8.3f | %8.3f | %8.3f)" %
(F2, fb[2][0], fb[2][1], fb[2][2]))
print(" T2: %8.3f (%8.3f | %8.3f | %8.3f)" %
(T2, fb[3][0], fb[3][1], fb[3][2]))
print("%s : t=%8.4f, p=%1d, F1=%10.3f, T1=%10.3f" %
(name, t, p, F1, T1))
print("%s : t=%8.4f, p=%1d, F2=%10.3f, T2=%10.3f" %
(name, t, p, F2, T2))
def setManipJoint(self, joint):
self.manip_joint = joint
def applyJointDamping(self, t, tstep):
if not self.frame:
return
tfb = self.tfbdamping
show = self.debug
for j in self.frame.joints:
name = j.label
pick = j.getBody(1).solid.wire
fb = None
if show:
fb = j.getFeedback()
if fb:
self.printFeedback(name, t, pick, fb)
if isinstance(j, ode.HingeJoint):
if show:
ang = j.getAngle()
ar = j.getAngleRate()
print("%s : t=%8.4f, p=%1d, ang=%6.3f, ar=%8.3f" %
(name, t, pick, ang, ar))
if self.rotdamping:
applyHingeJointDamping(show, t, pick, j, tfb, fb)
else:
j.setParam(ode.ParamFMax, 0.0)
elif isinstance(j, ode.UniversalJoint):
if show:
ang1 = j.getAngle1()
ang2 = j.getAngle2()
ar1 = j.getAngle1Rate()
ar2 = j.getAngle2Rate()
print("%s : t=%8.4f, p=%1d, ang1=%6.3f, ang2=%6.3f, ar1=%8.3f, ar2=%8.3f" %
(name, t, pick, ang1, ang2, ar1, ar2))
if self.rotdamping:
applyUniversalJointDamping(show, t, pick, j, tfb, fb)
else:
j.setParam(ode.ParamFMax, 0.0)
j.setParam(ode.ParamFMax2, 0.0)
elif isinstance(j, ode.BallJoint):
if show:
motor = j.getBody(1).solid.motor
if motor:
ang0 = motor.getAngle(0)
ang1 = motor.getAngle(1)
ang2 = motor.getAngle(2)
print("%s : t=%8.4f, p=%1d, ang0=%6.3f, ang1=%6.3f, ang2=%6.3f" %
(name, t, pick, ang0, ang1, ang2))
(ar0, ar1, ar2) = getBallJointAngularRates(j)
print("%s : t=%8.4f, p=%1d, ar0=%8.3f, ar1=%8.3f, ar2=%8.3f" %
(name, t, pick, ar0, ar1, ar2))
if self.rotdamping:
applyBallJointDamping(show, t, pick, j, tfb, fb)
else:
j.setParam(ode.ParamFMax, 0.0)
j.setParam(ode.ParamFMax2, 0.0)
j.setParam(ode.ParamFMax3, 0.0)
def applyRestoringTorque(self, t, tstep, m):
name = m.label
pick = m.getBody(1).solid.wire
show = self.debug
TOL = self.angerr_tol
RATE = self.angvel_max
for k in range(m.getNumAxes()):
loStop = m.getParam(AMotorAxisParams[k]['LoStop'])
hiStop = m.getParam(AMotorAxisParams[k]['HiStop'])
if hiStop > loStop:
# This motor axis can rotate
error = m.getAngle(k)
if abs(error) > TOL:
rotateAxisToZero(m, k, TOL, RATE)
if show:
print("%s : t=%8.4f, p=%1d, error=%6.2f - axis %d restoring" %
(name, t, pick, error, k))
else:
removeTorqueFromAxis(m, k)
def applyForcingTorque(self, t, tstep, m):
name = m.label
pick = m.getBody(1).solid.wire
show = self.debug
TOL = self.angerr_tol
RATE = 0.5 * self.angvel_max
for k in range(m.getNumAxes()):
loStop = m.getParam(AMotorAxisParams[k]['LoStop'])
hiStop = m.getParam(AMotorAxisParams[k]['HiStop'])
if hiStop > loStop:
# This motor axis can rotate
if k == self.torqueaxis:
angle = m.getAngle(k)
if (angle > (loStop - self.angerr_tol)) and \
(angle < (hiStop + self.angerr_tol)):
rotateAxisAtRate(m, k, self.direction * RATE)
if show:
print("%s : t=%8.4f, p=%1d, angle=%6.2f - axis %d forcing" %
(name, t, pick, angle, k))
else:
removeTorqueFromAxis(m, k)
elif self.rotlimited:
error = m.getAngle(k)
if abs(error) > TOL:
rotateAxisToZero(m, k, TOL, RATE)
if show:
print("%s : t=%8.4f, p=%1d, error=%6.2f - axis %d limiting" %
(name, t, pick, error, k))
else:
removeTorqueFromAxis(m, k)
def applyMotorTorques(self, t, tstep):
if not self.frame:
return
show = self.debug
for m in self.frame.motors:
name = m.label
pick = m.getBody(1).solid.wire
fb = None
if show:
fb = m.getFeedback()
if fb:
self.printFeedback(name, t, pick, fb)
if isinstance(m, ode.AMotor):
if show:
ang0 = m.getAngle(0)
ang1 = m.getAngle(1)
ang2 = m.getAngle(2)
print("%s : t=%8.4f, p=%1d, ang0=%6.3f, ang1=%6.3f, ang2=%6.3f" %
(name, t, pick, ang0, ang1, ang2))
if self.torquemode == 0:
# Remove all motor restoring torques
if m.getMode() == ode.AMotorEuler:
removeTorqueFromAxis(m, m.getNumAxes())
if self.torquemode == 1:
# Apply forcing, limiting or restoring torques
if m.getMode() == ode.AMotorEuler:
if m.getBody(1).solid.wire:
# Joint selected - Apply forcing/limiting torques
self.applyForcingTorque(t, tstep, m)
elif self.frame.figure.currStateJointManip():
# Manipulating joints, but joint not currently selected
if not self.maintainjr:
# Not maintaining manipulated rotation - Remove all
# forcing/limiting torques
removeTorqueFromAxis(m, m.getNumAxes())
if self.rotzeroerr:
# Joint not selected - Apply zero error restoring torques
self.applyRestoringTorque(t, tstep, m)
elif self.rotzeroerr:
# Joint not selected - Apply zero error restoring torques
self.applyRestoringTorque(t, tstep, m)