scale_interaction.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import rospy
00004 from geometry_msgs.msg import Polygon, Point32, PolygonStamped
00005 from std_msgs.msg import Float32
00006 from threading import Lock
00007 
00008 width_scale = None
00009 height_scale = None
00010 pending_polygon = None
00011 lock = Lock()
00012 
00013 def widthScallCallback(msg):
00014     global width_scale
00015     with lock:
00016         if msg.data == 0:
00017             rospy.logdebug("scale is 0!, we skip it")
00018         else:
00019             width_scale = msg.data
00020         updateForPendingData()
00021     
00022 def heightScallCallback(msg):
00023     global height_scale
00024     with lock:
00025         if msg.data == 0:
00026             rospy.logdebug("scale is 0!, we skip it")
00027         else:
00028             height_scale = msg.data
00029         updateForPendingData()
00030     
00031 def polygonCallback(msg):
00032     global pending_polygon
00033     with lock:
00034         if width_scale and height_scale:
00035             publishResizedPolygon(msg)
00036         else:
00037             pending_polygon = msg
00038 
00039 def updateForPendingData():
00040     global pending_polygon, width_scale, height_scale
00041     if (pending_polygon and width_scale and height_scale
00042         and width_scale != 0 and height_scale != 0):
00043         publishResizedPolygon(pending_polygon)
00044         pending_polygon = None
00045         
00046 def publishResizedPolygon(poly_msg):
00047     global width_scale, height_scale, inverse, pub
00048     if inverse:
00049         Rw = 1.0 / width_scale
00050         Rh = 1.0 / height_scale
00051     else:
00052         Rw = width_scale
00053         Rh = height_scale
00054     for point in poly_msg.polygon.points:
00055         point.x = Rw * point.x
00056         point.y = Rh * point.y
00057     pub.publish(poly_msg)
00058 
00059 rospy.init_node("scale_interaction")
00060 
00061 inverse = rospy.get_param("~inverse", False)
00062 pub = rospy.Publisher("~output", PolygonStamped)
00063 sub_polygon = rospy.Subscriber("~input", PolygonStamped, polygonCallback)
00064 sub_width = rospy.Subscriber("~input/width_scale", Float32, widthScallCallback)
00065 sub_height = rospy.Subscriber("~input/height_scale", Float32, heightScallCallback)
00066 
00067 rospy.spin()


image_view2
Author(s): Kei Okada
autogenerated on Fri Sep 8 2017 03:38:44