robot.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2013, Willow Garage, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Willow Garage, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 #
33 # Author: Ioan Sucan
34 
35 from moveit_commander import MoveGroupCommander
36 from .exception import MoveItCommanderException
37 from moveit_ros_planning_interface import _moveit_robot_interface
38 from moveit_msgs.msg import RobotState
39 from visualization_msgs.msg import MarkerArray
40 import moveit_commander.conversions as conversions
41 
42 
43 class RobotCommander(object):
44  class Joint(object):
45  def __init__(self, robot, name):
46  self._robot = robot
47  self._name = name
48 
49  def name(self):
50  return self._name
51 
52  def variable_count(self):
53  """
54  @return number of the list that _Joint__get_joint_limits
55  methods returns.
56  @see: http://docs.ros.org/indigo/api/moveit_core/html/classmoveit_1_1core_1_1JointModel.html#details
57  for more about variable.
58  """
59  return len(self.__get_joint_limits())
60 
61  def bounds(self):
62  """
63  @return: Either a single list of min and max joint limits, or
64  a set of those lists, depending on the number of variables
65  available in this joint.
66  """
67  l = self.__get_joint_limits()
68  if len(l) == 1:
69  return l[0]
70  else:
71  return l
72 
73  def min_bound(self):
74  """
75  @return: Either a single min joint limit value, or
76  a set of min values, depending on the number of variables
77  available in this joint.
78  """
79  limits = self.__get_joint_limits()
80  if len(limits) == 1:
81  return limits[0][0]
82  else:
83  return [l[0] for l in limits]
84 
85  def max_bound(self):
86  """
87  @return: Either a single max joint limit value, or
88  a set of max values, depending on the number of variables
89  available in this joint.
90  """
91  limits = self.__get_joint_limits()
92  if len(limits) == 1:
93  return limits[0][1]
94  else:
95  return [l[1] for l in limits]
96 
97  def value(self):
98  """
99  @rtype float
100 
101  (Editor's comment by @130s) I doubt there's a case where this method goes into
102  "else" block, because get_current_joint_values always return a single list.
103 
104  cf. getCurrentJointValues https://github.com/moveit/moveit_ros/blob/8e819dda2b19462b8d0c5aacc69706c8a9d8d883/planning_interface/robot_interface/src/wrap_python_robot_interface.cpp#L176
105  """
106  vals = self._robot._r.get_current_joint_values(self._name)
107  if len(vals) == 1:
108  return vals[0]
109  else:
110  return vals
111 
112  def move(self, position, wait=True):
113  """
114  @param position [float]: List of joint angles to achieve.
115  @param wait bool: If false, the commands gets operated asynchronously.
116  """
117  group = self._robot.get_default_owner_group(self.name())
118  if group is None:
120  "There is no known group containing joint %s. Cannot move."
121  % self._name
122  )
123  gc = self._robot.get_group(group)
124  if gc is not None:
125  gc.set_joint_value_target(gc.get_current_joint_values())
126  gc.set_joint_value_target(self._name, position)
127  return gc.go(wait)
128  return False
129 
131  """
132  @return: A list of length of 2 that contains max and min positional
133  limits of the specified joint.
134  """
135  return self._robot._r.get_joint_limits(self._name)
136 
137  class Link(object):
138  def __init__(self, robot, name):
139  self._robot = robot
140  self._name = name
141 
142  def name(self):
143  return self._name
144 
145  def pose(self):
146  """
147  @rtype: geometry_msgs.Pose
148  """
149  return conversions.list_to_pose_stamped(
150  self._robot._r.get_link_pose(self._name),
151  self._robot.get_planning_frame(),
152  )
153 
154  def __init__(self, robot_description="robot_description", ns=""):
155  self._robot_description = robot_description
156  self._ns = ns
157  self._r = _moveit_robot_interface.RobotInterface(robot_description, ns)
158  self._groups = {}
160 
162  """
163  Get the frame of reference in which planning is done (and environment
164  is maintained)
165  """
166  return self._r.get_planning_frame()
167 
168  def get_robot_markers(self, *args):
169  """Get a MarkerArray of the markers that make up this robot
170 
171  Usage:
172  (): get's all markers for current state
173  state (RobotState): gets markers for a particular state
174  values (dict): get markers with given values
175  values, links (dict, list): get markers with given values and these links
176  group (string): get all markers for a group
177  group, values (string, dict): get all markers for a group with desired values
178  """
179  mrkr = MarkerArray()
180  if not args:
181  conversions.msg_from_string(mrkr, self._r.get_robot_markers())
182  else:
183  if isinstance(args[0], RobotState):
184  msg_str = conversions.msg_to_string(args[0])
185  conversions.msg_from_string(mrkr, self._r.get_robot_markers(msg_str))
186  elif isinstance(args[0], dict):
187  conversions.msg_from_string(mrkr, self._r.get_robot_markers(*args))
188  elif isinstance(args[0], str):
189  conversions.msg_from_string(mrkr, self._r.get_group_markers(*args))
190  else:
191  raise MoveItCommanderException("Unexpected type")
192  return mrkr
193 
194  def get_root_link(self):
195  """Get the name of the root link of the robot model"""
196  return self._r.get_robot_root_link()
197 
198  def get_active_joint_names(self, group=None):
199  """
200  Get the names of all the movable joints that make up a group.
201  If no group name is specified, all joints in the robot model are returned.
202  Excludes fixed and mimic joints.
203  """
204  if group is not None:
205  if self.has_group(group):
206  return self._r.get_group_active_joint_names(group)
207  else:
208  raise MoveItCommanderException("There is no group named %s" % group)
209  else:
210  return self._r.get_active_joint_names()
211 
212  def get_joint_names(self, group=None):
213  """
214  Get the names of all the movable joints that make up a group.
215  If no group name is specified, all joints in the robot model are returned.
216  Includes fixed and mimic joints.
217  """
218  if group is not None:
219  if self.has_group(group):
220  return self._r.get_group_joint_names(group)
221  else:
222  raise MoveItCommanderException("There is no group named %s" % group)
223  else:
224  return self._r.get_joint_names()
225 
226  def get_link_names(self, group=None):
227  """
228  Get the links that make up a group. If no group name is specified,
229  all the links in the robot model are returned.
230  """
231  if group is not None:
232  if self.has_group(group):
233  return self._r.get_group_link_names(group)
234  else:
235  raise MoveItCommanderException("There is no group named %s" % group)
236  else:
237  return self._r.get_link_names()
238 
239  def get_group_names(self):
240  """Get the names of the groups defined for the robot"""
241  return self._r.get_group_names()
242 
243  def get_current_state(self):
244  """Get a RobotState message describing the current state of the robot"""
245  s = RobotState()
246  s.deserialize(self._r.get_current_state())
247  return s
248 
250  """
251  Get a dictionary mapping variable names to values.
252  Note that a joint may consist of one or more variables.
253  """
254  return self._r.get_current_variable_values()
255 
256  def get_joint(self, name):
257  """
258  @param name str: Name of movegroup
259  @rtype: moveit_commander.robot.Joint
260  @raise exception: MoveItCommanderException
261  """
262  if name in self.get_joint_names():
263  return self.Joint(self, name)
264  else:
265  raise MoveItCommanderException("There is no joint named %s" % name)
266 
267  def get_link(self, name):
268  """
269  @param name str: Name of movegroup
270  @rtype: moveit_commander.robot.Link
271  @raise exception: MoveItCommanderException
272  """
273  if name in self.get_link_names():
274  return self.Link(self, name)
275  else:
276  raise MoveItCommanderException("There is no link named %s" % name)
277 
278  def get_group(self, name):
279  """
280  @param name str: Name of movegroup
281  @rtype: moveit_commander.MoveGroupCommander
282  """
283  if not name in self._groups:
284  if not self.has_group(name):
285  raise MoveItCommanderException("There is no group named %s" % name)
286  self._groups[name] = MoveGroupCommander(
287  name, self._robot_description, self._ns
288  )
289  return self._groups[name]
290 
291  def has_group(self, name):
292  """
293  @param name str: Name of movegroup
294  @rtype: bool
295  """
296  return self._r.has_group(name)
297 
298  def get_default_owner_group(self, joint_name):
299  """
300  Get the name of the smallest group (fewest joints) that includes
301  the joint name specified as argument.
302  """
303  if not joint_name in self._joint_owner_groups:
304  group = None
305  for g in self.get_group_names():
306  if joint_name in self.get_joint_names(g):
307  if group is None:
308  group = g
309  else:
310  if len(self.get_link_names(g)) < len(
311  self.get_link_names(group)
312  ):
313  group = g
314  self._joint_owner_groups[joint_name] = group
315  return self._joint_owner_groups[joint_name]
316 
317  def __getattr__(self, name):
318  """
319  We catch the names of groups, joints and links to allow easy access
320  to their properties.
321  """
322  if name in self.get_group_names():
323  return self.get_group(name)
324  elif name in self.get_joint_names():
325  return self.Joint(self, name)
326  elif name in self.get_link_names():
327  return self.Link(self, name)
328  else:
329  return object.__getattribute__(self, name)
moveit_commander.robot.RobotCommander._groups
_groups
Definition: robot.py:158
moveit_commander.robot.RobotCommander.Joint.__init__
def __init__(self, robot, name)
Definition: robot.py:45
moveit_commander.robot.RobotCommander.get_link_names
def get_link_names(self, group=None)
Definition: robot.py:226
moveit_commander.robot.RobotCommander.get_root_link
def get_root_link(self)
Definition: robot.py:194
moveit_commander.move_group.MoveGroupCommander
Definition: move_group.py:57
moveit_commander.robot.RobotCommander.get_group
def get_group(self, name)
Definition: robot.py:278
moveit_commander.robot.RobotCommander.get_robot_markers
def get_robot_markers(self, *args)
Definition: robot.py:168
moveit_commander.robot.RobotCommander.Joint.variable_count
def variable_count(self)
Definition: robot.py:52
moveit_commander.robot.RobotCommander._ns
_ns
Definition: robot.py:156
moveit_commander.robot.RobotCommander.Joint.value
def value(self)
Definition: robot.py:97
moveit_commander.robot.RobotCommander.get_group_names
def get_group_names(self)
Definition: robot.py:239
moveit_commander.robot.RobotCommander.has_group
def has_group(self, name)
Definition: robot.py:291
moveit_commander.robot.RobotCommander.Joint.name
def name(self)
Definition: robot.py:49
moveit_commander.robot.RobotCommander.Joint._robot
_robot
Definition: robot.py:46
moveit_commander.exception.MoveItCommanderException
Definition: exception.py:36
moveit_commander.robot.RobotCommander._robot_description
_robot_description
Definition: robot.py:155
moveit_commander.robot.RobotCommander.get_current_variable_values
def get_current_variable_values(self)
Definition: robot.py:249
moveit_commander.robot.RobotCommander._joint_owner_groups
_joint_owner_groups
Definition: robot.py:159
moveit_commander.robot.RobotCommander.get_active_joint_names
def get_active_joint_names(self, group=None)
Definition: robot.py:198
moveit_commander.robot.RobotCommander.Joint.bounds
def bounds(self)
Definition: robot.py:61
moveit_commander.robot.RobotCommander.get_planning_frame
def get_planning_frame(self)
Definition: robot.py:161
moveit_commander.robot.RobotCommander.Joint.__get_joint_limits
def __get_joint_limits(self)
Definition: robot.py:130
moveit_commander.robot.RobotCommander.Joint._name
_name
Definition: robot.py:47
moveit_commander.robot.RobotCommander.__getattr__
def __getattr__(self, name)
Definition: robot.py:317
moveit_commander.conversions
Definition: conversions.py:1
moveit_commander.robot.RobotCommander._r
_r
Definition: robot.py:157
moveit_commander.robot.RobotCommander.Joint.min_bound
def min_bound(self)
Definition: robot.py:73
moveit_commander.robot.RobotCommander.Joint.max_bound
def max_bound(self)
Definition: robot.py:85
moveit_commander.robot.RobotCommander.Joint.move
def move(self, position, wait=True)
Definition: robot.py:112
moveit_commander.robot.RobotCommander.get_joint_names
def get_joint_names(self, group=None)
Definition: robot.py:212
moveit_commander.robot.RobotCommander.get_current_state
def get_current_state(self)
Definition: robot.py:243
moveit_commander.robot.RobotCommander.get_link
def get_link(self, name)
Definition: robot.py:267
moveit_commander.robot.RobotCommander.get_default_owner_group
def get_default_owner_group(self, joint_name)
Definition: robot.py:298
moveit_commander.robot.RobotCommander.Joint
Definition: robot.py:44
moveit_commander.robot.RobotCommander.__init__
def __init__(self, robot_description="robot_description", ns="")
Definition: robot.py:154
moveit_commander.robot.RobotCommander
Definition: robot.py:43
moveit_commander.robot.RobotCommander.get_joint
def get_joint(self, name)
Definition: robot.py:256


moveit_commander
Author(s): Ioan Sucan
autogenerated on Thu Nov 21 2024 03:25:17