pcl_topic_to_request.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import roslib; roslib.load_manifest('iri_deformable_analysis')
00004 import rospy
00005 import sys
00006 import threading
00007 import time
00008 
00009 from iri_deformable_analysis.srv import DoDeformableAnalysis, DoDeformableAnalysisRequest
00010 from sensor_msgs.msg import PointCloud2
00011 from std_msgs.msg import Int8, Int32, String
00012 
00013 
00014 class GetPCL():
00015     def __init__(self, pcl_topic, service_topic):
00016         self._last_pcl_RGB     = None
00017         self._pcl_RGB_recieved = False
00018         self.mutex             = threading.Lock()
00019         self.subscriber        = rospy.Subscriber(pcl_topic, PointCloud2,
00020                                                   self.recieve_pcl_callback)
00021         self.srv_topic         = service_topic
00022 
00023     def recieve_pcl_callback(self, data):
00024         self.mutex.acquire()
00025         rospy.loginfo(" <- PCL recieved from topic")
00026         self._last_pcl_RGB     = data
00027         self._pcl_RGB_recieved = True
00028         self.subscriber.unregister() # we only want one message
00029         self.mutex.release()
00030 
00031     def execute(self):
00032         # wait for a maximum of 5 seconds
00033         for i in range(0, 50):
00034             self.mutex.acquire()
00035             if self._pcl_RGB_recieved:
00036                 # Ok PCL recieved
00037                 self.send_pcl(self._last_pcl_RGB)
00038                 self.mutex.release()
00039                 return
00040 
00041             self.mutex.release()
00042             time.sleep(.1)
00043 
00044         # we didn't get PCL in the 5 sec
00045         rospy.loginfo("Waiting more than 5 seconds for PCL")
00046         return 'fail'
00047 
00048     def send_pcl(self, pcl):
00049         # Config and launch the deformable analysis
00050         handler = rospy.ServiceProxy(self.srv_topic,
00051                                      DoDeformableAnalysis)
00052 
00053         analysis_request = DoDeformableAnalysisRequest()
00054         analysis_request.pcl_to_analyze = pcl
00055         # TODO: define the fusion criteria here
00056         #analysis_request.fusion_criteria_id = userdata.sm_pomdp_config.best_pose_algorithm_id
00057 
00058         try:
00059             rospy.loginfo(" -> PCL sent to service request")
00060             response = handler(analysis_request)
00061 
00062             if (not response):
00063                 return 'fail'
00064 
00065         except rospy.ServiceException, e:
00066             return 'timeout'
00067 
00068         pose_st = response.deformable_analysis.graspability.best_grasp_pose
00069 
00070         return 'success'
00071 
00072 
00073 def main():
00074     if len(sys.argv) < 2:
00075         sys.exit('Usage: %s pcl_input_topic deformable_srv' % sys.argv[0])
00076 
00077     rospy.init_node("pcl_2_deformable_request")
00078 
00079     getter = GetPCL(sys.argv[1], sys.argv[2])
00080     getter.execute()
00081 
00082     rospy.spin()
00083 
00084 if __name__ == "__main__":
00085     main()


iri_deformable_analysis
Author(s): Pol Monso, Arnau Ramisa, Jose Luis Rivero
autogenerated on Fri Dec 6 2013 21:15:35