gripper_action_example.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright 2018 RT Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 import sys
18 import argparse
19 
20 import rospy
21 import time
22 import actionlib
23 from control_msgs.msg import (
24  GripperCommandAction,
25  GripperCommandGoal
26 )
27 import math
28 
29 CONTROL_R = 0
30 CONTROL_L = 1
31 
32 class GripperClient(object):
33  def __init__(self):
34  self._clientR = actionlib.SimpleActionClient("/sciurus17/controller1/right_hand_controller/gripper_cmd",GripperCommandAction)
35  self._clientL = actionlib.SimpleActionClient("/sciurus17/controller2/left_hand_controller/gripper_cmd",GripperCommandAction)
36 
37  self._goalR = GripperCommandGoal()
38  self._goalL = GripperCommandGoal()
39 
40  # Waits 10 Seconds for the gripper action server to start or exit
41  self._clientR.wait_for_server(rospy.Duration(5.0))
42  if not self._clientR.wait_for_server(rospy.Duration(5.0)):
43  rospy.logerr("Exiting - Gripper R Action Server Not Found")
44  rospy.signal_shutdown("Action Server not found")
45  sys.exit(1)
46  self.clear()
47 
48  # Waits 10 Seconds for the gripper action server to start or exit
49  self._clientL.wait_for_server(rospy.Duration(5.0))
50  if not self._clientL.wait_for_server(rospy.Duration(5.0)):
51  rospy.logerr("Exiting - Gripper L Action Server Not Found")
52  rospy.signal_shutdown("Action Server not found")
53  sys.exit(1)
54  self.clear()
55 
56 
57  def command(self, position, effort, type):
58  if type == CONTROL_R:
59  self._goalR.command.position = position
60  self._goalR.command.max_effort = effort
61  self._clientR.send_goal(self._goalR,feedback_cb=self.feedbackR)
62  if type == CONTROL_L:
63  self._goalL.command.position = position
64  self._goalL.command.max_effort = effort
65  self._clientL.send_goal(self._goalL,feedback_cb=self.feedbackL)
66 
67  def feedbackR(self,msg):
68  print("feedbackR")
69  print(msg)
70 
71  def feedbackL(self,msg):
72  print("feedbackL")
73  print(msg)
74 
75  def stop(self):
76  self._clientR.cancel_goal()
77  self._clientL.cancel_goal()
78 
79  def wait(self, type, timeout=0.1 ):
80  if type == CONTROL_R:
81  self._clientR.wait_for_result(timeout=rospy.Duration(timeout))
82  return self._clientR.get_result()
83  if type == CONTROL_L:
84  self._clientL.wait_for_result(timeout=rospy.Duration(timeout))
85  return self._clientL.get_result()
86 
87  def clear(self):
88  self._goalR = GripperCommandGoal()
89  self._goalL = GripperCommandGoal()
90 
91 def main():
92  arg_fmt = argparse.RawDescriptionHelpFormatter
93  parser = argparse.ArgumentParser(formatter_class=arg_fmt,
94  description=main.__doc__)
95 
96  print("Initializing node... ")
97  rospy.init_node("gipper_action_client")
98 
99  gc = GripperClient()
100 
101  # Open grippers
102  print("Test - Open Grippers")
103  gripper = 26.0
104  gc.command(math.radians(gripper),0.1,CONTROL_R)
105  gripper = -26.0
106  gc.command(math.radians(gripper),0.1,CONTROL_L)
107  result = gc.wait(CONTROL_R,1.0)
108  print(result)
109  result = gc.wait(CONTROL_L,1.0)
110  print(result)
111  time.sleep(1)
112 
113  # Close grippers
114  print("Test - Close Grippers")
115  gripper = 0.0
116  gc.command(math.radians(gripper),0.5,CONTROL_R)
117  gripper = 0.0
118  gc.command(math.radians(gripper),0.5,CONTROL_L)
119  result = gc.wait(CONTROL_R,1.0)
120  print(result)
121  result = gc.wait(CONTROL_L,1.0)
122  print(result)
123  time.sleep(1)
124 
125  print("Exiting - Gripper Action Test Example Complete")
126 
127 if __name__ == "__main__":
128  main()
gripper_action_example.GripperClient.feedbackR
def feedbackR(self, msg)
Definition: gripper_action_example.py:67
gripper_action_example.GripperClient.command
def command(self, position, effort, type)
Definition: gripper_action_example.py:57
gripper_action_example.GripperClient.__init__
def __init__(self)
Definition: gripper_action_example.py:33
gripper_action_example.GripperClient.stop
def stop(self)
Definition: gripper_action_example.py:75
actionlib::SimpleActionClient
gripper_action_example.GripperClient._clientL
_clientL
Definition: gripper_action_example.py:35
gripper_action_example.GripperClient.wait
def wait(self, type, timeout=0.1)
Definition: gripper_action_example.py:79
gripper_action_example.GripperClient._goalR
_goalR
Definition: gripper_action_example.py:37
gripper_action_example.main
def main()
Definition: gripper_action_example.py:91
gripper_action_example.GripperClient.feedbackL
def feedbackL(self, msg)
Definition: gripper_action_example.py:71
gripper_action_example.GripperClient._clientR
_clientR
Definition: gripper_action_example.py:34
gripper_action_example.GripperClient.clear
def clear(self)
Definition: gripper_action_example.py:87
gripper_action_example.GripperClient
Definition: gripper_action_example.py:32
gripper_action_example.GripperClient._goalL
_goalL
Definition: gripper_action_example.py:38


sciurus17_examples
Author(s): Daisuke Sato , Hiroyuki Nomura
autogenerated on Fri Aug 2 2024 08:37:20