Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 import select
00032 import sys
00033 import termios
00034 from threading import Lock
00035 import tty
00036
00037 import actionlib
00038 from control_msgs.msg import GripperCommandAction
00039 from control_msgs.msg import GripperCommandGoal
00040 import rospy
00041 from sensor_msgs.msg import JointState
00042
00043
00044 def getch():
00045 tty.setraw(sys.stdin.fileno())
00046 select.select([sys.stdin], [], [], 0)
00047 key = sys.stdin.read(1)
00048 termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
00049 return key
00050
00051
00052 class GripperKeyboard(object):
00053
00054 OPEN_POSITION = 0.1
00055 CLOSED_POSITION = 0
00056
00057 def __init__(self):
00058 self._lock = Lock()
00059
00060 self.max_effort = 20.0
00061 self.position = None
00062 self._sub_pos = rospy.Subscriber('joint_states', JointState,
00063 self._set_state)
00064
00065 self.action_name = 'gripper_controller/gripper_action'
00066 self.client = actionlib.SimpleActionClient(self.action_name,
00067 GripperCommandAction)
00068 rospy.loginfo('Waiting for gripper_controller...')
00069 self.client.wait_for_server()
00070 rospy.loginfo('...connected.')
00071
00072 def _set_state(self, joint_state):
00073 l_gripper_position = None
00074 r_gripper_position = None
00075 for joint, pos in zip(joint_state.name, joint_state.position):
00076 if joint == 'l_gripper_finger_joint':
00077 l_gripper_finger_pos = pos
00078 if joint == 'r_gripper_finger_joint':
00079 r_gripper_finger_pos = pos
00080 with self._lock:
00081 self.position = l_gripper_finger_pos + r_gripper_finger_pos
00082
00083 def set_position(self, position):
00084 goal = GripperCommandGoal()
00085 goal.command.max_effort = self.max_effort
00086 goal.command.position = position
00087
00088 self.client.send_goal(goal)
00089 self.client.wait_for_result(rospy.Duration.from_sec(5.0))
00090 res = self.client.get_result()
00091 with self._lock:
00092 self.position = res.position
00093
00094 def open(self):
00095 self.set_position(self.OPEN_POSITION)
00096
00097 def close(self):
00098 self.set_position(self.CLOSED_POSITION)
00099
00100
00101 if __name__ == '__main__':
00102 settings = termios.tcgetattr(sys.stdin)
00103
00104 rospy.init_node('grippper_keyboard')
00105
00106 gripper_keyboard = GripperKeyboard()
00107
00108 gripper_bindings = {
00109 'o': 'Open gripper',
00110 'c': 'Close gripper',
00111 'E': 'Increase max_effort',
00112 'e': 'Decrease max_effort',
00113 's': 'Show status',
00114 '?': 'Show help',
00115 }
00116 usage = 'Usage: '
00117 usage += ''.join('\n {}: {}'.format(k, v)
00118 for k, v in gripper_bindings.items())
00119 usage += '\n Ctrl-C to quit'
00120
00121 try:
00122 print(usage)
00123 while True:
00124 c = getch()
00125 if c.lower() in gripper_bindings:
00126 if c.lower() == 'o':
00127 gripper_keyboard.open()
00128 rospy.loginfo('Opened gripper')
00129 elif c.lower() == 'c':
00130 gripper_keyboard.close()
00131 rospy.loginfo('Closed gripper')
00132 elif c.lower() == 's':
00133 rospy.loginfo('''State:
00134 position: {}
00135 max_effort: {}'''.format(gripper_keyboard.position,
00136 gripper_keyboard.max_effort))
00137 elif c == 'e':
00138 gripper_keyboard.max_effort -= 1
00139 rospy.loginfo('Decrease max_effort to: {}'
00140 .format(gripper_keyboard.max_effort))
00141 elif c == 'E':
00142 gripper_keyboard.max_effort += 1
00143 rospy.loginfo('Increase max_effort to: {}'
00144 .format(gripper_keyboard.max_effort))
00145 elif c == '?':
00146 print(usage)
00147 else:
00148 if c == '\x03':
00149 break
00150 finally:
00151 termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)