Skip to content
22 changes: 17 additions & 5 deletions src/roboticstoolbox/ets/ETS.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def insert(
elif isinstance(value, ETS):
for j, et in enumerate(value):
self._data.insert(index + j, et)
self._fknm_stale = True
self._changed()

def fkine(
self,
Expand Down Expand Up @@ -1011,6 +1011,10 @@ def partial_fkine0(self, q: ArrayLike, n: int) -> NDArray:
where the Jacobian is the first partial derivative and the Hessian is the
second.

The result has shape ``(self.n,) * (n - 1) + (6, self.n)``. Each
leading axis differentiates with respect to a joint in sequence order;
the last two axes have the same ordering as :meth:`jacob0`.

.. rubric:: Examples

The following example makes a ``Panda`` robot object, and solves for the
Expand Down Expand Up @@ -1135,6 +1139,11 @@ def add_pdi(pdi: Any) -> Any:
rot = np.zeros(3)
trn = np.zeros(3)

# H[k, :3, j] = w[min(j, k)] x v[max(j, k)].
# H[k, 3:, j] = w[k] x w[j] only when k precedes j:
# a downstream joint cannot change an upstream joint axis.
first, last = sorted(count[:2])

# This loop calculates a single column ([trn, rot])
# of the tensor for dT(x)
for j in range(len(indices)):
Expand All @@ -1158,11 +1167,14 @@ def add_pdi(pdi: Any) -> Any:
col0_rot = pdr0[tuple(idx0_slices) + (slice(3, 6), idx0_n)]
col1_rot = pdr1[tuple(idx1_slices) + (slice(3, 6), idx1_n)]

# Use our indices to select the translational column from pdr1
col1_trn = pdr1[tuple(idx1_slices) + (slice(0, 3), idx1_n)]

# Perform the cross product as described in the maths above
rot += np.cross(col0_rot, col1_rot)
if count[1] < count[0]:
rot += np.cross(col0_rot, col1_rot)

# Apply the same product rule to the ordered translational
# Hessian factors, retaining each factor's derivative indices.
col0_rot = pdr0[tuple(idx0_slices) + (slice(3, 6), first)]
col1_trn = pdr1[tuple(idx1_slices) + (slice(0, 3), last)]
trn += np.cross(col0_rot, col1_trn)

pd[tuple(np.flip(count[1:])) + (slice(0, 3), count[0])] = trn
Expand Down
32 changes: 23 additions & 9 deletions src/roboticstoolbox/ets/ETS2.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def insert(
elif isinstance(arg, ETS2):
for j, et in enumerate(arg):
self._data.insert(i + j, et)
self._fknm_stale = True
self._changed()

def fkine(
self,
Expand Down Expand Up @@ -336,6 +336,7 @@ def eval(

``T = ets.eval(q)`` evaluates forward kinematics for the robot at
joint configuration ``q``, returning the raw ndarray.
An empty sequence evaluates to the identity, with base and tool applied.

**Trajectory operation**: If ``q`` has multiple rows (mxn), it is considered a
trajectory and the result is an ndarray of shape (m,3,3).
Expand All @@ -356,7 +357,7 @@ def eval(

q = self._resolve_q(q, allow_trajectory=True)
l, _ = q.shape
end = self[-1]
end = self[-1] if self.m else None

if base is None:
bases = None
Expand All @@ -376,16 +377,20 @@ def eval(
else: # pragma: nocover
tools = tool

T: NDArray
if l > 1:
T = np.zeros((l, 3, 3), dtype=object)
else:
T = np.zeros((3, 3), dtype=object)

for k, qk in enumerate(q):
link = end # start with last link

jindex = 0 if link.jindex is None and link.isjoint else link.jindex
Tk = link.A(qk[jindex])
if end is None:
# Constant folding can leave an empty identity sequence.
Tk = np.eye(3)
else:
link = end # start with last link
jindex = 0 if link.jindex is None and link.isjoint else link.jindex
Tk = link.A(qk[jindex])

if tools is not None:
Tk = Tk @ tools
Expand Down Expand Up @@ -560,6 +565,10 @@ def ikine_LM(
that lies outside the joint limits is rejected when ``joint_limits`` is
True, and the search continues.

Coordinates follow :meth:`fkine`: when explicit joint indices permute
``0..n-1``, ``q0`` and the returned ``q`` use joint-index order. For a
sparse subchain they use compact ET order.

**Trajectory operation**: for a trajectory of N poses the solver is run
once per pose, each from the same starting points, and the returned
``q`` has shape (N, n). ``success`` is True only if every pose was
Expand Down Expand Up @@ -617,7 +626,12 @@ def ikine_LM(
)

n = self.n
qlim = self.qlim
# FK interprets a full 0..n-1 vector in joint-index order, whereas
# limits, joint types and Jacobian columns are stored in ET order.
order = np.arange(n)
if np.array_equal(np.sort(self.jindices), order):
order = np.argsort(self.jindices)
qlim = self.qlim[:, order]

if not np.all(np.isfinite(qlim)):
bad = np.flatnonzero(~np.all(np.isfinite(qlim), axis=0))
Expand All @@ -639,7 +653,7 @@ def ikine_LM(
rows = min(q0a.shape[0], slimit)
starts[:rows] = q0a[:rows]

revolute = np.array([joint.isrotation for joint in self.joints()])
revolute = np.array([joint.isrotation for joint in self.joints()])[order]

def solve_one(T: NDArray) -> IKSolution:
theta_d = np.arctan2(T[1, 0], T[0, 0])
Expand Down Expand Up @@ -692,7 +706,7 @@ def error(q: NDArray) -> tuple[NDArray, float]:

i += 1

J = self.jacob0(q)
J = self.jacob0(q)[:, order]
A = J.T @ We @ J + k * E * np.eye(n)
g = J.T @ We @ e

Expand Down
25 changes: 11 additions & 14 deletions src/roboticstoolbox/ets/_ETS.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _dirties_fknm(func: _F) -> _F:
@wraps(func)
def wrapper(self: Any, *args: Any, **kwargs: Any) -> Any:
result = func(self, *args, **kwargs)
self._fknm_stale = True
self._changed()
return result

return wrapper # type: ignore[return-value]
Expand Down Expand Up @@ -611,7 +611,7 @@ def data(self) -> list[ETT]:
@data.setter
def data(self, new_data: list[ETT]) -> None:
self._data = new_data
self._fknm_stale = True
self._changed()

def inv(self) -> Self:
r"""
Expand Down Expand Up @@ -731,7 +731,8 @@ def random_q(self, i: int = 1) -> NDArray:
:rtype: ndarray(n,) or ndarray(i,n)

Generates a random q vector within the joint limits defined by
``self.qlim``.
``self.qlim``, in the coordinate order accepted by :meth:`fkine`.
A subchain with sparse joint indices returns compact coordinates.

.. rubric:: Examples

Expand All @@ -745,18 +746,14 @@ def random_q(self, i: int = 1) -> NDArray:

"""

if i == 1:
q = np.zeros(self.n)

for i in range(self.n):
q[i] = uniform(self.qlim[0, i], self.qlim[1, i])

else:
q = np.zeros((i, self.n))
qlim = self.qlim
shape = (self.n,) if i == 1 else (i, self.n)
q = uniform(qlim[0], qlim[1], size=shape)

for j in range(i):
for i in range(self.n):
q[j, i] = uniform(self.qlim[0, i], self.qlim[1, i])
# A full chain consumes global coordinates, even when its joints occur
# in a different order. Sparse subchains consume compact coordinates.
if self.n and np.array_equal(np.sort(self.jindices), np.arange(self.n)):
q[..., self.jindices] = q.copy()

return q

Expand Down
8 changes: 6 additions & 2 deletions src/roboticstoolbox/robot/BaseRobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1861,7 +1861,8 @@ def random_q(self) -> Any:
Return a random joint configuration

The value for each joint is uniform randomly distributed between the
limits set for the robot.
limits set for the robot. Values are addressed by joint index, as in
the robot's configuration vector.

.. rubric:: Notes

Expand All @@ -1876,7 +1877,10 @@ def random_q(self) -> Any:
qlim = self.qlim
if np.any(np.isnan(qlim)):
raise ValueError("some joint limits not defined") # pragma nocover
return np.random.uniform(low=qlim[0, :], high=qlim[1, :], size=(self.n,))
q = np.empty(self.n)
indices = [link.jindex for link in self.links if link.jindex is not None]
q[indices] = np.random.uniform(low=qlim[0, :], high=qlim[1, :], size=(self.n,))
return q

def hierarchy(self) -> None:
"""
Expand Down
Loading
Loading