lc_gripper_cmder.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 import roslib; roslib.load_manifest('life_test')
00004 import rospy
00005 from std_msgs.msg import Float64
00006 from sensor_msgs.msg import JointState
00007 import time
00008 import sys
00009 
00010 from pr2_controller_manager import pr2_controller_manager_interface
00011 
00012 class LastMessage():
00013     def __init__(self, topic, msg_type):
00014         self.msg = None
00015         rospy.Subscriber(topic, msg_type, self.callback)
00016 
00017     def last(self):
00018         return self.msg
00019 
00020     def callback(self, msg):
00021         self.msg = msg
00022 
00023 start_pos = 0
00024 end_pos = 0
00025 
00026 fwd = -0.008
00027 bck = 0.008
00028 stop = 0
00029 eff_list = []
00030 pos_list = []
00031 
00032 controller_name = None
00033 
00034 def stop_controller():    
00035     print "Please wait, stopping controller..."
00036     global controller_name
00037     if controller_name is not None:
00038         pr2_controller_manager_interface.stop_controller(controller_name)
00039     print "Done."
00040 
00041 
00042 def calibrate():
00043     calibration = False    
00044     global controller_name 
00045     controller_name = rospy.myargv()[1]
00046     control_topic = '%s/command' % controller_name
00047     pub = rospy.Publisher(control_topic, Float64) 
00048     rospy.init_node('calibration')
00049     rospy.on_shutdown(stop_controller) 
00050 
00051     pr2_controller_manager_interface.start_controller(controller_name)    
00052 
00053     while calibration == False:
00054        
00055         last_state = LastMessage('joint_states', JointState)
00056         msg = last_state.last() 
00057 
00058         rospy.sleep(0.4)
00059         pub.publish(Float64(-.0025)) 
00060         rospy.sleep(8.5)
00061         pub.publish(Float64(stop))
00062         
00063         msg = last_state.last()
00064         end_pos = msg.position[0]
00065         print "end position =", end_pos
00066 
00067         start_pos = end_pos + (0.010914138798)
00068         print "start position =", start_pos
00069         
00070         calibration = True
00071         sleep = False
00072         fwd_cnt = 0
00073         cycle_cnt = 0
00074         crnt_pos = end_pos
00075         target = start_pos
00076         seize_cnt = 0
00077         new_pos = 0
00078 
00079         while not rospy.is_shutdown():
00080 
00081             if crnt_pos < target and target == start_pos:
00082                 pub.publish(Float64(bck))
00083                 msg = last_state.last()
00084                 crnt_pos = msg.position[0]
00085         
00086                 rospy.sleep(0.02)
00087                 target = start_pos
00088                 fwd_cnt = 0
00089                 seize_cnt += 1
00090                 sleep = True
00091                
00092             if crnt_pos > (target) and sleep == True:
00093                 pub.publish(Float64(0.0)) 
00094                 time.sleep(3.0)
00095                 sleep = False
00096             if crnt_pos > target:
00097                 pub.publish(Float64(fwd))
00098                 msg = last_state.last()
00099                 crnt_pos = msg.position[0]
00100                 new_pos = crnt_pos
00101 
00102                 rospy.sleep(0.02)
00103                 target = end_pos  
00104                 fwd_cnt += 1
00105                 sleep = False
00106                 seize_cnt = 0
00107             if fwd_cnt > 110:
00108                 target = start_pos
00109                 cycle_cnt += 1
00110                 print "grip count =", cycle_cnt        
00111                 sleep = False
00112             if crnt_pos <= target:
00113                 target = start_pos
00114                 sleep = False
00115             if seize_cnt > 200:
00116                 pr2_controller_manager_interface.stop_controller(controller_name)
00117                 rospy.sleep(0.5)
00118                 sys.exit()
00119 
00120               
00121 if __name__ == '__main__':
00122     calibrate()
00123 


life_test
Author(s): Kevin Watts
autogenerated on Sat Dec 28 2013 17:56:37