st_get_pcl.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import roslib; roslib.load_manifest('iri_common_smach')
00004 import rospy
00005 import smach
00006 import smach_ros
00007 import threading
00008 import time
00009 
00010 from sensor_msgs.msg import PointCloud2
00011 
00012 class GetPCL(smach.State):
00013     """
00014     Custom smach for reading a PCL from a topic
00015     """
00016     def __init__(self, pcl_topic = ''):
00017         smach.State.__init__(self, outcomes=['success','fail'],
00018                                    input_keys  = ['pcl_topic'],
00019                                    output_keys = ['pcl_RGB'])
00020 
00021         self._last_pcl_RGB     = None
00022         self._pcl_RGB_recieved = False
00023         self.mutex             = threading.Lock()
00024         self.subscriber        = None
00025         if (pcl_topic != ''):
00026             self.subscriber        = rospy.Subscriber(pcl_topic, PointCloud2,
00027                                                       self.recieve_pcl_callback)
00028 
00029     def recieve_pcl_callback(self, data):
00030         self.mutex.acquire()
00031         self._last_pcl_RGB     = data
00032         self._pcl_RGB_recieved = True
00033         self.mutex.release()
00034 
00035     def execute(self, userdata):
00036         # Check if constructor was used
00037         if (self.subscriber == None):
00038             self.subscriber = rospy.Subscriber(userdata.pcl_topic, PointCloud2,
00039                                                self.recieve_pcl_callback)
00040         # wait for a maximum of 5 seconds
00041         for i in range(0, 50):
00042             self.mutex.acquire()
00043             if self._pcl_RGB_recieved:
00044                 # Ok PCL recieved
00045                 userdata.pcl_RGB = self._last_pcl_RGB;
00046                 self.mutex.release()
00047                 return 'success'
00048             self.mutex.release()
00049 
00050             time.sleep(.1)
00051 
00052         # we didn't get PCL in the 5 sec
00053         rospy.loginfo("Waiting more than 5 seconds for PCL")
00054         return 'fail'
00055 


iri_common_smach
Author(s): Jose Luis Rivero
autogenerated on Fri Dec 6 2013 21:05:19