gripper_keyboard.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Copyright (c) 2015, Fetch Robotics Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions are met:
00008 #
00009 #     * Redistributions of source code must retain the above copyright
00010 #       notice, this list of conditions and the following disclaimer.
00011 #     * Redistributions in binary form must reproduce the above copyright
00012 #       notice, this list of conditions and the following disclaimer in the
00013 #       documentation and/or other materials provided with the distribution.
00014 #     * Neither the name of the Fetch Robotics Inc. nor the names of its
00015 #       contributors may be used to endorse or promote products derived from
00016 #       this software without specific prior written permission.
00017 #
00018 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 # ARE DISCLAIMED. IN NO EVENT SHALL FETCH ROBOTICS INC. BE LIABLE FOR ANY
00022 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00024 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00025 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00026 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00027 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 
00029 # Author: Kentaro Wada
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         # Fill in the goal here
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)


fetch_teleop
Author(s): Michael Ferguson
autogenerated on Sat Apr 27 2019 03:12:18