IKSolution - as IK Solution dataclass

class IKSolution(q, success, iterations=0, searches=0, residual=0.0, reason='')[source]

A dataclass for representing an IK solution

q

The joint coordinates of the solution (ndarray). Note that these will not be valid if failed to find a solution. The shape is (n,) for a single pose, or (N, n) for a trajectory of N poses

Type:

ndarray

success

True if a valid solution was found

Type:

bool

iterations

How many iterations were performed

Type:

int

searches

How many searches were performed

Type:

int

residual

The final error value from the cost function

Type:

float

reason

The reason the IK problem failed if applicable

Type:

str

Changed in version 1.0.3: Added IKSolution dataclass to replace the IKsolution named tuple

Changed in version 1.4.2: Added __getitem__ (positional indexing matching __iter__’s order) and a __repr__ matching __str__, so ik_LM/ik_NR/ ik_GN (which now also return IKSolution, see ik_LM()) remain compatible with code that indexed the old bare tuple return

Changed in version 1.5.0: An IKSolution is now a sequence of the rows of q, one per pose: len(sol) is the number of poses (1 for a single pose), and for q in sol and sol[i] give the joint vector of each pose. This replaces iterating over the fields, so q, success, ... = sol and sol[1] no longer work, use the attributes or astuple. An IKSolution is true if success is true. Printing a trajectory shows the number of poses and abbreviates a long q, and the residual of a failed solution is no longer rounded to zero.

The solution is a sequence of the rows of q, one per pose.

>>> import numpy as np
>>> import roboticstoolbox as rtb
>>> panda = rtb.models.Panda()
>>> T = panda.fkine(panda.qr + np.array([[0], [0.05], [0.1]]))
>>> sol = panda.ikine_LM(T, q0=panda.qr, seed=0)
>>> len(sol)
3
>>> sol[2]
array([ 0.098 , -0.2004,  0.1018, -2.1014,  0.1013,  2.1012,  0.8844])
>>> sol[:2]
array([[ 0.    , -0.3   ,  0.    , -2.2   ,  0.    ,  2.    ,  0.7854],
       [ 0.0491, -0.25  ,  0.0509, -2.1501,  0.0503,  2.05  ,  0.8352]])
>>> bool(sol) == sol.success
True

A single pose is treated as one row, so len(sol) is 1 and sol[0] is q. sol[i] is the same as np.atleast_2d(sol.q)[i], it does not return an IKSolution since success, residual and reason are values for the whole solution, not for each pose.