Skip to content

Commit 14ffc4a

Browse files
committed
mypy fix test
1 parent c36aa27 commit 14ffc4a

File tree

13 files changed

+150
-366
lines changed

13 files changed

+150
-366
lines changed

ArmNavigation/n_joint_arm_3d/NLinkArm.py renamed to ArmNavigation/n_joint_arm_3d/NLinkArm3d.py

+66-50
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from mpl_toolkits.mplot3d import Axes3D
88
import matplotlib.pyplot as plt
99

10+
1011
class Link:
1112
def __init__(self, dh_params):
1213
self.dh_params_ = dh_params
@@ -28,16 +29,22 @@ def transformation_matrix(self):
2829

2930
return trans
3031

31-
def basic_jacobian(self, trans_prev, ee_pos):
32-
pos_prev = np.array([trans_prev[0, 3], trans_prev[1, 3], trans_prev[2, 3]])
33-
z_axis_prev = np.array([trans_prev[0, 2], trans_prev[1, 2], trans_prev[2, 2]])
32+
@staticmethod
33+
def basic_jacobian(trans_prev, ee_pos):
34+
pos_prev = np.array(
35+
[trans_prev[0, 3], trans_prev[1, 3], trans_prev[2, 3]])
36+
z_axis_prev = np.array(
37+
[trans_prev[0, 2], trans_prev[1, 2], trans_prev[2, 2]])
3438

35-
basic_jacobian = np.hstack((np.cross(z_axis_prev, ee_pos - pos_prev), z_axis_prev))
39+
basic_jacobian = np.hstack(
40+
(np.cross(z_axis_prev, ee_pos - pos_prev), z_axis_prev))
3641
return basic_jacobian
37-
42+
3843

3944
class NLinkArm:
4045
def __init__(self, dh_params_list):
46+
self.fig = plt.figure()
47+
self.ax = Axes3D(self.fig)
4148
self.link_list = []
4249
for i in range(len(dh_params_list)):
4350
self.link_list.append(Link(dh_params_list[i]))
@@ -47,7 +54,7 @@ def transformation_matrix(self):
4754
for i in range(len(self.link_list)):
4855
trans = np.dot(trans, self.link_list[i].transformation_matrix())
4956
return trans
50-
57+
5158
def forward_kinematics(self, plot=False):
5259
trans = self.transformation_matrix()
5360

@@ -57,15 +64,12 @@ def forward_kinematics(self, plot=False):
5764
alpha, beta, gamma = self.euler_angle()
5865

5966
if plot:
60-
self.fig = plt.figure()
61-
self.ax = Axes3D(self.fig)
62-
6367
x_list = []
6468
y_list = []
6569
z_list = []
66-
70+
6771
trans = np.identity(4)
68-
72+
6973
x_list.append(trans[0, 3])
7074
y_list.append(trans[1, 3])
7175
z_list.append(trans[2, 3])
@@ -74,56 +78,61 @@ def forward_kinematics(self, plot=False):
7478
x_list.append(trans[0, 3])
7579
y_list.append(trans[1, 3])
7680
z_list.append(trans[2, 3])
77-
78-
self.ax.plot(x_list, y_list, z_list, "o-", color="#00aa00", ms=4, mew=0.5)
81+
82+
self.ax.plot(x_list, y_list, z_list, "o-", color="#00aa00", ms=4,
83+
mew=0.5)
7984
self.ax.plot([0], [0], [0], "o")
80-
85+
8186
self.ax.set_xlim(-1, 1)
8287
self.ax.set_ylim(-1, 1)
8388
self.ax.set_zlim(-1, 1)
84-
89+
8590
plt.show()
8691

8792
return [x, y, z, alpha, beta, gamma]
8893

8994
def basic_jacobian(self):
9095
ee_pos = self.forward_kinematics()[0:3]
9196
basic_jacobian_mat = []
92-
93-
trans = np.identity(4)
97+
98+
trans = np.identity(4)
9499
for i in range(len(self.link_list)):
95-
basic_jacobian_mat.append(self.link_list[i].basic_jacobian(trans, ee_pos))
100+
basic_jacobian_mat.append(
101+
self.link_list[i].basic_jacobian(trans, ee_pos))
96102
trans = np.dot(trans, self.link_list[i].transformation_matrix())
97-
103+
98104
return np.array(basic_jacobian_mat).T
99105

100106
def inverse_kinematics(self, ref_ee_pose, plot=False):
101107
for cnt in range(500):
102108
ee_pose = self.forward_kinematics()
103109
diff_pose = np.array(ref_ee_pose) - ee_pose
104-
110+
105111
basic_jacobian_mat = self.basic_jacobian()
106112
alpha, beta, gamma = self.euler_angle()
107-
108-
K_zyz = np.array([[0, -math.sin(alpha), math.cos(alpha) * math.sin(beta)],
109-
[0, math.cos(alpha), math.sin(alpha) * math.sin(beta)],
110-
[1, 0, math.cos(beta)]])
113+
114+
K_zyz = np.array(
115+
[[0, -math.sin(alpha), math.cos(alpha) * math.sin(beta)],
116+
[0, math.cos(alpha), math.sin(alpha) * math.sin(beta)],
117+
[1, 0, math.cos(beta)]])
111118
K_alpha = np.identity(6)
112119
K_alpha[3:, 3:] = K_zyz
113120

114-
theta_dot = np.dot(np.dot(np.linalg.pinv(basic_jacobian_mat), K_alpha), np.array(diff_pose))
121+
theta_dot = np.dot(
122+
np.dot(np.linalg.pinv(basic_jacobian_mat), K_alpha),
123+
np.array(diff_pose))
115124
self.update_joint_angles(theta_dot / 100.)
116-
125+
117126
if plot:
118127
self.fig = plt.figure()
119128
self.ax = Axes3D(self.fig)
120-
129+
121130
x_list = []
122131
y_list = []
123132
z_list = []
124-
133+
125134
trans = np.identity(4)
126-
135+
127136
x_list.append(trans[0, 3])
128137
y_list.append(trans[1, 3])
129138
z_list.append(trans[2, 3])
@@ -132,48 +141,54 @@ def inverse_kinematics(self, ref_ee_pose, plot=False):
132141
x_list.append(trans[0, 3])
133142
y_list.append(trans[1, 3])
134143
z_list.append(trans[2, 3])
135-
136-
self.ax.plot(x_list, y_list, z_list, "o-", color="#00aa00", ms=4, mew=0.5)
144+
145+
self.ax.plot(x_list, y_list, z_list, "o-", color="#00aa00", ms=4,
146+
mew=0.5)
137147
self.ax.plot([0], [0], [0], "o")
138-
148+
139149
self.ax.set_xlim(-1, 1)
140150
self.ax.set_ylim(-1, 1)
141151
self.ax.set_zlim(-1, 1)
142-
143-
self.ax.plot([ref_ee_pose[0]], [ref_ee_pose[1]], [ref_ee_pose[2]], "o")
152+
153+
self.ax.plot([ref_ee_pose[0]], [ref_ee_pose[1]], [ref_ee_pose[2]],
154+
"o")
144155
plt.show()
145-
156+
146157
def euler_angle(self):
147158
trans = self.transformation_matrix()
148-
159+
149160
alpha = math.atan2(trans[1][2], trans[0][2])
150-
if not (-math.pi / 2 <= alpha and alpha <= math.pi / 2):
161+
if not (-math.pi / 2 <= alpha <= math.pi / 2):
151162
alpha = math.atan2(trans[1][2], trans[0][2]) + math.pi
152-
if not (-math.pi / 2 <= alpha and alpha <= math.pi / 2):
163+
if not (-math.pi / 2 <= alpha <= math.pi / 2):
153164
alpha = math.atan2(trans[1][2], trans[0][2]) - math.pi
154-
beta = math.atan2(trans[0][2] * math.cos(alpha) + trans[1][2] * math.sin(alpha), trans[2][2])
155-
gamma = math.atan2(-trans[0][0] * math.sin(alpha) + trans[1][0] * math.cos(alpha), -trans[0][1] * math.sin(alpha) + trans[1][1] * math.cos(alpha))
156-
165+
beta = math.atan2(
166+
trans[0][2] * math.cos(alpha) + trans[1][2] * math.sin(alpha),
167+
trans[2][2])
168+
gamma = math.atan2(
169+
-trans[0][0] * math.sin(alpha) + trans[1][0] * math.cos(alpha),
170+
-trans[0][1] * math.sin(alpha) + trans[1][1] * math.cos(alpha))
171+
157172
return alpha, beta, gamma
158-
173+
159174
def set_joint_angles(self, joint_angle_list):
160175
for i in range(len(self.link_list)):
161176
self.link_list[i].dh_params_[0] = joint_angle_list[i]
162177

163178
def update_joint_angles(self, diff_joint_angle_list):
164179
for i in range(len(self.link_list)):
165180
self.link_list[i].dh_params_[0] += diff_joint_angle_list[i]
166-
181+
167182
def plot(self):
168183
self.fig = plt.figure()
169184
self.ax = Axes3D(self.fig)
170-
185+
171186
x_list = []
172187
y_list = []
173188
z_list = []
174189

175190
trans = np.identity(4)
176-
191+
177192
x_list.append(trans[0, 3])
178193
y_list.append(trans[1, 3])
179194
z_list.append(trans[2, 3])
@@ -182,15 +197,16 @@ def plot(self):
182197
x_list.append(trans[0, 3])
183198
y_list.append(trans[1, 3])
184199
z_list.append(trans[2, 3])
185-
186-
self.ax.plot(x_list, y_list, z_list, "o-", color="#00aa00", ms=4, mew=0.5)
200+
201+
self.ax.plot(x_list, y_list, z_list, "o-", color="#00aa00", ms=4,
202+
mew=0.5)
187203
self.ax.plot([0], [0], [0], "o")
188204

189205
self.ax.set_xlabel("x")
190206
self.ax.set_ylabel("y")
191-
self.ax.set_zlabel("z")
192-
207+
self.ax.set_zlabel("z")
208+
193209
self.ax.set_xlim(-1, 1)
194210
self.ax.set_ylim(-1, 1)
195-
self.ax.set_zlim(-1, 1)
211+
self.ax.set_zlim(-1, 1)
196212
plt.show()

ArmNavigation/n_joint_arm_3d/__init__py.py

Whitespace-only changes.

ArmNavigation/n_joint_arm_3d/random_forward_kinematics.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
Author: Takayuki Murooka (takayuki5168)
44
"""
55
import math
6-
from NLinkArm import NLinkArm
6+
from NLinkArm3d import NLinkArm
77
import random
88

9+
910
def random_val(min_val, max_val):
1011
return min_val + random.random() * (max_val - min_val)
1112

@@ -14,17 +15,17 @@ def random_val(min_val, max_val):
1415
print("Start solving Forward Kinematics 10 times")
1516

1617
# init NLinkArm with Denavit-Hartenberg parameters of PR2
17-
n_link_arm = NLinkArm([[0., -math.pi/2, .1, 0.],
18-
[math.pi/2, math.pi/2, 0., 0.],
19-
[0., -math.pi/2, 0., .4],
20-
[0., math.pi/2, 0., 0.],
21-
[0., -math.pi/2, 0., .321],
22-
[0., math.pi/2, 0., 0.],
18+
n_link_arm = NLinkArm([[0., -math.pi / 2, .1, 0.],
19+
[math.pi / 2, math.pi / 2, 0., 0.],
20+
[0., -math.pi / 2, 0., .4],
21+
[0., math.pi / 2, 0., 0.],
22+
[0., -math.pi / 2, 0., .321],
23+
[0., math.pi / 2, 0., 0.],
2324
[0., 0., 0., 0.]])
2425

2526
# execute FK 10 times
2627
for i in range(10):
27-
n_link_arm.set_joint_angles([random_val(-1, 1) for j in range(len(n_link_arm.link_list))])
28-
29-
ee_pose = n_link_arm.forward_kinematics(plot=True)
28+
n_link_arm.set_joint_angles(
29+
[random_val(-1, 1) for j in range(len(n_link_arm.link_list))])
3030

31+
ee_pose = n_link_arm.forward_kinematics(plot=True)

ArmNavigation/n_joint_arm_3d/random_inverse_kinematics.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@
33
Author: Takayuki Murooka (takayuki5168)
44
"""
55
import math
6-
from NLinkArm import NLinkArm
6+
from NLinkArm3d import NLinkArm
77
import random
88

99

1010
def random_val(min_val, max_val):
1111
return min_val + random.random() * (max_val - min_val)
1212

13+
1314
if __name__ == "__main__":
1415
print("Start solving Inverse Kinematics 10 times")
1516

1617
# init NLinkArm with Denavit-Hartenberg parameters of PR2
17-
n_link_arm = NLinkArm([[0., -math.pi/2, .1, 0.],
18-
[math.pi/2, math.pi/2, 0., 0.],
19-
[0., -math.pi/2, 0., .4],
20-
[0., math.pi/2, 0., 0.],
21-
[0., -math.pi/2, 0., .321],
22-
[0., math.pi/2, 0., 0.],
18+
n_link_arm = NLinkArm([[0., -math.pi / 2, .1, 0.],
19+
[math.pi / 2, math.pi / 2, 0., 0.],
20+
[0., -math.pi / 2, 0., .4],
21+
[0., math.pi / 2, 0., 0.],
22+
[0., -math.pi / 2, 0., .321],
23+
[0., math.pi / 2, 0., 0.],
2324
[0., 0., 0., 0.]])
2425

2526
# execute IK 10 times

ArmNavigation/n_joint_arm_to_point_control/__init__.py

Whitespace-only changes.

PathPlanning/AStar/a_star.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ def __init__(self, x, y, cost, pind):
4141
self.pind = pind
4242

4343
def __str__(self):
44-
return str(self.x) + "," + str(self.y) + "," + str(self.cost) + "," + str(self.pind)
44+
return str(self.x) + "," + str(self.y) + "," + str(
45+
self.cost) + "," + str(self.pind)
4546

4647
def planning(self, sx, sy, gx, gy):
4748
"""
@@ -72,7 +73,10 @@ def planning(self, sx, sy, gx, gy):
7273
break
7374

7475
c_id = min(
75-
open_set, key=lambda o: open_set[o].cost + self.calc_heuristic(ngoal, open_set[o]))
76+
open_set,
77+
key=lambda o: open_set[o].cost + self.calc_heuristic(ngoal,
78+
open_set[
79+
o]))
7680
current = open_set[c_id]
7781

7882
# show graph
@@ -81,7 +85,8 @@ def planning(self, sx, sy, gx, gy):
8185
self.calc_grid_position(current.y, self.miny), "xc")
8286
# for stopping simulation with the esc key.
8387
plt.gcf().canvas.mpl_connect('key_release_event',
84-
lambda event: [exit(0) if event.key == 'escape' else None])
88+
lambda event: [exit(
89+
0) if event.key == 'escape' else None])
8590
if len(closed_set.keys()) % 10 == 0:
8691
plt.pause(0.001)
8792

@@ -104,7 +109,6 @@ def planning(self, sx, sy, gx, gy):
104109
current.cost + self.motion[i][2], c_id)
105110
n_id = self.calc_grid_index(node)
106111

107-
108112
# If the node is not safe, do nothing
109113
if not self.verify_node(node):
110114
continue

PathPlanning/BSplinePath/bspline_path.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import scipy.interpolate as scipy_interpolate
1212

1313

14-
def approximate_b_spline_path(x: int, y: list, n_path_points: int,
14+
def approximate_b_spline_path(x: list, y: list, n_path_points: int,
1515
degree: int = 3) -> tuple:
1616
"""
1717
approximate points with a B-Spline path

PathPlanning/CubicSpline/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)