-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMotors.py
More file actions
324 lines (260 loc) · 10.5 KB
/
Motors.py
File metadata and controls
324 lines (260 loc) · 10.5 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
# Motors.py
#
# viewODE figure frame motors definition module.
#
# Originally by Gary Deschaines, 2009.
#
# Disclaimer
#
# See the file DISCLAIMER-GaryDeschaines
from __future__ import print_function
from sys import exit, modules, stderr
from math import cos, sin
#
# Import ODE module for joint and motor models.
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 module for vector math.
try:
from vecMath import *
except ModuleNotFoundError or ImportError as ee:
print("Error: {0}".format(ee.msg), file=stderr)
if not modules["vecMath"]:
print("Error: viewODE not installed properly !!", file=stderr)
exit(1)
AMotorAxisParams = ({'LoStop': ode.ParamLoStop,
'HiStop': ode.ParamHiStop,
'Vel': ode.ParamVel,
'FMax': ode.ParamFMax},
{'LoStop': ode.ParamLoStop2,
'HiStop': ode.ParamHiStop2,
'Vel': ode.ParamVel2,
'FMax': ode.ParamFMax2},
{'LoStop': ode.ParamLoStop3,
'HiStop': ode.ParamHiStop3,
'Vel': ode.ParamVel3,
'FMax': ode.ParamFMax3})
def axesFromSpecs(specs, side, num):
axes = []
for i in range(num):
axes.append(specs['Axes'][str.join('-', (side, str(i)))])
return (axes)
def motorLabel(motor):
"""
Constructs and returns the label string assembled from the
labels for the given motor's joint body solids, joint type
and motor type.
@param motor: A viewODE figure frame motor.
@type motor: ODE motor object
@return: motor label
@rtype: string
"""
b1name = motor.joint.getBody(0).solid.label
b2name = motor.joint.getBody(1).solid.label
jtype = motor.joint.type
mtype = motor.type
label = b1name + "-" + str.join('', (b2name, jtype, mtype))
return label
def attachBallAMotor(frame, joint, specs):
"""
Instantiates an ODE AMotor object to attach between the given Ball
joint's joined bodies as defined by the given figure joint specs,
appends the motor to the given frame motors list, and links the
motor to the given joint's 2nd body solid.
@param frame: A viewODE figure frame for the instantiated motor.
@type frame: viewODE Frame object
@param joint: A viewODE figure frame joint to be driven by the instantiated motor.
@type joint: ODE BallJoint object
@param specs: Figure joint and motor specs for the given ball joint.
@type specs: viewODE Figure class JointSpecs dictionary entry for the given joint
@return: The instantiated motor attached to the given ball joint.
@rtype: ODE AMotor object
"""
if joint.type != "Ball":
print("attachBallAmotor: Joint type %s not Ball!" % joint.type)
exit(1)
Axes = axesFromSpecs(joint.specs, joint.side, 3)
LoStop = joint.specs['LoStop']
HiStop = joint.specs['HiStop']
motor = ode.AMotor(frame.world, frame.motorgroup)
motor.attach(joint.getBody(0), joint.getBody(1))
motor.setMode(ode.AMotorEuler)
motor.setAxis(0, Axes[0]['mode'], Axes[0]['axis'])
# motor.setAxis(1, Axes[1]['mode'], Axes[1]['axis'])
motor.setAxis(1, 0, Axes[1]['axis'])
motor.setAxis(2, Axes[2]['mode'], Axes[2]['axis'])
motor.setFeedback(frame.feedback)
motor.setParam(ode.ParamLoStop, LoStop[0])
motor.setParam(ode.ParamHiStop, HiStop[0])
motor.setParam(ode.ParamLoStop2, LoStop[1])
motor.setParam(ode.ParamHiStop2, HiStop[1])
motor.setParam(ode.ParamLoStop3, LoStop[2])
motor.setParam(ode.ParamHiStop3, HiStop[2])
motor.type = "AMotor"
motor.joint = joint
motor.label = motorLabel(motor)
frame.motors.append(motor)
joint.motor = motor
joint.getBody(1).solid.motor = motor
return motor
def attachUniversalAMotor(frame, joint, specs):
"""
Instantiates an ODE AMotor object to attach between the given
Universal joint's joined bodies as defined by the given figure
joint specs, appends the motor to the given frame motors list,
and links the motor to the given joint's 2nd body solid.
@param frame: A viewODE figure frame for the instantiated motor.
@type frame: viewODE Frame object
@param joint: A viewODE figure frame joint to be driven by the instantiated motor.
@type joint: ODE UniversalJoint object
@param specs: Figure joint and motor specs for the given Universal joint.
@type specs: viewODE Figure class JointSpecs dictionary entry for the given joint
@return: The instantiated motor attached to the given Universal joint.
@rtype: ODE AMotor object
"""
if joint.type != "Universal":
print("attachUniversalAmotor: Joint type %s not Universal!" % joint.type)
exit(1)
Axes = axesFromSpecs(joint.specs, joint.side, 3)
LoStop = joint.specs['LoStop']
HiStop = joint.specs['HiStop']
motor = ode.AMotor(frame.world, frame.motorgroup)
motor.attach(joint.getBody(0), joint.getBody(1))
motor.setMode(ode.AMotorEuler)
motor.setAxis(0, Axes[0]['mode'], Axes[0]['axis'])
# motor.setAxis(1, Axes[1]['mode'], Axes[1]['axis'])
motor.setAxis(1, 0, Axes[1]['axis'])
motor.setAxis(2, Axes[2]['mode'], Axes[2]['axis'])
motor.setFeedback(frame.feedback)
motor.setParam(ode.ParamLoStop, LoStop[0])
motor.setParam(ode.ParamHiStop, HiStop[0])
motor.setParam(ode.ParamLoStop2, 0.0)
motor.setParam(ode.ParamHiStop2, 0.0)
motor.setParam(ode.ParamLoStop3, LoStop[2])
motor.setParam(ode.ParamHiStop3, HiStop[2])
motor.type = "AMotor"
motor.joint = joint
motor.label = motorLabel(motor)
frame.motors.append(motor)
joint.motor = motor
joint.getBody(1).solid.motor = motor
return motor
def attachHingeAMotor(frame, joint, specs):
"""
Instantiates an ODE AMotor object to attach between the given Hinge
joint's joined bodies as defined by the given figure joint specs,
appends the motor to the given frame motors list, and links the
motor to the given joint's 2nd body solid.
@param frame: A viewODE figure frame for the instantiated motor.
@type frame: viewODE Frame object
@param joint: A viewODE figure frame joint to be driven by the instantiated motor.
@type joint: ODE HingeJoint object
@param specs: Figure joint and motor specs for the given hinge joint.
@type specs: viewODE Figure class JointSpecs dictionary entry for the given joint
@return: The instantiated motor attached to the given hinge joint.
@rtype: ODE AMotor object
"""
if joint.type != "Hinge":
print("attachHingeAmotor: Joint type %s not Hinge!" % joint.type)
exit(1)
Axes = axesFromSpecs(joint.specs, joint.side, 3)
LoStop = joint.specs['LoStop']
HiStop = joint.specs['HiStop']
motor = ode.AMotor(frame.world, frame.motorgroup)
motor.attach(joint.getBody(0), joint.getBody(1))
motor.setMode(ode.AMotorEuler)
motor.setAxis(0, Axes[0]['mode'], Axes[0]['axis'])
# motor.setAxis(1, Axes[1]['mode'], Axes[1]['axis'])
motor.setAxis(1, 0, Axes[1]['axis'])
motor.setAxis(2, Axes[2]['mode'], Axes[2]['axis'])
motor.setFeedback(frame.feedback)
motor.setParam(ode.ParamLoStop, LoStop[0])
motor.setParam(ode.ParamHiStop, HiStop[0])
motor.setParam(ode.ParamLoStop2, 0.0)
motor.setParam(ode.ParamHiStop2, 0.0)
motor.setParam(ode.ParamLoStop3, 0.0)
motor.setParam(ode.ParamHiStop3, 0.0)
motor.type = "AMotor"
motor.joint = joint
motor.label = motorLabel(motor)
frame.motors.append(motor)
joint.motor = motor
joint.getBody(1).solid.motor = motor
return motor
def getEulerAngRatesFromAngVelVec(motor, Wvec):
""" Assumes ang1 never goes to +/- pi/2.0
"""
w0 = Wvec[0]
w1 = Wvec[1]
w2 = Wvec[2]
ang0 = motor.getAngle(0)
ang1 = motor.getAngle(1)
ang2 = motor.getAngle(2)
ar0 = (w0 * cos(ang2) - w1 * sin(ang2)) / cos(ang1)
ar1 = w0 * sin(ang2) + w1 * cos(ang2)
ar2 = w2 - ar0 * sin(ang1)
return (ar0, ar1, ar2)
def getAngVelVecFromEulerAngRates(motor, Rates):
""" Assumes ang1 never goes to +/- pi/2.0
"""
ar0 = Rates[0]
ar1 = Rates[1]
ar2 = Rates[2]
ang0 = motor.getAngle(0)
ang1 = motor.getAngle(1)
ang2 = motor.getAngle(2)
w0 = ar1 * sin(ang2) + ar0 * cos(ang2) * cos(ang1)
w1 = ar1 * cos(ang2) - ar0 * sin(ang2) * cos(ang1)
w2 = ar2 + ar0 * sin(ang1)
return (w0, w1, w2)
def removeTorqueFromAxis(motor, axis):
naxis = motor.getNumAxes()
if axis < 0:
return
if axis > naxis:
return
if axis == naxis:
for k in range(naxis):
motor.setParam(AMotorAxisParams[k]['Vel'], 0.0)
motor.setParam(AMotorAxisParams[k]['FMax'], 0.0)
else:
motor.setParam(AMotorAxisParams[axis]['Vel'], 0.0)
motor.setParam(AMotorAxisParams[axis]['FMax'], 0.0)
def rotateAxisAtRate(motor, axis, rate):
ang = motor.getAngle(axis)
if rate > 0.0:
angHi = motor.getParam(AMotorAxisParams[axis]['HiStop'])
if ang < angHi:
FMax = motor.joint.specs['AMFMax'][axis]
motor.setParam(AMotorAxisParams[axis]['Vel'], rate)
motor.setParam(AMotorAxisParams[axis]['FMax'], FMax)
if rate < 0.0:
angLo = motor.getParam(AMotorAxisParams[axis]['LoStop'])
if ang > angLo:
FMax = motor.joint.specs['AMFMax'][axis]
motor.setParam(AMotorAxisParams[axis]['Vel'], rate)
motor.setParam(AMotorAxisParams[axis]['FMax'], FMax)
def rotateAxisToZero(motor, axis, tol, rate):
err = motor.getAngle(axis)
if abs(err) > tol:
FMax = motor.joint.specs['AMFMax'][axis]
motor.setParam(AMotorAxisParams[axis]['Vel'], -err * rate)
motor.setParam(AMotorAxisParams[axis]['FMax'], FMax)
def rotateAxisToAngle(motor, axis, ang, tol, rate):
ang = max(ang, motor.getParam(AMotorAxisParams[axis]['LoStop']))
ang = min(ang, motor.getParam(AMotorAxisParams[axis]['HiStop']))
err = motor.getAngle(axis) - ang
if abs(err) > tol:
FMax = motor.joint.specs['AMFMax'][axis]
motor.setParam(AMotorAxisParams[axis]['Vel'], -err * rate)
motor.setParam(AMotorAxisParams[axis]['FMax'], FMax)
def rotateAxisToStop(motor, axis, stop, tol, rate):
err = motor.getAngle(axis) - motor.joint.specs[stop][axis]
if abs(err) > tol:
FMax = motor.joint.specs['AMFMax'][axis]
motor.setParam(AMotorAxisParams[axis]['Vel'], -err * rate)
motor.setParam(AMotorAxisParams[axis]['FMax'], FMax)