full_demo.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 University of Arizona 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 import rospy
36 import rospkg
37 import actionlib
38 import yaml
39 from std_msgs.msg import Bool
40 from svenzva_msgs.msg import *
41 from svenzva_msgs.srv import *
42 from trajectory_msgs.msg import JointTrajectoryPoint
43 from control_msgs.msg import JointTrajectoryAction, JointTrajectoryGoal, FollowJointTrajectoryAction, FollowJointTrajectoryGoal
44 from sensor_msgs.msg import JointState
45 from std_msgs.msg import Int32
46 from collections import deque
47 
48 
49 #sends a j6 command as specified in the given yaml file.
50 #files must exist in the config directory of the demo package
51 def js_playback(filename, state_name):
52  global qmap, fkine
53 
54  #2 - execute action on yaml file
55  req = SvenzvaJointGoal()
56  if len(qmap[state_name]) < 6:
57  rospy.logerr("Could not find specified state. Configuration file ill-formed or missing. Aborting.")
58  return
59  req.positions = qmap[state_name]
60 
61  rospy.loginfo("Sending state command...")
62  fkine.send_goal_and_wait(req)
63 
64 
65 def js_cb(data):
66  global joint_states, states_list
67  joint_states = data
68  states_list.append(data)
69 
70 
71 def setup():
72  rospy.init_node('svenzva_pick_place_demo', anonymous=False)
73  global qmap, fkine, joint_states, states_list
74 
75  states_list = deque(maxlen=10)
76 
77  record = rospy.get_param('record_points', False)
78  joint_states = JointState()
79  gripper_client = actionlib.SimpleActionClient('/revel/gripper_action', GripperAction)
80  joint_states_sub = rospy.Subscriber('/joint_states', JointState, js_cb, queue_size=1)
81  fkine = actionlib.SimpleActionClient('/svenzva_joint_action', SvenzvaJointAction)
82  fkine.wait_for_server()
83  rospy.loginfo("Found Trajectory action server")
84 
85  gripper_client.wait_for_server()
86  rospy.loginfo("Found Revel gripper action server")
87 
88  goal = GripperGoal()
89 
90  filename = "full_pick_and_place"
91  fname=""
92  rospack = rospkg.RosPack()
93  path = rospack.get_path('svenzva_demo')
94 
95  rospy.sleep(1)
96  if record:
97 
98  while(not rospy.is_shutdown()):
99  raw_input("Press Enter to save the current joint state to file. Enter q/Q for exit.")
100  saved_js = joint_states
101  name = raw_input("What to name this point: ")
102  if name == 'q' or name == 'Q':
103  rospy.shutdown()
104  f = open(path+"/config/" + filename + ".yaml", "a")
105  ar = []
106  ar.append(saved_js.position[0])
107  ar.append(saved_js.position[1])
108  ar.append(saved_js.position[2])
109  ar.append(saved_js.position[3])
110  ar.append(saved_js.position[4])
111  ar.append(saved_js.position[5])
112  f.write(name + ": " + str(ar) + "\n")
113  f.close()
114  else:
115 
116  f = open(path+"/config/" + filename + ".yaml")
117  qmap = yaml.safe_load(f)
118  f.close()
119 
120  #go to first position
121  js_playback(fname, "home")
122 
123 
124  while not rospy.is_shutdown():
125 
126  """
127  Grab an existing item from location 1
128  """
129  gripper_client.send_goal(open_gripper())
130 
131  js_playback(fname, "retract_p1_2")
132 
133 
134  js_playback(fname, "approach_p1_1")
135  js_playback(fname, "approach_p1_2")
136 
137  js_playback(fname, "grasp_p1")
138 
139  gripper_client.send_goal(close_gripper(500))
140 
141  js_playback(fname, "retract_p1_1")
142  js_playback(fname, "retract_p1_2")
143 
144  js_playback(fname, "hand_off")
145 
146  rospy.sleep(1.0)
147  gripper_client.send_goal(feel_and_open_gripper())
148 
149  rospy.sleep(0.5)
150 
151  """
152  Take an item from human and drop it off at location 2
153  """
154  rospy.sleep(1.0)
155 
156  gripper_client.send_goal(feel_and_close_gripper(200))
157 
158  rospy.sleep(0.1)
159 
160  js_playback(fname, "approach_p2")
161 
162  js_playback(fname, "grasp_p2")
163 
164  gripper_client.send_goal(open_gripper())
165 
166  rospy.sleep(0.5)
167 
168  js_playback(fname, "retract_p2_1")
169  js_playback(fname, "retract_p2_2")
170 
171  js_playback(fname, "hand_off")
172 
173  """
174  Take an item from human and take to location 1
175  """
176  rospy.sleep(1.0)
177 
178  gripper_client.send_goal(feel_and_close_gripper(300))
179 
180 
181  js_playback(fname, "approach_p1_1")
182  js_playback(fname, "grasp_p1")
183 
184  gripper_client.send_goal(open_gripper())
185 
186  rospy.sleep(0.5)
187 
188  js_playback(fname, "retract_p1_1")
189 
190  """
191  Pick up item from location 2 and hand to human
192  """
193 
194 
195  js_playback(fname, "approach_p2")
196 
197  js_playback(fname, "grasp_p2")
198 
199  gripper_client.send_goal(close_gripper(300))
200 
201  rospy.sleep(0.5)
202 
203  js_playback(fname, "retract_p2_2")
204 
205  js_playback(fname, "hand_off")
206 
207  rospy.sleep(1.0)
208 
209  gripper_client.send_goal(feel_and_open_gripper())
210 
211  rospy.sleep(1.0)
212 
213 
214 
215 
216  #old stuff. notably, the gripping current was 400 mA for the 1.2kg box. == .7 Nm
217  # should delete when above script is working
218  """
219  goal.target_action = goal.CLOSE
220  goal.target_current = 400
221  gripper_client.send_goal(goal)
222  rospy.sleep(0.5)
223 
224  js_playback(fname, "lift1")
225  js_playback(fname, "lift2")
226  js_playback(fname, "lift1")
227  js_playback(fname, "grasp")
228 
229 
230  rospy.sleep(0.5)
231  goal.target_action = goal.OPEN
232  gripper_client.send_goal(goal)
233  rospy.sleep(0.5)
234 
235  js_playback(fname, "approach")
236  js_playback(fname, "home")
237 
238 
239 
240  while not rospy.is_shutdown():
241  if gripper_react():
242  goal.target_action = goal.CLOSE
243  goal.target_current = 100
244  gripper_client.send_goal(goal)
245  break
246  rospy.sleep(0.1)
247 
248  """
249 
251  goal = GripperGoal()
252  goal.target_action = goal.OPEN
253  return goal
254 
255 def close_gripper(target_current):
256  goal = GripperGoal()
257  goal.target_action = goal.CLOSE
258  goal.target_current = target_current
259  return goal
260 
262  while not rospy.is_shutdown():
263  if gripper_react():
264  return open_gripper()
265  rospy.sleep(0.1)
266 
267 
268 def feel_and_close_gripper(target_current):
269  while not rospy.is_shutdown():
270  if gripper_react():
271  return close_gripper(target_current)
272  rospy.sleep(0.1)
273 
274 
275 
276 """
277 Returns whether or not the gripper wrist feels a non-negligible external force
278 """
280  global joint_states, states_list
281  delta = 25
282 
283 
284  while not rospy.is_shutdown():
285  avg = 0
286  for state in list(states_list):
287  avg = avg + state.effort[5]
288  avg = avg / len(states_list)
289 
290  if abs(avg - joint_states.effort[5]) > delta:
291  return True
292  return False
293 
294 if __name__ == '__main__':
295  try:
296  setup()
297  except rospy.ROSInterruptException:
298  pass
299 
300 
def open_gripper()
Definition: full_demo.py:250
def js_cb(data)
Definition: full_demo.py:65
def feel_and_open_gripper()
Definition: full_demo.py:261
def close_gripper(target_current)
Definition: full_demo.py:255
def js_playback(filename, state_name)
Definition: full_demo.py:51
def setup()
Definition: full_demo.py:71
def gripper_react()
Definition: full_demo.py:279
def feel_and_close_gripper(target_current)
Definition: full_demo.py:268


svenzva_demo
Author(s): Max Svetlik
autogenerated on Wed Oct 28 2020 03:31:24