gripper_keyboard.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (c) 2015, Fetch Robotics 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 are met:
8 #
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # * Neither the name of the Fetch Robotics Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 # ARE DISCLAIMED. IN NO EVENT SHALL FETCH ROBOTICS INC. BE LIABLE FOR ANY
22 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 # Author: Kentaro Wada
30 
31 import select
32 import sys
33 import termios
34 from threading import Lock
35 import tty
36 
37 import actionlib
38 from control_msgs.msg import GripperCommandAction
39 from control_msgs.msg import GripperCommandGoal
40 import rospy
41 from sensor_msgs.msg import JointState
42 
43 
44 def getch():
45  tty.setraw(sys.stdin.fileno())
46  select.select([sys.stdin], [], [], 0)
47  key = sys.stdin.read(1)
48  termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
49  return key
50 
51 
52 class GripperKeyboard(object):
53 
54  OPEN_POSITION = 0.1
55  CLOSED_POSITION = 0
56 
57  def __init__(self):
58  self._lock = Lock()
59 
60  self.max_effort = 20.0
61  self.position = None
62  self._sub_pos = rospy.Subscriber('joint_states', JointState,
63  self._set_state)
64 
65  self.action_name = 'gripper_controller/gripper_action'
67  GripperCommandAction)
68  rospy.loginfo('Waiting for gripper_controller...')
69  self.client.wait_for_server()
70  rospy.loginfo('...connected.')
71 
72  def _set_state(self, joint_state):
73  l_gripper_position = None
74  r_gripper_position = None
75  for joint, pos in zip(joint_state.name, joint_state.position):
76  if joint == 'l_gripper_finger_joint':
77  l_gripper_finger_pos = pos
78  if joint == 'r_gripper_finger_joint':
79  r_gripper_finger_pos = pos
80  with self._lock:
81  self.position = l_gripper_finger_pos + r_gripper_finger_pos
82 
83  def set_position(self, position):
84  goal = GripperCommandGoal()
85  goal.command.max_effort = self.max_effort
86  goal.command.position = position
87  # Fill in the goal here
88  self.client.send_goal(goal)
89  self.client.wait_for_result(rospy.Duration.from_sec(5.0))
90  res = self.client.get_result()
91  with self._lock:
92  self.position = res.position
93 
94  def open(self):
95  self.set_position(self.OPEN_POSITION)
96 
97  def close(self):
98  self.set_position(self.CLOSED_POSITION)
99 
100 
101 if __name__ == '__main__':
102  settings = termios.tcgetattr(sys.stdin)
103 
104  rospy.init_node('grippper_keyboard')
105 
106  gripper_keyboard = GripperKeyboard()
107 
108  gripper_bindings = {
109  'o': 'Open gripper',
110  'c': 'Close gripper',
111  'E': 'Increase max_effort',
112  'e': 'Decrease max_effort',
113  's': 'Show status',
114  '?': 'Show help',
115  }
116  usage = 'Usage: '
117  usage += ''.join('\n {}: {}'.format(k, v)
118  for k, v in gripper_bindings.items())
119  usage += '\n Ctrl-C to quit'
120 
121  try:
122  print(usage)
123  while True:
124  c = getch()
125  if c.lower() in gripper_bindings:
126  if c.lower() == 'o':
127  gripper_keyboard.open()
128  rospy.loginfo('Opened gripper')
129  elif c.lower() == 'c':
130  gripper_keyboard.close()
131  rospy.loginfo('Closed gripper')
132  elif c.lower() == 's':
133  rospy.loginfo('''State:
134  position: {}
135  max_effort: {}'''.format(gripper_keyboard.position,
136  gripper_keyboard.max_effort))
137  elif c == 'e':
138  gripper_keyboard.max_effort -= 1
139  rospy.loginfo('Decrease max_effort to: {}'
140  .format(gripper_keyboard.max_effort))
141  elif c == 'E':
142  gripper_keyboard.max_effort += 1
143  rospy.loginfo('Increase max_effort to: {}'
144  .format(gripper_keyboard.max_effort))
145  elif c == '?':
146  print(usage)
147  else:
148  if c == '\x03':
149  break
150  finally:
151  termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
def _set_state(self, joint_state)


fetch_teleop
Author(s): Michael Ferguson
autogenerated on Mon Feb 28 2022 22:24:06