sr_hand_commander.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright 2015 Shadow Robot Company Ltd.
4 #
5 # This program is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation version 2 of the License.
8 #
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 # more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 import rospy
18 
19 from sr_robot_commander import SrRobotCommander, SrRobotCommanderException
20 from sr_robot_msgs.srv import ForceController
21 from sr_hand.tactile_receiver import TactileReceiver
22 from sr_utilities.hand_finder import HandFinder
23 from sys import exit
24 
25 
26 class SrHandCommander(SrRobotCommander):
27  """
28  Commander class for hand
29  """
30 
31  __set_force_srv = {}
32 
33  def __init__(self, name=None, prefix=None, hand_parameters=None, hand_serial=None, hand_number=0):
34  """
35  Initialize the hand commander, using either a name + prefix, or the parameters returned by the hand finder.
36  @param name - name of the MoveIt group
37  @param prefix - prefix used for the tactiles and max_force
38  @param hand_parameters - from hand_finder.get_hand_parameters(). Will overwrite the name and prefix
39  @param hand_serial - which hand are you using
40  @param hand_number - which hand in a multi-hand system to use (starting at 0 and sorted by name/serial).
41  """
42 
43  self._hand_h = False
44  if name is None and prefix is None and hand_parameters is None and hand_serial is None:
45  hand_finder = HandFinder()
46  if hand_finder.hand_e_available():
47  name, prefix, hand_serial = hand_finder.get_hand_e(number=hand_number)
48  elif hand_finder.hand_h_available():
49  self._hand_h = True
50  name, prefix, hand_serial = hand_finder.get_hand_h(number=hand_number)
51  else:
52  rospy.logfatal("No hands found and no information given to define hand commander.")
53  raise SrRobotCommanderException("No hand found.")
54 
55  elif hand_parameters is not None:
56  # extracting the name and prefix from the hand finder parameters
57  if len(hand_parameters.mapping) is 0:
58  rospy.logfatal("No hand detected")
59  raise SrRobotCommanderException("No hand found.")
60 
61  hand_mapping = hand_parameters.mapping[hand_serial]
62  prefix = hand_parameters.joint_prefix[hand_serial]
63 
64  if name is None:
65  if hand_mapping == 'rh':
66  name = "right_hand"
67  else:
68  name = "left_hand"
69  else:
70  if name is None:
71  name = "right_hand"
72  if prefix is None:
73  prefix = "rh_"
74 
75  super(SrHandCommander, self).__init__(name)
76 
77  if not self._hand_h:
78  self._tactiles = TactileReceiver(prefix)
79 
80  # appends trailing slash if necessary
81  self._topic_prefix = prefix
82  if self._topic_prefix and self._topic_prefix.endswith("_"):
83  self._topic_prefix = self._topic_prefix[:-1] # Remove trailing _
84 
85  if self._topic_prefix and not self._topic_prefix.endswith("/"):
86  self._topic_prefix += "/"
87 
88  self._hand_serial = hand_serial
89 
90  def get_hand_serial(self):
91  return self._hand_serial
92 
93  def get_joints_effort(self):
94  """
95  Returns joints effort
96  @return - dictionary with joints efforts
97  """
98  return self._get_joints_effort()
99 
100  def set_max_force(self, joint_name, value):
101  """
102  Set maximum force for hand
103  @param value - maximum force value
104  """
105  joint_name = self._strip_prefix(joint_name)
106 
107  if not self.__set_force_srv.get(joint_name):
108  service_name = "sr_hand_robot/" + self._topic_prefix + \
109  "change_force_PID_"+joint_name.upper()
110  self.__set_force_srv[joint_name] = \
111  rospy.ServiceProxy(service_name,
112  ForceController)
113 
114  # get the current settings for the motor
115  motor_settings = None
116  try:
117  motor_settings = rospy.get_param(self._topic_prefix +
118  joint_name.lower() + "/pid")
119  except KeyError, e:
120  rospy.logerr("Couldn't get the motor parameters for joint " +
121  joint_name + " -> " + str(e))
122 
123  motor_settings["torque_limit"] = value
124 
125  try:
126  # reorder parameters in the expected order since names don't match:
127  self.__set_force_srv[joint_name](motor_settings["max_pwm"],
128  motor_settings["sg_left"],
129  motor_settings["sg_right"],
130  motor_settings["f"],
131  motor_settings["p"],
132  motor_settings["i"],
133  motor_settings["d"],
134  motor_settings["imax"],
135  motor_settings["deadband"],
136  motor_settings["sign"],
137  motor_settings["torque_limit"],
138  motor_settings["torque_limiter_gain"])
139  except rospy.ServiceException, e:
140  rospy.logerr("Couldn't set the max force for joint " +
141  joint_name + ": " + str(e))
142 
143  def get_tactile_type(self):
144  """
145  Returns a string indicating the type of tactile sensors present.
146  Possible values are: PST, biotac, UBI0.
147  """
148  return self._tactiles.get_tactile_type()
149 
150  def get_tactile_state(self):
151  """
152  Returns an object containing tactile data. The structure of the
153  data is different for every tactile_type.
154  """
155  return self._tactiles.get_tactile_state()
156 
157  def _strip_prefix(self, joint_name):
158  """
159  Strips the prefix from the joint name (e.g. rh_ffj3 -> ffj3) if present, returns the joint name otherwise.
160 
161  We know that all joint names for the shadow hands are 4 char long. So we only keep the last 4 chars.
162 
163  @param joint_name the joint name
164  @return stripped joint name
165  """
166  return joint_name[-4:]
167 
168  def attach_object(self, object_name):
169  self._move_group_commander.attach_object(object_name)
170 
171  def detach_object(self, object_name):
172  self._move_group_commander.detach_object(object_name)
def __init__(self, name=None, prefix=None, hand_parameters=None, hand_serial=None, hand_number=0)


sr_robot_commander
Author(s): Andriy Petlovanyy
autogenerated on Wed Oct 14 2020 04:05:30