-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.py
177 lines (105 loc) · 3.79 KB
/
robot.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
import snap
from PySide2 import QtWidgets
import sys
from threading import Thread
import OpenGL
from OpenGL.GL import *
from OpenGL.GLU import *
from snap import gl
from snap.math import *
import snap.viewer
import time
class RigidBody(object):
'''a rigid body object'''
def __init__(self):
# properties
self.mass = 1.0
self.inertia = [1, 1, 1]
self.name = 'unnamed body'
# dimensions for drawing
self.dim = [1, 1, 1]
# body dofs
self.frame = Rigid3()
class Joint(object):
'''a joint between rigid bodies'''
def __init__(self):
# properties
self.parent_index = 0
self.parent_frame = Rigid3()
self.child_index = 0
self.child_frame = Rigid3()
# joint dofs
self.dofs = Rigid3()
self.name = 'unnamed joint'
class Robot(object):
def __init__(self):
# an array of 3 rigid bodies
self.body = [ RigidBody() for i in range(3) ]
# body properties
self.body[0].name = 'basis'
self.body[1].name = 'link1'
self.body[2].name = 'link2'
# configuration
self.body[0].frame.center[2] = 2
self.body[0].frame.center[1] = 0.5
self.body[0].dim = [0.1, 1, 0.1]
self.body[1].dim = [0.1, 2, 0.1]
self.body[2].dim = [0.1, 2, 0.1]
# an array of 2 joints
self.joint = [ Joint() for i in range(2) ]
self.joint[0].name = 'basis-link1'
self.joint[0].parent_index = 0
self.joint[0].child_index = 1
# joint coordinates
self.joint[0].parent_frame.center = [0, 0.5, 0]
self.joint[0].child_frame.center = [0, -1, 0]
self.joint[1].name = 'link1-link2'
self.joint[1].parent_index = 1
self.joint[1].child_index = 2
self.joint[1].parent_frame.center = [0, 1, 0]
self.joint[1].child_frame.center = [0, -1, 0]
# root body
self.root = 0
# joint traversal order for forward kinematics (TODO compute
# automatically)
self.forward = [0, 1]
def update(self):
'''update body dofs from joint dofs (forward kinematics)'''
# traverse joints in forward order
for i in self.forward:
j = self.joint[i]
c = self.body[j.child_index]
p = self.body[j.parent_index]
# compute child frame
c.frame = p.frame * j.parent_frame * j.dofs * j.child_frame.inv()
def draw(self):
glColor(1, 1, 1)
for b in self.body:
# in body frame
with gl.frame(b.frame):
# scale by body dimensions
glScale(*b.dim)
# offset drawing on the y axis
glTranslate(0, -0.5, 0)
# draw a cylinder along the y axis
with gl.lookat(ey):
gl.cylinder()
class Viewer(snap.viewer.Viewer):
def init(self):
self.robot = Robot()
self.animation.start()
self.start_time = time.time()
def animate(self):
t = time.time() - self.start_time
self.robot.joint[0].dofs.orient = Quaternion.exp( math.pi/4 * math.sin(t) * ez )
self.robot.joint[1].dofs.orient = Quaternion.exp( math.pi/4 * math.sin(2 * t) * ez )
self.robot.update()
def draw(self):
self.robot.draw()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Viewer()
w.show()
w.camera.pivot = w.robot.body[w.robot.root].frame.center
w.camera.frame.center[2] = 5
sys.exit(app.exec_())