-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.py
195 lines (169 loc) · 5.28 KB
/
misc.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
###############################################################################
# Author: Qasim Khawaja
# Version: 1.0.0
# Licence: LGPL-3.0 (GNU Lesser General Public License version 3)
#
# Description: Derive the Jacobian Matrix from the Denavit-Hartenberg Transformation Matrices
###############################################################################
from sympy import *
t = Symbol("t", real=True)
init_printing(use_unicode=True)
class Theta(Function):
"""
Theta Function
"""
nargs = 2
is_real = True
is_Function = True
is_commutative = True
thetas = [
symbols("q1", real=True), # Theta(symbols("q1"), t),
symbols("q2", real=True), # Theta(symbols("q2"), t),
symbols("q3", real=True), # Theta(symbols("q3"), t),
symbols("q4", real=True), # Theta(symbols("q4"), t),
]
def jacobian_matrix() -> Matrix:
"""Calculates the Jacobian Matrix"""
T4 = denavit_hartenberg(4)
d_4 = T4[:3, 3]
J: Matrix = ones(6, 4)
for i in range(4):
if i == 0:
# pretty_print(Matrix([[0], [0], [1]]).cross(d_4))
J[:3, i] = Matrix([[0], [0], [1]]).cross(d_4)
J[3:, i] = Matrix([[0], [0], [1]])
else:
T_i: Matrix = denavit_hartenberg(i)
d_i = T_i[:3, 3]
R_i = T_i.copy()
R_i = R_i[:3, :3]
J[:3, i] = (R_i @ Matrix([[0], [0], [1]])).cross(d_4 - d_i)
J[3:, i] = R_i @ Matrix([[0], [0], [1]])
J.simplify()
print_python(J.tolist())
def denavit_hartenberg(index: int):
"""
The following function calculates the Denavit-Hartenberg Transformation Matrix for a 4-DoF Arm
with the following parameters:
+---+-----------+-----------+-----------+-----------+--------------------+
| j | theta | d | a | alpha | offset |
+---+-----------+-----------+-----------+-----------+--------------------+
| 1| q1| 10.4902| 0| pi/2| 0 |
| 2| q2| 0| 14.2400| 0| pi/2 + 10.54*pi/180|
| 3| q3| 0| 16.3006| 0| pi/2 - 10.54*pi/180|
| 4| q4| 0| 10.5900| 0| -19.3*pi/180 |
+---+-----------+-----------+-----------+-----------+--------------------+
"""
d = [
10.4902,
0,
0,
0
]
a = [
0,
14.2400,
16.3006,
10.5900
]
offset = [0, pi/2 + 10.54*pi/180, pi/2 - 10.54*pi/180, -19.3*pi/180]
alpha = [pi / 2, 0, 0, 0]
T = eye(4)
for i in range(index):
T = T * transformation_matrix(thetas[i], d[i], a[i], alpha[i], offset[i])
return T
def transformation_matrix(
theta: Symbol, d: Symbol, a: float, alpha: float, offset: Symbol
) -> Matrix:
"""Calculates the Transformation Matrix"""
theta = theta + offset
return Matrix(
[
[
cos(theta),
-sin(theta) * cos(alpha),
sin(theta) * sin(alpha),
a * cos(theta),
],
[
sin(theta),
cos(theta) * cos(alpha),
-cos(theta) * sin(alpha),
a * sin(theta),
],
[0, sin(alpha), cos(alpha), d],
[0, 0, 0, 1],
]
)
def translation_matrix(x: float, y: float, z: float) -> Matrix:
"""Calculates the Translation Matrix"""
return Matrix(
[
[1, 0, 0, x],
[0, 1, 0, y],
[0, 0, 1, z],
[0, 0, 0, 1],
]
)
def rotation_matrix(axis: str, angle: float) -> Matrix:
"""Calculates the Rotation Matrix"""
if axis == "x":
return Matrix(
[
[
1,
0,
0,
],
[0, cos(angle), -sin(angle)],
[0, sin(angle), cos(angle)],
]
)
elif axis == "y":
return Matrix(
[
[
cos(angle),
0,
sin(angle),
],
[
0,
1,
0,
],
[-sin(angle), 0, cos(angle)],
]
)
elif axis == "z":
return Matrix(
[
[
cos(angle),
-sin(angle),
0,
],
[
sin(angle),
cos(angle),
0,
],
[
0,
0,
1,
],
]
)
else:
raise ValueError("Invalid axis")
# a = transformation_matrix(symbols("q"), symbols("d"), symbols("a"), symbols("alpha"), symbols("offset"))
# a = translation_matrix(symbols("x"), symbols("y"), symbols("z"))
# print_latex(a)
# rx = rotation_matrix("x", symbols("Theta"))
# print_latex(Equality(symbols("R_x"), rx, evaluate=False))
# ry = rotation_matrix("y", symbols("Theta"))
# print_latex(Equality(symbols("R_y"), ry, evaluate=False))
# rz = rotation_matrix("z", symbols("Theta"))
# print_latex(Equality(symbols("R_z"), rz, evaluate=False))
jacobian_matrix()