8000 allow link key to be a str · ctc-eng/robotics-toolbox-python@eeadff3 · GitHub
[go: up one dir, main page]

Skip to content

Commit eeadff3

Browse files
committed
allow link key to be a str
1 parent d7f8ef4 commit eeadff3

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

roboticstoolbox/robot/Robot.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ def __getitem__(self, i):
126126
"""
127127
Get link (Robot superclass)
128128
129-
:param i: link number
130-
:type i: int
131-
:return: i'th link of robot
129+
:param i: link number or name
130+
:type i: int or str
131+
:return: i'th link or named link
132132
:rtype: Link subclass
133133
134134
This also supports iterating over each link in the robot object,
@@ -141,8 +141,18 @@ def __getitem__(self, i):
141141
>>> print(robot[1]) # print the 2nd link
142142
>>> print([link.a for link in robot]) # print all the a_j values
143143
144+
.. note:: ``ERobot`` supports link lookup by name,
145+
eg. ``robot['link1']``
144146
"""
145-
return self._links[i]
147+
if isinstance(i, str):
148+
try:
149+
return self.link_dict[i]
150+
except KeyError:
151+
raise KeyError(f"link {i} not in link dictionary")
152+
except AttributeError:
153+
raise AttributeError(f"robot has no link dictionary")
154+
else:
155+
return self._links[i]
146156

147157
# URDF Parser Attempt
148158
# @staticmethod

0 commit comments

Comments
 (0)
0