labeled_cloud_pub_service.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 #
00003 # Copyright (c) 2010, Georgia Tech Research Corporation
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions are met:
00008 #     * Redistributions of source code must retain the above copyright
00009 #       notice, this list of conditions and the following disclaimer.
00010 #     * Redistributions in binary form must reproduce the above copyright
00011 #       notice, this list of conditions and the following disclaimer in the
00012 #       documentation and/or other materials provided with the distribution.
00013 #     * Neither the name of the Georgia Tech Research Corporation nor the
00014 #       names of its contributors may be used to endorse or promote products
00015 #       derived from this software without specific prior written permission.
00016 #
00017 # THIS SOFTWARE IS PROVIDED BY GEORGIA TECH RESEARCH CORPORATION ''AS IS'' AND
00018 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 # DISCLAIMED. IN NO EVENT SHALL GEORGIA TECH BE LIABLE FOR ANY DIRECT, INDIRECT,
00021 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00025 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00026 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 #
00028 
00029 #  \author Jason Okerman (Healthcare Robotics Lab, Georgia Tech.)
00030 
00031 import roslib; roslib.load_manifest('pr2_clutter_helper')
00032 from pr2_lcs_helper.srv import *
00033 import rospy
00034 from std_msgs.msg import String
00035 from sensor_msgs.msg import PointCloud
00036 from sensor_msgs.msg import ChannelFloat32
00037 from geometry_msgs.msg import Point32
00038 
00039 '''
00040     This function publishes a sensor_msgs.msg.PointCloud object for rviz
00041     It doesn't import anything but generates points in a rectangular region and 
00042     lets this region move accross the screen over time. 
00043 
00044     The frame is '/my_frame', so rviz view frame must be changed accordingly.
00045     
00046     NOT FINISHED YET, have not tested -- hope to soon!
00047 
00048 '''
00049 
00050 pub=''
00051 
00052 def handle_service_request(req):
00053     #<generate data to send in this function> 
00054     cloud = req.cloud
00055     try:
00056         #cloud = local_rand_cloud()
00057         #cloud.header.frame_id = '/my_frame'
00058         #cloud.header.stamp = rospy.Time.now()
00059         print 'sending cloud at time', cloud.header.stamp, '&', rospy.Time.now()
00060         global pub
00061         pub.publish(cloud)
00062         return  True 
00063     except:
00064         return  False
00065 
00066 
00067 def start_server():
00068     rospy.init_node('cloud_broadcaster_node')
00069     s = rospy.Service('pub_cloud_service', CloudPub, handle_service_request)
00070     print "Ready to publish cloud"
00071     
00072     global pub
00073     pub = start_publisher()
00074     
00075     rospy.spin()
00076     #rospy.sleep(.5) #possibly publish periodically
00077 
00078 
00079 def start_publisher():
00080     #global pub
00081     pub = rospy.Publisher('my_cloud_chatter', PointCloud)
00082     #rospy.init_node('point_cloud_broadcaster')
00083     ## while not rospy.is_shutdown():
00084     
00085     #cloud = local_rand_cloud()
00086     #cloud.header.frame_id = '/my_frame'
00087     #cloud.header.stamp = rospy.Time.now()
00088     #print 'NOT YET sending cloud at time', cloud.header.stamp
00089     #pub.publish(cloud)
00090     
00091     ## rospy.sleep(1) #one second
00092     return pub
00093            
00094        
00095 if __name__ == '__main__':
00096     try:
00097         # talker()
00098         start_server()
00099         
00100     except rospy.ROSInterruptException: pass
00101     
00102 '''
00103 def local_rand_cloud()
00104     ### Create a new cloud object and stuff it
00105     from random import *
00106 
00107     ROS_pointcloud = PointCloud() #sensor_msgs.msg.PointCloud()
00108     x = 0 #const offset easy to change.
00109     
00110     #stuff point cloud with random points in a rectangle which drifts over time.
00111     #Add an extra intensity channel as well.
00112     intensity_channel = ChannelFloat32()
00113     intensity_channel.name = 'intensities'
00114     for i in range(3000):
00115         intensity_channel.values += [random()]
00116    
00117     for i in range(3000):
00118         ROS_pointcloud.points += [Point32(random()*5-x,random(), intensity_channel.values[i])]
00119     #rgb color has maximum value of 0xFFFFFF
00120     rgb_channel = ChannelFloat32()
00121     rgb_channel.name = 'rgb'
00122     rgb_channel.values = normList(intensity_channel.values, 0xFF)
00123     
00124     #Separate r,g,b channels range betweeon 0 and 1.0.
00125     r, g, b = [], [], []
00126     for pt in ROS_pointcloud.points: 
00127         b += [pt.y]; 
00128         r += [pt.z];
00129         g += [0];
00130     r_channel = ChannelFloat32(name='r', values = normList(r) )
00131     g_channel = ChannelFloat32(name='g', values = normList(g) )
00132     b_channel = ChannelFloat32(name='b', values = b)
00133     
00134     ROS_pointcloud.channels = (intensity_channel, rgb_channel, r_channel, g_channel, b_channel)
00135     return ROS_pointcloud
00136     
00137 def normList(L, normalizeTo=1):
00138     ### normalize values of a list to make its max = normalizeTo
00139     vMax = max(L)
00140     if vMax == 0: vMax = 1; #avoid divide by zero
00141     return [ x/(vMax*1.0)*normalizeTo for x in L]    
00142     
00143 '''
00144     
00145     
00146 


pr2_clutter_helper
Author(s): Jason Okerman, Advisors: Prof. Charlie Kemp and Jim Regh, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 11:53:06