revel_gripper_server.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2017 Svenzva Robotics
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of Svenzva Robotics LLC nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 
34 """
35 Svenzva's Revel Gripper action interface
36 
37 Opening and closing are performed at fixed torque rates.
38 The 'target_current' value in the action goal is only used in the event that
39 'target_action' is 'close'. In which case, after the fingers are touching the target object,
40 the 'target_current' torque value is applied.
41 
42 
43 Author: Maxwell Svetlik
44 """
45 from __future__ import division
46 from threading import Thread
47 
48 import rospy
49 import actionlib
50 
51 from std_msgs.msg import Float64, Int32
52 from svenzva_msgs.msg import MotorState, MotorStateList, GripperFeedback, GripperResult, GripperAction
53 
55 
56  _feedback = GripperFeedback()
57  _result = GripperResult()
58 
59  def __init__(self, controller_namespace, mx_io):
60  self.mx_io = mx_io
61  self.motor_id = 7
62  self.closing_force = 150 #mA
63  self.opening_force = -200 #mA
64  self.moving_distance = 2.0 #rad
65  self.open_position = -2.1
66 
67  self.motor_state = MotorState()
68  rospy.Subscriber("revel/motor_states", MotorStateList, self.motor_state_cb, queue_size=1)
69 
70  self._as = actionlib.SimpleActionServer("revel/gripper_action", GripperAction, execute_cb=self.gripper_cb, auto_start = False)
71  self._as.start()
72  self._result = GripperResult()
73  rospy.on_shutdown(self.shutdown)
74 
75 
76  def gripper_cb(self, goal):
77  r = rospy.Rate(0.5)
78  success = True
79 
80  if goal.target_action == goal.OPEN:
81  self._feedback.action = 'opening'
82  elif goal.target_action == goal.CLOSE:
83  self._feedback.action = 'closing'
84  else:
85  rospy.logerr("Unable to perform gripper action due to malformed command. Expecting [open, close], got: %s", goal.target_action)
86  self._result.success = False
87  self._as.set_aborted(self._result)
88  return
89 
90  self._as.publish_feedback(self._feedback)
91  rospy.loginfo("Executing gripper action.")
92 
93  if goal.target_action == goal.OPEN:
94  self.open_gripper(self.opening_force)
95  elif goal.target_action == goal.CLOSE:
96  #close fingers gently until they have met the target object
97  self.close_gripper(self.closing_force)
98  rospy.sleep(0.5)
99  self._feedback.action = 'grasping'
100  self._as.publish_feedback(self._feedback)
101  self.close_gripper(goal.target_current)
102 
103  if self._as.is_preempt_requested():
104  self._as.set_preempted()
105  self._result.success = False
106  return
107  self._result.success = True
108  self._as.set_succeeded(self._result)
109 
110  def motor_state_cb(self, data):
111  self.motor_state = data.motor_states[self.motor_id - 1]
112 
113  def start(self, open_and_close=False):
114  #wait for motor_state
115  rospy.sleep(0.2)
116  self.initial_pos = self.motor_state.position
117  if open_and_close:
118 
119  #close gripper completely
120  self.mx_io.set_torque_goal(self.motor_id, self.closing_force)
121 
122  #wait for fingers to close
123  rospy.sleep(3.0)
124 
125  #open gripper the initial_distance
126  self.mx_io.set_torque_goal(self.motor_id, self.opening_force)
127 
128  start = rospy.Time.now()
129  while( self.motor_state.position > self.open_position):
130  rospy.sleep(0.05)
131  if rospy.Time.now() - start > rospy.Duration(0.25):
132  pass
133 
134  #stop movement
135  self.mx_io.set_torque_goal(self.motor_id, 0)
136 
137 
138  def open_gripper(self, force):
139  cur_pos = 0 #self.motor_state.position
140  self.mx_io.set_torque_goal(self.motor_id, force)
141 
142  while( self.motor_state.position > self.open_position):
143  if self._as.is_preempt_requested():
144  rospy.loginfo('Gripper action preempted.')
145  self._as.set_preempted()
146  break
147  rospy.sleep(0.02)
148 
149  #stop movement
150  self.mx_io.set_torque_goal(self.motor_id, 0)
151 
152 
153  def close_gripper(self, force):
154  self.mx_io.set_torque_goal(self.motor_id, force)
155 
156  def shutdown(self):
157  self.mx_io.set_torque_goal(self.motor_id,0)


svenzva_drivers
Author(s): Max Svetlik
autogenerated on Wed Oct 28 2020 03:31:27