Skip to content

Commit 9c98231

Browse files
committed
Make IKSolution iterable and add test
1 parent a7d1307 commit 9c98231

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

roboticstoolbox/robot/IK.py

+23
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ class IKSolution:
5454
residual: float = 0.0
5555
reason: str = ""
5656

57+
def __iter__(self):
58+
return iter(
59+
(
60+
self.q,
61+
self.success,
62+
self.iterations,
63+
self.searches,
64+
self.residual,
65+
self.reason,
66+
)
67+
)
68+
5769
def __str__(self):
5870

5971
if self.q is not None:
@@ -1435,3 +1447,14 @@ def step(
14351447
q += xd[: ets.n]
14361448

14371449
return E, q
1450+
1451+
1452+
if __name__ == "__main__": # pragma nocover
1453+
1454+
sol = IKSolution(
1455+
np.array([1, 2, 3]), success=True, iterations=10, searches=100, residual=0.1
1456+
)
1457+
1458+
a, b, c, d, e = sol
1459+
1460+
print(a, b, c, d, e)

tests/test_IK.py

+19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import roboticstoolbox as rtb
77
import numpy as np
88
import unittest
9+
import numpy.testing as nt
910

1011
# import sympy
1112
import pytest
@@ -821,6 +822,24 @@ def test_sol_print5(self):
821822

822823
self.assertEqual(s, ans)
823824

825+
def test_iter_iksol(self):
826+
sol = rtb.IKSolution(
827+
np.array([1.0, 2.0, 3.0]),
828+
success=True,
829+
iterations=10,
830+
searches=100,
831+
residual=0.1,
832+
)
833+
834+
a, b, c, d, e, f = sol
835+
836+
nt.assert_almost_equal(a, np.array([1.0, 2.0, 3.0])) # type: ignore
837+
self.assertEqual(b, True)
838+
self.assertEqual(c, 10)
839+
self.assertEqual(d, 100)
840+
self.assertEqual(e, 0.1)
841+
self.assertEqual(f, "")
842+
824843

825844
if __name__ == "__main__":
826845

0 commit comments

Comments
 (0)