diff --git a/src/roboticstoolbox/robot/BaseRobot.py b/src/roboticstoolbox/robot/BaseRobot.py index 6971a362c..0b2caa7c8 100644 --- a/src/roboticstoolbox/robot/BaseRobot.py +++ b/src/roboticstoolbox/robot/BaseRobot.py @@ -1567,19 +1567,19 @@ def copy(self) -> Any: return deepcopy(self) def __deepcopy__(self, memo: Any) -> Any: - links = [] + # All links share one memo so parent references point to the same copies. + links = [deepcopy(link, memo) for link in self.links] + gripper_links = [] + for gripper in self.grippers: + gripper_links.append(deepcopy(gripper.links[0], memo)) + links.extend(deepcopy(link, memo) for link in gripper.links) - # if isinstance(self, rtb.DHRobot): - # cls = rtb.DHRobot cls: type[BaseRobot[Any]] if isinstance(self, rtb.Robot2): cls = rtb.Robot2 else: cls = rtb.Robot - for link in self.links: - links.append(deepcopy(link)) - name = deepcopy(self.name) manufacturer = deepcopy(self.manufacturer) comment = deepcopy(self.comment) @@ -1592,6 +1592,7 @@ def __deepcopy__(self, memo: Any) -> Any: result = cls( links, + gripper_links=gripper_links, name=name, manufacturer=manufacturer, comment=comment, @@ -1603,6 +1604,12 @@ def __deepcopy__(self, memo: Any) -> Any: configs=configs, ) + # Gripper constructor recreates joint indices and defaults; restore + # its original tool and configuration without sharing mutable state. + for original, cloned in zip(self.grippers, result.grippers): + cloned.tool = deepcopy(original.tool, memo) + cloned.q = deepcopy(original.q, memo) + # if a configuration was an attribute of original robot, make it an # attribute of the deep copy for config in configs: diff --git a/src/roboticstoolbox/robot/Dynamics.py b/src/roboticstoolbox/robot/Dynamics.py index dd6ca6950..858fd527e 100644 --- a/src/roboticstoolbox/robot/Dynamics.py +++ b/src/roboticstoolbox/robot/Dynamics.py @@ -191,8 +191,9 @@ def nofriction(self: RobotProto, coulomb: bool = True, viscous: bool = False) -> nf = self.copy() nf.name = "NF/" + self.name - # add the modified links (copies) - nf._links = [link.nofriction(coulomb, viscous) for link in self.links] + # Modify the copied links without replacing them or breaking topology. + for link in nf.links: + link.nofriction(coulomb, viscous, inplace=True) return nf diff --git a/src/roboticstoolbox/robot/Link.py b/src/roboticstoolbox/robot/Link.py index a41d7b929..7171366c0 100644 --- a/src/roboticstoolbox/robot/Link.py +++ b/src/roboticstoolbox/robot/Link.py @@ -419,20 +419,21 @@ def _copy(self) -> None: raise DeprecationWarning("Use copy method of Link class") def __deepcopy__(self, memo: Any) -> Any: - ets = deepcopy(self.ets) - name = deepcopy(self.name) - parent = self.parent - joint_name = deepcopy(self._joint_name) - m = deepcopy(self.m) - r = deepcopy(self.r) - I = deepcopy(self.I) - Jm = deepcopy(self.Jm) - B = deepcopy(self.B) - Tc = deepcopy(self.Tc) - G = deepcopy(self.G) - qlim = deepcopy(self.qlim) - qdlim = deepcopy(self.qdlim) - tlim = deepcopy(self.tlim) + ets = deepcopy(self.ets, memo) + name = deepcopy(self.name, memo) + # A detached link can still refer to its parent by name. + parent = self._parent_name if self.parent is None else None + joint_name = deepcopy(self._joint_name, memo) + m = deepcopy(self.m, memo) + r = deepcopy(self.r, memo) + I = deepcopy(self.I, memo) + Jm = deepcopy(self.Jm, memo) + B = deepcopy(self.B, memo) + Tc = deepcopy(self.Tc, memo) + G = deepcopy(self.G, memo) + qlim = deepcopy(self.qlim, memo) + qdlim = deepcopy(self.qdlim, memo) + tlim = deepcopy(self.tlim, memo) geometry = _copy_shapes(self._geometry, self.name) collision = _copy_shapes(self._collision, self.name) @@ -456,12 +457,11 @@ def __deepcopy__(self, memo: Any) -> Any: collision=collision, ) - if self._children: - result._children = self._children.copy() - - result._robot = self.robot - + # Register first: a link's parent and children form a graph, not a tree. memo[id(self)] = result + if self.parent is not None: + result.parent = deepcopy(self.parent, memo) + # Robot construction rebuilds the children and owning robot references. return result # -------------------------------------------------------------------------- # @@ -1416,16 +1416,20 @@ def islimit(self, q: float) -> Any: else: return q < self.qlim[0] or q > self.qlim[1] - def nofriction(self, coulomb: bool = True, viscous: bool = False) -> Any: + def nofriction( + self, coulomb: bool = True, viscous: bool = False, *, inplace: bool = False + ) -> Any: """ - Clone link without friction + Return a link with selected friction terms removed :param coulomb: if True, will set the Coulomb friction to 0 :param viscous: if True, will set the viscous friction to 0 + :param inplace: if True, update this link instead of creating a copy - ``link.nofriction()`` is a copy of the link instance with the same - parameters except, the Coulomb and/or viscous friction parameters are - set to zero. + By default, ``link.nofriction()`` returns a copy with the specified + friction parameters set to zero. With ``inplace=True``, only those + parameters are changed on this link, preserving its identity and + relationships to other links. .. rubric:: Notes @@ -1434,8 +1438,7 @@ def nofriction(self, coulomb: bool = True, viscous: bool = False) -> Any: """ - # Copy the Link - link = self.copy() + link = self if inplace else self.copy() if viscous: link.B = 0.0 diff --git a/tests/test_BaseRobot.py b/tests/test_BaseRobot.py index d610c12d2..c519ad2c3 100644 --- a/tests/test_BaseRobot.py +++ b/tests/test_BaseRobot.py @@ -601,6 +601,79 @@ def test_copy3(self): self.assertTrue(r2.links[0] != r.links[0]) + def test_copy_preserves_parent_graph_and_dynamics(self): + # A fixed link between two joints exercises the parent references + # used by both the forward and backward RNE recursions. + base = Link(ET.Rz(), name="joint0", m=1.0, r=[0.1, 0, 0], I=[0.02] * 3) + fixed = Link( + ET.tx(0.25), name="fixed", parent=base, + m=0.3, r=[0.05, 0, 0], I=[0.01] * 3, + ) + tip = Link( + ET.Ry(), name="joint1", parent=fixed, + m=0.6, r=[0.1, 0, 0], I=[0.02] * 3, + ) + robot = Robot([base, fixed, tip]) + q = np.array([0.2, -0.3]) + qd = np.array([0.1, 0.2]) + qdd = np.array([0.3, -0.1]) + + expected_tau = robot.rne(q, qd, qdd) + clone = robot.copy() + + self.assertIsNot(clone.links[0], robot.links[0]) + self.assertIsNone(clone.links[0].parent) + self.assertIs(clone.links[1].parent, clone.links[0]) + self.assertIs(clone.links[2].parent, clone.links[1]) + self.assertEqual(clone.links[0]._children, [clone.links[1]]) + self.assertEqual(clone.links[1]._children, [clone.links[2]]) + self.assertEqual(robot.links[0]._children, [robot.links[1]]) + self.assertEqual(robot.links[1]._children, [robot.links[2]]) + nt.assert_allclose(clone.rne(q, qd, qdd), expected_tau) + + frictionless = robot.nofriction(coulomb=True, viscous=True) + self.assertIs(frictionless.links[1].parent, frictionless.links[0]) + self.assertIs(frictionless.links[2].parent, frictionless.links[1]) + nt.assert_allclose(frictionless.rne(q, qd, qdd), expected_tau) + nt.assert_allclose(clone.coriolis(q, qd), robot.coriolis(q, qd)) + + def test_nofriction_preserves_link_identity_and_source(self): + base = Link(ET.Rz(), name="base", B=0.3, Tc=[1, -1]) + tip = Link(ET.Ry(), name="tip", parent=base, B=0.2, Tc=[2, -2]) + robot = Robot([base, tip]) + + frictionless = robot.nofriction(coulomb=True, viscous=True) + + self.assertIsNot(frictionless.links[0], robot.links[0]) + self.assertIsNot(frictionless.links[1], robot.links[1]) + self.assertIs(frictionless.links[1].parent, frictionless.links[0]) + self.assertEqual(frictionless.links[0]._children, [frictionless.links[1]]) + for original, modified in zip(robot.links, frictionless.links): + self.assertEqual(modified.B, 0.0) + nt.assert_array_equal(modified.Tc, [0.0, 0.0]) + self.assertNotEqual(original.B, 0.0) + self.assertTrue(np.any(original.Tc)) + + def test_copy_preserves_gripper_links_and_fkine(self): + robot = rtb.models.Panda() + self.assertGreater(len(robot.grippers), 0) + gripper = robot.grippers[0] + gripper.q = np.full(gripper.n, 0.01) + gripper.tool = SE3(0, 0, 0.12) + + copied = robot.copy() + self.assertEqual(len(copied.grippers), len(robot.grippers)) + self.assertEqual(copied.grippers[0].name, gripper.name) + self.assertIsNot(copied.grippers[0], gripper) + self.assertIsNot(copied.grippers[0].links[0], gripper.links[0]) + self.assertIs(copied.grippers[0].links[0].parent, copied.ee_links[0]) + nt.assert_allclose(copied.grippers[0].q, gripper.q) + nt.assert_allclose(copied.grippers[0].tool.A, gripper.tool.A) + nt.assert_allclose(copied.fkine(robot.qr).A, robot.fkine(robot.qr).A) + + copied.grippers[0].q[:] = 0 + self.assertTrue(np.all(gripper.q == 0.01)) + def test_toradians(self): r = rtb.models.Panda() diff --git a/tests/test_Link.py b/tests/test_Link.py index 1b8770b0f..bda1bc7cb 100644 --- a/tests/test_Link.py +++ b/tests/test_Link.py @@ -102,6 +102,19 @@ def test_nofriction(self): nt.assert_array_almost_equal(n2.B, l0.B) nt.assert_array_almost_equal(n2.Tc, l0.Tc) + def test_nofriction_inplace(self): + parent = rp.Link(rp.ET.Rz(), name="parent", B=4.0, Tc=[1, -1]) + child = rp.Link(rp.ET.Rz(), name="child", parent=parent, B=3.0, Tc=[2, -2]) + + result = child.nofriction(viscous=True, inplace=True) + + self.assertIs(result, child) + self.assertIs(child.parent, parent) + nt.assert_array_equal(child.Tc, [0.0, 0.0]) + self.assertEqual(child.B, 0.0) + nt.assert_array_equal(parent.Tc, [1, -1]) + self.assertEqual(parent.B, 4.0) + def test_add(self): l0 = rp.DHLink() l1 = rp.DHLink()