test_analytical_ik.py
Go to the documentation of this file.
1 from __future__ import print_function
2 import numpy as np
3 import sys
4 import roslib
5 roslib.load_manifest("ur_kinematics")
6 from ur_kin_py import forward, inverse
7 
8 def best_sol(sols, q_guess, weights):
9  valid_sols = []
10  for sol in sols:
11  test_sol = np.ones(6)*9999.
12  for i in range(6):
13  for add_ang in [-2.*np.pi, 0, 2.*np.pi]:
14  test_ang = sol[i] + add_ang
15  if (abs(test_ang) <= 2.*np.pi and
16  abs(test_ang - q_guess[i]) < abs(test_sol[i] - q_guess[i])):
17  test_sol[i] = test_ang
18  if np.all(test_sol != 9999.):
19  valid_sols.append(test_sol)
20  if len(valid_sols) == 0:
21  return None
22  best_sol_ind = np.argmin(np.sum((weights*(valid_sols - np.array(q_guess)))**2,1))
23  return valid_sols[best_sol_ind]
24 
25 def test_q(q):
26  x = forward(q)
27  sols = inverse(np.array(x), float(q[5]))
28 
29  qsol = best_sol(sols, q, [1.]*6)
30  if qsol is None:
31  qsol = [999.]*6
32  diff = np.sum(np.abs(np.array(qsol) - q))
33  if diff > 0.001:
34  print(np.array(sols))
35  print('Best q:', qsol)
36  print('Actual:', np.array(q))
37  print('Diff: ', q - qsol)
38  print('Difdiv:', (q - qsol)/np.pi)
39  print(i1-3, i2-3, i3-3, i4-3, i5-3, i6-3)
40  if raw_input() == 'q':
41  sys.exit()
42 
43 def main():
44  np.set_printoptions(precision=3)
45  print("Testing multiples of pi/2...")
46  for i1 in range(0,5):
47  for i2 in range(0,5):
48  print(i1, i2)
49  for i3 in range(0,5):
50  for i4 in range(0,5):
51  for i5 in range(0,5):
52  for i6 in range(0,5):
53  q = np.array([i1*np.pi/2., i2*np.pi/2., i3*np.pi/2.,
54  i4*np.pi/2., i5*np.pi/2., i6*np.pi/2.])
55  test_q(q)
56  print("Testing random configurations...")
57  for i in range(10000):
58  q = (np.random.rand(6)-.5)*4*np.pi
59  test_q(q)
60  print("Done!")
61 
62 if __name__ == "__main__":
63  if False:
64  import cProfile
65  cProfile.run('main()', 'ik_prof')
66  else:
67  main()
int inverse(const double *T, double *q_sols, double q6_des=0.0)
Definition: ur_kin.cpp:197
void forward(const double *q, double *T)
Definition: ur_kin.cpp:47
def best_sol(sols, q_guess, weights)


ur_kinematics
Author(s): Kelsey Hawkins
autogenerated on Sun Nov 24 2019 03:36:27