gripper_action_example.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import sys
3 import argparse
4 
5 import rospy
6 import time
7 import actionlib
8 from control_msgs.msg import (
9  GripperCommandAction,
10  GripperCommandGoal
11 )
12 import math
13 
14 CONTROL_L = 0
15 CONTROL_R = 1
16 
17 class GripperClient(object):
18  def __init__(self):
19  self._clientR = actionlib.SimpleActionClient("/sciurus17/controller1/right_hand_controller/gripper_cmd",GripperCommandAction)
20  self._clientL = actionlib.SimpleActionClient("/sciurus17/controller2/left_hand_controller/gripper_cmd",GripperCommandAction)
21 
22  self._goalR = GripperCommandGoal()
23  self._goalL = GripperCommandGoal()
24 
25  # Wait 10 Seconds for the gripper action server to start or exit
26  self._clientR.wait_for_server(rospy.Duration(5.0))
27  if not self._clientR.wait_for_server(rospy.Duration(5.0)):
28  rospy.logerr("Exiting - Gripper R Action Server Not Found")
29  rospy.signal_shutdown("Action Server not found")
30  sys.exit(1)
31  self.clear()
32 
33  # Wait 10 Seconds for the gripper action server to start or exit
34  self._clientL.wait_for_server(rospy.Duration(5.0))
35  if not self._clientL.wait_for_server(rospy.Duration(5.0)):
36  rospy.logerr("Exiting - Gripper L Action Server Not Found")
37  rospy.signal_shutdown("Action Server not found")
38  sys.exit(1)
39  self.clear()
40 
41 
42  def command(self, position, effort, type):
43  if type == CONTROL_L:
44  self._goalL.command.position = position
45  self._goalL.command.max_effort = effort
46  self._clientL.send_goal(self._goalL,feedback_cb=self.feedbackL)
47  if type == CONTROL_R:
48  self._goalR.command.position = position
49  self._goalR.command.max_effort = effort
50  self._clientR.send_goal(self._goalR,feedback_cb=self.feedbackR)
51 
52  def feedbackL(self,msg):
53  print("feedbackL")
54  print(msg)
55 
56  def feedbackR(self,msg):
57  print("feedbackR")
58  print(msg)
59 
60  def stop(self):
61  self._clientL.cancel_goal()
62  self._clientR.cancel_goal()
63 
64  def wait(self, type, timeout=0.1 ):
65  if type == CONTROL_L:
66  self._clientL.wait_for_result(timeout=rospy.Duration(timeout))
67  return self._clientL.get_result()
68  if type == CONTROL_R:
69  self._clientR.wait_for_result(timeout=rospy.Duration(timeout))
70  return self._clientR.get_result()
71 
72  def clear(self):
73  self._goalL = GripperCommandGoal()
74  self._goalR = GripperCommandGoal()
75 
76 def main():
77  arg_fmt = argparse.RawDescriptionHelpFormatter
78  parser = argparse.ArgumentParser(formatter_class=arg_fmt,
79  description=main.__doc__)
80 
81  print("Initializing node... ")
82  rospy.init_node("gipper_action_client")
83 
84  gc = GripperClient()
85 
86  # Open grippers
87  print("Test - Open Grippers")
88  gripper = 26.0
89  gc.command(math.radians(gripper),0.1,CONTROL_R)
90  gripper = -26.0
91  gc.command(math.radians(gripper),0.1,CONTROL_L)
92  result = gc.wait(1.0,CONTROL_R)
93  print(result)
94  result = gc.wait(1.0,CONTROL_L)
95  print(result)
96  time.sleep(1)
97 
98  # Close grippers
99  print("Test - Close Grippers")
100  gripper = 0.0
101  gc.command(math.radians(gripper),0.5,CONTROL_R)
102  gripper = 0.0
103  gc.command(math.radians(gripper),0.5,CONTROL_L)
104  result = gc.wait(CONTROL_R,1.0)
105  print(result)
106  result = gc.wait(CONTROL_L,1.0)
107  print(result)
108  time.sleep(1)
109 
110  print("Exiting - Gripper Action Test Example Complete")
111 
112 if __name__ == "__main__":
113  main()
def command(self, position, effort, type)


sciurus17_examples
Author(s): Daisuke Sato , Hiroyuki Nomura
autogenerated on Sun Oct 2 2022 02:21:39