click_to_if.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import roslib; roslib.load_manifest('projected_lightswitch_interface')
00003 import rospy
00004 import tf
00005 from geometry_msgs.msg import Point, PointStamped, PolygonStamped
00006 from pr2_object_manipulation_msgs.msg import ImageClick
00007 from std_msgs.msg import ColorRGBA, String
00008 from projector_interface.srv import GetCursorStats, DrawPolygon, CircleInhibit
00009 from copy import deepcopy
00010 
00011 from rcommander_interface.behaviors import Behaviors
00012 
00013 polygon_viz = None
00014 
00015 
00016 tfl = None
00017 poly_proxy = None
00018 hilight_proxy = None
00019 clear_hilight_proxy = None
00020 inited = False
00021 
00022 SWITCH_WIDTH  = 0.08
00023 SWITCH_HEIGHT = 0.125
00024 BUTTON_WIDTH  = SWITCH_HEIGHT*2
00025 BUTTON_HEIGHT = SWITCH_HEIGHT*1.25
00026 
00027 WHITE  = ColorRGBA(255,255,255,0)
00028 PURPLE = ColorRGBA(255,0  ,255,0)
00029 HILIGHT_COLOR = PURPLE
00030 # HILIGHT_COLOR = WHITE
00031 
00032 poly_switch = None
00033 poly_on = None
00034 poly_off = None
00035 
00036 behaviors = None
00037 
00038 click_sub = None
00039 
00040 polygons = dict()
00041 
00042 def rectangle(header, width, height, center):
00043         poly = PolygonStamped()
00044         poly.header = deepcopy(header)
00045         x,y,z = center.x,center.y,center.z
00046 
00047         # this is for doing it in image coordinates
00048         # poly.polygon.points.append(Point(x,y-width/2,z-height/2))
00049         # poly.polygon.points.append(Point(x,y+width/2,z-height/2))
00050         # poly.polygon.points.append(Point(x,y+width/2,z+height/2))
00051         # poly.polygon.points.append(Point(x,y-width/2,z+height/2))
00052 
00053         # normal frame alignment
00054         poly.polygon.points.append(Point(x-width/2,y-height/2,z))
00055         poly.polygon.points.append(Point(x+width/2,y-height/2,z))
00056         poly.polygon.points.append(Point(x+width/2,y+height/2,z))
00057         poly.polygon.points.append(Point(x-width/2,y+height/2,z))
00058 
00059 
00060         return poly
00061 
00062 def click_cb(msg):
00063         global poly_switch, poly_on, poly_off
00064         poly_switch = rectangle(msg.ray.header, SWITCH_WIDTH, SWITCH_HEIGHT, msg.ray.direction)
00065         on_center   = deepcopy(msg.ray.direction)
00066         on_center.x -= (SWITCH_WIDTH/2 + BUTTON_WIDTH/2)
00067         on_center.y += BUTTON_HEIGHT/2
00068         poly_on     = rectangle(msg.ray.header, BUTTON_WIDTH, BUTTON_HEIGHT, on_center)
00069         off_center = deepcopy(on_center)
00070         # off_center.y += SWITCH_WIDTH/2 + BUTTON_WIDTH/2
00071         off_center.y -= BUTTON_HEIGHT
00072         poly_off    = rectangle(msg.ray.header, BUTTON_WIDTH, BUTTON_HEIGHT, off_center)
00073 
00074         global polygons
00075         polygons["Light\nSwitch"] = poly_switch
00076         polygons["Turn On"]       = poly_on
00077         polygons["Turn Off"]      = poly_off
00078 
00079         # rospy.loginfo("Sent polygon %s" % str(poly))
00080         polygon_proxy("Light\nSwitch", True, poly_switch, WHITE)
00081         print 'published viz'
00082         # import pdb; pdb.set_trace()
00083         polygon_viz.publish(poly_switch)
00084 
00085         print poly_switch
00086         if inited:
00087                 polygon_proxy("Turn On", True, poly_on, WHITE)
00088                 polygon_proxy("Turn Off", True, poly_off, WHITE)
00089         rospy.loginfo("Sent polygons")
00090 
00091 def if_click_cb(msg):
00092         global click_sub
00093         rospy.loginfo('if click cb')
00094         if click_sub: click_sub.unregister() # this is a terrible hack to ignore multiple click messages
00095         global inited
00096         
00097         polygon_proxy("Turn On", True, poly_on, WHITE)
00098         polygon_proxy("Turn Off", True, poly_off, WHITE)
00099 
00100         print msg.data
00101         if msg.data == "Light\nSwitch":
00102                 inited = True
00103                 polygon_proxy("Turn On", True, poly_on, HILIGHT_COLOR)
00104                 polygon_proxy("Turn Off", True, poly_off, WHITE)
00105         if msg.data == "Turn On":
00106                 behaviors.rocker_on()
00107                 polygon_proxy(msg.data, True, polygons[msg.data], HILIGHT_COLOR)
00108         if msg.data == "Turn Off":
00109                 behaviors.rocker_off()
00110                 polygon_proxy(msg.data, True, polygons[msg.data], HILIGHT_COLOR)
00111 
00112         click_sub = rospy.Subscriber('/clicked_object', String, if_click_cb, queue_size=1)
00113 
00114 def outline_switch():
00115         fake_click = ImageClick()
00116         # fake_click.ray.header.frame_id = '/4x4_0'
00117         fake_click.ray.header.frame_id = 'freeze_frame0'
00118         # fake_click.ray.header.frame_id = 'head_origin'
00119         fake_click.ray.header.stamp = rospy.Time.now()
00120         fake_click.ray.direction.x = -0.11
00121         fake_click.ray.direction.y =  0.0
00122         fake_click.ray.direction.z =  0.0
00123         click_cb(fake_click)
00124 
00125 if __name__ == '__main__':
00126         rospy.init_node('click_to_if')
00127         tfl = tf.TransformListener()
00128         polygon_proxy = rospy.ServiceProxy('/draw_polygon', DrawPolygon)
00129         rospy.loginfo("Waiting for polygon service")
00130         polygon_proxy.wait_for_service()
00131         rospy.loginfo("polygon service ready")
00132 
00133         behaviors = Behaviors()
00134 
00135         polygon_viz = rospy.Publisher('/polygon_viz', PolygonStamped)
00136         
00137         # rospy.loginfo("Waiting for hilight service")
00138         # hilight_proxy = rospy.ServiceProxy("hilight_object")
00139         # rospy.loginfo("hilight service ready")
00140         # rospy.loginfo("waiting for clear hilight service")
00141         # clearhilight_proxy = rospy.ServiceProxy("clear_hilights")
00142         # rospy.loginfo("clear hilight service ready")
00143         outline_switch()
00144         # rospy.Subscriber('/interactive_manipulation_image_click', ImageClick, click_cb)
00145         click_sub = rospy.Subscriber('/clicked_object', String, if_click_cb, queue_size=1)
00146         rospy.spin()


projected_lightswitch_interface
Author(s): Dan Lazewatsky
autogenerated on Mon Oct 6 2014 10:17:19