-
-
Notifications
You must be signed in to change notification settings - Fork 584
Expand file tree
/
Copy pathik_exp.py
More file actions
276 lines (249 loc) · 6.16 KB
/
Copy pathik_exp.py
File metadata and controls
276 lines (249 loc) · 6.16 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
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
import numpy as np
import roboticstoolbox as rtb
import spatialmath as sm
import fknm
import time
import swift
import spatialgeometry as sg
import sys
from ansitable import ANSITable
from numpy import ndarray
from spatialmath import SE3
from typing import Union, overload, List, Set
# Our robot and ETS
robot = rtb.models.Panda()
ets = robot.ets()
### Experiment parameters
# Number of problems to solve
problems = 10000
# Cartesion DoF priority matrix
we = np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
# random valid q values which will define Tep
q_rand = ets.random_q(problems)
# Our desired end-effector poses
Tep = np.zeros((problems, 4, 4))
for i in range(problems):
Tep[i] = ets.eval(q_rand[i])
# Maximum iterations allowed in a search
ilimit = 30
# Maximum searches allowed per problem
slimit = 100
# Solution tolerance
tol = 1e-6
# Reject solutions with invalid joint limits
reject_jl = True
class IK:
def __init__(self, name, solve, problems=problems):
# Solver attributes
self.name = name
self.solve = solve
# Solver results
self.iterations = np.zeros(problems)
self.searches = np.zeros(problems)
self.residual = np.zeros(problems)
self.success = np.zeros(problems)
self.total_iterations = 0
self.total_searches = 0
solvers = [
# IK(
# "Newton Raphson",
# lambda Tep: ets.ik_nr(
# Tep,
# q0=None,
# ilimit=ilimit,
# slimit=slimit,
# tol=tol,
# reject_jl=reject_jl,
# we=we,
# use_pinv=False,
# pinv_damping=0.0,
# ),
# ),
# IK(
# "Gauss Newton",
# lambda Tep: ets.ik_gn(
# Tep,
# q0=None,
# ilimit=ilimit,
# slimit=slimit,
# tol=tol,
# reject_jl=reject_jl,
# we=we,
# use_pinv=False,
# pinv_damping=0.0,
# ),
# ),
IK(
"Newton Raphson Pinv",
lambda Tep: ets.ik_nr(
Tep,
q0=None,
ilimit=ilimit,
slimit=slimit,
tol=tol,
reject_jl=reject_jl,
we=we,
use_pinv=True,
pinv_damping=0.0,
),
),
IK(
"Gauss Newton Pinv",
lambda Tep: ets.ik_gn(
Tep,
q0=None,
ilimit=ilimit,
slimit=slimit,
tol=tol,
reject_jl=reject_jl,
we=we,
use_pinv=True,
pinv_damping=0.0,
),
),
IK(
"LM Chan 0.1",
lambda Tep: ets.ik_lm_chan(
Tep,
q0=None,
ilimit=ilimit,
slimit=slimit,
tol=tol,
reject_jl=reject_jl,
we=we,
λ=0.1,
),
),
# IK(
# "LM Chan 1.0",
# lambda Tep: ets.ik_lm_chan(
# Tep,
# q0=None,
# ilimit=ilimit,
# slimit=slimit,
# tol=tol,
# reject_jl=reject_jl,
# we=we,
# λ=1.0,
# ),
# ),
# IK(
# "LM Wampler",
# lambda Tep: ets.ik_lm_wampler(
# Tep,
# q0=None,
# ilimit=ilimit,
# slimit=slimit,
# tol=tol,
# reject_jl=reject_jl,
# we=we,
# λ=1e-2,
# ),
# ),
IK(
"LM Wampler 1e-4",
lambda Tep: ets.ik_lm_wampler(
Tep,
q0=None,
ilimit=ilimit,
slimit=slimit,
tol=tol,
reject_jl=reject_jl,
we=we,
λ=1e-4,
),
),
# IK(
# "LM Wampler 1e-6",
# lambda Tep: ets.ik_lm_wampler(
# Tep,
# q0=None,
# ilimit=ilimit,
# slimit=slimit,
# tol=tol,
# reject_jl=reject_jl,
# we=we,
# λ=1e-6,
# ),
# ),
# IK(
# "LM Sugihara 0.001",
# lambda Tep: ets.ik_lm_sugihara(
# Tep,
# q0=None,
# ilimit=ilimit,
# slimit=slimit,
# tol=tol,
# reject_jl=reject_jl,
# we=we,
# λ=0.001,
# ),
# ),
# IK(
# "LM Sugihara 0.01",
# lambda Tep: ets.ik_lm_sugihara(
# Tep,
# q0=None,
# ilimit=ilimit,
# slimit=slimit,
# tol=tol,
# reject_jl=reject_jl,
# we=we,
# λ=0.01,
# ),
# ),
IK(
"LM Sugihara 0.1",
lambda Tep: ets.ik_lm_sugihara(
Tep,
q0=None,
ilimit=ilimit,
slimit=slimit,
tol=tol,
reject_jl=reject_jl,
we=we,
λ=0.1,
),
),
]
for i in range(problems):
print(i + 1)
for solver in solvers:
_, success, iterations, searches, residual = solver.solve(Tep[i])
if success:
solver.success[i] = success
solver.iterations[i] = iterations
solver.searches[i] = searches
solver.residual[i] = residual
solver.total_iterations += solver.iterations[i]
solver.total_searches += solver.searches[i]
else:
solver.success[i] = np.nan
solver.iterations[i] = np.nan
solver.searches[i] = np.nan
solver.residual[i] = np.nan
print(f"\nNumerical Inverse Kinematics Methods Compared over {problems} problems\n")
table = ANSITable(
"Method",
"sLimit/iLimit",
"Mean Steps",
"Median Steps",
"Infeasible",
"Infeasible %",
"Mean Attempts",
# "Median Attempts",
"Max Attempts",
border="thin",
)
for solver in solvers:
table.row(
solver.name,
f"{slimit}, {ilimit}",
np.round(np.nanmean(solver.iterations), 2),
np.nanmedian(solver.iterations),
np.sum(np.isnan(solver.success)),
np.round(np.sum(np.isnan(solver.success)) / problems * 100.0, 2),
np.round(np.nanmean(solver.searches), 2),
np.nanmax(solver.searches),
)
table.print()