gripper_monitor_service.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 import roslib; roslib.load_manifest("pr2_laser_pointer_grasp")
00003 import rospy
00004 import copy
00005 
00006 from std_msgs.msg import Bool, Float64
00007 from pr2_msgs.msg import PressureState
00008 from pr2_laser_pointer_grasp.srv import GripperMonitor
00009 from numpy import sqrt
00010 
00011 def loginfo(str):
00012     rospy.loginfo("PressureMonitor: " + str)
00013 
00014 class PressureMonitor():
00015 
00016     def __init__(self, grip_top, bias_thresh):
00017         self.bias_pub = rospy.Publisher("/pressure/" + grip_top + "/bias_dist", Float64)
00018 
00019         self.NUM_SENSORS = 22
00020         self.SENSOR_WEIGHTS = [1.0] * self.NUM_SENSORS 
00021         if bias_thresh == 0.0:
00022             self.BIAS_DIST_THRESH = 900.0
00023         else:
00024             self.BIAS_DIST_THRESH = bias_thresh
00025         self.BIAS_TIME = 0.3 
00026         self.setup()
00027 
00028     def setup(self):
00029         self.l_bias_sum = [0.0] * self.NUM_SENSORS
00030         self.r_bias_sum = [0.0] * self.NUM_SENSORS
00031         self.l_bias = [0.0] * self.NUM_SENSORS
00032         self.r_bias = [0.0] * self.NUM_SENSORS
00033         self.bias_done = False
00034         self.st_bias_time = None 
00035         self.num_samples = 0
00036         self.pressure_trigger = False
00037 
00038     def bias_dist(self, x, y):
00039         def subsq(a, b): return (a - b) ** 2
00040         return sqrt(sum(map(subsq,x,y)))
00041 
00042     def pressure_biaser(self, msg):
00043         if self.st_bias_time is None:
00044             self.st_bias_time = rospy.Time.now().to_sec()
00045             self.l_bias_sum = copy.copy(msg.l_finger_tip)
00046             self.r_bias_sum = copy.copy(msg.r_finger_tip)
00047             self.num_samples = 1
00048         elapsed = rospy.Time.now().to_sec() - self.st_bias_time
00049         if elapsed > self.BIAS_TIME:
00050             def div(x): return x/self.num_samples
00051             self.l_bias = map(div, self.l_bias_sum)
00052             self.r_bias = map(div, self.r_bias_sum)
00053             self.bias_done = True
00054         def add(x,y): return x+y
00055         self.l_bias_sum = map(add, self.l_bias_sum, msg.l_finger_tip)
00056         self.r_bias_sum = map(add, self.r_bias_sum, msg.r_finger_tip)
00057         self.num_samples += 1
00058 
00059     def pressure_monitor(self, msg):
00060         total_bias_dist = (self.bias_dist(msg.l_finger_tip,self.l_bias) + 
00061                                 self.bias_dist(msg.r_finger_tip,self.r_bias))
00062         self.bias_pub.publish(total_bias_dist)
00063 #       import pdb; pdb.set_trace()
00064         if total_bias_dist > self.BIAS_DIST_THRESH:
00065             self.pressure_trigger = True
00066 
00067 def monitor(msg):
00068     pm = PressureMonitor(msg.gripper_topic, msg.bias_thresh)
00069     pbsub = rospy.Subscriber('/pressure/' + msg.gripper_topic, PressureState, pm.pressure_biaser)
00070     loginfo("Subscribing to " + msg.gripper_topic + ", biasing starting")
00071     while not pm.bias_done:
00072         rospy.sleep(0.4)
00073     pbsub.unregister()
00074     loginfo("Biasing complete, monitoring...")
00075     pmsub = rospy.Subscriber('/pressure/' + msg.gripper_topic, PressureState, pm.pressure_monitor)
00076     while not pm.pressure_trigger:
00077         rospy.sleep(0.1)
00078     loginfo("Pressure difference detected!")
00079     pmsub.unregister()
00080     pm.setup()
00081     return True   
00082 
00083 if __name__ == "__main__":
00084     rospy.init_node('gripper_monitor_service')
00085     svc = rospy.Service('gripper_monitor', GripperMonitor, monitor)
00086     loginfo("Offering gripper_monitor service.")
00087     rospy.spin()


hrl_pr2_lib
Author(s): haidai
autogenerated on Wed Nov 27 2013 11:40:34