-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.py
340 lines (277 loc) · 10.9 KB
/
simulation.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
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
"""Shows a 3D simulation of a leg prosthesis
"""
import copy
import math
import OpenGL.GL as gl
import OpenGL.GLU as glu
from numpy.lib import math
import pygame
from pyquaternion import Quaternion
from sensors import SensorData, quat_to_euler
def quat_to_axis_rotation(*args):
"""Converts quaternion to euler angles
"""
if len(args) == 4 and all(map(lambda x: isinstance(x, float), args)):
Quaternion(args).unit
elif len(args) == 1 and isinstance(args[0], Quaternion):
quat = args[0].unit
else:
raise TypeError(
"Use either 4 floats (w, x, y, z) or one Quaternion object.")
# 2 * math.acos(quat.w)
angle = math.atan2(math.sqrt(sum(i**2 for i in quat.vector)), quat.w)
# assuming quaternion normalised then w is less than 1, so term always positive.
s = math.sqrt(1-quat.w * quat.w)
if s < 0.001: # test to avoid divide by zero, s is always positive due to sqrt
# if s close to zero then direction of axis not important
x = quat.x # if it is important that axis is normalised then replace with x=1; y=z=0;
y = quat.y
z = quat.z
else:
x = quat.x / s # normalise axis
y = quat.y / s
z = quat.z / s
# theta = 2 * \
# math.atan2(math.sqrt(sum(i**2 for i in quat.vector)), quat.w)
# x, y, z = (n / math.sin(theta / 2) for n in quat.vector) \
# if theta != 0 else (0, 0, 0)
return (math.degrees(angle), z, x, -y)
class Simulation():
"""Shows a 3D simulation of a leg prosthesis
"""
sensor_data = SensorData()
offset = SensorData()
pose = 0
__num_poses = 2
flex_bent = 54000.0
flex_straight = 123000.0
def translate_range(self, value, leftMin, leftMax, rightMin, rightMax):
"""Translates one range to another"""
# Figure out how 'wide' each range is
leftSpan = leftMax - leftMin
rightSpan = rightMax - rightMin
# Convert the left range into a 0-1 range (float)
valueScaled = float(value - leftMin) / float(leftSpan)
# Convert the 0-1 range into a value in the right range.
return rightMin + (valueScaled * rightSpan)
def resize(self, width, height):
if height == 0:
height = 1
gl.glViewport(0, 0, width, height)
gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity()
glu.gluPerspective(45, 1.0*width/height, 0.1, 100.0)
gl.glMatrixMode(gl.GL_MODELVIEW)
gl.glLoadIdentity()
def __init__(self, width: int, height: int):
"""
Arguments:
width {int} -- Window width in pixels
height {int} -- Window height in pixels
"""
self.resize(width, height)
self.quad = glu.gluNewQuadric()
glu.gluQuadricDrawStyle(self.quad, gl.GL_LINE)
glu.gluQuadricTexture(self.quad, gl.GL_TRUE)
def nextPose(self):
"""Show next pose of the foot
"""
self.setPose((self.pose + 1) % self.__num_poses)
def prevPose(self):
"""Show previous pose of the foot
"""
self.setPose((self.pose - 1) % self.__num_poses)
def setPose(self, pose: int):
"""Sets a specific pose
Arguments:
pose {int} -- The pose number
"""
self.pose = pose
def recenter(self, data: SensorData = None):
"""Sets an offset to define the resting standing pose
Keyword Arguments:
data {SensorData} -- the sensor data to set as the resting pose, or
{None} to set the current sensor data (default: {None})
"""
if data is None:
data = self.sensor_data
self.offset = copy.deepcopy(data)
def drawText(self, position, textString):
font = pygame.font.SysFont("Courier", 18, True)
text_surface = font.render(
textString, True, (20, 20, 20, 255), (204, 204, 204, 230))
text_data = pygame.image.tostring(text_surface, "RGBA", True)
gl.glRasterPos3d(*position)
gl.glDrawPixels(text_surface.get_width(), text_surface.get_height(),
gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, text_data)
def draw(self):
"""Draws one frame in the OpenGL window
"""
blue = (.27, .388, .678)
dark_grey = (.235, .243, .266)
grey = (.309, .309, .309)
light_grey = (.447, .435, .449)
sensor_data = self.sensor_data
print("\r%s" % sensor_data, end='')
#FIXME: Workaround to avoid using quaternions
quat = Quaternion(sensor_data.gyro.w, sensor_data.gyro.x,
sensor_data.gyro.y, sensor_data.gyro.z)#.unit
offset = Quaternion(self.offset.gyro.w, self.offset.gyro.x,
self.offset.gyro.y, self.offset.gyro.z)
if offset:
#TODO: Change back to quaternions
# quat = offset.inverse * sensor_data.gyro
quat = quat - offset
self.flex_bent = self.offset.flex - self.flex_straight + self.flex_bent
self.flex_straight = self.offset.flex
gyro_euler = quat_to_euler(quat)
rotation = quat_to_axis_rotation(quat)
flex_angle =\
self.translate_range(self.sensor_data.flex, self.flex_straight, self.flex_bent, 0.0, 90.0) \
if self.sensor_data.flex != 0 else 0
flex_angle = min(170, max(-20, flex_angle))
gl.glClearColor(.8, .8, .8, 1.0)
gl.glClearDepth(1.0)
gl.glEnable(gl.GL_DEPTH_TEST)
gl.glEnable(gl.GL_LIGHTING)
gl.glShadeModel(gl.GL_SMOOTH)
gl.glDisable(gl.GL_COLOR_MATERIAL)
gl.glDepthFunc(gl.GL_LEQUAL)
gl.glHint(gl.GL_PERSPECTIVE_CORRECTION_HINT, gl.GL_NICEST)
gl.glClear(gl.GL_COLOR_BUFFER_BIT |
gl.GL_DEPTH_BUFFER_BIT | gl.GL_STENCIL_BUFFER_BIT)
gl.glEnable(gl.GL_LIGHT0)
gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION, (1, 2, 3))
gl.glLightfv(gl.GL_LIGHT0, gl.GL_AMBIENT, (.5, .5, .5))
gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, (.6, .6, .6))
gl.glLightfv(gl.GL_LIGHT0, gl.GL_SPECULAR, (0, 0, 0))
# gl.glLightfv(gl.GL_LIGHT0, gl.GL_SPOT_DIRECTION, (-2, -3, -3))
gl.glLightf(gl.GL_LIGHT0, gl.GL_SPOT_CUTOFF, 180) # omnidirectional
gl.glLoadIdentity()
gl.glTranslatef(0, 0.0, -7.0)
osd_line = \
"x: {0:<7.2f}".format(quat.x) + \
"y: {0:<7.2f}".format(quat.y) + \
"z: {0:<7.2f}".format(quat.z) + \
"flex: {0:>8}".format("{0:.2f}°".format(flex_angle))
self.drawText((-2, 1.9, 2), osd_line)
gl.glPushMatrix()
gl.glTranslatef(0, 2.0, 0.0)
gl.glNormal3f(0.0, -1.0, 0.0)
#TODO: Change back to quaternions
# gl.glRotatef(-gyro_euler.x, 0, 1, 0)
# gl.glRotatef(-gyro_euler.y, 1, 0, 0)
# gl.glRotatef(gyro_euler.z*2, 0, 0, 1)
# gl.glRotatef(quat.x, 0, 1, 0)
gl.glRotatef(2*quat.y, 0, 0, 1)
gl.glRotatef(quat.z, 1, 0, 0)
gl.glRotatef(120, .5, .5, -.5)
gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT_AND_DIFFUSE, blue)
glu.gluCylinder(self.quad, 0.2, 0.15, 2, 10, 1)
gl.glTranslatef(0, 0, 2)
# Flex sensor:
# Pitch, rotate around x-axis
gl.glRotatef(flex_angle, 1.0, 0.0, 0.0)
gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT_AND_DIFFUSE,dark_grey)
glu.gluSphere(self.quad, 0.2, 6, 6)
gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT_AND_DIFFUSE,blue)
glu.gluCylinder(self.quad, 0.15, 0.125, 1.8, 9, 1)
gl.glTranslatef(0, 0, 1.8)
# -------------------
# First part of foot
# -------------------
if self.pose == 0:
pass
elif self.pose == 1:
gl.glRotatef(60.0, 1.0, 0.0, 0.0)
gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT_AND_DIFFUSE,dark_grey)
glu.gluSphere(self.quad, 0.2, 6, 6)
gl.glBegin(gl.GL_QUADS)
gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT_AND_DIFFUSE,grey)
# foot - back
gl.glNormal3f(0, -1, 0)
gl.glVertex3f(-0.2, -0.1, 0.0)
gl.glVertex3f(0.2, -0.1, 0.0)
gl.glVertex3f(0.2, -0.1, 0.3)
gl.glVertex3f(-0.2, -0.1, 0.3)
# foot - right
gl.glNormal3f(-1, 0, 0)
gl.glVertex3f(-0.2, -0.1, 0.3)
gl.glVertex3f(-0.2, 0.8, 0.3)
gl.glVertex3f(-0.2, 0.8, 0.1)
gl.glVertex3f(-0.2, -0.1, 0.0)
# foot - left
gl.glNormal3f(1, 0, 0)
gl.glVertex3f(0.2, -0.1, 0.3)
gl.glVertex3f(0.2, 0.8, 0.3)
gl.glVertex3f(0.2, 0.8, 0.1)
gl.glVertex3f(0.2, -0.1, 0.0)
# foot - top
gl.glNormal3f(0, 0, -1)
gl.glVertex3f(-0.2, -0.1, 0.0)
gl.glVertex3f(-0.2, 0.8, 0.1)
gl.glVertex3f(0.2, 0.8, 0.1)
gl.glVertex3f(0.2, -0.1, 0.0)
# foot - front
gl.glNormal3f(0, 1, 0)
gl.glVertex3f(-0.2, -0.1, 0.3)
gl.glVertex3f(-0.2, 0.8, 0.3)
gl.glVertex3f(0.2, 0.8, 0.3)
gl.glVertex3f(0.2, -0.1, 0.3)
# foot - bottom
gl.glNormal3f(0, 0, 1)
gl.glVertex3f(-0.2, 0.8, 0.3)
gl.glVertex3f(-0.2, 0.8, 0.1)
gl.glVertex3f(0.2, 0.8, 0.1)
gl.glVertex3f(0.2, 0.8, 0.3)
gl.glEnd()
gl.glTranslatef(0, 0.8, 0.1)
# -------------------
# Second part of foot
# -------------------
if self.pose == 0:
pass
elif self.pose == 1:
gl.glRotatef(-60.0, 1.0, 0.0, 0.0)
gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT_AND_DIFFUSE,dark_grey)
glu.gluSphere(self.quad, 0.1, 6, 6)
gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT_AND_DIFFUSE,grey)
gl.glBegin(gl.GL_QUADS)
# foot - back
gl.glNormal3f(0, -1, 0)
gl.glVertex3f(-0.2, 0.02, 0.0)
gl.glVertex3f(0.2, 0.02, 0.0)
gl.glVertex3f(0.2, 0.02, 0.2)
gl.glVertex3f(-0.2, 0.02, 0.2)
# foot - right
gl.glNormal3f(-1, 0, 0)
gl.glVertex3f(-0.2, 0.02, 0.2)
gl.glVertex3f(-0.2, 0.4, 0.2)
gl.glVertex3f(-0.2, 0.4, 0.1)
gl.glVertex3f(-0.2, 0.02, 0.0)
# foot - left
gl.glNormal3f(1, 0, 0)
gl.glVertex3f(0.2, 0.02, 0.2)
gl.glVertex3f(0.2, 0.4, 0.2)
gl.glVertex3f(0.2, 0.4, 0.1)
gl.glVertex3f(0.2, 0.02, 0.0)
# foot - top
gl.glNormal3f(0, 0, -1)
gl.glVertex3f(-0.2, 0.02, 0.0)
gl.glVertex3f(-0.2, 0.4, 0.1)
gl.glVertex3f(0.2, 0.4, 0.1)
gl.glVertex3f(0.2, 0.02, 0.0)
# foot - front
gl.glNormal3f(0, 1, 0)
gl.glVertex3f(-0.2, 0.02, 0.2)
gl.glVertex3f(-0.2, 0.4, 0.2)
gl.glVertex3f(0.2, 0.4, 0.2)
gl.glVertex3f(0.2, 0.02, 0.2)
# foot - bottom
gl.glNormal3f(0, 0, 1)
gl.glVertex3f(-0.2, 0.4, 0.2)
gl.glVertex3f(-0.2, 0.4, 0.1)
gl.glVertex3f(0.2, 0.4, 0.1)
gl.glVertex3f(0.2, 0.4, 0.2)
gl.glEnd()
gl.glPopMatrix()