Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/roboticstoolbox/robot/DHRobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,26 +636,32 @@ def reach(self) -> Any:
:rtype: float

A conservative estimate of the reach of the robot. It is computed as
:math:`\sum_j |a_j| + |d_j|` where :math:`d_j` is taken as the maximum
joint coordinate (``qlim``) if the joint is primsmatic.
:math:`\sum_j |a_j| + |d_j|`. For a prismatic joint, :math:`|d_j|` is
the largest displacement over ``qlim``, including the joint offset
and flip. Base and tool transforms are not included.

.. note::

- This is the *length sum* referred to in Craig's book
- Probably an overestimate of the actual reach
- Used by numerical inverse kinematics to scale translational
error.
- For a prismatic joint, uses ``qlim`` if it is set
- Without prismatic joint limits, only the fixed offset
contributes; unbounded joint travel cannot be estimated.

.. warning:: Computed on the first access. If kinematic parameters
subsequently change this will not be reflected.
"""
if self._reach is None:
d = 0
for link in self:
d += abs(link.a) + (link.d)
if link.isprismatic and link.qlim is not None:
d += link.qlim[1]
d += abs(link.a)
if link.isprismatic:
if link.qlim is not None:
travel = -link.qlim if link.isflip else link.qlim
d += max(abs(travel + link.offset))
else:
d += abs(link.offset)
else:
d += abs(link.d)
self._reach = d
return self._reach

Expand Down
93 changes: 59 additions & 34 deletions src/roboticstoolbox/robot/Robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,17 +663,18 @@ def reach(self) -> float:
:rtype: float

A conservative estimate of the reach of the robot. It is computed as
the sum of the translational ETs that define the link transform.
the sum of the translation lengths along the longest branch, including
compound transforms. Base and tool transforms are not included.

Computed on the first access. If kinematic parameters
subsequently change this will not be reflected.

.. rubric:: Notes

- Probably an overestimate of reach
- Used by numerical inverse kinematics to scale translational
error.
- For a prismatic joint, uses ``qlim`` if it is set
- For a prismatic joint, bounds the displacement over ``qlim``,
including its offset and flip. Without limits, only the fixed
offset contributes; unbounded joint travel cannot be estimated.

"""

Expand All @@ -693,12 +694,16 @@ def reach(self) -> float:
# joint limits. They might be set in the ET
# or in the Link depending on how the robot
# was constructed
if link.qlim is not None:
d += max(link.qlim)
elif et.qlim is not None: # pragma nocover
d += max(et.qlim)
qlim = link.qlim if link.qlim is not None else et.qlim
if qlim is not None:
travel = -qlim if et.isflip else qlim
d += max(abs(travel + et.offset))
else:
d += abs(cast(float, et.offset))
else:
d += abs(cast(float, et.param))
elif not et.iselementary:
d += float(np.linalg.norm(et.A()[:-1, -1]))
link = link.parent # type: ignore[assignment]
if link is None or isinstance(link, str):
d_all.append(d)
Expand Down Expand Up @@ -1413,6 +1418,9 @@ def closest_point(
world frame which connect the line of length distance between the
shapes. If the distance is negative then the shapes are collided.

Includes collision geometry on all grippers, using each gripper's
own current joint configuration.

"""

if not skip:
Expand All @@ -1424,7 +1432,8 @@ def closest_point(
p1 = None
p2 = None

for link in self.links:
links = [*self.links, *(link for g in self.grippers for link in g.links)]
for link in links:
td, tp1, tp2 = link.closest_point(shape, inf_dist, skip=True)

if td is not None and d is not None and td < d:
Expand Down Expand Up @@ -1611,11 +1620,14 @@ def joint_velocity_damper(
"""
Compute the joint velocity damper for QP motion control

:param q: joint coordinates in joint-index order, defaults to the
robot's current configuration
:param ps: The minimum angle (in radians) in which the joint is
allowed to approach to its limit
:param pi: The influence angle (in radians) in which the velocity
damper becomes active
:param n: The number of joints to consider. Defaults to all joints
:param n: The number of leading joint coordinates to consider.
Defaults to all joints
:param gain: The gain for the velocity damper
:returns: tuple of (Ain, Bin) inequality constraint matrices for an optimiser

Expand All @@ -1635,12 +1647,20 @@ def joint_velocity_damper(
Ain = np.zeros((n, n))
Bin = np.zeros(n)

if n == 0:
return Ain, Bin

# qlim follows link order, whereas q and the constraint columns follow
# joint indices. These orders can differ in an explicitly indexed robot.
jindices = [link.jindex for link in self.links if link.isjoint]
qlim = self.qlim[:, np.argsort(jindices)]

for i in range(n):
if q[i] - self.qlim[0, i] <= pi:
Bin[i] = -gain * (((self.qlim[0, i] - q[i]) + ps) / (pi - ps))
if q[i] - qlim[0, i] <= pi:
Bin[i] = -gain * (((qlim[0, i] - q[i]) + ps) / (pi - ps))
Ain[i, i] = -1
if self.qlim[1, i] - q[i] <= pi:
Bin[i] = gain * ((self.qlim[1, i] - q[i]) - ps) / (pi - ps)
if qlim[1, i] - q[i] <= pi:
Bin[i] = gain * ((qlim[1, i] - q[i]) - ps) / (pi - ps)
Ain[i, i] = 1

return Ain, Bin
Expand Down Expand Up @@ -1673,6 +1693,9 @@ def link_collision_damper(
make it impossible for the robot to run into a collision. Requires
See examples/neo.py for use case.

The contact-point Jacobian and the obstacle's translational velocity
are projected onto the separation normal in world coordinates.

"""

end, start, _ = self._get_limit_links(start=start, end=end)
Expand Down Expand Up @@ -1704,22 +1727,19 @@ def indiv_calculation(link: Link, link_col: CollisionShape, q: NDArray) -> Any:
axis=0,
)

# tool = (self.fkine(q, end=link).inv() * SE3(wTlp)).A[:3, 3]

# Je = self.jacob0(q, end=link, tool=tool)
# Je[:3, :] = self._T[:3, :3] @ Je[:3, :]

# n_dim = Je.shape[1]
# dp = norm_h @ shape.v
# l_Ain = zeros((1, self.n))

Je = self.jacobe(q, start=start, end=link, tool=link_col.T)
n_dim = Je.shape[1]
# Transform the link-origin body Jacobian to the closest
# point in world axes, including the contact's lever arm.
# Ignore the robot's end-effector tool for this link pose.
wTl = self.fkine(q, end=link, tool=SE3())
cTl = SE3(wTlp).inv() * wTl
Jlink = self.jacobe(q, start=start, end=link, tool=SE3())
Jc = cTl.Ad() @ Jlink
n_dim = Jc.shape[1]
dp = norm_h @ shape.v
l_Ain = np.zeros((1, self.n))

start_jidx = get_link_joint_index(start)
l_Ain[0, start_jidx : start_jidx + n_dim] = 1 * norm_h @ Je
l_Ain[0, start_jidx : start_jidx + n_dim] = norm_h @ Jc
l_bin = (xi * (d - ds) / (di - ds)) + dp
else: # pragma nocover
l_Ain = None
Expand Down Expand Up @@ -2321,17 +2341,18 @@ def reach(self) -> float:
:rtype: float

A conservative estimate of the reach of the robot. It is computed as
the sum of the translational ETs that define the link transform.
the sum of the translation lengths along the longest branch, including
compound transforms. Base and tool transforms are not included.

Computed on the first access. If kinematic parameters
subsequently change this will not be reflected.

.. rubric:: Notes

- Probably an overestimate of reach
- Used by numerical inverse kinematics to scale translational
error.
- For a prismatic joint, uses ``qlim`` if it is set
- For a prismatic joint, bounds the displacement over ``qlim``,
including its offset and flip. Without limits, only the fixed
offset contributes; unbounded joint travel cannot be estimated.

"""

Expand All @@ -2351,12 +2372,16 @@ def reach(self) -> float:
# joint limits. They might be set in the ET
# or in the Link depending on how the robot
# was constructed
if link.qlim is not None:
d += max(link.qlim)
elif et.qlim is not None: # pragma nocover
d += max(et.qlim)
qlim = link.qlim if link.qlim is not None else et.qlim
if qlim is not None:
travel = -qlim if et.isflip else qlim
d += max(abs(travel + et.offset))
else:
d += abs(cast(float, et.offset))
else:
d += abs(cast(float, et.param))
elif not et.iselementary:
d += float(np.linalg.norm(et.A()[:-1, -1]))
link = link.parent # type: ignore[assignment]
if link is None or isinstance(link, str):
d_all.append(d)
Expand Down
94 changes: 94 additions & 0 deletions tests/test_closest_grippers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""Robot distance queries include independently configured gripper geometry."""

import numpy as np
import pytest
from spatialgeometry import Sphere
from spatialmath import SE3

from roboticstoolbox import ET, Link, Robot
from tests import skip_no_collision_checking

pytestmark = skip_no_collision_checking


def gripper_robot(arm_geometry: bool = True) -> Robot:
arm = Link(ET.Rz(), name="arm", collision=[Sphere(0.1)] if arm_geometry else [])
finger = Link(
ET.tx(1) * ET.tx(), parent=arm, name="finger", collision=[Sphere(0.1)]
)
robot = Robot([arm, finger], gripper_links=finger)
robot.grippers[0].q = [0.2]
return robot


@pytest.mark.parametrize("arm_geometry", [False, True])
@pytest.mark.parametrize("obstacle_x, expected", [(1.6, 0.2), (1.3, -0.1)])
def test_gripper_is_the_closest_collision_body(
arm_geometry: bool, obstacle_x: float, expected: float
) -> None:
robot = gripper_robot(arm_geometry)
obstacle = Sphere(0.1, pose=SE3.Tx(obstacle_x))

distance, robot_point, obstacle_point = robot.closest_point(
np.zeros(1), obstacle, inf_dist=0.5
)

assert distance == pytest.approx(expected)
np.testing.assert_allclose(robot_point, [1.3, 0, 0], atol=1e-9)
np.testing.assert_allclose(obstacle_point, [obstacle_x - 0.1, 0, 0], atol=1e-9)
assert robot.iscollided(np.zeros(1), obstacle) == (expected < 0)


def test_arm_can_remain_closer_than_the_gripper() -> None:
robot = gripper_robot()
obstacle = Sphere(0.1, pose=SE3.Tx(-0.4))

distance, robot_point, obstacle_point = robot.closest_point(
np.zeros(1), obstacle, inf_dist=2
)

assert distance == pytest.approx(0.2)
np.testing.assert_allclose(robot_point, [-0.1, 0, 0], atol=1e-9)
np.testing.assert_allclose(obstacle_point, [-0.3, 0, 0], atol=1e-9)


def test_distance_follows_each_grippers_independent_configuration() -> None:
arm = Link(ET.Rz(), name="arm")
first = Link(ET.tx(), parent=arm, name="first", collision=[Sphere(0.1)])
second = Link(ET.tx(), parent=arm, name="second", collision=[Sphere(0.1)])
robot = Robot([arm, first, second], gripper_links=[first, second])
robot.base = SE3(0.4, -0.2, 0.3) * SE3.Rz(np.pi / 2)
robot.grippers[0].q = [-1.0]
robot.grippers[1].q = [1.2]
obstacle = Sphere(0.1, pose=robot.base * SE3.Tx(1.6))

distance, robot_point, obstacle_point = robot.closest_point(np.zeros(1), obstacle)

assert distance == pytest.approx(0.2)
np.testing.assert_allclose(robot_point, (robot.base * SE3.Tx(1.3)).t, atol=1e-9)
np.testing.assert_allclose(obstacle_point, (robot.base * SE3.Tx(1.5)).t, atol=1e-9)
robot.grippers[1].q = [-1.0]
assert robot.closest_point(np.zeros(1), obstacle) == (None, None, None)
robot.grippers[0].q = [1.4]
assert robot.closest_point(np.zeros(1), obstacle)[0] == pytest.approx(0.0)


def test_skip_uses_the_grippers_existing_world_transforms() -> None:
robot = gripper_robot()
obstacle = Sphere(0.1, pose=SE3.Tx(1.6))
robot.closest_point(np.zeros(1), obstacle)
robot.grippers[0].q = [0.3]

assert robot.closest_point(np.array([np.pi]), obstacle, skip=True)[
0
] == pytest.approx(0.2)
assert robot.closest_point(np.zeros(1), obstacle)[0] == pytest.approx(0.1)


def test_empty_and_distant_gripper_geometry_give_no_result() -> None:
robot = gripper_robot(arm_geometry=False)
obstacle = Sphere(0.1, pose=SE3.Tx(10))
assert robot.closest_point(np.zeros(1), obstacle) == (None, None, None)
robot.grippers[0].links[0].collision = []
obstacle.T = SE3.Tx(1.3)
assert robot.closest_point(np.zeros(1), obstacle) == (None, None, None)
Loading
Loading