-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrobot.py
More file actions
248 lines (191 loc) · 5.57 KB
/
robot.py
File metadata and controls
248 lines (191 loc) · 5.57 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
"""
Robot Module
Methods
---
init_link
init_dh
transform
_transform
forward_kinematics
str, repr
"""
import numpy as np
from link import Link
from visualize import plot_transformations
class Robot:
"""
Robot Module
...
Parmeters
---
Methods
---
init_link : Initialize robot class with links
init_dh : Initialize robot class with dh parameters
transform: Calculate transform till given link
forward_kinematics: Calculate forward kinematics for the robot
plot: Plot the robot frames
_transform: Private function to calculate intermediate transform
"""
def __str__(self):
string = f"Robot Links\n"
string += f"Link Name\ta\talpha\td\ttheta\tJoint Type\n"
string += f"---\t\t---\t---\t---\t---\t---\n"
for link in self._links:
string += f"{link.name}\t\t{link.a}\t{link.alpha}\t{link.d}\t"
string += f"{link.theta}\t{link.joint_type}\n"
return string
def __repr__(self):
string = "Robot()"
return string
def __init__(self):
"""
Initialize robot class
...
Parameters
---
None
"""
self._links = []
self._link_names = []
def init_link(self, link_list):
"""
Initialize robot with list of links
...
Parameters
---
link_list: List of link objects
Returns
---
None
"""
self._links = []
self._link_names = []
for link in link_list:
try:
assert(isinstance(link, Link))
except AssertionError:
raise("Links should be of Link Object type")
self._links.append(link)
self._link_names.append(link.name)
def init_dh(self, dh_parameters):
"""
Initialize robot with dh parameter table
...
Parameters
---
dh_parameters: nx5 array of dh parameters
[a, alpha, d, theta, joint_type]
Returns
---
None
"""
self._links = []
self._link_names = []
dof = dh_parameters.shape[0]
try:
assert(dh_parameters.shape[1] == 5)
except AssertionError:
raise("DH Parameters Table not correct")
for i in range(dof):
name = f"link_{i}"
a = dh_parameters[i, 0]
alpha = dh_parameters[i, 1]
d = dh_parameters[i, 2]
theta = dh_parameters[i, 3]
joint_type = dh_parameters[i, 4]
link = Link(name = name, alpha = alpha, d = d, a = a,
theta = theta, joint_type = joint_type)
self._links.append(link)
self._link_names.append(name)
def _transform(self, index, q):
"""
Private Function to calculate intermediate transformation
...
Parameters
---
index : index of the link to calculate transformation for
q : transformation parameter
Returns
---
transformation: 4x4 transformation matrix
"""
link = self._links[index]
transformation = link.transform(q)
return transformation
def transform(self, link_name, q):
"""
Calculate transform till given link
...
Parameters
---
link_name: Name of the link till transformation is required
q: List of transformation parameters
Returns
---
transformation: 4x4 transformation matrix
"""
try:
link_index = self._link_names.index(link_name)
except ValueError:
raise("Link Name not present in Robot")
try:
assert(q.shape == (len(self._links), ))
except AssertionError:
raise("Parameter q is of not correct size")
transformation = np.eye(4)
for i in range(0, link_index + 1):
transformation = transformation @ self._transform(i, q[i])
return transformation
def forward_kinematics(self, q):
"""
Calculate forward kinematics of the robot
...
Parameters
---
q : List of transformation parameters
Returns
---
transformation: 4x4 transformation matrix
"""
end_effector = self._link_names[-1]
transformation = self.transform(end_effector, q)
return transformation
def get_configuration(self, *joint_values):
"""
Function to get a configuration array
...
Parameters
---
*joint_values: Different Joint values
Returns
---
q: Numpy array of joint values
"""
try:
assert(len(joint_values) == len(self._links))
except AssertionError:
raise("Number of parameters provided are not equal to DOF")
q = np.array(joint_values)
return q
def plot(self, q):
"""
Function to plot robot frames
...
Parameters
---
q : List of transformation parameters
Returns
---
None
"""
try:
assert(q.shape == (len(self._links), ))
except AssertionError:
raise("Parameter q is of not correct size")
transformations = []
transformations.append(np.eye(4))
for i in range(len(self._links)):
transformation = transformations[i] @ self._transform(i, q[i])
transformations.append(transformation)
plot_transformations(transformations)