__init__.py
Go to the documentation of this file.
1 from copy import deepcopy
2 import os
3 import yaml
4 import string
5 from rospy import logerr, loginfo, get_param
6 import rospkg
7 import genpy
8 import moveit_msgs.msg
9 from trajectory_msgs.msg import JointTrajectoryPoint
10 from sr_robot_msgs.msg import GraspArray
11 
12 
13 def reindent(s, numSpaces):
14  """
15  http://code.activestate.com/recipes/66055-changing-the-indentation-of-a-multi-line-string/
16  """
17  s = string.split(s, '\n')
18  s = [(numSpaces * ' ') + line for line in s]
19  s = string.join(s, '\n')
20  return s
21 
22 
23 class Grasp(moveit_msgs.msg.Grasp):
24  """
25  Represents a single grasp, basically a wrapper around moveit_msgs/Grasp
26  with added functions and Shadow Hand specific knowledge.
27  """
28 
29  def __init__(self):
30  moveit_msgs.msg.Grasp.__init__(self)
31  self.grasp_quality = 0.001
32  self.joint_names = [
33  'FFJ1', 'FFJ2', 'FFJ3', 'FFJ4',
34  'LFJ1', 'LFJ2', 'LFJ3', 'LFJ4', 'LFJ5',
35  'MFJ1', 'MFJ2', 'MFJ3', 'MFJ4',
36  'RFJ1', 'RFJ2', 'RFJ3', 'RFJ4',
37  'THJ1', 'THJ2', 'THJ3', 'THJ4', 'THJ5',
38  'WRJ1', 'WRJ2']
39 
40  # Default the pre grasp to all 0.0
41  zero_joints = dict.fromkeys(self.joint_names, 0.0)
42  self.set_pre_grasp_point(zero_joints)
43 
44  @classmethod
45  def from_msg(cls, msg):
46  """Construct a shadow grasp object from moveit grasp object."""
47  grasp = Grasp()
48  grasp.id = msg.id
49  grasp.pre_grasp_posture = deepcopy(msg.pre_grasp_posture)
50  grasp.grasp_posture = deepcopy(msg.grasp_posture)
51  grasp.grasp_pose = deepcopy(msg.grasp_pose)
52  grasp.grasp_quality = msg.grasp_quality
53  grasp.pre_grasp_approach = deepcopy(msg.pre_grasp_approach)
54  grasp.post_grasp_retreat = deepcopy(msg.post_grasp_retreat)
55  grasp.post_place_retreat = deepcopy(msg.post_place_retreat)
56  grasp.max_contact_force = msg.max_contact_force
57  grasp.allowed_touch_objects = deepcopy(msg.allowed_touch_objects)
58  return grasp
59 
60  def to_msg(self):
61  """Return plain moveit_msgs/Grasp version of self."""
62  raise Exception("TODO - to_msg")
63 
64  @classmethod
65  def from_yaml(self, y):
66  """
67  Construct a shadow grasp object from YAML object. For example YAML
68  grabbed from rostopic to a file.
69  """
70  grasp = Grasp()
71  genpy.message.fill_message_args(grasp, y)
72  return grasp
73 
74  def set_pre_grasp_point(self, *args, **kwargs):
75  """
76  Set the positions for a point (default 0) in the pre-grasp to a dict of
77  joint positions.
78  """
79  self._set_posture_point(self.pre_grasp_posture, *args, **kwargs)
80 
81  def set_grasp_point(self, *args, **kwargs):
82  """
83  Set the positions for a point (default 0) in the grasp to a dict of
84  joint positions.
85  """
86  self._set_posture_point(self.grasp_posture, *args, **kwargs)
87 
88  def _set_posture_point(self, posture, positions, point=0):
89  """Set the posture positions using a dict of joint positions."""
90  # XXX: Why have we been doing this?
91  # posture.header.stamp = now
92  posture.joint_names = positions.keys()
93 
94  # Extend the array to be big enough.
95  if len(posture.points) < point + 1:
96  for _ in range(point + 1):
97  posture.points.append(JointTrajectoryPoint())
98 
99  # Update the point in place
100  jtp = JointTrajectoryPoint()
101  for _, pos in positions.iteritems():
102  jtp.positions.append(pos)
103  posture.points[point] = jtp
104 
105 
106 class GraspStash(object):
107  """
108  Interface to the list of grasps stored in the system. Clients should all
109  use this library so that it can deal with the detail of the undelying
110  storage.
111  """
112 
113  def __init__(self):
114  # Store of all loaded grasps, indexed on grasp.id.
115  self._store = {}
116  rp = rospkg.RosPack()
117  self.grasps_file = get_param('~grasps_file',
118  default=os.path.join(
119  rp.get_path('sr_grasp'), 'resource', 'grasps.yaml'))
120 
121  def get_all(self):
122  """Return list of all grasps."""
123  return self._store.values()
124 
125  def get_grasp_array(self):
126  arr = GraspArray()
127  arr.grasps = self.get_all()
128  return arr
129 
130  def get_grasp(self, grasp_index):
131  """Return a single grasp from the stash from it's id field."""
132  return self._store.get(grasp_index)
133 
134  def get_grasp_at(self, idx):
135  """Return the Grasp at the given index."""
136  return self.get_all()[idx]
137 
138  def size(self):
139  """Return the number of grasps."""
140  return len(self._store)
141 
142  def put_grasp(self, grasp):
143  """Stash the given grasp, using it's id field, which must be set."""
144  if grasp.id is None or grasp.id == "":
145  raise Exception("Grasp has no id")
146  # Up convert a plain grasp msg to our wrapper
147  # if isinstance(grasp, moveit_msgs.msg.Grasp):
148  # grasp = Grasp.from_msg(grasp)
149  self._store[grasp.id] = grasp
150 
151  def load_all(self):
152  """Load all configured sources of grasps into the stash."""
153  self.load_yaml_file(self.grasps_file)
154 
155  def as_yaml(self):
156  return genpy.message.strify_message(self.get_grasp_array().grasps)
157 
158  def load_yaml_file(self, fname):
159  """Load a set of grasps from a YAML file."""
160  try:
161  data = yaml.load(file(fname))
162  self.load_yaml(data)
163  except Exception as e:
164  logerr("Failed to load YAML grasp file: %s error:%s" % (fname, e))
165  return False
166  else:
167  loginfo("Loaded grasps from file: %s" % (fname))
168  return True
169 
170  def load_yaml(self, data):
171  """Load a set of grasps from a YAML object. Throws exceptions on errors."""
172  for g in data:
173  grasp = Grasp.from_yaml(g)
174  self.put_grasp(grasp)
175 
176  def save_yaml_file(self, fname=""):
177  if fname == "":
178  fname = self.grasps_file
179  with open(fname, "w") as txtfile:
180  txtfile.write(self.as_yaml())
def __init__(self)
Definition: __init__.py:29
def set_pre_grasp_point(self, args, kwargs)
Definition: __init__.py:74
def set_grasp_point(self, args, kwargs)
Definition: __init__.py:81
def reindent(s, numSpaces)
Definition: __init__.py:13
def to_msg(self)
Definition: __init__.py:60
def put_grasp(self, grasp)
Definition: __init__.py:142
def get_grasp(self, grasp_index)
Definition: __init__.py:130
def get_grasp_array(self)
Definition: __init__.py:125
def size(self)
Definition: __init__.py:138
def load_yaml(self, data)
Definition: __init__.py:170
def get_all(self)
Definition: __init__.py:121
def load_yaml_file(self, fname)
Definition: __init__.py:158
def save_yaml_file(self, fname="")
Definition: __init__.py:176
def from_yaml(self, y)
Definition: __init__.py:65
def __init__(self)
Definition: __init__.py:113
def from_msg(cls, msg)
Definition: __init__.py:45
def _set_posture_point(self, posture, positions, point=0)
Definition: __init__.py:88
def as_yaml(self)
Definition: __init__.py:155
def get_grasp_at(self, idx)
Definition: __init__.py:134
def load_all(self)
Definition: __init__.py:151


sr_grasp
Author(s): Mark Pitchless
autogenerated on Wed Oct 14 2020 04:05:13